@@ -30,6 +30,7 @@ export class AST {
3030 this : { type : ASTType . _ThisExpression } ,
3131 $locals : { type : ASTType . _LocalsExpression } ,
3232 } ;
33+ this . _index = 0 ;
3334 }
3435
3536 /**
@@ -40,10 +41,11 @@ export class AST {
4041 _ast ( text ) {
4142 this . _text = text ;
4243 this . _tokens = this . _lexer . _lex ( text ) ;
44+
4345 const value = this . _program ( ) ;
4446
45- if ( this . _tokens . length !== 0 ) {
46- this . _throwError ( "is an unexpected token" , this . _tokens [ 0 ] ) ;
47+ if ( this . _tokens . length > this . _index ) {
48+ this . _throwError ( "is an unexpected token" , this . _tokens [ this . _index ] ) ;
4749 }
4850
4951 return value ;
@@ -59,7 +61,7 @@ export class AST {
5961 let hasMore = true ;
6062
6163 while ( hasMore ) {
62- if ( this . _tokens . length > 0 && ! this . _peek ( "}" , ")" , ";" , "]" ) )
64+ if ( this . _tokens . length > this . _index && ! this . _peek ( "}" , ")" , ";" , "]" ) )
6365 body . push ( this . _expressionStatement ( ) ) ;
6466
6567 if ( ! this . _expect ( ";" ) ) {
@@ -542,7 +544,7 @@ export class AST {
542544 * @returns {import("../lexer/lexer.js").Token } The consumed token.
543545 */
544546 _consume ( e1 ) {
545- if ( this . _tokens . length === 0 ) {
547+ if ( this . _tokens . length === this . _index ) {
546548 throw $parseMinErr (
547549 "ueoe" ,
548550 "Unexpected end of expression: {0}" ,
@@ -569,15 +571,15 @@ export class AST {
569571 * @returns {import("../lexer/lexer.js").Token } The next token.
570572 */
571573 _peekToken ( ) {
572- if ( this . _tokens . length === 0 ) {
574+ if ( this . _tokens . length === this . _index ) {
573575 throw $parseMinErr (
574576 "ueoe" ,
575577 "Unexpected end of expression: {0}" ,
576578 this . _text ,
577579 ) ;
578580 }
579581
580- return this . _tokens [ 0 ] ;
582+ return this . _tokens [ this . _index ] ;
581583 }
582584
583585 /**
@@ -586,18 +588,8 @@ export class AST {
586588 * @returns {import('../lexer/lexer.js').Token|boolean } The next token if it matches, otherwise false.
587589 */
588590 _peek ( ...expected ) {
589- return this . _peekAhead ( 0 , ...expected ) ;
590- }
591-
592- /**
593- * Checks if the token at the specified index matches any of the expected types.
594- * @param {number } i - The index to check.
595- * @param {...string } [expected] - The expected token types.
596- * @returns {import("../lexer/lexer.js").Token|boolean } The token at the specified index if it matches, otherwise false.
597- */
598- _peekAhead ( i , ...expected ) {
599- if ( this . _tokens . length > i ) {
600- const token = this . _tokens [ i ] ;
591+ if ( this . _tokens . length > this . _index ) {
592+ const token = this . _tokens [ this . _index ] ;
601593
602594 const { text } = token ;
603595
@@ -621,7 +613,7 @@ export class AST {
621613 const token = this . _peek ( ...expected ) ;
622614
623615 if ( token ) {
624- this . _tokens . shift ( ) ;
616+ this . _index ++ ;
625617
626618 return token ;
627619 }
0 commit comments