From fc8c3a3499c27c6540f9ed5f422e9e2cb132dbac Mon Sep 17 00:00:00 2001 From: Erin Date: Tue, 27 Apr 2021 11:57:11 +0200 Subject: [PATCH] Made parser to throw error when unexpected EOF --- able-script-test/parse_test.able | 5 +--- src/parser/item.rs | 2 +- src/parser/mod.rs | 43 ++++++++++++++++++++++++++------ src/variables.rs | 20 +-------------- 4 files changed, 39 insertions(+), 31 deletions(-) diff --git a/able-script-test/parse_test.able b/able-script-test/parse_test.able index efe7b0e..9597bd1 100644 --- a/able-script-test/parse_test.able +++ b/able-script-test/parse_test.able @@ -1,4 +1 @@ -functio test () { - var a = 3; - var b = 4; -} \ No newline at end of file +bff a {+++<<>>>[]]]][[]]} \ No newline at end of file diff --git a/src/parser/item.rs b/src/parser/item.rs index 39e2fbf..d5f670f 100644 --- a/src/parser/item.rs +++ b/src/parser/item.rs @@ -12,7 +12,7 @@ pub enum Expr { }, BfFDeclaration { iden: String, - code: String, + body: String, }, Literal(Value), } diff --git a/src/parser/mod.rs b/src/parser/mod.rs index f4970c5..57cd844 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -100,8 +100,22 @@ impl<'a> Parser<'a> { self.require(Token::LeftBrace)?; // Parse function body let mut body = Vec::new(); - while let Some(token) = self.lexer.next() { - if token == Token::RightBrace { break } + loop { + let token = { + match self.lexer.next() { + Some(t) => t, + None => { + return Err(Error { + kind: ErrorKind::EndOfTokenStream, + position: self.lexer.span(), + }) + } + } + }; + + if token == Token::RightBrace { + break; + } body.push(self.parse_expr(Some(token))?); } @@ -112,12 +126,27 @@ impl<'a> Parser<'a> { /// /// `bff [iden] { ... }` fn bff_declaration(&mut self) -> Result { - // TODO: Make it throw error when EOF let iden = self.require_iden()?; self.require(Token::LeftBrace)?; - let mut code = String::new(); - while let Some(token) = self.lexer.next() { - code.push_str(match token { + + let mut body = String::new(); + loop { + let token = { + match self.lexer.next() { + Some(t) => t, + None => { + return Err(Error { + kind: ErrorKind::EndOfTokenStream, + position: self.lexer.span(), + }) + } + } + }; + + if token == Token::RightBrace { + break; + } + body.push_str(match token { Token::OpGt | Token::OpLt | Token::Addition @@ -130,6 +159,6 @@ impl<'a> Parser<'a> { _ => return Err(self.unexpected_token(None)), }); } - Ok(Expr::BfFDeclaration { iden, code }) + Ok(Expr::BfFDeclaration { iden, body }) } } diff --git a/src/variables.rs b/src/variables.rs index b3020a1..4bfed39 100644 --- a/src/variables.rs +++ b/src/variables.rs @@ -1,5 +1,4 @@ use rand::Rng; -use std::collections::HashMap; #[derive(Debug, Clone, PartialEq)] pub enum Abool { @@ -30,21 +29,4 @@ pub enum Value { pub struct Variable { melo: bool, value: Value, -} -pub fn test() { - let mut map = HashMap::new(); - let a = Variable { - melo: false, - value: Value::Str("1".to_string()), - }; - let b = Variable { - melo: false, - value: Value::Int(2), - }; - map.insert("a", a); - map.insert("b", b); - - for (key, value) in &map { - println!("{}: {:?}", key, value); - } -} +} \ No newline at end of file