[antlr-interest] retrieving the length of a production match

Jim Idle jimi at temporal-wave.com
Tue Aug 21 14:06:42 PDT 2007


Just refer to $start and $stop in a rule or more likely for you
$rulelabel.start and $rulelabel.stop. 

These are the actual tokens, which will give you the offset of their
text start in your input stream, which will give you the length in terms
of the input text as well as the start point.

Also, if you determine that there is a semantic error (say an incorrect
cast or something), then you presumably know the tokens involved and can
use their text offsets to determine the length of the text that
indicates the error?

Jim


> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of Adrian Herscu
> Sent: Tuesday, August 21, 2007 1:53 PM
> To: antlr-interest at antlr.org
> Subject: Re: [antlr-interest] retrieving the length of a production
> match
> 
> Thanks for the suggestion, Kay.
> 
> Kay Roepke wrote:
> >
> > On Aug 21, 2007, at 9:32 PM, Adrian Herscu wrote:
> >
> >> I want to embed my parser in an IDE. One of the required features
is
> >> to provide error markers. The markers framework API requires the
> >> length of the error besides of the line and column.
> >>
> >> I have looked into the AST API and couldn't find nothing to help
> with
> >> this task... Searched the group and google, but still nothing :-(
> >
> > A tree node has the start and stop token index (in the sense that in
> a
> > tree like ^(+ 3 4) the + encompasses everything below). From that
you
> > can get the tokens from the token stream and eventually figure out
> the
> > length of the matched string in the character stream :)
> 
> I couldn't find a method that returns the stop token...
> But having an AST, it seems possible to iterate over its children
using
> getNextSibling() till the last child... then iterate over its children
> till the last child... and so on, till the so called "stop token"...
> awful! Is there something that I am missing?
> 
> Thinking...
> Hmmm, that information is already available during the AST
> construction...
> 
> >
> > HTH,
> > -k



More information about the antlr-interest mailing list