Skip to content

Commit

Permalink
Merge pull request #38 from ferraith/feature/grammar-filters
Browse files Browse the repository at this point in the history
Simplify filtering of grammars
  • Loading branch information
ferraith committed Jul 29, 2018
2 parents 213dc97 + c54ace5 commit c72a7af
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 22 deletions.
12 changes: 7 additions & 5 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ This is an extension for `setuptools <https://pypi.org/project/setuptools/>`__ i

All command line options of ANTLR are also available through the setuptools command. The user have the choice to pass the options on the command line or configure ANTLR in a dedicated section in the ``setup.cfg`` file.

ANTLR grammars and its dependencies like imported grammars or token files are automatically detected. For each root-level grammar a Python package will be generated during execution of the ``antlr`` command.
ANTLR grammars and its dependencies like imported grammars or token files are automatically detected. For each grammar a Python package will be generated during execution of the ``antlr`` command.

Installation
------------
Expand Down Expand Up @@ -114,7 +114,7 @@ See ``python setup.py antlr --help`` for available command line options:
...
Options for 'AntlrCommand' command:
--grammars (-g) specify grammars to generate parsers for
--output (-o) specify output directories where output is generated
--output (-o) specify directories where output is generated
--atn generate rule augmented transition network diagrams
--encoding specify grammar file encoding e.g. euc-jp
--message-format specify output style for messages in antlr, gnu, vs2005
Expand Down Expand Up @@ -142,8 +142,8 @@ Apart from passing options on the command line it's also possible to add a dedic
[antlr]
# Specify grammars to generate parsers for; default: None
#grammars = <root-level grammar> [<root-level-grammar> ...]
# Specify output directories where all output is generated; default: ./
#grammars = <grammar> [<grammar> ...]
# Specify directories where all output is generated; default: ./
output = default=gen
# Generate DOT graph files that represent the internal ATN data structures (yes|no); default: no
#atn = no
Expand Down Expand Up @@ -175,12 +175,14 @@ Apart from passing options on the command line it's also possible to add a dedic
# Dump lots of logging info to antlr-<timestamp>.log (yes|no); default: no
#x-log = no
A reference configuration is provided in the ``resources`` directory.

Example
*******

Alongside the ``setuptools-antlr`` source code a example project called ``foobar`` is provided in the ``example`` directory. This example consists of the two ANTLR grammars ``Foo`` and ``Bar``. During the execution of ``setuptools-antlr`` two Python packages will be generated into the ``foobar`` package directory containing a parser for each grammar.

To generate parsers for all root-level grammars and build a ``foobar`` wheel package execute the following command:
To generate parsers for both grammars and build a ``foobar`` wheel package execute the following command:

::

Expand Down
8 changes: 4 additions & 4 deletions example/setup.cfg
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
[antlr]
# Specify grammars to generate parsers for; default: None
#grammars = <root-level grammar> [<root-level-grammar> ...]
# Specify output directories where output is generated; default: ./
grammars = Foo Bar
# Specify directories where output is generated; default: ./
#output = [default=<output path>]
# [<root-level grammar>=<output path> ...]
# [<grammar>=<output path> ...]
# Generate DOT graph files that represent the internal ATN data structures (yes|no); default: no
#atn = no
# Specify grammar file encoding; default: utf-8
Expand All @@ -15,7 +15,7 @@
# Generate a parse tree listener (yes|no); default: yes
#listener = yes
# Generate parse tree visitor (yes|no); default: no
#visitor = no
visitor = yes
# Generate file dependencies (yes|no); default: no
#depend = no
# Set/override grammar-level options (<option>=<value> [<option>=value ...]); default: language=Python3
Expand Down
39 changes: 39 additions & 0 deletions resources/setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
[antlr]
# Specify grammars to generate parsers for; default: None
#grammars = <grammar> [grammar> ...]
# Specify directories where output is generated; default: ./
#output = [default=<output path>]
# [<grammar>=<output path> ...]
# Generate DOT graph files that represent the internal ATN data structures (yes|no); default: no
#atn = no
# Specify grammar file encoding; default: utf-8
#encoding = utf-8
# Specify output style for messages in antlr (antlr|gnu|vs2005); default: antlr
#message-format = antlr
# Show exception details when available for errors and warnings (yes|no); default: no
#long-messages = no
# Generate a parse tree listener (yes|no); default: yes
#listener = yes
# Generate parse tree visitor (yes|no); default: no
#visitor = no
# Generate file dependencies (yes|no); default: no
#depend = no
# Set/override grammar-level options (<option>=<value> [<option>=value ...]); default: language=Python3
# Documentation of all grammar-level options
# See https://github.com/antlr/antlr4/blob/master/doc/options.md
#grammar-options = superClass=<class>
# tokenVocab=<lexer>
# TokenLabelType=<class>
# contextSuperClass=<class>
# Treat warnings as errors (yes|no); default: no
#w-error = no
# Launch StringTemplate visualizer on generated code (yes|no); default: no
#x-dbg-st = no
# Wait for STViz to close before continuing
#x-dbg-st-wait = no
# Output goes into -o directories regardless of paths/package (yes|no); default: no
#x-exact-output-dir = yes
# Use the ATN simulator for all predictions (yes|no); default: no
#x-force-atn = no
# Dump lots of logging info to antlr-<timestamp>.log (yes|no); default: no
#x-log = no
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

setuptools.setup(
name='setuptools-antlr',
version='0.2.2',
version='0.3.0',
packages=setuptools.find_packages(),
package_data={'setuptools_antlr': ['lib/antlr-4.7.1-complete.jar', 'lib/LICENSE.txt']},
entry_points={
Expand Down
10 changes: 2 additions & 8 deletions setuptools_antlr/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class AntlrCommand(setuptools.Command):

user_options = [
('grammars=', 'g', 'specify grammars to generate parsers for'),
('output=', 'o', 'specify output directories where output is generated'),
('output=', 'o', 'specify directories where output is generated'),
('atn', None, 'generate rule augmented transition network diagrams'),
('encoding=', None, 'specify grammar file encoding e.g. euc-jp'),
('message-format=', None, 'specify output style for messages in antlr, gnu, vs2005'),
Expand Down Expand Up @@ -245,7 +245,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 @@ -270,7 +269,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 @@ -284,12 +282,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
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 c72a7af

Please sign in to comment.