Merge pull request #1720 from parrt/fix-1703-part-deux

Fix 1703 part deux
This commit is contained in:
Terence Parr 2017-03-02 14:18:37 -08:00 committed by GitHub
commit 94ac8d4b4c
4 changed files with 14 additions and 32 deletions

View File

@ -548,7 +548,10 @@ public class DefaultErrorStrategy implements ANTLRErrorStrategy {
protected Token getMissingSymbol(Parser recognizer) {
Token currentSymbol = recognizer.getCurrentToken();
IntervalSet expecting = getExpectedTokens(recognizer);
int expectedTokenType = expecting.getMinElement(); // get any element
int expectedTokenType = Token.INVALID_TYPE;
if ( !expecting.isNil() ) {
expectedTokenType = expecting.getMinElement(); // get any element
}
String tokenText;
if ( expectedTokenType== Token.EOF ) tokenText = "<missing EOF>";
else tokenText = "<missing "+recognizer.getVocabulary().getDisplayName(expectedTokenType)+">";

View File

@ -407,7 +407,10 @@ public class ParserInterpreter extends Parser {
if ( e instanceof InputMismatchException ) {
InputMismatchException ime = (InputMismatchException)e;
Token tok = e.getOffendingToken();
int expectedTokenType = ime.getExpectedTokens().getMinElement(); // get any element
int expectedTokenType = Token.INVALID_TYPE;
if ( !ime.getExpectedTokens().isNil() ) {
expectedTokenType = ime.getExpectedTokens().getMinElement(); // get any element
}
Token errToken =
getTokenFactory().create(new Pair<TokenSource, CharStream>(tok.getTokenSource(), tok.getTokenSource().getInputStream()),
expectedTokenType, tok.getText(),

View File

@ -128,16 +128,6 @@ public interface IntSet {
@Override
boolean equals(Object obj);
/**
* Returns the single value contained in the set, if {@link #size} is 1;
* otherwise, result is undefined. Check {@link #isNil()} before using
* this function.
*
* @return the single value contained in the set, if {@link #size} is 1;
* otherwise, result is undefined.
*/
int getSingleElement();
/**
* Returns {@code true} if the set contains the specified element.
*

View File

@ -416,28 +416,15 @@ public class IntervalSet implements IntSet {
return intervals==null || intervals.isEmpty();
}
/** {@inheritDoc} */
@Override
public int getSingleElement() {
if ( intervals!=null && intervals.size()==1 ) {
Interval I = intervals.get(0);
if ( I.a == I.b ) {
return I.a;
}
}
return Token.INVALID_TYPE;
}
/**
* Returns the maximum value contained in the set if not isNil().
* Otherwise, result is undefined.
*
* @return the maximum value contained in the set. If the set is empty,
* result is undefined.
* @return the maximum value contained in the set.
* @throws RuntimeException if set is empty
*/
public int getMaxElement() {
if ( isNil() ) {
return Token.INVALID_TYPE;
throw new RuntimeException("set is empty");
}
Interval last = intervals.get(intervals.size()-1);
return last.b;
@ -445,14 +432,13 @@ public class IntervalSet implements IntSet {
/**
* Returns the minimum value contained in the set if not isNil().
* Otherwise, result is undefined.
*
* @return the minimum value contained in the set. If the set is empty,
* result is undefined.
* @return the minimum value contained in the set.
* @throws RuntimeException if set is empty
*/
public int getMinElement() {
if ( isNil() ) {
return Token.INVALID_TYPE;
throw new RuntimeException("set is empty");
}
return intervals.get(0).a;