List of usage examples for org.antlr.v4.runtime Parser getCurrentToken
public Token getCurrentToken()
From source file:com.eprosima.idl.parser.strategy.DefaultErrorStrategy.java
License:Apache License
@Override protected void reportUnwantedToken(@NotNull Parser recognizer) { if (inErrorRecoveryMode(recognizer)) { return;// w w w. j ava 2s. c o m } beginErrorCondition(recognizer); Token t = recognizer.getCurrentToken(); String tokenName = getTokenErrorDisplay(t); String msg = "Unexpected input " + ColorMessage.bold(tokenName); recognizer.notifyErrorListeners(t, msg, null); }
From source file:com.facebook.presto.sql.parser.ErrorHandler.java
License:Apache License
@Override public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String message, RecognitionException e) { try {// ww w . j a v a2 s . c o m Parser parser = (Parser) recognizer; ATN atn = parser.getATN(); ATNState currentState; Token currentToken; RuleContext context; if (e != null) { currentState = atn.states.get(e.getOffendingState()); currentToken = e.getOffendingToken(); context = e.getCtx(); if (e instanceof NoViableAltException) { currentToken = ((NoViableAltException) e).getStartToken(); } } else { currentState = atn.states.get(parser.getState()); currentToken = parser.getCurrentToken(); context = parser.getContext(); } Analyzer analyzer = new Analyzer(atn, parser.getVocabulary(), specialRules, specialTokens, ignoredRules, parser.getTokenStream()); Multimap<Integer, String> candidates = analyzer.process(currentState, currentToken.getTokenIndex(), context); // pick the candidate tokens associated largest token index processed (i.e., the path that consumed the most input) String expected = candidates.asMap().entrySet().stream().max(Comparator.comparing(Map.Entry::getKey)) .get().getValue().stream().sorted().collect(Collectors.joining(", ")); message = String.format("mismatched input '%s'. Expecting: %s", ((Token) offendingSymbol).getText(), expected); } catch (Exception exception) { LOG.error(exception, "Unexpected failure when handling parsing error. This is likely a bug in the implementation"); } throw new ParsingException(message, e, line, charPositionInLine); }
From source file:com.github.jknack.handlebars.internal.HbsErrorStrategy.java
License:Apache License
@Override public void reportMissingToken(final Parser recognizer) { if (errorRecoveryMode) { return;/*w w w.j av a 2s.co m*/ } Token offendingToken = recognizer.getCurrentToken(); IntervalSet expecting = getExpectedTokens(recognizer); String msg = expecting.toString(recognizer.getTokenNames()); recognizer.notifyErrorListeners(offendingToken, msg, null); }
From source file:com.nextbreakpoint.nextfractal.contextfree.compiler.CompilerErrorStrategy.java
License:Open Source License
@Override public void reportError(Parser recognizer, RecognitionException e) { String message = generateErrorMessage("Parse failed", recognizer); CompilerError error = new CompilerError(CompilerError.ErrorType.CFDG_COMPILER, e.getOffendingToken().getLine(), e.getOffendingToken().getCharPositionInLine(), e.getOffendingToken().getStartIndex(), recognizer.getCurrentToken().getStopIndex() - recognizer.getCurrentToken().getStartIndex(), message);//from ww w. j a v a 2 s . c o m logger.log(Level.FINE, error.toString(), e); errors.add(error); }
From source file:com.nextbreakpoint.nextfractal.contextfree.compiler.CompilerErrorStrategy.java
License:Open Source License
@Override protected void reportInputMismatch(Parser recognizer, InputMismatchException e) { String message = generateErrorMessage("Input mismatch", recognizer); CompilerError error = new CompilerError(CompilerError.ErrorType.CFDG_COMPILER, e.getOffendingToken().getLine(), e.getOffendingToken().getCharPositionInLine(), e.getOffendingToken().getStartIndex(), recognizer.getCurrentToken().getStopIndex() - recognizer.getCurrentToken().getStartIndex(), message);//from w w w . j a v a 2s.c o m logger.log(Level.FINE, error.toString(), e); errors.add(error); }
From source file:com.nextbreakpoint.nextfractal.contextfree.compiler.CompilerErrorStrategy.java
License:Open Source License
@Override protected void reportFailedPredicate(Parser recognizer, FailedPredicateException e) { String message = generateErrorMessage("Failed predicate", recognizer); CompilerError error = new CompilerError(CompilerError.ErrorType.CFDG_COMPILER, e.getOffendingToken().getLine(), e.getOffendingToken().getCharPositionInLine(), e.getOffendingToken().getStartIndex(), recognizer.getCurrentToken().getStopIndex() - recognizer.getCurrentToken().getStartIndex(), message);//from ww w . java2 s . c o m logger.log(Level.FINE, error.toString(), e); errors.add(error); }
From source file:com.nextbreakpoint.nextfractal.contextfree.compiler.CompilerErrorStrategy.java
License:Open Source License
@Override protected void reportUnwantedToken(Parser recognizer) { String message = generateErrorMessage("Unwanted token", recognizer); CompilerError error = new CompilerError(CompilerError.ErrorType.CFDG_COMPILER, recognizer.getCurrentToken().getLine(), recognizer.getCurrentToken().getCharPositionInLine(), recognizer.getCurrentToken().getStartIndex(), recognizer.getCurrentToken().getStopIndex() - recognizer.getCurrentToken().getStartIndex(), message);//from w w w .j a v a 2s . c o m logger.log(Level.FINE, error.toString()); errors.add(error); }
From source file:com.nextbreakpoint.nextfractal.contextfree.compiler.CompilerErrorStrategy.java
License:Open Source License
@Override protected void reportMissingToken(Parser recognizer) { String message = generateErrorMessage("Missing token", recognizer); CompilerError error = new CompilerError(CompilerError.ErrorType.CFDG_COMPILER, recognizer.getCurrentToken().getLine(), recognizer.getCurrentToken().getCharPositionInLine(), recognizer.getCurrentToken().getStartIndex(), recognizer.getCurrentToken().getStopIndex() - recognizer.getCurrentToken().getStartIndex(), message);/* www .j a v a 2 s. c om*/ logger.log(Level.FINE, error.toString()); errors.add(error); }
From source file:com.nextbreakpoint.nextfractal.mandelbrot.compiler.CompilerErrorStrategy.java
License:Open Source License
@Override public void reportError(Parser recognizer, RecognitionException e) { String message = generateErrorMessage("Parse failed", recognizer); CompilerError error = new CompilerError(CompilerError.ErrorType.M_COMPILER, e.getOffendingToken().getLine(), e.getOffendingToken().getCharPositionInLine(), e.getOffendingToken().getStartIndex(), recognizer.getCurrentToken().getStopIndex() - recognizer.getCurrentToken().getStartIndex(), message);/*from w ww . j av a 2 s. co m*/ logger.log(Level.FINE, error.toString(), e); errors.add(error); }
From source file:com.nextbreakpoint.nextfractal.mandelbrot.compiler.CompilerErrorStrategy.java
License:Open Source License
@Override protected void reportInputMismatch(Parser recognizer, InputMismatchException e) { String message = generateErrorMessage("Input mismatch", recognizer); CompilerError error = new CompilerError(CompilerError.ErrorType.M_COMPILER, e.getOffendingToken().getLine(), e.getOffendingToken().getCharPositionInLine(), e.getOffendingToken().getStartIndex(), recognizer.getCurrentToken().getStopIndex() - recognizer.getCurrentToken().getStartIndex(), message);/*from w ww.ja v a2 s . c o m*/ logger.log(Level.FINE, error.toString(), e); errors.add(error); }