Allow tests to use the default tool listener for error reporting (prints formatted error messages). Use this in TestPerformance.

This commit is contained in:
Sam Harwell 2012-04-04 09:56:32 -05:00
parent 64e0a8734f
commit 7082590240
3 changed files with 22 additions and 7 deletions

View File

@ -54,6 +54,7 @@ import org.antlr.v4.runtime.misc.Interval;
import org.antlr.v4.runtime.misc.Nullable;
import org.antlr.v4.semantics.SemanticPipeline;
import org.antlr.v4.tool.ANTLRMessage;
import org.antlr.v4.tool.DefaultToolListener;
import org.antlr.v4.tool.DOTGenerator;
import org.antlr.v4.tool.Grammar;
import org.antlr.v4.tool.GrammarSemanticsMessage;
@ -383,7 +384,7 @@ public abstract class BaseTest {
/** Return true if all is ok, no errors */
protected boolean antlr(String fileName, String grammarFileName, String grammarStr, String... extraOptions) {
protected boolean antlr(String fileName, String grammarFileName, String grammarStr, boolean defaultListener, String... extraOptions) {
boolean allIsWell = true;
System.out.println("dir "+tmpdir);
mkdir(tmpdir);
@ -401,9 +402,12 @@ public abstract class BaseTest {
ErrorQueue equeue = new ErrorQueue();
Tool antlr = newTool(optionsA);
antlr.addListener(equeue);
if (defaultListener) {
antlr.addListener(new DefaultToolListener(antlr));
}
antlr.processGrammarsOnCommandLine();
if ( !equeue.errors.isEmpty() ) {
allIsWell = false;
allIsWell = equeue.errors.isEmpty();
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);
@ -413,7 +417,7 @@ public abstract class BaseTest {
System.out.println(grammarStr);
System.out.println("###");
}
if ( !equeue.warnings.isEmpty() ) {
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);
@ -485,9 +489,20 @@ public abstract class BaseTest {
@Nullable String parserName,
String lexerName,
String... extraOptions)
{
return rawGenerateAndBuildRecognizer(grammarFileName, grammarStr, parserName, lexerName, false, extraOptions);
}
/** Return true if all is well */
protected boolean rawGenerateAndBuildRecognizer(String grammarFileName,
String grammarStr,
@Nullable String parserName,
String lexerName,
boolean defaultListener,
String... extraOptions)
{
boolean allIsWell =
antlr(grammarFileName, grammarFileName, grammarStr, extraOptions);
antlr(grammarFileName, grammarFileName, grammarStr, defaultListener, extraOptions);
boolean ok;
List<String> files = new ArrayList<String>();
if ( lexerName!=null ) {

View File

@ -653,7 +653,7 @@ public class TestCompositeGrammars extends BaseTest {
"s : a ;\n" +
"B : 'b' ;" + // defines B from inherited token space
"WS : (' '|'\\n') {skip();} ;\n" ;
boolean ok = antlr("M.g4", "M.g4", master);
boolean ok = antlr("M.g4", "M.g4", master, false);
boolean expecting = true; // should be ok
assertEquals(expecting, ok);
}

View File

@ -433,7 +433,7 @@ public class TestPerformance extends BaseTest {
extraOptions.add("-atn");
}
String[] extraOptionsArray = extraOptions.toArray(new String[extraOptions.size()]);
boolean success = rawGenerateAndBuildRecognizer(grammarFileName, body, "JavaParser", "JavaLexer", extraOptionsArray);
boolean success = rawGenerateAndBuildRecognizer(grammarFileName, body, "JavaParser", "JavaLexer", true, extraOptionsArray);
assertTrue(success);
}