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)));
}
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));
}
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);
}
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;
}
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);
}
}
Aggregations