def clear_grammar_cache(self): """ Since grammars are just Python modules, Python will cache them in `sys.modules` once they've been imported. This function will clear the cache so changes in grammars can be reloaded. """ grammars = utilities.get_grammar_names() for grammar in grammars: module = ".".join(["grammars", grammar]) del sys.modules[module]
def load_grammars(self): """ Loads each compiled grammar from the subdirectories of the grammars directory. """ grammar_names = utilities.get_grammar_names() for grammar_name in grammar_names: module = ".".join(["grammars", grammar_name]) grammar = importlib.import_module(module) self.grammars[grammar_name] = grammar.grammar utilities.log("Compiling grammar: '" + grammar_name + "'", verbose=True) self.compiled_grammars[grammar_name] = globals.Compiler.compile(parser.parse(grammar.grammar))