Search in sources :

Example 1 with EofNode

use of com.google.auto.value.processor.escapevelocity.TokenNode.EofNode in project auto by google.

the class Parser method parse.

/**
   * Parse the input completely to produce a {@link Template}.
   *
   * <p>Parsing happens in two phases. First, we parse a sequence of "tokens", where tokens include
   * entire references such as <pre>
   *    ${x.foo()[23]}
   * </pre>or entire directives such as<pre>
   *    #set ($x = $y + $z)
   * </pre>But tokens do not span complex constructs. For example,<pre>
   *    #if ($x == $y) something #end
   * </pre>is three tokens:<pre>
   *    #if ($x == $y)
   *    (literal text " something ")
   *   #end
   * </pre>
   *
   * <p>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 <pre>
   *    ${x.foo()[23]}
   *    #set ($x = $y + $z)
   * </pre> 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.
   *
   * <p>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 <code>${x.foo()}</code>; 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 <pre>
   *    #if ($x == $a + $b)
   * </pre> 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.Builder<Node> tokens = ImmutableList.builder();
    Node token;
    do {
        token = parseNode();
        tokens.add(token);
    } while (!(token instanceof EofNode));
    return new Reparser(tokens.build()).reparse();
}
Also used : EofNode(com.google.auto.value.processor.escapevelocity.TokenNode.EofNode) ImmutableList(com.google.common.collect.ImmutableList) IndexReferenceNode(com.google.auto.value.processor.escapevelocity.ReferenceNode.IndexReferenceNode) ForEachTokenNode(com.google.auto.value.processor.escapevelocity.TokenNode.ForEachTokenNode) NotExpressionNode(com.google.auto.value.processor.escapevelocity.ExpressionNode.NotExpressionNode) SetNode(com.google.auto.value.processor.escapevelocity.DirectiveNode.SetNode) EofNode(com.google.auto.value.processor.escapevelocity.TokenNode.EofNode) MemberReferenceNode(com.google.auto.value.processor.escapevelocity.ReferenceNode.MemberReferenceNode) CommentTokenNode(com.google.auto.value.processor.escapevelocity.TokenNode.CommentTokenNode) IfTokenNode(com.google.auto.value.processor.escapevelocity.TokenNode.IfTokenNode) ElseTokenNode(com.google.auto.value.processor.escapevelocity.TokenNode.ElseTokenNode) BinaryExpressionNode(com.google.auto.value.processor.escapevelocity.ExpressionNode.BinaryExpressionNode) ElseIfTokenNode(com.google.auto.value.processor.escapevelocity.TokenNode.ElseIfTokenNode) MethodReferenceNode(com.google.auto.value.processor.escapevelocity.ReferenceNode.MethodReferenceNode) EndTokenNode(com.google.auto.value.processor.escapevelocity.TokenNode.EndTokenNode) PlainReferenceNode(com.google.auto.value.processor.escapevelocity.ReferenceNode.PlainReferenceNode) MacroDefinitionTokenNode(com.google.auto.value.processor.escapevelocity.TokenNode.MacroDefinitionTokenNode)

Example 2 with EofNode

use of com.google.auto.value.processor.escapevelocity.TokenNode.EofNode in project auto by google.

the class Reparser method reparse.

Template reparse() {
    Node root = parseTo(EOF_SET, new EofNode(1));
    linkMacroCalls();
    return new Template(root);
}
Also used : EofNode(com.google.auto.value.processor.escapevelocity.TokenNode.EofNode) ForEachNode(com.google.auto.value.processor.escapevelocity.DirectiveNode.ForEachNode) IfNode(com.google.auto.value.processor.escapevelocity.DirectiveNode.IfNode) MacroCallNode(com.google.auto.value.processor.escapevelocity.DirectiveNode.MacroCallNode) ForEachTokenNode(com.google.auto.value.processor.escapevelocity.TokenNode.ForEachTokenNode) ElseIfTokenNode(com.google.auto.value.processor.escapevelocity.TokenNode.ElseIfTokenNode) SetNode(com.google.auto.value.processor.escapevelocity.DirectiveNode.SetNode) Node.emptyNode(com.google.auto.value.processor.escapevelocity.Node.emptyNode) EndTokenNode(com.google.auto.value.processor.escapevelocity.TokenNode.EndTokenNode) EofNode(com.google.auto.value.processor.escapevelocity.TokenNode.EofNode) CommentTokenNode(com.google.auto.value.processor.escapevelocity.TokenNode.CommentTokenNode) IfTokenNode(com.google.auto.value.processor.escapevelocity.TokenNode.IfTokenNode) ElseTokenNode(com.google.auto.value.processor.escapevelocity.TokenNode.ElseTokenNode) IfOrElseIfTokenNode(com.google.auto.value.processor.escapevelocity.TokenNode.IfOrElseIfTokenNode) MacroDefinitionTokenNode(com.google.auto.value.processor.escapevelocity.TokenNode.MacroDefinitionTokenNode)

Example 3 with EofNode

use of com.google.auto.value.processor.escapevelocity.TokenNode.EofNode in project auto by google.

the class Reparser method removeSpaceBeforeSet.

/**
   * Returns a copy of the given list where spaces have been moved where appropriate after {@code
   * #set}. This hack is needed to match what appears to be special treatment in Apache Velocity of
   * spaces before {@code #set} directives. If you have <i>thing</i> <i>whitespace</i> {@code #set},
   * then the whitespace is deleted if the <i>thing</i> is a comment ({@code ##...\n}); a reference
   * ({@code $x} or {@code $x.foo} etc); a macro definition; or another {@code #set}.
   */
private static ImmutableList<Node> removeSpaceBeforeSet(ImmutableList<Node> nodes) {
    assert Iterables.getLast(nodes) instanceof EofNode;
    // Since the last node is EofNode, the i + 1 and i + 2 accesses below are safe.
    ImmutableList.Builder<Node> newNodes = ImmutableList.builder();
    for (int i = 0; i < nodes.size(); i++) {
        Node nodeI = nodes.get(i);
        newNodes.add(nodeI);
        if (shouldDeleteSpaceBetweenThisAndSet(nodeI) && isWhitespaceLiteral(nodes.get(i + 1)) && nodes.get(i + 2) instanceof SetNode) {
            // Skip the space.
            i++;
        }
    }
    return newNodes.build();
}
Also used : SetNode(com.google.auto.value.processor.escapevelocity.DirectiveNode.SetNode) EofNode(com.google.auto.value.processor.escapevelocity.TokenNode.EofNode) ImmutableList(com.google.common.collect.ImmutableList) ForEachNode(com.google.auto.value.processor.escapevelocity.DirectiveNode.ForEachNode) IfNode(com.google.auto.value.processor.escapevelocity.DirectiveNode.IfNode) MacroCallNode(com.google.auto.value.processor.escapevelocity.DirectiveNode.MacroCallNode) ForEachTokenNode(com.google.auto.value.processor.escapevelocity.TokenNode.ForEachTokenNode) ElseIfTokenNode(com.google.auto.value.processor.escapevelocity.TokenNode.ElseIfTokenNode) SetNode(com.google.auto.value.processor.escapevelocity.DirectiveNode.SetNode) Node.emptyNode(com.google.auto.value.processor.escapevelocity.Node.emptyNode) EndTokenNode(com.google.auto.value.processor.escapevelocity.TokenNode.EndTokenNode) EofNode(com.google.auto.value.processor.escapevelocity.TokenNode.EofNode) CommentTokenNode(com.google.auto.value.processor.escapevelocity.TokenNode.CommentTokenNode) IfTokenNode(com.google.auto.value.processor.escapevelocity.TokenNode.IfTokenNode) ElseTokenNode(com.google.auto.value.processor.escapevelocity.TokenNode.ElseTokenNode) IfOrElseIfTokenNode(com.google.auto.value.processor.escapevelocity.TokenNode.IfOrElseIfTokenNode) MacroDefinitionTokenNode(com.google.auto.value.processor.escapevelocity.TokenNode.MacroDefinitionTokenNode)

Example 4 with EofNode

use of com.google.auto.value.processor.escapevelocity.TokenNode.EofNode in project auto by google.

the class Reparser method parseTo.

/**
   * Parse subtrees until one of the token types in {@code stopSet} is encountered.
   * If this is the top level, {@code stopSet} will include {@link EofNode} so parsing will stop
   * when it reaches the end of the input. Otherwise, if an {@code EofNode} is encountered it is an
   * error because we have something like {@code #if} without {@code #end}.
   *
   * @param stopSet the kinds of tokens that will stop the parse. For example, if we are parsing
   *     after an {@code #if}, we will stop at any of {@code #else}, {@code #elseif},
   *     or {@code #end}.
   * @param forWhat the token that triggered this call, for example the {@code #if} whose
   *     {@code #end} etc we are looking for.
   *
   * @return a Node that is the concatenation of the parsed subtrees
   */
private Node parseTo(Set<Class<? extends TokenNode>> stopSet, TokenNode forWhat) {
    ImmutableList.Builder<Node> nodeList = ImmutableList.builder();
    while (true) {
        Node currentNode = currentNode();
        if (stopSet.contains(currentNode.getClass())) {
            break;
        }
        if (currentNode instanceof EofNode) {
            throw new ParseException("Reached end of file while parsing " + forWhat.name(), forWhat.lineNumber);
        }
        Node parsed;
        if (currentNode instanceof TokenNode) {
            parsed = parseTokenNode();
        } else {
            parsed = currentNode;
            nextNode();
        }
        nodeList.add(parsed);
    }
    return Node.cons(forWhat.lineNumber, nodeList.build());
}
Also used : EofNode(com.google.auto.value.processor.escapevelocity.TokenNode.EofNode) ForEachTokenNode(com.google.auto.value.processor.escapevelocity.TokenNode.ForEachTokenNode) ElseIfTokenNode(com.google.auto.value.processor.escapevelocity.TokenNode.ElseIfTokenNode) EndTokenNode(com.google.auto.value.processor.escapevelocity.TokenNode.EndTokenNode) CommentTokenNode(com.google.auto.value.processor.escapevelocity.TokenNode.CommentTokenNode) IfTokenNode(com.google.auto.value.processor.escapevelocity.TokenNode.IfTokenNode) ElseTokenNode(com.google.auto.value.processor.escapevelocity.TokenNode.ElseTokenNode) IfOrElseIfTokenNode(com.google.auto.value.processor.escapevelocity.TokenNode.IfOrElseIfTokenNode) MacroDefinitionTokenNode(com.google.auto.value.processor.escapevelocity.TokenNode.MacroDefinitionTokenNode) ImmutableList(com.google.common.collect.ImmutableList) ForEachNode(com.google.auto.value.processor.escapevelocity.DirectiveNode.ForEachNode) IfNode(com.google.auto.value.processor.escapevelocity.DirectiveNode.IfNode) MacroCallNode(com.google.auto.value.processor.escapevelocity.DirectiveNode.MacroCallNode) ForEachTokenNode(com.google.auto.value.processor.escapevelocity.TokenNode.ForEachTokenNode) ElseIfTokenNode(com.google.auto.value.processor.escapevelocity.TokenNode.ElseIfTokenNode) SetNode(com.google.auto.value.processor.escapevelocity.DirectiveNode.SetNode) Node.emptyNode(com.google.auto.value.processor.escapevelocity.Node.emptyNode) EndTokenNode(com.google.auto.value.processor.escapevelocity.TokenNode.EndTokenNode) EofNode(com.google.auto.value.processor.escapevelocity.TokenNode.EofNode) CommentTokenNode(com.google.auto.value.processor.escapevelocity.TokenNode.CommentTokenNode) IfTokenNode(com.google.auto.value.processor.escapevelocity.TokenNode.IfTokenNode) ElseTokenNode(com.google.auto.value.processor.escapevelocity.TokenNode.ElseTokenNode) IfOrElseIfTokenNode(com.google.auto.value.processor.escapevelocity.TokenNode.IfOrElseIfTokenNode) MacroDefinitionTokenNode(com.google.auto.value.processor.escapevelocity.TokenNode.MacroDefinitionTokenNode)

Aggregations

SetNode (com.google.auto.value.processor.escapevelocity.DirectiveNode.SetNode)4 CommentTokenNode (com.google.auto.value.processor.escapevelocity.TokenNode.CommentTokenNode)4 ElseIfTokenNode (com.google.auto.value.processor.escapevelocity.TokenNode.ElseIfTokenNode)4 ElseTokenNode (com.google.auto.value.processor.escapevelocity.TokenNode.ElseTokenNode)4 EndTokenNode (com.google.auto.value.processor.escapevelocity.TokenNode.EndTokenNode)4 EofNode (com.google.auto.value.processor.escapevelocity.TokenNode.EofNode)4 ForEachTokenNode (com.google.auto.value.processor.escapevelocity.TokenNode.ForEachTokenNode)4 IfTokenNode (com.google.auto.value.processor.escapevelocity.TokenNode.IfTokenNode)4 MacroDefinitionTokenNode (com.google.auto.value.processor.escapevelocity.TokenNode.MacroDefinitionTokenNode)4 ForEachNode (com.google.auto.value.processor.escapevelocity.DirectiveNode.ForEachNode)3 IfNode (com.google.auto.value.processor.escapevelocity.DirectiveNode.IfNode)3 MacroCallNode (com.google.auto.value.processor.escapevelocity.DirectiveNode.MacroCallNode)3 Node.emptyNode (com.google.auto.value.processor.escapevelocity.Node.emptyNode)3 IfOrElseIfTokenNode (com.google.auto.value.processor.escapevelocity.TokenNode.IfOrElseIfTokenNode)3 ImmutableList (com.google.common.collect.ImmutableList)3 BinaryExpressionNode (com.google.auto.value.processor.escapevelocity.ExpressionNode.BinaryExpressionNode)1 NotExpressionNode (com.google.auto.value.processor.escapevelocity.ExpressionNode.NotExpressionNode)1 IndexReferenceNode (com.google.auto.value.processor.escapevelocity.ReferenceNode.IndexReferenceNode)1 MemberReferenceNode (com.google.auto.value.processor.escapevelocity.ReferenceNode.MemberReferenceNode)1 MethodReferenceNode (com.google.auto.value.processor.escapevelocity.ReferenceNode.MethodReferenceNode)1