/* * Copyright (C) 2018 Google, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.google.escapevelocity; import com.google.common.base.CharMatcher; import com.google.common.base.Verify; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableListMultimap; import com.google.common.collect.Iterables; import com.google.common.primitives.Chars; import com.google.common.primitives.Ints; import com.google.escapevelocity.DirectiveNode.SetNode; import com.google.escapevelocity.ExpressionNode.BinaryExpressionNode; import com.google.escapevelocity.ExpressionNode.NotExpressionNode; import com.google.escapevelocity.ReferenceNode.IndexReferenceNode; import com.google.escapevelocity.ReferenceNode.MemberReferenceNode; import com.google.escapevelocity.ReferenceNode.MethodReferenceNode; import com.google.escapevelocity.ReferenceNode.PlainReferenceNode; import com.google.escapevelocity.TokenNode.CommentTokenNode; import com.google.escapevelocity.TokenNode.ElseIfTokenNode; import com.google.escapevelocity.TokenNode.ElseTokenNode; import com.google.escapevelocity.TokenNode.EndTokenNode; import com.google.escapevelocity.TokenNode.EofNode; import com.google.escapevelocity.TokenNode.ForEachTokenNode; import com.google.escapevelocity.TokenNode.IfTokenNode; import com.google.escapevelocity.TokenNode.MacroDefinitionTokenNode; import com.google.escapevelocity.TokenNode.NestedTokenNode; import java.io.IOException; import java.io.LineNumberReader; import java.io.Reader; /** * A parser that reads input from the given {@link Reader} and parses it to produce a * {@link Template}. * * @author emcmanus@google.com (Éamonn McManus) */ class Parser { private static final int EOF = -1; private final LineNumberReader reader; private final String resourceName; private final Template.ResourceOpener resourceOpener; /** * The invariant of this parser is that {@code c} is always the next character of interest. * This means that we almost never have to "unget" a character by reading too far. For example, * after we parse an integer, {@code c} will be the first character after the integer, which is * exactly the state we will be in when there are no more digits. * *
Sometimes we need to read two characters ahead, and in that case we use {@link #pushback}. */ private int c; /** * A single character of pushback. If this is not negative, the {@link #next()} method will * return it instead of reading a character. */ private int pushback = -1; Parser(Reader reader, String resourceName, Template.ResourceOpener resourceOpener) throws IOException { this.reader = new LineNumberReader(reader); this.reader.setLineNumber(1); next(); this.resourceName = resourceName; this.resourceOpener = resourceOpener; } /** * Parse the input completely to produce a {@link Template}. * *
Parsing happens in two phases. First, we parse a sequence of "tokens", where tokens include * entire references such as
* ${x.foo()[23]}
* or entire directives such as* #set ($x = $y + $z) *But tokens do not span complex constructs. For example,
* #if ($x == $y) something #end *is three tokens:
* #if ($x == $y) * (literal text " something ") * #end ** *
The second phase then takes the sequence of tokens and constructs a parse tree out of it. * Some nodes in the parse tree will be unchanged from the token sequence, such as the
* ${x.foo()[23]}
* #set ($x = $y + $z)
* examples above. But a construct such as the {@code #if ... #end} mentioned above will
* become a single IfNode in the parse tree in the second phase.
*
* The main reason for this approach is that Velocity has two kinds of lexical contexts. At the
* top level, there can be arbitrary literal text; references like ${x.foo()}; and
* directives like {@code #if} or {@code #set}. Inside the parentheses of a directive, however,
* neither arbitrary text nor directives can appear, but expressions can, so we need to tokenize
* the inside of
* #if ($x == $a + $b) *as the five tokens "$x", "==", "$a", "+", "$b". Rather than having a classical * parser/lexer combination, where the lexer would need to switch between these two modes, we * replace the lexer with an ad-hoc parser that is the first phase described above, and we * define a simple parser over the resultant tokens that is the second phase. */ Template parse() throws IOException { ImmutableList
{@code
* -> |
* |
*
* }
*/
private Node parseNode() throws IOException {
if (c == '#') {
next();
switch (c) {
case '#':
return parseLineComment();
case '*':
return parseBlockComment();
case '[':
return parseHashSquare();
case '{':
return parseDirective();
default:
if (isAsciiLetter(c)) {
return parseDirective();
} else {
// For consistency with Velocity, we treat # not followed by a letter or one of the
// characters above as a plain character, and we treat #$foo as a literal # followed by
// the reference $foo.
return parsePlainText('#');
}
}
}
if (c == EOF) {
return new EofNode(resourceName, lineNumber());
}
return parseNonDirective();
}
private Node parseHashSquare() throws IOException {
// We've just seen #[ which might be the start of a #[[quoted block]]#. If the next character
// is not another [ then it's not a quoted block, but it *is* a literal #[ followed by whatever
// that next character is.
assert c == '[';
next();
if (c != '[') {
return parsePlainText(new StringBuilder("#["));
}
int startLine = lineNumber();
next();
StringBuilder sb = new StringBuilder();
while (true) {
if (c == EOF) {
throw new ParseException(
"Unterminated #[[ - did not see matching ]]#", resourceName, startLine);
}
if (c == '#') {
// This might be the last character of ]]# or it might just be a random #.
int len = sb.length();
if (len > 1 && sb.charAt(len - 1) == ']' && sb.charAt(len - 2) == ']') {
next();
break;
}
}
sb.append((char) c);
next();
}
String quoted = sb.substring(0, sb.length() - 2);
return new ConstantExpressionNode(resourceName, lineNumber(), quoted);
}
/**
* Parses a single non-directive node from the reader.
* {@code
* -> |
*
* }
*/
private Node parseNonDirective() throws IOException {
if (c == '$') {
next();
if (isAsciiLetter(c) || c == '{') {
return parseReference();
} else {
return parsePlainText('$');
}
} else {
int firstChar = c;
next();
return parsePlainText(firstChar);
}
}
/**
* Parses a single directive token from the reader. Directives can be spelled with or without
* braces, for example {@code #if} or {@code #{if}}. We omit the brace spelling in the productions
* here: {@code
* -> |
* |
* |
* |
* |
* |
* |
* |
* |
*
* }
*/
private Node parseDirective() throws IOException {
String directive;
if (c == '{') {
next();
directive = parseId("Directive inside #{...}");
expect('}');
} else {
directive = parseId("Directive");
}
Node node;
switch (directive) {
case "end":
node = new EndTokenNode(resourceName, lineNumber());
break;
case "if":
case "elseif":
node = parseIfOrElseIf(directive);
break;
case "else":
node = new ElseTokenNode(resourceName, lineNumber());
break;
case "foreach":
node = parseForEach();
break;
case "set":
node = parseSet();
break;
case "parse":
node = parseParse();
break;
case "macro":
node = parseMacroDefinition();
break;
default:
node = parsePossibleMacroCall(directive);
}
// Velocity skips a newline after any directive.
// TODO(emcmanus): in fact it also skips space before the newline, which should be implemented.
if (c == '\n') {
next();
}
return node;
}
/**
* Parses the condition following {@code #if} or {@code #elseif}.
* {@code
* -> #if ( )
* -> #elseif ( )
* }
*
* @param directive either {@code "if"} or {@code "elseif"}.
*/
private Node parseIfOrElseIf(String directive) throws IOException {
expect('(');
ExpressionNode condition = parseExpression();
expect(')');
return directive.equals("if") ? new IfTokenNode(condition) : new ElseIfTokenNode(condition);
}
/**
* Parses a {@code #foreach} token from the reader. {@code
* -> #foreach ( $ in )
* }
*/
private Node parseForEach() throws IOException {
expect('(');
expect('$');
String var = parseId("For-each variable");
skipSpace();
boolean bad = false;
if (c != 'i') {
bad = true;
} else {
next();
if (c != 'n') {
bad = true;
}
}
if (bad) {
throw parseException("Expected 'in' for #foreach");
}
next();
ExpressionNode collection = parseExpression();
expect(')');
return new ForEachTokenNode(var, collection);
}
/**
* Parses a {@code #set} token from the reader. {@code
* -> #set ( $ = )
* }
*/
private Node parseSet() throws IOException {
expect('(');
expect('$');
String var = parseId("#set variable");
expect('=');
ExpressionNode expression = parseExpression();
expect(')');
return new SetNode(var, expression);
}
/**
* Parses a {@code #parse} token from the reader. {@code
* -> #parse ( )
* }
*
* The way this works is inconsistent with Velocity. In Velocity, the {@code #parse} directive
* is evaluated when it is encountered during template evaluation. That means that the argument
* can be a variable, and it also means that you can use {@code #if} to choose whether or not
* to do the {@code #parse}. Neither of those is true in EscapeVelocity. The contents of the
* {@code #parse} are integrated into the containing template pretty much as if they had been
* written inline. That also means that EscapeVelocity allows forward references to macros
* inside {@code #parse} directives, which Velocity does not.
*/
private Node parseParse() throws IOException {
expect('(');
skipSpace();
if (c != '"' && c != '\'') {
throw parseException("#parse only supported with string literal argument");
}
ExpressionNode nestedResourceNameExpression = parseStringLiteral(c, false);
String nestedResourceName = nestedResourceNameExpression.evaluate(null).toString();
expect(')');
try (Reader nestedReader = resourceOpener.openResource(nestedResourceName)) {
Parser nestedParser = new Parser(nestedReader, nestedResourceName, resourceOpener);
ImmutableList Macro parameters are optionally separated by commas.
*/
private Node parseMacroDefinition() throws IOException {
expect('(');
skipSpace();
String name = parseId("Macro name");
ImmutableList.Builder On entry to this method, {@link #c} is the character immediately after the {@code $}.
*/
private Node parseReference() throws IOException {
if (c == '{') {
next();
if (!isAsciiLetter(c)) {
return parsePlainText(new StringBuilder("${"));
}
ReferenceNode node = parseReferenceNoBrace();
expect('}');
return node;
} else {
return parseReferenceNoBrace();
}
}
/**
* Same as {@link #parseReference()}, except it really must be a reference. A {@code $} in
* normal text doesn't start a reference if it is not followed by an identifier. But in an
* expression, for example in {@code #if ($x == 23)}, {@code $} must be followed by an
* identifier.
*/
private ReferenceNode parseRequiredReference() throws IOException {
if (c == '{') {
next();
ReferenceNode node = parseReferenceNoBrace();
expect('}');
return node;
} else {
return parseReferenceNoBrace();
}
}
/**
* Parses a reference, in the simple form without braces.
* {@code
*
*
* {@code
*
*/
private Node parsePossibleMacroCall(String directive) throws IOException {
skipSpace();
if (c != '(') {
throw parseException("Unrecognized directive #" + directive);
}
next();
ImmutableList.Builder{@code
*
*
* {@code
*
*/
private ReferenceNode parseReferenceNoBrace() throws IOException {
String id = parseId("Reference");
ReferenceNode lhs = new PlainReferenceNode(resourceName, lineNumber(), id);
return parseReferenceSuffix(lhs);
}
/**
* Parses the modifiers that can appear at the tail of a reference.
* {@code
*
*
* @param lhs the reference node representing the first part of the reference
* {@code $x} in {@code $x.foo} or {@code $x.foo()}, or later {@code $x.y} in {@code $x.y.z}.
*/
private ReferenceNode parseReferenceSuffix(ReferenceNode lhs) throws IOException {
switch (c) {
case '.':
return parseReferenceMember(lhs);
case '[':
return parseReferenceIndex(lhs);
default:
return lhs;
}
}
/**
* Parses a reference member, which is either a property reference like {@code $x.y} or a method
* call like {@code $x.y($z)}.
* {@code
*
*
* @param lhs the reference node representing what appears to the left of the dot, like the
* {@code $x} in {@code $x.foo} or {@code $x.foo()}.
*/
private ReferenceNode parseReferenceMember(ReferenceNode lhs) throws IOException {
assert c == '.';
next();
if (!isAsciiLetter(c)) {
// We've seen something like `$foo.!`, so it turns out it's not a member after all.
pushback('.');
return lhs;
}
String id = parseId("Member");
ReferenceNode reference;
if (c == '(') {
reference = parseReferenceMethodParams(lhs, id);
} else {
reference = new MemberReferenceNode(lhs, id);
}
return parseReferenceSuffix(reference);
}
/**
* Parses the parameters to a method reference, like {@code $foo.bar($a, $b)}.
* {@code
*
*
* @param lhs the reference node representing what appears to the left of the dot, like the
* {@code $x} in {@code $x.foo()}.
*/
private ReferenceNode parseReferenceMethodParams(ReferenceNode lhs, String id)
throws IOException {
assert c == '(';
nextNonSpace();
ImmutableList.Builder{@code
*
*
* @param lhs the reference node representing what appears to the left of the dot, like the
* {@code $x} in {@code $x[$i]}.
*/
private ReferenceNode parseReferenceIndex(ReferenceNode lhs) throws IOException {
assert c == '[';
next();
ExpressionNode index = parseExpression();
if (c != ']') {
throw parseException("Expected ]");
}
next();
ReferenceNode reference = new IndexReferenceNode(lhs, index);
return parseReferenceSuffix(reference);
}
enum Operator {
/**
* A dummy operator with low precedence. When parsing subexpressions, we always stop when we
* reach an operator of lower precedence than the "current precedence". For example, when
* parsing {@code 1 + 2 * 3 + 4}, we'll stop parsing the subexpression {@code * 3 + 4} when
* we reach the {@code +} because it has lower precedence than {@code *}. This dummy operator,
* then, behaves like {@code +} when the minimum precedence is {@code *}. We also return it
* if we're looking for an operator and don't find one. If this operator is {@code ⊙}, it's as
* if our expressions are bracketed with it, like {@code ⊙ 1 + 2 * 3 + 4 ⊙}.
*/
STOP("", 0),
// If a one-character operator is a prefix of a two-character operator, like < and <=, then
// the one-character operator must come first.
OR("||", 1),
AND("&&", 2),
EQUAL("==", 3), NOT_EQUAL("!=", 3),
LESS("<", 4), LESS_OR_EQUAL("<=", 4), GREATER(">", 4), GREATER_OR_EQUAL(">=", 4),
PLUS("+", 5), MINUS("-", 5),
TIMES("*", 6), DIVIDE("/", 6), REMAINDER("%", 6);
final String symbol;
final int precedence;
Operator(String symbol, int precedence) {
this.symbol = symbol;
this.precedence = precedence;
}
@Override
public String toString() {
return symbol;
}
}
/**
* Maps a code point to the operators that begin with that code point. For example, maps
* {@code <} to {@code LESS} and {@code LESS_OR_EQUAL}.
*/
private static final ImmutableListMultimap{@code
*
*/
private ExpressionNode parseExpression() throws IOException {
ExpressionNode lhs = parseUnaryExpression();
return new OperatorParser().parse(lhs, 1);
}
/**
* An operator-precedence parser for the binary operations we understand. It implements an
* algorithm from Wikipedia
* that uses recursion rather than having an explicit stack of operators and values.
*/
private class OperatorParser {
/**
* The operator we have just scanned, in the same way that {@link #c} is the character we have
* just read. If we were not able to scan an operator, this will be {@link Operator#STOP}.
*/
private Operator currentOperator;
OperatorParser() throws IOException {
nextOperator();
}
/**
* Parse a subexpression whose left-hand side is {@code lhs} and where we only consider
* operators with precedence at least {@code minPrecedence}.
*
* @return the parsed subexpression
*/
ExpressionNode parse(ExpressionNode lhs, int minPrecedence) throws IOException {
while (currentOperator.precedence >= minPrecedence) {
Operator operator = currentOperator;
ExpressionNode rhs = parseUnaryExpression();
nextOperator();
while (currentOperator.precedence > operator.precedence) {
rhs = parse(rhs, currentOperator.precedence);
}
lhs = new BinaryExpressionNode(lhs, operator, rhs);
}
return lhs;
}
/**
* Updates {@link #currentOperator} to be an operator read from the input,
* or {@link Operator#STOP} if there is none.
*/
private void nextOperator() throws IOException {
skipSpace();
ImmutableList{@code
*
*/
private ExpressionNode parseUnaryExpression() throws IOException {
skipSpace();
ExpressionNode node;
if (c == '(') {
nextNonSpace();
node = parseExpression();
expect(')');
skipSpace();
return node;
} else if (c == '!') {
next();
node = new NotExpressionNode(parseUnaryExpression());
skipSpace();
return node;
} else {
return parsePrimary();
}
}
/**
* Parses an expression containing only literals or references.
* {@code
*
*/
private ExpressionNode parsePrimary() throws IOException {
ExpressionNode node;
if (c == '$') {
next();
node = parseRequiredReference();
} else if (c == '"') {
node = parseStringLiteral(c, true);
} else if (c == '\'') {
node = parseStringLiteral(c, false);
} else if (c == '-') {
// Velocity does not have a negation operator. If we see '-' it must be the start of a
// negative integer literal.
next();
node = parseIntLiteral("-");
} else if (isAsciiDigit(c)) {
node = parseIntLiteral("");
} else if (isAsciiLetter(c)) {
node = parseBooleanLiteral();
} else {
throw parseException("Expected an expression");
}
skipSpace();
return node;
}
/**
* Parses a string literal, which may contain references to be expanded. Examples are
* {@code "foo"} or {@code "foo${bar}baz"}.
* {@code
*
*/
private ExpressionNode parseStringLiteral(int quote, boolean allowReferences)
throws IOException {
assert c == quote;
next();
ImmutableList.Builder