[antlr-interest] C target: behaviour of getCharPositionInLine()

Wincent Colaiuta win at wincent.com
Wed Jun 13 04:40:23 PDT 2007


I'm using the C target and some of my lexer rules look like this:

   H6 : { char_position_in_line(ctx) == 0 }?=> '======' ;

That is, the token will only match if it appears in the first column.

In testing I noticed that if such a token appeared at the very  
beginning of the input then its char position would be -1 rather than  
0. On inspecting antlr3inputstream.c I saw that this was because in  
antlr3InputReset() the charPositionInLine is explicitly set to -1.

This is no big deal, I can handle this in my helper method:

   ANTLR3_UINT32 ANTLR3_INLINE char_position_in_line(pWikiTextLexer ctx)
   {
       ANTLR3_UINT32 pos = ctx->pLexer->getCharPositionInLine(ctx- 
 >pLexer);
       return pos == -1 ? 0 : pos;
   }

But I am still curious to know, why is charPositionInLine being set  
to -1 rather than 0? The Java target seems to set to 0 when resetting  
(see the reset() method in runtime/Java/src/org/antlr/runtime/ 
ANTLRStringStream.java).

Cheers,
Wincent



More information about the antlr-interest mailing list