Search in sources :

Example 1 with ASTAnnotation

use of org.apache.commons.jexl3.parser.ASTAnnotation in project commons-jexl by apache.

the class Interpreter method processAnnotation.

/**
 * Processes an annotated statement.
 * @param stmt the statement
 * @param index the index of the current annotation being processed
 * @param data the contextual data
 * @return  the result of the statement block evaluation
 */
protected Object processAnnotation(final ASTAnnotatedStatement stmt, final int index, final Object data) {
    // are we evaluating the block ?
    final int last = stmt.jjtGetNumChildren() - 1;
    if (index == last) {
        final JexlNode cblock = stmt.jjtGetChild(last);
        // if the context has changed, might need a new interpreter
        final JexlArithmetic jexla = arithmetic.options(context);
        if (jexla == arithmetic) {
            return cblock.jjtAccept(Interpreter.this, data);
        }
        if (!arithmetic.getClass().equals(jexla.getClass()) && logger.isWarnEnabled()) {
            logger.warn("expected arithmetic to be " + arithmetic.getClass().getSimpleName() + ", got " + jexla.getClass().getSimpleName());
        }
        final Interpreter ii = new Interpreter(Interpreter.this, jexla);
        final Object r = cblock.jjtAccept(ii, data);
        if (ii.isCancelled()) {
            Interpreter.this.cancel();
        }
        return r;
    }
    // tracking whether we processed the annotation
    final AnnotatedCall jstmt = new AnnotatedCall(stmt, index + 1, data);
    // the annotation node and name
    final ASTAnnotation anode = (ASTAnnotation) stmt.jjtGetChild(index);
    final String aname = anode.getName();
    // evaluate the arguments
    final Object[] argv = anode.jjtGetNumChildren() > 0 ? visit((ASTArguments) anode.jjtGetChild(0), null) : null;
    // wrap the future, will recurse through annotation processor
    Object result;
    try {
        result = processAnnotation(aname, argv, jstmt);
        // not processing an annotation is an error
        if (!jstmt.isProcessed()) {
            return annotationError(anode, aname, null);
        }
    } catch (final JexlException xany) {
        throw xany;
    } catch (final Exception xany) {
        return annotationError(anode, aname, xany);
    }
    // the caller may return a return, break or continue
    if (result instanceof JexlException) {
        throw (JexlException) result;
    }
    return result;
}
Also used : JexlException(org.apache.commons.jexl3.JexlException) JexlArithmetic(org.apache.commons.jexl3.JexlArithmetic) JexlException(org.apache.commons.jexl3.JexlException)

Example 2 with ASTAnnotation

use of org.apache.commons.jexl3.parser.ASTAnnotation in project commons-jexl by apache.

the class Debugger method acceptStatement.

/**
 * Adds a statement node to the rebuilt expression.
 * @param child the child node
 * @param data  visitor pattern argument
 * @return visitor pattern value
 */
protected Object acceptStatement(final JexlNode child, final Object data) {
    final JexlNode parent = child.jjtGetParent();
    if (indent > 0 && (parent instanceof ASTBlock || parent instanceof ASTJexlScript)) {
        for (int i = 0; i < indentLevel; ++i) {
            for (int s = 0; s < indent; ++s) {
                builder.append(' ');
            }
        }
    }
    depth -= 1;
    final Object value = accept(child, data);
    depth += 1;
    // blocks, if, for & while don't need a ';' at end
    if (!(child instanceof ASTJexlScript || child instanceof ASTBlock || child instanceof ASTIfStatement || child instanceof ASTForeachStatement || child instanceof ASTWhileStatement || child instanceof ASTDoWhileStatement || child instanceof ASTAnnotation)) {
        builder.append(';');
        if (indent > 0) {
            builder.append('\n');
        } else {
            builder.append(' ');
        }
    }
    return value;
}
Also used : ASTForeachStatement(org.apache.commons.jexl3.parser.ASTForeachStatement) ASTWhileStatement(org.apache.commons.jexl3.parser.ASTWhileStatement) ASTBlock(org.apache.commons.jexl3.parser.ASTBlock) ASTJexlScript(org.apache.commons.jexl3.parser.ASTJexlScript) ASTIfStatement(org.apache.commons.jexl3.parser.ASTIfStatement) ASTDoWhileStatement(org.apache.commons.jexl3.parser.ASTDoWhileStatement) ASTAnnotation(org.apache.commons.jexl3.parser.ASTAnnotation) JexlNode(org.apache.commons.jexl3.parser.JexlNode)

Aggregations

JexlArithmetic (org.apache.commons.jexl3.JexlArithmetic)1 JexlException (org.apache.commons.jexl3.JexlException)1 ASTAnnotation (org.apache.commons.jexl3.parser.ASTAnnotation)1 ASTBlock (org.apache.commons.jexl3.parser.ASTBlock)1 ASTDoWhileStatement (org.apache.commons.jexl3.parser.ASTDoWhileStatement)1 ASTForeachStatement (org.apache.commons.jexl3.parser.ASTForeachStatement)1 ASTIfStatement (org.apache.commons.jexl3.parser.ASTIfStatement)1 ASTJexlScript (org.apache.commons.jexl3.parser.ASTJexlScript)1 ASTWhileStatement (org.apache.commons.jexl3.parser.ASTWhileStatement)1 JexlNode (org.apache.commons.jexl3.parser.JexlNode)1