diff --git a/crates/fuse-parser/src/parsers/expressions.rs b/crates/fuse-parser/src/parsers/expressions.rs index a5d7a62..7f72855 100644 --- a/crates/fuse-parser/src/parsers/expressions.rs +++ b/crates/fuse-parser/src/parsers/expressions.rs @@ -43,6 +43,7 @@ impl<'a> Parser<'a> { } pub(crate) fn parse_identifier(&mut self) -> ParserResult { + debug_assert!(self.at(TokenKind::Identifier)); let token = self.consume(); let view = self.view_token(*token); Ok(Identifier { @@ -61,6 +62,7 @@ impl<'a> Parser<'a> { } fn parse_if(&mut self) -> ParserResult { + debug_assert!(matches!(self.cur_kind(), TokenKind::If | TokenKind::ElseIf)); let start = self.start_span(); // Consume the keyword self.consume(); @@ -96,4 +98,9 @@ impl<'a> Parser<'a> { r#else, }) } + + pub(crate) fn parse_unary_operator_expression(&mut self) -> ParserResult { + self.parse_unary_operator() + .map(|op| self.ast.unary_operator_expression(op)) + } } diff --git a/crates/fuse-parser/src/parsers/mod.rs b/crates/fuse-parser/src/parsers/mod.rs index 51f0e7e..858e7a8 100644 --- a/crates/fuse-parser/src/parsers/mod.rs +++ b/crates/fuse-parser/src/parsers/mod.rs @@ -3,6 +3,7 @@ mod declarations; mod expressions; mod functions; mod numbers; +mod operators; mod statements; mod strings; mod types; diff --git a/crates/fuse-parser/src/parsers/operators.rs b/crates/fuse-parser/src/parsers/operators.rs new file mode 100644 index 0000000..1ddcf87 --- /dev/null +++ b/crates/fuse-parser/src/parsers/operators.rs @@ -0,0 +1,31 @@ +use fuse_ast::UnaryOperator; + +use crate::{lexer::TokenKind, Parser, ParserResult}; + +impl<'a> Parser<'a> { + pub(crate) fn parse_unary_operator(&mut self) -> ParserResult { + match self.cur_kind() { + TokenKind::Not => self.parse_unary_not_operator(), + TokenKind::Plus => self.parse_unary_plus_operator(), + TokenKind::Minus => self.parse_unary_minus_operator(), + _ => Err(Self::unexpected_error(self.cur_token())), + } + } + + fn parse_unary_not_operator(&mut self) -> ParserResult { + debug_assert!(self.at(TokenKind::Not)); + // Consume the keyword. + self.consume(); + todo!() + } + + fn parse_unary_plus_operator(&mut self) -> ParserResult { + debug_assert!(self.at(TokenKind::Plus)); + todo!() + } + + fn parse_unary_minus_operator(&mut self) -> ParserResult { + debug_assert!(self.at(TokenKind::Minus)); + todo!() + } +} diff --git a/crates/fuse-parser/src/parsers/statements.rs b/crates/fuse-parser/src/parsers/statements.rs index 31b58b2..bcf9930 100644 --- a/crates/fuse-parser/src/parsers/statements.rs +++ b/crates/fuse-parser/src/parsers/statements.rs @@ -62,6 +62,10 @@ impl<'a> Parser<'a> { .parse_expression() .map(|expr| self.ast.expression_statement(expr)), + TokenKind::Not | TokenKind::Plus | TokenKind::Minus => self + .parse_unary_operator_expression() + .map(|expr| self.ast.expression_statement(expr)), + TokenKind::Function | TokenKind::Fn => { if self.nth_kind(1) == TokenKind::Identifier { // function declaration