use of com.dexels.navajo.parser.compiled.CompiledParser in project navajo by Dexels.
the class ExpressionCache method parse.
public ContextExpression parse(List<String> problems, String expression, Function<String, FunctionClassification> functionClassifier, Function<String, Optional<Node>> mapResolver) {
Optional<ContextExpression> cachedParsedExpression = exprCache.getUnchecked(expression);
if (cachedParsedExpression.isPresent()) {
hitCount.incrementAndGet();
return cachedParsedExpression.get();
}
CompiledParser cp;
try {
StringReader sr = new StringReader(expression);
cp = new CompiledParser(sr);
cp.Expression();
ContextExpression parsed = cp.getJJTree().rootNode().interpretToLambda(problems, expression, functionClassifier, mapResolver);
parsedCount.incrementAndGet();
if (parsed.isLiteral()) {
Operand result = parsed.apply();
exprCache.put(expression, Optional.ofNullable(parsed));
if (result != null) {
expressionValueCache.put(expression, Optional.of(result));
}
return new ContextExpression() {
@Override
public boolean isLiteral() {
return true;
}
@Override
public Operand apply(Navajo doc, Message parentMsg, Message parentParamMsg, Selection parentSel, MappableTreeNode mapNode, TipiLink tipiLink, Access access, Optional<ImmutableMessage> immutableMessage, Optional<ImmutableMessage> paramMessage) {
return result;
}
@Override
public Optional<String> returnType() {
return Optional.ofNullable(result.type);
}
@Override
public String expression() {
return expression;
}
};
} else {
exprCache.put(expression, Optional.ofNullable(parsed));
return parsed;
}
} catch (ParseException e) {
throw new TMLExpressionException("Error parsing expression: " + expression, e);
} catch (Throwable e) {
throw new TMLExpressionException("Unexpected error parsing expression: " + expression, e);
}
}
Aggregations