Item Id store
This commit is contained in:
parent
b517bc2366
commit
8dc8833eb3
@ -15,11 +15,33 @@ pub struct ItemId {
|
||||
}
|
||||
|
||||
impl ItemId {
|
||||
pub fn new(n: u32) -> ItemId {
|
||||
fn new(n: u32) -> ItemId {
|
||||
ItemId { idx: n }
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ItemIdStore {
|
||||
last_idx: u32
|
||||
}
|
||||
|
||||
impl ItemIdStore {
|
||||
pub fn new() -> ItemIdStore {
|
||||
ItemIdStore { last_idx: 0 }
|
||||
}
|
||||
/// Always returns an ItemId with internal value zero
|
||||
pub fn new_id() -> ItemId {
|
||||
ItemId { idx: 0 }
|
||||
}
|
||||
|
||||
/// This limits the size of the AST to 2^32 tree elements
|
||||
pub fn fresh(&mut self) -> ItemId {
|
||||
let idx = self.last_idx;
|
||||
self.last_idx += 1;
|
||||
ItemId::new(idx)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct Meta<T> {
|
||||
pub n: T,
|
||||
|
@ -183,6 +183,7 @@ pub struct Parser {
|
||||
parse_record: Vec<ParseRecord>,
|
||||
parse_level: u32,
|
||||
restrictions: ParserRestrictions,
|
||||
id_store: ItemIdStore,
|
||||
}
|
||||
|
||||
struct ParserRestrictions {
|
||||
@ -231,7 +232,8 @@ impl Parser {
|
||||
token_handler: TokenHandler::new(initial_input),
|
||||
parse_record: vec![],
|
||||
parse_level: 0,
|
||||
restrictions: ParserRestrictions { no_struct_literal: false }
|
||||
restrictions: ParserRestrictions { no_struct_literal: false },
|
||||
id_store: ItemIdStore::new(),
|
||||
}
|
||||
}
|
||||
|
||||
@ -334,7 +336,7 @@ impl Parser {
|
||||
),
|
||||
}
|
||||
}
|
||||
Ok(AST { id: ItemId::new(0), statements })
|
||||
Ok(AST { id: self.id_store.fresh(), statements })
|
||||
}
|
||||
|
||||
/// `statement := expression | declaration`
|
||||
|
@ -4,7 +4,7 @@ use std::str::FromStr;
|
||||
|
||||
use super::tokenize;
|
||||
use super::ParseResult;
|
||||
use crate::ast::{ItemId, AST, Meta, Expression, Statement, StatementKind, IfExpressionBody, Discriminator, Pattern, PatternLiteral, TypeBody, Enumerator, ForBody, InvocationArgument, FormalParam, PrefixOp, BinOp, QualifiedName};
|
||||
use crate::ast::{ItemIdStore, AST, Meta, Expression, Statement, StatementKind, IfExpressionBody, Discriminator, Pattern, PatternLiteral, TypeBody, Enumerator, ForBody, InvocationArgument, FormalParam, PrefixOp, BinOp, QualifiedName};
|
||||
use super::Declaration::*;
|
||||
use super::Signature;
|
||||
use super::TypeIdentifier::*;
|
||||
@ -20,12 +20,12 @@ fn parse(input: &str) -> ParseResult<AST> {
|
||||
}
|
||||
|
||||
macro_rules! parse_test {
|
||||
($string:expr, $correct:expr) => {
|
||||
($string:expr, $correct:expr) => {
|
||||
assert_eq!(parse($string).unwrap(), $correct)
|
||||
};
|
||||
}
|
||||
macro_rules! parse_test_wrap_ast {
|
||||
($string:expr, $correct:expr) => { parse_test!($string, AST { id: ItemId::new(4), statements: vec![$correct] }) }
|
||||
($string:expr, $correct:expr) => { parse_test!($string, AST { id: ItemIdStore::new_id(), statements: vec![$correct] }) }
|
||||
}
|
||||
macro_rules! parse_error {
|
||||
($string:expr) => { assert!(parse($string).is_err()) }
|
||||
@ -99,7 +99,7 @@ fn parsing_number_literals_and_binexps() {
|
||||
|
||||
parse_test! {"3; 4; 4.3",
|
||||
AST {
|
||||
id: ItemId::new(0),
|
||||
id: ItemIdStore::new_id(),
|
||||
statements: vec![exst!(NatLiteral(3)), exst!(NatLiteral(4)),
|
||||
exst!(FloatLiteral(4.3))]
|
||||
}
|
||||
@ -550,7 +550,7 @@ fn more_advanced_lambdas() {
|
||||
r#"fn wahoo() { let a = 10; \(x) { x + a } };
|
||||
wahoo()(3) "#,
|
||||
AST {
|
||||
id: ItemId::new(0),
|
||||
id: ItemIdStore::new_id(),
|
||||
statements: vec![
|
||||
exst!(s r"fn wahoo() { let a = 10; \(x) { x + a } }"),
|
||||
exst! {
|
||||
|
Loading…
Reference in New Issue
Block a user