From 6c5dbac406ba480bdf62d165d9a2e443e3990a9f Mon Sep 17 00:00:00 2001 From: greg Date: Fri, 15 Sep 2017 04:23:39 -0700 Subject: [PATCH] Starting to add logic to track recursive descent calls --- src/schala_lang/mod.rs | 1 + src/schala_lang/parsing.rs | 32 +++++++++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/schala_lang/mod.rs b/src/schala_lang/mod.rs index 349c72a..9b60e8d 100644 --- a/src/schala_lang/mod.rs +++ b/src/schala_lang/mod.rs @@ -34,6 +34,7 @@ impl ProgrammingLanguageInterface for Schala { let ast = match parsing::parse(tokens) { Ok(ast) => { if options.debug_parse { + output.add_artifact(TraceArtifact::new("Recursive descent calls:", format!("{:?}", "OI"))); output.add_artifact(TraceArtifact::new("ast", format!("{:?}", ast))); } ast diff --git a/src/schala_lang/parsing.rs b/src/schala_lang/parsing.rs index 7047d74..8159b55 100644 --- a/src/schala_lang/parsing.rs +++ b/src/schala_lang/parsing.rs @@ -333,13 +333,16 @@ impl ParseError { pub type ParseResult = Result; +pub struct ParseRecord(String); + struct Parser { tokens: TokenIter, + parse_record: Vec, } impl Parser { fn new(input: Vec) -> Parser { - Parser { tokens: input.into_iter().peekable() } + Parser { tokens: input.into_iter().peekable(), parse_record: vec![] } } fn peek(&mut self) -> TokenType { @@ -435,7 +438,19 @@ impl Operation { } } +macro_rules! parse_method { + ($name:ident, $self:ident, $type:ty, $body:tt) => { + fn $name(&mut $self) -> $type { + let next_token = $self.peek(); + let record = ParseRecord(format!("Token: {:?}", next_token)); + $self.parse_record.push(record); + $body + } + } +} + impl Parser { + /* fn program(&mut self) -> ParseResult { let mut statements = Vec::new(); loop { @@ -450,6 +465,21 @@ impl Parser { } Ok(AST(statements)) } + */ + parse_method!(program, self, ParseResult, { + let mut statements = Vec::new(); + loop { + match self.peek() { + EOF => break, + Newline | Semicolon => { + self.next(); + continue; + }, + _ => statements.push(self.statement()?), + } + } + Ok(AST(statements)) + }); fn statement(&mut self) -> ParseResult { //TODO handle error recovery here