use of org.antlr.v4.runtime.Parser in project antlr4 by antlr.
the class TestExpectedTokens method testOptionalSubrule.
@Test
public void testOptionalSubrule() 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_4\n" + "BlockStart_4->s3\n" + "BlockStart_4->BlockEnd_5\n" + "s3-B->BlockEnd_5\n" + "BlockEnd_5->s6\n" + "s6-C->s7\n" + "s7->RuleStop_a_1\n" + "RuleStop_a_1-EOF->s8\n";
checkRuleATN(g, "a", atnText);
ATN atn = g.getATN();
int blkStartStateNumber = 4;
IntervalSet tokens = atn.getExpectedTokens(blkStartStateNumber, null);
assertEquals("{B, C}", tokens.toString(g.getTokenNames()));
}
use of org.antlr.v4.runtime.Parser in project antlr4 by antlr.
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.getTokenNames()));
}
use of org.antlr.v4.runtime.Parser in project antlr4 by antlr.
the class TestExpectedTokens method testFollowIncluded.
@Test
public void testFollowIncluded() throws Exception {
String gtext = "parser grammar T;\n" + "a : b A ;\n" + "b : B | ;";
Grammar g = new Grammar(gtext);
String atnText = "RuleStart_a_0->s4\n" + "s4-b->RuleStart_b_2\n" + "s5-A->s6\n" + "s6->RuleStop_a_1\n" + "RuleStop_a_1-EOF->s11\n";
checkRuleATN(g, "a", atnText);
atnText = "RuleStart_b_2->BlockStart_9\n" + "BlockStart_9->s7\n" + "BlockStart_9->s8\n" + "s7-B->BlockEnd_10\n" + "s8->BlockEnd_10\n" + "BlockEnd_10->RuleStop_b_3\n" + "RuleStop_b_3->s5\n";
checkRuleATN(g, "b", atnText);
ATN atn = g.getATN();
// From the start of 'b' with empty stack, can only see B and EOF
int blkStartStateNumber = 9;
IntervalSet tokens = atn.getExpectedTokens(blkStartStateNumber, RuleContext.EMPTY);
assertEquals("{<EOF>, B}", tokens.toString(g.getTokenNames()));
// Now call from 'a'
tokens = atn.getExpectedTokens(blkStartStateNumber, new ParserRuleContext(ParserRuleContext.EMPTY, 4));
assertEquals("{A, B}", tokens.toString(g.getTokenNames()));
}
use of org.antlr.v4.runtime.Parser in project antlr4 by antlr.
the class TestUnicodeGrammar method binaryGrammar.
@Test
public void binaryGrammar() throws Exception {
String grammarText = "grammar Binary;\n" + "r : HEADER PACKET+ FOOTER;\n" + "HEADER : '\\u0002\\u0000\\u0001\\u0007';\n" + "PACKET : '\\u00D0' ('\\u00D1' | '\\u00D2' | '\\u00D3') +;\n" + "FOOTER : '\\u00FF';\n";
byte[] toParse = new byte[] { (byte) 0x02, (byte) 0x00, (byte) 0x01, (byte) 0x07, (byte) 0xD0, (byte) 0xD2, (byte) 0xD2, (byte) 0xD3, (byte) 0xD3, (byte) 0xD3, (byte) 0xD0, (byte) 0xD3, (byte) 0xD3, (byte) 0xD1, (byte) 0xFF };
CharStream charStream;
try (ByteArrayInputStream is = new ByteArrayInputStream(toParse);
// U+0000 to U+00FF.
InputStreamReader isr = new InputStreamReader(is, StandardCharsets.ISO_8859_1)) {
charStream = new ANTLRInputStream(isr);
}
Grammar grammar = new Grammar(grammarText);
LexerInterpreter lexEngine = grammar.createLexerInterpreter(charStream);
CommonTokenStream tokens = new CommonTokenStream(lexEngine);
GrammarParserInterpreter parser = grammar.createGrammarParserInterpreter(tokens);
ParseTree parseTree = parser.parse(grammar.rules.get("r").index);
InterpreterTreeTextProvider nodeTextProvider = new InterpreterTreeTextProvider(grammar.getRuleNames());
String result = Trees.toStringTree(parseTree, nodeTextProvider);
assertEquals("(r:1 ÐÒÒÓÓÓ ÐÓÓÑ ÿ)", result);
}
use of org.antlr.v4.runtime.Parser in project antlr4 by antlr.
the class TestXPath method testError.
protected void testError(String input, String path, String expected, String startRuleName, String parserName, String lexerName) throws Exception {
Pair<Parser, Lexer> pl = getParserAndLexer(input, parserName, lexerName);
Parser parser = pl.a;
ParseTree tree = execStartRule(startRuleName, parser);
IllegalArgumentException e = null;
try {
XPath.findAll(tree, path, parser);
} catch (IllegalArgumentException iae) {
e = iae;
}
assertNotNull(e);
assertEquals(expected, e.getMessage());
}
Aggregations