Search in sources :

Example 11 with ASTNode

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

the class MVELInterpretedRuntime method procBooleanOperator.

private int procBooleanOperator(int operator) {
    switch(operator) {
        case RETURN:
            return RETURN;
        case NOOP:
            return -2;
        case AND:
            reduceRight();
            if (!stk.peekBoolean()) {
                if (unwindStatement(operator)) {
                    return -1;
                } else {
                    stk.clear();
                    return OP_RESET_FRAME;
                }
            } else {
                stk.discard();
                return OP_RESET_FRAME;
            }
        case OR:
            reduceRight();
            if (stk.peekBoolean()) {
                if (unwindStatement(operator)) {
                    return OP_TERMINATE;
                } else {
                    stk.clear();
                    return OP_RESET_FRAME;
                }
            } else {
                stk.discard();
                return OP_RESET_FRAME;
            }
        case CHOR:
            if (!BlankLiteral.INSTANCE.equals(stk.peek())) {
                return OP_TERMINATE;
            }
            break;
        case TERNARY:
            if (!stk.popBoolean()) {
                stk.clear();
                ASTNode tk;
                for (; ; ) {
                    if ((tk = nextToken()) == null || tk.isOperator(Operator.TERNARY_ELSE))
                        break;
                }
            }
            return OP_RESET_FRAME;
        case TERNARY_ELSE:
            captureToEOS();
            return OP_RESET_FRAME;
        case END_OF_STMT:
            if (hasMore()) {
                holdOverRegister = stk.pop();
                stk.clear();
            }
            return OP_RESET_FRAME;
    }
    return OP_CONTINUE;
}
Also used : ASTNode(org.mvel2.ast.ASTNode)

Example 12 with ASTNode

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

the class DebuggerTests method testBreakpoints4.

public void testBreakpoints4() {
    String expression = "System.out.println('foo');\n" + "a = new Foo244();\n" + "update (a) { name = 'bar' };\n" + "System.out.println('name:' + a.name);\n" + "return a.name;";
    Map<String, Interceptor> interceptors = new HashMap<String, Interceptor>();
    Map<String, Macro> macros = new HashMap<String, Macro>();
    class TestResult {

        boolean firedBefore;

        boolean firedAfter;
    }
    final TestResult result = new TestResult();
    interceptors.put("Update", new Interceptor() {

        public int doBefore(ASTNode node, VariableResolverFactory factory) {
            ((WithNode) node).getNestedStatement().getValue(null, factory);
            System.out.println("fired update interceptor -- before");
            result.firedBefore = true;
            return 0;
        }

        public int doAfter(Object val, ASTNode node, VariableResolverFactory factory) {
            System.out.println("fired update interceptor -- after");
            result.firedAfter = true;
            return 0;
        }
    });
    macros.put("update", new Macro() {

        public String doMacro() {
            return "@Update with";
        }
    });
    expression = parseMacros(expression, macros);
    ExpressionCompiler compiler = new ExpressionCompiler(expression);
    ParserContext ctx = new ParserContext();
    ctx.setDebugSymbols(true);
    ctx.setSourceFile("test2.mv");
    ctx.addImport("Foo244", Foo.class);
    ctx.setInterceptors(interceptors);
    CompiledExpression compiled = compiler.compile(ctx);
    System.out.println("\nExpression:------------");
    System.out.println(expression);
    System.out.println("------------");
    MVELRuntime.registerBreakpoint("test2.mv", 3);
    MVELRuntime.registerBreakpoint("test2.mv", 4);
    MVELRuntime.registerBreakpoint("test2.mv", 5);
    final Set<Integer> breaked = new HashSet<Integer>();
    Debugger testDebugger = new Debugger() {

        public int onBreak(Frame frame) {
            System.out.println("Breakpoint [source:" + frame.getSourceName() + "; line:" + frame.getLineNumber() + "]");
            breaked.add(frame.getLineNumber());
            return 0;
        }
    };
    MVELRuntime.setThreadDebugger(testDebugger);
    assertEquals("bar", MVEL.executeDebugger(compiled, null, new MapVariableResolverFactory(createTestMap())));
    assertTrue("did not fire before", result.firedBefore);
    assertTrue("did not fire after", result.firedAfter);
    assertEquals("did not break at expected points", Make.Set.<Integer>$()._(3)._(4)._(5)._(), breaked);
}
Also used : Debugger(org.mvel2.debug.Debugger) Frame(org.mvel2.debug.Frame) HashMap(java.util.HashMap) Macro(org.mvel2.Macro) WithNode(org.mvel2.ast.WithNode) CompiledExpression(org.mvel2.compiler.CompiledExpression) DefaultLocalVariableResolverFactory(org.mvel2.integration.impl.DefaultLocalVariableResolverFactory) VariableResolverFactory(org.mvel2.integration.VariableResolverFactory) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) ASTNode(org.mvel2.ast.ASTNode) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) ParserContext(org.mvel2.ParserContext) Interceptor(org.mvel2.integration.Interceptor) HashSet(java.util.HashSet)

Example 13 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 14 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 15 with ASTNode

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

the class MVELInterpretedRuntime method parseAndExecuteInterpreted.

/**
   * Main interpreter loop.
   *
   * @return value
   */
private Object parseAndExecuteInterpreted() {
    ASTNode tk = null;
    int operator;
    lastWasIdentifier = false;
    try {
        while ((tk = nextToken()) != null) {
            holdOverRegister = null;
            if (lastWasIdentifier && lastNode.isDiscard()) {
                stk.discard();
            }
            /**
         * If we are at the beginning of a statement, then we immediately push the first token
         * onto the stack.
         */
            if (stk.isEmpty()) {
                stk.push(tk.getReducedValue(ctx, ctx, variableFactory));
                /**
           * If this is a substatement, we need to move the result into the d-stack to preserve
           * proper execution order.
           */
                if (tk instanceof Substatement && (tk = nextToken()) != null) {
                    if (isArithmeticOperator(operator = tk.getOperator())) {
                        stk.push(nextToken().getReducedValue(ctx, ctx, variableFactory), operator);
                        if (procBooleanOperator(arithmeticFunctionReduction(operator)) == -1)
                            return stk.peek();
                        else
                            continue;
                    }
                } else {
                    continue;
                }
            }
            if (variableFactory.tiltFlag()) {
                return stk.pop();
            }
            switch(procBooleanOperator(operator = tk.getOperator())) {
                case RETURN:
                    variableFactory.setTiltFlag(true);
                    return stk.pop();
                case OP_TERMINATE:
                    return stk.peek();
                case OP_RESET_FRAME:
                    continue;
                case OP_OVERFLOW:
                    if (!tk.isOperator()) {
                        if (!(stk.peek() instanceof Class)) {
                            throw new CompileException("unexpected token or unknown identifier:" + tk.getName(), expr, st);
                        }
                        variableFactory.createVariable(tk.getName(), null, (Class) stk.peek());
                    }
                    continue;
            }
            stk.push(nextToken().getReducedValue(ctx, ctx, variableFactory), operator);
            switch((operator = arithmeticFunctionReduction(operator))) {
                case OP_TERMINATE:
                    return stk.peek();
                case OP_RESET_FRAME:
                    continue;
            }
            if (procBooleanOperator(operator) == OP_TERMINATE)
                return stk.peek();
        }
        if (holdOverRegister != null) {
            return holdOverRegister;
        }
    } catch (CompileException e) {
        throw ErrorUtil.rewriteIfNeeded(e, expr, start);
    } catch (NullPointerException e) {
        if (tk != null && tk.isOperator()) {
            CompileException ce = new CompileException("incomplete statement: " + tk.getName() + " (possible use of reserved keyword as identifier: " + tk.getName() + ")", expr, st, e);
            ce.setExpr(expr);
            ce.setLineNumber(line);
            ce.setCursor(cursor);
            throw ce;
        } else {
            throw e;
        }
    }
    return stk.peek();
}
Also used : ASTNode(org.mvel2.ast.ASTNode) Substatement(org.mvel2.ast.Substatement)

Aggregations

ASTNode (org.mvel2.ast.ASTNode)11 AstNode (com.linkedin.pinot.pql.parsers.pql2.ast.AstNode)5 Interceptor (org.mvel2.integration.Interceptor)4 VariableResolverFactory (org.mvel2.integration.VariableResolverFactory)4 MapVariableResolverFactory (org.mvel2.integration.impl.MapVariableResolverFactory)4 FunctionCallAstNode (com.linkedin.pinot.pql.parsers.pql2.ast.FunctionCallAstNode)3 IdentifierAstNode (com.linkedin.pinot.pql.parsers.pql2.ast.IdentifierAstNode)3 HashMap (java.util.HashMap)3 CompileException (org.mvel2.CompileException)3 BetweenPredicateAstNode (com.linkedin.pinot.pql.parsers.pql2.ast.BetweenPredicateAstNode)2 BinaryMathOpAstNode (com.linkedin.pinot.pql.parsers.pql2.ast.BinaryMathOpAstNode)2 BooleanOperatorAstNode (com.linkedin.pinot.pql.parsers.pql2.ast.BooleanOperatorAstNode)2 ComparisonPredicateAstNode (com.linkedin.pinot.pql.parsers.pql2.ast.ComparisonPredicateAstNode)2 ExpressionParenthesisGroupAstNode (com.linkedin.pinot.pql.parsers.pql2.ast.ExpressionParenthesisGroupAstNode)2 FloatingPointLiteralAstNode (com.linkedin.pinot.pql.parsers.pql2.ast.FloatingPointLiteralAstNode)2 GroupByAstNode (com.linkedin.pinot.pql.parsers.pql2.ast.GroupByAstNode)2 HavingAstNode (com.linkedin.pinot.pql.parsers.pql2.ast.HavingAstNode)2 InPredicateAstNode (com.linkedin.pinot.pql.parsers.pql2.ast.InPredicateAstNode)2 IntegerLiteralAstNode (com.linkedin.pinot.pql.parsers.pql2.ast.IntegerLiteralAstNode)2 IsPredicateAstNode (com.linkedin.pinot.pql.parsers.pql2.ast.IsPredicateAstNode)2