[antlr-interest] Improving lexer error messages for missing end quotes

Jim Idle jimi at temporal-wave.com
Mon Nov 22 13:30:21 PST 2010


You code your rules for predictable errors, including an empty alt for lack
of closing quote:


LIT
@init
{
  int startCol = getCharPostionInLine();
}

: '"' ~NLCHARS* ('"' | { error(MISSING_QUOTE, startPos, $pos .... etc) }) ;


You should encapsulate all such errors that you can and try and avoid
letting the error handler catch errors (not always possible) and anyway you
should be overriding the default error handler. You can also override the
recovery mechanism when you know you can do better than reporting a missing
character or consuming the unexpected character.

Jim

> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of Robert Parks
> Sent: Monday, November 22, 2010 12:56 PM
> To: antlr-interest at antlr.org
> Subject: [antlr-interest] Improving lexer error messages for missing
> end quotes
> 
> * Sorry if this is a dupe - I posted through google groups first then
> read that you're are not supposed to do that *
> 
> Hi,
> I am negative testing my lexer and finding that it doesn't handle
> missing end quotes on literal strings as well as I would like. It
> throws a MismatchedTokenException saying that it is expecting end quote
> found EOF which is correct but I would like it to say the line/ offset
> of the starting quote. Is there a way to get this information?
> Thanks,
> Rob
> 
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-
> email-address



More information about the antlr-interest mailing list