[YANGTOOLS-28] New parser builder imports all yang files in directory Created: 30/Sep/13  Updated: 10/Apr/22  Resolved: 01/Oct/13

Status: Resolved
Project: yangtools
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Bug
Reporter: Christopher Hartley Assignee: Unassigned
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Operating System: Windows
Platform: PC


External issue ID: 91

 Description   

The new builder
public Set<org.opendaylight.yangtools.yang.model.api.Module> parseYangModels(File yangFile,File directory)
loads all yang files in the directory, not just those needed to parse the file.
This will not scale when we have large numbers of files.

Below shows the key code I used to build the list (I have omitted the antlr related code, but happy to attach that if it helps).

===============================================================
I'm not saying my code is any good, just showing it can be done.
===============================================================

>>> So my call in main was.
Set<Module> modules = parser.parseYangModels(getAllYangFiles(yangFileStr, dirStr));

>>>and here are the methods

// parse the imports in the main file
public List<File> getAllYangFiles(final String fileName, final String dirStr) throws IOException {
assert fileName != null;
assert dirStr != null;

imports = new HashSet<String>(127);
List<File> files = new ArrayList<File>();
files.add(new File(dirStr + File.separator + fileName + YANG_FILE_EXTENSION)); // add the main file

getYangImports(fileName, dirStr); // recursively get the file imports

Iterator<String> i = imports.iterator();

while (i.hasNext())

{ files.add(new File(dirStr + File.separator + i.next()+ YANG_FILE_EXTENSION)); }

return files;
}

// recursively parse the imported yang files
public void getYangImports(final String fileName, final String dirStr) throws IOException {
assert fileName != null;
assert dirStr != null;

Set<String> fileImports = getYangFileImports(fileName, dirStr);

if (!fileImports.isEmpty()) { // have some imports !
Iterator<String> i = fileImports.iterator();
String importStr;

while (i.hasNext()) {
importStr = i.next();
if (!imports.contains(importStr))

{ // a new file to add to the list imports.add(importStr); getYangImports(importStr, dirStr); // recursive goodness }

}
}
}

// get the import statements in each file
public Set<String> getYangFileImports(final String fileName, final String dirStr) throws IOException

{ assert fileName != null; assert dirStr != null; //lexer splits input into tokens YangLexer lexer = new YangLexer(new ANTLRFileStream(dirStr + File.separator + fileName + YANG_FILE_EXTENSION)); // need to use subclass here (not Lexer) //parser generates abstract syntax tree YangParser parser = new YangParser(new CommonTokenStream(lexer)); // need to use subclass here (not Parser) parser.setBuildParseTree(true); ParseTree tree = parser.module_stmt(); YangListener listener = new YangListener(); ParseTreeWalker.DEFAULT.walk(listener, tree); return listener.getImports(); }

 Comments   
Comment by Martin Vitez [ 01/Oct/13 ]

Fixed in yang-parser-impl version 0.5.9-SNAPSHOT.

Generated at Wed Feb 07 20:52:03 UTC 2024 using Jira 8.20.10#820010-sha1:ace47f9899e9ee25d7157d59aa17ab06aee30d3d.