Search in sources :

Example 1 with Scope

use of org.apache.commons.jexl3.internal.Scope in project commons-jexl by apache.

the class JexlParser method declareVariable.

/**
 * Declares a local variable.
 * <p> This method creates an new entry in the symbol map. </p>
 * @param variable the identifier used to declare
 * @param token      the variable name toekn
 */
protected void declareVariable(final ASTVar variable, final Token token) {
    final String name = token.image;
    if (!allowVariable(name)) {
        throwFeatureException(JexlFeatures.LOCAL_VAR, token);
    }
    if (frame == null) {
        frame = new Scope(null, (String[]) null);
    }
    final int symbol = frame.declareVariable(name);
    variable.setSymbol(symbol, name);
    if (frame.isCapturedSymbol(symbol)) {
        variable.setCaptured(true);
    }
    // lexical feature error
    if (!declareSymbol(symbol)) {
        if (getFeatures().isLexical()) {
            throw new JexlException(variable, name + ": variable is already declared");
        }
        variable.setRedefined(true);
    }
}
Also used : Scope(org.apache.commons.jexl3.internal.Scope) LexicalScope(org.apache.commons.jexl3.internal.LexicalScope) JexlException(org.apache.commons.jexl3.JexlException)

Example 2 with Scope

use of org.apache.commons.jexl3.internal.Scope in project commons-jexl by apache.

the class JexlParser method declareParameter.

/**
 * Declares a local parameter.
 * <p> This method creates an new entry in the symbol map. </p>
 * @param token the parameter name toekn
 */
protected void declareParameter(final Token token) {
    final String identifier = token.image;
    if (!allowVariable(identifier)) {
        throwFeatureException(JexlFeatures.LOCAL_VAR, token);
    }
    if (frame == null) {
        frame = new Scope(null, (String[]) null);
    }
    final int symbol = frame.declareParameter(identifier);
    // lexical feature error
    if (!block.declareSymbol(symbol) && getFeatures().isLexical()) {
        final JexlInfo xinfo = info.at(token.beginLine, token.beginColumn);
        throw new JexlException(xinfo, identifier + ": variable is already declared", null);
    }
}
Also used : Scope(org.apache.commons.jexl3.internal.Scope) LexicalScope(org.apache.commons.jexl3.internal.LexicalScope) JexlException(org.apache.commons.jexl3.JexlException) JexlInfo(org.apache.commons.jexl3.JexlInfo)

Example 3 with Scope

use of org.apache.commons.jexl3.internal.Scope in project commons-jexl by apache.

the class JexlParser method pushFrame.

/**
 * Create a new local variable frame and push it as current scope.
 */
protected void pushFrame() {
    if (frame != null) {
        frames.push(frame);
    }
    frame = new Scope(frame, (String[]) null);
    loopCounts.push(loopCount);
    loopCount = 0;
}
Also used : Scope(org.apache.commons.jexl3.internal.Scope) LexicalScope(org.apache.commons.jexl3.internal.LexicalScope)

Example 4 with Scope

use of org.apache.commons.jexl3.internal.Scope in project commons-jexl by apache.

the class Closure method setCaptured.

/**
 * Sets the captured index of a given symbol, ie the target index of a parent
 * captured symbol in this closure's frame.
 * <p>This is meant to allow a locally defined function to "see" and call
 * itself as a local (captured) variable;
 * in other words, this allows recursive call of a function.
 * @param symbol the symbol index (in the caller of this closure)
 * @param value the value to set in the local frame
 */
public void setCaptured(final int symbol, final Object value) {
    if (script instanceof ASTJexlLambda) {
        final ASTJexlLambda lambda = (ASTJexlLambda) script;
        final Scope scope = lambda.getScope();
        if (scope != null) {
            final Integer reg = scope.getCaptured(symbol);
            if (reg != null) {
                frame.set(reg, value);
            }
        }
    }
}
Also used : ASTJexlLambda(org.apache.commons.jexl3.parser.ASTJexlLambda)

Example 5 with Scope

use of org.apache.commons.jexl3.internal.Scope in project commons-jexl by apache.

the class JexlParser method declareFunction.

/**
 * Declares a local function.
 * @param variable the identifier used to declare
 * @param token      the variable name toekn
 */
protected void declareFunction(final ASTVar variable, final Token token) {
    final String name = token.image;
    // function foo() ... <=> const foo = ()->...
    if (scope == null) {
        scope = new Scope(null);
    }
    final int symbol = scope.declareVariable(name, true, true);
    variable.setSymbol(symbol, name);
    variable.setLexical(true);
    if (scope.isCapturedSymbol(symbol)) {
        variable.setCaptured(true);
    }
    // function is const fun...
    if (declareSymbol(symbol)) {
        scope.addLexical(symbol);
        block.setConstant(symbol);
    } else {
        if (getFeatures().isLexical()) {
            throw new JexlException(variable, name + ": variable is already declared");
        }
        variable.setRedefined(true);
    }
}
Also used : Scope(org.apache.commons.jexl3.internal.Scope) LexicalScope(org.apache.commons.jexl3.internal.LexicalScope) JexlException(org.apache.commons.jexl3.JexlException)

Aggregations

LexicalScope (org.apache.commons.jexl3.internal.LexicalScope)8 JexlException (org.apache.commons.jexl3.JexlException)7 Scope (org.apache.commons.jexl3.internal.Scope)7 ASTJexlScript (org.apache.commons.jexl3.parser.ASTJexlScript)4 JexlInfo (org.apache.commons.jexl3.JexlInfo)3 JexlNode (org.apache.commons.jexl3.parser.JexlNode)3 JexlFeatures (org.apache.commons.jexl3.JexlFeatures)2 JexlScript (org.apache.commons.jexl3.JexlScript)1 ASTArguments (org.apache.commons.jexl3.parser.ASTArguments)1 ASTFunctionNode (org.apache.commons.jexl3.parser.ASTFunctionNode)1 ASTIdentifier (org.apache.commons.jexl3.parser.ASTIdentifier)1 ASTJexlLambda (org.apache.commons.jexl3.parser.ASTJexlLambda)1 ASTNumberLiteral (org.apache.commons.jexl3.parser.ASTNumberLiteral)1 Parser (org.apache.commons.jexl3.parser.Parser)1 StringProvider (org.apache.commons.jexl3.parser.StringProvider)1 Test (org.junit.Test)1