Skip to main content

Fast, C++-accelerated ANTLR Python runtime for target-agnostic grammars

Project description

antlrope

A fast, C++-accelerated ANTLR runtime for Python for target-agnostic grammars

antlrope = ANTLR + Ordered Parse Events — your parse delivered as one ordered stream of events, not a per-node tree walk.

10–20× faster than the official pure-Python antlr4-python3-runtime on workloads that touch most nodes — and more when your listener subscribes to only a subset of the grammar.

Parsing and reading a 2.6 MB SystemRDL file: antlrope is ~21x faster than the pure-Python runtime and ~8x faster than the speedy-antlr accelerator, at lower peak memory

Parsing + reading a real 2.6 MB SystemRDL file — see the full benchmark (vs the pure-Python runtime and the speedy-antlr accelerator).

Generate your parser with the ordinary ANTLR tool targeting Python, install this package, generate a small facade class, and write a pure-Python event listener. Parsing itself runs inside the official ANTLR4 C++ runtime, driven directly from the serialized ATN (Augmented Transition Network) the stock Python target already emits. Instead of a per-node parse-tree walk (one foreign-function crossing per tree node), the C++ side collects a single bulk, filtered event stream and hands it to Python in one transfer — and it drops the rules/tokens your listener doesn't subscribe to before they ever reach Python.

It complements, rather than replaces, the official antlr4-python3-runtime: same generated parser, a faster way to consume it.

Install

From PyPI:

pip install antlrope

Or from conda-forge (with conda, mamba, or pixi):

conda install -c conda-forge antlrope

Either way you get a pre-compiled binary — nothing to build — and the official antlr4-python3-runtime is pulled in automatically.

Quickstart

  1. Generate your parser with the stock ANTLR tool (Python target):

    antlr4 -Dlanguage=Python3 MyGrammar.g4 -o generated
    
  2. Generate the facade from the generated parser module:

    antlrope gen generated.MyGrammarParser MyGrammar -o my_listener.py
    

    This emits a MyGrammarEventListener base class with enter<Rule> / exit<Rule> / visitTerminal / visitError stubs and token-type constants. The facade also imports and bakes in your lexer/parser, so you never pass them at parse time. The lexer module is derived from the parser's by ANTLR's <Grammar>Lexer / <Grammar>Parser convention; pass --lexer if yours is named differently.

  3. Subclass it and override only the callbacks you care about:

    from my_listener import MyGrammarEventListener
    
    class Collector(MyGrammarEventListener):
        def enterPair(self) -> None:
            ...
        def visitTerminal(self, token_type: int, text: str) -> None:
            ...
    
    Collector().walk(source_text)
    

Only the callbacks you override drive native masks, so the C++ side skips every other rule/token — the fewer node kinds you subscribe to, the faster the walk.

How it works

  • The C++ extension deserializes the ATN from your generated lexer/parser and drives LexerInterpreter / ParserInterpreter — no generated C++ parser.
  • A native iterative depth-first traversal over the finished parse tree appends fixed (kind, payload, start, stop) int32 records to one buffer (parse_events).
  • kind: 0=ENTER_RULE, 1=EXIT_RULE, 2=TERMINAL, 3=ERROR; payload is the rule index or token type; start/stop are char indices into the source (-1 for rule events). Token text is recovered Python-side by slicing text[start:stop + 1] — no string copies cross the boundary.
  • Rule/token masks built from your overrides filter the stream in C++.

Limitations

This can only be used for target-language-agnostic grammars. This runtime executes the interpreted ATN; it cannot run target-language semantic predicates or embedded grammar actions. Grammars that depend on them will not parse correctly here. See docs/ for the full discussion and the performance characteristics of the bulk event-stream approach.

Contributing

Development setup, pixi environments, and the vendored-runtime workflow live in CONTRIBUTING.md.

Documentation

Full documentation: zuzukin.github.io/antlrope. In the repo, see docs/: getting started, installation, API reference, chunking, migrating from antlr4-python3-runtime, performance & limitations, how it works, and the SystemRDL benchmark.

Using an AI coding assistant to write a listener (or port a ParseTreeListener)? Give it the LLM-oriented docs summary at zuzukin.github.io/antlrope/llms.txt plus your grammar — and, when porting, your existing listener.

License

Apache-2.0. Bundles the ANTLR4 C++ runtime (BSD-3-Clause) under vendor/antlr4-cpp/; see vendor/antlr4-cpp/UPDATING.md for provenance.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

antlrope-1.0.0.tar.gz (439.7 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

antlrope-1.0.0-cp312-abi3-win_amd64.whl (286.4 kB view details)

Uploaded CPython 3.12+Windows x86-64

antlrope-1.0.0-cp312-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (436.4 kB view details)

Uploaded CPython 3.12+manylinux: glibc 2.17+ x86-64

antlrope-1.0.0-cp312-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (420.5 kB view details)

Uploaded CPython 3.12+manylinux: glibc 2.17+ ARM64

antlrope-1.0.0-cp312-abi3-macosx_11_0_x86_64.whl (369.4 kB view details)

Uploaded CPython 3.12+macOS 11.0+ x86-64

antlrope-1.0.0-cp312-abi3-macosx_11_0_arm64.whl (358.5 kB view details)

Uploaded CPython 3.12+macOS 11.0+ ARM64

File details

Details for the file antlrope-1.0.0.tar.gz.

File metadata

  • Download URL: antlrope-1.0.0.tar.gz
  • Upload date:
  • Size: 439.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for antlrope-1.0.0.tar.gz
Algorithm Hash digest
SHA256 87f318e6c188bc7058807b203e1e71117812bed114f76b79a8bd9043175b99a2
MD5 d3b29d232ca9efaec25048915805f532
BLAKE2b-256 60855a6619d28973355cda8ac346e9ed1717e1770e3f35a718dc8d59e2f01f64

See more details on using hashes here.

Provenance

The following attestation bundles were made for antlrope-1.0.0.tar.gz:

Publisher: wheels.yml on zuzukin/antlrope

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file antlrope-1.0.0-cp312-abi3-win_amd64.whl.

File metadata

  • Download URL: antlrope-1.0.0-cp312-abi3-win_amd64.whl
  • Upload date:
  • Size: 286.4 kB
  • Tags: CPython 3.12+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for antlrope-1.0.0-cp312-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 c2475df1862d54505d11c6fe3d5830bd94586f6d27215c17b42cb277e908a7bc
MD5 ff0a11d2707ce520fc2bc15fbfcd4edd
BLAKE2b-256 227bfcefc5e3b448936d6ac440ea15191da047a1dc2901c1294ddbcea7b575fa

See more details on using hashes here.

Provenance

The following attestation bundles were made for antlrope-1.0.0-cp312-abi3-win_amd64.whl:

Publisher: wheels.yml on zuzukin/antlrope

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file antlrope-1.0.0-cp312-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for antlrope-1.0.0-cp312-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 8c925feedb31b1f00e327172459bb0103567ece00dd2b335444f844a4e3e3177
MD5 afbfa0c3bb6a45e338b3fd452df870a9
BLAKE2b-256 853c0aac383d2965c311a2898f93bcd102ddac67ddd5d993b0c9c1a3cc6c5e32

See more details on using hashes here.

Provenance

The following attestation bundles were made for antlrope-1.0.0-cp312-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: wheels.yml on zuzukin/antlrope

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file antlrope-1.0.0-cp312-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for antlrope-1.0.0-cp312-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 72f948bcf518a8c7065926de49f8adcaaa9af9709aff652c3d4deab0e8694c72
MD5 546a96b849785341fd76fb106cc389e2
BLAKE2b-256 9ed5f0fa8bd565364fc8883f92cb201e244cdda82be00ec2ae26d810da8825ff

See more details on using hashes here.

Provenance

The following attestation bundles were made for antlrope-1.0.0-cp312-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: wheels.yml on zuzukin/antlrope

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file antlrope-1.0.0-cp312-abi3-macosx_11_0_x86_64.whl.

File metadata

File hashes

Hashes for antlrope-1.0.0-cp312-abi3-macosx_11_0_x86_64.whl
Algorithm Hash digest
SHA256 32e87f747e2c29dba6a0837d117f8f90f3764d3e01307d8ddb3cde5e920655c9
MD5 4a30a2fb63b7e8869ec007296fe8ddf7
BLAKE2b-256 f4380b2843fa3129b2404430bcb97d5397b314f15d5175c75f98b863c42958ca

See more details on using hashes here.

Provenance

The following attestation bundles were made for antlrope-1.0.0-cp312-abi3-macosx_11_0_x86_64.whl:

Publisher: wheels.yml on zuzukin/antlrope

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file antlrope-1.0.0-cp312-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for antlrope-1.0.0-cp312-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 1817233083b0b060654a4628aa59cc0a56da5e4e07d27cd2d08c79984e2770b2
MD5 31c2490e44db6ae4ab748d0b948a4b78
BLAKE2b-256 579df48734b0e527626063cf546e3da62f04e49d1b805a942da518617504c6ca

See more details on using hashes here.

Provenance

The following attestation bundles were made for antlrope-1.0.0-cp312-abi3-macosx_11_0_arm64.whl:

Publisher: wheels.yml on zuzukin/antlrope

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page