use of org.antlr.v4.runtime.Recognizer.EOF in project antlr4 by tunnelvisionlabs.
the class TestParserInterpreter method testEmptyInput.
@Test
public void testEmptyInput() throws Exception {
LexerGrammar lg = new LexerGrammar("lexer grammar L;\n" + "A : 'a' ;\n");
Grammar g = new Grammar("parser grammar T;\n" + "s : x EOF ;\n" + "x : ;\n", lg);
ParseTree t = testInterp(lg, g, "s", "", "(s x <EOF>)");
// s
assertEquals("0..0", t.getSourceInterval().toString());
// x
assertEquals("0..-1", t.getChild(0).getSourceInterval().toString());
}
use of org.antlr.v4.runtime.Recognizer.EOF in project antlr4 by tunnelvisionlabs.
the class LexerATNFactory method tokenRef.
@Override
public Handle tokenRef(TerminalAST node) {
// Ref to EOF in lexer yields char transition on -1
if (node.getText().equals("EOF")) {
ATNState left = newState(node);
ATNState right = newState(node);
left.addTransition(new AtomTransition(right, IntStream.EOF));
return new Handle(left, right);
}
return _ruleRef(node);
}
use of org.antlr.v4.runtime.Recognizer.EOF in project antlr4 by tunnelvisionlabs.
the class ParserATNSimulator method getTokenName.
@NotNull
public String getTokenName(int t) {
if (t == Token.EOF) {
return "EOF";
}
Vocabulary vocabulary = parser != null ? parser.getVocabulary() : VocabularyImpl.EMPTY_VOCABULARY;
String displayName = vocabulary.getDisplayName(t);
if (displayName.equals(Integer.toString(t))) {
return displayName;
}
return displayName + "<" + t + ">";
}
use of org.antlr.v4.runtime.Recognizer.EOF in project antlr4 by tunnelvisionlabs.
the class Parser method consume.
/**
* Consume and return the {@linkplain #getCurrentToken current symbol}.
*
* <p>E.g., given the following input with {@code A} being the current
* lookahead symbol, this function moves the cursor to {@code B} and returns
* {@code A}.</p>
*
* <pre>
* A B
* ^
* </pre>
*
* If the parser is not in error recovery mode, the consumed symbol is added
* to the parse tree using {@link ParserRuleContext#addChild(TerminalNode)}, and
* {@link ParseTreeListener#visitTerminal} is called on any parse listeners.
* If the parser <em>is</em> in error recovery mode, the consumed symbol is
* added to the parse tree using {@link #createErrorNode(ParserRuleContext, Token)} then
* {@link ParserRuleContext#addErrorNode(ErrorNode)} and
* {@link ParseTreeListener#visitErrorNode} is called on any parse
* listeners.
*/
public Token consume() {
Token o = getCurrentToken();
if (o.getType() != EOF) {
getInputStream().consume();
}
boolean hasListener = _parseListeners != null && !_parseListeners.isEmpty();
if (_buildParseTrees || hasListener) {
if (_errHandler.inErrorRecoveryMode(this)) {
ErrorNode node = _ctx.addErrorNode(createErrorNode(_ctx, o));
if (_parseListeners != null) {
for (ParseTreeListener listener : _parseListeners) {
listener.visitErrorNode(node);
}
}
} else {
TerminalNode node = createTerminalNode(_ctx, o);
_ctx.addChild(node);
if (_parseListeners != null) {
for (ParseTreeListener listener : _parseListeners) {
listener.visitTerminal(node);
}
}
}
}
return o;
}
use of org.antlr.v4.runtime.Recognizer.EOF in project antlr4 by tunnelvisionlabs.
the class DefaultErrorStrategy method getMissingSymbol.
/**
* 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.
*/
@NotNull
protected Token getMissingSymbol(@NotNull Parser recognizer) {
Token currentSymbol = recognizer.getCurrentToken();
IntervalSet expecting = getExpectedTokens(recognizer);
int expectedTokenType = Token.INVALID_TYPE;
if (!expecting.isNil()) {
// get any element
expectedTokenType = expecting.getMinElement();
}
String tokenText;
if (expectedTokenType == Token.EOF)
tokenText = "<missing EOF>";
else
tokenText = "<missing " + recognizer.getVocabulary().getDisplayName(expectedTokenType) + ">";
Token current = currentSymbol;
Token lookback = recognizer.getInputStream().LT(-1);
if (current.getType() == Token.EOF && lookback != null) {
current = lookback;
}
return constructToken(recognizer.getInputStream().getTokenSource(), expectedTokenType, tokenText, current);
}
Aggregations