nom_packrat/
lib.rs

1//! `nom-packrat` is an extension of [nom](https://docs.rs/nom) to apply "Packrat Parsing".
2//!
3//! ## Examples
4//!
5//! The following example show a quick example.
6//!
7//! ```
8//! use nom::character::complete::char;
9//! use nom::IResult;
10//! use nom_packrat::{init, packrat_parser, storage};
11//!
12//! // Declare storage used by packrat_parser
13//! storage!(String);
14//!
15//! // Apply packrat_parser by custom attribute
16//! #[packrat_parser]
17//! pub fn parser(s: &str) -> IResult<&str, String> {
18//!     let (s, x) = char('a')(s)?;
19//!     Ok((s, x.to_string()))
20//! }
21//!
22//! fn main() {
23//!     let input = "a";
24//!
25//!     // Initialize before parsing
26//!     init!();
27//!     let result = parser(input);
28//!
29//!     println!("{:?}", result);
30//! }
31//! ```
32
33extern crate nom_packrat_macros;
34#[doc(inline)]
35pub use nom_packrat_macros::packrat_parser;
36use std::collections::{HashMap, VecDeque};
37use std::hash::Hash;
38
39/// Initialize packrat storage
40///
41/// This must be called before each parsing.
42/// If this is not called, the parse result may be wrong.
43#[macro_export]
44macro_rules! init {
45    () => {
46        crate::PACKRAT_STORAGE.with(|storage| storage.borrow_mut().clear())
47    };
48}
49
50/// Declare packrat storage
51///
52/// # Arguments
53/// * An output type of parser. The type must implement `Clone`.
54/// * (Optional) An extra key type. The type must implement `Eq + Hash + Clone`.
55/// * (Optional) Maximum entries of storage.
56///
57/// # Examples
58///
59/// ```compile_fail
60/// storage!(String);
61/// storage!(String, 1024);
62/// storage!(String, ExtraInfo);
63/// storage!(String, ExtraInfo, 1024);
64/// ```
65#[macro_export]
66macro_rules! storage {
67    ($t:ty) => {
68        thread_local!(
69            pub(crate) static PACKRAT_STORAGE: core::cell::RefCell<
70                nom_packrat::PackratStorage<$t, ()>
71            > = {
72                core::cell::RefCell::new(nom_packrat::PackratStorage::new(None))
73            }
74        );
75    };
76    ($t:ty, $u:ty) => {
77        thread_local!(
78            pub(crate) static PACKRAT_STORAGE: core::cell::RefCell<
79                nom_packrat::PackratStorage<$t, $u>
80            > = {
81                core::cell::RefCell::new(nom_packrat::PackratStorage::new(None))
82            }
83        );
84    };
85    ($t:ty, $n:expr) => {
86        thread_local!(
87            pub(crate) static PACKRAT_STORAGE: core::cell::RefCell<
88                nom_packrat::PackratStorage<$t, ()>
89            > = {
90                core::cell::RefCell::new(nom_packrat::PackratStorage::new(Some($n)))
91            }
92        );
93    };
94    ($t:ty, $u:ty, $n:expr) => {
95        thread_local!(
96            pub(crate) static PACKRAT_STORAGE: core::cell::RefCell<
97                nom_packrat::PackratStorage<$t, $u>
98            > = {
99                core::cell::RefCell::new(nom_packrat::PackratStorage::new(Some($n)))
100            }
101        );
102    };
103}
104
105pub struct PackratStorage<T, U> {
106    size: Option<usize>,
107    map: HashMap<(&'static str, *const u8, U), Option<(T, usize)>>,
108    keys: VecDeque<(&'static str, *const u8, U)>,
109}
110
111impl<T, U> PackratStorage<T, U>
112where
113    U: Eq + Hash + Clone,
114{
115    pub fn new(size: Option<usize>) -> Self {
116        let init_size = size.unwrap_or_else(|| 0);
117        PackratStorage {
118            size,
119            map: HashMap::with_capacity(init_size),
120            keys: VecDeque::with_capacity(init_size),
121        }
122    }
123
124    pub fn get(&self, key: &(&'static str, *const u8, U)) -> Option<&Option<(T, usize)>> {
125        self.map.get(key)
126    }
127
128    pub fn insert(&mut self, key: (&'static str, *const u8, U), value: Option<(T, usize)>) {
129        if let Some(size) = self.size {
130            if self.keys.len() > size - 1 {
131                let key = self.keys.pop_front().unwrap();
132                self.map.remove(&key);
133            }
134        }
135
136        self.keys.push_back(key.clone());
137        self.map.insert(key, value);
138    }
139
140    pub fn clear(&mut self) {
141        self.map.clear();
142        self.keys.clear();
143    }
144}
145
146pub trait HasExtraState<T> {
147    fn get_extra_state(&self) -> T;
148}
149
150impl HasExtraState<()> for &str {
151    fn get_extra_state(&self) -> () {
152        ()
153    }
154}
155
156impl HasExtraState<()> for &[u8] {
157    fn get_extra_state(&self) -> () {
158        ()
159    }
160}
161
162impl<T> HasExtraState<()> for nom_locate::LocatedSpan<T, ()> {
163    fn get_extra_state(&self) -> () {
164        ()
165    }
166}
167
168impl<T, U, V> HasExtraState<T> for nom_locate::LocatedSpan<U, V>
169where
170    V: HasExtraState<T>,
171{
172    fn get_extra_state(&self) -> T {
173        self.extra.get_extra_state()
174    }
175}