-
Notifications
You must be signed in to change notification settings - Fork 204
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixes #528
- Loading branch information
Showing
4 changed files
with
204 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
* parser.input method | ||
- Maybe the parser should not be able to accept tokenVector array at constructor? | ||
- the input should have an any (or generic T argument?) not specific to tokenVector |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
// Using TypeScript we have both classes and static properties to define Tokens | ||
|
||
import { createToken, IToken, Token } from "../../../src/scan/tokens_public" | ||
import { Lexer } from "../../../src/scan/lexer_public" | ||
import { END_OF_FILE, Parser } from "../../../src/parse/parser_public" | ||
|
||
const Return = createToken({ | ||
name: "Return", | ||
pattern: /return/y | ||
}) | ||
|
||
const DivisionOperator = createToken({ | ||
name: "DivisionOperator", | ||
pattern: /\//y | ||
}) | ||
|
||
const RegExpLiteral = createToken({ | ||
name: "RegExpLiteral", | ||
pattern: /\/\d+\//y | ||
}) | ||
|
||
const NumberLiteral = createToken({ | ||
name: "NumberLiteral", | ||
pattern: /\d+/y | ||
}) | ||
|
||
// todo differentiate line terminators and other whitespace? | ||
const WhiteSpace = createToken({ | ||
name: "WhiteSpace", | ||
pattern: /\s+/y, | ||
group: Lexer.SKIPPED, | ||
line_breaks: true | ||
}) | ||
|
||
const Semicolon = createToken({ | ||
name: "Semicolon", | ||
pattern: /;/y | ||
}) | ||
|
||
const allTokens = [WhiteSpace, NumberLiteral] | ||
|
||
class EcmaScriptQuirksParser extends Parser { | ||
constructor(input: Token[]) { | ||
super(input, allTokens) | ||
Parser.performSelfAnalysis(this) | ||
} | ||
|
||
public statement = this.RULE("statement", () => { | ||
this.CONSUME(Return) | ||
this.SUBRULE(this.expression) | ||
this.CONSUME(Semicolon) | ||
}) | ||
|
||
public expression = this.RULE("expression", () => { | ||
this.SUBRULE(this.atomic) | ||
this.MANY(() => { | ||
this.CONSUME(DivisionOperator) | ||
this.SUBRULE2(this.atomic) | ||
}) | ||
}) | ||
|
||
public atomic = this.RULE("atomic", () => { | ||
this.OR([ | ||
{ ALT: () => this.CONSUME(RegExpLiteral) }, | ||
{ ALT: () => this.CONSUME(NumberLiteral) } | ||
]) | ||
}) | ||
|
||
private orgText | ||
private textIdx | ||
|
||
// lexer related methods | ||
public set textInput(newInput: string) { | ||
this.reset() | ||
this.orgText = newInput | ||
} | ||
|
||
public get textInput(): string { | ||
return this.orgText | ||
} | ||
|
||
protected resetLexerState(): void { | ||
this.textIdx = 0 | ||
} | ||
|
||
protected LA(howMuch: number): IToken { | ||
if (this.orgText.length <= this.textIdx) { | ||
return END_OF_FILE | ||
} else { | ||
} | ||
} | ||
} | ||
|
||
// reuse the same parser instance. | ||
const parser = new EcmaScriptQuirksParser([]) | ||
|
||
export function parse(text): any { | ||
parser.input = text | ||
let value = parser.statement() | ||
|
||
return { | ||
value: value, | ||
parseErrors: parser.errors | ||
} | ||
} |
Empty file.