Search in sources :

Example 46 with ASTNode

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

the class MacroProcessorTest method testMacroSupportWithDebugging.

public void testMacroSupportWithDebugging() {
    Map<String, Object> vars = new HashMap<String, Object>();
    vars.put("foo", new Foo());
    Map<String, Interceptor> interceptors = new HashMap<String, Interceptor>();
    Map<String, Macro> macros = new HashMap<String, Macro>();
    interceptors.put("Modify", new Interceptor() {

        public int doBefore(ASTNode node, VariableResolverFactory factory) {
            ((WithNode) node).getNestedStatement().getValue(null, factory);
            factory.createVariable("mod", "FOOBAR!");
            return 0;
        }

        public int doAfter(Object val, ASTNode node, VariableResolverFactory factory) {
            return 0;
        }
    });
    macros.put("modify", new Macro() {

        public String doMacro() {
            return "@Modify with";
        }
    });
    ParserContext ctx = new ParserContext(null, interceptors, null);
    ctx.setSourceFile("test.mv");
    ctx.setDebugSymbols(true);
    ExpressionCompiler compiler = new ExpressionCompiler(parseMacros("System.out.println('hello');\n" + "System.out.println('bye');\n" + "modify (foo) { aValue = 'poo', \n" + " aValue = 'poo' };\n mod", macros), ctx);
    // compiler.setDebugSymbols(true);
    CompiledExpression compiled = compiler.compile();
    MVELRuntime.setThreadDebugger(new Debugger() {

        public int onBreak(Frame frame) {
            System.out.println(frame.getSourceName() + ":" + frame.getLineNumber());
            return Debugger.STEP;
        }
    });
    MVELRuntime.registerBreakpoint("test.mv", 3);
    System.out.println(DebugTools.decompile(compiled));
    Assert.assertEquals("FOOBAR!", MVEL.executeDebugger(compiled, null, new MapVariableResolverFactory(vars)));
}
Also used : Debugger(org.mvel2.debug.Debugger) Frame(org.mvel2.debug.Frame) HashMap(java.util.HashMap) Macro(org.mvel2.Macro) Foo(org.mvel2.tests.core.res.Foo) WithNode(org.mvel2.ast.WithNode) CompiledExpression(org.mvel2.compiler.CompiledExpression) 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)

Example 47 with ASTNode

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

the class MacroProcessorTest method testMacroSupport.

public void testMacroSupport() {
    Map<String, Object> vars = new HashMap<String, Object>();
    vars.put("foo", new Foo());
    Map<String, Interceptor> interceptors = new HashMap<String, Interceptor>();
    Map<String, Macro> macros = new HashMap<String, Macro>();
    interceptors.put("Modify", new Interceptor() {

        public int doBefore(ASTNode node, VariableResolverFactory factory) {
            ((WithNode) node).getNestedStatement().getValue(null, factory);
            factory.createVariable("mod", "FOOBAR!");
            return 0;
        }

        public int doAfter(Object val, ASTNode node, VariableResolverFactory factory) {
            return 0;
        }
    });
    macros.put("modify", new Macro() {

        public String doMacro() {
            return "@Modify with";
        }
    });
    ParserContext ctx = new ParserContext(null, interceptors, null);
    ctx.setSourceFile("test.mv");
    ctx.setDebugSymbols(true);
    ExpressionCompiler compiler = new ExpressionCompiler(parseMacros("modify (foo) { aValue = 'poo = poo', bValue = 'poo, poo' }; mod", macros), ctx);
    assertEquals("FOOBAR!", executeExpression(compiler.compile(), null, vars));
}
Also used : HashMap(java.util.HashMap) Macro(org.mvel2.Macro) Foo(org.mvel2.tests.core.res.Foo) WithNode(org.mvel2.ast.WithNode) VariableResolverFactory(org.mvel2.integration.VariableResolverFactory) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) ASTNode(org.mvel2.ast.ASTNode) ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) ParserContext(org.mvel2.ParserContext) Interceptor(org.mvel2.integration.Interceptor)

Example 48 with ASTNode

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

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);
    ParserContext ctx = new ParserContext();
    ctx.setDebugSymbols(true);
    ctx.setSourceFile("test2.mv");
    ctx.addImport("Foo244", Foo.class);
    ctx.setInterceptors(interceptors);
    ExpressionCompiler compiler = new ExpressionCompiler(expression, ctx);
    CompiledExpression compiled = compiler.compile();
    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)._finish(), 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 49 with ASTNode

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

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 50 with ASTNode

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

the class AbstractParser method createBlockToken.

/**
 * Generate a code block token.
 *
 * @param condStart  the start offset for the condition
 * @param condEnd    the end offset for the condition
 * @param blockStart the start offset for the block
 * @param blockEnd   the end offset for the block
 * @param type       the type of block
 * @return and ast node
 */
private ASTNode createBlockToken(final int condStart, final int condEnd, final int blockStart, final int blockEnd, int type) {
    lastWasIdentifier = false;
    cursor++;
    if (isStatementNotManuallyTerminated()) {
        splitAccumulator.add(new EndOfStatement(pCtx));
    }
    int condOffset = condEnd - condStart;
    int blockOffset = blockEnd - blockStart;
    if (blockOffset < 0)
        blockOffset = 0;
    switch(type) {
        case ASTNode.BLOCK_IF:
            return new IfNode(expr, condStart, condOffset, blockStart, blockOffset, fields, pCtx);
        case ASTNode.BLOCK_FOR:
            for (int i = condStart; i < condEnd; i++) {
                if (expr[i] == ';')
                    return new ForNode(expr, condStart, condOffset, blockStart, blockOffset, fields, pCtx);
                else if (expr[i] == ':')
                    break;
            }
        case ASTNode.BLOCK_FOREACH:
            return new ForEachNode(expr, condStart, condOffset, blockStart, blockOffset, fields, pCtx);
        case ASTNode.BLOCK_WHILE:
            return new WhileNode(expr, condStart, condOffset, blockStart, blockOffset, fields, pCtx);
        case ASTNode.BLOCK_UNTIL:
            return new UntilNode(expr, condStart, condOffset, blockStart, blockOffset, fields, pCtx);
        case ASTNode.BLOCK_DO:
            return new DoNode(expr, condStart, condOffset, blockStart, blockOffset, fields, pCtx);
        case ASTNode.BLOCK_DO_UNTIL:
            return new DoUntilNode(expr, condStart, condOffset, blockStart, blockOffset, pCtx);
        default:
            return new WithNode(expr, condStart, condOffset, blockStart, blockOffset, fields, pCtx);
    }
}
Also used : UntilNode(org.mvel2.ast.UntilNode) DoUntilNode(org.mvel2.ast.DoUntilNode) ForNode(org.mvel2.ast.ForNode) EndOfStatement(org.mvel2.ast.EndOfStatement) DoNode(org.mvel2.ast.DoNode) WhileNode(org.mvel2.ast.WhileNode) ForEachNode(org.mvel2.ast.ForEachNode) IfNode(org.mvel2.ast.IfNode) DoUntilNode(org.mvel2.ast.DoUntilNode) WithNode(org.mvel2.ast.WithNode) ThisWithNode(org.mvel2.ast.ThisWithNode)

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