1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
pub use nom_recursive_macros::recursive_parser;
use std::collections::HashMap;
#[cfg(all(not(feature = "tracer128"), not(feature = "tracer256"),))]
const RECURSIVE_FLAG_WORDS: usize = 1;
#[cfg(all(feature = "tracer128", not(feature = "tracer256"),))]
const RECURSIVE_FLAG_WORDS: usize = 2;
#[cfg(feature = "tracer256")]
const RECURSIVE_FLAG_WORDS: usize = 4;
pub struct RecursiveIndexes {
indexes: HashMap<&'static str, usize>,
next: usize,
}
impl RecursiveIndexes {
pub fn new() -> Self {
RecursiveIndexes {
indexes: HashMap::new(),
next: 0,
}
}
pub fn get(&mut self, key: &'static str) -> usize {
if let Some(x) = self.indexes.get(key) {
*x
} else {
let new_index = self.next;
assert!(new_index < RECURSIVE_FLAG_WORDS * 64, "Recursive tracers exceed the maximum number({}). Consider use feature `tracer128` or `tracer256` to extend it.", RECURSIVE_FLAG_WORDS * 64);
self.next += 1;
self.indexes.insert(key, new_index);
new_index
}
}
}
thread_local!(
pub static RECURSIVE_STORAGE: core::cell::RefCell<crate::RecursiveIndexes> = {
core::cell::RefCell::new(crate::RecursiveIndexes::new())
}
);
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct RecursiveInfo {
flag: [u64; RECURSIVE_FLAG_WORDS],
ptr: *const u8,
}
impl Default for RecursiveInfo {
fn default() -> Self {
Self::new()
}
}
impl RecursiveInfo {
pub fn new() -> Self {
RecursiveInfo {
flag: [0; RECURSIVE_FLAG_WORDS],
ptr: std::ptr::null(),
}
}
pub fn check_flag(&self, id: usize) -> bool {
let upper = id / 64;
let lower = id % 64;
((self.flag[upper] >> lower) & 1) == 1
}
pub fn set_flag(&mut self, id: usize) {
let upper = id / 64;
let lower = id % 64;
let val = 1u64 << lower;
let mask = !(1u64 << lower);
self.flag[upper] = (self.flag[upper] & mask) | val;
}
pub fn clear_flags(&mut self) {
for i in 0..self.flag.len() {
self.flag[i] = 0u64;
}
}
pub fn get_ptr(&self) -> *const u8 {
self.ptr
}
pub fn set_ptr(&mut self, ptr: *const u8) {
self.ptr = ptr;
}
}
pub trait HasRecursiveInfo {
fn get_recursive_info(&self) -> RecursiveInfo;
fn set_recursive_info(self, info: RecursiveInfo) -> Self;
}
impl HasRecursiveInfo for RecursiveInfo {
fn get_recursive_info(&self) -> RecursiveInfo {
*self
}
fn set_recursive_info(self, info: RecursiveInfo) -> Self {
info
}
}
impl<T, U> HasRecursiveInfo for nom_locate1::LocatedSpanEx<T, U>
where
U: HasRecursiveInfo,
{
fn get_recursive_info(&self) -> RecursiveInfo {
self.extra.get_recursive_info()
}
fn set_recursive_info(mut self, info: RecursiveInfo) -> Self {
self.extra = self.extra.set_recursive_info(info);
self
}
}
impl<T, U> HasRecursiveInfo for nom_locate::LocatedSpan<T, U>
where
U: HasRecursiveInfo,
{
fn get_recursive_info(&self) -> RecursiveInfo {
self.extra.get_recursive_info()
}
fn set_recursive_info(mut self, info: RecursiveInfo) -> Self {
self.extra = self.extra.set_recursive_info(info);
self
}
}