org.antlr.runtime.FailedPredicateException Java Examples

The following examples show how to use org.antlr.runtime.FailedPredicateException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Lexer.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public Token nextToken() {
	while (true) {
		state.token = null;
		state.channel = Token.DEFAULT_CHANNEL;
		state.tokenStartCharIndex = input.index();
		state.tokenStartCharPositionInLine = input.getCharPositionInLine();
		state.tokenStartLine = input.getLine();
		state.text = null;
		if ( input.LA(1)==CharStream.EOF ) {
			return Token.EOF_TOKEN;
		}
		try {
			mTokens();
			if ( state.token==null ) {
				emit();
			}
			else if ( state.token==Token.SKIP_TOKEN ) {
				continue;
			}
			return state.token;
		}
		/*
		 * Avoid infinite loop (editor freeze) on {@link FailedPredicateException}
		 */
		catch (NoViableAltException | FailedPredicateException e) {
			reportError(e);
			recover(e); // throw out current char and try again
		}
		catch (RecognitionException re) {
			reportError(re);
			// match() routine has already called recover()
		}
	}
}
 
Example #2
Source File: Lexer.java    From gef with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public Token nextToken() {
	while (true) {
		this.state.token = null;
		this.state.channel = Token.DEFAULT_CHANNEL;
		this.state.tokenStartCharIndex = input.index();
		this.state.tokenStartCharPositionInLine = input
				.getCharPositionInLine();
		this.state.tokenStartLine = input.getLine();
		this.state.text = null;
		if (input.LA(1) == CharStream.EOF) {
			return Token.EOF_TOKEN;
		}
		try {
			mTokens();
			if (this.state.token == null) {
				emit();
			} else if (this.state.token == Token.SKIP_TOKEN) {
				continue;
			}
			return this.state.token;
		} catch (RecognitionException re) {
			reportError(re);
			if (re instanceof NoViableAltException
					|| re instanceof FailedPredicateException) {
				recover(re);
			}
			// create token that holds mismatched char
			Token t = new CommonToken(input, Token.INVALID_TOKEN_TYPE,
					Token.HIDDEN_CHANNEL, this.state.tokenStartCharIndex,
					getCharIndex() - 1);
			t.setLine(this.state.tokenStartLine);
			t.setCharPositionInLine(
					this.state.tokenStartCharPositionInLine);
			tokenErrorMap.put(t, getErrorMessage(re, this.getTokenNames()));
			emit(t);
			return this.state.token;
		}
	}
}
 
Example #3
Source File: Lexer.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public Token nextToken() {
	while (true) {
		this.state.token = null;
		this.state.channel = Token.DEFAULT_CHANNEL;
		this.state.tokenStartCharIndex = input.index();
		this.state.tokenStartCharPositionInLine = input.getCharPositionInLine();
		this.state.tokenStartLine = input.getLine();
		this.state.text = null;
		if (input.LA(1) == CharStream.EOF) {
			return Token.EOF_TOKEN;
		}
		try {
			mTokens();
			if (this.state.token == null) {
				emit();
			}
			else if (this.state.token == Token.SKIP_TOKEN) {
				continue;
			}
			return this.state.token;
		}
		catch (RecognitionException re) {
			reportError(re);
			if (re instanceof NoViableAltException ||
				re instanceof FailedPredicateException) {
				recover(re);
			}
			// create token that holds mismatched char
			Token t = new CommonToken(input, Token.INVALID_TOKEN_TYPE, Token.HIDDEN_CHANNEL,
					this.state.tokenStartCharIndex, getCharIndex() - 1);
			t.setLine(this.state.tokenStartLine);
			t.setCharPositionInLine(this.state.tokenStartCharPositionInLine);
			tokenErrorMap.put(t, getErrorMessage(re, this.getTokenNames()));
			emit(t);
			return this.state.token;
		}
	}
}
 
Example #4
Source File: BaseInternalContentAssistParser.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void recover(IntStream stream, RecognitionException ex) {
	if (recoveryListener != null)
		recoveryListener.beginErrorRecovery();
	removeUnexpectedElements();
	if (ex instanceof FailedPredicateException && ex.token.getType() == Token.EOF) {
		failedPredicateAtEOF = true;
	}
	super.recover(stream, ex);
	if (recoveryListener != null)
		recoveryListener.endErrorRecovery();
}
 
Example #5
Source File: Lexer.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public Token nextToken() {
	while (true) {
		state.token = null;
		state.channel = Token.DEFAULT_CHANNEL;
		state.tokenStartCharIndex = input.index();
		state.tokenStartCharPositionInLine = input.getCharPositionInLine();
		state.tokenStartLine = input.getLine();
		state.text = null;
		if ( input.LA(1)==CharStream.EOF ) {
			return Token.EOF_TOKEN;
		}
		try {
			mTokens();
			if ( state.token==null ) {
				emit();
			}
			else if ( state.token==Token.SKIP_TOKEN ) {
				continue;
			}
			return state.token;
		}
		/*
		 * Avoid infinite loop (editor freeze) on {@link FailedPredicateException}
		 */
		catch (NoViableAltException | FailedPredicateException e) {
			reportError(e);
			recover(e); // throw out current char and try again
		}
		catch (RecognitionException re) {
			reportError(re);
			// match() routine has already called recover()
		}
	}
}
 
Example #6
Source File: RecognizerErrorHandler.java    From legstar-core2 with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Simplify error message text for end users.
 * @param e exception that occurred
 * @param msg as formatted by ANTLR
 * @return a more readable error message
 */
public static String makeUserMsg(final RecognitionException e, final String msg) {
    if (e instanceof NoViableAltException) {
        return msg.replace("no viable alternative at", "unrecognized");
    } else if (e instanceof UnwantedTokenException) {
        return msg.replace("extraneous input", "unexpected token");
    } else if (e instanceof MismatchedTokenException) {
        if (msg.contains("mismatched input '<EOF>'")) {
            return msg.replace("mismatched input '<EOF>' expecting", "reached end of file looking for");
        } else {
            return msg.replace("mismatched input", "unexpected token");
        }
    } else if (e instanceof EarlyExitException) {
        return msg.replace("required (...)+ loop did not match anything", "required tokens not found");
    } else if (e instanceof FailedPredicateException) {
        if (msg.contains("picture_string failed predicate: {Unbalanced parentheses}")) {
            return "Unbalanced parentheses in picture string";
        }
        if (msg.contains("PICTURE_PART failed predicate: {Contains invalid picture symbols}")) {
            return "Picture string contains invalid symbols";
        }
        if (msg.contains("PICTURE_PART failed predicate: {Syntax error in last picture clause}")) {
            return "Syntax error in last picture clause";
        }
        if (msg.contains("DATA_NAME failed predicate: {Syntax error in last clause}")) {
            return "Syntax error in last COBOL clause";
        }
    }
    return msg;
}
 
Example #7
Source File: AbstractInternalAntlrParser.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
protected UnorderedGroupErrorContext(FailedPredicateException exception) {
	super(exception);
}
 
Example #8
Source File: AbstractInternalAntlrParser.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public FailedPredicateException getRecognitionException() {
	return (FailedPredicateException) super.getRecognitionException();
}
 
Example #9
Source File: AbstractInternalAntlrParser.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
protected IParserErrorContext createErrorContext(RecognitionException e) {
	if (e instanceof FailedPredicateException)
		return new UnorderedGroupErrorContext((FailedPredicateException) e);
	return new ParserErrorContext(e);
}
 
Example #10
Source File: ISyntaxErrorMessageProvider.java    From xtext-core with Eclipse Public License 2.0 2 votes vote down vote up
/**
 * Returns the failed predicate exception caused by a specific unordered group.
 * @return the failed predicate exception caused by a specific unordered group. Never <code>null</code>.
 */
@Override
FailedPredicateException getRecognitionException();