Skip to content

Commit

Permalink
Fix max recursion
Browse files Browse the repository at this point in the history
  • Loading branch information
loicgasser committed Aug 24, 2017
1 parent 43bc151 commit d092b77
Showing 1 changed file with 3 additions and 10 deletions.
13 changes: 3 additions & 10 deletions mappyfile/parser.py
Expand Up @@ -11,7 +11,6 @@ def __init__(self, expand_includes=True):
self.expand_includes = expand_includes
self.lalr = self._create_lalr_parser()
self.earley = None # only load this grammar as required
self._nested_include = 0

def load_grammar(self, grammar_file):
gf = os.path.join(os.path.dirname(__file__), grammar_file)
Expand All @@ -29,19 +28,15 @@ def _strip_quotes(self, s):
s = s[:s.index('#')] if '#' in s else s
return s.strip("'").strip('"')

def load_includes(self, text, fn=None):
def load_includes(self, text, fn=None, _nested_includes=0):
# Per default use working directory of the process
if fn is None:
fn = os.getcwd()
lines = text.split('\n')
includes = {}
include_discovered = False
for idx, l in enumerate(lines):
if l.strip().lower().startswith("include"):
if not include_discovered:
include_discovered = True
self._nested_include += 1
if self._nested_include > 5:
if _nested_includes == 5:
raise Exception("Maximum nested include exceeded! (MaxNested=5)")

inc, inc_file_path = l.split()
Expand All @@ -54,7 +49,7 @@ def load_includes(self, text, fn=None):
logging.warning("Include file '%s' not found", inc_file_path)
raise ex
# recursively load any further includes
includes[idx] = self.load_includes(include_text, fn=inc_file_path)
includes[idx] = self.load_includes(include_text, fn=inc_file_path, _nested_includes=_nested_includes+1)

for idx, txt in includes.items():
lines.pop(idx) # remove the original include
Expand All @@ -71,7 +66,6 @@ def open_file(self, fn):
raise

def parse_file(self, fn):
self._nested_include = 0
text = self.open_file(fn)
return self.parse(text, fn=fn)

Expand All @@ -80,7 +74,6 @@ def parse(self, text, fn=None):
Parse the Mapfile, using one of three options, from the quickest (LALR)
to slowest, but handles all cases
"""
self._nested_include = 0

if self.expand_includes:
text = self.load_includes(text, fn=fn)
Expand Down

0 comments on commit d092b77

Please sign in to comment.