currentState = atn.states.get(parser.getState()); currentToken = parser.getCurrentToken(); context = parser.getContext();
/** * Get matched token by token type. * * @param tokenType token type * @return matched token * @throws RecognitionException mismatch throw exception */ public Token getMatchedToken(final int tokenType) throws RecognitionException { Token result = parser.getCurrentToken(); boolean isIdentifierCompatible = false; if (identifierTokenIndex == tokenType && identifierTokenIndex > result.getType()) { isIdentifierCompatible = true; } if (result.getType() == tokenType || isIdentifierCompatible) { if (Token.EOF != tokenType && isIdentifierCompatible && result instanceof CommonToken) { ((CommonToken) result).setType(identifierTokenIndex); } parser.getErrorHandler().reportMatch(parser); parser.consume(); } else { result = parser.getErrorHandler().recoverInline(parser); if (parser.getBuildParseTree() && -1 == result.getTokenIndex()) { parser.getContext().addErrorNode(parser.createErrorNode(parser.getContext(), result)); } } return result; } }
nextTokensContext = recognizer.getContext(); nextTokensState = recognizer.getState();
/** Instead of recovering from exception {@code e}, re-throw it wrapped * in a {@link ParseCancellationException} so it is not caught by the * rule function catches. Use {@link Exception#getCause()} to get the * original {@link RecognitionException}. */ @Override public void recover(Parser recognizer, RecognitionException e) { for (ParserRuleContext context = recognizer.getContext(); context != null; context = context.getParent()) { context.exception = e; } throw new ParseCancellationException(e); }
/** * Computes the set of input symbols which could follow the current parser * state and context, as given by {@link #getState} and {@link #getContext}, * respectively. * * @see ATN#getExpectedTokens(int, RuleContext) */ public IntervalSet getExpectedTokens() { return getATN().getExpectedTokens(getState(), getContext()); }
/** 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); for (ParserRuleContext context = recognizer.getContext(); context != null; context = context.getParent()) { context.exception = e; } throw new ParseCancellationException(e); }
nextTokensContext = recognizer.getContext(); nextTokensState = recognizer.getState();
/** Instead of recovering from exception {@code e}, re-throw it wrapped * in a {@link org.antlr.v4.runtime.misc.ParseCancellationException} so it is not caught by the * rule function catches. Use {@link Exception#getCause()} to get the * original {@link org.antlr.v4.runtime.RecognitionException}. */ @Override public void recover(Parser recognizer, RecognitionException e) { for (ParserRuleContext context = recognizer.getContext(); context != null; context = context.getParent()) { context.exception = e; } super.recover(recognizer,e); }
/** Instead of recovering from exception {@code e}, re-throw it wrapped * in a {@link ParseCancellationException} so it is not caught by the * rule function catches. Use {@link Exception#getCause()} to get the * original {@link RecognitionException}. */ @Override public void recover(Parser recognizer, RecognitionException e) { for (ParserRuleContext context = recognizer.getContext(); context != null; context = context.getParent()) { context.exception = e; } throw new ParseCancellationException(e); }
/** * throws RecognitionException to handle in parser's catch block */ public Token recoverInline(Parser recognizer) throws RecognitionException { throw new RecognitionException(recognizer, recognizer.getInputStream(), recognizer.getContext()); }
/** Instead of recovering from exception {@code e}, re-throw it wrapped * in a {@link org.antlr.v4.runtime.misc.ParseCancellationException} so it is not caught by the * rule function catches. Use {@link Exception#getCause()} to get the * original {@link org.antlr.v4.runtime.RecognitionException}. */ @Override public void recover(Parser recognizer, RecognitionException e) { for (ParserRuleContext context = recognizer.getContext(); context != null; context = context.getParent()) { context.exception = e; } super.recover(recognizer,e); }
/** * throws RecognitionException to handle in parser's catch block */ public Token recoverInline(Parser recognizer) throws RecognitionException { throw new RecognitionException(recognizer, recognizer.getInputStream(), recognizer.getContext()); }
/** Instead of recovering from exception {@code e}, re-throw it wrapped * in a {@link ParseCancellationException} so it is not caught by the * rule function catches. Use {@link Exception#getCause()} to get the * original {@link RecognitionException}. */ @Override public void recover(Parser recognizer, RecognitionException e) { for (ParserRuleContext context = recognizer.getContext(); context != null; context = context.getParent()) { context.exception = e; } throw new ParseCancellationException(e); }
/** Instead of recovering from exception {@code e}, re-throw it wrapped * in a {@link ParseCancellationException} so it is not caught by the * rule function catches. Use {@link Exception#getCause()} to get the * original {@link RecognitionException}. */ @Override public void recover(Parser recognizer, RecognitionException e) { for (ParserRuleContext context = recognizer.getContext(); context != null; context = context.getParent()) { context.exception = e; } throw new ParseCancellationException(e); }
@Override public void reportInputMismatch(Parser parser, InputMismatchException e) { if (!parser.getContext().start.getTokenSource().getSourceName() .equals(context.get(DocumentServiceKeys.RELATIVE_FILE_PATH_KEY).replace("\\", "/"))) { return; } this.context.put(CompletionKeys.TOKEN_STREAM_KEY, parser.getTokenStream()); }
@Override public void reportMissingToken(Parser parser) { if (!parser.getContext().start.getTokenSource().getSourceName() .equals(context.get(DocumentServiceKeys.RELATIVE_FILE_PATH_KEY).replace("\\", "/"))) { return; } this.context.put(CompletionKeys.TOKEN_STREAM_KEY, parser.getTokenStream()); }
@Override public void reportNoViableAlternative(Parser parser, NoViableAltException e) { if (!parser.getContext().start.getTokenSource().getSourceName() .equals(context.get(DocumentServiceKeys.RELATIVE_FILE_PATH_KEY).replace("\\", "/"))) { return; } this.context.put(CompletionKeys.TOKEN_STREAM_KEY, parser.getTokenStream()); }
@Override public void reportFailedPredicate(Parser parser, FailedPredicateException e) { setErrorState(parser); DiagnosticPos pos = getPosition(getMissingSymbol(parser)); if (parser.getContext() instanceof BallerinaParser.ShiftExprPredicateContext) { dlog.error(pos, DiagnosticCode.INVALID_SHIFT_OPERATOR); } else if (parser.getContext() instanceof BallerinaParser.RestDescriptorPredicateContext) { dlog.error(pos, DiagnosticCode.INVALID_RECORD_REST_DESCRIPTOR); } else { dlog.error(pos, DiagnosticCode.FAILED_PREDICATE, e.getMessage()); } }
@Override public void reportUnwantedToken(Parser parser) { if (parser.getContext().exception != null || inErrorRecoveryMode(parser)) { return; } beginErrorCondition(parser); setErrorState(parser); Token token = parser.getCurrentToken(); DiagnosticPos pos = getPosition(getMissingSymbol(parser)); dlog.error(pos, DiagnosticCode.EXTRANEOUS_INPUT, getTokenErrorDisplay(token)); }
protected void reportFailedPredicate(@NotNull Parser recognizer, @NotNull FailedPredicateException e) { String ruleName = recognizer.getRuleNames()[recognizer.getContext().getRuleIndex()]; BeetlException exception = new BeetlParserException(BeetlException.PARSER_PREDICATE_ERROR, ruleName, e); // exception.token = this.getGrammarToken(e.getOffendingToken()); exception.pushToken(this.getGrammarToken(e.getOffendingToken())); throw exception; }