Skip to content

Commit

Permalink
Merge pull request #45 from paulsemel/handle-recurisve-imports-from-d…
Browse files Browse the repository at this point in the history
…ifferent-directories

Handle recursive imports/includes from different directories
  • Loading branch information
ifratric authored Aug 29, 2024
2 parents 739329a + 7664c6f commit 053714b
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -931,16 +931,23 @@ def _include_from_string(self, grammar_str):
return num_errors

def _include_from_file(self, filename):
filepath = os.path.join(self._definitions_dir, filename)
try:
f = open(os.path.join(self._definitions_dir,
filename
))
f = open(filepath)
content = f.read()
f.close()
except IOError:
print('Error reading ' + filename)
return 1
return self.parse_from_string(content)

# we temporarily change the definitions dir to make it relative to the
# current file being parsed so that we can safely recursively
# include/import other files from it.
saved_definitions_dir = self._definitions_dir
self._definitions_dir = os.path.dirname(filepath)
errors = self.parse_from_string(content)
self._definitions_dir = saved_definitions_dir
return errors

def parse_from_string(self, grammar_str):
"""Parses grammar rules from string.
Expand Down

0 comments on commit 053714b

Please sign in to comment.