Created
July 9, 2021 02:14
-
-
Save SpiffGreen/72e1164e3f9e4e5f77516c42eb26f727 to your computer and use it in GitHub Desktop.
A simple regular expression based lexer generator
This file contains hidden or 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
/** | |
* @description A simple lexer generator in javascript | |
*/ | |
class LexToken { | |
constructor(name, value, index, row, col) { | |
this.name = name; | |
this.value = value; | |
this.index = index; | |
this.line = row; | |
this.col = col; | |
} | |
} | |
class LexerGenerator { | |
constructor() { | |
this.lexemes = {}; | |
this.buildReg = ""; | |
this.add = this.add.bind(this); | |
this.build = this.build.bind(this); | |
} | |
add(label, grammar) { | |
this.buildReg.length > 1 ? this.buildReg += `|${grammar}` : this.buildReg = grammar; | |
return this; | |
} | |
build() { | |
this.buildReg = new RegExp(this.buildReg, "g"); | |
return this; | |
} | |
lex(str) { | |
return str.matchAll(this.buildReg); | |
} | |
} | |
const lexerGen = new LexerGenerator(); | |
lexerGen.add("IF", "if"); | |
lexerGen.add("LBRACE", "\\("); | |
lexerGen.add("RBRACE", "\\)"); | |
lexerGen.add("LCURL", "\\{"); | |
lexerGen.add("RCURL", "\\}"); | |
lexerGen.add("VAR", "var"); | |
lexerGen.add("CONST", "const"); | |
lexerGen.add("EQUAL", "="); | |
lexerGen.add("NAME", "\[a-zA-Z_\]\[a-zA-Z0-9_\]\+"); | |
lexerGen.add("NUMBER", "\[0-9\]\+"); | |
lexerGen.add("STRING", "\".\*\""); | |
lexerGen.add("SEMICOLON", ";"); | |
lexerGen.add("PRINT", "print"); | |
lex = lexerGen.build(); | |
code = `{ | |
var name = "d"; | |
name = "Spiff Jekey-Green"; | |
print(name); | |
}`; | |
const toks = lex.lex(code); | |
let t = null; | |
while(!(t = toks.next()).done) { | |
console.log(t); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment