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