List of usage examples for org.antlr.v4.runtime Parser consume
public Token consume()
From source file:com.sample.JavaErrorStrategy.java
License:BSD License
/** * {@inheritDoc}//from w ww. ja va 2 s .c om * * <p> * The default implementation resynchronizes the parser by consuming tokens * until we find one in the resynchronization set--loosely the set of tokens * that can follow the current rule. * </p> */ @Override public void recover(Parser recognizer, RecognitionException e) { // System.out.println("recover in "+recognizer.getRuleInvocationStack()+ // " index="+recognizer.getInputStream().index()+ // ", lastErrorIndex="+ // lastErrorIndex+ // ", states="+lastErrorStates); if (lastErrorIndex == recognizer.getInputStream().index() && lastErrorStates != null && lastErrorStates.contains(recognizer.getState())) { // uh oh, another error at same token index and previously-visited // state in ATN; must be a case where LT(1) is in the recovery // token set so nothing got consumed. Consume a single token // at least to prevent an infinite loop; this is a failsafe. // System.err.println("seen error condition before index="+ // lastErrorIndex+", states="+lastErrorStates); // System.err.println("FAILSAFE consumes "+recognizer.getTokenNames()[recognizer.getInputStream().LA(1)]); recognizer.consume(); } lastErrorIndex = recognizer.getInputStream().index(); if (lastErrorStates == null) lastErrorStates = new IntervalSet(); lastErrorStates.add(recognizer.getState()); }
From source file:com.sample.JavaErrorStrategy.java
License:BSD License
/** * {@inheritDoc}/*from w w w .j a v a 2 s . c o m*/ * * <p> * The default implementation attempts to recover from the mismatched input * by using single token insertion and deletion as described below. If the * recovery attempt fails, this method throws an * {@link InputMismatchException}. * </p> * * <p> * <strong>EXTRA TOKEN</strong> (single token deletion) * </p> * * <p> * {@code LA(1)} is not what we are looking for. If {@code LA(2)} has the * right token, however, then assume {@code LA(1)} is some extra spurious * token and delete it. Then consume and return the next token (which was * the {@code LA(2)} token) as the successful result of the match operation. * </p> * * <p> * This recovery strategy is implemented by {@link #singleTokenDeletion}. * </p> * * <p> * <strong>MISSING TOKEN</strong> (single token insertion) * </p> * * <p> * If current token (at {@code LA(1)}) is consistent with what could come * after the expected {@code LA(1)} token, then assume the token is missing * and use the parser's {@link TokenFactory} to create it on the fly. The * "insertion" is performed by returning the created token as the successful * result of the match operation. * </p> * * <p> * This recovery strategy is implemented by {@link #singleTokenInsertion}. * </p> * * <p> * <strong>EXAMPLE</strong> * </p> * * <p> * For example, Input {@code i=(3;} is clearly missing the {@code ')'}. When * the parser returns from the nested call to {@code expr}, it will have * call chain: * </p> * * <pre> * stat → expr → atom * </pre> * * and it will be trying to match the {@code ')'} at this point in the * derivation: * * <pre> * => ID '=' '(' INT ')' ('+' atom)* ';' * ^ * </pre> * * The attempt to match {@code ')'} will fail when it sees {@code ';'} and * call {@link #recoverInline}. To recover, it sees that {@code LA(1)==';'} * is in the set of tokens that can follow the {@code ')'} token reference * in rule {@code atom}. It can assume that you forgot the {@code ')'}. */ @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); } // even that didn't work; must throw the exception throw new InputMismatchException(recognizer); }
From source file:com.sample.JavaErrorStrategy.java
License:BSD License
/** * This method implements the single-token deletion inline error recovery * strategy. It is called by {@link #recoverInline} to attempt to recover * from mismatched input. If this method returns null, the parser and error * handler state will not have changed. If this method returns non-null, * {@code recognizer} will <em>not</em> be in error recovery mode since the * returned token was a successful match. * * <p>/* ww w . ja v a2s .c o m*/ * If the single-token deletion is successful, this method calls * {@link #reportUnwantedToken} to report the error, followed by * {@link Parser#consume} to actually "delete" the extraneous token. Then, * before returning {@link #reportMatch} is called to signal a successful * match. * </p> * * @param recognizer * the parser instance * @return the successfully matched {@link Token} instance if single-token * deletion successfully recovers from the mismatched input, * otherwise {@code null} */ @Nullable protected Token singleTokenDeletion(@NotNull Parser recognizer) { int nextTokenType = recognizer.getInputStream().LA(2); IntervalSet expecting = getExpectedTokens(recognizer); if (expecting.contains(nextTokenType)) { reportUnwantedToken(recognizer); /* * System.err.println("recoverFromMismatchedToken deleting "+ * ((TokenStream)recognizer.getInputStream()).LT(1)+ * " since "+((TokenStream)recognizer.getInputStream()).LT(2)+ * " is what we want"); */ recognizer.consume(); // simply delete extra token // we want to return the token we're actually matching Token matchedSymbol = recognizer.getCurrentToken(); reportMatch(recognizer); // we know current token is correct return matchedSymbol; } return null; }
From source file:com.sample.JavaErrorStrategy.java
License:BSD License
/** Consume tokens until one matches the given token set. */ protected void consumeUntil(@NotNull Parser recognizer, @NotNull IntervalSet set) { // System.err.println("consumeUntil("+set.toString(recognizer.getTokenNames())+")"); int ttype = recognizer.getInputStream().LA(1); while (ttype != Token.EOF && !set.contains(ttype)) { // System.out.println("consume during recover LA(1)="+getTokenNames()[input.LA(1)]); // recognizer.getInputStream().consume(); recognizer.consume(); ttype = recognizer.getInputStream().LA(1); }/*from www . j a v a 2 s .c o m*/ }
From source file:org.ballerinalang.langserver.completions.CompletionCustomErrorStrategy.java
License:Open Source License
private void removePendingTokensAfterThisToken(Parser recognizer, Token token, TokenRemovalStrategy currentStrategy) { int currentTokenIndex = recognizer.getCurrentToken().getTokenIndex(); if (token != null && (isInLastTermination(recognizer) || TokenRemovalStrategy.SYNC.equals(currentStrategy) && currentTokenIndex < token.getTokenIndex() || TokenRemovalStrategy.MATCH.equals(currentStrategy) && currentTokenIndex <= token.getTokenIndex())) { return;/*from w w w. j a v a 2s.com*/ } while (removeTokenCount > 0) { forceConsumedTokens.push(recognizer.consume()); removeTokenCount--; } this.context.put(CompletionKeys.FORCE_CONSUMED_TOKENS_KEY, forceConsumedTokens); }
From source file:org.beetl.core.parser.BeetlAntlrErrorStrategy.java
License:BSD License
/** Make sure we don't attempt to recover inline; if the parser * successfully recovers, it won't throw an exception. *///from w w w. j a v a 2s . co m @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); } // even that didn't work; must throw the exception BeetlException exception = new BeetlParserException(BeetlException.PARSER_MISS_ERROR); exception.token = this.getGrammarToken(recognizer.getCurrentToken()); throw exception; // // throw new InputMismatchException(recognizer); }