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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
//! `IntStream` extension for Lexer that allows subslicing of underlying data
use std::borrow::Cow;
use std::char::REPLACEMENT_CHARACTER;
use std::convert::TryFrom;
use std::fmt::Debug;
use std::ops::{Index, Range, RangeFrom};
use crate::int_stream::IntStream;
/// Provides underlying data for Tokens.
pub trait CharStream<'input>: IntStream {
/// Returns underlying data piece, either slice or owned copy.
/// Panics if provided indexes are invalid
/// Called by parser only on token intervals.
/// This fact can be used by custom implementations
fn get_text(&self, a: isize, b: isize) -> Cow<'input, str>;
/// Whether [`CharStream::item_at`] is usable on this stream. `false`
/// (the default) means the stream only supports cursor-based
/// `la`/`consume` access; the table-driven lexer walker then uses the
/// (slower) cursor path. A method (not an associated const) to keep
/// `CharStream` dyn-compatible; inlines to a constant in the
/// monomorphized walker.
#[doc(hidden)]
#[inline]
fn has_item_at(&self) -> bool {
false
}
/// Random access to the underlying data: the code point starting at
/// index `index`, or `None` at/past end of input. Only usable when
/// [`CharStream::has_item_at`] is true; the default (never called then)
/// returns `None`. Lets the table-driven lexer walker hold a local
/// cursor and peek the next input item directly, instead of a `la(1)`
/// decode plus a `consume()` walk per char.
#[doc(hidden)]
#[inline]
fn item_at(&self, index: isize) -> Option<i32> {
let _ = index;
None
}
/// Random access to the raw input bytes from byte index `index` to end
/// of input; `None` when the stream has no byte view (the default) or
/// `index` is out of bounds. Only usable when [`CharStream::has_item_at`]
/// is true. Lets the table-driven lexer walker run vectorized scans
/// over the self-loop runs marked in the static lexer tables.
#[doc(hidden)]
#[inline]
fn bytes_at(&self, index: isize) -> Option<&'input [u8]> {
let _ = index;
None
}
#[doc(hidden)]
#[inline]
fn get_data_str(&self) -> Option<&'input str> {
None
}
#[doc(hidden)]
#[inline]
fn get_line(&self, byte_offset: isize) -> u32 {
let end = byte_offset.max(0).min(self.size());
let prefix = self.get_text(0, end);
let bytes = prefix.as_bytes();
let len = (byte_offset.max(0) as usize).min(bytes.len());
1 + memchr::memchr_iter(b'\n', &bytes[..len]).count() as u32
}
#[doc(hidden)]
#[inline]
fn get_char_position_in_line(&self, byte_offset: isize) -> i32 {
let end = byte_offset.max(0).min(self.size());
let prefix = self.get_text(0, end);
let bytes = prefix.as_bytes();
let len = (byte_offset.max(0) as usize).min(bytes.len());
let line_start = memchr::memrchr(b'\n', &bytes[..len]).map_or(0, |i| i + 1);
prefix[line_start..len].chars().count() as i32
}
}
#[allow(clippy::len_without_is_empty)]
/// Trait for input that can be accepted by `InputStream` to be able to provide lexer with data.
/// Public for implementation reasons.
pub trait InputData:
Index<Range<usize>, Output = Self>
+ Index<RangeFrom<usize>, Output = Self>
+ ToOwned
+ Debug
+ 'static
{
// fn to_indexed_vec(&self) -> Vec<(u32, u32)>;
#[doc(hidden)]
fn offset(&self, index: isize, item_offset: isize) -> Option<isize>;
#[doc(hidden)]
fn item(&self, index: isize) -> Option<i32>;
#[doc(hidden)]
fn len(&self) -> usize;
#[doc(hidden)]
fn from_text(text: &str) -> Self::Owned;
#[doc(hidden)]
fn to_display(&self) -> String;
}
impl<T: Into<u32> + From<u8> + TryFrom<u32> + Copy + Debug + 'static> InputData for [T]
where
<T as TryFrom<u32>>::Error: Debug,
{
// fn to_indexed_vec(&self) -> Vec<(u32, u32)> {
// self.into_iter()
// .enumerate()
// .map(|(x, &y)| (x as u32, y.into()))
// .collect()
// }
#[inline]
fn offset(&self, index: isize, item_offset: isize) -> Option<isize> {
let new_index = index + item_offset;
if new_index < 0 {
return None; // invalid; no char before first char
}
if new_index > self.len() as isize {
return None;
}
Some(new_index)
}
#[inline]
fn item(&self, index: isize) -> Option<i32> {
self.get(index as usize).map(|&it| it.into() as i32)
}
#[inline]
fn len(&self) -> usize {
self.len()
}
#[inline]
fn from_text(text: &str) -> Self::Owned {
text.chars()
.map(|it| T::try_from(it as u32).unwrap())
.collect()
}
#[inline]
// default
fn to_display(&self) -> String {
self.iter()
.map(|x| char::try_from((*x).into()).unwrap_or(REPLACEMENT_CHARACTER))
.collect()
}
}
//
// impl InputData for [u8] {
// #[inline]
// fn to_display(&self) -> String { String::from_utf8_lossy(self).into_owned() }
// }
// impl InputData for [u16] {
// }
//
// impl InputData for [u32] {
// #[inline]
// fn to_display(&self) -> String {
// self.iter()
// .map(|x| char::try_from(*x).unwrap_or(REPLACEMENT_CHARACTER))
// .collect()
// }
// }
impl InputData for str {
// fn to_indexed_vec(&self) -> Vec<(u32, u32)> {
// self.char_indices()
// .map(|(i, ch)| (i as u32, ch as u32))
// .collect()
// }
#[inline]
fn offset(&self, mut index: isize, mut item_offset: isize) -> Option<isize> {
if item_offset == 0 {
return Some(index);
}
let direction = item_offset.signum();
while {
index += direction;
if index < 0 || index > self.len() as isize {
return None;
}
if self.is_char_boundary(index as usize) {
item_offset -= direction;
}
item_offset != 0
} {}
Some(index)
}
#[inline]
fn item(&self, index: isize) -> Option<i32> {
self.get(index as usize..)
.and_then(|it| it.chars().next())
.map(|it| it as i32)
}
#[inline]
fn len(&self) -> usize {
self.len()
}
fn from_text(text: &str) -> Self::Owned {
text.to_owned()
}
// #[inline]
// fn from_text(text: &str) -> Self::Owned { text.to_owned() }
#[inline]
fn to_display(&self) -> String {
self.to_owned()
}
}