diff --git a/src/error.rs b/src/error.rs index 510aa33..1ae42e8 100644 --- a/src/error.rs +++ b/src/error.rs @@ -8,7 +8,8 @@ pub struct Error { #[derive(Debug, Clone)] pub enum ErrorKind { - SyntaxError, + SyntaxError(String), + EndOfTokenStream, } impl Error { diff --git a/src/main.rs b/src/main.rs index bf9230a..32d3d3c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -32,7 +32,7 @@ fn main() { // Parse let mut parser = Parser::new(&source); - let ast = parser.parse(); + let ast = parser.init(); println!("{:#?}", ast); } None => { diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 33a8409..d1d432e 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -11,6 +11,7 @@ use logos::Logos; /// Parser structure / state machine pub struct Parser<'a> { lexer: logos::Lexer<'a, Token>, + ast: Vec, } impl<'a> Parser<'a> { @@ -18,9 +19,33 @@ impl<'a> Parser<'a> { pub fn new(source: &'a str) -> Self { Self { lexer: Token::lexer(source), + ast: Vec::new(), } } + pub fn init(&mut self) -> Result, Error> { + loop { + let token = self.lexer.next(); + if token.is_none() { + return Ok(self.ast.clone()); + }; + let expr = self.parse_expr(&token)?; + self.ast.push(expr); + } + } + + fn parse_expr(&mut self, token: &Option) -> Result { + if matches!(token, None) { + return Err(Error { + kind: ErrorKind::EndOfTokenStream, + position: self.lexer.span(), + }); + } + + Ok(todo!()) + } + + /* /// Start parsing Token Vector into Abstract Syntax Tree pub fn parse(&mut self) -> Vec { let mut ast = vec![]; @@ -46,6 +71,7 @@ impl<'a> Parser<'a> { ast } + */ /// Parse variable declaration /// @@ -62,7 +88,7 @@ impl<'a> Parser<'a> { } _ => { return Err(Error { - kind: ErrorKind::SyntaxError, + kind: ErrorKind::SyntaxError("Unexpected token".to_owned()), position: self.lexer.span(), }) } @@ -80,7 +106,7 @@ impl<'a> Parser<'a> { // TODO: Arguments self.require(Token::RightParenthesis)?; self.require(Token::LeftBrace)?; - let body = self.parse(); + let body = vec![]; Ok(Expr::FunctionDeclaration { iden, body }) } @@ -91,7 +117,21 @@ impl<'a> Parser<'a> { fn bff_declaration(&mut self) -> Result { let iden = self.require(Token::Identifier)?; self.require(Token::LeftBrace)?; - let code = self.require(Token::String)?; // <-- Nasty hack, but works + let mut code = String::new(); + while let Some(token) = self.lexer.next() { + code.push_str(match token { + Token::OpGt + | Token::OpLt + | Token::Addition + | Token::Subtract + | Token::FullStop + | Token::Comma + | Token::LeftBracket + | Token::RightBracket => self.lexer.slice(), + Token::RightBrace => break, + _ => break, + }); + } self.require(Token::RightBrace)?; Ok(Expr::BfFDeclaration { iden, code }) } diff --git a/src/parser/utils.rs b/src/parser/utils.rs index bae92a3..cbf8d8c 100644 --- a/src/parser/utils.rs +++ b/src/parser/utils.rs @@ -27,7 +27,7 @@ impl<'a> Parser<'a> { Ok(self.lexer.slice().to_owned()) } else { Err(Error { - kind: ErrorKind::SyntaxError, + kind: ErrorKind::SyntaxError("Mysterious parse error".to_owned()), position: self.lexer.span(), }) } diff --git a/src/tokens.rs b/src/tokens.rs index ddfe26e..8b458c5 100644 --- a/src/tokens.rs +++ b/src/tokens.rs @@ -44,6 +44,12 @@ pub enum Token { #[token(";")] Semicolon, + #[token(".")] + FullStop, + + #[token(",")] + Comma, + #[regex(r"#.*")] Comment, @@ -63,6 +69,19 @@ pub enum Token { #[token("=")] Assignment, + // Logical operators + #[token("<")] + OpLt, + + #[token(">")] + OpGt, + + #[token("==")] + OpEq, + + #[token("!=")] + OpNeq, + /// Base52 based character ('a') #[token("'.*'")] Char,