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