From f0e7c9906e158bd73e44a1532cf3a21c06d112bd Mon Sep 17 00:00:00 2001 From: greg Date: Fri, 2 Mar 2018 00:42:52 -0800 Subject: [PATCH] Fixed bug w/ lines in functions Also improved debugging --- source_files/first.schala | 8 +++++++- src/schala_lang/parsing.rs | 11 ++++++++--- src/schala_lang/tokenizing.rs | 2 +- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/source_files/first.schala b/source_files/first.schala index a24a321..4668e14 100644 --- a/source_files/first.schala +++ b/source_files/first.schala @@ -1,2 +1,8 @@ +fn main() { + const a = 10 + const b = 20 + a + b +} + +main() -1 + 2 diff --git a/src/schala_lang/parsing.rs b/src/schala_lang/parsing.rs index 48497ad..304d8d8 100644 --- a/src/schala_lang/parsing.rs +++ b/src/schala_lang/parsing.rs @@ -134,7 +134,9 @@ impl Parser { fn peek(&mut self) -> TokenType { self.tokens.peek().map(|ref t| { t.token_type.clone() }).unwrap_or(TokenType::EOF) } - + fn peek_with_token_offset(&mut self) -> Token { + self.tokens.peek().map(|t: &Token| { t.clone()}).unwrap_or(Token { token_type: TokenType::EOF, offset: 0}) + } fn next(&mut self) -> TokenType { self.tokens.next().map(|ref t| { t.token_type.clone() }).unwrap_or(TokenType::EOF) } @@ -259,7 +261,7 @@ pub struct Pattern(Rc); macro_rules! parse_method { ($name:ident(&mut $self:ident) -> $type:ty $body:block) => { fn $name(&mut $self) -> $type { - let next_token = $self.peek(); + let next_token = $self.peek_with_token_offset(); let record = ParseRecord { production_name: stringify!($name).to_string(), next_token: format!("{:?}", next_token), @@ -405,7 +407,7 @@ impl Parser { parse_method!(func_declaration(&mut self) -> ParseResult { let signature = self.signature()?; if let LCurlyBrace = self.peek() { - let statements = delimited!(self, LCurlyBrace, '{', statement, Newline | Semicolon, RCurlyBrace, '}'); + let statements = delimited!(self, LCurlyBrace, '{', statement, Newline | Semicolon, RCurlyBrace, '}', nonstrict); Ok(Declaration::FuncDecl(signature, statements)) } else { Ok(Declaration::FuncSig(signature)) @@ -982,6 +984,9 @@ mod parse_tests { parse_test!("fn a(x) { x() }", AST(vec![Declaration( FuncDecl(Signature { name: rc!(a), params: vec![(rc!(x),None)], type_anno: None }, vec![exprstatement!(Call { f: bx!(ex!(val!("x"))), arguments: vec![] })]))])); + parse_test!("fn a(x) {\n x() }", AST(vec![Declaration( + FuncDecl(Signature { name: rc!(a), params: vec![(rc!(x),None)], type_anno: None }, + vec![exprstatement!(Call { f: bx!(ex!(val!("x"))), arguments: vec![] })]))])); } #[test] diff --git a/src/schala_lang/tokenizing.rs b/src/schala_lang/tokenizing.rs index bc078bb..ba12cdc 100644 --- a/src/schala_lang/tokenizing.rs +++ b/src/schala_lang/tokenizing.rs @@ -67,7 +67,7 @@ lazy_static! { }; } -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct Token { pub token_type: TokenType, pub offset: usize,