@@ -205,21 +205,6 @@ impl<'a> Parser<'a> {
205
205
Ok ( Statement :: Function ( Box :: from ( function) ) )
206
206
}
207
207
208
- // MARK: Block Parsing
209
- fn parse_inline_block ( & mut self ) -> ParserResult < WithCursor < Block > > {
210
- let Some ( next) = self . peek ( ) . cloned ( ) else {
211
- return Err ( ParserErrorKind :: ExpectedStatement ) ;
212
- } ;
213
-
214
- let start = self . cursor ;
215
- let Some ( stmt) = self . parse_statement ( next) ? else {
216
- return Err ( ParserErrorKind :: ExpectedStatement ) ;
217
- } ;
218
-
219
- self . next ( ) ;
220
- Ok ( WithCursor :: create_with ( start, self . cursor , vec ! [ stmt] ) )
221
- }
222
-
223
208
fn expression ( & mut self ) -> ParserResult < Option < WithCursor < Expression > > > {
224
209
self . assignment ( )
225
210
}
@@ -470,7 +455,7 @@ impl<'a> Parser<'a> {
470
455
// MARK: Primary
471
456
/// Highest precedence
472
457
fn primary ( & mut self ) -> ParserResult < Option < WithCursor < Expression > > > {
473
- let_expr ! ( token = self . peek ( ) ) ;
458
+ let_expr ! ( token = self . next ( ) ) ;
474
459
475
460
let expr = match token. kind {
476
461
LexerTokenKind :: String => Expression :: Literal ( Box :: from ( Literal :: String ( Box :: from (
@@ -494,38 +479,48 @@ impl<'a> Parser<'a> {
494
479
}
495
480
496
481
LexerTokenKind :: LParen => {
497
- self . next ( ) ;
498
482
return self . group ( ) ;
499
483
}
500
484
501
485
LexerTokenKind :: LBracket => {
502
- self . next ( ) ;
503
486
return self . block_expr ( ) ;
504
487
}
505
488
506
489
LexerTokenKind :: If => {
507
- self . next ( ) ;
508
490
return self . if_expr ( ) ;
509
491
}
510
492
511
493
LexerTokenKind :: Match => {
512
- self . next ( ) ;
513
494
return self . match_expr ( ) ;
514
495
}
515
496
516
497
LexerTokenKind :: EOL | LexerTokenKind :: EOF => return Ok ( None ) ,
517
498
518
- _ => {
519
- let token = token. to_owned ( ) ;
520
- self . cursor_next ( & token) ;
521
- return Err ( ParserErrorKind :: UnexpectedToken ( token. kind . clone ( ) ) ) ;
522
- }
499
+ _ => return Err ( ParserErrorKind :: UnexpectedToken ( token. kind . clone ( ) ) ) ,
523
500
} ;
524
501
525
- let token = token. to_owned ( ) ;
526
- self . next ( ) ;
502
+ let_expr ! ( post = self . range( WithCursor :: create_with( token. start, token. end, expr) ) ?) ;
527
503
528
- Ok ( Some ( WithCursor :: create_with ( token. start , token. end , expr) ) )
504
+ Ok ( Some ( post) )
505
+ }
506
+
507
+ // MARK: Range
508
+ fn range ( & mut self , lhs : WithCursor < Expression > ) -> ParserResult < Option < WithCursor < Expression > > > {
509
+ let_expr ! ( peek = self . peek( ) ) ;
510
+ let inclusive = match peek. kind {
511
+ LexerTokenKind :: Range => false ,
512
+ LexerTokenKind :: RangeInclusive => true ,
513
+ _ => return Ok ( Some ( lhs) )
514
+ } ;
515
+
516
+ self . next ( ) ;
517
+ let_expr ! ( rhs = self . expression( ) ?) ;
518
+
519
+ Ok ( Some ( WithCursor :: create_with ( lhs. start , rhs. end , Expression :: Range ( Box :: from ( (
520
+ lhs,
521
+ rhs,
522
+ inclusive
523
+ ) ) ) ) ) )
529
524
}
530
525
531
526
// MARK: Grouping
@@ -545,7 +540,6 @@ impl<'a> Parser<'a> {
545
540
546
541
let start = self . cursor ;
547
542
548
-
549
543
let truthy_block = if self . next_if_eq ( & & LexerTokenKind :: LBracket ) . is_some ( ) {
550
544
self . parse_block ( ) ?
551
545
} else if self . next_if_eq ( & & LexerTokenKind :: Colon ) . is_some ( ) {
@@ -554,7 +548,6 @@ impl<'a> Parser<'a> {
554
548
return Err ( ParserErrorKind :: ExpectedExpression ) ;
555
549
} ;
556
550
557
-
558
551
let else_condition = if self . next_if_eq ( & & LexerTokenKind :: Else ) . is_some ( ) {
559
552
let start = self . cursor ;
560
553
@@ -601,7 +594,7 @@ impl<'a> Parser<'a> {
601
594
self . expect_terminator ( ) ?;
602
595
603
596
let mut hash_map = MatchCase :: new ( ) ;
604
-
597
+
605
598
while let Some ( token) = self . peek ( ) . cloned ( ) {
606
599
if token. kind == LexerTokenKind :: RBracket {
607
600
// self.next();
@@ -653,6 +646,21 @@ impl<'a> Parser<'a> {
653
646
) ) )
654
647
}
655
648
649
+ // MARK: Block Parsing
650
+ fn parse_inline_block ( & mut self ) -> ParserResult < WithCursor < Block > > {
651
+ let Some ( next) = self . peek ( ) . cloned ( ) else {
652
+ return Err ( ParserErrorKind :: ExpectedStatement ) ;
653
+ } ;
654
+
655
+ let start = self . cursor ;
656
+ let Some ( stmt) = self . parse_statement ( next) ? else {
657
+ return Err ( ParserErrorKind :: ExpectedStatement ) ;
658
+ } ;
659
+
660
+ self . next ( ) ;
661
+ Ok ( WithCursor :: create_with ( start, self . cursor , vec ! [ stmt] ) )
662
+ }
663
+
656
664
fn block_expr ( & mut self ) -> ParserResult < Option < WithCursor < Expression > > > {
657
665
let block = self . parse_block ( ) ?;
658
666
@@ -680,7 +688,7 @@ impl<'a> Parser<'a> {
680
688
if let Some ( statement) = self . parse_statement ( token) ? {
681
689
block. push ( statement) ;
682
690
}
683
-
691
+
684
692
self . next ( ) ;
685
693
}
686
694
@@ -693,12 +701,13 @@ impl<'a> Parser<'a> {
693
701
} )
694
702
}
695
703
696
- fn expect_terminator ( & mut self ) -> ParserResult < & LexerToken > {
704
+ fn expect_terminator ( & mut self ) -> ParserResult < Option < & LexerToken > > {
697
705
match self . expect_any ( & [ & LexerTokenKind :: EOL , & LexerTokenKind :: EOF ] ) {
698
- Ok ( found) => Ok ( found) ,
699
- Err ( found) => Err ( ParserErrorKind :: ExpectedToken (
706
+ Ok ( found) => Ok ( Some ( found) ) ,
707
+ Err ( None ) => Ok ( None ) ,
708
+ Err ( Some ( found) ) => Err ( ParserErrorKind :: ExpectedToken (
700
709
vec ! [ LexerTokenKind :: EOL , LexerTokenKind :: EOF ] ,
701
- found . map ( |t| t . kind . clone ( ) ) ,
710
+ Some ( found . kind . clone ( ) ) ,
702
711
) ) ,
703
712
}
704
713
}
0 commit comments