Class Scope

java.lang.Object
org.apache.commons.jexl3.internal.Scope

public final class Scope extends Object
A 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 Details

    • UNDECLARED

      static final Object UNDECLARED
      The value of an as-yet undeclared but variable, for instance: x; before var x;.
    • UNDEFINED

      static final Object UNDEFINED
      The value of a declared but undefined variable, for instance: var x;.
    • EMPTY_STRS

      private static final 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 Map<String,Integer> namedVariables
      The map of named variables aka script parameters and local variables. Each parameter is associated to a symbol and is materialized as an offset in the stacked array used during evaluation.
    • capturedVariables

      private Map<Integer,Integer> capturedVariables
      The map of local captured variables to parent scope variables, ie closure.
    • lexicalVariables

      private LexicalScope lexicalVariables
      Let symbols.
  • Constructor Details

    • Scope

      public Scope(Scope scope, String... parameters)
      Creates a new scope with a list of parameters.
      Parameters:
      scope - the parent scope if any
      parameters - the list of parameters
  • Method Details

    • addLexical

      public boolean addLexical(int s)
      Marks a symbol as a 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(Frame frame, Object... args)
      Creates a frame by copying values up to the number of parameters.

      This captures the captured variables values.

      Parameters:
      frame - the caller frame
      args - the arguments
      Returns:
      the arguments array
    • declareParameter

      public int declareParameter(String name)
      Declares a parameter.

      This method creates an new entry in the symbol map.

      Parameters:
      name - the parameter name
      Returns:
      the register index storing this variable
    • declareVariable

      public int declareVariable(String name)
      Declares a local variable.

      This method creates an new entry in the symbol map.

      Parameters:
      name - 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 Integer getCaptured(int symbol)
      Gets the captured index of a given symbol, ie 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, ie 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 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 String[] getLocalVariables()
      Gets this script local variable, i.e. symbols assigned to local variables excluding captured variables.
      Returns:
      the local variable names
    • getParameters

      public String[] getParameters()
      Gets this script parameters, i.e. symbols assigned before creating local variables.
      Returns:
      the parameter names
    • getParameters

      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 Integer getSymbol(String name)
      Checks whether an identifier is a local variable or argument, ie a symbol. If this fails, look in parents for symbol that can be captured.
      Parameters:
      name - the symbol name
      Returns:
      the symbol index
    • getSymbol

      private Integer getSymbol(String name, boolean capture)
      Checks whether an identifier is a local variable or argument, ie a symbol.
      Parameters:
      name - the symbol name
      capture - whether solving by capturing a parent symbol is allowed
      Returns:
      the symbol index
    • getSymbols

      public 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