use of org.antlr.v4.tool.Grammar in project antlr4 by antlr.
the class TestCompositeGrammars method testImportFileNotSearchedForInOutputDir.
@Test
public void testImportFileNotSearchedForInOutputDir() throws Exception {
String slave = "parser grammar S;\n" + "a : B {System.out.println(\"S.a\");} ;\n";
BaseRuntimeTest.mkdir(tmpdir);
String outdir = tmpdir + "/out";
BaseRuntimeTest.mkdir(outdir);
writeFile(outdir, "S.g4", slave);
String master = "grammar M;\n" + "import S;\n" + "s : a ;\n" + // defines B from inherited token space
"B : 'b' ;" + "WS : (' '|'\\n') -> skip ;\n";
writeFile(tmpdir, "M.g4", master);
ErrorQueue equeue = BaseRuntimeTest.antlrOnString(tmpdir, "Java", "M.g4", false, "-o", outdir);
assertEquals(ErrorType.CANNOT_FIND_IMPORTED_GRAMMAR, equeue.errors.get(0).getErrorType());
}
use of org.antlr.v4.tool.Grammar in project antlr4 by antlr.
the class TestCompositeGrammars method test3LevelImport.
// Make sure that M can import S that imports T.
@Test
public void test3LevelImport() throws Exception {
ErrorQueue equeue = new ErrorQueue();
String slave = "parser grammar T;\n" + "a : T ;\n";
BaseRuntimeTest.mkdir(tmpdir);
writeFile(tmpdir, "T.g4", slave);
String slave2 = "parser grammar S;\n" + "import T;\n" + "a : S ;\n";
BaseRuntimeTest.mkdir(tmpdir);
writeFile(tmpdir, "S.g4", slave2);
String master = "grammar M;\n" + "import S;\n" + "a : M ;\n";
writeFile(tmpdir, "M.g4", master);
Grammar g = new Grammar(tmpdir + "/M.g4", master, equeue);
// S and T aren't imported; overridden
String expectedTokenIDToTypeMap = "{EOF=-1, M=1}";
String expectedStringLiteralToTypeMap = "{}";
String expectedTypeToTokenList = "[M]";
assertEquals(expectedTokenIDToTypeMap, g.tokenNameToTypeMap.toString());
assertEquals(expectedStringLiteralToTypeMap, g.stringLiteralToTypeMap.toString());
assertEquals(expectedTypeToTokenList, realElements(g.typeToTokenList).toString());
assertEquals("unexpected errors: " + equeue, 0, equeue.errors.size());
boolean ok = rawGenerateAndBuildRecognizer("M.g4", master, "MParser", null);
// should be ok
boolean expecting = true;
assertEquals(expecting, ok);
}
use of org.antlr.v4.tool.Grammar in project antlr4 by antlr.
the class TestAmbigParseTrees method testParseDecisionWithinAmbiguousStartRule.
@Test
public void testParseDecisionWithinAmbiguousStartRule() throws Exception {
LexerGrammar lg = new LexerGrammar("lexer grammar L;\n" + "A : 'a' ;\n" + "B : 'b' ;\n" + "C : 'c' ;\n");
Grammar g = new Grammar("parser grammar T;\n" + "s : A x C" + " | A B C" + " ;" + "x : B ; \n", lg);
testInterpAtSpecificAlt(lg, g, "s", 1, "abc", "(s:1 a (x:1 b) c)");
testInterpAtSpecificAlt(lg, g, "s", 2, "abc", "(s:2 a b c)");
}
use of org.antlr.v4.tool.Grammar in project antlr4 by antlr.
the class TestAmbigParseTrees method testAmbiguousTrees.
public void testAmbiguousTrees(LexerGrammar lg, Grammar g, String startRule, String input, int decision, String expectedAmbigAlts, String overallTree, String[] expectedParseTrees) {
InterpreterTreeTextProvider nodeTextProvider = new InterpreterTreeTextProvider(g.getRuleNames());
LexerInterpreter lexEngine = lg.createLexerInterpreter(new ANTLRInputStream(input));
CommonTokenStream tokens = new CommonTokenStream(lexEngine);
final GrammarParserInterpreter parser = g.createGrammarParserInterpreter(tokens);
parser.setProfile(true);
parser.getInterpreter().setPredictionMode(PredictionMode.LL_EXACT_AMBIG_DETECTION);
// PARSE
int ruleIndex = g.rules.get(startRule).index;
ParserRuleContext parseTree = parser.parse(ruleIndex);
assertEquals(overallTree, Trees.toStringTree(parseTree, nodeTextProvider));
System.out.println();
DecisionInfo[] decisionInfo = parser.getParseInfo().getDecisionInfo();
List<AmbiguityInfo> ambiguities = decisionInfo[decision].ambiguities;
assertEquals(1, ambiguities.size());
AmbiguityInfo ambiguityInfo = ambiguities.get(0);
List<ParserRuleContext> ambiguousParseTrees = GrammarParserInterpreter.getAllPossibleParseTrees(g, parser, tokens, decision, ambiguityInfo.ambigAlts, ambiguityInfo.startIndex, ambiguityInfo.stopIndex, ruleIndex);
assertEquals(expectedAmbigAlts, ambiguityInfo.ambigAlts.toString());
assertEquals(ambiguityInfo.ambigAlts.cardinality(), ambiguousParseTrees.size());
for (int i = 0; i < ambiguousParseTrees.size(); i++) {
ParserRuleContext t = ambiguousParseTrees.get(i);
assertEquals(expectedParseTrees[i], Trees.toStringTree(t, nodeTextProvider));
}
}
use of org.antlr.v4.tool.Grammar in project antlr4 by antlr.
the class TestAmbigParseTrees method testAmbigAltDipsIntoOuterContextToRoot.
@Test
public void testAmbigAltDipsIntoOuterContextToRoot() throws Exception {
LexerGrammar lg = new LexerGrammar("lexer grammar L;\n" + "SELF : 'self' ;\n" + "ID : [a-z]+ ;\n" + "DOT : '.' ;\n");
Grammar g = new Grammar("parser grammar T;\n" + "e : p (DOT ID)* ;\n" + "p : SELF" + " | SELF DOT ID" + " ;", lg);
String startRule = "e";
String input = "self.x";
String expectedAmbigAlts = "{1, 2}";
// decision in p
int decision = 1;
String expectedOverallTree = "(e:1 (p:1 self) . x)";
String[] expectedParseTrees = { "(e:1 (p:1 self) . x)", "(p:2 self . x)" };
testAmbiguousTrees(lg, g, startRule, input, decision, expectedAmbigAlts, expectedOverallTree, expectedParseTrees);
}
Aggregations