List of usage examples for org.antlr.v4.runtime Parser getTokenFactory
@Override
public TokenFactory<?> getTokenFactory()
From source file:com.sample.JavaErrorStrategy.java
License:BSD License
/** * Conjure up a missing token during error recovery. * * The recognizer attempts to recover from single missing symbols. But, * actions might refer to that missing symbol. For example, x=ID {f($x);}. * The action clearly assumes that there has been an identifier matched * previously and that $x points at that token. If that token is missing, * but the next token in the stream is what we want we assume that this * token is missing and we keep going. Because we have to return some token * to replace the missing token, we have to conjure one up. This method * gives the user control over the tokens returned for missing tokens. * Mostly, you will want to create something special for identifier tokens. * For literals such as '{' and ',', the default action in the parser or * tree parser works. It simply creates a CommonToken of the appropriate * type. The text will be the token. If you change what tokens must be * created by the lexer, override this method to create the appropriate * tokens./* ww w . ja va2 s.c o m*/ */ @NotNull protected Token getMissingSymbol(@NotNull Parser recognizer) { Token currentSymbol = recognizer.getCurrentToken(); IntervalSet expecting = getExpectedTokens(recognizer); int expectedTokenType = expecting.getMinElement(); // get any element String tokenText; if (expectedTokenType == Token.EOF) tokenText = "<missing EOF>"; else tokenText = "<missing " + recognizer.getTokenNames()[expectedTokenType] + ">"; Token current = currentSymbol; Token lookback = recognizer.getInputStream().LT(-1); if (current.getType() == Token.EOF && lookback != null) { current = lookback; } return recognizer.getTokenFactory() .create(new Pair<TokenSource, CharStream>(current.getTokenSource(), current.getTokenSource().getInputStream()), expectedTokenType, tokenText, Token.DEFAULT_CHANNEL, -1, -1, current.getLine(), current.getCharPositionInLine()); }