Search in sources :

Example 26 with ASTNode

use of org.mvel2.ast.ASTNode in project mvel by mvel.

the class AbstractParser method procTypedNode.

/**
 * Process the current typed node
 *
 * @param decl node is a declaration or not
 * @return and ast node
 */
private ASTNode procTypedNode(boolean decl) {
    while (true) {
        if (lastNode.getLiteralValue() instanceof String) {
            char[] tmp = ((String) lastNode.getLiteralValue()).toCharArray();
            TypeDescriptor tDescr = new TypeDescriptor(tmp, 0, tmp.length, 0);
            try {
                lastNode.setLiteralValue(getClassReference(pCtx, tDescr));
                lastNode.discard();
            } catch (Exception e) {
            // fall through;
            }
        }
        if (lastNode.isLiteral() && lastNode.getLiteralValue() instanceof Class) {
            lastNode.discard();
            captureToEOS();
            if (decl) {
                splitAccumulator.add(new DeclTypedVarNode(new String(expr, st, cursor - st), expr, st, cursor - st, (Class) lastNode.getLiteralValue(), fields | ASTNode.ASSIGN, pCtx));
            } else {
                captureToEOS();
                splitAccumulator.add(new TypedVarNode(expr, st, cursor - st - 1, fields | ASTNode.ASSIGN, (Class) lastNode.getLiteralValue(), pCtx));
            }
        } else if (lastNode instanceof Proto) {
            captureToEOS();
            if (decl) {
                splitAccumulator.add(new DeclProtoVarNode(new String(expr, st, cursor - st), (Proto) lastNode, fields | ASTNode.ASSIGN, pCtx));
            } else {
                splitAccumulator.add(new ProtoVarNode(expr, st, cursor - st, fields | ASTNode.ASSIGN, (Proto) lastNode, pCtx));
            }
        } else // this redundant looking code is needed to work with the interpreter and MVELSH properly.
        if ((fields & ASTNode.COMPILE_IMMEDIATE) == 0) {
            if (stk.peek() instanceof Class) {
                captureToEOS();
                if (decl) {
                    splitAccumulator.add(new DeclTypedVarNode(new String(expr, st, cursor - st), expr, st, cursor - st, (Class) stk.pop(), fields | ASTNode.ASSIGN, pCtx));
                } else {
                    splitAccumulator.add(new TypedVarNode(expr, st, cursor - st, fields | ASTNode.ASSIGN, (Class) stk.pop(), pCtx));
                }
            } else if (stk.peek() instanceof Proto) {
                captureToEOS();
                if (decl) {
                    splitAccumulator.add(new DeclProtoVarNode(new String(expr, st, cursor - st), (Proto) stk.pop(), fields | ASTNode.ASSIGN, pCtx));
                } else {
                    splitAccumulator.add(new ProtoVarNode(expr, st, cursor - st, fields | ASTNode.ASSIGN, (Proto) stk.pop(), pCtx));
                }
            } else {
                throw new CompileException("unknown class or illegal statement: " + lastNode.getLiteralValue(), expr, cursor);
            }
        } else {
            throw new CompileException("unknown class or illegal statement: " + lastNode.getLiteralValue(), expr, cursor);
        }
        skipWhitespace();
        if (cursor < end && expr[cursor] == ',') {
            st = ++cursor;
            splitAccumulator.add(new EndOfStatement(pCtx));
        } else {
            return (ASTNode) splitAccumulator.pop();
        }
    }
}
Also used : ProtoVarNode(org.mvel2.ast.ProtoVarNode) DeclProtoVarNode(org.mvel2.ast.DeclProtoVarNode) EndOfStatement(org.mvel2.ast.EndOfStatement) CompileException(org.mvel2.CompileException) RedundantCodeException(org.mvel2.ast.RedundantCodeException) TypeDescriptor(org.mvel2.ast.TypeDescriptor) Proto(org.mvel2.ast.Proto) DeclProtoVarNode(org.mvel2.ast.DeclProtoVarNode) IndexedDeclTypedVarNode(org.mvel2.ast.IndexedDeclTypedVarNode) DeclTypedVarNode(org.mvel2.ast.DeclTypedVarNode) ASTNode(org.mvel2.ast.ASTNode) CompileException(org.mvel2.CompileException) IndexedDeclTypedVarNode(org.mvel2.ast.IndexedDeclTypedVarNode) TypedVarNode(org.mvel2.ast.TypedVarNode) DeclTypedVarNode(org.mvel2.ast.DeclTypedVarNode)

Example 27 with ASTNode

use of org.mvel2.ast.ASTNode in project mvel by mvel.

the class CompilerTools method extractAllDeclaredFunctions.

/**
 * Returns an ordered Map of all functions declared within an compiled script.
 *
 * @param compile compile
 * @return - ordered Map
 */
public static Map<String, Function> extractAllDeclaredFunctions(CompiledExpression compile) {
    Map<String, Function> allFunctions = new LinkedHashMap<String, Function>();
    ASTIterator instructions = new ASTLinkedList(compile.getFirstNode());
    ASTNode n;
    while (instructions.hasMoreNodes()) {
        if ((n = instructions.nextNode()) instanceof Function) {
            allFunctions.put(n.getName(), (Function) n);
        }
    }
    return allFunctions;
}
Also used : Function(org.mvel2.ast.Function) ASTNode(org.mvel2.ast.ASTNode) LinkedHashMap(java.util.LinkedHashMap)

Example 28 with ASTNode

use of org.mvel2.ast.ASTNode in project pinot by linkedin.

the class Pql2AstListener method popNode.

private void popNode() {
    AstNode topNode = _nodeStack.pop();
    topNode.doneProcessingChildren();
}
Also used : BetweenPredicateAstNode(com.linkedin.pinot.pql.parsers.pql2.ast.BetweenPredicateAstNode) IdentifierAstNode(com.linkedin.pinot.pql.parsers.pql2.ast.IdentifierAstNode) AstNode(com.linkedin.pinot.pql.parsers.pql2.ast.AstNode) FloatingPointLiteralAstNode(com.linkedin.pinot.pql.parsers.pql2.ast.FloatingPointLiteralAstNode) HavingAstNode(com.linkedin.pinot.pql.parsers.pql2.ast.HavingAstNode) OutputColumnAstNode(com.linkedin.pinot.pql.parsers.pql2.ast.OutputColumnAstNode) OrderByAstNode(com.linkedin.pinot.pql.parsers.pql2.ast.OrderByAstNode) OrderByExpressionAstNode(com.linkedin.pinot.pql.parsers.pql2.ast.OrderByExpressionAstNode) StarExpressionAstNode(com.linkedin.pinot.pql.parsers.pql2.ast.StarExpressionAstNode) LimitAstNode(com.linkedin.pinot.pql.parsers.pql2.ast.LimitAstNode) SelectAstNode(com.linkedin.pinot.pql.parsers.pql2.ast.SelectAstNode) WhereAstNode(com.linkedin.pinot.pql.parsers.pql2.ast.WhereAstNode) IsPredicateAstNode(com.linkedin.pinot.pql.parsers.pql2.ast.IsPredicateAstNode) StringLiteralAstNode(com.linkedin.pinot.pql.parsers.pql2.ast.StringLiteralAstNode) ExpressionParenthesisGroupAstNode(com.linkedin.pinot.pql.parsers.pql2.ast.ExpressionParenthesisGroupAstNode) FunctionCallAstNode(com.linkedin.pinot.pql.parsers.pql2.ast.FunctionCallAstNode) IntegerLiteralAstNode(com.linkedin.pinot.pql.parsers.pql2.ast.IntegerLiteralAstNode) TopAstNode(com.linkedin.pinot.pql.parsers.pql2.ast.TopAstNode) OutputColumnListAstNode(com.linkedin.pinot.pql.parsers.pql2.ast.OutputColumnListAstNode) BooleanOperatorAstNode(com.linkedin.pinot.pql.parsers.pql2.ast.BooleanOperatorAstNode) BinaryMathOpAstNode(com.linkedin.pinot.pql.parsers.pql2.ast.BinaryMathOpAstNode) ComparisonPredicateAstNode(com.linkedin.pinot.pql.parsers.pql2.ast.ComparisonPredicateAstNode) TableNameAstNode(com.linkedin.pinot.pql.parsers.pql2.ast.TableNameAstNode) InPredicateAstNode(com.linkedin.pinot.pql.parsers.pql2.ast.InPredicateAstNode) PredicateParenthesisGroupAstNode(com.linkedin.pinot.pql.parsers.pql2.ast.PredicateParenthesisGroupAstNode) GroupByAstNode(com.linkedin.pinot.pql.parsers.pql2.ast.GroupByAstNode) PredicateListAstNode(com.linkedin.pinot.pql.parsers.pql2.ast.PredicateListAstNode) StarColumnListAstNode(com.linkedin.pinot.pql.parsers.pql2.ast.StarColumnListAstNode)

Example 29 with ASTNode

use of org.mvel2.ast.ASTNode in project pinot by linkedin.

the class Pql2Compiler method compileToExpressionTree.

@Override
public TransformExpressionTree compileToExpressionTree(String expression) {
    CharStream charStream = new ANTLRInputStream(expression);
    PQL2Lexer lexer = new PQL2Lexer(charStream);
    lexer.setTokenFactory(new CommonTokenFactory(true));
    TokenStream tokenStream = new UnbufferedTokenStream<CommonToken>(lexer);
    PQL2Parser parser = new PQL2Parser(tokenStream);
    parser.setErrorHandler(new BailErrorStrategy());
    // Parse
    ParseTree parseTree = parser.expression();
    ParseTreeWalker walker = new ParseTreeWalker();
    Pql2AstListener listener = new Pql2AstListener(expression);
    walker.walk(listener, parseTree);
    final AstNode rootNode = listener.getRootNode();
    return TransformExpressionTree.buildTree(rootNode);
}
Also used : TokenStream(org.antlr.v4.runtime.TokenStream) UnbufferedTokenStream(org.antlr.v4.runtime.UnbufferedTokenStream) CommonTokenFactory(org.antlr.v4.runtime.CommonTokenFactory) BailErrorStrategy(org.antlr.v4.runtime.BailErrorStrategy) UnbufferedTokenStream(org.antlr.v4.runtime.UnbufferedTokenStream) CharStream(org.antlr.v4.runtime.CharStream) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) ParseTree(org.antlr.v4.runtime.tree.ParseTree) ParseTreeWalker(org.antlr.v4.runtime.tree.ParseTreeWalker) AstNode(com.linkedin.pinot.pql.parsers.pql2.ast.AstNode)

Example 30 with ASTNode

use of org.mvel2.ast.ASTNode in project drools by kiegroup.

the class ConditionAnalyzer method analyzeSingleCondition.

private SingleCondition analyzeSingleCondition(ASTNode node, boolean isNegated) {
    SingleCondition condition = new SingleCondition(isNegated);
    if (node instanceof BinaryOperation) {
        BinaryOperation binaryOperation = (BinaryOperation) node;
        condition.left = analyzeNode(binaryOperation.getLeft());
        condition.operation = BooleanOperator.fromMvelOpCode(binaryOperation.getOperation());
        condition.right = analyzeNode(binaryOperation.getRight());
    } else if (node instanceof RegExMatch) {
        condition.left = analyzeNode(node);
        condition.operation = BooleanOperator.MATCHES;
        RegExMatch regExNode = (RegExMatch) node;
        Pattern pattern = regExNode.getPattern();
        if (pattern != null) {
            condition.right = new FixedExpression(String.class, pattern.pattern());
        } else {
            condition.right = analyzeNode(((ExecutableAccessor) regExNode.getPatternStatement()).getNode());
        }
    } else if (node instanceof Contains) {
        condition.left = analyzeNode(((Contains) node).getFirstStatement());
        condition.operation = BooleanOperator.CONTAINS;
        condition.right = analyzeNode(((Contains) node).getSecondStatement());
    } else if (node instanceof Soundslike) {
        condition.left = analyzeNode(((Soundslike) node).getStatement());
        condition.operation = BooleanOperator.SOUNDSLIKE;
        condition.right = analyzeNode(((Soundslike) node).getSoundslike());
    } else if (node instanceof Instance) {
        condition.left = analyzeNode(((Instance) node).getStatement());
        condition.operation = BooleanOperator.INSTANCEOF;
        condition.right = analyzeNode(((Instance) node).getClassStatement());
    } else {
        condition.left = analyzeNode(node);
    }
    return condition;
}
Also used : Pattern(java.util.regex.Pattern) RegExMatch(org.mvel2.ast.RegExMatch) Soundslike(org.mvel2.ast.Soundslike) Instance(org.mvel2.ast.Instance) BinaryOperation(org.mvel2.ast.BinaryOperation) Contains(org.mvel2.ast.Contains)

Aggregations

ASTNode (org.mvel2.ast.ASTNode)37 CompileException (org.mvel2.CompileException)12 LiteralNode (org.mvel2.ast.LiteralNode)11 CompiledExpression (org.mvel2.compiler.CompiledExpression)8 Interceptor (org.mvel2.integration.Interceptor)8 VariableResolverFactory (org.mvel2.integration.VariableResolverFactory)8 MapVariableResolverFactory (org.mvel2.integration.impl.MapVariableResolverFactory)8 HashMap (java.util.HashMap)7 BinaryOperation (org.mvel2.ast.BinaryOperation)7 WithNode (org.mvel2.ast.WithNode)7 ExecutableStatement (org.mvel2.compiler.ExecutableStatement)7 ParserContext (org.mvel2.ParserContext)6 Substatement (org.mvel2.ast.Substatement)6 ExecutableLiteral (org.mvel2.compiler.ExecutableLiteral)6 ExpressionCompiler (org.mvel2.compiler.ExpressionCompiler)6 AstNode (com.linkedin.pinot.pql.parsers.pql2.ast.AstNode)5 OperatorNode (org.mvel2.ast.OperatorNode)5 Union (org.mvel2.ast.Union)5 ExecutableAccessor (org.mvel2.compiler.ExecutableAccessor)5 MethodAccessor (org.mvel2.optimizers.impl.refl.nodes.MethodAccessor)5