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