From 64e0a8734f8bd5d479f9c3ac41bddfd00cd37b2c Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Wed, 4 Apr 2012 09:53:10 -0500 Subject: [PATCH 1/3] Report warnings as well as errors --- tool/test/org/antlr/v4/test/BaseTest.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/tool/test/org/antlr/v4/test/BaseTest.java b/tool/test/org/antlr/v4/test/BaseTest.java index a26d63303..0ee9561f9 100644 --- a/tool/test/org/antlr/v4/test/BaseTest.java +++ b/tool/test/org/antlr/v4/test/BaseTest.java @@ -402,7 +402,7 @@ public abstract class BaseTest { Tool antlr = newTool(optionsA); antlr.addListener(equeue); antlr.processGrammarsOnCommandLine(); - if ( equeue.errors.size()>0 ) { + if ( !equeue.errors.isEmpty() ) { allIsWell = false; System.err.println("antlr reports errors from "+options); for (int i = 0; i < equeue.errors.size(); i++) { @@ -413,6 +413,13 @@ public abstract class BaseTest { System.out.println(grammarStr); System.out.println("###"); } + if ( !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); + } + } } catch (Exception e) { allIsWell = false; From 70825902408948581bca6f10d272a201eb6b33e9 Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Wed, 4 Apr 2012 09:56:32 -0500 Subject: [PATCH 2/3] Allow tests to use the default tool listener for error reporting (prints formatted error messages). Use this in TestPerformance. --- tool/test/org/antlr/v4/test/BaseTest.java | 25 +++++++++++++++---- .../antlr/v4/test/TestCompositeGrammars.java | 2 +- .../org/antlr/v4/test/TestPerformance.java | 2 +- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/tool/test/org/antlr/v4/test/BaseTest.java b/tool/test/org/antlr/v4/test/BaseTest.java index 0ee9561f9..0340c6649 100644 --- a/tool/test/org/antlr/v4/test/BaseTest.java +++ b/tool/test/org/antlr/v4/test/BaseTest.java @@ -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 files = new ArrayList(); if ( lexerName!=null ) { diff --git a/tool/test/org/antlr/v4/test/TestCompositeGrammars.java b/tool/test/org/antlr/v4/test/TestCompositeGrammars.java index ea2be8676..d2245bbc4 100644 --- a/tool/test/org/antlr/v4/test/TestCompositeGrammars.java +++ b/tool/test/org/antlr/v4/test/TestCompositeGrammars.java @@ -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); } diff --git a/tool/test/org/antlr/v4/test/TestPerformance.java b/tool/test/org/antlr/v4/test/TestPerformance.java index 84773cb7a..65bbe9b0d 100644 --- a/tool/test/org/antlr/v4/test/TestPerformance.java +++ b/tool/test/org/antlr/v4/test/TestPerformance.java @@ -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); } From d054a358a4158bde67bfb910d56459f2dd0f34b4 Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Wed, 4 Apr 2012 09:57:38 -0500 Subject: [PATCH 3/3] Don't run compiler if tool reported error(s) --- tool/test/org/antlr/v4/test/BaseTest.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/tool/test/org/antlr/v4/test/BaseTest.java b/tool/test/org/antlr/v4/test/BaseTest.java index 0340c6649..333452ab3 100644 --- a/tool/test/org/antlr/v4/test/BaseTest.java +++ b/tool/test/org/antlr/v4/test/BaseTest.java @@ -503,7 +503,10 @@ public abstract class BaseTest { { boolean allIsWell = antlr(grammarFileName, grammarFileName, grammarStr, defaultListener, extraOptions); - boolean ok; + if (!allIsWell) { + return false; + } + List files = new ArrayList(); if ( lexerName!=null ) { files.add(lexerName+".java"); @@ -521,8 +524,7 @@ public abstract class BaseTest { files.add(grammarFileName.substring(0, grammarFileName.lastIndexOf('.'))+"BaseParseListener.java"); } } - ok = compile(files.toArray(new String[files.size()])); - if ( !ok ) { allIsWell = false; } + allIsWell = compile(files.toArray(new String[files.size()])); return allIsWell; }