@@ -19,6 +19,7 @@ pub use error::{ParserError, ParserErrorKind};
1919pub mod ast;
2020mod error;
2121
22+ // MARK: Parser Struct
2223pub struct Parser < ' a > {
2324 tokens : Peekable < Iter < ' a , LexerToken > > ,
2425 token : Option < LexerToken > ,
@@ -62,6 +63,7 @@ impl<'a> Parser<'a> {
6263 parser
6364 }
6465
66+ // MARK: Parse
6567 pub fn parse ( & mut self ) -> ProgramTree {
6668 let mut statements = ProgramTree :: new ( ) ;
6769
@@ -92,6 +94,7 @@ impl<'a> Parser<'a> {
9294 } ) ) ;
9395 }
9496
97+ // MARK: Statement
9598 fn parse_statement ( & mut self , token : & LexerToken ) -> ParserResult < Option < Statement > > {
9699 use LexerTokenKind :: * ;
97100 Ok ( Some ( match & token. kind {
@@ -107,6 +110,7 @@ impl<'a> Parser<'a> {
107110 } ) )
108111 }
109112
113+ // MARK: Var Stmt
110114 fn var_decl ( & mut self ) -> ParserResult < Statement > {
111115 self . expect_token ( & LexerTokenKind :: Var ) ?;
112116 let value = self . parse_var ( ) ?;
@@ -115,6 +119,7 @@ impl<'a> Parser<'a> {
115119 Ok ( Statement :: Variable ( Box :: from ( value) ) )
116120 }
117121
122+ // MARK: Var Parsing
118123 fn parse_var ( & mut self ) -> ParserResult < Variable > {
119124 let identifier = self
120125 . expect_token ( & LexerTokenKind :: Identifier ) ?
@@ -151,6 +156,7 @@ impl<'a> Parser<'a> {
151156 } )
152157 }
153158
159+ // MARK: Func Decl
154160 fn func_decl ( & mut self ) -> ParserResult < Statement > {
155161 self . expect_token ( & LexerTokenKind :: Function ) ?;
156162
@@ -199,16 +205,7 @@ impl<'a> Parser<'a> {
199205 Ok ( Statement :: Function ( Box :: from ( function) ) )
200206 }
201207
202- fn parse_if ( & mut self ) -> ParserResult < Statement > {
203- self . expect_token ( & LexerTokenKind :: If ) ?;
204-
205- if let Some ( expr) = self . if_expr ( ) ? {
206- Ok ( Statement :: If ( Box :: from ( expr) ) )
207- } else {
208- Err ( ParserErrorKind :: UnexpectedEnd )
209- }
210- }
211-
208+ // MARK: Block Parsing
212209 fn parse_inline_block ( & mut self ) -> ParserResult < WithCursor < Block > > {
213210 let Some ( next) = self . peek ( ) . cloned ( ) else {
214211 return Err ( ParserErrorKind :: ExpectedStatement ) ;
@@ -222,7 +219,6 @@ impl<'a> Parser<'a> {
222219 Ok ( WithCursor :: create_with ( start, self . cursor , vec ! [ stmt] ) )
223220 }
224221
225-
226222 fn parse_block ( & mut self ) -> ParserResult < WithCursor < Block > > {
227223 let mut block = Block :: new ( ) ;
228224 let start = self . cursor ;
@@ -246,6 +242,7 @@ impl<'a> Parser<'a> {
246242 self . assignment ( )
247243 }
248244
245+ // MARK: Assignment
249246 fn assignment ( & mut self ) -> ParserResult < Option < WithCursor < Expression > > > {
250247 let_expr ! ( lhs = self . logic_or( ) ?) ;
251248
@@ -280,6 +277,7 @@ impl<'a> Parser<'a> {
280277 Ok ( Some ( lhs) )
281278 }
282279
280+ // MARK: Logic OR
283281 fn logic_or ( & mut self ) -> ParserResult < Option < WithCursor < Expression > > > {
284282 let_expr ! ( mut lhs = self . logic_and( ) ?) ;
285283
@@ -302,6 +300,7 @@ impl<'a> Parser<'a> {
302300 Ok ( Some ( lhs) )
303301 }
304302
303+ // MARK: Logic AND
305304 fn logic_and ( & mut self ) -> ParserResult < Option < WithCursor < Expression > > > {
306305 let_expr ! ( mut lhs = self . cmp_equality( ) ?) ;
307306
@@ -324,6 +323,7 @@ impl<'a> Parser<'a> {
324323 Ok ( Some ( lhs) )
325324 }
326325
326+ // MARK: Cmp Equal
327327 fn cmp_equality ( & mut self ) -> ParserResult < Option < WithCursor < Expression > > > {
328328 let_expr ! ( mut lhs = self . cmp_lgt( ) ?) ;
329329
@@ -348,6 +348,7 @@ impl<'a> Parser<'a> {
348348 Ok ( Some ( lhs) )
349349 }
350350
351+ // MARK: Cmp LGT
351352 fn cmp_lgt ( & mut self ) -> ParserResult < Option < WithCursor < Expression > > > {
352353 let_expr ! ( mut lhs = self . arith_add_sub( ) ?) ;
353354
@@ -375,6 +376,7 @@ impl<'a> Parser<'a> {
375376 Ok ( Some ( lhs) )
376377 }
377378
379+ // MARK: Arith Add Sub
378380 fn arith_add_sub ( & mut self ) -> ParserResult < Option < WithCursor < Expression > > > {
379381 let_expr ! ( mut lhs = self . arith_mul_div( ) ?) ;
380382
@@ -399,6 +401,7 @@ impl<'a> Parser<'a> {
399401 Ok ( Some ( lhs) )
400402 }
401403
404+ // MARK: Arith Mul Div
402405 fn arith_mul_div ( & mut self ) -> ParserResult < Option < WithCursor < Expression > > > {
403406 let_expr ! ( mut lhs = self . unary( ) ?) ;
404407
@@ -423,6 +426,8 @@ impl<'a> Parser<'a> {
423426 Ok ( Some ( lhs) )
424427 }
425428
429+
430+ // MARK: Unary
426431 fn unary ( & mut self ) -> ParserResult < Option < WithCursor < Expression > > > {
427432 let_expr ! ( mut lhs = self . func_invoke( ) ?) ;
428433
@@ -444,6 +449,7 @@ impl<'a> Parser<'a> {
444449 Ok ( Some ( lhs) )
445450 }
446451
452+ // MARK: Func Invoke
447453 fn func_invoke ( & mut self ) -> ParserResult < Option < WithCursor < Expression > > > {
448454 let_expr ! ( mut expr = self . literals( ) ?) ;
449455
@@ -483,6 +489,7 @@ impl<'a> Parser<'a> {
483489 Ok ( Some ( expr) )
484490 }
485491
492+ // MARK: Literals
486493 fn literals ( & mut self ) -> ParserResult < Option < WithCursor < Expression > > > {
487494 let_expr ! ( token = self . peek( ) ) ;
488495
@@ -532,6 +539,7 @@ impl<'a> Parser<'a> {
532539 Ok ( Some ( WithCursor :: create_with ( token. start , token. end , expr) ) )
533540 }
534541
542+ // MARK: Grouping
535543 fn group ( & mut self ) -> ParserResult < Option < WithCursor < Expression > > > {
536544 let_expr ! ( mut expr = self . expression( ) ?) ;
537545
@@ -542,6 +550,7 @@ impl<'a> Parser<'a> {
542550 Ok ( Some ( expr) )
543551 }
544552
553+ // MARK: If Expr
545554 fn if_expr ( & mut self ) -> ParserResult < Option < WithCursor < Expression > > > {
546555 let_expr ! ( condition = self . expression( ) ?) ;
547556
@@ -584,6 +593,16 @@ impl<'a> Parser<'a> {
584593 Ok ( Some ( WithCursor :: create_with ( start, self . cursor , if_expr) ) )
585594 }
586595
596+ fn parse_if ( & mut self ) -> ParserResult < Statement > {
597+ self . expect_token ( & LexerTokenKind :: If ) ?;
598+
599+ if let Some ( expr) = self . if_expr ( ) ? {
600+ Ok ( Statement :: If ( Box :: from ( expr) ) )
601+ } else {
602+ Err ( ParserErrorKind :: UnexpectedEnd )
603+ }
604+ }
605+
587606 fn expect_token ( & mut self , expected : & ' a LexerTokenKind ) -> ParserResult < & LexerToken > {
588607 self . expect ( & expected) . map_err ( |found| {
589608 ParserErrorKind :: ExpectedToken ( vec ! [ expected. clone( ) ] , found. map ( |t| t. kind . clone ( ) ) )
@@ -602,6 +621,7 @@ impl<'a> Parser<'a> {
602621 }
603622}
604623
624+ // MARK: Comp Error
605625impl ComponentErrors for Parser < ' _ > {
606626 fn fetch_errors ( & self ) -> & ErrorList {
607627 & self . errors
@@ -613,6 +633,7 @@ impl ComponentErrors for Parser<'_> {
613633 }
614634}
615635
636+ // MARK: Comp Iter
616637impl < ' a > ComponentIter < ' a , & ' a LexerTokenKind , & ' a LexerToken , Iter < ' a , LexerToken > >
617638 for Parser < ' a >
618639{
0 commit comments