[antlr-interest] unget a character

Paul J. Lucas pauljlucas at mac.com
Wed Dec 22 11:52:22 PST 2004


On Wed, 22 Dec 2004, Terence Parr wrote:

> You don't so much unget as "rewind", which is more sophisticated.  From 
> TokenStreamSelector:
> 
>      public void retry() throws TokenStreamRetryException {
>          throw new TokenStreamRetryException();
>      }

	I tried that, but the character had already been consumed and
	the now-current lexer didn't see it.

> Alternatively, you can just use rewind yourself.  The following grammar

	[ snip ]

> has the interesting tidbit:
> 
>                      rewind(_m);
>                      resetText();
>                      try {mSkip(false);}
> 
> upon RecognitionException in a token rule where _m is set in the 
> nextToken() rule.
> 
>          int _m;
>          _m = mark();
> 
> I suggest something like this.

	It's not clear where/when to set call mark() in my case.  In my
	case, the token in the 'B' lexer is:

		RBRACE
			: '}'
			;

	If 'B' ever sees '}', it's wrong.  I need to pop 'B' switching
	back to 'A' then have 'A' see the '}'.

	So where do I call mark() and rewind() ?

	- Paul



More information about the antlr-interest mailing list