diff --git a/TODO.md b/TODO.md index ba39492..c4b4c37 100644 --- a/TODO.md +++ b/TODO.md @@ -13,3 +13,7 @@ a(1,2, c=_): Int -> Int a(_,_,_) : Int -> Int -> Int -> Int + + +- AST : maybe replace the Expression type with "Ascription(TypeName, Box) nodes?? +- parser: add a "debug" field to the Parser struct for all debug-related things diff --git a/src/schala_lang/parsing.rs b/src/schala_lang/parsing.rs index ec81ea8..7112aca 100644 --- a/src/schala_lang/parsing.rs +++ b/src/schala_lang/parsing.rs @@ -346,16 +346,18 @@ pub type ParseResult = Result; pub struct ParseRecord { production_name: String, next_token: String, + level: u32, } struct Parser { tokens: TokenIter, parse_record: Vec, + parse_level: u32 } impl Parser { fn new(input: Vec) -> Parser { - Parser { tokens: input.into_iter().peekable(), parse_record: vec![] } + Parser { tokens: input.into_iter().peekable(), parse_record: vec![], parse_level: 0 } } fn peek(&mut self) -> TokenType { @@ -503,9 +505,16 @@ macro_rules! parse_method { let record = ParseRecord { production_name: stringify!($name).to_string(), next_token: format!("{:?}", next_token), + level: $self.parse_level, }; + $self.parse_level += 1; $self.parse_record.push(record); - $body + let result = { $body }; + + if $self.parse_level != 0 { + $self.parse_level -= 1; + } + result } }; } @@ -749,6 +758,7 @@ impl Parser { let record = ParseRecord { production_name: "precedence_expr".to_string(), next_token: format!("{:?}", next_token), + level: self.parse_level, }; self.parse_record.push(record); @@ -1022,7 +1032,11 @@ pub fn parse(input: Vec) -> (Result, Vec) { let ast = parser.program(); let trace = parser.parse_record.into_iter().map(|r| { - format!("Production `{}`, token: {:?}", r.production_name, r.next_token) + let mut indent = String::new(); + for i in 0..r.level { + indent.push(' '); + } + format!("{}Production `{}`, token: {:?}", indent, r.production_name, r.next_token) }).collect(); (ast, trace) }