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
List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe:
http://www.antlr.org/mailman/options/antlr-interest/your-email-address
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"il-antlr-interest" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/il-antlr-interest?hl=en
-~----------~----~----~----~------~----~------~--~---