[antlr-interest] String Templates and return values

Andreas Stefik stefika at gmail.com
Tue Mar 15 14:15:09 PDT 2011


Hello all,

My team is changing over our existing grammars from
Parser->TreeWalker, to adding a new phase where we are outputting text
from String templates. We have made some test grammars, which work
fine, generating some output, but are having some difficulty
integrating templates into our real system. Specifically, we started
out doing the simplest thing, putting output = template; into the
options. After changing around some of our grammar to account for
different return types from outputting templates, we ran into one
grammar rule that we haven't quite figured out how to fix:

^(FUNCTION_CALL qualified_name(COLON ID)? LEFT_PAREN ({temp++;}exprs
+= expression (COMMA exprs += expression)*)? RIGHT_PAREN)

Inside the semantic action for this rule is the following:

if($exprs != null) {
			for(Object o : $exprs) {
                		expression_return ex = (expression_return)o;
                		types.add(ex.eval.getType());
                		argumentTypes.add(ex.eval.getType());
                		steps.add(ex.step);
                		values.add(ex.eval);
                		registers.add(ex.eval.getRegister());
                	}
                }

Basically, we are adding expressions into an ArrayList (exprs +=
expression), which we then crawl through to grab details for the
parameters. However, when we comment out output=template; this code
works just fine, but when we leave it in, the array list is populated
with null values. I don't quite understand what is going on here. For
example, the code generated for the expression return looks about the
same:

//templating on
public static class expression_return extends TreeRuleReturnScope {
        public ExpressionValue eval;
        public ExecutionStep step;
        public StringTemplate st;
        public Object getTemplate() { return st; }
        public String toString() { return st==null?null:st.toString(); }
    };

as opposed to

//templating off
public static class expression_return extends TreeRuleReturnScope {
        public ExpressionValue eval;
        public ExecutionStep step;
    };

I assume that, perhaps, there is some other way should be doing this. Any clues?

Stefik


More information about the antlr-interest mailing list