@@ -8,10 +8,9 @@ use error::ParserResult;
88
99use crate :: {
1010 component:: { ComponentErrors , ComponentIter } ,
11- cursor:: WithCursor ,
11+ cursor:: { Cursor , WithCursor } ,
1212 error:: { EngineErrorKind , ErrorList } ,
1313 lexer:: tokens:: { LexerToken , LexerTokenKind , LexerTokenList } ,
14- Cursor ,
1514} ;
1615
1716pub use error:: { ParserError , ParserErrorKind } ;
@@ -25,6 +24,7 @@ pub struct Parser<'a> {
2524 token : Option < LexerToken > ,
2625 cursor : Cursor ,
2726 errors : ErrorList ,
27+ tree : ProgramTree ,
2828
2929 #[ cfg( feature = "cli" ) ]
3030 source : crate :: error:: SourceFile ,
@@ -54,6 +54,7 @@ impl<'a> Parser<'a> {
5454 token : None ,
5555 cursor : Cursor :: create ( ) ,
5656 errors : ErrorList :: new ( ) ,
57+ tree : ProgramTree :: new ( ) ,
5758
5859 #[ cfg( feature = "cli" ) ]
5960 source,
@@ -64,12 +65,10 @@ impl<'a> Parser<'a> {
6465 }
6566
6667 // MARK: Parser Main
67- pub fn parse ( & mut self ) -> ProgramTree {
68- let mut statements = ProgramTree :: new ( ) ;
69-
68+ pub fn parse ( & mut self ) -> & ProgramTree {
7069 while let Some ( token) = self . peek ( ) . cloned ( ) {
7170 match self . parse_statement ( token) {
72- Ok ( Some ( statement) ) => statements . push ( statement) ,
71+ Ok ( Some ( statement) ) => self . tree . push ( statement) ,
7372 Err ( err) => {
7473 debug ! ( "adding error {err:?}" ) ;
7574 let token = self . token . as_ref ( ) . unwrap_or ( token) ;
@@ -81,7 +80,7 @@ impl<'a> Parser<'a> {
8180 self . next ( ) ;
8281 }
8382
84- statements
83+ & self . tree
8584 }
8685
8786 fn add_error ( & mut self , start : Cursor , end : Cursor , kind : ParserErrorKind ) {
@@ -444,23 +443,24 @@ impl<'a> Parser<'a> {
444443
445444 // MARK: Unary
446445 fn expr_unary ( & mut self ) -> ParserResult < Option < WithCursor < Expression > > > {
447- let_expr ! ( mut lhs = self . expr_func_invoke( ) ?) ;
448-
449- while let Some ( token_unary) = self . next_if_eq ( & & LexerTokenKind :: Not ) {
446+ let start = self . cursor ;
447+ if let Some ( token_unary) = self . next_if_eq ( & & LexerTokenKind :: Not ) {
450448 let_expr ! ( rhs = self . expr_unary( ) ?) ;
451-
449+
452450 let operator: UnaryOperator = token_unary. kind . clone ( ) . try_into ( ) ?;
453-
454- lhs = WithCursor :: create_with (
455- lhs . start ,
451+
452+ return Ok ( Some ( WithCursor :: create_with (
453+ start,
456454 rhs. end ,
457455 Expression :: Unary ( Box :: from ( (
458456 WithCursor :: create_with ( token_unary. start , token_unary. end , operator) ,
459- lhs ,
457+ rhs ,
460458 ) ) ) ,
461- ) ;
459+ ) ) ) ;
462460 }
463461
462+ let_expr ! ( lhs = self . expr_func_invoke( ) ?) ;
463+
464464 Ok ( Some ( lhs) )
465465 }
466466
@@ -762,10 +762,13 @@ impl<'a> Parser<'a> {
762762 match self . expect_any ( & [ & LexerTokenKind :: EOL , & LexerTokenKind :: EOF ] ) {
763763 Ok ( found) => Ok ( Some ( found) ) ,
764764 Err ( None ) => Ok ( None ) ,
765- Err ( Some ( found) ) => Err ( ParserErrorKind :: ExpectedToken (
766- vec ! [ LexerTokenKind :: EOL , LexerTokenKind :: EOF ] ,
767- Some ( found. kind . clone ( ) ) ,
768- ) ) ,
765+ Err ( Some ( found) ) => {
766+ self . next ( ) ;
767+ Err ( ParserErrorKind :: ExpectedToken (
768+ vec ! [ LexerTokenKind :: EOL , LexerTokenKind :: EOF ] ,
769+ Some ( found. kind . clone ( ) ) ,
770+ ) )
771+ } ,
769772 }
770773 }
771774}
0 commit comments