Search in sources :

Example 71 with EOF

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());
}
Also used : Grammar(org.antlr.v4.tool.Grammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) ParseTree(org.antlr.v4.runtime.tree.ParseTree) Test(org.junit.Test)

Example 72 with EOF

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);
}
Also used : AtomTransition(org.antlr.v4.runtime.atn.AtomTransition) ATNState(org.antlr.v4.runtime.atn.ATNState)

Example 73 with EOF

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 + ">";
}
Also used : Vocabulary(org.antlr.v4.runtime.Vocabulary) NotNull(org.antlr.v4.runtime.misc.NotNull)

Example 74 with EOF

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;
}
Also used : ParseTreeListener(org.antlr.v4.runtime.tree.ParseTreeListener) ErrorNode(org.antlr.v4.runtime.tree.ErrorNode) TerminalNode(org.antlr.v4.runtime.tree.TerminalNode)

Example 75 with EOF

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);
}
Also used : IntervalSet(org.antlr.v4.runtime.misc.IntervalSet) NotNull(org.antlr.v4.runtime.misc.NotNull)

Aggregations

Test (org.junit.Test)218 LexerGrammar (org.antlr.v4.tool.LexerGrammar)182 Grammar (org.antlr.v4.tool.Grammar)110 CommonToken (org.antlr.v4.runtime.CommonToken)35 JavadocContext (com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocParser.JavadocContext)31 TextContext (com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocParser.TextContext)29 Token (org.antlr.v4.runtime.Token)19 ArrayList (java.util.ArrayList)18 ATN (org.antlr.v4.runtime.atn.ATN)18 IntervalSet (org.antlr.v4.runtime.misc.IntervalSet)18 DescriptionContext (com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocParser.DescriptionContext)15 ParseTree (org.antlr.v4.runtime.tree.ParseTree)13 HtmlElementContext (com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocParser.HtmlElementContext)12 JavadocTagContext (com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocParser.JavadocTagContext)12 JavadocInlineTagContext (com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocParser.JavadocInlineTagContext)10 ReferenceContext (com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocParser.ReferenceContext)10 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)10 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)10 HtmlElementCloseContext (com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocParser.HtmlElementCloseContext)9 HtmlElementOpenContext (com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocParser.HtmlElementOpenContext)9