Created
June 15, 2016 13:33
-
-
Save mythmon/dba7d5763e7fe6e420004a22e9e4851c to your computer and use it in GitHub Desktop.
Jexl custom evaluator shim
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
import {elements as defaultGrammar} from 'jexl/lib/grammar.js'; | |
import defaultParser from 'jexl/lib/parser/Parser.js'; | |
import defaultEvaluator from 'jexl/lib/evaluator/Evaluator.js'; | |
import defaultLexer from 'jexl/lib/Lexer.js'; | |
export function customEval( | |
expr, | |
context={}, | |
{ | |
transforms={}, | |
grammar=defaultGrammar, | |
parser=defaultParser, | |
evaluator=defaultEvaluator, | |
lexer=defaultLexer, | |
}={} | |
) { | |
const parserInst = new parser(grammar); | |
const evaluatorInst = new evaluator(grammar, transforms, context); | |
const lexerInst = new lexer(grammar); | |
const tokens = lexerInst.tokenize(expr); | |
parserInst.addTokens(tokens); | |
const ast = parserInst.complete(); | |
console.log('ast =', JSON.stringify(ast, null, 2)); | |
return evaluatorInst.eval(ast); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment