Add ProxyErrorListener to allow dispatching error reporting to multiple listeners without manually iterating over the list of listeners.

This commit is contained in:
Sam Harwell 2012-03-08 13:24:08 -06:00
parent dce72dcbfb
commit 768bfc0cf2
4 changed files with 125 additions and 8 deletions

View File

@ -309,10 +309,9 @@ public abstract class Lexer extends Recognizer<Integer, LexerATNSimulator>
public void notifyListeners(LexerNoViableAltException e) {
String msg = "token recognition error at: '"+
_input.substring(_tokenStartCharIndex, _input.index())+"'";
List<? extends ANTLRErrorListener<? super Integer>> listeners = getErrorListeners();
for (ANTLRErrorListener<? super Integer> listener : listeners) {
listener.error(this, null, _tokenStartLine, _tokenStartCharPositionInLine, msg, e);
}
ANTLRErrorListener<? super Integer> listener = getErrorListenerDispatch();
listener.error(this, null, _tokenStartLine, _tokenStartCharPositionInLine, msg, e);
}
public String getCharErrorDisplay(int c) {

View File

@ -280,10 +280,9 @@ public abstract class Parser extends Recognizer<Token, ParserATNSimulator<Token>
line = ((Token) offendingToken).getLine();
charPositionInLine = ((Token) offendingToken).getCharPositionInLine();
}
List<? extends ANTLRErrorListener<? super Token>> listeners = getErrorListeners();
for (ANTLRErrorListener<? super Token> listener : listeners) {
listener.error(this, offendingToken, line, charPositionInLine, msg, e);
}
ANTLRErrorListener<? super Token> listener = getErrorListenerDispatch();
listener.error(this, offendingToken, line, charPositionInLine, msg, e);
}
/** Consume the current symbol and return it. E.g., given the following

View File

@ -0,0 +1,115 @@
/*
[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 java.util.Collection;
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 ProxyErrorListener<Symbol> implements ANTLRErrorListener<Symbol> {
private final Collection<? extends ANTLRErrorListener<? super Symbol>> delegates;
public ProxyErrorListener(Collection<? extends ANTLRErrorListener<? super Symbol>> delegates) {
this.delegates = delegates;
}
@Override
public <T extends Symbol> void error(Recognizer<T, ?> recognizer,
T offendingSymbol,
int line,
int charPositionInLine,
String msg,
RecognitionException e)
{
for (ANTLRErrorListener<? super Symbol> listener : delegates) {
listener.error(recognizer, offendingSymbol, line, charPositionInLine, msg, e);
}
}
@Override
public void reportAmbiguity(Parser recognizer,
DFA dfa,
int startIndex,
int stopIndex,
IntervalSet ambigAlts,
ATNConfigSet configs)
{
for (ANTLRErrorListener<? super Symbol> listener : delegates) {
listener.reportAmbiguity(recognizer, dfa, startIndex, stopIndex, ambigAlts, configs);
}
}
@Override
public void reportAttemptingFullContext(Parser recognizer,
DFA dfa,
int startIndex,
int stopIndex,
ATNConfigSet configs)
{
for (ANTLRErrorListener<? super Symbol> listener : delegates) {
listener.reportAttemptingFullContext(recognizer, dfa, startIndex, stopIndex, configs);
}
}
@Override
public void reportContextSensitivity(Parser recognizer,
DFA dfa,
int startIndex,
int stopIndex,
ATNConfigSet configs)
{
for (ANTLRErrorListener<? super Symbol> listener : delegates) {
listener.reportContextSensitivity(recognizer, dfa, startIndex, stopIndex, configs);
}
}
@Override
public void reportInsufficientPredicates(Parser recognizer,
DFA dfa,
int startIndex,
int stopIndex,
IntervalSet ambigAlts,
DecisionState decState,
SemanticContext[] altToPred,
ATNConfigSet configs,
boolean fullContextParse)
{
for (ANTLRErrorListener<? super Symbol> listener : delegates) {
listener.reportInsufficientPredicates(recognizer, dfa, startIndex, stopIndex, ambigAlts, decState, altToPred, configs, fullContextParse);
}
}
}

View File

@ -120,6 +120,10 @@ public abstract class Recognizer<Symbol, ATNInterpreter extends ATNSimulator> {
return new ArrayList<ANTLRErrorListener<? super Symbol>>(_listeners);
}
public ANTLRErrorListener<? super Symbol> getErrorListenerDispatch() {
return new ProxyErrorListener<Symbol>(getErrorListeners());
}
// subclass needs to override these if there are sempreds or actions
// that the ATN interp needs to execute
public boolean sempred(@Nullable RuleContext _localctx, int ruleIndex, int actionIndex) {