A compact and expressive DSL and expression interpreter toolkit
$ npm install langkit
import { createLexer, createParser } from 'langkit';
const calcLexer = createLexer({
plus: /\+/,
number: /\d+(?:\.\d+)?/,
space: {
pattern: /\s+/,
skip: true,
},
});
const calculate = createParser(calcLexer, (h) => {
return h.fromTokens(['number', 'plus', 'number'], ([left, _, right]) => {
return Number(left.value) + Number(right.value);
});
});
console.log(calculate('5 + 45'));
// => 50