Skip to content

Commit

Permalink
#34: Moved filtering of root-level grammars to run method
Browse files Browse the repository at this point in the history
  • Loading branch information
ferraith committed Jul 28, 2018
1 parent 3d5460a commit 95faaad
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
17 changes: 8 additions & 9 deletions setuptools_antlr/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,6 @@ def _find_antlr_log(cls, log_path: pathlib.Path) -> pathlib.Path:

def _find_grammars(self, base_path: pathlib.Path=pathlib.Path('.')) -> typing.List[AntlrGrammar]:
"""Searches for all ANTLR grammars starting from base directory and returns a list of it.
Only grammars which aren't included by other grammars are part of this list.
:param base_path: base path to search for ANTLR grammars
:return: a list of all found ANTLR grammars
Expand All @@ -272,7 +271,6 @@ def get_grammar(name: str) -> AntlrGrammar:
grammars.append(AntlrGrammar(pathlib.Path(root, fb)))

# generate a dependency tree for each grammar
grammar_tree = []
try:
for grammar in grammars:
imports = grammar.read_imports()
Expand All @@ -286,12 +284,8 @@ def get_grammar(name: str) -> AntlrGrammar:
raise distutils.errors.DistutilsFileError('Imported grammar "{}" in file "{}" isn\'t '
'present in package source directory.'.format(
str(e), str(e.parent.path)))
else:
# remove all grammars which aren't the root of a dependency tree
grammar_tree[:] = filter(lambda r: all(r not in g.dependencies for g in grammars),
grammars)

return grammar_tree
return grammars

@classmethod
def _create_init_file(cls, path: pathlib.Path) -> bool:
Expand Down Expand Up @@ -320,10 +314,15 @@ def run(self):
if not antlr_jar:
raise distutils.errors.DistutilsExecError('no ANTLR jar was found in lib directory')

# find grammars and filter result if grammars are passed by user
# find grammars
grammars = self._find_grammars()

# remove all grammars which aren't the root of a dependency tree
grammars[:] = filter(lambda r: all(r not in g.dependencies for g in grammars), grammars)

# filter found grammars if grammars are passed by user
if self.grammars:
grammars = filter(lambda g: g.name in self.grammars, grammars)
grammars[:] = filter(lambda g: g.name in self.grammars, grammars)

# generate parser for each grammar
for grammar in grammars:
Expand Down
18 changes: 14 additions & 4 deletions test/test_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,25 @@ def test_find_grammars_standalone(self, command):
def test_find_grammars_distributed(self, command):
g = command._find_grammars(pathlib.Path('distributed'))

assert len(g) == 1
assert g[0].name == 'SomeGrammar'
assert len(g) == 3

assert g[0].name == 'CommonTerminals'
d = g[0].dependencies
assert len(d) == 0

assert g[1].name == 'SharedRules'
d = g[1].dependencies
assert len(d) == 1
assert d[0].name == 'CommonTerminals'

assert g[2].name == 'SomeGrammar'
d = g[2].dependencies
assert len(d) == 2
assert d[0].name == 'CommonTerminals'
assert d[1].name == 'SharedRules'
dd = g[0].dependencies[0].dependencies
dd = g[2].dependencies[0].dependencies
assert len(dd) == 0
dd = g[0].dependencies[1].dependencies
dd = g[2].dependencies[1].dependencies
assert len(dd) == 1
assert dd[0].name == 'CommonTerminals'

Expand Down

0 comments on commit 95faaad

Please sign in to comment.