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

Robert Parks robertjparks at gmail.com
Tue Nov 23 06:46:15 PST 2010


Good trick. Thanks! For c# target this worked well for me:

StringLiteral
@init
{
 int startPos = input.CharPositionInLine+1;
 int startLine = input.Line;
}
: '"' ( ~('"') )* ('"'| { throw new Exception("Unterminated [\"] starting on
line="+ startLine+", position="+ startPos); })
| '\'' ( ~('\'') )* ('\''| { throw new Exception("Unterminated [\'] starting
on line="+ startLine+", position="+ startPos); })
;




On Mon, Nov 22, 2010 at 4:30 PM, Jim Idle <jimi at temporal-wave.com> wrote:

> 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
>
>
> 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