From 47400ee2ce9907aefe2fc822ecb2ba215a0285b7 Mon Sep 17 00:00:00 2001 From: Erin Date: Mon, 26 Apr 2021 10:44:42 +0200 Subject: [PATCH] Starting work on parser improvements - Parser should parse single expressions --- src/error.rs | 3 ++- src/main.rs | 2 +- src/parser/mod.rs | 46 ++++++++++++++++++++++++++++++++++++++++++--- src/parser/utils.rs | 2 +- src/tokens.rs | 19 +++++++++++++++++++ 5 files changed, 66 insertions(+), 6 deletions(-) diff --git a/src/error.rs b/src/error.rs index 510aa334..1ae42e84 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 bf9230a9..32d3d3c2 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 33a84097..d1d432ed 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 bae92a37..cbf8d8c6 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 ddfe26eb..8b458c50 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,