Class JexlParser
- java.lang.Object
-
- org.apache.commons.jexl3.parser.StringParser
-
- org.apache.commons.jexl3.parser.JexlParser
-
- All Implemented Interfaces:
JexlScriptParser
- Direct Known Subclasses:
Parser
public abstract class JexlParser extends StringParser implements JexlScriptParser
The base class for parsing, manages the parameter/local variable frame.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceJexlParser.LexicalUnitA lexical unit is the container defining local symbols and their visibility boundaries.protected classJexlParser.SwitchSet
-
Field Summary
Fields Modifier and Type Field Description private static java.util.Set<java.lang.Class<? extends JexlNode>>ASSIGN_NODESThe set of assignment operators as classes.protected booleanautoSemicolonWhether automatic semicolon insertion is enabled.protected java.util.concurrent.atomic.AtomicReference<JexlParser.LexicalUnit>blockReferenceThe current lexical block.protected java.util.Deque<JexlParser.LexicalUnit>blocksStack of lexical blocks.protected java.util.Map<JexlParser.LexicalUnit,Scope>blockScopesThe map of lexical to functional blocks.static java.lang.ObjectDFLTThe name of the default case constant.protected FeatureControllerfeatureControllerThe associated controller.protected java.util.concurrent.atomic.AtomicReference<JexlUberspect.ClassConstantResolver>fqcnResolverThe optional class name and constant resolver.protected java.util.List<java.lang.String>importsThe list of imports.protected JexlInfoinfoThe basic source info.protected java.util.concurrent.atomic.AtomicIntegerloopCountThe number of nested loops.protected java.util.Deque<java.lang.Integer>loopCountsStack of parsing loop counts.protected java.util.Set<java.lang.String>namespacesThe known namespaces.static java.lang.ObjectNANThe name of the default NaN constant.static java.lang.ObjectNILThe name of the null case constant.protected JexlParserparentThe parent parser if any.static java.lang.StringPRAGMA_IMPORTThe import pragma.static java.lang.StringPRAGMA_JEXLNSThe prefix of a namespace pragma.static java.lang.StringPRAGMA_MODULEThe prefix of a module pragma.static java.lang.StringPRAGMA_OPTIONSThe name of the options pragma.protected java.util.Map<java.lang.String,java.lang.Object>pragmasThe list of pragma declarations.protected java.util.concurrent.atomic.AtomicReference<Scope>scopeReferenceThe map of named registers aka script parameters.protected java.util.Deque<Scope>scopesWhen parsing inner functions/lambda, need to stack the scope (sic).protected java.lang.StringsourceThe source being processed.
-
Constructor Summary
Constructors Modifier Constructor Description protectedJexlParser()Creates a new parser.protectedJexlParser(JexlParser parser)Creates a new inner-parser.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description (package private) voidaddImport(java.lang.String importName)voidallowRegisters(boolean registers)Internal, for debug purpose only.protected booleanallowVariable(java.lang.String image)Tests whether a given variable name is allowed.protected voidbeginLambda(ASTJexlScript jjtThis)Starts the definition of a lambda.protected voidcheckLambda(Token token)Check fat vs thin arrow syntax feature.protected java.lang.StringcheckVariable(ASTIdentifier identifier, java.lang.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 voidcontrolPragmaAnywhere()Disables 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(java.lang.String key, java.lang.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 JexlFeaturesgetFeatures()Gets 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 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(java.lang.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(java.lang.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.ASTJexlScriptjxltParse(JexlInfo info, JexlFeatures features, java.lang.String src, Scope scope)Parses an embedded Jexl expression within an interpolation node.(package private) static JxltEngine.ExpressionparseInterpolation(JexlInfo info, java.lang.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 voidpopUnit(JexlParser.LexicalUnit unit)Restores the previous lexical unit.protected voidpushLoop()Escape any outer (parent) loops.protected voidpushScope()Creates a new local variable scope and push it as current.protected voidpushUnit(JexlParser.LexicalUnit unit)Pushes a new lexical unit.protected static java.lang.StringreadSourceLine(java.lang.String src, int lineno)Reads a given source line.(package private) java.lang.ObjectresolveConstant(java.lang.String name)protected voidsetFeatures(JexlFeatures features)Sets a new set of options.protected static java.lang.Stringstringify(java.lang.Iterable<java.lang.String> lstr)Utility function to create '.' separated string from a list of string.(package private) static java.lang.ObjectswitchCode(java.lang.Object value)Encode a value to a switch predicate.protected JexlParser.SwitchSetswitchSet()Constructs a set of constants amenable to switch expression.protected voidthrowAmbiguousException(JexlNode node)Throws 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, escapeString, readString, unescapeIdentifier
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.apache.commons.jexl3.parser.JexlScriptParser
parse
-
-
-
-
Field Detail
-
PRAGMA_OPTIONS
public static final java.lang.String PRAGMA_OPTIONS
The name of the options pragma.- See Also:
- Constant Field Values
-
PRAGMA_JEXLNS
public static final java.lang.String PRAGMA_JEXLNS
The prefix of a namespace pragma.- See Also:
- Constant Field Values
-
PRAGMA_MODULE
public static final java.lang.String PRAGMA_MODULE
The prefix of a module pragma.- See Also:
- Constant Field Values
-
PRAGMA_IMPORT
public static final java.lang.String PRAGMA_IMPORT
The import pragma.- See Also:
- Constant Field Values
-
ASSIGN_NODES
private static final java.util.Set<java.lang.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 java.lang.String source
The source being processed.
-
scopeReference
protected final java.util.concurrent.atomic.AtomicReference<Scope> 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
protected final java.util.Deque<Scope> scopes
When parsing inner functions/lambda, need to stack the scope (sic).
-
pragmas
protected java.util.Map<java.lang.String,java.lang.Object> pragmas
The list of pragma declarations.
-
fqcnResolver
protected final java.util.concurrent.atomic.AtomicReference<JexlUberspect.ClassConstantResolver> fqcnResolver
The optional class name and constant resolver.
-
imports
protected final java.util.List<java.lang.String> imports
The list of imports.Imports are used to resolve simple class names into fully qualified class names.
-
autoSemicolon
protected boolean autoSemicolon
Whether automatic semicolon insertion is enabled.
-
namespaces
protected java.util.Set<java.lang.String> namespaces
The known namespaces.
-
loopCount
protected java.util.concurrent.atomic.AtomicInteger loopCount
The number of nested loops.
-
loopCounts
protected final java.util.Deque<java.lang.Integer> loopCounts
Stack of parsing loop counts.
-
blockReference
protected final java.util.concurrent.atomic.AtomicReference<JexlParser.LexicalUnit> blockReference
The current lexical block.
-
blocks
protected final java.util.Deque<JexlParser.LexicalUnit> blocks
Stack of lexical blocks.
-
blockScopes
protected final java.util.Map<JexlParser.LexicalUnit,Scope> blockScopes
The map of lexical to functional blocks.
-
parent
protected final JexlParser parent
The parent parser if any.
-
NIL
public static final java.lang.Object NIL
The name of the null case constant.
-
DFLT
public static final java.lang.Object DFLT
The name of the default case constant.
-
NAN
public static final java.lang.Object NAN
The name of the default NaN constant.
-
-
Constructor Detail
-
JexlParser
protected JexlParser()
Creates a new parser.This constructor is protected so that it can only be used by subclasses.
-
JexlParser
protected JexlParser(JexlParser parser)
Creates a new inner-parser.This is the constructor used to create a parser for template expressions.
-
-
Method Detail
-
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 java.lang.String readSourceLine(java.lang.String src, int lineno)Reads a given source line.- Parameters:
src- the sourcelineno- the line number- Returns:
- the line
-
stringify
protected static java.lang.String stringify(java.lang.Iterable<java.lang.String> lstr)
Utility function to create '.' separated string from a list of string.- Parameters:
lstr- the list of strings- Returns:
- the dotted version
-
addImport
void addImport(java.lang.String importName)
-
resolveConstant
java.lang.Object resolveConstant(java.lang.String name)
-
switchCode
static java.lang.Object switchCode(java.lang.Object value)
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
protected JexlParser.SwitchSet 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
protected boolean allowVariable(java.lang.String image)
Tests 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 java.lang.String checkVariable(ASTIdentifier identifier, java.lang.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 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
protected void declarePragma(java.lang.String key, java.lang.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.lexical- whether the symbol is lexical.constant- whether the symbol is constant.token- the variable name token.
-
getFeatures
protected JexlFeatures getFeatures()
Gets the current set of features active during parsing.- 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 ParseExceptionDefault 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(java.lang.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 nameparen- 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(java.lang.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.
-
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.
-
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.
-
jxltParse
public ASTJexlScript jxltParse(JexlInfo info, JexlFeatures features, java.lang.String src, Scope scope)
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
static JxltEngine.Expression parseInterpolation(JexlInfo info, java.lang.String src, Scope scope)
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
protected void jjtreeOpenNodeScope(JexlNode node)
Called by parser at the beginning of a node construction.- Parameters:
node- the node.
-
beginLambda
protected void beginLambda(ASTJexlScript jjtThis)
Starts the definition of a lambda.- Parameters:
jjtThis- the script.
-
endLambda
protected void endLambda(ASTJexlScript jjtThis)
Ends the definition of a lambda.- Parameters:
jjtThis- the script.
-
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()
Creates 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.
-
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
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 cannot 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.
-
-