antlr4rust
The ANTLR4 parser runtime for Rust — power parsers generated from .g4 grammars with idiomatic Rust parse trees.
Repository Health
Technical Analysis
antlr4rust is the ANTLR4 runtime library for the Rust programming language. ANTLR (ANother Tool for Language Recognition) is a widely used parser generator: from a grammar file it produces a parser that builds parse trees and listener/visitor interfaces. antlr4rust provides the runtime that those generated Rust parsers depend on.
You use the ANTLR4 tool (a JAR) to generate Rust parser sources from a .g4 grammar, add antlr4rust as a Cargo dependency, and the generated code links against this runtime to tokenize input, build parse trees, and drive listeners or visitors. It supports idiomatic Rust syntax trees via ANTLR’s label feature and works on stable Rust.
What You Get
- The ANTLR4 runtime types (lexer, parser, token streams, ATN interpreter) for generated Rust parsers
- Parse-tree, listener, and visitor infrastructure for walking recognized input
- Support for idiomatic Rust syntax trees via ANTLR’s label feature
- A
build.rsintegration pattern to regenerate parsers when grammars change - Compatibility with stable Rust (since version 0.3)
Common Use Cases
- Building a parser for a custom domain-specific language in Rust from a .g4 grammar
- Porting an existing ANTLR grammar to a Rust project
- Implementing interpreters, transpilers, or linters that need a real parse tree
- Processing structured text or config formats with grammar-driven parsing
Under The Hood
Architecture - The crate implements ANTLR’s runtime model in Rust: an ATN (Augmented Transition Network) interpreter drives lexers and parsers, token streams feed the parser, and generated context structs form a parse tree that can be traversed with listeners or visitors. ANTLR’s label feature maps grammar alternatives to Rust enums and struct fields, yielding idiomatic, matchable trees. The ANTLR4 tool JAR (built via Maven or downloaded) generates the Rust sources that consume this runtime. Tech Stack - Rust (edition 2021, MSRV 1.80) with dependencies including lazy_static/once_cell for statics, typed-arena and better_any for arena-allocated tree nodes, bit-set, byteorder, murmur3, and parking_lot. Code Quality - The runtime ships example grammars, generated code under tests/gen, and integration tests (tests/my_test.rs); it is an actively revived fork picking up the original @rrevenantt work, with modest but real maintenance. API Design - Most of the surface is consumed indirectly through generated code, so the ergonomics come from ANTLR’s well-established listener/visitor patterns rendered in Rust; the main friction is the two-step workflow of running the Java tool then compiling, which the documented build.rs setup smooths over.