do_not_use_antlr_rust/
lib.rs

1#![crate_type = "lib"]
2// #![feature(try_blocks)]
3//#![feature(nll)]
4// #![feature(raw)]
5// #![feature(is_sorted)]
6// #![feature(cell_update)]
7// #![feature(get_mut_unchecked)]
8// #![feature(specialization)]
9// #![feature(coerce_unsized)]
10// #![feature(associated_type_defaults)]
11// #![feature(generic_associated_types)]
12// #![feature(crate_visibility_modifier)]
13// #![feature(generic_associated_types)]
14#![warn(rust_2018_idioms)]
15#![warn(missing_docs)] // warn if there is missing docs
16#![warn(missing_debug_implementations)]
17#![warn(trivial_numeric_casts)]
18// #![allow(incomplete_features)]
19
20//! # Antlr4 runtime
21//!
22//! This is a Rust runtime for [ANTLR4] parser generator.
23//! It is required to use parsers and lexers generated by [ANTLR4] parser generator
24//!
25//! This documentation refers to particular api used by generated parsers,lexers and syntax trees.
26//!
27//! For info on what is [ANTLR4] and how to generate parser please refer to:
28//!  - [ANTLR4] main repository
29//!  - [README] for Rust target
30//!
31//! [ANTLR4]: https://github.com/antlr/antlr4
32//! [README]: https://github.com/rrevenantt/antlr4rust/blob/master/README.md
33//!
34//! ### Customization
35//!
36//! All input and output can be customized and optimized for particular usecase by implementing
37//! related trait. Each of them already has different implementations that should be enough for most cases.
38//! For more details see docs for corresponding trait and containing module.
39//!
40//! Currently available are:
41//!  - [`CharStream`] - Lexer input, stream of char values with slicing support
42//!  - [`TokenFactory`] - How lexer creates tokens.
43//!  - [`Token`] - Element of [`TokenStream`]
44//!  - [`TokenStream`] - Parser input, created from lexer or other token source.
45//!  - [`ParserRuleContext`] - Node of created syntax tree.
46//!
47//! ### Zero-copy and lifetimes
48//!
49//! This library supports full zero-copy parsing. To allow this
50//! `'input` lifetime is used everywhere inside to refer to data borrowed by parser/lexer.
51//! Besides references to input it also can be [`TokenFactory`] if it returns references to tokens.
52//! See [`ArenaFactory`] as an example of such behavior.
53//! It allocates tokens in [`Arena`](typed_arena::Arena) and returns references.
54//!
55//! Using generated parse tree you should be careful to not require longer lifetime after the parsing.
56//! If that's the case you will likely get "does not live long enough" error on the input string,
57//! despite actual lifetime conflict is happening much later
58//!
59//! If you need to generate owned versions of parse tree or you want simpler usage,
60//! you can opt out zero-copy by requiring `'input` to be static. In this case it is easier to also use
61//! types that contains "owned" in their name or constructor function like `OwningTokenFactory`
62//! or `InputStream::new_owned()`.
63//!
64//! ### Visitors and Listeners
65//!
66//! Parse listeners must outlive `'input` because they have to be stored inside of the parser.
67//! It still allows to retrieve borrowed data from parse tree which should be enough to cover 99% use cases.
68//!
69//! `ParseTreeWalker` can accept listeners with arbitrary lifetime.
70//!
71//! `Visitor`s also can have arbitrary lifetime.
72//!
73//! ### Downcasting
74//!
75//! Rule context trait object support downcasting even for zero-copy case.
76//! Also generic types(currently these are `H:ErrorStrategy` and `I:`[`TokenStream`]) that you can
77//! access in generated parser from embedded actions also can be downcasted to concrete types.
78//! To do it `TidExt::downcast_*` extension methods should be used.
79//!
80//! [`CharStream`]: crate::char_stream::CharStream
81//! [`TokenFactory`]: crate::token_factory::TokenFactory
82//! [`ArenaFactory`]: crate::token_factory::ArenaFactory
83//! [`Token`]: crate::token::Token
84//! [`TokenStream`]: crate::token_stream::TokenStream
85//! [`ParserRuleContext`]: crate::parser_rule_context::ParserRuleContext
86
87#[macro_use]
88extern crate lazy_static;
89
90#[doc(hidden)]
91pub use lazy_static::lazy_static;
92
93#[doc(hidden)]
94pub use parking_lot::RwLock;
95
96#[doc(hidden)]
97pub use better_any::{tid, Tid, TidAble, TidExt};
98
99#[doc(inline)]
100pub use error_strategy::{BailErrorStrategy, DefaultErrorStrategy, ErrorStrategy};
101
102pub use input_stream::InputStream;
103
104#[doc(inline)]
105pub use lexer::{BaseLexer, Lexer};
106#[doc(inline)]
107pub use parser::{BaseParser, ListenerId, Parser};
108#[doc(inline)]
109pub use token_source::TokenSource;
110//extern crate uuid;
111#[doc(hidden)]
112pub use prediction_context::PredictionContextCache;
113
114#[doc(inline)]
115pub use prediction_mode::PredictionMode;
116
117#[doc(hidden)]
118pub mod atn_config;
119#[doc(hidden)]
120pub mod atn_simulator;
121pub mod int_stream;
122mod lexer_action;
123mod ll1_analyzer;
124#[doc(hidden)]
125pub mod recognizer;
126pub mod token_factory;
127//pub mod tokenstream_rewriter;
128#[doc(hidden)]
129pub mod atn_deserialization_options;
130#[doc(hidden)]
131pub mod atn_state;
132pub mod char_stream;
133#[doc(hidden)]
134pub mod dfa_state;
135#[doc(hidden)]
136pub mod interval_set;
137pub mod parser_rule_context;
138mod prediction_context;
139#[doc(hidden)]
140pub mod semantic_context;
141mod token_source;
142pub mod token_stream;
143//pub mod trace_listener;
144#[doc(hidden)]
145pub mod dfa;
146#[doc(hidden)]
147pub mod transition;
148pub mod tree;
149//pub mod file_stream;
150#[doc(hidden)]
151pub mod atn;
152#[doc(hidden)]
153pub mod atn_config_set;
154#[doc(hidden)]
155pub mod atn_deserializer;
156pub mod common_token_stream;
157mod dfa_serializer;
158pub mod error_listener;
159pub mod error_strategy;
160pub mod errors;
161pub mod input_stream;
162pub mod lexer;
163#[doc(hidden)]
164pub mod lexer_action_executor;
165pub mod lexer_atn_simulator;
166pub mod parser;
167pub mod parser_atn_simulator;
168mod prediction_mode;
169pub mod token;
170pub mod trees;
171mod utils;
172//pub mod tokenstream_rewriter_test;
173mod atn_type;
174// mod context_factory;
175pub mod rule_context;
176pub mod vocabulary;
177//#[cfg(test)]
178// tests are either integration tests in "tests" foulder or unit tests in some modules
179
180use std::rc::Rc;
181/// Stable workaround for CoerceUnsized
182// #[doc(hidden)]
183pub trait CoerceFrom<T> {
184    fn coerce_rc(from: Rc<T>) -> Rc<Self>;
185    fn coerce_box(from: Box<T>) -> Box<Self>;
186    fn coerce_ref(from: &T) -> &Self;
187    fn coerce_mut(from: &mut T) -> &mut Self;
188}
189
190#[doc(hidden)]
191#[macro_export]
192macro_rules! coerce_from {
193    ($lt:lifetime : $p:path) => {
194        const _: () = {
195            use std::rc::Rc;
196            impl<$lt, T> $crate::CoerceFrom<T> for dyn $p + $lt
197            where
198                T: $p + $lt,
199            {
200                fn coerce_rc(from: Rc<T>) -> Rc<Self> { from as _ }
201                fn coerce_box(from: Box<T>) -> Box<Self> { from as _ }
202                fn coerce_ref(from: &T) -> &Self { from as _ }
203                fn coerce_mut(from: &mut T) -> &mut Self { from as _ }
204            }
205        };
206    };
207}
208
209/// Stable workaround for CoerceUnsized
210// #[doc(hidden)]
211pub trait CoerceTo<T: ?Sized> {
212    fn coerce_rc_to(self: Rc<Self>) -> Rc<T>;
213    fn coerce_box_to(self: Box<Self>) -> Box<T>;
214    fn coerce_ref_to(self: &Self) -> &T;
215    fn coerce_mut_to(self: &mut Self) -> &mut T;
216}
217
218impl<T: ?Sized, X> CoerceTo<T> for X
219where
220    T: CoerceFrom<X>,
221{
222    fn coerce_rc_to(self: Rc<Self>) -> Rc<T> { T::coerce_rc(self) }
223    fn coerce_box_to(self: Box<Self>) -> Box<T> { T::coerce_box(self) }
224
225    fn coerce_ref_to(self: &Self) -> &T { T::coerce_ref(self) }
226
227    fn coerce_mut_to(self: &mut Self) -> &mut T { T::coerce_mut(self) }
228}