Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 19 additions & 10 deletions m2isar/frontends/coredsl2/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import logging
import pathlib
from typing import List

from .parser_gen import CoreDSL2Listener, CoreDSL2Parser, CoreDSL2Visitor
from .utils import make_parser
Expand Down Expand Up @@ -38,23 +39,25 @@ def enterImport_file(self, ctx: CoreDSL2Parser.Import_fileContext):
print(f"importing file {filename}")
self.got_new = True
self.imported.add(filename)
import_path = self.search_path / filename
assert import_path.is_file()

parser = make_parser(self.search_path/filename)
parser = make_parser(import_path)

tree = parser.description_content()

self.new_children.extend(tree.children)
self.new_defs.extend(tree.definitions)

def recursive_import(tree, search_path):
def recursive_import(tree, search_paths):
"""Helper method to recursively process all import statements of a given
parse tree. The search path should be set to the directory of the root document.
"""

path_extender = ImportPathExtender(search_path)
path_extender = ImportPathExtender(search_paths)
path_extender.visit(tree)

importer = VisitImporter(search_path)
importer = VisitImporter(search_paths)

while importer.got_new:
importer.new_imports.clear()
Expand All @@ -74,15 +77,15 @@ class VisitImporter(CoreDSL2Visitor):
to the import statements and stops traversion after that.
"""

def __init__(self, search_path) -> None:
def __init__(self, search_paths: List[pathlib.Path]) -> None:
super().__init__()
self.imported = set()
self.new_children = []
self.new_imports = []
self.new_defs = []
self.got_new = True
self.search_path = search_path
self.logger = logging.getLogger("visit_importer")
self.search_paths = search_paths

def visitDescription_content(self, ctx: CoreDSL2Parser.Description_contentContext):
for i in ctx.imports:
Expand All @@ -107,12 +110,13 @@ def visitImport_file(self, ctx: CoreDSL2Parser.Import_fileContext):
# extract file path and search path
file_path = pathlib.Path(filename)
file_dir = file_path.parent
assert file_path.is_file()

parser = make_parser(file_path)

# run ImportPathExtender on the new tree
tree = parser.description_content()
path_extender = ImportPathExtender(file_dir)
path_extender = ImportPathExtender([file_dir, *self.search_paths])
path_extender.visit(tree)

# keep track of the new children
Expand All @@ -126,14 +130,19 @@ class ImportPathExtender(CoreDSL2Visitor):
with their equivalent absolute path, relative to search_path.
"""

def __init__(self, search_path: pathlib.Path) -> None:
def __init__(self, search_paths: List[pathlib.Path]) -> None:
super().__init__()
self.search_path = search_path
self.search_paths = search_paths

def visitDescription_content(self, ctx: CoreDSL2Parser.Description_contentContext):
for i in ctx.imports:
self.visit(i)

def visitImport_file(self, ctx: CoreDSL2Parser.Import_fileContext):
filename = self.search_path / ctx.uri.text.replace('"', '')
fname = ctx.uri.text.replace('"', "")
found_paths = [x for x in self.search_paths if (x / fname).is_file()]
assert len(found_paths) > 0, f"{fname} not found in search paths: " + ", ".join(map(str, self.search_paths))
found_path = found_paths[0]
filename = found_path / fname
assert filename.is_file()
ctx.uri.text = str(filename)
9 changes: 6 additions & 3 deletions m2isar/frontends/coredsl2/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def main():
parser = argparse.ArgumentParser()
parser.add_argument("top_level", help="The top-level CoreDSL file.")
parser.add_argument("--log", default="info", choices=["critical", "error", "warning", "info", "debug"])
parser.add_argument("-I", dest="includes", action="append", default=[], help="Extra include directories")

args = parser.parse_args()

Expand All @@ -37,15 +38,17 @@ def main():

top_level = pathlib.Path(args.top_level)
abs_top_level = top_level.resolve()
search_path = abs_top_level.parent
extra_includes = args.includes
extra_includes = list(map(lambda x: x.resolve(), map(pathlib.Path, extra_includes)))
search_paths = [abs_top_level.parent] + extra_includes

parser = make_parser(abs_top_level)

try:
logger.info("parsing top level")
tree = parser.description_content()

recursive_import(tree, search_path)
recursive_import(tree, search_paths)
except M2SyntaxError as e:
logger.critical("Error during parsing: %s", e)
sys.exit(1)
Expand All @@ -58,7 +61,7 @@ def main():
logger.critical("Error during load order building: %s", e)
sys.exit(1)

model_path = search_path.joinpath('gen_model')
model_path = abs_top_level.parent.joinpath('gen_model')
model_path.mkdir(exist_ok=True)

temp_save = {}
Expand Down