This commit is contained in:
parrt 2016-11-21 13:45:05 -08:00
parent 39ddd0da3a
commit 4e6e7edf0d
2 changed files with 18 additions and 1 deletions

View File

@ -69,7 +69,19 @@ public class TestCompositeGrammars extends BaseJavaTest {
"WS : (' '|'\\n') -> skip ;\n" ;
writeFile(tmpdir, "M.g4", master);
ErrorQueue equeue = antlr("M.g4", false, "-lib", subdir);
assertEquals(equeue.size(), 0);
assertEquals(0, equeue.size());
}
// Test for https://github.com/antlr/antlr4/issues/1317
@Test public void testImportSelfLoop() throws Exception {
mkdir(tmpdir);
String master =
"grammar M;\n" +
"import M;\n" +
"s : 'a' ;\n";
writeFile(tmpdir, "M.g4", master);
ErrorQueue equeue = antlr("M.g4", false, "-lib", tmpdir);
assertEquals(0, equeue.size());
}
@Test public void testDelegatesSeeSameTokenType() throws Exception {

View File

@ -392,6 +392,8 @@ public class Grammar implements AttributeResolver {
if ( ast==null ) return;
GrammarAST i = (GrammarAST)ast.getFirstChildWithType(ANTLRParser.IMPORT);
if ( i==null ) return;
Set<String> visited = new HashSet<>();
visited.add(this.name);
importedGrammars = new ArrayList<Grammar>();
for (Object c : i.getChildren()) {
GrammarAST t = (GrammarAST)c;
@ -403,6 +405,9 @@ public class Grammar implements AttributeResolver {
else if ( t.getType()==ANTLRParser.ID ) {
importedGrammarName = t.getText();
}
if ( visited.contains(importedGrammarName) ) { // ignore circular refs
continue;
}
Grammar g;
try {
g = tool.loadImportedGrammar(this, t);