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" ; "WS : (' '|'\\n') -> skip ;\n" ;
writeFile(tmpdir, "M.g4", master); writeFile(tmpdir, "M.g4", master);
ErrorQueue equeue = antlr("M.g4", false, "-lib", subdir); 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 { @Test public void testDelegatesSeeSameTokenType() throws Exception {

View File

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