gramma/
lib.rs

1#![doc = include_str!("crate.md")]
2#![no_std]
3extern crate alloc;
4extern crate either;
5#[cfg(feature = "regex")]
6extern crate once_cell;
7#[cfg(feature = "regex")]
8extern crate regex;
9
10#[doc(hidden)]
11pub use either::Either;
12#[cfg(all(feature = "regex", feature = "std"))]
13#[doc(hidden)]
14pub use once_cell::sync::Lazy;
15#[cfg(feature = "regex")]
16#[doc(hidden)]
17pub use regex::Regex;
18
19#[macro_use]
20pub(crate) mod utils;
21pub mod ast;
22pub mod parse;
23pub mod token;
24pub(crate) mod internal_prelude {
25    pub use alloc::{boxed::Box, vec::Vec};
26}
27pub mod error;
28pub mod string_matcher;
29
30pub use ast::{display_tree, parse_tree, Rule};
31pub use parse::ParseError;
32pub use token::Token;
33
34#[cfg(feature = "regex")]
35#[doc(hidden)]
36#[macro_export]
37macro_rules! _lazy_regex {
38    ($vis:vis static ref $NAME:ident => $regex:expr;) => {
39        $vis static $NAME: $crate::Lazy<$crate::Regex> = $crate::Lazy::new(|| $crate::Regex::new($regex).unwrap());
40    };
41}
42
43#[cfg(all(feature = "regex", not(feature = "std")))]
44mod _lazy {
45    pub struct Lazy<T> {
46        inner: once_cell::race::OnceBox<T>,
47        init: fn() -> T,
48    }
49
50    impl<T> Lazy<T> {
51        pub const fn new(init: fn() -> T) -> Self {
52            Self {
53                inner: once_cell::race::OnceBox::new(),
54                init,
55            }
56        }
57    }
58
59    impl<T> core::ops::Deref for Lazy<T> {
60        type Target = T;
61
62        fn deref(&self) -> &Self::Target {
63            self.inner.get_or_init(|| (self.init)().into())
64        }
65    }
66}
67
68#[cfg(all(feature = "regex", not(feature = "std")))]
69#[doc(hidden)]
70pub use _lazy::Lazy;