[antlr-interest] Lexer error handling

A Z asicaddress at gmail.com
Mon May 14 22:44:53 PDT 2012


Hello all,

  The lexer rule below simply matches a quoted string while allowing
escaped characters such as \\n. Any non-escaped control characters are an
error so I'd like to exit the rule when this happens but this doesn't seem
possible unless I use recursive calls to LoopChar instead of *.  I'm
wondering if there is better way to handle this without recursion.

//Quoted string
STR           : '\u0022' LoopChar* '\u0022';

fragment LoopChar :
   '\u0000'..'\u001F'   {ctx->dirLexerError();} //Exit rule here
 | '\u0020'
 | '\u0021'
 | '\u0023'..'\u005B'
 | '\u005C' .
 | '\u005D'..'\u007E'
 | '\u007F'..'\uFFFF'   {ctx->dirLexerError();} //Exit rule here
 ;

Thanks


More information about the antlr-interest mailing list