Added preparsing logic and recursion checking
This commit is contained in:
parent
bc28c78a48
commit
2b057796ba
|
|
@ -10,10 +10,13 @@ class PreParser:
|
||||||
"""
|
"""
|
||||||
Takes a path and environment, executes all pre-parsing methods on the specified file.
|
Takes a path and environment, executes all pre-parsing methods on the specified file.
|
||||||
"""
|
"""
|
||||||
def __init__(self, path, __env):
|
def __init__(self, path, __env, list_of_paths=[], dict_of_imports={}):
|
||||||
self.path = path
|
self.path = path
|
||||||
self.__env = __env
|
self.__env = __env
|
||||||
self.path_resolved = pr.PathResolver(path)
|
self.path_resolved = pr.PathResolver(path)
|
||||||
|
self.list_of_paths = list_of_paths + [self.path_resolved] # List of files in a branch of imports until its end
|
||||||
|
self.global_dict_of_imports = dict_of_imports # Dictionary of all imports made to avoid duplicate file opening
|
||||||
|
self.local_dict_of_imports = {}
|
||||||
|
|
||||||
def open(self):
|
def open(self):
|
||||||
pass
|
pass
|
||||||
|
|
@ -26,10 +29,15 @@ class PreParser:
|
||||||
"""
|
"""
|
||||||
import_list = self.parse_import_list()
|
import_list = self.parse_import_list()
|
||||||
for e in import_list:
|
for e in import_list:
|
||||||
pp = PreParser(e, self.__env)
|
if e in self.list_of_paths:
|
||||||
pp.parse_import_list()
|
raise RecursionError("Error: {} was imported earlier in {}".format(e, self.list_of_paths))
|
||||||
# todo: add to import log list
|
if e in self.global_dict_of_imports:
|
||||||
# todo: check for recursion errors
|
pp = self.global_dict_of_imports[e]
|
||||||
|
else:
|
||||||
|
pp = PreParser(e, self.__env, self.list_of_paths.copy(), self.global_dict_of_imports)
|
||||||
|
self.global_dict_of_imports[e] = pp
|
||||||
|
pp.parse_import_list()
|
||||||
|
return
|
||||||
|
|
||||||
def parse_import_list(self):
|
def parse_import_list(self):
|
||||||
"""
|
"""
|
||||||
|
|
@ -55,4 +63,5 @@ class PreParser:
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
site_path = "../../../example_userfiles/index.bpr"
|
site_path = "../../../example_userfiles/index.bpr"
|
||||||
__env = environment.Environment()
|
__env = environment.Environment()
|
||||||
# rich.inspect(__env)
|
michel = PreParser(site_path, __env)
|
||||||
|
rich.inspect(michel)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue