[antlr-interest] Reg Multi-line comments

David-Sarah Hopwood david-sarah at jacaranda.org
Thu Jul 16 12:32:44 PDT 2009


David-Sarah Hopwood wrote:
[...]
> This is a special case of the more general issue of knowing what rule(s)
> you are in when an error occurs. This information is available in the
> "rule invocation stack". Override the following methods in your lexer:
> 
> lexer::members {
>   public String getErrorMessage(RecognitionException e,
>                                 String[] tokenNames) {
>     List stack = getRuleInvocationStack(e, this.getClass().getName());
> 
>     // The top-level token rule is almost always at position 1 in the
>     // stack, after "mTokens".
>     // .substring(1) strips the "m" prepended to lexer rule names.
>     String rule = stack.size() < 2 ? "" :
>       "in " + stack.get(1).toString().substring(1) + ": ";
> 
>     return rule + super.getErrorMessage(e, tokenNames);
>   }
> 
>   public String getTokenErrorDisplay(Token t) {
>     return t.toString();
>   }
> }

Actually the override of getTokenErrorDisplay is not needed and will
result in more cryptic error messages; overriding getErrorMessage as
above should be sufficient.

-- 
David-Sarah Hopwood  ⚥  http://davidsarah.livejournal.com



More information about the antlr-interest mailing list