Class JexlParser
- All Implemented Interfaces:
JexlScriptParser
- Direct Known Subclasses:
Parser
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceA lexical unit is the container defining local symbols and their visibility boundaries.private static final classThrown when a pragma key is not allowed by the features pragma test.protected class -
Field Summary
FieldsModifier and TypeFieldDescriptionThe set of assignment operators as classes.protected booleanWhether automatic semicolon insertion is enabled.protected final AtomicReference<JexlParser.LexicalUnit>The current lexical block.protected final Deque<JexlParser.LexicalUnit>Stack of lexical blocks.protected final Map<JexlParser.LexicalUnit,Scope> The map of lexical to functional blocks.static final ObjectThe name of the default case constant.protected final FeatureControllerThe associated controller.protected final AtomicReference<JexlUberspect.ClassConstantResolver>The optional class name and constant resolver.The list of imports.protected JexlInfoThe basic source info.protected AtomicIntegerThe number of nested loops.Stack of parsing loop counts.The known namespaces.static final ObjectThe name of the default NaN constant.static final ObjectThe name of the null case constant.protected final JexlParserThe parent parser if any.static final StringThe import pragma.static final StringThe prefix of a namespace pragma.static final StringThe prefix of a module pragma.static final StringThe name of the options pragma.The list of pragma declarations.protected final AtomicReference<Scope>The map of named registers aka script parameters.When parsing inner functions/lambda, need to stack the scope (sic).protected StringThe source being processed. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedCreates a new parser.protectedJexlParser(JexlParser parser) Creates a new inner-parser. -
Method Summary
Modifier and TypeMethodDescription(package private) voidvoidallowRegisters(boolean registers) Internal, for debug purpose only.protected booleanallowVariable(String image) Tests whether a given variable name is allowed.(package private) static TokenassignToken(Token src, Token dest) Assigns the content of a token to another token.protected voidbeginLambda(ASTJexlScript jjtThis) Starts the definition of a lambda.protected voidcheckLambda(Token token) Check fat vs thin arrow syntax feature.protected StringcheckVariable(ASTIdentifier identifier, String name) Checks whether an identifier is a local variable or argument, ie a symbol, stored in a register.protected voidcleanup(JexlFeatures features) Cleanup.protected voidDisables pragma feature if pragma-anywhere feature is disabled.protected voiddeclareFunction(ASTVar variable, Token token) Declares a local function.protected voiddeclareParameter(Token token, boolean lexical, boolean constant) Declares a local parameter.protected voiddeclarePragma(String key, Object value) Adds a pragma declaration.private booleandeclareSymbol(int symbol) Declares a symbol.protected voiddeclareVariable(ASTVar variable, Token token, boolean lexical, boolean constant) Declares a local variable.protected voidendLambda(ASTJexlScript jjtThis) Ends the definition of a lambda.protected static TokenerrorToken(Token... tokens) Pick the most significant token for error reporting.protected JexlFeaturesGets the current set of features active during parsing.protected ScopegetScope()Gets the frame used by this parser.protected abstract TokengetToken(int index) Overridden in actual parser to access tokens stack.protected JexlParser.LexicalUnitgetUnit()Gets the lexical unit used by this parser.protected voidIdentifier(boolean top) Default implementation does nothing but is overridden by generated code.protected booleanisAllDigits(String image) Checks whether a token image is composed only of decimal digits.protected booleanisAmbiguousStatement(int semicolon) Checks whether a statement is ambiguous.private booleanisConstant(int symbol) Checks whether a symbol has been declared as a const in the current stack of lexical units.private booleanisNamespace(String name) Checks whether a name is a declared namespace.protected booleanisNamespaceFuncall(Token ns, Token colon, Token fun, Token paren) Semantic check identifying whether a list of 4 tokens forms a namespace function call.private booleanisSymbolDeclared(JexlNode.Info info, int symbol) Checks if a symbol is defined in lexical scopes.protected booleanisVariable(String name) Checks whether an identifier is a local variable or argument.protected voidjjtreeCloseNodeScope(JexlNode node) Called by parser at end of node construction.protected voidjjtreeOpenNodeScope(JexlNode node) Called by parser at the beginning of a node construction.jxltParse(JexlInfo info, JexlFeatures features, String src, Scope scope) Parses an embedded Jexl expression within an interpolation node.(package private) static JxltEngine.ExpressionparseInterpolation(JexlInfo info, String src, Scope scope) Parses an interpolation expression.protected voidpopLoop()Restores the previous loop count.protected voidpopScope()Pops back to previous local variable scope.protected voidRestores the previous lexical unit.protected voidpushLoop()Escape any outer (parent) loops.protected voidCreates a new local variable scope and push it as current.protected voidPushes a new lexical unit.protected static StringreadSourceLine(String src, int lineno) Reads a given source line.(package private) ObjectresolveConstant(String name) protected voidsetFeatures(JexlFeatures features) Sets a new set of options.protected static StringUtility function to create '.' separated string from a list of string.(package private) static ObjectswitchCode(Object value) Encode a value to a switch predicate.protected JexlParser.SwitchSetConstructs a set of constants amenable to switch expression.protected voidThrows Ambiguous exception.protected voidthrowFeatureException(int feature, JexlInfo info) Throws a feature exception.protected voidthrowFeatureException(int feature, Token trigger) Throws a feature exception.protected voidthrowParsingException(Token parsed) Throws a parsing exception.Methods inherited from class org.apache.commons.jexl3.parser.StringParser
buildRegex, buildString, buildTemplate, escapeIdentifier, escapeRegex, escapeString, readString, unescapeIdentifierMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.apache.commons.jexl3.parser.JexlScriptParser
parse
-
Field Details
-
PRAGMA_OPTIONS
The name of the options pragma.- See Also:
-
PRAGMA_JEXLNS
The prefix of a namespace pragma.- See Also:
-
PRAGMA_MODULE
The prefix of a module pragma.- See Also:
-
PRAGMA_IMPORT
The import pragma.- See Also:
-
ASSIGN_NODES
The set of assignment operators as classes. -
featureController
The associated controller. -
info
The basic source info. -
source
The source being processed. -
scopeReference
The map of named registers aka script parameters.Each parameter is associated with a register and is materialized as an offset in the registers array used during evaluation.
-
scopes
When parsing inner functions/lambda, need to stack the scope (sic). -
pragmas
The list of pragma declarations. -
fqcnResolver
The optional class name and constant resolver. -
imports
The list of imports.Imports are used to resolve simple class names into fully qualified class names.
-
autoSemicolon
protected boolean autoSemicolonWhether automatic semicolon insertion is enabled. -
namespaces
The known namespaces. -
loopCount
The number of nested loops. -
loopCounts
Stack of parsing loop counts. -
blockReference
The current lexical block. -
blocks
Stack of lexical blocks. -
blockScopes
The map of lexical to functional blocks. -
parent
The parent parser if any. -
NIL
The name of the null case constant. -
DFLT
The name of the default case constant. -
NAN
The name of the default NaN constant.
-
-
Constructor Details
-
JexlParser
protected JexlParser()Creates a new parser.This constructor is protected so that it can only be used by subclasses.
-
JexlParser
Creates a new inner-parser.This is the constructor used to create a parser for template expressions.
-
-
Method Details
-
errorToken
Pick the most significant token for error reporting.- Parameters:
tokens- The tokens to choose from- Returns:
- The token
-
assignToken
Assigns the content of a token to another token.- Parameters:
src- The source token, if null, the destination token is returneddest- The destination token, if null, the source token is returned- Returns:
- The destination token with the content of the source token
-
readSourceLine
Reads a given source line.- Parameters:
src- The sourcelineno- The line number- Returns:
- The line
-
stringify
Utility function to create '.' separated string from a list of string.- Parameters:
lstr- The list of strings- Returns:
- The dotted version
-
addImport
-
resolveConstant
-
switchCode
Encode a value to a switch predicate.- Parameters:
value- The value.- Returns:
- The encoded value, which is either the value itself, or NAN (for NaN) or NIL (for null).
-
switchSet
Constructs a set of constants amenable to switch expression. -
allowRegisters
public void allowRegisters(boolean registers) Internal, for debug purpose only.- Parameters:
registers- sets whether this parser recognizes the register syntax
-
allowVariable
Tests whether a given variable name is allowed.- Parameters:
image- The name.- Returns:
- true if allowed, false if reserved.
-
checkLambda
Check fat vs thin arrow syntax feature.- Parameters:
token- The arrow token.
-
checkVariable
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
Cleanup.- Parameters:
features- The feature set to restore if any.
-
controlPragmaAnywhere
protected void controlPragmaAnywhere()Disables pragma feature if pragma-anywhere feature is disabled. -
declareFunction
Declares a local function.- Parameters:
variable- The identifier used to declare.token- The variable name token.
-
declareParameter
Declares a local parameter.This method creates a 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
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
Declares a local variable.This method creates an new entry in the symbol map.
- Parameters:
variable- The identifier used to declare.lexical- whether the symbol is lexical.constant- whether the symbol is constant.token- The variable name token.
-
getFeatures
Gets the current set of features active during parsing.- Returns:
- The current set of features active during parsing.
-
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
Overridden in actual parser to access tokens stack.- Parameters:
index- 0 to get current token.- Returns:
- The token on the stack.
-
getUnit
Gets the lexical unit used by this parser.- Returns:
- The named register map.
-
Identifier
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
Checks whether a name is a declared namespace.- Parameters:
name- The namespace name.- Returns:
- true if declared, false otherwise.
-
isNamespaceFuncall
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 nameparen- expected to be "("- Returns:
- true if the name qualifies a namespace function call.
-
isSymbolDeclared
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
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.
-
isAmbiguousStatement
protected boolean isAmbiguousStatement(int semicolon) Checks whether a statement is ambiguous.This is used to detect statements that are not terminated by a semicolon, and that may be confused with an expression.
- Parameters:
semicolon- The semicolon token kind.- Returns:
- true if statement is ambiguous, false otherwise.
-
isAllDigits
Checks whether a token image is composed only of decimal digits.Used to recognize a floating point literal with an omitted leading zero (e.g.
.1) that is tokenized asDOT DOT_IDENTIFIER.- Parameters:
image- the token image- Returns:
- true if every character is a decimal digit
-
jjtreeCloseNodeScope
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.
-
jxltParse
Parses an embedded Jexl expression within an interpolation node.This creates a sub-parser that shares the scopes of the parent parser.
- Specified by:
jxltParsein interfaceJexlScriptParser- Parameters:
info- The JexlInfosrc- The source to parsefeatures- The set of parsing featuresscope- The script frame- Returns:
- The parsed tree
-
parseInterpolation
Parses an interpolation expression.Requires the JEXL engine to be accessible through its thread-local.
- Parameters:
info- The JexlInfosrc- The source to parsescope- The scope- Returns:
- The expression
-
jjtreeOpenNodeScope
Called by parser at the beginning of a node construction.- Parameters:
node- The node.
-
beginLambda
Starts the definition of a lambda.- Parameters:
jjtThis- The script.
-
endLambda
Ends the definition of a lambda.- Parameters:
jjtThis- The script.
-
popScope
protected void popScope()Pops back to previous local variable scope. -
popUnit
Restores the previous lexical unit.- Parameters:
unit- restores the previous lexical scope.
-
pushScope
protected void pushScope()Creates a new local variable scope and push it as current. -
pushUnit
Pushes a new lexical unit.- Parameters:
unit- The new lexical unit.
-
pushLoop
protected void pushLoop()Escape any outer (parent) loops.A lambda definition embedded in a for-block escapes that block; break/continue are not valid within that lambda.
-
popLoop
protected void popLoop()Restores the previous loop count. -
setFeatures
Sets a new set of options.- Parameters:
features- The parser features
-
throwAmbiguousException
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
Throws a feature exception.- Parameters:
feature- The feature code.info- The exception surroundings.- Throws:
JexlException.Feature- in all cases.
-
throwFeatureException
Throws a feature exception.- Parameters:
feature- The feature code.trigger- The token that triggered it.- Throws:
JexlException.Parsing- if actual error token cannot be found.JexlException.Feature- in all other cases.
-
throwParsingException
Throws a parsing exception.- Parameters:
parsed- The token to report.- Throws:
JexlException.Parsing- in all cases.
-