use of org.antlr.v4.tool.DefaultToolListener in project antlr4 by tunnelvisionlabs.
the class BaseTest method antlr.
protected ErrorQueue antlr(String grammarFileName, boolean defaultListener, String... extraOptions) {
final List<String> options = new ArrayList<String>();
Collections.addAll(options, extraOptions);
if (!options.contains("-o")) {
options.add("-o");
options.add(tmpdir);
}
if (!options.contains("-lib")) {
options.add("-lib");
options.add(tmpdir);
}
if (!options.contains("-encoding")) {
options.add("-encoding");
options.add("UTF-8");
}
options.add(new File(tmpdir, grammarFileName).toString());
final String[] optionsA = new String[options.size()];
options.toArray(optionsA);
Tool antlr = newTool(optionsA);
ErrorQueue equeue = new ErrorQueue(antlr);
antlr.addListener(equeue);
if (defaultListener) {
antlr.addListener(new DefaultToolListener(antlr));
}
antlr.processGrammarsOnCommandLine();
if (!defaultListener && !equeue.errors.isEmpty()) {
System.err.println("antlr reports errors from " + options);
for (int i = 0; i < equeue.errors.size(); i++) {
ANTLRMessage msg = equeue.errors.get(i);
System.err.println(msg);
}
System.out.println("!!!\ngrammar:");
try {
System.out.println(new String(Utils.readFile(tmpdir + "/" + grammarFileName)));
} catch (IOException ioe) {
System.err.println(ioe.toString());
}
System.out.println("###");
}
if (!defaultListener && !equeue.warnings.isEmpty()) {
System.err.println("antlr reports warnings from " + options);
for (int i = 0; i < equeue.warnings.size(); i++) {
ANTLRMessage msg = equeue.warnings.get(i);
System.err.println(msg);
}
}
if (!defaultListener && !equeue.warnings.isEmpty()) {
System.err.println("antlr reports warnings from " + options);
for (int i = 0; i < equeue.warnings.size(); i++) {
ANTLRMessage msg = equeue.warnings.get(i);
System.err.println(msg);
}
}
return equeue;
}
use of org.antlr.v4.tool.DefaultToolListener in project antlr4 by antlr.
the class BaseRuntimeTest method antlrOnString.
/**
* Run ANTLR on stuff in workdir and error queue back
*/
public static ErrorQueue antlrOnString(String workdir, String targetName, String grammarFileName, boolean defaultListener, String... extraOptions) {
final List<String> options = new ArrayList<>();
Collections.addAll(options, extraOptions);
if (targetName != null) {
options.add("-Dlanguage=" + targetName);
}
if (!options.contains("-o")) {
options.add("-o");
options.add(workdir);
}
if (!options.contains("-lib")) {
options.add("-lib");
options.add(workdir);
}
if (!options.contains("-encoding")) {
options.add("-encoding");
options.add("UTF-8");
}
options.add(new File(workdir, grammarFileName).toString());
final String[] optionsA = new String[options.size()];
options.toArray(optionsA);
Tool antlr = new Tool(optionsA);
ErrorQueue equeue = new ErrorQueue(antlr);
antlr.addListener(equeue);
if (defaultListener) {
antlr.addListener(new DefaultToolListener(antlr));
}
synchronized (antlrLock) {
antlr.processGrammarsOnCommandLine();
}
List<String> errors = new ArrayList<>();
if (!defaultListener && !equeue.errors.isEmpty()) {
for (int i = 0; i < equeue.errors.size(); i++) {
ANTLRMessage msg = equeue.errors.get(i);
ST msgST = antlr.errMgr.getMessageTemplate(msg);
errors.add(msgST.render());
}
}
if (!defaultListener && !equeue.warnings.isEmpty()) {
for (int i = 0; i < equeue.warnings.size(); i++) {
ANTLRMessage msg = equeue.warnings.get(i);
// antlrToolErrors.append(msg); warnings are hushed
}
}
return equeue;
}
Aggregations