[antlr-interest] ast rewrite
Gavin Lambert
antlr at mirality.co.nz
Mon Dec 4 12:09:06 PST 2006
At 08:33 5/12/2006, Terence Parr wrote:
>
>Hi Alexander. I think it's okay, because the Java API for
String
>says "The substring begins at the specified beginIndex and
extends
>to the character at index endIndex - 1."
Yeah, but if you pass getText().length() as endIndex then
endIndex-1 is the trailing single quote. So the OP is quite
correct.
I prefer doing something that avoids copying the text though; more
like this:
String : '\'' str=UnquotedString '\'' { emit($str);
$setType(String); };
fragment UnquotedString: ~('\'')*;
The subrule outputs a token (which simply indexes into the input
stream rather than copying the text), which the parent rule then
uses as its own output. Note that I'm not 100% sure of the syntax
you have to use in that first rule, you might have to experiment a
bit.
More information about the antlr-interest
mailing list