drmfinlay / pyjsgf

JSpeech Grammar Format (JSGF) compiler, matcher and parser package for Python.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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:

  1. Check if the Import is currently being resolved and raise an error if it is (circular import resolution).
  2. Check if the Import has already been resolved and use the appropriate Grammar object instead of parsing it from a file. Skip to 7.
  3. Otherwise check if there is a grammar file called com.example.grammar.jsgf in the working directory.
  4. If not, then use os.walk to find the com directory (if it exists), collect files below the directory and check if com/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.*>;.
  5. Raise an error if neither the grammar.jsgf or com.example.grammar.jsgf files were found.
  6. Parse the file that was found using parse_grammar_file and get a Grammar object.
  7. Resolve any import statements in the new Grammar object. Pass a list of Import objects currently being resolved to detect circular importing and a list of already resolved Imports for optimisation.
  8. Add the new Grammar object to an imported_grammars list in the grammar it was imported from.
  9. 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.