本文整理汇总了Java中org.antlr.v4.runtime.InputMismatchException类的典型用法代码示例。如果您正苦于以下问题:Java InputMismatchException类的具体用法?Java InputMismatchException怎么用?Java InputMismatchException使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
InputMismatchException类属于org.antlr.v4.runtime包,在下文中一共展示了InputMismatchException类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: recover
import org.antlr.v4.runtime.InputMismatchException; //导入依赖的package包/类
@Override
public void recover(final Parser recognizer, final RecognitionException re) {
final Token token = re.getOffendingToken();
String message;
if (token == null) {
message = "no parse token found.";
} else if (re instanceof InputMismatchException) {
message = "unexpected token [" + getTokenErrorDisplay(token) + "]" +
" was expecting one of [" + re.getExpectedTokens().toString(recognizer.getVocabulary()) + "].";
} else if (re instanceof NoViableAltException) {
if (token.getType() == PainlessParser.EOF) {
message = "unexpected end of script.";
} else {
message = "invalid sequence of tokens near [" + getTokenErrorDisplay(token) + "].";
}
} else {
message = "unexpected token near [" + getTokenErrorDisplay(token) + "].";
}
Location location = new Location(sourceName, token == null ? -1 : token.getStartIndex());
throw location.createError(new IllegalArgumentException(message, re));
}
示例2: recoverInline
import org.antlr.v4.runtime.InputMismatchException; //导入依赖的package包/类
/**
* Make sure we don't attempt to recover inline; if the parser successfully
* recovers, it won't throw an exception.
*/
@Override
public Token recoverInline(Parser recognizer) throws RecognitionException {
InputMismatchException e = new InputMismatchException(recognizer);
String policies = recognizer.getInputStream().getText();
StringTokenizer tk = new StringTokenizer(policies, ";");
String policy = "";
int idx = 0;
while (tk.hasMoreElements()) {
policy = (String) tk.nextElement();
idx += policy.length();
if (idx >= e.getOffendingToken().getStartIndex()) {
break;
}
}
String message = Messages.get(Messages.DEFAULT_LOCALE,
"error_invalid_firewallconfig", new Object[] {
e.getOffendingToken().getText(), policy });
throw new RuntimeException(message);
}
示例3: formatMessage
import org.antlr.v4.runtime.InputMismatchException; //导入依赖的package包/类
@Override
public String formatMessage(SyntaxError error) {
RecognitionException exception = error.getException();
if (exception instanceof InputMismatchException) {
InputMismatchException mismatchException = (InputMismatchException) exception;
RuleContext ctx = mismatchException.getCtx();
int ruleIndex = ctx.getRuleIndex();
switch (ruleIndex) {
case ProtoParser.RULE_ident:
return ProtostuffBundle.message("error.expected.identifier");
default:
break;
}
}
return super.formatMessage(error);
}
示例4: reportError
import org.antlr.v4.runtime.InputMismatchException; //导入依赖的package包/类
public void reportError(Parser recognizer, RecognitionException e) {
if (!this.inErrorRecoveryMode(recognizer)) {
this.beginErrorCondition(recognizer);
if (e instanceof NoViableAltException) {
this.reportNoViableAlternative(recognizer, (NoViableAltException) e);
} else if (e instanceof InputMismatchException) {
this.reportInputMismatch(recognizer, (InputMismatchException) e);
} else if (e instanceof FailedPredicateException) {
this.reportFailedPredicate(recognizer, (FailedPredicateException) e);
} else {
System.err.println("unknown recognition error type: " + e.getClass().getName());
recognizer.notifyErrorListeners(e.getOffendingToken(), e.getMessage(), e);
}
}
}
示例5: recover
import org.antlr.v4.runtime.InputMismatchException; //导入依赖的package包/类
@Override
public void recover(Parser recognizer, RecognitionException e) {
for (ParserRuleContext context = recognizer.getContext(); context != null; context = context.getParent()) {
context.exception = e;
}
if (PredictionMode.LL.equals(recognizer.getInterpreter().getPredictionMode())) {
if (e instanceof NoViableAltException) {
this.reportNoViableAlternative(recognizer, (NoViableAltException) e);
} else if (e instanceof InputMismatchException) {
this.reportInputMismatch(recognizer, (InputMismatchException) e);
} else if (e instanceof FailedPredicateException) {
this.reportFailedPredicate(recognizer, (FailedPredicateException) e);
}
}
throw new ParseCancellationException(e);
}
示例6: syntaxError
import org.antlr.v4.runtime.InputMismatchException; //导入依赖的package包/类
@Override
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) {
if (e instanceof InputMismatchException || e instanceof NoViableAltException) {
String symbol = (offendingSymbol instanceof Token) ?
((Token) offendingSymbol).getText() :
String.valueOf(offendingSymbol);
errors.add(String.format(
"Unexpected input \'%s\' at %d:%d. Valid symbols are: %s",
symbol,
line,
charPositionInLine,
e.getExpectedTokens().toString(recognizer.getVocabulary())
));
} else {
errors.add(String.format("Parse error: \'%s\' near \'%s\' at %d:%d", msg, String.valueOf(offendingSymbol), line, charPositionInLine));
}
}
示例7: compile
import org.antlr.v4.runtime.InputMismatchException; //导入依赖的package包/类
public Walk compile( CharStream input, ErrorHandler errors ) {
try {
return compile( input );
} catch (ParseCancellationException ex) {
int line = ErrorHandler.UNKNOWN;
int column = ErrorHandler.UNKNOWN;
String msg = "Parser Cancelled.";
Throwable cause = ex.getCause();
if( cause instanceof InputMismatchException ) {
InputMismatchException immEx = (InputMismatchException) cause;
Token offender = immEx.getOffendingToken();
if( offender != null ) {
line = offender.getLine();
column = offender.getCharPositionInLine();
String txt = offender.getText();
if(txt != null) {
msg = " Unexpected Token '" + txt + "'.";
}
}
}
errors.parseError( line, column, msg );
}
return getProgram();
}
示例8: generate
import org.antlr.v4.runtime.InputMismatchException; //导入依赖的package包/类
Axis generate() {
try {
Axis axis = namingConvention.getAxis(axisColor, indexFullName() );
// This check has been moved into a thorough unit test instead of a runtime check.
// but I'll leave the code commented out here in case additional colors are eventually supported and need debugging
// String check = namingConvention.getName( axis );
// if ( axis != namingConvention.getAxis(axisColor, check ) ) {
// log( axisColor + " " + indexFullName() + " mapped to " + check );
// }
if(axis == null) {
InputMismatchException imex = new InputMismatchException(parser);
logger.log(Level.WARNING, imex.getMessage(), imex);
throw imex;
}
return axis;
//} catch( ArrayIndexOutOfBoundsException ex ){
//} catch( NullPointerException ex ) {
} catch( RuntimeException ex) {
String msg = "bad axis specification: '" + axisColor + " " + indexFullName() + "'";
logger.warning(msg);
throw new RuntimeException( msg, ex);
}
}
示例9: recoverInline
import org.antlr.v4.runtime.InputMismatchException; //导入依赖的package包/类
/** Make sure we don't attempt to recover inline; if the parser
* successfully recovers, it won't throw an exception.
*/
@Override
public Token recoverInline(Parser recognizer) throws RecognitionException
{
// SINGLE TOKEN DELETION
Token matchedSymbol = singleTokenDeletion(recognizer);
if (matchedSymbol != null)
{
// we have deleted the extra token.
// now, move past ttype token as if all were ok
recognizer.consume();
return matchedSymbol;
}
// SINGLE TOKEN INSERTION
if (singleTokenInsertion(recognizer))
{
return getMissingSymbol(recognizer);
}
// BeetlException exception = new BeetlParserException(BeetlException.PARSER_MISS_ERROR);
// exception.pushToken(this.getGrammarToken(recognizer.getCurrentToken()));
// throw exception;
throw new InputMismatchException(recognizer);
}
示例10: reportInputMismatch
import org.antlr.v4.runtime.InputMismatchException; //导入依赖的package包/类
@Override
public void reportInputMismatch(Parser recognizer, InputMismatchException e) throws RecognitionException {
String msg = "mismatched input " + getTokenErrorDisplay(e.getOffendingToken());
msg += " expecting one of " + e.getExpectedTokens().toString(recognizer.getTokenNames());
RecognitionException ex = new RecognitionException(msg, recognizer, recognizer.getInputStream(), recognizer.getContext());
ex.initCause(e);
throw ex;
}
示例11: exceptionString
import org.antlr.v4.runtime.InputMismatchException; //导入依赖的package包/类
public static String exceptionString(Parser parser, RecognitionException ex) {
if (ex instanceof FailedPredicateException) {
return failedPredicate((FailedPredicateException) ex);
} else if (ex instanceof InputMismatchException) {
return inputMismatch((InputMismatchException) ex);
} else if (ex instanceof NoViableAltException) {
return noViableAlt(parser, (NoViableAltException) ex);
} else {
System.err.println("unknown recognition exception:" + ex);
return ex.toString();
}
}
示例12: recoverInline
import org.antlr.v4.runtime.InputMismatchException; //导入依赖的package包/类
@Override
public Token recoverInline(Parser recognizer) throws RecognitionException {
int errIndex = recognizer.getInputStream().index();
if ( firstErrorTokenIndex == -1 ) {
firstErrorTokenIndex = errIndex; // latch
}
// System.err.println("recoverInline: error at " + errIndex);
InputMismatchException e = new InputMismatchException(recognizer);
// TokenStream input = recognizer.getInputStream(); // seek EOF
// input.seek(input.size() - 1);
throw e;
}
示例13: recoverInline
import org.antlr.v4.runtime.InputMismatchException; //导入依赖的package包/类
@Override
public Token recoverInline(Parser recognizer)
throws RecognitionException {
this.recover(recognizer, new InputMismatchException(recognizer)); // stop parsing
return null;
}
示例14: syntaxError
import org.antlr.v4.runtime.InputMismatchException; //导入依赖的package包/类
/**
* {@inheritDoc}
* <p/>
* The method is overridden to throw an exception if the parsed input has an invalid syntax.
*/
@Override
public void syntaxError(Recognizer<?, ?> recognizer, @Nullable Object offendingSymbol, int line,
int charPositionInLine, String msg, @Nullable RecognitionException e) {
String errorMessage = msg;
if (e instanceof InputMismatchException && msg.endsWith(" expecting VALUE")) {
errorMessage += ". Please make sure that all values are surrounded by double quotes.";
}
throw new IllegalArgumentException(errorMessage);
}
示例15: reportInputMismatch
import org.antlr.v4.runtime.InputMismatchException; //导入依赖的package包/类
@Override
public void reportInputMismatch(Parser recognizer, InputMismatchException e) throws RecognitionException {
String msg = "";
msg += "In file " + recognizer.getSourceName() + " at line " + recognizer.getContext().start.getLine() + ": ";
msg += "Mismatched input " + getTokenErrorDisplay(e.getOffendingToken());
msg += " expecting one of "+e.getExpectedTokens().toString(recognizer.getTokenNames()) + "\n";
msg += "Line Number " + recognizer.getContext().start.getLine() + ", Column " + recognizer.getContext().start.getCharPositionInLine() + ";";
RecognitionException ex = new RecognitionException(msg, recognizer, recognizer.getInputStream(), recognizer.getContext());
ex.initCause(e);
throw ex;
}