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 = 8;
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    (7, $generator_version:literal) => {};
20    (8, $generator_version:literal) => {};
21    ($requested:literal, $generator_version:literal) => {
22        compile_error!(concat!(
23            "antlr4-rust generated-code API mismatch: antlr4-rust-gen v",
24            $generator_version,
25            " emitted generated-code API revision ",
26            stringify!($requested),
27            ", but the selected antlr-rust-runtime supports revisions 1, 2, 3, 4, 5, 6, 7, and 8; \
28             regenerate this recognizer with a compatible antlr4-rust-gen or select a compatible \
29             antlr-rust-runtime dependency"
30        ));
31    };
32}
33
34pub mod atn;
35pub mod byte_stream;
36pub mod char_stream;
37pub mod dfa;
38pub mod errors;
39pub mod generated;
40pub mod int_stream;
41pub mod lexer;
42pub mod parser;
43#[cfg(feature = "perf-counters")]
44pub mod perf;
45pub mod prediction;
46pub mod recognizer;
47pub mod semir;
48pub mod token;
49pub mod token_stream;
50pub mod tree;
51pub mod tree_pattern;
52pub mod vocabulary;
53pub mod xpath;
54
55pub use atn::parser::{ParserAtnPrediction, ParserAtnSimulator, ParserAtnSimulatorError};
56pub use byte_stream::ByteStream;
57pub use char_stream::{CharStream, InputStream, PositionSummary, TextInterval};
58pub use dfa::{DfaStateId, DfaTransition, ParserDfa, ParserDfaStateView, ParserDfaStats};
59pub use errors::{AntlrError, ConsoleErrorListener, ErrorListener, SyntaxErrorEvent};
60pub use generated::{GeneratedLexer, GeneratedParser, GrammarMetadata};
61pub use int_stream::{EOF, IntStream, UNKNOWN_SOURCE_NAME};
62pub use lexer::{
63    BaseLexer, Lexer, LexerCustomAction, LexerLifecycleCtx, LexerMode, LexerPredicate, LexerSemCtx,
64    LexerSemIrCtx, LexerSemanticAction, LexerSemanticPredicate, LexerSemantics,
65};
66pub use parser::{
67    BailErrorStrategy, BaseParser, EnterRuleEvent, ExpectedTokenSet, NoSemanticHooks,
68    ParseListener, Parser, ParserAction, ParserMemberAction, ParserPredicate, ParserReturnAction,
69    ParserRuleArg, ParserRuntimeOptions, ParserSemCtx, ParserSemanticAction,
70    ParserSemanticPredicate, ParserSemantics, PredictionMode, RecognitionArenaStats, SemanticHooks,
71    UnknownSemanticPolicy, grow_generated_rule_stack,
72};
73#[cfg(feature = "perf-counters")]
74pub use perf::{dump as dump_prediction_perf_counters, reset as reset_prediction_perf_counters};
75pub use prediction::{ContextId, PredictionContextStats, SemanticContext};
76pub use recognizer::{Recognizer, RecognizerData};
77pub use token::{
78    DEFAULT_CHANNEL, HIDDEN_CHANNEL, INVALID_TOKEN_TYPE, MAX_TOKEN_OFFSET, TOKEN_EOF, Token,
79    TokenChannel, TokenId, TokenIter, TokenSink, TokenSource, TokenSpec, TokenStore,
80    TokenStoreError, TokenView,
81};
82pub use token_stream::CommonTokenStream;
83pub use tree::{
84    AsRuleNode, ErrorNodeView, FromRuleNode, GeneratedAttrs, MissingChildError, Node, NodeChildren,
85    NodeId, NodeKind, ParseTree, ParseTreeDescendants, ParseTreeListener, ParseTreeStats,
86    ParseTreeStorage, ParseTreeVisitor, ParseTreeWalker, ParsedFile, ParserRuleContext,
87    RuleNodeView, TerminalNodeView,
88};
89pub use tree_pattern::{
90    ParseTreeMatch, ParseTreePattern, ParseTreePatternError, ParseTreePatternMatcher, PatternLexer,
91    lex_pattern_chunk,
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}