Search in sources :

Example 66 with EOF

use of org.antlr.v4.runtime.Recognizer.EOF in project antlr4 by tunnelvisionlabs.

the class TestExpectedTokens method testEpsilonAltSubrule.

@Test
public void testEpsilonAltSubrule() throws Exception {
    String gtext = "parser grammar T;\n" + "a : A (B | ) C ;\n";
    Grammar g = new Grammar(gtext);
    String atnText = "RuleStart_a_0->s2\n" + "s2-A->BlockStart_5\n" + "BlockStart_5->s3\n" + "BlockStart_5->s4\n" + "s3-B->BlockEnd_6\n" + "s4->BlockEnd_6\n" + "BlockEnd_6->s7\n" + "s7-C->s8\n" + "s8->RuleStop_a_1\n" + "RuleStop_a_1-EOF->s9\n";
    checkRuleATN(g, "a", atnText);
    ATN atn = g.getATN();
    int blkStartStateNumber = 5;
    IntervalSet tokens = atn.getExpectedTokens(blkStartStateNumber, null);
    assertEquals("{B, C}", tokens.toString(g.getVocabulary()));
}
Also used : IntervalSet(org.antlr.v4.runtime.misc.IntervalSet) Grammar(org.antlr.v4.tool.Grammar) ATN(org.antlr.v4.runtime.atn.ATN) Test(org.junit.Test)

Example 67 with EOF

use of org.antlr.v4.runtime.Recognizer.EOF in project antlr4 by tunnelvisionlabs.

the class GrammarParserInterpreter method getLookaheadParseTrees.

/**
 * Return a list of parse trees, one for each alternative in a decision
 *  given the same input.
 *
 *  Very similar to {@link #getAllPossibleParseTrees} except
 *  that it re-parses the input for every alternative in a decision,
 *  not just the ambiguous ones (there is no alts parameter here).
 *  This method also tries to reduce the size of the parse trees
 *  by stripping away children of the tree that are completely out of range
 *  of startIndex..stopIndex. Also, because errors are expected, we
 *  use a specialized error handler that more or less bails out
 *  but that also consumes the first erroneous token at least. This
 *  ensures that an error node will be in the parse tree for display.
 *
 *  NOTES:
 *    // we must parse the entire input now with decision overrides
 *	// we cannot parse a subset because it could be that a decision
 *	// above our decision of interest needs to read way past
 *	// lookaheadInfo.stopIndex. It seems like there is no escaping
 *	// the use of a full and complete token stream if we are
 *	// resetting to token index 0 and re-parsing from the start symbol.
 *	// It's not easy to restart parsing somewhere in the middle like a
 *	// continuation because our call stack does not match the
 *	// tree stack because of left recursive rule rewriting. grrrr!
 *
 * @since 4.5.1
 */
public static List<ParserRuleContext> getLookaheadParseTrees(Grammar g, ParserInterpreter originalParser, TokenStream tokens, int startRuleIndex, int decision, int startIndex, int stopIndex) {
    List<ParserRuleContext> trees = new ArrayList<ParserRuleContext>();
    // Create a new parser interpreter to parse the ambiguous subphrase
    ParserInterpreter parser = deriveTempParserInterpreter(g, originalParser, tokens);
    DecisionState decisionState = originalParser.getATN().decisionToState.get(decision);
    for (int alt = 1; alt <= decisionState.getTransitions().length; alt++) {
        // re-parse entire input for all ambiguous alternatives
        // (don't have to do first as it's been parsed, but do again for simplicity
        // using this temp parser.)
        GrammarParserInterpreter.BailButConsumeErrorStrategy errorHandler = new GrammarParserInterpreter.BailButConsumeErrorStrategy();
        parser.setErrorHandler(errorHandler);
        parser.reset();
        parser.addDecisionOverride(decision, startIndex, alt);
        ParserRuleContext tt = parser.parse(startRuleIndex);
        int stopTreeAt = stopIndex;
        if (errorHandler.firstErrorTokenIndex >= 0) {
            // cut off rest at first error
            stopTreeAt = errorHandler.firstErrorTokenIndex;
        }
        Interval overallRange = tt.getSourceInterval();
        if (stopTreeAt > overallRange.b) {
            // If we try to look beyond range of tree, stopTreeAt must be EOF
            // for which there is no EOF ref in grammar. That means tree
            // will not have node for stopTreeAt; limit to overallRange.b
            stopTreeAt = overallRange.b;
        }
        ParserRuleContext subtree = Trees.getRootOfSubtreeEnclosingRegion(tt, startIndex, stopTreeAt);
        // Use higher of overridden decision tree or tree enclosing all tokens
        if (Trees.isAncestorOf(parser.getOverrideDecisionRoot(), subtree)) {
            subtree = parser.getOverrideDecisionRoot();
        }
        Trees.stripChildrenOutOfRange(subtree, parser.getOverrideDecisionRoot(), startIndex, stopTreeAt);
        trees.add(subtree);
    }
    return trees;
}
Also used : ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) ParserInterpreter(org.antlr.v4.runtime.ParserInterpreter) ArrayList(java.util.ArrayList) DecisionState(org.antlr.v4.runtime.atn.DecisionState) Interval(org.antlr.v4.runtime.misc.Interval)

Example 68 with EOF

use of org.antlr.v4.runtime.Recognizer.EOF in project antlr4 by tunnelvisionlabs.

the class TestIntervalSet method testSubtractFromSetWithEOF.

@Test
public void testSubtractFromSetWithEOF() throws Exception {
    IntervalSet s = IntervalSet.of(10, 20);
    s.add(Token.EOF);
    IntervalSet s2 = IntervalSet.of(12, 15);
    String expecting = "{<EOF>, 10..11, 16..20}";
    String result = (s.subtract(s2)).toString();
    assertEquals(expecting, result);
}
Also used : IntervalSet(org.antlr.v4.runtime.misc.IntervalSet) Test(org.junit.Test)

Example 69 with EOF

use of org.antlr.v4.runtime.Recognizer.EOF in project antlr4 by tunnelvisionlabs.

the class TestParserInterpreter method testEmptyRuleAfterEOFInChild.

@Test
public void testEmptyRuleAfterEOFInChild() throws Exception {
    LexerGrammar lg = new LexerGrammar("lexer grammar L;\n" + "A : 'a' ;\n");
    Grammar g = new Grammar("parser grammar T;\n" + "s : x y;\n" + "x : A EOF ;\n" + "y : ;", lg);
    ParseTree t = testInterp(lg, g, "s", "a", "(s (x a <EOF>) y)");
    // s
    assertEquals("0..1", t.getSourceInterval().toString());
    // x
    assertEquals("0..1", t.getChild(0).getSourceInterval().toString());
// unspecified		assertEquals("1..0", t.getChild(1).getSourceInterval().toString()); // y
}
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 70 with EOF

use of org.antlr.v4.runtime.Recognizer.EOF in project antlr4 by tunnelvisionlabs.

the class TestParserInterpreter method testEmptyInputWithCallsAfter.

@Test
public void testEmptyInputWithCallsAfter() throws Exception {
    LexerGrammar lg = new LexerGrammar("lexer grammar L;\n" + "A : 'a' ;\n");
    Grammar g = new Grammar("parser grammar T;\n" + "s : x y ;\n" + "x : EOF ;\n" + "y : z ;\n" + "z : ;", lg);
    ParseTree t = testInterp(lg, g, "s", "", "(s (x <EOF>) (y z))");
    // s
    assertEquals("0..0", t.getSourceInterval().toString());
    // x
    assertEquals("0..0", t.getChild(0).getSourceInterval().toString());
// unspecified		assertEquals("0..-1", t.getChild(1).getSourceInterval().toString()); // x
}
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)

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