private void tryToExecuteByID(final Parser recognizer, final InputMismatchException cause) { Token token = cause.getOffendingToken(); CommonToken commonToken; if (token instanceof CommonToken) { commonToken = (CommonToken) token; } else { throw cause; } int previousType = commonToken.getType(); if (previousType > identifierTokenIndex) { return; } commonToken.setType(identifierTokenIndex); try { super.sync(recognizer); } catch (InputMismatchException ex) { if (cause.getOffendingToken() == ex.getOffendingToken()) { commonToken.setType(previousType); throw cause; } tryToExecuteByID(recognizer, ex); // CHECKSTYLE:OFF } catch (final Exception ex) { // CHECKSTYLE:ON commonToken.setType(previousType); throw cause; } } }
private int tryToExecuteByID(final DFA dfa, final DFAState s0, final TokenStream input, final int startIndex, final ParserRuleContext outerContext, final NoViableAltException cause) { Token token = cause.getOffendingToken(); CommonToken commonToken; if (token instanceof CommonToken) { commonToken = (CommonToken) token; } else { throw cause; } int previousType = commonToken.getType(); if (previousType > identifierTokenIndex || Token.EOF == token.getType()) { throw cause; } commonToken.setType(identifierTokenIndex); try { return super.execATN(dfa, s0, input, startIndex, outerContext); } catch (NoViableAltException ex) { if (cause.getOffendingToken() == ex.getOffendingToken()) { throw cause; } return tryToExecuteByID(dfa, s0, input, startIndex, outerContext, ex); // CHECKSTYLE:OFF } catch (final Exception ex) { // CHECKSTYLE:ON commonToken.setType(previousType); throw cause; } } }
/** * 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; } }
((CommonToken) token).setType(identifierTokenIndex);
private Token createToken(int tokenType, Token copyFrom) { String tokenTypeStr; if (tokenType == nlToken) { tokenTypeStr = "newline"; } else if (tokenType == indentToken) { tokenTypeStr = "indent"; } else if (tokenType == dedentToken) { tokenTypeStr = "dedent"; } else { tokenTypeStr = null; } CommonToken r = new InjectedToken(copyFrom, tokenTypeStr); r.setType(tokenType); return r; }
public void setIndexOfLastToken(int indexOfLastToken) { System.out.println("setIndexOfLastToken("+indexOfLastToken+")"); if ( indexOfLastToken<0 ) { System.out.println("replacing "+saveToken.getTokenIndex()+" with "+saveToken); tokens.set(saveToken.getTokenIndex(), saveToken); // this.indexOfLastToken = indexOfLastToken; return; } int i = indexOfLastToken + 1; // we want to keep token at indexOfLastToken sync(i); saveToken = tokens.get(i); System.out.println("saving "+saveToken); CommonToken stopToken = new CommonToken(saveToken); stopToken.setType(STOP_TOKEN_TYPE); System.out.println("setting "+i+" to "+stopToken); tokens.set(i, stopToken); // this.indexOfLastToken = indexOfLastToken; } }