[antlr-interest] ast rewrite
Emond Papegaaij
e.papegaaij at student.utwente.nl
Mon Dec 4 23:57:39 PST 2006
On Monday 04 December 2006 18:48, Terence Parr wrote:
> On Dec 4, 2006, at 6:43 AM, Alexander Gängel wrote:
> > Ok I found it in the wiki:
> >
> > STRING: '\"' CHARS '\"' {setText(getText().substring(1, getText
> > ().length()));} ;
> >
> > but with this I still have the tailing '
> >
> > I thins the wiki is wrong in this point. (I'mnot sure if I just
> > misssomething)
>
> 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."
>
> > I now use this:
> >
> > String :
> > '\'' ~('\'')* '\'' {setText(getText().substring(1, getText
> > ().length()-1));}
> > ;
>
> Does that work? are you not missing the last character before the
> last quote?
It should be getText().length()-1. The indices are zero based. I always use
short strings to find the right index:
str = "'a'", length = 3, charAt(length-1) = ''', substring(1,2) = "a"
Also from the Javadoc: 'Thus the length of the substring is
endIndex-beginIndex.'
Unfortunately, I can't update the wiki, because I don't have a login.
Best regards,
Emond
More information about the antlr-interest
mailing list