Skip to main content

antlr4_runtime/
lib.rs

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