Skip to content

opt eatToken #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 24, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 18 additions & 20 deletions assembly/parser/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,13 @@ export class Parser {
return new Parser(input).toAST();
}

private eatToken(value: string = ""): u32 {
const current = this.currentToken;
if (value.length > 0 && current != value) {
private eatToken(value: u32 = -1): u32 {
const token = this.currentToken.charCodeAt(0) as u32;
if (value != -1 && token != value) {
throw new Error("invalid token");
}

this.cursor++;
this.currentToken = this.input.charAt(this.cursor);
return current.charCodeAt(0);
this.currentToken = this.input.charAt(++this.cursor);
return token;
}

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

private resetCursor(): void {
this.cursor = 0;
this.currentToken = this.input.charAt(this.cursor);
this.currentToken = this.input.charAt(0);
}

private toAST(): AST {
Expand All @@ -89,7 +87,7 @@ export class Parser {
private parseCharacter(): Node {
let token = this.currentToken.charCodeAt(0);
if (token == 0x5c /* \ */) {
this.eatToken("\\");
this.eatToken(0x5c);
token = this.currentToken.charCodeAt(0);
if (isSpecialCharacter(token)) {
this.eatToken();
Expand All @@ -106,7 +104,7 @@ export class Parser {
}

if (token == CharClass.Dot) {
this.eatToken(".");
this.eatToken(CharClass.Dot);
return new CharacterClassNode(CharClass.Dot);
}

Expand All @@ -116,7 +114,7 @@ export class Parser {
private maybeParseRepetitionRange(): Range {
// snapshot
const previousCursor = this.cursor;
this.eatToken("{");
this.eatToken(0x7b /* { */);

let range = new Range();

Expand All @@ -138,7 +136,7 @@ export class Parser {
digitStr = "";
range.to = -1;
} else if (token == 0x7d /* } */) {
this.eatToken("}");
this.eatToken(0x7d /* } */);
// close brace, this is a single value range
return range;
} else {
Expand All @@ -153,7 +151,7 @@ export class Parser {
} else {
range.to = digitStr.length ? <i32>parseInt(digitStr) : -1;
if (token == 0x7d /* } */) {
this.eatToken("}");
this.eatToken(0x7d /* } */);
// close brace, end of range
return range;
} else {
Expand All @@ -167,7 +165,7 @@ export class Parser {

// repetition not found - reset state
this.cursor = previousCursor;
this.currentToken = this.input.charAt(this.cursor);
this.currentToken = this.input.charAt(previousCursor);

return range;
}
Expand All @@ -180,14 +178,14 @@ export class Parser {
if (token == 0x29 /* ) */) break;
// @ts-ignore
if (token == 0x7c /* | */) {
this.eatToken("|");
this.eatToken(0x7c /* | */);
const left = nodes.length > 1 ? new ConcatenationNode(nodes) : nodes[0];
nodes = [new AlternationNode(left, this.parseSequence())];
// @ts-ignore
} else if (token == 0x28 /* ( */) {
this.eatToken("(");
this.eatToken(0x28 /* ( */);
nodes.push(new GroupNode(this.parseSequence()));
this.eatToken(")");
this.eatToken(0x29 /* ) */);
// @ts-ignore
} else if (token == 0x7b /* { */) {
const range = this.maybeParseRepetitionRange();
Expand Down Expand Up @@ -215,10 +213,10 @@ export class Parser {

private parseCharacterSet(): CharacterSetNode {
let chars = "";
this.eatToken("[");
this.eatToken(0x5b /* [ */);
const negated = this.currentToken == "^";
if (negated) {
this.eatToken("^");
this.eatToken(0x5e /* ^ */);
}
while (
this.currentToken != "]" ||
Expand All @@ -229,7 +227,7 @@ export class Parser {
this.eatToken();
// TODO error if we run out of chars?
}
this.eatToken("]");
this.eatToken(0x5d /* ] */);
return new CharacterSetNode(chars, negated);
}
}