Import resolution
drmfinlay opened this issue · comments
Currently Import
objects added to grammars are compiled but do nothing else. It would be nice if imports were resolved prior to matching so that matching works as expected with cross-grammar references. This is much easier now that the parser is implemented.
The import resolver could check if the specified grammar file exists in a folder hierarchy or as a file. For example, the procedure for the import statement import <com.example.grammar.*>;
would be:
- Check if the
Import
is currently being resolved and raise an error if it is (circular import resolution). - Check if the
Import
has already been resolved and use the appropriateGrammar
object instead of parsing it from a file. Skip to 7. - Otherwise check if there is a grammar file called
com.example.grammar.jsgf
in the working directory. - If not, then use
os.walk
to find thecom
directory (if it exists), collect files below the directory and check ifcom/example/grammar.jsgf
was collected.- This should not be done if the import statement doesn't use a qualified name, e.g. something like
import <grammar.*>;
.
- This should not be done if the import statement doesn't use a qualified name, e.g. something like
- Raise an error if neither the
grammar.jsgf
orcom.example.grammar.jsgf
files were found. - Parse the file that was found using
parse_grammar_file
and get aGrammar
object. - Resolve any import statements in the new
Grammar
object. Pass a list ofImport
objects currently being resolved to detect circular importing and a list of already resolvedImports
for optimisation. - Add the new
Grammar
object to animported_grammars
list in the grammar it was imported from. - After all import statements are resolved, disable public rules in the new
Grammar
object(s) that were not imported. Not necessary in this case because of the.*
in the import statement.
After all this is accomplished, rules that reference a rule in another grammar should find the referenced rule using the imported_grammars
list. If the list doesn't contain the required Grammar
object, the relevant Import
should be resolved. An error should be raised if the import resolution fails.