You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Antonio García Domínguez <ny...@gmail.com> on 2013/01/24 23:19:49 UTC

Problems with #foreach when using the ANTLR parser directly

Hello everyone,

One of my students and I are working on a SWT component that provides a
simple text field for editing Velocity templates with syntax highlighting
and validation. We've had some success in using the Visitor pattern with
the ANTLR parser provided by Velocity, like this:

public class Visitor implements ParserVisitor {
    public StyledText textdraw;
    public int endL = 1;

    public void parse(String text, StyledText t,int num) throws
ParseException {
        textdraw=t;
        endL=num;
        Parser p = new Parser(new RuntimeInstance());

        try{
            SimpleNode node = p.parse(new StringReader(text), "prueba");
            node.jjtAccept(this, null);
        } catch(Exception e) {
               System.out.print("Error:\n" + e.getMessage());
        }
    }
    // ...
}

This works fine for most scripts, but we're running into problems with
#foreach. If we understood it correctly, it's a pluggable macro and not
part of the grammar, so it needs some additional configuration. We thought
that the above code was enough, but we can't parse the following program
properly:


#foreach( $l in $lo )

#end

The error is:

Encountered "#end\n" at line 3, column 1 of try
Was expecting one of:
    <EOF>
    "(" ...
    <RPAREN> ...
    <ESCAPE_DIRECTIVE> ...
    <SET_DIRECTIVE> ...
    "##" ...
    "\\\\" ...
    "\\" ...
    <TEXT> ...
    "*#" ...
    "*#" ...
    <STRING_LITERAL> ...
    <NEWLINE> ...
    <IF_DIRECTIVE> ...
    <STOP_DIRECTIVE> ...
    <INTEGER_LITERAL> ...
    <FLOATING_POINT_LITERAL> ...
    <WORD> ...
    <BRACKETED_WORD> ...
    <IDENTIFIER> ...
    <DOT> ...
    "{" ...
    "}" ...


It seems that we need to initialize it with RuntimeServices, but still does
not work.

We'd appreciate it if someone could point us in the right direction. We
must be missing something quite simple here :-).

Thanks in advance,
Antonio