Made parser to throw error when unexpected EOF

This commit is contained in:
Erin 2021-04-27 11:57:11 +02:00 committed by ondra05
parent 39a8bf6a54
commit 3f9e6b72cc
4 changed files with 39 additions and 31 deletions

View file

@ -1,4 +1 @@
functio test () { bff a {+++<<>>>[]]]][[]]}
var a = 3;
var b = 4;
}

View file

@ -12,7 +12,7 @@ pub enum Expr {
}, },
BfFDeclaration { BfFDeclaration {
iden: String, iden: String,
code: String, body: String,
}, },
Literal(Value), Literal(Value),
} }

View file

@ -100,8 +100,22 @@ impl<'a> Parser<'a> {
self.require(Token::LeftBrace)?; self.require(Token::LeftBrace)?;
// Parse function body // Parse function body
let mut body = Vec::new(); let mut body = Vec::new();
while let Some(token) = self.lexer.next() { loop {
if token == Token::RightBrace { break } 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))?); body.push(self.parse_expr(Some(token))?);
} }
@ -112,12 +126,27 @@ impl<'a> Parser<'a> {
/// ///
/// `bff [iden] { ... }` /// `bff [iden] { ... }`
fn bff_declaration(&mut self) -> Result<Expr, Error> { fn bff_declaration(&mut self) -> Result<Expr, Error> {
// TODO: Make it throw error when EOF
let iden = self.require_iden()?; let iden = self.require_iden()?;
self.require(Token::LeftBrace)?; self.require(Token::LeftBrace)?;
let mut code = String::new();
while let Some(token) = self.lexer.next() { let mut body = String::new();
code.push_str(match token { 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::OpGt
| Token::OpLt | Token::OpLt
| Token::Addition | Token::Addition
@ -130,6 +159,6 @@ impl<'a> Parser<'a> {
_ => return Err(self.unexpected_token(None)), _ => return Err(self.unexpected_token(None)),
}); });
} }
Ok(Expr::BfFDeclaration { iden, code }) Ok(Expr::BfFDeclaration { iden, body })
} }
} }

View file

@ -1,5 +1,4 @@
use rand::Rng; use rand::Rng;
use std::collections::HashMap;
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub enum Abool { pub enum Abool {
@ -30,21 +29,4 @@ pub enum Value {
pub struct Variable { pub struct Variable {
melo: bool, melo: bool,
value: Value, 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);
}
}