Class Scope
- java.lang.Object
-
- org.apache.commons.jexl3.internal.Scope
-
public final class Scope extends java.lang.ObjectA script scope, stores the declaration of parameters and local variables as symbols.This also acts as the functional scope and variable definition store.
- Since:
- 3.0
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.Map<java.lang.Integer,java.lang.Integer>capturedVariablesThe map of local captured variables to parent scope variables, i.e., closure.private static java.lang.String[]EMPTY_STRSThe empty string array.private LexicalScopelexicalVariablesLet symbols.private java.util.Map<java.lang.String,java.lang.Integer>namedVariablesThe map of named variables aka script parameters and local variables.private ScopeparentThe parent scope.private intparmsThe number of parameters.(package private) static java.lang.ObjectUNDECLAREDThe value of an as-yet undeclared but variable, for instance: x; before var x;.(package private) static java.lang.ObjectUNDEFINEDThe value of a declared but undefined variable, for instance: var x;.private intvarsThe number of local variables.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanaddLexical(int s)Marks a symbol as lexical, declared through let or const.FramecreateFrame(boolean ref, Frame frame, java.lang.Object... args)Creates a frame by copying values up to the number of parameters.intdeclareParameter(java.lang.String param)Declares a parameter.intdeclareVariable(java.lang.String varName)Declares a local variable.intgetArgCount()Gets the (maximum) number of arguments this script expects.java.lang.IntegergetCaptured(int symbol)Gets the captured index of a given symbol, i.e., the target index of a symbol in a child scope.intgetCaptureDeclaration(int symbol)Gets the index of a captured symbol, i.e., the source index of a symbol in a parent scope.java.lang.String[]getCapturedVariables()Gets this script captured symbols names, i.e., local variables defined in outer scopes and used by this scope.java.lang.String[]getLocalVariables()Gets this script local variable, i.e.java.lang.String[]getParameters()Gets this script parameters, i.e., symbols assigned before creating local variables.(package private) java.lang.String[]getParameters(int bound)Gets this script parameters.(package private) ScopegetParent()java.lang.IntegergetSymbol(java.lang.String name)Checks whether an identifier is a local variable or argument, i.e., a symbol.private java.lang.IntegergetSymbol(java.lang.String name, boolean capture)Checks whether an identifier is a local variable or argument, i.e., a symbol.java.lang.String[]getSymbols()Gets this script symbols names, i.e., parameters and local variables.(package private) static java.util.Map<java.lang.String,java.lang.Integer>getSymbolsMap(Scope scope)Gets an unmodifiable view of a scope"e;s symbols map.booleanisCapturedSymbol(int symbol)Checks whether a given symbol is captured.booleanisLexical(int s)Checks whether a symbol is declared through a let or const.
-
-
-
Field Detail
-
UNDECLARED
static final java.lang.Object UNDECLARED
The value of an as-yet undeclared but variable, for instance: x; before var x;.
-
UNDEFINED
static final java.lang.Object UNDEFINED
The value of a declared but undefined variable, for instance: var x;.
-
EMPTY_STRS
private static final java.lang.String[] EMPTY_STRS
The empty string array.
-
parent
private final Scope parent
The parent scope.
-
parms
private int parms
The number of parameters.
-
vars
private int vars
The number of local variables.
-
namedVariables
private java.util.Map<java.lang.String,java.lang.Integer> namedVariables
The map of named variables aka script parameters and local variables. Each parameter is associated with a symbol and is materialized as an offset in the stacked array used during evaluation.
-
capturedVariables
private java.util.Map<java.lang.Integer,java.lang.Integer> capturedVariables
The map of local captured variables to parent scope variables, i.e., closure.
-
lexicalVariables
private LexicalScope lexicalVariables
Let symbols.
-
-
Constructor Detail
-
Scope
public Scope(Scope scope, java.lang.String... parameters)
Creates a new scope with a list of parameters.- Parameters:
scope- the parent scope if anyparameters- the list of parameters
-
-
Method Detail
-
getSymbolsMap
static java.util.Map<java.lang.String,java.lang.Integer> getSymbolsMap(Scope scope)
Gets an unmodifiable view of a scope"e;s symbols map.- Parameters:
scope- the scope- Returns:
- the symbols map
-
addLexical
public boolean addLexical(int s)
Marks a symbol as lexical, declared through let or const.- Parameters:
s- the symbol- Returns:
- true if the symbol was not already present in the lexical set
-
createFrame
public Frame createFrame(boolean ref, Frame frame, java.lang.Object... args)
Creates a frame by copying values up to the number of parameters.This captures the captured variables values.
- Parameters:
frame- the caller frameargs- the arguments- Returns:
- the arguments array
-
declareParameter
public int declareParameter(java.lang.String param)
Declares a parameter.This method creates a new entry in the symbol map.
- Parameters:
param- the parameter name- Returns:
- the register index storing this variable
-
declareVariable
public int declareVariable(java.lang.String varName)
Declares a local variable.This method creates a new entry in the symbol map.
- Parameters:
varName- the variable name- Returns:
- the register index storing this variable
-
getArgCount
public int getArgCount()
Gets the (maximum) number of arguments this script expects.- Returns:
- the number of parameters
-
getCaptured
public java.lang.Integer getCaptured(int symbol)
Gets the captured index of a given symbol, i.e., the target index of a symbol in a child scope.- Parameters:
symbol- the symbol index- Returns:
- the target symbol index or null if the symbol is not captured
-
getCaptureDeclaration
public int getCaptureDeclaration(int symbol)
Gets the index of a captured symbol, i.e., the source index of a symbol in a parent scope.- Parameters:
symbol- the symbol index- Returns:
- the source symbol index or -1 if the symbol is not captured
-
getCapturedVariables
public java.lang.String[] getCapturedVariables()
Gets this script captured symbols names, i.e., local variables defined in outer scopes and used by this scope.- Returns:
- the captured names
-
getLocalVariables
public java.lang.String[] getLocalVariables()
Gets this script local variable, i.e. symbols assigned to local variables excluding captured variables.- Returns:
- the local variable names
-
getParameters
public java.lang.String[] getParameters()
Gets this script parameters, i.e., symbols assigned before creating local variables.- Returns:
- the parameter names
-
getParameters
java.lang.String[] getParameters(int bound)
Gets this script parameters.- Parameters:
bound- number of known bound parameters (curry)- Returns:
- the parameter names
-
getParent
Scope getParent()
-
getSymbol
public java.lang.Integer getSymbol(java.lang.String name)
Checks whether an identifier is a local variable or argument, i.e., a symbol. If this fails, look within parents for a symbol that can be captured.- Parameters:
name- the symbol name- Returns:
- the symbol index
-
getSymbol
private java.lang.Integer getSymbol(java.lang.String name, boolean capture)Checks whether an identifier is a local variable or argument, i.e., a symbol.- Parameters:
name- the symbol namecapture- whether solving by capturing a parent symbol is allowed- Returns:
- the symbol index
-
getSymbols
public java.lang.String[] getSymbols()
Gets this script symbols names, i.e., parameters and local variables.- Returns:
- the symbol names
-
isCapturedSymbol
public boolean isCapturedSymbol(int symbol)
Checks whether a given symbol is captured.- Parameters:
symbol- the symbol number- Returns:
- true if captured, false otherwise
-
isLexical
public boolean isLexical(int s)
Checks whether a symbol is declared through a let or const.- Parameters:
s- the symbol- Returns:
- true if symbol was declared through let or const
-
-