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 () {
var a = 3;
var b = 4;
}
bff a {+++<<>>>[]]]][[]]}

View file

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

View file

@ -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<Expr, Error> {
// 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 })
}
}

View file

@ -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);
}
}
}