From 3f1f76df7d44332c637e5a92f27933e9c9f3e5ac Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Thu, 8 Mar 2012 13:36:46 -0600 Subject: [PATCH] Move reportAmbiguity, reportContextSensitivity, reportAttemptingFullContext, reportInsufficientPredicates from ANTLRErrorStrategy to ANTLRErrorListener. Add BaseErrorListener to allow implementing ANTLRErrorListener without implementing every method (e.g. ConsoleErrorListener). DiagnosticErrorStrategy is now DiagnosticErrorListener, updated tests. --- .../antlr/v4/runtime/ANTLRErrorListener.java | 44 +++++++++ .../antlr/v4/runtime/ANTLRErrorStrategy.java | 43 --------- .../antlr/v4/runtime/BaseErrorListener.java | 93 +++++++++++++++++++ .../v4/runtime/ConsoleErrorListener.java | 2 +- .../v4/runtime/DefaultErrorStrategy.java | 33 ------- ...tegy.java => DiagnosticErrorListener.java} | 2 +- .../v4/runtime/atn/ParserATNSimulator.java | 8 +- .../org/antlr/v4/runtime/misc/TestRig.java | 2 +- tool/playground/TestR.java | 4 +- tool/test/org/antlr/v4/test/BaseTest.java | 2 +- .../org/antlr/v4/test/TestPerformance.java | 2 +- 11 files changed, 148 insertions(+), 87 deletions(-) create mode 100644 runtime/Java/src/org/antlr/v4/runtime/BaseErrorListener.java rename runtime/Java/src/org/antlr/v4/runtime/{DiagnosticErrorStrategy.java => DiagnosticErrorListener.java} (98%) diff --git a/runtime/Java/src/org/antlr/v4/runtime/ANTLRErrorListener.java b/runtime/Java/src/org/antlr/v4/runtime/ANTLRErrorListener.java index fed6ba2f8..deb386a72 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/ANTLRErrorListener.java +++ b/runtime/Java/src/org/antlr/v4/runtime/ANTLRErrorListener.java @@ -29,6 +29,12 @@ package org.antlr.v4.runtime; +import org.antlr.v4.runtime.atn.ATNConfigSet; +import org.antlr.v4.runtime.atn.DecisionState; +import org.antlr.v4.runtime.atn.SemanticContext; +import org.antlr.v4.runtime.dfa.DFA; +import org.antlr.v4.runtime.misc.IntervalSet; +import org.antlr.v4.runtime.misc.NotNull; import org.antlr.v4.runtime.misc.Nullable; /** How to emit recognition errors */ @@ -72,4 +78,42 @@ public interface ANTLRErrorListener { int charPositionInLine, String msg, @Nullable RecognitionException e); + + /** Called when the parser detects a true ambiguity: an input sequence can be matched + * literally by two or more pass through the grammar. ANTLR resolves the ambiguity in + * favor of the alternative appearing first in the grammar. The start and stop index are + * zero-based absolute indices into the token stream. ambigAlts is a set of alternative numbers + * that can match the input sequence. This method is only called when we are parsing with + * full context. + */ + void reportAmbiguity(@NotNull Parser recognizer, + DFA dfa, int startIndex, int stopIndex, @NotNull IntervalSet ambigAlts, + @NotNull ATNConfigSet configs); + + void reportAttemptingFullContext(@NotNull Parser recognizer, + @NotNull DFA dfa, + int startIndex, int stopIndex, + @NotNull ATNConfigSet configs); + + /** Called by the parser when it find a conflict that is resolved by retrying the parse + * with full context. This is not a warning; it simply notifies you that your grammar + * is more complicated than Strong LL can handle. The parser moved up to full context + * parsing for that input sequence. + */ + void reportContextSensitivity(@NotNull Parser recognizer, + @NotNull DFA dfa, + int startIndex, int stopIndex, + @NotNull ATNConfigSet configs); + + /** Called by the parser when it finds less than n-1 predicates for n ambiguous alternatives. + * If there are n-1, we assume that the missing predicate is !(the "or" of the other predicates). + * If there are fewer than n-1, then we don't know which make it alternative to protect + * if the predicates fail. + */ + void reportInsufficientPredicates(@NotNull Parser recognizer, + @NotNull DFA dfa, + int startIndex, int stopIndex, @NotNull IntervalSet ambigAlts, + DecisionState decState, + @NotNull SemanticContext[] altToPred, + @NotNull ATNConfigSet configs, boolean fullContextParse); } diff --git a/runtime/Java/src/org/antlr/v4/runtime/ANTLRErrorStrategy.java b/runtime/Java/src/org/antlr/v4/runtime/ANTLRErrorStrategy.java index d3aed46e1..dbf4163af 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/ANTLRErrorStrategy.java +++ b/runtime/Java/src/org/antlr/v4/runtime/ANTLRErrorStrategy.java @@ -1,10 +1,5 @@ package org.antlr.v4.runtime; -import org.antlr.v4.runtime.atn.ATNConfigSet; -import org.antlr.v4.runtime.atn.DecisionState; -import org.antlr.v4.runtime.atn.SemanticContext; -import org.antlr.v4.runtime.dfa.DFA; -import org.antlr.v4.runtime.misc.IntervalSet; import org.antlr.v4.runtime.misc.NotNull; import org.antlr.v4.runtime.misc.Nullable; @@ -114,42 +109,4 @@ public interface ANTLRErrorStrategy { void reportError(@NotNull Parser recognizer, @Nullable RecognitionException e) throws RecognitionException; - - /** Called when the parser detects a true ambiguity: an input sequence can be matched - * literally by two or more pass through the grammar. ANTLR resolves the ambiguity in - * favor of the alternative appearing first in the grammar. The start and stop index are - * zero-based absolute indices into the token stream. ambigAlts is a set of alternative numbers - * that can match the input sequence. This method is only called when we are parsing with - * full context. - */ - void reportAmbiguity(@NotNull Parser recognizer, - DFA dfa, int startIndex, int stopIndex, @NotNull IntervalSet ambigAlts, - @NotNull ATNConfigSet configs); - - void reportAttemptingFullContext(@NotNull Parser recognizer, - @NotNull DFA dfa, - int startIndex, int stopIndex, - @NotNull ATNConfigSet configs); - - /** Called by the parser when it find a conflict that is resolved by retrying the parse - * with full context. This is not a warning; it simply notifies you that your grammar - * is more complicated than Strong LL can handle. The parser moved up to full context - * parsing for that input sequence. - */ - void reportContextSensitivity(@NotNull Parser recognizer, - @NotNull DFA dfa, - int startIndex, int stopIndex, - @NotNull ATNConfigSet configs); - - /** Called by the parser when it finds less than n-1 predicates for n ambiguous alternatives. - * If there are n-1, we assume that the missing predicate is !(the "or" of the other predicates). - * If there are fewer than n-1, then we don't know which make it alternative to protect - * if the predicates fail. - */ - void reportInsufficientPredicates(@NotNull Parser recognizer, - @NotNull DFA dfa, - int startIndex, int stopIndex, @NotNull IntervalSet ambigAlts, - DecisionState decState, - @NotNull SemanticContext[] altToPred, - @NotNull ATNConfigSet configs, boolean fullContextParse); } diff --git a/runtime/Java/src/org/antlr/v4/runtime/BaseErrorListener.java b/runtime/Java/src/org/antlr/v4/runtime/BaseErrorListener.java new file mode 100644 index 000000000..51d7c98dd --- /dev/null +++ b/runtime/Java/src/org/antlr/v4/runtime/BaseErrorListener.java @@ -0,0 +1,93 @@ +/* + [The "BSD license"] + Copyright (c) 2012 Terence Parr + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. The name of the author may not be used to endorse or promote products + derived from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package org.antlr.v4.runtime; + +import org.antlr.v4.runtime.atn.ATNConfigSet; +import org.antlr.v4.runtime.atn.DecisionState; +import org.antlr.v4.runtime.atn.SemanticContext; +import org.antlr.v4.runtime.dfa.DFA; +import org.antlr.v4.runtime.misc.IntervalSet; + +/** + * + * @author Sam Harwell + */ +public class BaseErrorListener implements ANTLRErrorListener { + + @Override + public void error(Recognizer recognizer, + T offendingSymbol, + int line, + int charPositionInLine, + String msg, + RecognitionException e) + { + } + + @Override + public void reportAmbiguity(Parser recognizer, + DFA dfa, + int startIndex, + int stopIndex, + IntervalSet ambigAlts, + ATNConfigSet configs) + { + } + + @Override + public void reportAttemptingFullContext(Parser recognizer, + DFA dfa, + int startIndex, + int stopIndex, + ATNConfigSet configs) + { + } + + @Override + public void reportContextSensitivity(Parser recognizer, + DFA dfa, + int startIndex, + int stopIndex, + ATNConfigSet configs) + { + } + + @Override + public void reportInsufficientPredicates(Parser recognizer, + DFA dfa, + int startIndex, + int stopIndex, + IntervalSet ambigAlts, + DecisionState decState, + SemanticContext[] altToPred, + ATNConfigSet configs, + boolean fullContextParse) + { + } +} diff --git a/runtime/Java/src/org/antlr/v4/runtime/ConsoleErrorListener.java b/runtime/Java/src/org/antlr/v4/runtime/ConsoleErrorListener.java index 2fa89eecd..fd2030e05 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/ConsoleErrorListener.java +++ b/runtime/Java/src/org/antlr/v4/runtime/ConsoleErrorListener.java @@ -32,7 +32,7 @@ package org.antlr.v4.runtime; * * @author Sam Harwell */ -public class ConsoleErrorListener implements ANTLRErrorListener { +public class ConsoleErrorListener extends BaseErrorListener { public static final ConsoleErrorListener INSTANCE = new ConsoleErrorListener(); @Override diff --git a/runtime/Java/src/org/antlr/v4/runtime/DefaultErrorStrategy.java b/runtime/Java/src/org/antlr/v4/runtime/DefaultErrorStrategy.java index f4f7cf87e..c62799480 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/DefaultErrorStrategy.java +++ b/runtime/Java/src/org/antlr/v4/runtime/DefaultErrorStrategy.java @@ -30,7 +30,6 @@ package org.antlr.v4.runtime; import org.antlr.v4.runtime.atn.*; -import org.antlr.v4.runtime.dfa.DFA; import org.antlr.v4.runtime.misc.IntervalSet; import org.antlr.v4.runtime.misc.NotNull; @@ -549,36 +548,4 @@ public class DefaultErrorStrategy implements ANTLRErrorStrategy { ttype = recognizer.getInputStream().LA(1); } } - - @Override - public void reportAmbiguity(@NotNull Parser recognizer, - DFA dfa, int startIndex, int stopIndex, @NotNull IntervalSet ambigAlts, - @NotNull ATNConfigSet configs) - { - } - - @Override - public void reportAttemptingFullContext(@NotNull Parser recognizer, - @NotNull DFA dfa, - int startIndex, int stopIndex, - @NotNull ATNConfigSet configs) - { - } - - @Override - public void reportContextSensitivity(@NotNull Parser recognizer, @NotNull DFA dfa, - int startIndex, int stopIndex, @NotNull ATNConfigSet configs) - { - } - - @Override - public void reportInsufficientPredicates(@NotNull Parser recognizer, - @NotNull DFA dfa, - int startIndex, int stopIndex, - @NotNull IntervalSet ambigAlts, - DecisionState decState, - @NotNull SemanticContext[] altToPred, - @NotNull ATNConfigSet configs, boolean fullContextParse) - { - } } diff --git a/runtime/Java/src/org/antlr/v4/runtime/DiagnosticErrorStrategy.java b/runtime/Java/src/org/antlr/v4/runtime/DiagnosticErrorListener.java similarity index 98% rename from runtime/Java/src/org/antlr/v4/runtime/DiagnosticErrorStrategy.java rename to runtime/Java/src/org/antlr/v4/runtime/DiagnosticErrorListener.java index d05944ca3..1c6f65fd7 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/DiagnosticErrorStrategy.java +++ b/runtime/Java/src/org/antlr/v4/runtime/DiagnosticErrorListener.java @@ -38,7 +38,7 @@ import org.antlr.v4.runtime.misc.NotNull; import java.util.Arrays; -public class DiagnosticErrorStrategy extends DefaultErrorStrategy { +public class DiagnosticErrorListener extends BaseErrorListener { @Override public void reportAmbiguity(@NotNull Parser recognizer, DFA dfa, int startIndex, int stopIndex, @NotNull IntervalSet ambigAlts, diff --git a/runtime/Java/src/org/antlr/v4/runtime/atn/ParserATNSimulator.java b/runtime/Java/src/org/antlr/v4/runtime/atn/ParserATNSimulator.java index 5d3d05489..41cae1f0c 100755 --- a/runtime/Java/src/org/antlr/v4/runtime/atn/ParserATNSimulator.java +++ b/runtime/Java/src/org/antlr/v4/runtime/atn/ParserATNSimulator.java @@ -1370,7 +1370,7 @@ public class ParserATNSimulator extends ATNSimulator { System.out.println("reportAttemptingFullContext decision="+dfa.decision+":"+configs+ ", input="+parser.getInputString(startIndex, stopIndex)); } - if ( parser!=null ) parser.getErrorHandler().reportAttemptingFullContext(parser, dfa, startIndex, stopIndex, configs); + if ( parser!=null ) parser.getErrorListenerDispatch().reportAttemptingFullContext(parser, dfa, startIndex, stopIndex, configs); } public void reportContextSensitivity(DFA dfa, ATNConfigSet configs, int startIndex, int stopIndex) { @@ -1378,7 +1378,7 @@ public class ParserATNSimulator extends ATNSimulator { System.out.println("reportContextSensitivity decision="+dfa.decision+":"+configs+ ", input="+parser.getInputString(startIndex, stopIndex)); } - if ( parser!=null ) parser.getErrorHandler().reportContextSensitivity(parser, dfa, startIndex, stopIndex, configs); + if ( parser!=null ) parser.getErrorListenerDispatch().reportContextSensitivity(parser, dfa, startIndex, stopIndex, configs); } /** If context sensitive parsing, we know it's ambiguity not conflict */ @@ -1407,7 +1407,7 @@ public class ParserATNSimulator extends ATNSimulator { ambigAlts+":"+configs+ ", input="+parser.getInputString(startIndex, stopIndex)); } - if ( parser!=null ) parser.getErrorHandler().reportAmbiguity(parser, dfa, startIndex, stopIndex, + if ( parser!=null ) parser.getErrorListenerDispatch().reportAmbiguity(parser, dfa, startIndex, stopIndex, ambigAlts, configs); } @@ -1424,7 +1424,7 @@ public class ParserATNSimulator extends ATNSimulator { parser.getInputString(startIndex, stopIndex)); } if ( parser!=null ) { - parser.getErrorHandler().reportInsufficientPredicates(parser, dfa, startIndex, stopIndex, ambigAlts, + parser.getErrorListenerDispatch().reportInsufficientPredicates(parser, dfa, startIndex, stopIndex, ambigAlts, decState, altToPred, configs, fullContextParse); } } diff --git a/runtime/Java/src/org/antlr/v4/runtime/misc/TestRig.java b/runtime/Java/src/org/antlr/v4/runtime/misc/TestRig.java index cae2de3e7..424605f10 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/misc/TestRig.java +++ b/runtime/Java/src/org/antlr/v4/runtime/misc/TestRig.java @@ -147,7 +147,7 @@ public class TestRig { Constructor parserCtor = parserClass.getConstructor(TokenStream.class); Parser parser = parserCtor.newInstance(tokens); - parser.setErrorHandler(new DiagnosticErrorStrategy()); + parser.addErrorListener(new DiagnosticErrorListener()); if ( printTree || gui || psFile!=null ) { parser.setBuildParseTree(true); diff --git a/tool/playground/TestR.java b/tool/playground/TestR.java index ff028942c..818a62e22 100644 --- a/tool/playground/TestR.java +++ b/tool/playground/TestR.java @@ -29,7 +29,7 @@ import org.antlr.v4.runtime.ANTLRFileStream; import org.antlr.v4.runtime.CommonTokenStream; -import org.antlr.v4.runtime.DiagnosticErrorStrategy; +import org.antlr.v4.runtime.DiagnosticErrorListener; public class TestR { public static void main(String[] args) throws Exception { @@ -41,7 +41,7 @@ public class TestR { // } RParser p = new RParser(tokens); p.setBuildParseTree(true); - p.setErrorHandler(new DiagnosticErrorStrategy()); + p.addErrorListener(new DiagnosticErrorListener()); p.prog(); } } diff --git a/tool/test/org/antlr/v4/test/BaseTest.java b/tool/test/org/antlr/v4/test/BaseTest.java index 2261b40ea..6975faefe 100644 --- a/tool/test/org/antlr/v4/test/BaseTest.java +++ b/tool/test/org/antlr/v4/test/BaseTest.java @@ -895,7 +895,7 @@ public abstract class BaseTest { createParserST = new ST( " parser = new (tokens);\n" + - " parser.setErrorHandler(new DiagnosticErrorStrategy());\n"); + " parser.addErrorListener(new DiagnosticErrorListener());\n"); } outputFileST.add("createParser", createParserST); outputFileST.add("parserName", parserName); diff --git a/tool/test/org/antlr/v4/test/TestPerformance.java b/tool/test/org/antlr/v4/test/TestPerformance.java index 8b37d2e5e..46c19b3fe 100644 --- a/tool/test/org/antlr/v4/test/TestPerformance.java +++ b/tool/test/org/antlr/v4/test/TestPerformance.java @@ -513,7 +513,7 @@ public class TestPerformance extends BaseTest { void parseFile(CharStream input); } - private static class DescriptiveErrorListener implements ANTLRErrorListener { + private static class DescriptiveErrorListener extends BaseErrorListener { public static DescriptiveErrorListener INSTANCE = new DescriptiveErrorListener(); @Override