Skip to content

Commit 0c65540

Browse files
committed
Index based ast processing
1 parent 0e55002 commit 0c65540

File tree

2 files changed

+12
-29
lines changed

2 files changed

+12
-29
lines changed

@types/core/parse/ast/ast.d.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export class AST {
1616
type: number;
1717
};
1818
};
19+
_index: number;
1920
/**
2021
* Parses the input text and generates an AST.
2122
* @param {string} text - The input text to parse.
@@ -143,16 +144,6 @@ export class AST {
143144
* @returns {import('../lexer/lexer.js').Token|boolean} The next token if it matches, otherwise false.
144145
*/
145146
_peek(...expected: string[]): import("../lexer/lexer.js").Token | boolean;
146-
/**
147-
* Checks if the token at the specified index matches any of the expected types.
148-
* @param {number} i - The index to check.
149-
* @param {...string} [expected] - The expected token types.
150-
* @returns {import("../lexer/lexer.js").Token|boolean} The token at the specified index if it matches, otherwise false.
151-
*/
152-
_peekAhead(
153-
i: number,
154-
...expected: string[]
155-
): import("../lexer/lexer.js").Token | boolean;
156147
/**
157148
* Consumes the next token if it matches any of the expected types.
158149
* @param {...string} [expected] - The expected token types.

src/core/parse/ast/ast.js

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)