Skip to main content

antlr4_runtime/
lib.rs

1// SPDX-License-Identifier: BSD-3-Clause
2// Copyright (c) 2026 Konstantin Vyatkin
3//! Clean-room ANTLR v4 runtime foundation for Rust.
4
5extern crate self as antlr4_runtime;
6
7/// Current generated-source/runtime contract revision emitted by the bundled generator.
8#[doc(hidden)]
9pub const __ANTLR4_RUST_CODEGEN_API: u32 = 13;
10
11/// Verifies that generated source is compatible with the selected runtime.
12#[doc(hidden)]
13#[macro_export]
14macro_rules! __antlr4_rust_require_codegen_api {
15    (13, $generator_version:literal) => {};
16    (12, $generator_version:literal) => {};
17    ($requested:literal, $generator_version:literal) => {
18        compile_error!(concat!(
19            "antlr4-rust generated-code API mismatch: antlr4-rust-gen v",
20            $generator_version,
21            " emitted generated-code API revision ",
22            stringify!($requested),
23            ", but the selected antlr-rust-runtime supports revisions 12 and 13; regenerate this \
24             recognizer with a compatible antlr4-rust-gen or \
25             select a compatible antlr-rust-runtime dependency"
26        ));
27    };
28}
29
30pub mod atn;
31pub mod byte_stream;
32pub mod char_stream;
33pub mod dfa;
34pub mod errors;
35pub mod generated;
36pub mod int_stream;
37pub mod lexer;
38pub mod parser;
39#[cfg(feature = "perf-counters")]
40pub mod perf;
41pub mod prediction;
42pub mod recognizer;
43pub mod semir;
44pub mod token;
45pub mod token_stream;
46pub mod tree;
47pub mod tree_pattern;
48pub mod validated;
49pub mod vocabulary;
50pub mod xpath;
51
52pub use atn::parser::{ParserAtnPrediction, ParserAtnSimulator, ParserAtnSimulatorError};
53pub use byte_stream::ByteStream;
54pub use char_stream::{CharStream, InputStream, PositionSummary, TextInterval};
55pub use dfa::{DfaStateId, DfaTransition, ParserDfa, ParserDfaStateView, ParserDfaStats};
56pub use errors::{AntlrError, ConsoleErrorListener, ErrorListener, SyntaxErrorEvent};
57pub use generated::{GeneratedLexer, GeneratedParseOutput, GeneratedParser, GrammarMetadata};
58pub use int_stream::{EOF, IntStream, UNKNOWN_SOURCE_NAME};
59pub use lexer::{
60    BaseLexer, Lexer, LexerCustomAction, LexerLifecycleCtx, LexerMode, LexerPredicate, LexerSemCtx,
61    LexerSemIrCtx, LexerSemanticAction, LexerSemanticPredicate, LexerSemantics,
62};
63pub use parser::{
64    BailErrorStrategy, BaseParser, EnterRuleEvent, ExpectedTokenSet, NoSemanticHooks,
65    ParseListener, Parser, ParserAction, ParserMemberAction, ParserPredicate, ParserReturnAction,
66    ParserRuleArg, ParserRuntimeOptions, ParserSemCtx, ParserSemanticAction,
67    ParserSemanticPredicate, ParserSemantics, PredictionMode, RecognitionArenaStats, SemanticHooks,
68    UnknownSemanticPolicy, grow_generated_rule_stack,
69};
70#[cfg(feature = "perf-counters")]
71pub use perf::{dump as dump_prediction_perf_counters, reset as reset_prediction_perf_counters};
72pub use prediction::{ContextId, PredictionContextStats, SemanticContext};
73pub use recognizer::{Recognizer, RecognizerData};
74pub use token::{
75    DEFAULT_CHANNEL, HIDDEN_CHANNEL, INVALID_TOKEN_TYPE, MAX_TOKEN_OFFSET, TOKEN_EOF, Token,
76    TokenChannel, TokenId, TokenIter, TokenSink, TokenSource, TokenSpec, TokenStore,
77    TokenStoreError, TokenView,
78};
79pub use token_stream::CommonTokenStream;
80pub use tree::{
81    AsRuleNode, ErrorNodeView, FromRuleNode, GeneratedAttrs, MissingChildError, Node, NodeChildren,
82    NodeId, NodeKind, ParseTree, ParseTreeDescendants, ParseTreeListener, ParseTreeStats,
83    ParseTreeStorage, ParseTreeVisitor, ParseTreeWalker, ParsedFile, ParserRuleContext,
84    RuleNodeView, TerminalNodeView,
85};
86pub use tree_pattern::{
87    ParseTreeMatch, ParseTreePattern, ParseTreePatternError, ParseTreePatternMatcher, PatternLexer,
88    lex_pattern_chunk,
89};
90pub use validated::{
91    FromValidatedRuleNode, ValidatedRuleNode, ValidatedTree, ValidationError, require_min_count,
92};
93pub use vocabulary::Vocabulary;
94pub use xpath::{XPath, XPathError};
95
96/// Formats a slice the way Java's `List.toString` does: `[a, b, c]`.
97///
98/// ANTLR's runtime-testsuite descriptors byte-compare output produced by
99/// Java's list rendering (`getRuleInvocationStack()`, token-getter lists).
100/// Rust's `Vec` `Debug` quotes elements, so — like Go's
101/// `antlr.PrintArrayJavaStyle` and Python's `str_list` — the Rust target
102/// exposes a dedicated formatter for generated test actions.
103pub fn java_style_list<T: std::fmt::Display>(items: &[T]) -> String {
104    let mut out = String::from("[");
105    for (index, item) in items.iter().enumerate() {
106        if index > 0 {
107            out.push_str(", ");
108        }
109        use std::fmt::Write;
110        write!(out, "{item}").expect("writing to a string cannot fail");
111    }
112    out.push(']');
113    out
114}