diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/lexer.js | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/src/lexer.js b/src/lexer.js new file mode 100644 index 0000000..ff7b0d4 --- /dev/null +++ b/src/lexer.js @@ -0,0 +1,44 @@ +const lexemeTypes = [] + +const newLexemeType = (type, regex, adder) => { + lexemeTypes.push({ + type, + regex: new RegExp(`^(${regex})(.*)$`), + adder: adder || (lexemes => { lexemes.push({ type }) }) + }) +} + +const newValueLexeme = (type, regex, converter = v => v) => { + newLexemeType(type, regex, (lexemes, value) => { + lexemes.push({ type, value: converter(value) }) + }) +} + +newValueLexeme('number', '\\d+', Number) +newLexemeType('d', 'd') + +const lex = (expressionString) => { + let lexemes = [] + + while (expressionString.length > 0) { + let matched = false + + lexemeTypes.forEach(lexemeType => { + let matches = lexemeType.regex.exec(expressionString) + + if (matches) { + matched = true + lexemeType.adder(lexemes, matches[1]) + expressionString = matches[2] + } + }) + + if (!matched) { + return 'error' + } + } + + return lexemes +} + +exports.lex = lex |