[antlr-interest] Tracking Character Position in antlr3
tirelli at post.com
Fri Jan 19 06:43:25 PST 2007
I think this is probably a recurring frequent question, but the
answers I was able to find, both in the FAQ(
http://www.jguru.com/faq/view.jsp?EID=550800 ) and the list archives
seems to refer to antlr 2.x version. So, here we go again:
We have a parser generated by antlr 3. We also have an IDE built
over eclipse. We have a requirement from the IDE guys that the parser
must provide location within the source files as character position from
the beggining of the file, instead of line/columns.
Looking through antlr source code, it seems that CommonToken has
this information encoded as "start" and "stop" indexes and you can get
them through methods: getStartIndex() and getStopIndex(). I developed
some unit tests (hardcoding class casts from Token to CommonToken) and
these attributes are consistent throughout the parsing.
So, my questions:
1) Is there any reason for these methods to not be exposed in the Token
interface? In other words, can they be exposed in the Token interface
implemented by CommonToken? This would avoid the nasty class casts...
2) If not, do you see any reason for me to not do the casts and get the
info from the CommonToken class?
3) Is there any other better alternative way of doing it?
Thank you in advance,
Software Engineer - JBoss Rules Core Developer
Office: +55 11 3124-6000
Mobile: +55 11 9218-4151
JBoss, a division of Red Hat @ www.jboss.com
More information about the antlr-interest