Skip to content

Instantly share code, notes, and snippets.

@mythmon
Created June 15, 2016 13:33
Embed
What would you like to do?
Jexl custom evaluator shim
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