Coverage Report

Created: 2026-10-28 06:06

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/antlr4rust-0.6.0/src/lib.rs
Line
Count
Source
1
#![crate_type = "lib"]
2
// #![warn(rust_2018_idioms)]
3
// #![warn(missing_docs)] // warn if there is missing docs
4
// #![warn(missing_debug_implementations)]
5
// #![warn(trivial_numeric_casts)]
6
7
//! # Antlr4 runtime
8
//!
9
//! This is a Rust runtime for [ANTLR4] parser generator.
10
//! It is required to use parsers and lexers generated by [ANTLR4] parser generator
11
//!
12
//! This documentation refers to particular api used by generated parsers,lexers and syntax trees.
13
//!
14
//! For info on what is [ANTLR4] and how to generate parser please refer to:
15
//!  - [ANTLR4] main repository
16
//!  - [README] for Rust target
17
//!
18
//! [ANTLR4]: https://github.com/antlr/antlr4
19
//! [README]: https://github.com/rrevenantt/antlr4rust/blob/master/README.md
20
//!
21
//! ### Customization
22
//!
23
//! All input and output can be customized and optimized for particular usecase by implementing
24
//! related trait. Each of them already has different implementations that should be enough for most cases.
25
//! For more details see docs for corresponding trait and containing module.
26
//!
27
//! Currently available are:
28
//!  - [`CharStream`] - Lexer input, stream of char values with slicing support
29
//!  - [`TokenFactory`] - How lexer creates tokens.
30
//!  - [`Token`] - Element of [`TokenStream`]
31
//!  - [`TokenStream`] - Parser input, created from lexer or other token source.
32
//!  - [`ParserRuleContext`] - Node of created syntax tree.
33
//!
34
//! ### Zero-copy and lifetimes
35
//!
36
//! This library supports full zero-copy parsing. To allow this
37
//! `'input` lifetime is used everywhere inside to refer to data borrowed by parser/lexer.
38
//! Besides references to input it also can be [`TokenFactory`] if it returns references to tokens.
39
//! See [`ArenaFactory`] as an example of such behavior.
40
//! It allocates tokens in [`Arena`](typed_arena::Arena) and returns references.
41
//!
42
//! Using generated parse tree you should be careful to not require longer lifetime after the parsing.
43
//! If that's the case you will likely get "does not live long enough" error on the input string,
44
//! despite actual lifetime conflict is happening much later
45
//!
46
//! If you need to generate owned versions of parse tree or you want simpler usage,
47
//! you can opt out zero-copy by requiring `'input` to be static. In this case it is easier to also use
48
//! types that contains "owned" in their name or constructor function like `OwningTokenFactory`
49
//! or `InputStream::new_owned()`.
50
//!
51
//! ### Visitors and Listeners
52
//!
53
//! Parse listeners must outlive `'input` because they have to be stored inside of the parser.
54
//! It still allows to retrieve borrowed data from parse tree which should be enough to cover 99% use cases.
55
//!
56
//! `ParseTreeWalker` can accept listeners with arbitrary lifetime.
57
//!
58
//! `Visitor`s also can have arbitrary lifetime.
59
//!
60
//! ### Downcasting
61
//!
62
//! Rule context trait object support downcasting even for zero-copy case.
63
//! Also generic types(currently these are `H:ErrorStrategy` and `I:`[`TokenStream`]) that you can
64
//! access in generated parser from embedded actions also can be downcasted to concrete types.
65
//! To do it [`TidExt::downcast_*`](crate::tid::TidExt) extension methods should be used.
66
//! [`std::any::Any`] cannot be used here because parse tree nodes borrow from the input;
67
//! see the [`tid`](mod@crate::tid) module for the `Any` equivalent that works for a single
68
//! lifetime.
69
//!
70
//! [`CharStream`]: crate::char_stream::CharStream
71
//! [`TokenFactory`]: crate::token_factory::TokenFactory
72
//! [`ArenaFactory`]: crate::token_factory::ArenaFactory
73
//! [`Token`]: crate::token::Token
74
//! [`TokenStream`]: crate::token_stream::TokenStream
75
//! [`ParserRuleContext`]: crate::parser_rule_context::ParserRuleContext
76
77
#[macro_use]
78
extern crate lazy_static;
79
80
#[doc(hidden)]
81
pub use lazy_static::lazy_static;
82
83
#[doc(hidden)]
84
pub use parking_lot::RwLock;
85
86
#[doc(hidden)]
87
pub use tid::{type_id, Tid, TidAble, TidExt};
88
89
#[doc(inline)]
90
pub use error_strategy::{BailErrorStrategy, DefaultErrorStrategy, ErrorStrategy};
91
92
pub use input_stream::InputStream;
93
94
#[doc(inline)]
95
pub use lexer::{BaseLexer, Lexer};
96
#[doc(inline)]
97
pub use parser::{BaseParser, ListenerId, Parser};
98
#[doc(inline)]
99
pub use token_source::TokenSource;
100
//extern crate uuid;
101
#[doc(hidden)]
102
pub use prediction_context::PredictionContextCache;
103
104
#[doc(inline)]
105
pub use prediction_mode::PredictionMode;
106
107
#[doc(hidden)]
108
pub mod atn_config;
109
#[doc(hidden)]
110
pub mod atn_simulator;
111
pub mod int_stream;
112
mod lexer_action;
113
mod ll1_analyzer;
114
#[doc(hidden)]
115
pub mod recognizer;
116
pub mod token_factory;
117
//pub mod tokenstream_rewriter;
118
#[doc(hidden)]
119
pub mod atn_deserialization_options;
120
#[doc(hidden)]
121
pub mod atn_state;
122
pub mod char_stream;
123
#[doc(hidden)]
124
pub mod dfa_state;
125
#[doc(hidden)]
126
pub mod interval_set;
127
pub mod parser_rule_context;
128
mod prediction_context;
129
#[doc(hidden)]
130
pub mod semantic_context;
131
mod token_source;
132
pub mod token_stream;
133
//pub mod trace_listener;
134
#[doc(hidden)]
135
pub mod dfa;
136
#[doc(hidden)]
137
pub mod transition;
138
pub mod tree;
139
//pub mod file_stream;
140
#[doc(hidden)]
141
pub mod atn;
142
#[doc(hidden)]
143
pub mod atn_config_set;
144
#[doc(hidden)]
145
pub mod atn_deserializer;
146
pub mod common_token_stream;
147
mod dfa_serializer;
148
pub mod error_listener;
149
pub mod error_strategy;
150
pub mod errors;
151
pub mod input_stream;
152
pub mod lexer;
153
#[doc(hidden)]
154
pub mod lexer_action_executor;
155
pub mod lexer_atn_simulator;
156
pub mod parser;
157
pub mod parser_atn_simulator;
158
mod prediction_mode;
159
pub mod token;
160
pub mod trees;
161
mod utils;
162
//pub mod tokenstream_rewriter_test;
163
mod atn_type;
164
pub mod tid;
165
// mod context_factory;
166
pub mod rule_context;
167
pub mod vocabulary;
168
//#[cfg(test)]
169
// tests are either integration tests in "tests" foulder or unit tests in some modules
170
171
use std::rc::Rc;
172
/// Stable workaround for CoerceUnsized
173
///
174
/// # Safety
175
///
176
/// Implementors must ensure that each method, if it returns, returns its argument converted
177
/// to `Self` by a coercion, such as the unsized coercion from `T` to `dyn Trait` that the
178
/// `coerce_from!` macro performs with `from as _`. The result must refer to the same `T` value
179
/// in the same allocation, taking over the argument's ownership (`coerce_rc`, `coerce_box`) or
180
/// borrow (`coerce_ref`, `coerce_mut`). It must not drop, move, replace or reallocate that
181
/// value.
182
///
183
/// [`BaseParser::remove_parse_listener`](crate::parser::BaseParser::remove_parse_listener)
184
/// relies on this: it turns the box that `add_parse_listener` stored back into the caller's
185
/// `Box<L>`, which is only sound if that box still owns the caller's value.
186
///
187
/// Implement this trait through `coerce_from!`. A safe `impl` is rejected:
188
///
189
/// ```compile_fail,E0200
190
/// use antlr4rust::CoerceFrom;
191
/// use std::rc::Rc;
192
///
193
/// trait Listener {}
194
/// struct NotAListener;
195
///
196
/// impl CoerceFrom<NotAListener> for dyn Listener {
197
///     fn coerce_rc(_: Rc<NotAListener>) -> Rc<Self> { unimplemented!() }
198
///     fn coerce_box(_: Box<NotAListener>) -> Box<Self> { unimplemented!() }
199
///     fn coerce_ref(_: &NotAListener) -> &Self { unimplemented!() }
200
///     fn coerce_mut(_: &mut NotAListener) -> &mut Self { unimplemented!() }
201
/// }
202
/// ```
203
// #[doc(hidden)]
204
pub unsafe trait CoerceFrom<T> {
205
    fn coerce_rc(from: Rc<T>) -> Rc<Self>;
206
    fn coerce_box(from: Box<T>) -> Box<Self>;
207
    fn coerce_ref(from: &T) -> &Self;
208
    fn coerce_mut(from: &mut T) -> &mut Self;
209
}
210
211
#[doc(hidden)]
212
#[macro_export]
213
macro_rules! coerce_from {
214
    ($lt:lifetime : $p:path) => {
215
        const _: () = {
216
            use std::rc::Rc;
217
            // SAFETY: `CoerceFrom` requires each method to return its argument coerced to
218
            // `Self`, still referring to the same value in the same allocation. Every body
219
            // below is `from as _` with `T: $p + $lt` and `Self = dyn $p + $lt`, which is the
220
            // unsized coercion of `Rc<T>`, `Box<T>`, `&T` or `&mut T` to the same pointer type
221
            // over `Self`: the pointee is unsized in place, so the result points to the same
222
            // `T` with `T`'s vtable attached, and nothing is dropped, moved or reallocated.
223
            unsafe impl<$lt, T> $crate::CoerceFrom<T> for dyn $p + $lt
224
            where
225
                T: $p + $lt,
226
            {
227
24.8M
                fn coerce_rc(from: Rc<T>) -> Rc<Self> {
228
24.8M
                    from as _
229
24.8M
                }
<dyn cel::parser::gen::celparser::CELParserContext<Ctx = cel::parser::gen::celparser::CELParserContextType, TF = antlr4rust::token_factory::CommonTokenFactory> as antlr4rust::CoerceFrom<antlr4rust::tree::LeafNode<cel::parser::gen::celparser::CELParserContextType, antlr4rust::tree::IsError>>>::coerce_rc
Line
Count
Source
227
15.9M
                fn coerce_rc(from: Rc<T>) -> Rc<Self> {
228
15.9M
                    from as _
229
15.9M
                }
<dyn cel::parser::gen::celparser::CELParserContext<Ctx = cel::parser::gen::celparser::CELParserContextType, TF = antlr4rust::token_factory::CommonTokenFactory> as antlr4rust::CoerceFrom<antlr4rust::tree::LeafNode<cel::parser::gen::celparser::CELParserContextType, antlr4rust::tree::NoError>>>::coerce_rc
Line
Count
Source
227
8.98M
                fn coerce_rc(from: Rc<T>) -> Rc<Self> {
228
8.98M
                    from as _
229
8.98M
                }
230
20.0k
                fn coerce_box(from: Box<T>) -> Box<Self> {
231
20.0k
                    from as _
232
20.0k
                }
233
                fn coerce_ref(from: &T) -> &Self {
234
                    from as _
235
                }
236
                fn coerce_mut(from: &mut T) -> &mut Self {
237
                    from as _
238
                }
239
            }
240
        };
241
    };
242
}
243
244
/// Stable workaround for CoerceUnsized
245
///
246
/// # Safety
247
///
248
/// The same as for [`CoerceFrom`], with `self` as the argument: implementors must ensure that
249
/// each method, if it returns, returns `self` converted to `T` by a coercion, referring to the
250
/// same value in the same allocation and taking over its ownership or borrow.
251
/// [`BaseParser::add_parse_listener`](crate::parser::BaseParser::add_parse_listener) stores
252
/// what `coerce_box_to` returns, and `remove_parse_listener` relies on it being the caller's
253
/// box.
254
///
255
/// This trait is implemented for every `X` where `T: CoerceFrom<X>`; implement [`CoerceFrom`]
256
/// instead. A safe `impl` is rejected:
257
///
258
/// ```compile_fail,E0200
259
/// use antlr4rust::CoerceTo;
260
/// use std::rc::Rc;
261
///
262
/// trait Listener {}
263
/// struct NotAListener;
264
///
265
/// impl CoerceTo<dyn Listener> for NotAListener {
266
///     fn coerce_rc_to(self: Rc<Self>) -> Rc<dyn Listener> { unimplemented!() }
267
///     fn coerce_box_to(self: Box<Self>) -> Box<dyn Listener> { unimplemented!() }
268
///     fn coerce_ref_to(&self) -> &(dyn Listener + 'static) { unimplemented!() }
269
///     fn coerce_mut_to(&mut self) -> &mut (dyn Listener + 'static) { unimplemented!() }
270
/// }
271
/// ```
272
// #[doc(hidden)]
273
pub unsafe trait CoerceTo<T: ?Sized> {
274
    fn coerce_rc_to(self: Rc<Self>) -> Rc<T>;
275
    fn coerce_box_to(self: Box<Self>) -> Box<T>;
276
    fn coerce_ref_to(&self) -> &T;
277
    fn coerce_mut_to(&mut self) -> &mut T;
278
}
279
280
// SAFETY: `CoerceTo` requires each method to return `self` coerced to `T`, still referring to
281
// the same value in the same allocation. Each method passes `self` to the matching method of
282
// `T: CoerceFrom<X>` and returns its result unchanged, and the `CoerceFrom` contract guarantees
283
// exactly that of the result.
284
unsafe impl<T: ?Sized, X> CoerceTo<T> for X
285
where
286
    T: CoerceFrom<X>,
287
{
288
24.8M
    fn coerce_rc_to(self: Rc<Self>) -> Rc<T> {
289
24.8M
        T::coerce_rc(self)
290
24.8M
    }
<antlr4rust::tree::LeafNode<cel::parser::gen::celparser::CELParserContextType, antlr4rust::tree::IsError> as antlr4rust::CoerceTo<dyn cel::parser::gen::celparser::CELParserContext<Ctx = cel::parser::gen::celparser::CELParserContextType, TF = antlr4rust::token_factory::CommonTokenFactory>>>::coerce_rc_to
Line
Count
Source
288
15.9M
    fn coerce_rc_to(self: Rc<Self>) -> Rc<T> {
289
15.9M
        T::coerce_rc(self)
290
15.9M
    }
<antlr4rust::tree::LeafNode<cel::parser::gen::celparser::CELParserContextType, antlr4rust::tree::NoError> as antlr4rust::CoerceTo<dyn cel::parser::gen::celparser::CELParserContext<Ctx = cel::parser::gen::celparser::CELParserContextType, TF = antlr4rust::token_factory::CommonTokenFactory>>>::coerce_rc_to
Line
Count
Source
288
8.98M
    fn coerce_rc_to(self: Rc<Self>) -> Rc<T> {
289
8.98M
        T::coerce_rc(self)
290
8.98M
    }
Unexecuted instantiation: <_ as antlr4rust::CoerceTo<_>>::coerce_rc_to
291
20.0k
    fn coerce_box_to(self: Box<Self>) -> Box<T> {
292
20.0k
        T::coerce_box(self)
293
20.0k
    }
<cel::parser::parser::RecursionListener as antlr4rust::CoerceTo<dyn cel::parser::gen::cellistener::CELListener>>::coerce_box_to
Line
Count
Source
291
20.0k
    fn coerce_box_to(self: Box<Self>) -> Box<T> {
292
20.0k
        T::coerce_box(self)
293
20.0k
    }
Unexecuted instantiation: <_ as antlr4rust::CoerceTo<_>>::coerce_box_to
294
295
0
    fn coerce_ref_to(&self) -> &T {
296
0
        T::coerce_ref(self)
297
0
    }
298
299
0
    fn coerce_mut_to(&mut self) -> &mut T {
300
0
        T::coerce_mut(self)
301
0
    }
302
}
303
304
pub mod const_check {
305
0
    pub const fn version(maj: &str, min: &str) -> bool {
306
0
        if maj.is_empty() || min.is_empty() {
307
0
            return false;
308
0
        }
309
0
        byte_eq(VERSION_MAJ.as_bytes(), maj.as_bytes())
310
0
            && byte_eq(VERSION_MIN.as_bytes(), min.as_bytes())
311
0
    }
312
313
    const VERSION_MAJ: &str = env!("CARGO_PKG_VERSION_MAJOR");
314
    const VERSION_MIN: &str = env!("CARGO_PKG_VERSION_MINOR");
315
316
0
    const fn byte_eq(lhs: &[u8], rhs: &[u8]) -> bool {
317
0
        if lhs.len() != rhs.len() {
318
0
            return false;
319
0
        }
320
0
        let mut i: usize = 0;
321
        loop {
322
0
            if i >= lhs.len() {
323
0
                return true;
324
0
            }
325
0
            if lhs[i] != rhs[i] {
326
0
                return false;
327
0
            }
328
0
            i += 1;
329
        }
330
0
    }
331
}