Java Code Examples for org.antlr.v4.runtime.Parser#getCurrentToken()
The following examples show how to use
org.antlr.v4.runtime.Parser#getCurrentToken() .
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: BatfishANTLRErrorStrategy.java From batfish with Apache License 2.0 | 6 votes |
/** * Consume all tokens a whole line at a time until the next token is one expected by the current * rule. Each line (as delimited by supplied separator token) starting from the current line up to * the last line consumed is placed in an {@link ErrorNode} and inserted as a child of the current * rule. * * @param recognizer The {@link Parser} to whom to delegate creation of each {@link ErrorNode} */ private void consumeBlocksUntilWanted(Parser recognizer) { IntervalSet expecting = recognizer.getExpectedTokens(); IntervalSet whatFollowsLoopIterationOrRule = expecting.or(getErrorRecoverySet(recognizer)); int nextToken; do { // Eat tokens until we are at the end of the line consumeUntilEndOfLine(recognizer); // Get the line number and separator text from the separator token Token separatorToken = recognizer.getCurrentToken(); // Insert the current line as an {@link ErrorNode} as a child of the current rule createErrorNode(recognizer, recognizer.getContext(), separatorToken); // Eat the separator token recognizer.consume(); nextToken = recognizer.getInputStream().LA(1); } while (!whatFollowsLoopIterationOrRule.contains(nextToken) && nextToken != Lexer.EOF); }
Example 2
Source File: BeetlAntlrErrorStrategy.java From beetl2.0 with BSD 3-Clause "New" or "Revised" License | 6 votes |
protected void reportUnwantedToken(@NotNull Parser recognizer) { if (inErrorRecoveryMode(recognizer)) { return; } beginErrorCondition(recognizer); Token t = recognizer.getCurrentToken(); String tokenName = getTokenErrorDisplay(t); IntervalSet expecting = getExpectedTokens(recognizer); String msg = "多余输入 " + tokenName + " 期望 " + expecting.toString(recognizer.getTokenNames()); BeetlException exception = new BeetlParserException(BeetlException.PARSER_MISS_ERROR, msg); // exception.token = this.getGrammarToken(t); exception.pushToken(this.getGrammarToken(t)); throw exception; }
Example 3
Source File: KsqlParserErrorStrategy.java From ksql-fork-with-deep-learning-function with Apache License 2.0 | 5 votes |
protected void reportUnwantedToken(Parser recognizer) { if (!this.inErrorRecoveryMode(recognizer)) { this.beginErrorCondition(recognizer); Token t = recognizer.getCurrentToken(); String tokenName = this.getTokenErrorDisplay(t); IntervalSet expecting = this.getExpectedTokens(recognizer); String msg = "extraneous input " + tokenName + " expecting " + expecting.toString(recognizer.getVocabulary()); recognizer.notifyErrorListeners(t, msg, (RecognitionException) null); } }
Example 4
Source File: KsqlParserErrorStrategy.java From ksql-fork-with-deep-learning-function with Apache License 2.0 | 5 votes |
protected void reportMissingToken(Parser recognizer) { if (!this.inErrorRecoveryMode(recognizer)) { this.beginErrorCondition(recognizer); Token t = recognizer.getCurrentToken(); IntervalSet expecting = this.getExpectedTokens(recognizer); String msg = "missing " + expecting.toString(recognizer.getVocabulary()) + " at " + this .getTokenErrorDisplay(t); recognizer.notifyErrorListeners(t, msg, (RecognitionException) null); } }
Example 5
Source File: JavascriptParserErrorStrategy.java From lucene-solr with Apache License 2.0 | 5 votes |
/** * Ensures the ANTLR parser will throw an exception after the first error * * @param recognizer the parser being used * @return no actual return value * @throws RecognitionException not used as a ParseException wrapped in a RuntimeException is thrown instead */ @Override public Token recoverInline(Parser recognizer) throws RecognitionException { Token token = recognizer.getCurrentToken(); String message = "unexpected token " + getTokenErrorDisplay(token) + " on line (" + token.getLine() + ") position (" + token.getCharPositionInLine() + ")" + " was expecting one of " + recognizer.getExpectedTokens().toString(recognizer.getVocabulary()); ParseException parseException = new ParseException(message, token.getStartIndex()); throw new RuntimeException(parseException); }
Example 6
Source File: ErrorStrategyAdaptor.java From antlr4-intellij-adaptor with BSD 2-Clause "Simplified" License | 5 votes |
@Override protected void consumeUntil(Parser recognizer, IntervalSet set) { Token o = recognizer.getCurrentToken(); if ( o.getType()==Token.EOF ) { recognizer.getRuleContext().addErrorNode(o); } super.consumeUntil(recognizer, set); }
Example 7
Source File: ErrorStrategyAdaptor.java From antlr4-intellij-adaptor with BSD 2-Clause "Simplified" License | 5 votes |
/** By default ANTLR makes the start/stop -1/-1 for invalid tokens * which is reasonable but here we want to highlight the * current position indicating that is where we lack a token. * if no input, highlight at position 0. */ protected Token getMissingSymbol(Parser recognizer) { Token missingSymbol = super.getMissingSymbol(recognizer); // alter the default missing symbol. if ( missingSymbol instanceof CommonToken) { int start, stop; Token current = recognizer.getCurrentToken(); start = current.getStartIndex(); stop = current.getStopIndex(); ((CommonToken) missingSymbol).setStartIndex(start); ((CommonToken) missingSymbol).setStopIndex(stop); } return missingSymbol; }
Example 8
Source File: CapitulatingErrorStrategy.java From rapidminer-studio with GNU Affero General Public License v3.0 | 5 votes |
@Override protected void reportUnwantedToken(Parser recognizer) { // change error message from default implementation if (inErrorRecoveryMode(recognizer)) { return; } beginErrorCondition(recognizer); Token t = recognizer.getCurrentToken(); String tokenName = getTokenErrorDisplay(t); String msg = "extraneous input " + tokenName + " expecting operator"; recognizer.notifyErrorListeners(t, msg, null); }
Example 9
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); }