@@ -40,7 +40,7 @@ use self::{
4040} ;
4141use crate :: {
4242 lexer:: { token:: EscapeSequence , Error as LexError , InputElement , Token , TokenKind } , parse_cmd, parser:: {
43- expression:: { BindingIdentifier , Initializer , PropertyName } , parse_loop:: ParseState , AllowAwait , AllowReturn , AllowYield , ControlFlow , Cursor , OrAbrupt , ParseResult , TokenLoopParser , TokenParser
43+ expression:: { BindingIdentifier , Initializer , PropertyName } , parse_loop:: ParseState , AllowAwait , AllowReturn , AllowYield , ControlFlow , Cursor , OrAbrupt , ParseResult , ParsedNode , TokenLoopParser , TokenParser
4444 } , source:: ReadChar , Error
4545} ;
4646use ast:: {
@@ -222,6 +222,14 @@ where
222222 }
223223}
224224
225+ impl < R : ReadChar > TokenLoopParser < R > for Statement {
226+ fn parse_loop ( & mut self , state : & mut ParseState < ' _ , R > , _continue_point : usize ) -> ParseResult < ControlFlow < R > > {
227+ let ( cursor, interner) = state. mut_inner ( ) ;
228+ let ok = self . parse ( cursor, interner) ?;
229+ parse_cmd ! ( [ DONE ] : state <= Statement ( ok) )
230+ }
231+ }
232+
225233/// Reads a list of statements.
226234///
227235/// More information:
@@ -603,10 +611,52 @@ impl<R> TokenLoopParser<R> for StatementListItem
603611where
604612 R : ReadChar ,
605613{
606- fn parse_loop ( & mut self , state : & mut ParseState < ' _ , R > , _continue_point : usize ) -> ParseResult < ControlFlow < R > > {
607- let ( cursor, interner) = state. mut_inner ( ) ;
608- let ok = self . parse ( cursor, interner) ?;
609- parse_cmd ! ( [ DONE ] : state <= StatementListItem ( ok) )
614+ fn parse_loop ( & mut self , state : & mut ParseState < ' _ , R > , continue_point : usize ) -> ParseResult < ControlFlow < R > > {
615+ if continue_point == 1 {
616+ parse_cmd ! [ [ POP LOCAL ] : state => Empty ] ;
617+ match state. pop_node ( ) ? {
618+ ParsedNode :: Declaration ( decl) => {
619+ parse_cmd ! [ [ DONE ] : state <= StatementListItem ( ast:: StatementListItem :: from( decl) ) ]
620+ }
621+ ParsedNode :: Statement ( stmt) => {
622+ parse_cmd ! [ [ DONE ] : state <= StatementListItem ( ast:: StatementListItem :: from( stmt) ) ]
623+ }
624+ _ => return Err ( state. general_error ( concat ! ( "expect `Declaration` or `Statement` node" ) ) )
625+ }
626+ } else if continue_point > 1 {
627+ return state. continue_point_error ( continue_point)
628+ }
629+
630+ let tok = state. peek ( 0 ) . or_abrupt ( ) ?;
631+
632+ let decl = Declaration :: new ( self . allow_yield , self . allow_await ) ;
633+ let stmt = Statement :: new ( self . allow_yield , self . allow_await , self . allow_return ) ;
634+
635+ match tok. kind ( ) . clone ( ) {
636+ TokenKind :: Keyword ( ( Keyword :: Function | Keyword :: Class | Keyword :: Const , _) ) => {
637+ parse_cmd ! [ [ SUB PARSE ] : decl; state <= Empty ( 1 ) ]
638+ }
639+ TokenKind :: Keyword ( ( Keyword :: Let , false ) ) if allowed_token_after_let ( state. peek ( 1 ) ?) => {
640+ parse_cmd ! [ [ SUB PARSE ] : decl; state <= Empty ( 1 ) ]
641+ }
642+ TokenKind :: Keyword ( ( Keyword :: Async , false ) ) => {
643+ let skip_n = if state. peek_is_line_terminator ( 0 ) . or_abrupt ( ) ? {
644+ 2
645+ } else {
646+ 1
647+ } ;
648+
649+ let is_line_terminator = state. peek_is_line_terminator ( skip_n) ?. unwrap_or ( true ) ;
650+
651+ match state. peek ( 1 ) ?. map ( Token :: kind) {
652+ Some ( TokenKind :: Keyword ( ( Keyword :: Function , _) ) ) if !is_line_terminator => {
653+ parse_cmd ! [ [ SUB PARSE ] : decl; state <= Empty ( 1 ) ]
654+ }
655+ _ => parse_cmd ! [ [ SUB PARSE ] : stmt; state <= Empty ( 1 ) ]
656+ }
657+ }
658+ _ => parse_cmd ! [ [ SUB PARSE ] : stmt; state <= Empty ( 1 ) ]
659+ }
610660 }
611661}
612662
0 commit comments