<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div><div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; min-height: 14px; ">Hy,</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; min-height: 14px; "><br></div> </div><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">I'm writing an eclipse plugin so I need the errors. <div><br></div><div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; "><span style="color: #7f0055">public</span> <span style="color: #7f0055">void</span> displayRecognitionError(String[] tokenNames,</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; "><span class="Apple-tab-span" style="white-space:pre">                        </span>RecognitionException e){ <span class="Apple-style-span" style="white-space: pre; ">                </span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; color: rgb(0, 0, 192); "><span style="color: #000000"><span class="Apple-tab-span" style="white-space:pre">                </span></span><span style="color: #7f0055">if</span><span style="color: #000000">(e</span>rrorHandler<span style="color: #000000">!=</span><span style="color: #7f0055">null</span><span style="color: #000000">){</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; "><span class="Apple-tab-span" style="white-space:pre">                        </span><span style="color: #7f0055">this</span>.register(e<span style="color: #0000c0">rrorHandler</span>, e, <span style="color: #7f0055">this</span>.getErrorMessage(e, <span style="color: #7f0055">this</span>.getTokenNames()));</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; color: rgb(42, 0, 255); "><span class="Apple-style-span" style="color: rgb(0, 0, 0); "><span class="Apple-tab-span" style="white-space:pre">                </span>} <span style="color: #7f0055">else</span>{</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; "><span class="Apple-tab-span" style="white-space:pre">                        </span>String hdr = getErrorHeader(e);</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; "><span class="Apple-tab-span" style="white-space:pre">                        </span>String msg = getErrorMessage(e, tokenNames);</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; "><span class="Apple-tab-span" style="white-space:pre">                        </span>emitErrorMessage(hdr+<span style="color: #2a00ff">" "</span>+msg);</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; min-height: 15px; "><br></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; "><span class="Apple-tab-span" style="white-space:pre">                </span>}</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; "><span class="Apple-tab-span" style="white-space:pre">        </span>}</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; "><br></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; ">I've overriden the displayRecognitionError in both the Parser and Lexer classes. My handler does all the work, I register the exception that has all the information on the error and then process it. </div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; "><br></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; ">miguel</div></div></div><blockquote type="cite"><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div></div><div><br><div><div>On 2008/07/01, at 18:02, Andy Tripp wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div>Jim,<br><br>As you point out, the default case is that people will want to process their own error messages,<br>rather than have ANTLR send them to stderr. So doesn't it make more sense to<br>have ANTLR package them into a data structure (which has a toString() method<br>which ANTLR calls and sends to stderr? The alternative is that people<br>have to either parse the error text or look at the ANTLR-generated code to<br>understand how to override the default behavior.<br><br>You mention reusing your error handling mechanism across "virtually all" your<br>grammars. I think that for almost ANTLR users, the number of lexer/parsers that they're<br>going to write is exactly 1. Better to make it as easy as possible to write that<br>first grammar and not assume that they're going to be creating more grammars<br>anyway. Part of making it easy is to make it possible to build a lexer/parser<br>as a "black box", without having to ever look at the ANTLR-generated code.<br><br>Andy<br><br><br>Jim Idle wrote:<br><blockquote type="cite">On Tue, 2008-07-01 at 08:54 +0200, Raphael Reitzig wrote:<br></blockquote><blockquote type="cite"><blockquote type="cite">Hi!<br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite">I second that for I am about to write something quite similar. System.err<br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite">is no good in a user oriented GUI application.<br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite">I can think of two possibilties to integrate such behaviour in ANTLR:<br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite">* grammar option like "warnMode", i.e. with values "console" and "collect".<br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite">I'd like to have _one_ exceptions thrown if there ocurred any error while<br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite">parsing.<br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite">* possibility to set output stream for error messages via grammar option:<br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite">@errors { System.err } (default)<br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite">Implementation of either should be no obstacle (*guess*).<br></blockquote></blockquote><blockquote type="cite">In the case of lexers, it is best to build a lexer that almost cannot throw errors as once you lex incorrectly then there isn't much you can do. Having rules in the lexer that catch known common mistakes and/or catch any character that makes no sense in your lexer means that your whole solution will be more robust. For most lexers,. just having:<br></blockquote><blockquote type="cite">BADCHAR: . {insert your error code};<br></blockquote><blockquote type="cite">As the last rule will improve things.<br></blockquote><blockquote type="cite">However, in the case of lexer, parser and tree parser it is trivial to override the error output method and add your errors to collections/a collection. As the standard error messages are usually of no use to a real application (and they cannot be, there are too many things you might wish to do on error), then you will almost certainly want to implement your own error output anyway. Just add the message to a collection. I do this with virtually every recognizer I write and it takes less time than learning some new syntax and access methods for ANTLR (which everyone will then complain about because they don't do exactly what they had in mind. ;-)<br></blockquote><blockquote type="cite">So, the method that is called has all the information that you could need, but YOU have to make it in to a collection, format it in a way that makes sense for your application, and present the errors to your users. There is no generic solution that would provide much more than a different set of questions than there is right now. Sure, the errors could all be collected as objects that you then iterate, but then there is more code for people to rip out when they don't want that!<br></blockquote><blockquote type="cite">Come on guys the error messages are an afternoons coding that you can probably reuse on related projects (if they are living in the same environment.) I last did this in C# and if it took an hour to get it all together I would be surprised. You only need to learn the ANTLR bit once.<br></blockquote><blockquote type="cite">Jim<br></blockquote><br></div></blockquote></div><br></div></div></blockquote></div><br></body></html>