[antlr-interest] Fragments and setText appear to not work at all - not even with code from the book
shmuel siegel
antlr at shmuelhome.mine.nu
Wed Nov 21 03:55:28 PST 2007
Harald Mueller wrote:
> Hi Shmuel -
>
> Output is: foobar
> :-(
> (at least with my antlr-3.0.1 ... did it work for you?)
>
> Regards
> Harald
>
>
No it didn't. The text of outer token "Tok" is being constructed
properly but the intermediate token "f" is created in the outer token
using the boundaries of the text stream. It is not using the token
created by the inner rule. I think that you have found a bug, or at
least a design issue.
In the meantime, you have to get around the problem by using some global
variables. The outer rule can set its own text.
But worse, this won't work either.
grammar x;
@header
{
package generated;
}
@lexer::header
{
package generated;
}
@lexer::members
{
String fooString;
String booString;
}
boo :t=Tok{System.out.println($t.getText());};
Tok: FOO BAR {setText(fooString+booString);};
fragment FOO: 'foo' {fooString=getText()+"123";};
fragment BAR: 'bar' {booString=getText()+"345";};
booString will get the value "foobar345" because the system still thinks
that it is in the Tok rule which begins with "foo".
This looks like something that one of the developers need to look at.
More information about the antlr-interest
mailing list