Skip to content

Commit

Permalink
Fixed #2296
Browse files Browse the repository at this point in the history
Visited set is now passed across internal invocations of Grammar#loadImportedGrammars of each imported grammar, to retain the knowledge if imported sets.

Signed-off-by: aermicioi <alexandru.ermicioi@gmail.com>
  • Loading branch information
aermicioi authored and parrt committed Jul 5, 2022
1 parent 003bd34 commit 2349068
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions tool/src/org/antlr/v4/tool/Grammar.java
Original file line number Diff line number Diff line change
Expand Up @@ -371,11 +371,15 @@ protected void initTokenSymbolTables() {
typeToTokenList.add(null);
}

public void loadImportedGrammars() {

public void loadImportedGrammars() {
this.loadImportedGrammars(new HashSet<>());
}

private void loadImportedGrammars(Set<String> visited) {
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()) {
Expand Down Expand Up @@ -407,7 +411,7 @@ else if ( t.getType()==ANTLRParser.ID ) {
if ( g == null ) continue;
g.parent = this;
importedGrammars.add(g);
g.loadImportedGrammars(); // recursively pursue any imports in this import
g.loadImportedGrammars(visited); // recursively pursue any imports in this import
}
}

Expand Down

0 comments on commit 2349068

Please sign in to comment.