Java Code Examples for org.antlr.v4.runtime.Parser#getContext()
The following examples show how to use
org.antlr.v4.runtime.Parser#getContext() .
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: DescriptiveErrorStrategy.java From groovy with Apache License 2.0 | 6 votes |
@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); }
Example 2
Source File: BailSyntaxErrorStrategy.java From gdl with Apache License 2.0 | 5 votes |
/** * 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}. To print the syntax error the * {@link DefaultErrorStrategy#recover(Parser, RecognitionException)} method * gets executed. */ @Override public void recover(Parser recognizer, RecognitionException e) { super.recover(recognizer, e); for (ParserRuleContext context = recognizer.getContext(); context != null; context = context.getParent()) { context.exception = e; } throw new ParseCancellationException(e); }
Example 3
Source File: BailSyntaxErrorStrategy.java From gdl with Apache License 2.0 | 5 votes |
/** * Make sure we don't attempt to recover inline; if the parser * successfully recovers, it won't throw an exception. * Again, the {@link DefaultErrorStrategy#recoverInline(Parser)} gets executed * to print the wrong syntax */ @Override public Token recoverInline(Parser recognizer) throws RecognitionException { super.recoverInline(recognizer); InputMismatchException e = new InputMismatchException(recognizer); for (ParserRuleContext context = recognizer.getContext(); context != null; context = context.getParent()) { context.exception = e; } throw new ParseCancellationException(e); }
Example 4
Source File: DoFailOnErrorHandler.java From JsoupXpath with Apache License 2.0 | 5 votes |
@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); }
Example 5
Source File: DoFailOnErrorHandler.java From JsoupXpath with Apache License 2.0 | 5 votes |
@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); }
Example 6
Source File: CompileErrorStrategy.java From BigDataScript with Apache License 2.0 | 5 votes |
/** Instead of recovering from pendingException {@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) { // Add a compiler error message String message = "Cannot parse input, near '" + e.getOffendingToken().getText() + "'"; CompilerMessage cm = new CompilerMessage(e.getOffendingToken().getInputStream().getSourceName(), e.getOffendingToken().getLine(), -1, message, MessageType.ERROR); CompilerMessages.get().add(cm); // Add pendingException to all contexts for (ParserRuleContext context = recognizer.getContext(); context != null; context = context.getParent()) context.exception = e; throw new ParseCancellationException(e); }
Example 7
Source File: CompileErrorStrategy.java From BigDataScript with Apache License 2.0 | 5 votes |
/** Make sure we don't attempt to recover inline; if the parser * successfully recovers, it won't throw an pendingException. */ @Override public Token recoverInline(Parser recognizer) throws RecognitionException { InputMismatchException e = new InputMismatchException(recognizer); String message = "Cannot parse input, near '" + e.getOffendingToken().getText() + "'"; CompilerMessage cm = new CompilerMessage(e.getOffendingToken().getInputStream().getSourceName(), e.getOffendingToken().getLine(), -1, message, MessageType.ERROR); CompilerMessages.get().add(cm); // Add pendingException to all contexts for (ParserRuleContext context = recognizer.getContext(); context != null; context = context.getParent()) context.exception = e; throw new ParseCancellationException(e); }
Example 8
Source File: ErrorHandler.java From presto with Apache License 2.0 | 4 votes |
@Override public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String message, RecognitionException e) { try { 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(parser, specialRules, specialTokens, ignoredRules); Result result = 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 = result.getExpected().stream() .sorted() .collect(Collectors.joining(", ")); message = format("mismatched input '%s'. Expecting: %s", parser.getTokenStream().get(result.getErrorTokenIndex()).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); }
Example 9
Source File: BatfishANTLRErrorStrategy.java From batfish with Apache License 2.0 | 4 votes |
@Override public void sync(Parser recognizer) throws RecognitionException { /* * BEGIN: Copied from super */ ATNState s = recognizer.getInterpreter().atn.states.get(recognizer.getState()); if (inErrorRecoveryMode(recognizer)) { return; } TokenStream tokens = recognizer.getInputStream(); int la = tokens.LA(1); IntervalSet nextTokens = recognizer.getATN().nextTokens(s); /* * If next token is unmatchable (i.e. from a lexer error), we need to hide the whole line before * returning so we don't unnecessarily pop out of the star or plus loop (if in one) afterwards. */ int atnStateType = s.getStateType(); boolean atLoopExitDecision = (atnStateType == ATNState.STAR_LOOP_BACK || atnStateType == ATNState.PLUS_LOOP_BACK || atnStateType == ATNState.STAR_LOOP_ENTRY); boolean lexerErrorAtLoopExitDecision = la == BatfishLexer.UNMATCHABLE_TOKEN && atLoopExitDecision; boolean lexerErrorAtStartOfLineAtLoopExitDecision = lexerErrorAtLoopExitDecision && ((BatfishParser) recognizer).getLastConsumedToken() == _separatorToken; if (!lexerErrorAtStartOfLineAtLoopExitDecision && (nextTokens.contains(Token.EPSILON) || nextTokens.contains(la))) { return; } /* * END: Copied from super */ boolean topLevel = recognizer.getContext().parent == null; switch (atnStateType) { case ATNState.BLOCK_START: case ATNState.STAR_BLOCK_START: case ATNState.PLUS_BLOCK_START: case ATNState.STAR_LOOP_ENTRY: case ATNState.PLUS_LOOP_BACK: case ATNState.STAR_LOOP_BACK: if (topLevel || lexerErrorAtStartOfLineAtLoopExitDecision) { /* * When at top level, we cannot pop up. So consume every "line" until we have one that * starts with a token acceptable at the top level. * * We also don't want to pop out of star or plus loops whose elements start at the * beginning of a line, or else we'd lose the whole loop. */ reportUnwantedToken(recognizer); consumeBlocksUntilWanted(recognizer); return; } else { /* * If not at the top level, error out to pop up a level. This may repeat until the next * token is acceptable at the given level. * * Note that this branch is also taken for errors occurring in start or plus loops in the * middle in the middle of a line; in that case we want to throw the whole loop (and its * containing context) away. */ beginErrorCondition(recognizer); throw new InputMismatchException(recognizer); } default: return; } }