@parser-generator/grammar-interpreter
Version:
A Parser Generator that supports LL,SLR,LR1,LALR
66 lines • 1.63 kB
JavaScript
import { AbstractRegexpLexer, EOF } from "@parser-generator/definition";
var Tag;
(function (Tag) {
Tag["EOF"] = "EOF";
Tag["NUM"] = "NUM";
Tag["SINGLE"] = "SINGLE";
Tag["SPACE"] = "SPACE";
})(Tag || (Tag = {}));
export class Token {
toString() {
return this.value().toString();
}
}
let DIGIT;
export class Single extends Token {
constructor(lexeme) {
super();
this.lexeme = lexeme;
}
value() {
return this.lexeme;
}
key() {
return this.lexeme;
}
}
export class Num extends Token {
constructor(lexeme) {
super();
this.lexeme = lexeme;
}
key() {
return DIGIT;
}
value() {
return this.lexeme;
}
}
class ArithmeticLexer extends AbstractRegexpLexer {
constructor(text) {
super(text, [
{ regexp: /(?<space>\s+)/y, type: Tag.SPACE },
{ regexp: /(?<num>(0(?![0-9]))|([1-9]\d*(?!\.)))/y, type: Tag.NUM },
{ regexp: /(?<real>([1-9]\d*\.\d+)|(0\.\d+))/y, type: Tag.NUM },
{ regexp: /(?<single>.)/y, type: Tag.SINGLE }
]);
}
createToken(lexeme, type, match) {
switch (type) {
case Tag.SPACE:
return undefined; /* it means ignoring the match */
case Tag.NUM:
return new Num(parseFloat(lexeme));
case Tag.SINGLE:
return new Single(lexeme);
}
}
getEOF() {
return EOF;
}
}
export function getLexer(text, digit) {
DIGIT = digit;
return new ArithmeticLexer(text);
}
//# sourceMappingURL=lexer.js.map