use of org.mvel2.ast.ForEachNode in project mvel by mvel.
the class TemplateCompiler method compileFrom.
public Node compileFrom(Node root, ExecutionStack stack) {
line = 1;
Node n = root;
if (root == null) {
n = root = new TextNode(0, 0);
}
IfNode last;
Integer opcode;
String name;
int x;
try {
while (cursor < length) {
switch(template[cursor]) {
case '\n':
line++;
colStart = cursor + 1;
break;
case '@':
case '$':
if (isNext(template[cursor])) {
start = ++cursor;
(n = markTextNode(n)).setEnd(n.getEnd() + 1);
start = lastTextRangeEnding = ++cursor;
continue;
}
if ((x = captureOrbToken()) != -1) {
start = x;
switch((opcode = OPCODES.get(name = new String(capture()))) == null ? 0 : opcode) {
case Opcodes.IF:
/**
* Capture any residual text node, and push the if statement on the nesting stack.
*/
stack.push(n = markTextNode(n).next = codeCache ? new CompiledIfNode(start, name, template, captureOrbInternal(), start, parserContext) : new IfNode(start, name, template, captureOrbInternal(), start));
n.setTerminus(new TerminalNode());
break;
case Opcodes.ELSE:
if (!stack.isEmpty() && stack.peek() instanceof IfNode) {
markTextNode(n).next = (last = (IfNode) stack.pop()).getTerminus();
last.demarcate(last.getTerminus(), template);
last.next = n = codeCache ? new CompiledIfNode(start, name, template, captureOrbInternal(), start, parserContext) : new IfNode(start, name, template, captureOrbInternal(), start);
n.setTerminus(last.getTerminus());
stack.push(n);
}
break;
case Opcodes.FOREACH:
stack.push(n = markTextNode(n).next = codeCache ? new CompiledForEachNode(start, name, template, captureOrbInternal(), start, parserContext) : new ForEachNode(start, name, template, captureOrbInternal(), start));
n.setTerminus(new TerminalNode());
break;
case Opcodes.INCLUDE_FILE:
n = markTextNode(n).next = codeCache ? new CompiledIncludeNode(start, name, template, captureOrbInternal(), start = cursor + 1, parserContext) : new IncludeNode(start, name, template, captureOrbInternal(), start = cursor + 1);
break;
case Opcodes.INCLUDE_NAMED:
n = markTextNode(n).next = codeCache ? new CompiledNamedIncludeNode(start, name, template, captureOrbInternal(), start = cursor + 1, parserContext) : new NamedIncludeNode(start, name, template, captureOrbInternal(), start = cursor + 1);
break;
case Opcodes.CODE:
n = markTextNode(n).next = codeCache ? new CompiledCodeNode(start, name, template, captureOrbInternal(), start = cursor + 1, parserContext) : new CodeNode(start, name, template, captureOrbInternal(), start = cursor + 1);
break;
case Opcodes.EVAL:
n = markTextNode(n).next = codeCache ? new CompiledEvalNode(start, name, template, captureOrbInternal(), start = cursor + 1, parserContext) : new EvalNode(start, name, template, captureOrbInternal(), start = cursor + 1);
break;
case Opcodes.COMMENT:
n = markTextNode(n).next = new CommentNode(start, name, template, captureOrbInternal(), start = cursor + 1);
break;
case Opcodes.DECLARE:
stack.push(n = markTextNode(n).next = codeCache ? new CompiledDeclareNode(start, name, template, captureOrbInternal(), start = cursor + 1, parserContext) : new DeclareNode(start, name, template, captureOrbInternal(), start = cursor + 1));
n.setTerminus(new TerminalNode());
break;
case Opcodes.END:
n = markTextNode(n);
Node end = (Node) stack.pop();
Node terminal = end.getTerminus();
terminal.setCStart(captureOrbInternal());
terminal.setEnd((lastTextRangeEnding = start) - 1);
terminal.calculateContents(template);
if (end.demarcate(terminal, template))
n = n.next = terminal;
else
n = terminal;
break;
default:
if (name.length() == 0) {
n = markTextNode(n).next = codeCache ? new CompiledExpressionNode(start, name, template, captureOrbInternal(), start = cursor + 1, parserContext) : new ExpressionNode(start, name, template, captureOrbInternal(), start = cursor + 1);
} else if (customNodes != null && customNodes.containsKey(name)) {
Class<? extends Node> customNode = customNodes.get(name);
try {
(n = markTextNode(n).next = (customNode.newInstance())).setBegin(start);
n.setName(name);
n.setCStart(captureOrbInternal());
n.setCEnd(start = cursor + 1);
n.setEnd(n.getCEnd());
n.setContents(subset(template, n.getCStart(), n.getCEnd() - n.getCStart() - 1));
if (n.isOpenNode()) {
stack.push(n);
}
} catch (InstantiationException e) {
throw new RuntimeException("unable to instantiate custom node class: " + customNode.getName());
} catch (IllegalAccessException e) {
throw new RuntimeException("unable to instantiate custom node class: " + customNode.getName());
}
} else {
throw new RuntimeException("unknown token type: " + name);
}
}
}
break;
}
cursor++;
}
} catch (RuntimeException e) {
CompileException ce = new CompileException(e.getMessage(), template, cursor, e);
ce.setExpr(template);
if (e instanceof CompileException) {
CompileException ce2 = (CompileException) e;
if (ce2.getCursor() != -1) {
ce.setCursor(ce2.getCursor());
if (ce2.getColumn() == -1)
ce.setColumn(ce.getCursor() - colStart);
else
ce.setColumn(ce2.getColumn());
}
}
ce.setLineNumber(line);
throw ce;
}
if (!stack.isEmpty()) {
CompileException ce = new CompileException("unclosed @" + ((Node) stack.peek()).getName() + "{} block. expected @end{}", template, cursor);
ce.setColumn(cursor - colStart);
ce.setLineNumber(line);
throw ce;
}
if (start < template.length) {
n = n.next = new TextNode(start, template.length);
}
n.next = new EndNode();
n = root;
do {
if (n.getLength() != 0) {
break;
}
} while ((n = n.getNext()) != null);
if (n != null && n.getLength() == template.length - 1) {
if (n instanceof ExpressionNode) {
return codeCache ? new CompiledTerminalExpressionNode(n, parserContext) : new TerminalExpressionNode(n);
} else {
return n;
}
}
return root;
}
use of org.mvel2.ast.ForEachNode in project jbpm by kiegroup.
the class RuleFlowProcessValidator method validateNodes.
private void validateNodes(Node[] nodes, List<ProcessValidationError> errors, RuleFlowProcess process) {
String isForCompensation = "isForCompensation";
for (int i = 0; i < nodes.length; i++) {
final Node node = nodes[i];
if (node instanceof StartNode) {
final StartNode startNode = (StartNode) node;
if (startNode.getTo() == null) {
addErrorMessage(process, node, errors, "Start has no outgoing connection.");
}
if (startNode.getTimer() != null) {
validateTimer(startNode.getTimer(), node, process, errors);
}
} else if (node instanceof EndNode) {
final EndNode endNode = (EndNode) node;
if (endNode.getFrom() == null) {
addErrorMessage(process, node, errors, "End has no incoming connection.");
}
validateCompensationIntermediateOrEndEvent(endNode, process, errors);
} else if (node instanceof RuleSetNode) {
final RuleSetNode ruleSetNode = (RuleSetNode) node;
if (ruleSetNode.getFrom() == null && !acceptsNoIncomingConnections(node)) {
addErrorMessage(process, node, errors, "RuleSet has no incoming connection.");
}
if (ruleSetNode.getTo() == null && !acceptsNoOutgoingConnections(node)) {
addErrorMessage(process, node, errors, "RuleSet has no outgoing connection.");
}
final String language = ruleSetNode.getLanguage();
if (RuleSetNode.DRL_LANG.equals(language)) {
final String ruleFlowGroup = ruleSetNode.getRuleFlowGroup();
if (ruleFlowGroup == null || "".equals(ruleFlowGroup)) {
addErrorMessage(process, node, errors, "RuleSet (DRL) has no ruleflow-group.");
}
} else if (RuleSetNode.DMN_LANG.equals(language)) {
final String namespace = ruleSetNode.getNamespace();
if (namespace == null || "".equals(namespace)) {
addErrorMessage(process, node, errors, "RuleSet (DMN) has no namespace.");
}
final String model = ruleSetNode.getModel();
if (model == null || "".equals(model)) {
addErrorMessage(process, node, errors, "RuleSet (DMN) has no model.");
}
} else {
addErrorMessage(process, node, errors, "Unsupported rule language '" + language + "'");
}
if (ruleSetNode.getTimers() != null) {
for (Timer timer : ruleSetNode.getTimers().keySet()) {
validateTimer(timer, node, process, errors);
}
}
} else if (node instanceof Split) {
final Split split = (Split) node;
if (split.getType() == Split.TYPE_UNDEFINED) {
addErrorMessage(process, node, errors, "Split has no type.");
}
if (split.getFrom() == null && !acceptsNoIncomingConnections(node)) {
addErrorMessage(process, node, errors, "Split has no incoming connection.");
}
if (split.getDefaultOutgoingConnections().size() < 2) {
addErrorMessage(process, node, errors, "Split does not have more than one outgoing connection: " + split.getOutgoingConnections().size() + ".");
}
if (split.getType() == Split.TYPE_XOR || split.getType() == Split.TYPE_OR) {
for (final Iterator<Connection> it = split.getDefaultOutgoingConnections().iterator(); it.hasNext(); ) {
final Connection connection = it.next();
if (split.getConstraint(connection) == null && !split.isDefault(connection) || (!split.isDefault(connection) && (split.getConstraint(connection).getConstraint() == null || split.getConstraint(connection).getConstraint().trim().length() == 0))) {
addErrorMessage(process, node, errors, "Split does not have a constraint for " + connection.toString() + ".");
}
}
}
} else if (node instanceof Join) {
final Join join = (Join) node;
if (join.getType() == Join.TYPE_UNDEFINED) {
addErrorMessage(process, node, errors, "Join has no type.");
}
if (join.getDefaultIncomingConnections().size() < 2) {
addErrorMessage(process, node, errors, "Join does not have more than one incoming connection: " + join.getIncomingConnections().size() + ".");
}
if (join.getTo() == null && !acceptsNoOutgoingConnections(node)) {
addErrorMessage(process, node, errors, "Join has no outgoing connection.");
}
if (join.getType() == Join.TYPE_N_OF_M) {
String n = join.getN();
if (!n.startsWith("#{") || !n.endsWith("}")) {
try {
new Integer(n);
} catch (NumberFormatException e) {
addErrorMessage(process, node, errors, "Join has illegal n value: " + n);
}
}
}
} else if (node instanceof MilestoneNode) {
final MilestoneNode milestone = (MilestoneNode) node;
if (milestone.getFrom() == null && !acceptsNoIncomingConnections(node)) {
addErrorMessage(process, node, errors, "Milestone has no incoming connection.");
}
if (milestone.getTo() == null && !acceptsNoOutgoingConnections(node)) {
addErrorMessage(process, node, errors, "Milestone has no outgoing connection.");
}
if (milestone.getConstraint() == null) {
addErrorMessage(process, node, errors, "Milestone has no constraint.");
}
if (milestone.getTimers() != null) {
for (Timer timer : milestone.getTimers().keySet()) {
validateTimer(timer, node, process, errors);
}
}
} else if (node instanceof StateNode) {
final StateNode stateNode = (StateNode) node;
if (stateNode.getDefaultIncomingConnections().size() == 0 && !acceptsNoIncomingConnections(node)) {
addErrorMessage(process, node, errors, "State has no incoming connection");
}
} else if (node instanceof SubProcessNode) {
final SubProcessNode subProcess = (SubProcessNode) node;
if (subProcess.getFrom() == null && !acceptsNoIncomingConnections(node)) {
addErrorMessage(process, node, errors, "SubProcess has no incoming connection.");
}
if (subProcess.getTo() == null && !acceptsNoOutgoingConnections(node)) {
Object compensationObj = subProcess.getMetaData(isForCompensation);
if (compensationObj == null || !((Boolean) compensationObj)) {
addErrorMessage(process, node, errors, "SubProcess has no outgoing connection.");
}
}
if (subProcess.getProcessId() == null && subProcess.getProcessName() == null) {
addErrorMessage(process, node, errors, "SubProcess has no process id.");
}
if (subProcess.getTimers() != null) {
for (Timer timer : subProcess.getTimers().keySet()) {
validateTimer(timer, node, process, errors);
}
}
if (!subProcess.isIndependent() && !subProcess.isWaitForCompletion()) {
addErrorMessage(process, node, errors, "SubProcess you can only set " + "independent to 'false' only when 'Wait for completion' is set to true.");
}
} else if (node instanceof ActionNode) {
final ActionNode actionNode = (ActionNode) node;
if (actionNode.getFrom() == null && !acceptsNoIncomingConnections(node)) {
addErrorMessage(process, node, errors, "Action has no incoming connection.");
}
if (actionNode.getTo() == null && !acceptsNoOutgoingConnections(node)) {
Object compensationObj = actionNode.getMetaData(isForCompensation);
if (compensationObj == null || !((Boolean) compensationObj)) {
addErrorMessage(process, node, errors, "Action has no outgoing connection.");
}
}
if (actionNode.getAction() == null) {
addErrorMessage(process, node, errors, "Action has no action.");
} else if (actionNode.getAction() instanceof DroolsConsequenceAction) {
DroolsConsequenceAction droolsAction = (DroolsConsequenceAction) actionNode.getAction();
String actionString = droolsAction.getConsequence();
if (actionString == null) {
addErrorMessage(process, node, errors, "Action has empty action.");
} else if ("mvel".equals(droolsAction.getDialect())) {
try {
ParserContext parserContext = new ParserContext();
// parserContext.setStrictTypeEnforcement(true);
ExpressionCompiler compiler = new ExpressionCompiler(actionString, parserContext);
compiler.setVerifying(true);
compiler.compile();
List<ErrorDetail> mvelErrors = parserContext.getErrorList();
if (mvelErrors != null) {
for (Iterator<ErrorDetail> iterator = mvelErrors.iterator(); iterator.hasNext(); ) {
ErrorDetail error = iterator.next();
addErrorMessage(process, node, errors, "Action has invalid action: " + error.getMessage() + ".");
}
}
} catch (Throwable t) {
addErrorMessage(process, node, errors, "Action has invalid action: " + t.getMessage() + ".");
}
}
// TODO: validation for "java" and "drools" scripts!
validateCompensationIntermediateOrEndEvent(actionNode, process, errors);
}
} else if (node instanceof WorkItemNode) {
final WorkItemNode workItemNode = (WorkItemNode) node;
if (workItemNode.getFrom() == null && !acceptsNoIncomingConnections(node)) {
addErrorMessage(process, node, errors, "Task has no incoming connection.");
}
if (workItemNode.getTo() == null && !acceptsNoOutgoingConnections(node)) {
Object compensationObj = workItemNode.getMetaData(isForCompensation);
if (compensationObj == null || !((Boolean) compensationObj)) {
addErrorMessage(process, node, errors, "Task has no outgoing connection.");
}
}
if (workItemNode.getWork() == null) {
addErrorMessage(process, node, errors, "Task has no work specified.");
} else {
Work work = workItemNode.getWork();
if (work.getName() == null || work.getName().trim().length() == 0) {
addErrorMessage(process, node, errors, "Task has no task type.");
}
}
if (workItemNode.getTimers() != null) {
for (Timer timer : workItemNode.getTimers().keySet()) {
validateTimer(timer, node, process, errors);
}
}
} else if (node instanceof ForEachNode) {
final ForEachNode forEachNode = (ForEachNode) node;
String variableName = forEachNode.getVariableName();
if (variableName == null || "".equals(variableName)) {
addErrorMessage(process, node, errors, "ForEach has no variable name");
}
String collectionExpression = forEachNode.getCollectionExpression();
if (collectionExpression == null || "".equals(collectionExpression)) {
addErrorMessage(process, node, errors, "ForEach has no collection expression");
}
if (forEachNode.getDefaultIncomingConnections().size() == 0 && !acceptsNoIncomingConnections(node)) {
addErrorMessage(process, node, errors, "ForEach has no incoming connection");
}
if (forEachNode.getDefaultOutgoingConnections().size() == 0 && !acceptsNoOutgoingConnections(node)) {
addErrorMessage(process, node, errors, "ForEach has no outgoing connection");
}
// TODO: check, if no linked connections, for start and end node(s)
// if (forEachNode.getLinkedIncomingNode(org.drools.workflow.core.Node.CONNECTION_DEFAULT_TYPE) == null) {
// errors.add(new ProcessValidationErrorImpl(process,
// "ForEach node '%s' [%d] has no linked start node"));
// }
// if (forEachNode.getLinkedOutgoingNode(org.drools.workflow.core.Node.CONNECTION_DEFAULT_TYPE) == null) {
// errors.add(new ProcessValidationErrorImpl(process,
// "ForEach node '%s' [%d] has no linked end node"));
// }
validateNodes(forEachNode.getNodes(), errors, process);
} else if (node instanceof DynamicNode) {
final DynamicNode dynamicNode = (DynamicNode) node;
if (dynamicNode.getDefaultIncomingConnections().size() == 0 && !acceptsNoIncomingConnections(dynamicNode)) {
addErrorMessage(process, node, errors, "Dynamic has no incoming connection");
}
if (dynamicNode.getDefaultOutgoingConnections().size() == 0 && !acceptsNoOutgoingConnections(dynamicNode)) {
addErrorMessage(process, node, errors, "Dynamic has no outgoing connection");
}
if ("".equals(dynamicNode.getCompletionExpression()) && !dynamicNode.isAutoComplete()) {
addErrorMessage(process, node, errors, "Dynamic has no completion condition set");
}
validateNodes(dynamicNode.getNodes(), errors, process);
} else if (node instanceof CompositeNode) {
final CompositeNode compositeNode = (CompositeNode) node;
for (Map.Entry<String, NodeAndType> inType : compositeNode.getLinkedIncomingNodes().entrySet()) {
if (compositeNode.getIncomingConnections(inType.getKey()).size() == 0 && !acceptsNoIncomingConnections(node)) {
addErrorMessage(process, node, errors, "Composite has no incoming connection for type " + inType.getKey());
}
if (inType.getValue().getNode() == null && !acceptsNoOutgoingConnections(node)) {
addErrorMessage(process, node, errors, "Composite has invalid linked incoming node for type " + inType.getKey());
}
}
for (Map.Entry<String, NodeAndType> outType : compositeNode.getLinkedOutgoingNodes().entrySet()) {
if (compositeNode.getOutgoingConnections(outType.getKey()).size() == 0) {
addErrorMessage(process, node, errors, "Composite has no outgoing connection for type " + outType.getKey());
}
if (outType.getValue().getNode() == null) {
addErrorMessage(process, node, errors, "Composite has invalid linked outgoing node for type " + outType.getKey());
}
}
if (compositeNode instanceof EventSubProcessNode) {
if (compositeNode.getIncomingConnections().size() > 0) {
addErrorMessage(process, node, errors, "Event subprocess is not allowed to have any incoming connections.");
}
if (compositeNode.getOutgoingConnections().size() > 0) {
addErrorMessage(process, node, errors, "Event subprocess is not allowed to have any outgoing connections.");
}
Node[] eventSubProcessNodes = compositeNode.getNodes();
int startEventCount = 0;
for (int j = 0; j < eventSubProcessNodes.length; ++j) {
if (eventSubProcessNodes[j] instanceof StartNode) {
StartNode startNode = (StartNode) eventSubProcessNodes[j];
if (++startEventCount == 2) {
addErrorMessage(process, compositeNode, errors, "Event subprocess is not allowed to have more than one start node.");
}
if (startNode.getTriggers() == null || startNode.getTriggers().isEmpty()) {
addErrorMessage(process, startNode, errors, "Start in Event SubProcess '" + compositeNode.getName() + "' [" + compositeNode.getId() + "] must contain a trigger (event definition).");
}
}
}
} else {
Boolean isForCompensationObject = (Boolean) compositeNode.getMetaData("isForCompensation");
if (compositeNode.getIncomingConnections().size() == 0 && !Boolean.TRUE.equals(isForCompensationObject)) {
addErrorMessage(process, node, errors, "Embedded subprocess does not have incoming connection.");
}
if (compositeNode.getOutgoingConnections().size() == 0 && !Boolean.TRUE.equals(isForCompensationObject)) {
addErrorMessage(process, node, errors, "Embedded subprocess does not have outgoing connection.");
}
}
if (compositeNode.getTimers() != null) {
for (Timer timer : compositeNode.getTimers().keySet()) {
validateTimer(timer, node, process, errors);
}
}
validateNodes(compositeNode.getNodes(), errors, process);
} else if (node instanceof EventNode) {
final EventNode eventNode = (EventNode) node;
if (eventNode.getEventFilters().size() == 0) {
addErrorMessage(process, node, errors, "Event should specify an event type");
}
if (eventNode.getDefaultOutgoingConnections().size() == 0) {
addErrorMessage(process, node, errors, "Event has no outgoing connection");
} else {
List<EventFilter> eventFilters = eventNode.getEventFilters();
boolean compensationHandler = false;
for (EventFilter eventFilter : eventFilters) {
if (((EventTypeFilter) eventFilter).getType().startsWith("Compensation")) {
compensationHandler = true;
break;
}
}
if (compensationHandler && eventNode instanceof BoundaryEventNode) {
Connection connection = eventNode.getDefaultOutgoingConnections().get(0);
Boolean isAssociation = (Boolean) connection.getMetaData().get("association");
if (isAssociation == null) {
isAssociation = false;
}
if (!(eventNode.getDefaultOutgoingConnections().size() == 1 && connection != null && isAssociation)) {
addErrorMessage(process, node, errors, "Compensation Boundary Event is only allowed to have 1 association to 1 compensation activity.");
}
}
}
} else if (node instanceof FaultNode) {
final FaultNode faultNode = (FaultNode) node;
if (faultNode.getFrom() == null && !acceptsNoIncomingConnections(node)) {
addErrorMessage(process, node, errors, "Fault has no incoming connection.");
}
if (faultNode.getFaultName() == null) {
addErrorMessage(process, node, errors, "Fault has no fault name.");
}
} else if (node instanceof TimerNode) {
TimerNode timerNode = (TimerNode) node;
if (timerNode.getFrom() == null && !acceptsNoIncomingConnections(node)) {
addErrorMessage(process, node, errors, "Timer has no incoming connection.");
}
if (timerNode.getTo() == null && !acceptsNoOutgoingConnections(node)) {
addErrorMessage(process, node, errors, "Timer has no outgoing connection.");
}
if (timerNode.getTimer() == null) {
addErrorMessage(process, node, errors, "Timer has no timer specified.");
} else {
validateTimer(timerNode.getTimer(), node, process, errors);
}
} else if (node instanceof CatchLinkNode) {
// catchlink validation here, there also are validations in
// ProcessHandler regarding connection issues
} else if (node instanceof ThrowLinkNode) {
// throw validation here, there also are validations in
// ProcessHandler regarding connection issues
} else {
errors.add(new ProcessValidationErrorImpl(process, "Unknown node type '" + node.getClass().getName() + "'"));
}
}
}
use of org.mvel2.ast.ForEachNode 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);
}
}
use of org.mvel2.ast.ForEachNode in project mvel by mikebrock.
the class TemplateCompiler method compileFrom.
public Node compileFrom(Node root, ExecutionStack stack) {
line = 1;
Node n = root;
if (root == null) {
n = root = new TextNode(0, 0);
}
IfNode last;
Integer opcode;
String name;
int x;
try {
while (cursor < length) {
switch(template[cursor]) {
case '\n':
line++;
colStart = cursor + 1;
break;
case '@':
case '$':
if (isNext(template[cursor])) {
start = ++cursor;
(n = markTextNode(n)).setEnd(n.getEnd() + 1);
start = lastTextRangeEnding = ++cursor;
continue;
}
if ((x = captureOrbToken()) != -1) {
start = x;
switch((opcode = OPCODES.get(name = new String(capture()))) == null ? 0 : opcode) {
case Opcodes.IF:
/**
* Capture any residual text node, and push the if statement on the nesting stack.
*/
stack.push(n = markTextNode(n).next = codeCache ? new CompiledIfNode(start, name, template, captureOrbInternal(), start, parserContext) : new IfNode(start, name, template, captureOrbInternal(), start));
n.setTerminus(new TerminalNode());
break;
case Opcodes.ELSE:
if (!stack.isEmpty() && stack.peek() instanceof IfNode) {
markTextNode(n).next = (last = (IfNode) stack.pop()).getTerminus();
last.demarcate(last.getTerminus(), template);
last.next = n = codeCache ? new CompiledIfNode(start, name, template, captureOrbInternal(), start, parserContext) : new IfNode(start, name, template, captureOrbInternal(), start);
n.setTerminus(last.getTerminus());
stack.push(n);
}
break;
case Opcodes.FOREACH:
stack.push(n = markTextNode(n).next = codeCache ? new CompiledForEachNode(start, name, template, captureOrbInternal(), start, parserContext) : new ForEachNode(start, name, template, captureOrbInternal(), start));
n.setTerminus(new TerminalNode());
break;
case Opcodes.INCLUDE_FILE:
n = markTextNode(n).next = codeCache ? new CompiledIncludeNode(start, name, template, captureOrbInternal(), start = cursor + 1, parserContext) : new IncludeNode(start, name, template, captureOrbInternal(), start = cursor + 1);
break;
case Opcodes.INCLUDE_NAMED:
n = markTextNode(n).next = codeCache ? new CompiledNamedIncludeNode(start, name, template, captureOrbInternal(), start = cursor + 1, parserContext) : new NamedIncludeNode(start, name, template, captureOrbInternal(), start = cursor + 1);
break;
case Opcodes.CODE:
n = markTextNode(n).next = codeCache ? new CompiledCodeNode(start, name, template, captureOrbInternal(), start = cursor + 1, parserContext) : new CodeNode(start, name, template, captureOrbInternal(), start = cursor + 1);
break;
case Opcodes.EVAL:
n = markTextNode(n).next = codeCache ? new CompiledEvalNode(start, name, template, captureOrbInternal(), start = cursor + 1, parserContext) : new EvalNode(start, name, template, captureOrbInternal(), start = cursor + 1);
break;
case Opcodes.COMMENT:
n = markTextNode(n).next = new CommentNode(start, name, template, captureOrbInternal(), start = cursor + 1);
break;
case Opcodes.DECLARE:
stack.push(n = markTextNode(n).next = codeCache ? new CompiledDeclareNode(start, name, template, captureOrbInternal(), start = cursor + 1, parserContext) : new DeclareNode(start, name, template, captureOrbInternal(), start = cursor + 1));
n.setTerminus(new TerminalNode());
break;
case Opcodes.END:
n = markTextNode(n);
Node end = (Node) stack.pop();
Node terminal = end.getTerminus();
terminal.setCStart(captureOrbInternal());
terminal.setEnd((lastTextRangeEnding = start) - 1);
terminal.calculateContents(template);
if (end.demarcate(terminal, template))
n = n.next = terminal;
else
n = terminal;
break;
default:
if (name.length() == 0) {
n = markTextNode(n).next = codeCache ? new CompiledExpressionNode(start, name, template, captureOrbInternal(), start = cursor + 1, parserContext) : new ExpressionNode(start, name, template, captureOrbInternal(), start = cursor + 1);
} else if (customNodes != null && customNodes.containsKey(name)) {
Class<? extends Node> customNode = customNodes.get(name);
try {
(n = markTextNode(n).next = (customNode.newInstance())).setBegin(start);
n.setName(name);
n.setCStart(captureOrbInternal());
n.setCEnd(start = cursor + 1);
n.setEnd(n.getCEnd());
n.setContents(subset(template, n.getCStart(), n.getCEnd() - n.getCStart() - 1));
if (n.isOpenNode()) {
stack.push(n);
}
} catch (InstantiationException e) {
throw new RuntimeException("unable to instantiate custom node class: " + customNode.getName());
} catch (IllegalAccessException e) {
throw new RuntimeException("unable to instantiate custom node class: " + customNode.getName());
}
} else {
throw new RuntimeException("unknown token type: " + name);
}
}
}
break;
}
cursor++;
}
} catch (RuntimeException e) {
CompileException ce = new CompileException(e.getMessage(), template, cursor, e);
ce.setExpr(template);
if (e instanceof CompileException) {
CompileException ce2 = (CompileException) e;
if (ce2.getCursor() != -1) {
ce.setCursor(ce2.getCursor());
if (ce2.getColumn() == -1)
ce.setColumn(ce.getCursor() - colStart);
else
ce.setColumn(ce2.getColumn());
}
}
ce.setLineNumber(line);
throw ce;
}
if (!stack.isEmpty()) {
CompileException ce = new CompileException("unclosed @" + ((Node) stack.peek()).getName() + "{} block. expected @end{}", template, cursor);
ce.setColumn(cursor - colStart);
ce.setLineNumber(line);
throw ce;
}
if (start < template.length) {
n = n.next = new TextNode(start, template.length);
}
n.next = new EndNode();
n = root;
do {
if (n.getLength() != 0) {
break;
}
} while ((n = n.getNext()) != null);
if (n != null && n.getLength() == template.length - 1) {
if (n instanceof ExpressionNode) {
return codeCache ? new CompiledTerminalExpressionNode(n, parserContext) : new TerminalExpressionNode(n);
} else {
return n;
}
}
return root;
}
Aggregations