diff --git a/examples/hello_world.fx b/examples/hello_world.fx index 9a48552..e51bafe 100644 --- a/examples/hello_world.fx +++ b/examples/hello_world.fx @@ -1,4 +1,6 @@ fn main() { - - } \ No newline at end of file +} + +fn fib() { +} \ No newline at end of file diff --git a/src/lexer/mod.rs b/src/lexer/mod.rs index 5c8b774..c56952a 100644 --- a/src/lexer/mod.rs +++ b/src/lexer/mod.rs @@ -108,7 +108,7 @@ pub fn first_token(input: &str) -> Token { pub fn is_whitespace(c: char) -> bool { match c { - ' ' => true, + ' ' | '\n' | '\r' | '\t' => true, _ => false, } } diff --git a/src/parser/mod.rs b/src/parser/mod.rs index d57b7c8..430b66f 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -13,8 +13,13 @@ pub struct Parser { impl Parser { pub fn new(tokens: Vec) -> Parser { + // FIXME: Fiter without collecting? + let tokens_without_whitespace: Vec = tokens + .into_iter() + .filter(|token| token.kind != TokenKind::Whitespace) + .collect(); Parser { - tokens: tokens.into_iter().peekable(), + tokens: tokens_without_whitespace.into_iter().peekable(), peeked: vec![], } } @@ -61,14 +66,6 @@ impl Parser { } fn match_token(&mut self, token_kind: TokenKind) -> Result { - loop { - match self.peek().expect("Failed to peek token").kind { - TokenKind::Whitespace | TokenKind::Tab | TokenKind::CarriageReturn => { - self.next_token(); - } - _ => break, - } - } match self.next() { Some(token) if token.kind == token_kind => Ok(token), other => Err(format!( @@ -90,6 +87,10 @@ impl Parser { fn match_identifier_kind(&mut self, identifier_kind: IdentifierKind) -> Result<(), String> { let token = self.next_token(); + println!( + "match_identifier_kind: Token: {:?}, identifier_kind: {:?}", + token, identifier_kind + ); match token.kind { TokenKind::Identifier { @@ -133,10 +134,7 @@ impl Parser { let globals = Vec::new(); while self.has_more() { - match self.next_token() { - t if t.kind == TokenKind::Whitespace => continue, - _ => functions.push(self.parse_function().expect("Failed to parse function")), - } + functions.push(self.parse_function().expect("Failed to parse function")) } Ok(Program { @@ -146,17 +144,17 @@ impl Parser { } fn parse_function(&mut self) -> Result { - self.match_identifier_kind(IdentifierKind::Function); + self.match_identifier_kind(IdentifierKind::Function)?; let name = self .match_token(TokenKind::Literal { kind: LiteralKind::Str, })? .raw; - self.match_token(TokenKind::BraceOpen); - self.match_token(TokenKind::BraceClose); - self.match_token(TokenKind::CurlyBracesOpen); - self.match_token(TokenKind::CurlyBracesClose); + self.match_token(TokenKind::BraceOpen)?; + self.match_token(TokenKind::BraceClose)?; + self.match_token(TokenKind::CurlyBracesOpen)?; + self.match_token(TokenKind::CurlyBracesClose)?; Ok(Function { name: name,