Class JexlParser

java.lang.Object
org.apache.commons.jexl3.parser.StringParser
org.apache.commons.jexl3.parser.JexlParser
Direct Known Subclasses:
Parser

public abstract class JexlParser extends StringParser
The base class for parsing, manages the parameter/local variable frame.
  • Field Details

    • PRAGMA_OPTIONS

      public static final String PRAGMA_OPTIONS
      The name of the options pragma.
      See Also:
    • PRAGMA_JEXLNS

      public static final String PRAGMA_JEXLNS
      The prefix of a namespace pragma.
      See Also:
    • PRAGMA_MODULE

      public static final String PRAGMA_MODULE
      The prefix of a module pragma.
      See Also:
    • PRAGMA_IMPORT

      public static final String PRAGMA_IMPORT
      The import pragma.
      See Also:
    • ASSIGN_NODES

      private static final Set<Class<? extends JexlNode>> ASSIGN_NODES
      The set of assignment operators as classes.
    • featureController

      protected final FeatureController featureController
      The associated controller.
    • info

      protected JexlInfo info
      The basic source info.
    • source

      protected String source
      The source being processed.
    • scope

      protected Scope scope
      The map of named registers aka script parameters.

      Each parameter is associated to a register and is materialized as an offset in the registers array used during evaluation.

    • scopes

      protected final Deque<Scope> scopes
      When parsing inner functions/lambda, need to stack the scope (sic).
    • pragmas

      protected Map<String,Object> pragmas
      The list of pragma declarations.
    • namespaces

      protected Set<String> namespaces
      The known namespaces.
    • loopCount

      protected int loopCount
      The number of nested loops.
    • loopCounts

      protected final Deque<Integer> loopCounts
      Stack of parsing loop counts.
    • block

      protected JexlParser.LexicalUnit block
      The current lexical block.
    • blocks

      protected final Deque<JexlParser.LexicalUnit> blocks
      Stack of lexical blocks.
    • blockScopes

      protected final Map<JexlParser.LexicalUnit,Scope> blockScopes
      The map of lexical to functional blocks.
  • Constructor Details

    • JexlParser

      public JexlParser()
  • Method Details

    • errorToken

      protected static Token errorToken(Token... tokens)
      Pick the most significant token for error reporting.
      Parameters:
      tokens - the tokens to choose from
      Returns:
      the token
    • readSourceLine

      protected static String readSourceLine(String src, int lineno)
      Read a given source line.
      Parameters:
      src - the source
      lineno - the line number
      Returns:
      the line
    • stringify

      protected static String stringify(Iterable<String> lstr)
      Utility function to create '.' separated string from a list of string.
      Parameters:
      lstr - the list of strings
      Returns:
      the dotted version
    • allowRegisters

      public void allowRegisters(boolean registers)
      Internal, for debug purpose only.
      Parameters:
      registers - whether register syntax is recognized by this parser
    • allowVariable

      protected boolean allowVariable(String image)
      Whether a given variable name is allowed.
      Parameters:
      image - the name
      Returns:
      true if allowed, false if reserved
    • checkLambda

      protected void checkLambda(Token token)
      Check fat vs thin arrow syntax feature.
      Parameters:
      token - the arrow token
    • checkVariable

      protected String checkVariable(ASTIdentifier identifier, String name)
      Checks whether an identifier is a local variable or argument, ie a symbol, stored in a register.
      Parameters:
      identifier - the identifier
      name - the identifier name
      Returns:
      the image
    • cleanup

      protected void cleanup(JexlFeatures features)
      Cleanup.
      Parameters:
      features - the feature set to restore if any
    • controlPragmaAnywhere

      protected void controlPragmaAnywhere()
      Disables pragma feature if pragma-anywhere feature is disabled.
    • declareFunction

      protected void declareFunction(ASTVar variable, Token token)
      Declares a local function.
      Parameters:
      variable - the identifier used to declare
      token - the variable name token
    • declareParameter

      protected void declareParameter(Token token, boolean lexical, boolean constant)
      Declares a local parameter.

      This method creates an new entry in the symbol map.

      Parameters:
      token - the parameter name token
      lexical - whether the parameter is lexical or not
      constant - whether the parameter is constant or not
    • declarePragma

      protected void declarePragma(String key, Object value)
      Adds a pragma declaration.
      Parameters:
      key - the pragma key
      value - the pragma value
    • declareSymbol

      private boolean declareSymbol(int symbol)
      Declares a symbol.
      Parameters:
      symbol - the symbol index
      Returns:
      true if symbol can be declared in lexical scope, false (error) if it is already declared
    • declareVariable

      protected void declareVariable(ASTVar variable, Token token, boolean lexical, boolean constant)
      Declares a local variable.

      This method creates an new entry in the symbol map.

      Parameters:
      variable - the identifier used to declare
      token - the variable name toekn
      lexical - whether the symbol is lexical
      constant - whether the symbol is constant
    • getFeatures

      protected JexlFeatures getFeatures()
      Returns:
      the current set of features active during parsing
    • getScope

      protected Scope getScope()
      Gets the frame used by this parser.

      Since local variables create new symbols, it is important to regain access after parsing to known which / how-many registers are needed.

      Returns:
      the named register map
    • getToken

      protected abstract Token getToken(int index)
      Overridden in actual parser to access tokens stack.
      Parameters:
      index - 0 to get current token
      Returns:
      the token on the stack
    • getUnit

      protected JexlParser.LexicalUnit getUnit()
      Gets the lexical unit used by this parser.
      Returns:
      the named register map
    • Identifier

      protected void Identifier(boolean top) throws ParseException
      Default implementation does nothing but is overridden by generated code.
      Parameters:
      top - whether the identifier is beginning an l/r value
      Throws:
      ParseException - subclasses may throw ParseException
    • isConstant

      private boolean isConstant(int symbol)
      Checks whether a symbol has been declared as a const in the current stack of lexical units.
      Parameters:
      symbol - the symbol
      Returns:
      true if constant, false otherwise
    • isNamespace

      private boolean isNamespace(String name)
      Checks whether a name is a declared namespace.
      Parameters:
      name - the namespace name
      Returns:
      true if declared, false otherwise
    • isNamespaceFuncall

      protected boolean isNamespaceFuncall(Token ns, Token colon, Token fun, Token paren)
      Semantic check identifying whether a list of 4 tokens forms a namespace function call.

      This is needed to disambiguate ternary operator, map entries and actual calls.

      Note that this check is performed before syntactic check so the expected parameters need to be verified.

      Parameters:
      ns - the namespace token
      colon - expected to be ":"
      fun - the function name
      paren - expected to be "("
      Returns:
      true if the name qualifies a namespace function call
    • isSymbolDeclared

      private boolean isSymbolDeclared(JexlNode.Info info, int symbol)
      Checks if a symbol is defined in lexical scopes.

      This works with parsed scripts in template resolution only.

      Parameters:
      info - an info linked to a node
      symbol - the symbol number
      Returns:
      true if symbol accessible in lexical scope
    • isVariable

      protected boolean isVariable(String name)
      Checks whether an identifier is a local variable or argument.
      Parameters:
      name - the variable name
      Returns:
      true if a variable with that name was declared
    • jjtreeCloseNodeScope

      protected void jjtreeCloseNodeScope(JexlNode node)
      Called by parser at end of node construction.

      Detects "Ambiguous statement" and 'non-left value assignment'.

      Parameters:
      node - the node
      Throws:
      JexlException.Parsing - when parsing fails
    • jjtreeOpenNodeScope

      protected void jjtreeOpenNodeScope(JexlNode node)
      Called by parser at beginning of node construction.
      Parameters:
      node - the node
    • popScope

      protected void popScope()
      Pops back to previous local variable scope.
    • popUnit

      protected void popUnit(JexlParser.LexicalUnit unit)
      Restores the previous lexical unit.
      Parameters:
      unit - restores the previous lexical scope
    • pushScope

      protected void pushScope()
      Create a new local variable scope and push it as current.
    • pushUnit

      protected void pushUnit(JexlParser.LexicalUnit unit)
      Pushes a new lexical unit.
      Parameters:
      unit - the new lexical unit
    • setFeatures

      protected void setFeatures(JexlFeatures features)
      Sets a new set of options.
      Parameters:
      features - the parser features
    • throwAmbiguousException

      protected void throwAmbiguousException(JexlNode node)
      Throws Ambiguous exception.

      Seeks the end of the ambiguous statement to recover.

      Parameters:
      node - the first token in ambiguous expression
      Throws:
      JexlException.Ambiguous - in all cases
    • throwFeatureException

      protected void throwFeatureException(int feature, JexlInfo info)
      Throws a feature exception.
      Parameters:
      feature - the feature code
      info - the exception surroundings
      Throws:
      JexlException.Feature - in all cases
    • throwFeatureException

      protected void throwFeatureException(int feature, Token trigger)
      Throws a feature exception.
      Parameters:
      feature - the feature code
      trigger - the token that triggered it
      Throws:
      JexlException.Parsing - if actual error token can not be found
      JexlException.Feature - in all other cases
    • throwParsingException

      protected void throwParsingException(Token parsed)
      Throws a parsing exception.
      Parameters:
      parsed - the token to report
      Throws:
      JexlException.Parsing - in all cases