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