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()));
}
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;
}
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);
}
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
}
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
}
Aggregations