|
16 | 16 | #![warn(trivial_numeric_casts)]
|
17 | 17 | //! # Antlr4 runtime
|
18 | 18 | //!
|
| 19 | +//! **This is pre-release version.** |
| 20 | +//! **Some small breaking changes are still possible, although none is currently planned** |
| 21 | +//! |
19 | 22 | //! This is a Rust runtime for [ANTLR4] parser generator.
|
20 | 23 | //! It is required to use parsers and lexers generated by [ANTLR4] parser generator
|
21 | 24 | //!
|
22 | 25 | //! This documentation refers to particular api used by generated parsers,lexers and syntax trees.
|
23 | 26 | //!
|
24 |
| -//! For info on how to generate parser please refer to: |
| 27 | +//! For info on what is [ANTLR4] and how to generate parser please refer to: |
25 | 28 | //! - [ANTLR4] main repository
|
26 |
| -//! - [README](https://github.com/rrevenantt/antlr4rust/blob/master/README.md) for Rust target |
| 29 | +//! - [README] for Rust target |
27 | 30 | //!
|
28 | 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. |
29 | 46 | //!
|
30 | 47 | //! ### Zero-copy and lifetimes
|
31 | 48 | //!
|
32 | 49 | //! This library supports full zero-copy parsing. To allow this
|
33 |
| -//! `'input` lifetime is used everywhere inside. |
| 50 | +//! `'input` lifetime is used everywhere inside to refer to data borrowed by parser. |
| 51 | +//! Besides reference to input it also can be [`TokenFactory`] if it returns references to tokens. |
| 52 | +//! See [`ArenaFactory`] as an example of such behavior. It allocates tokens in [`Arena`](typed_arena::Arena) and return references. |
| 53 | +//! |
| 54 | +//! Using generated parse tree you should be careful to not require longer lifetime after the parsing. |
| 55 | +//! If that's the case you will likely get "does not live long enough" error on the input string, |
| 56 | +//! despite actual lifetime conflict is happening much later |
34 | 57 | //!
|
35 |
| -//! Rust infers lifetimes from the end. It means that if something requires longer lifetime |
36 |
| -//! when you are using generated tree, then you will get error |
| 58 | +//! If you need to generate owned versions of parse tree or you want simpler usage, |
| 59 | +//! you can opt out zero-copy by requiring `'input` to be static. In this case it is easier to also use |
| 60 | +//! types that contains "owned" in their name or constructor function like `OwningTokenFactory` |
| 61 | +//! or `InputStream::new_owned()` |
37 | 62 | //!
|
| 63 | +//! ### Visitors and Listeners |
38 | 64 | //!
|
| 65 | +//! Currently visitors and listeners must outlive `'input`. |
| 66 | +//! In general this means that visitor has either `'static` or `'input` lifetime. |
| 67 | +//! Thus you can retrieve references to parsed data from syntax tree to listener/visitor. (as example you can see visitor test) |
| 68 | +//! |
| 69 | +//! You can try to give visitor outside references but in this case |
| 70 | +//! if those references do not outlive `'input` you will get very confusing error messages, |
| 71 | +//! so this is not recommended. |
| 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 |
39 | 86 |
|
40 | 87 | #[macro_use]
|
41 | 88 | extern crate lazy_static;
|
@@ -112,8 +159,7 @@ pub mod token;
|
112 | 159 | pub mod trees;
|
113 | 160 | mod utils;
|
114 | 161 | //pub mod tokenstream_rewriter_test;
|
115 |
| -#[doc(hidden)] |
116 |
| -pub mod atn_type; |
| 162 | +mod atn_type; |
117 | 163 | pub mod rule_context;
|
118 | 164 | pub mod vocabulary;
|
119 | 165 |
|
|
0 commit comments