use of org.mule.mvel2.CompileException 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;
}
use of org.mule.mvel2.CompileException in project mvel by mikebrock.
the class CollectionParser method parseCollection.
private Object parseCollection(boolean subcompile) {
if (end - start == 0) {
if (type == LIST)
return new ArrayList();
else
return EMPTY_ARRAY;
}
Map<Object, Object> map = null;
List<Object> list = null;
int st = start;
if (type != -1) {
switch(type) {
case ARRAY:
case LIST:
list = new ArrayList<Object>();
break;
case MAP:
map = new HashMap<Object, Object>();
break;
}
}
Object curr = null;
int newType = -1;
for (; cursor < end; cursor++) {
switch(property[cursor]) {
case '{':
if (newType == -1) {
newType = ARRAY;
}
case '[':
if (cursor > start && isIdentifierPart(property[cursor - 1]))
continue;
if (newType == -1) {
newType = LIST;
}
/**
* Sub-parse nested collections.
*/
Object o = new CollectionParser(newType).parseCollection(property, (st = cursor) + 1, (cursor = balancedCapture(property, st, end, property[st])) - st - 1, subcompile, colType, pCtx);
if (type == MAP) {
map.put(curr, o);
} else {
list.add(curr = o);
}
cursor = skipWhitespace(property, ++cursor);
if ((st = cursor) < end && property[cursor] == ',') {
st = cursor + 1;
} else if (cursor < end) {
if (ParseTools.opLookup(property[cursor]) == -1) {
throw new CompileException("unterminated collection element", property, cursor);
}
}
continue;
case '(':
cursor = balancedCapture(property, cursor, end, '(');
break;
case '\"':
case '\'':
cursor = balancedCapture(property, cursor, end, property[cursor]);
break;
case ',':
if (type != MAP) {
list.add(new String(property, st, cursor - st).trim());
} else {
map.put(curr, createStringTrimmed(property, st, cursor - st));
}
if (subcompile) {
subCompile(st, cursor - st);
}
st = cursor + 1;
break;
case ':':
if (type != MAP) {
map = new HashMap<Object, Object>();
type = MAP;
}
curr = createStringTrimmed(property, st, cursor - st);
if (subcompile) {
subCompile(st, cursor - st);
}
st = cursor + 1;
break;
case '.':
cursor++;
cursor = skipWhitespace(property, cursor);
if (cursor != end && property[cursor] == '{') {
cursor = balancedCapture(property, cursor, '{');
}
break;
}
}
if (st < end && isWhitespace(property[st])) {
st = skipWhitespace(property, st);
}
if (st < end) {
if (cursor < (end - 1))
cursor++;
if (type == MAP) {
map.put(curr, createStringTrimmed(property, st, cursor - st));
} else {
if (cursor < end)
cursor++;
list.add(createStringTrimmed(property, st, cursor - st));
}
if (subcompile)
subCompile(st, cursor - st);
}
switch(type) {
case MAP:
return map;
case ARRAY:
return list.toArray();
default:
return list;
}
}
use of org.mule.mvel2.CompileException in project mvel by mikebrock.
the class FunctionParser method parse.
public Function parse() {
int start = cursor;
int startCond = 0;
int endCond = 0;
int blockStart;
int blockEnd;
int end = cursor + length;
cursor = ParseTools.captureToNextTokenJunction(expr, cursor, end, pCtx);
if (expr[cursor = ParseTools.nextNonBlank(expr, cursor)] == '(') {
/**
* If we discover an opening bracket after the function name, we check to see
* if this function accepts parameters.
*/
endCond = cursor = balancedCaptureWithLineAccounting(expr, startCond = cursor, end, '(', pCtx);
startCond++;
cursor++;
cursor = ParseTools.skipWhitespace(expr, cursor);
if (cursor >= end) {
throw new CompileException("incomplete statement", expr, cursor);
} else if (expr[cursor] == '{') {
blockEnd = cursor = balancedCaptureWithLineAccounting(expr, blockStart = cursor, end, '{', pCtx);
} else {
blockStart = cursor - 1;
cursor = ParseTools.captureToEOS(expr, cursor, end, pCtx);
blockEnd = cursor;
}
} else {
/**
* This function has not parameters.
*/
if (expr[cursor] == '{') {
/**
* This function is bracketed. We capture the entire range in the brackets.
*/
blockEnd = cursor = balancedCaptureWithLineAccounting(expr, blockStart = cursor, end, '{', pCtx);
} else {
/**
* This is a single statement function declaration. We only capture the statement.
*/
blockStart = cursor - 1;
cursor = ParseTools.captureToEOS(expr, cursor, end, pCtx);
blockEnd = cursor;
}
}
/**
* Trim any whitespace from the captured block range.
*/
blockStart = ParseTools.trimRight(expr, start, blockStart + 1);
blockEnd = ParseTools.trimLeft(expr, start, blockEnd);
cursor++;
/**
* Check if the function is manually terminated.
*/
if (splitAccumulator != null && ParseTools.isStatementNotManuallyTerminated(expr, cursor)) {
/**
* Add an EndOfStatement to the split accumulator in the parser.
*/
splitAccumulator.add(new EndOfStatement());
}
/**
* Produce the funciton node.
*/
return new Function(name, expr, startCond, endCond - startCond, blockStart, blockEnd - blockStart, fields, pCtx == null ? pCtx = AbstractParser.getCurrentThreadParserContext() : pCtx);
}
use of org.mule.mvel2.CompileException in project mvel by mikebrock.
the class ProtoParser method parse.
public Proto parse() {
Proto proto = new Proto(protoName);
Mainloop: while (cursor < endOffset) {
cursor = ParseTools.skipWhitespace(expr, cursor);
int start = cursor;
if (tk2 == null) {
while (cursor < endOffset && isIdentifierPart(expr[cursor])) cursor++;
if (cursor > start) {
tk1 = new String(expr, start, cursor - start);
if ("def".equals(tk1) || "function".equals(tk1)) {
cursor++;
cursor = ParseTools.skipWhitespace(expr, cursor);
start = cursor;
while (cursor < endOffset && isIdentifierPart(expr[cursor])) cursor++;
if (start == cursor) {
throw new CompileException("attempt to declare an anonymous function as a prototype member", expr, start);
}
FunctionParser parser = new FunctionParser(new String(expr, start, cursor - start), cursor, endOffset, expr, 0, pCtx, null);
proto.declareReceiver(parser.getName(), parser.parse());
cursor = parser.getCursor() + 1;
tk1 = null;
continue;
}
}
cursor = ParseTools.skipWhitespace(expr, cursor);
}
if (cursor > endOffset) {
throw new CompileException("unexpected end of statement in proto declaration: " + protoName, expr, start);
}
switch(expr[cursor]) {
case ';':
cursor++;
calculateDecl();
if (interpreted && type == DeferredTypeResolve.class) {
/**
* If this type could not be immediately resolved, it may be a look-ahead case, so
* we defer resolution of the type until later and place it in the wait queue.
*/
enqueueReceiverForLateResolution(deferredName, proto.declareReceiver(name, Proto.ReceiverType.DEFERRED, null), null);
} else {
proto.declareReceiver(name, type, null);
}
break;
case '=':
cursor++;
cursor = ParseTools.skipWhitespace(expr, cursor);
start = cursor;
Loop: while (cursor < endOffset) {
switch(expr[cursor]) {
case '{':
case '[':
case '(':
case '\'':
case '"':
cursor = balancedCaptureWithLineAccounting(expr, cursor, endOffset, expr[cursor], pCtx);
break;
case ';':
break Loop;
}
cursor++;
}
calculateDecl();
String initString = new String(expr, start, cursor++ - start);
if (interpreted && type == DeferredTypeResolve.class) {
enqueueReceiverForLateResolution(deferredName, proto.declareReceiver(name, Proto.ReceiverType.DEFERRED, null), initString);
} else {
proto.declareReceiver(name, type, (ExecutableStatement) subCompileExpression(initString, pCtx));
}
break;
default:
start = cursor;
while (cursor < endOffset && isIdentifierPart(expr[cursor])) cursor++;
if (cursor > start) {
tk2 = new String(expr, start, cursor - start);
}
}
}
cursor++;
/**
* Check if the function is manually terminated.
*/
if (splitAccumulator != null && ParseTools.isStatementNotManuallyTerminated(expr, cursor)) {
/**
* Add an EndOfStatement to the split accumulator in the parser.
*/
splitAccumulator.add(new EndOfStatement());
}
return proto;
}
use of org.mule.mvel2.CompileException in project mvel by mikebrock.
the class ProtoParser method checkForPossibleUnresolvedViolations.
/**
* This is such a horrible hack, but it's more performant than any other horrible hack I can think of
* right now.
*
* @param expr
* @param cursor
* @param pCtx
*/
public static void checkForPossibleUnresolvedViolations(char[] expr, int cursor, ParserContext pCtx) {
if (isUnresolvedWaiting()) {
LinkedHashMap<String, Object> imports = (LinkedHashMap<String, Object>) pCtx.getParserConfiguration().getImports();
Object o = imports.values().toArray()[imports.size() - 1];
if (o instanceof Proto) {
Proto proto = (Proto) o;
int last = proto.getCursorEnd();
cursor--;
while (cursor > last && ParseTools.isWhitespace(expr[cursor])) cursor--;
while (cursor > last && ParseTools.isIdentifierPart(expr[cursor])) cursor--;
while (cursor > last && (ParseTools.isWhitespace(expr[cursor]) || expr[cursor] == ';')) cursor--;
if (cursor != last) {
throw new CompileException("unresolved reference (possible illegal forward-reference?): " + ProtoParser.getNextUnresolvedWaiting(), expr, proto.getCursorStart());
}
}
}
}
Aggregations