Skip to content

Commit 8150633

Browse files
authored
opt eatToken (#10)
more more
1 parent 8a4e63c commit 8150633

File tree

1 file changed

+18
-20
lines changed

1 file changed

+18
-20
lines changed

assembly/parser/parser.ts

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,13 @@ export class Parser {
6161
return new Parser(input).toAST();
6262
}
6363

64-
private eatToken(value: string = ""): u32 {
65-
const current = this.currentToken;
66-
if (value.length > 0 && current != value) {
64+
private eatToken(value: u32 = -1): u32 {
65+
const token = this.currentToken.charCodeAt(0) as u32;
66+
if (value != -1 && token != value) {
6767
throw new Error("invalid token");
6868
}
69-
70-
this.cursor++;
71-
this.currentToken = this.input.charAt(this.cursor);
72-
return current.charCodeAt(0);
69+
this.currentToken = this.input.charAt(++this.cursor);
70+
return token;
7371
}
7472

7573
private more(): bool {
@@ -78,7 +76,7 @@ export class Parser {
7876

7977
private resetCursor(): void {
8078
this.cursor = 0;
81-
this.currentToken = this.input.charAt(this.cursor);
79+
this.currentToken = this.input.charAt(0);
8280
}
8381

8482
private toAST(): AST {
@@ -89,7 +87,7 @@ export class Parser {
8987
private parseCharacter(): Node {
9088
let token = this.currentToken.charCodeAt(0);
9189
if (token == 0x5c /* \ */) {
92-
this.eatToken("\\");
90+
this.eatToken(0x5c);
9391
token = this.currentToken.charCodeAt(0);
9492
if (isSpecialCharacter(token)) {
9593
this.eatToken();
@@ -106,7 +104,7 @@ export class Parser {
106104
}
107105

108106
if (token == CharClass.Dot) {
109-
this.eatToken(".");
107+
this.eatToken(CharClass.Dot);
110108
return new CharacterClassNode(CharClass.Dot);
111109
}
112110

@@ -116,7 +114,7 @@ export class Parser {
116114
private maybeParseRepetitionRange(): Range {
117115
// snapshot
118116
const previousCursor = this.cursor;
119-
this.eatToken("{");
117+
this.eatToken(0x7b /* { */);
120118

121119
let range = new Range();
122120

@@ -138,7 +136,7 @@ export class Parser {
138136
digitStr = "";
139137
range.to = -1;
140138
} else if (token == 0x7d /* } */) {
141-
this.eatToken("}");
139+
this.eatToken(0x7d /* } */);
142140
// close brace, this is a single value range
143141
return range;
144142
} else {
@@ -153,7 +151,7 @@ export class Parser {
153151
} else {
154152
range.to = digitStr.length ? <i32>parseInt(digitStr) : -1;
155153
if (token == 0x7d /* } */) {
156-
this.eatToken("}");
154+
this.eatToken(0x7d /* } */);
157155
// close brace, end of range
158156
return range;
159157
} else {
@@ -167,7 +165,7 @@ export class Parser {
167165

168166
// repetition not found - reset state
169167
this.cursor = previousCursor;
170-
this.currentToken = this.input.charAt(this.cursor);
168+
this.currentToken = this.input.charAt(previousCursor);
171169

172170
return range;
173171
}
@@ -180,14 +178,14 @@ export class Parser {
180178
if (token == 0x29 /* ) */) break;
181179
// @ts-ignore
182180
if (token == 0x7c /* | */) {
183-
this.eatToken("|");
181+
this.eatToken(0x7c /* | */);
184182
const left = nodes.length > 1 ? new ConcatenationNode(nodes) : nodes[0];
185183
nodes = [new AlternationNode(left, this.parseSequence())];
186184
// @ts-ignore
187185
} else if (token == 0x28 /* ( */) {
188-
this.eatToken("(");
186+
this.eatToken(0x28 /* ( */);
189187
nodes.push(new GroupNode(this.parseSequence()));
190-
this.eatToken(")");
188+
this.eatToken(0x29 /* ) */);
191189
// @ts-ignore
192190
} else if (token == 0x7b /* { */) {
193191
const range = this.maybeParseRepetitionRange();
@@ -215,10 +213,10 @@ export class Parser {
215213

216214
private parseCharacterSet(): CharacterSetNode {
217215
let chars = "";
218-
this.eatToken("[");
216+
this.eatToken(0x5b /* [ */);
219217
const negated = this.currentToken == "^";
220218
if (negated) {
221-
this.eatToken("^");
219+
this.eatToken(0x5e /* ^ */);
222220
}
223221
while (
224222
this.currentToken != "]" ||
@@ -229,7 +227,7 @@ export class Parser {
229227
this.eatToken();
230228
// TODO error if we run out of chars?
231229
}
232-
this.eatToken("]");
230+
this.eatToken(0x5d /* ] */);
233231
return new CharacterSetNode(chars, negated);
234232
}
235233
}

0 commit comments

Comments
 (0)