public final class ExpressionParser extends Object implements ExpressionParserConstants
Important: Do not use outside of Soy code (treat as superpackage-private).
This parser parses several related things: variables, data references, globals, expression lists,
and expressions.
A. Variable:
+ A dollar sign "$" followed by an identifier (no space between).
B. Data reference:
+ The first part must be "$" followed by the first key name (no space between).
+ The first key name cannot be a number.
+ A variable will only have the first part. A data reference may have subsequent parts.
+ Subsequent parts may be:
- A dot "." or question-dot "?." followed by a key name or array index (spaces between are
allowed).
- Brackets "[ ]" or question-brackets "?[ ]" with any expression inside the brackets (see
below for definition of expression).
+ A special case is when the first key name is "ij". In this case, it's a reference to
injected data, and the reference is considered to start from the second key (i.e. the second
key actually becomes the first key in the parsed node).
Examples: $aaa $ij.aaa $aaa.bbb.0.ccc.12 $aaa[0]['bbb'].ccc $aaa[$bbb + $ccc]
C. Global:
+ One or more identifiers. If more than one, a dot "." is used to separate them.
+ Must not be preceded by a dollar sign "$".
Examples: AAA aaa.bbb.CCC a22.b88_
D. Expression list:
+ A comma-separated list of one or more expressions (see below for definition of expression).
Example: $aaa, $bbb.ccc + 1, round(3.14)
E. Expression:
1. Data reference:
+ See above for definition.
2. Global:
+ See above for definition.
3. Null: null
4. Boolean: false true
5. Integer:
+ No octal numbers.
+ Hex numbers have strict lower case "x" in "0x" and "A-F" or "a-f".
Examples: 0 26 -729 0x1a2B
6. Float:
+ Decimal numbers only.
+ Must have digits on both sides of decimal point.
+ Exponents have strict lower case "e".
Examples: 0.0 3.14159 -20.0 6.03e23 -3e-3
7. String:
+ Single quotes only.
+ Escape sequences: \\ \' \" \n \r \t \b \f
+ Unicode escape: \ u #### (backslash, "u", four hex digits -- no spaces in between)
Examples: '' 'abc' 'blah bleh bluh' 'aa\\bb\'cc\ndd'
8. List literal:
+ Delimited by brackets.
Examples: [] ['blah', 123, $foo]
9. Map literal:
+ Delimited by brackets.
+ Empty map has a single colon within the brackets (to distinguish from empty list).
+ Keys must be strings (or expressions that will evaluate to strings).
Examples: [:] ['aaa': 'blah', 'bbb': 123, $boo: $foo]
10. Operators:
+ Parentheses can be used to override precedence rules: ( )
+ Precedence 8: - (unary) not
+ Precedence 7: * / %
+ Precedence 6: + - (binary)
+ Precedence 5: < > <= >=
+ Precedence 4: == !=
+ Precedence 3: and
+ Precedence 2: or
+ Precedence 1: ?: (binary) ? : (ternary)
11. Functions:
+ Function name, open parenthesis, optional expression list, close parenthesis.
+ The function name is one identifier.
+ See above for the definition of an expression list.
Examples: isFirst($item) foo() myFunction(2, 'aa')
| Modifier and Type | Field and Description |
|---|---|
static SoyErrorKind |
INVALID_EXPRESSION_LIST |
static SoyErrorKind |
INVALID_VAR_NAME |
Token |
jj_nt
Next token.
|
Token |
token
Current token.
|
ExpressionParserTokenManager |
token_source
Generated Token Manager.
|
BOOLEAN, DEC_DIGITS, DEFAULT, DOLLAR_IDENT, DOT_IDENT, DOUBLE_QUOTE, EOF, FLOAT, HEX_DIGIT, IDENT, INTEGER, LEGACY_AND, LEGACY_NOT, LEGACY_OR, NULL, PRECEDENCE_2_OP, PRECEDENCE_3_OP, PRECEDENCE_4_OP, PRECEDENCE_5_OP, PRECEDENCE_6_OP, PRECEDENCE_7_OP, QUESTION_DOT_IDENT, STRING, tokenImage, UNARY_OP, UNEXPECTED_TOKEN, WS| Constructor and Description |
|---|
ExpressionParser(ExpressionParserTokenManager tm)
Constructor with generated Token Manager.
|
ExpressionParser(InputStream stream)
Constructor with InputStream.
|
ExpressionParser(InputStream stream,
String encoding)
Constructor with InputStream and supplied encoding
|
ExpressionParser(Reader stream)
Constructor.
|
ExpressionParser(String input,
SourceLocation parentSourceLocation,
SoyParsingContext context)
Constructor that takes a string input.
|
| Modifier and Type | Method and Description |
|---|---|
void |
disable_tracing()
Disable tracing.
|
void |
enable_tracing()
Enable tracing.
|
ParseException |
generateParseException()
Generate ParseException.
|
Token |
getNextToken()
Get the next Token.
|
Token |
getToken(int index)
Get the specific Token.
|
ExprNode |
parseExpression()
Parses the input as an expression.
|
List<ExprNode> |
parseExpressionList()
Parses the input as a comma-separated list of expressions.
|
VarNode |
parseVariable()
Parses the input as a variable name.
|
void |
ReInit(ExpressionParserTokenManager tm)
Reinitialise.
|
void |
ReInit(InputStream stream)
Reinitialise.
|
void |
ReInit(InputStream stream,
String encoding)
Reinitialise.
|
void |
ReInit(Reader stream)
Reinitialise.
|
public static final SoyErrorKind INVALID_EXPRESSION_LIST
public static final SoyErrorKind INVALID_VAR_NAME
public ExpressionParserTokenManager token_source
public Token token
public Token jj_nt
public ExpressionParser(String input, SourceLocation parentSourceLocation, SoyParsingContext context)
input - The input to parse.parentSourceLocation - the source location associated with the input.errorReporter - For reporting syntax errors.public ExpressionParser(InputStream stream)
public ExpressionParser(InputStream stream, String encoding)
public ExpressionParser(Reader stream)
public ExpressionParser(ExpressionParserTokenManager tm)
public List<ExprNode> parseExpressionList()
Maintains the following invariant: an empty list is returned if and only if
an error is reported to the parser's ErrorReporter.
public ExprNode parseExpression()
Maintains the following invariant: an error node is returned if and only if an error
is reported to the parser's ErrorReporter.
public VarNode parseVariable()
Maintains the following invariant: an error node is returned if and only if an error
is reported to the parser's ErrorReporter.
public void ReInit(InputStream stream)
public void ReInit(InputStream stream, String encoding)
public void ReInit(Reader stream)
public void ReInit(ExpressionParserTokenManager tm)
public final Token getNextToken()
public final Token getToken(int index)
public ParseException generateParseException()
public final void enable_tracing()
public final void disable_tracing()