Search in sources :

Example 1 with CompiledParser

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);
    }
}
Also used : MappableTreeNode(com.dexels.navajo.script.api.MappableTreeNode) ImmutableMessage(com.dexels.immutable.api.ImmutableMessage) Message(com.dexels.navajo.document.Message) Optional(java.util.Optional) Operand(com.dexels.navajo.document.Operand) Selection(com.dexels.navajo.document.Selection) ContextExpression(com.dexels.navajo.expression.api.ContextExpression) Access(com.dexels.navajo.script.api.Access) Navajo(com.dexels.navajo.document.Navajo) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException) CompiledParser(com.dexels.navajo.parser.compiled.CompiledParser) TipiLink(com.dexels.navajo.expression.api.TipiLink) StringReader(java.io.StringReader) ParseException(com.dexels.navajo.parser.compiled.ParseException)

Aggregations

ImmutableMessage (com.dexels.immutable.api.ImmutableMessage)1 Message (com.dexels.navajo.document.Message)1 Navajo (com.dexels.navajo.document.Navajo)1 Operand (com.dexels.navajo.document.Operand)1 Selection (com.dexels.navajo.document.Selection)1 ContextExpression (com.dexels.navajo.expression.api.ContextExpression)1 TMLExpressionException (com.dexels.navajo.expression.api.TMLExpressionException)1 TipiLink (com.dexels.navajo.expression.api.TipiLink)1 CompiledParser (com.dexels.navajo.parser.compiled.CompiledParser)1 ParseException (com.dexels.navajo.parser.compiled.ParseException)1 Access (com.dexels.navajo.script.api.Access)1 MappableTreeNode (com.dexels.navajo.script.api.MappableTreeNode)1 StringReader (java.io.StringReader)1 Optional (java.util.Optional)1