Skip to content

Commit

Permalink
Add some example contents
Browse files Browse the repository at this point in the history
  • Loading branch information
amykyta3 committed Jan 9, 2020
1 parent 0edac0e commit 02ed9d9
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "docs/speedy-antlr-example"]
path = docs/speedy-antlr-example
url = https://github.com/amykyta3/speedy-antlr-example.git
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,4 @@
# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ['_static']
html_static_path = []
96 changes: 95 additions & 1 deletion docs/example.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,98 @@ Tutorial
This section describes how to set up a fully-functional project using Speedy
Antlr. You can find `the completed example here. <https://github.com/amykyta3/speedy-antlr-example>`_

TODO: Write out the tutorial
In this example, a we will put together a fictional Python module called
``spam`` that implements an Antlr parser for a grammar called ``MyGrammar``.

generate_parsers.sh
-------------------

`src/spam/parser/generate_parsers.sh <https://github.com/amykyta3/speedy-antlr-example/blob/master/src/spam/parser/generate_parsers.sh>`_

This script is what the developer uses to re-generate Antlr targets, as well as
the Speedy Antlr accelerator files.

As usual, generate targets from your grammar file using Antlr. We want to
generate both Python and C++ targets since both will be used together.

.. literalinclude:: speedy-antlr-example/src/spam/parser/generate_parsers.sh
:language: bash
:lines: 4-10

After the targets are generated, invoke ``speedy-antlr-tool`` via Python to
generate the accelerator files.

.. literalinclude:: speedy-antlr-example/src/spam/parser/generate_parsers.sh
:language: bash
:lines: 12-20

You'll notice this last step generates the following files:

* sa_mygrammar.py
* cpp_src/sa_mygrammar_cpp_parser.cpp
* cpp_src/sa_mygrammar_translator.cpp/.h
* cpp_src/speedy_antlr.cpp/.h


sa_mygrammar.py
---------------

`src/spam/parser/sa_mygrammar.py <https://github.com/amykyta3/speedy-antlr-example/blob/master/src/spam/parser/sa_mygrammar.py>`_

TODO


print_tree.py
-------------

`src/spam/print_tree.py <https://github.com/amykyta3/speedy-antlr-example/blob/master/src/spam/print_tree.py>`_

Now let's use the resulting parser.

You can use the following boolean flag to detect whether the C++ accelerator
extension will be used. Using this flag is not necessary, but it can be
overridden to ``False`` if you want to force it to use the fall-back Python
parser.

.. literalinclude:: speedy-antlr-example/src/spam/print_tree.py
:language: python
:lines: 20-23

In some applications it is useful to intercept lex/parse syntax errors. The
Antlr runtime provides a mechanism to do so via the ``ErrorListener`` class.
Unfortunately since it is not practical to translate all Antlr C++ objects back
to Python, the usual error listener can not be used. Instead, an equivalent
``SA_ErrorListener`` is provided that provides a very similar interface.

Using the error listener is totally optional. If it is omitted, Antlr's default
ConsoleErrorListener is used.

For this example, let's define a pretty verbose listener:

.. literalinclude:: speedy-antlr-example/src/spam/print_tree.py
:language: python
:lines: 6-16

And finally put everything together:

.. literalinclude:: speedy-antlr-example/src/spam/print_tree.py
:language: python
:lines: 25-32


setup.py
--------

`setup.py <https://github.com/amykyta3/speedy-antlr-example/blob/master/setup.py>`_

TODO


LICENSE-3RD-PARTY
-----------------

`LICENSE-3RD-PARTY <https://github.com/amykyta3/speedy-antlr-example/blob/master/LICENSE-3RD-PARTY>`_

Since you'll be bundling the Antlr C++ runtime in your package's distribution
(source and binary), be a good steward of open-source software and include a
copy of Antlr's BSD license.
1 change: 1 addition & 0 deletions docs/speedy-antlr-example
Submodule speedy-antlr-example added at 677492

0 comments on commit 02ed9d9

Please sign in to comment.