I can load a grammar file in to my java project (in netbeans) and generate/build my lexer and parser classes, load them via reflection and then parse what I need to parse.
What I would like is a simple example of redirecting the results from the generate/build of the lexer and parser to a string so that I can put the errors to a popup like JOptionPane.
Just like ANTLRworks does when you do CTRL-R (in windows at least) showing Error(123) grammar.g:100:12: …Warning…etc…
Now I am already using a DiagnosticCollector but this does not always enough details as the CTRL-r in ANTLRworks does. The idea being the user corrects the grammar in order to correct the mistake.
Here’s a snippet of my code
Tool tool = new Tool(new String[]{userDir + grammarPath + grammarName + ".g"});
//tool.setOutputDirectory(userDir + grammarPath );
tool.process();
if(tool.getNumErrors()>0) {
errors=true;
unableToLoadGrammar_AddLine("1. Detected "+tool.getNumErrors()+" Error(s)"+tool.toString()+"\n");
}
Thanks
Call
org.antlr.tool.ErrorManager‘ssetErrorListenermethod:Passing a bad grammar to the code above produces the following output:
Note that
Toolstill operates directly onSystem.errandSystem.out. If you want to handle everything coming out ofTool, you’ll have to callSystem.setErrandSystem.setOutas well.