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