antlr4rust/
lib.rs

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_*` extension methods should be used.
66//!
67//! [`CharStream`]: crate::char_stream::CharStream
68//! [`TokenFactory`]: crate::token_factory::TokenFactory
69//! [`ArenaFactory`]: crate::token_factory::ArenaFactory
70//! [`Token`]: crate::token::Token
71//! [`TokenStream`]: crate::token_stream::TokenStream
72//! [`ParserRuleContext`]: crate::parser_rule_context::ParserRuleContext
73
74#[macro_use]
75extern crate lazy_static;
76
77#[doc(hidden)]
78pub use lazy_static::lazy_static;
79
80#[doc(hidden)]
81pub use parking_lot::RwLock;
82
83#[doc(hidden)]
84pub use better_any::{tid, Tid, TidAble, TidExt};
85
86#[doc(inline)]
87pub use error_strategy::{BailErrorStrategy, DefaultErrorStrategy, ErrorStrategy};
88
89pub use input_stream::InputStream;
90
91#[doc(inline)]
92pub use lexer::{BaseLexer, Lexer};
93#[doc(inline)]
94pub use parser::{BaseParser, ListenerId, Parser};
95#[doc(inline)]
96pub use token_source::TokenSource;
97//extern crate uuid;
98#[doc(hidden)]
99pub use prediction_context::PredictionContextCache;
100
101#[doc(inline)]
102pub use prediction_mode::PredictionMode;
103
104#[doc(hidden)]
105pub mod atn_config;
106#[doc(hidden)]
107pub mod atn_simulator;
108pub mod int_stream;
109mod lexer_action;
110mod ll1_analyzer;
111#[doc(hidden)]
112pub mod recognizer;
113pub mod token_factory;
114//pub mod tokenstream_rewriter;
115#[doc(hidden)]
116pub mod atn_deserialization_options;
117#[doc(hidden)]
118pub mod atn_state;
119pub mod char_stream;
120#[doc(hidden)]
121pub mod dfa_state;
122#[doc(hidden)]
123pub mod interval_set;
124pub mod parser_rule_context;
125mod prediction_context;
126#[doc(hidden)]
127pub mod semantic_context;
128mod token_source;
129pub mod token_stream;
130//pub mod trace_listener;
131#[doc(hidden)]
132pub mod dfa;
133#[doc(hidden)]
134pub mod transition;
135pub mod tree;
136//pub mod file_stream;
137#[doc(hidden)]
138pub mod atn;
139#[doc(hidden)]
140pub mod atn_config_set;
141#[doc(hidden)]
142pub mod atn_deserializer;
143pub mod common_token_stream;
144mod dfa_serializer;
145pub mod error_listener;
146pub mod error_strategy;
147pub mod errors;
148pub mod input_stream;
149pub mod lexer;
150#[doc(hidden)]
151pub mod lexer_action_executor;
152pub mod lexer_atn_simulator;
153pub mod parser;
154pub mod parser_atn_simulator;
155mod prediction_mode;
156pub mod token;
157pub mod trees;
158mod utils;
159//pub mod tokenstream_rewriter_test;
160mod atn_type;
161// mod context_factory;
162pub mod rule_context;
163pub mod vocabulary;
164//#[cfg(test)]
165// tests are either integration tests in "tests" foulder or unit tests in some modules
166
167use std::rc::Rc;
168/// Stable workaround for CoerceUnsized
169// #[doc(hidden)]
170pub trait CoerceFrom<T> {
171    fn coerce_rc(from: Rc<T>) -> Rc<Self>;
172    fn coerce_box(from: Box<T>) -> Box<Self>;
173    fn coerce_ref(from: &T) -> &Self;
174    fn coerce_mut(from: &mut T) -> &mut Self;
175}
176
177#[doc(hidden)]
178#[macro_export]
179macro_rules! coerce_from {
180    ($lt:lifetime : $p:path) => {
181        const _: () = {
182            use std::rc::Rc;
183            impl<$lt, T> $crate::CoerceFrom<T> for dyn $p + $lt
184            where
185                T: $p + $lt,
186            {
187                fn coerce_rc(from: Rc<T>) -> Rc<Self> {
188                    from as _
189                }
190                fn coerce_box(from: Box<T>) -> Box<Self> {
191                    from as _
192                }
193                fn coerce_ref(from: &T) -> &Self {
194                    from as _
195                }
196                fn coerce_mut(from: &mut T) -> &mut Self {
197                    from as _
198                }
199            }
200        };
201    };
202}
203
204/// Stable workaround for CoerceUnsized
205// #[doc(hidden)]
206pub trait CoerceTo<T: ?Sized> {
207    fn coerce_rc_to(self: Rc<Self>) -> Rc<T>;
208    fn coerce_box_to(self: Box<Self>) -> Box<T>;
209    fn coerce_ref_to(&self) -> &T;
210    fn coerce_mut_to(&mut self) -> &mut T;
211}
212
213impl<T: ?Sized, X> CoerceTo<T> for X
214where
215    T: CoerceFrom<X>,
216{
217    fn coerce_rc_to(self: Rc<Self>) -> Rc<T> {
218        T::coerce_rc(self)
219    }
220    fn coerce_box_to(self: Box<Self>) -> Box<T> {
221        T::coerce_box(self)
222    }
223
224    fn coerce_ref_to(&self) -> &T {
225        T::coerce_ref(self)
226    }
227
228    fn coerce_mut_to(&mut self) -> &mut T {
229        T::coerce_mut(self)
230    }
231}