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
230
231
232
233
234
235
236
237
238
239
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
use crate::;
;
// pub fn take_string<'a>(
// to_match: &'a str,
// ) -> impl Fn(&mut Gaze<&str>) -> Option<&'a str> + 'a {
// let graphemes = to_match.graphemes(true).collect::<Vec<&str>>();
// move |gaze: &mut Gaze<&str>| -> Option<&str> {
// let mut offset = 0usize;
// while offset < graphemes.len() {
// let next_char = gaze.next();
// match next_char {
// Some(next_char) => {
// if graphemes[offset] == next_char {
// offset += 1;
// } else {
// return None;
// }
// }
// None => return None,
// }
// }
// Some(to_match)
// }
// }
// pub fn ignore_all<'a>(
// to_match: Vec<&'a str>, //TODO maybe make this an array instead of Vec
// ) -> impl Fn(&mut Gaze<&'a str>) -> Option<()> {
// move |gaze: &mut Gaze<&'a str>| -> Option<()> {
// while !gaze.is_complete() {
// let peek = gaze.peek();
// match peek {
// Some(peek) => {
// if to_match.contains(&peek) {
// gaze.next();
// } else {
// return Some(());
// }
// }
// None => return Some(()),
// }
// }
// Some(())
// }
// }
// pub fn take_while_str(
// matcher: impl Fn(&str) -> bool,
// ) -> impl Fn(&mut Gaze<&str>) -> Option<String> {
// move |gaze: &mut Gaze<&str>| -> Option<String> {
// let mut res = String::new();
// loop {
// let peek = gaze.peek();
// match peek {
// Some(peek) => {
// if matcher(peek) {
// gaze.next();
// res += peek;
// } else if res.is_empty() {
// return None;
// } else {
// return Some(res);
// }
// }
// None => {
// if res.is_empty() {
// return None;
// } else {
// return Some(res);
// }
// }
// }
// }
// }
// }
// struct TakeWhile<T> { matcher: dyn Fn(T) -> bool }
// impl <T>Nibbler<T, T> for TakeWhile<T> {
// fn run(&mut self, gaze: &mut Gaze<T>) -> Option<T> {
// todo!()
// }
// }
// pub fn take_while<T>(
// matcher: impl Fn(T) -> bool,
// ) -> impl Fn(&mut Gaze<T>) -> Option<Vec<T>>
// where
// T: Copy,
// {
// move |gaze: &mut Gaze<T>| -> Option<Vec<T>> {
// let mut res = Vec::new();
// loop {
// let next = gaze.next();
// match next {
// Some(next) => {
// if matcher(next) {
// res.push(next);
// } else if res.is_empty() {
// return None;
// } else {
// return Some(res);
// }
// }
// None => {
// if res.is_empty() {
// return None;
// } else {
// return Some(res);
// }
// }
// }
// }
// }
// }
// /// A Step that takes values from the String until the predicate passes.
// pub struct TakeUntil<'a>(pub &'a dyn Fn(&str) -> bool);
// impl Tokenizer<String> for TakeUntil<'_> {
// fn attempt(&self, gaze: &mut Gaze) -> Result<String, GazeErr> {
// //TODO this will need to be rewritten once handle Unicode better
// //TODO also this should share code with TakeWhile
// let mut res = String::new();
// loop {
// let next_value = gaze.peek();
// match next_value {
// None => {
// return Ok(res);
// }
// Some(c) => {
// if !self.0(c) {
// res += c;
// gaze.next();
// } else {
// return Ok(res);
// }
// }
// }
// }
// }
// }
// pub struct TakeFirst<'a, T>(pub Box<[&'a dyn Tokenizer<T>]>);
// impl<T> Tokenizer<T> for TakeFirst<'_, T> {
// fn attempt(&self, gaze: &mut Gaze) -> Result<T, GazeErr> {
// for step in &*self.0 {
// let res = gaze.run(*step);
// match res {
// Ok(_) => return res,
// Err(_) => continue,
// }
// }
// Err(GazeErr::NoMatch)
// }
// }
// pub struct TakeAll<'a, T>(pub Box<[&'a dyn Tokenizer<T>]>);
// impl<T> Tokenizer<Box<[T]>> for TakeAll<'_, T> {
// fn attempt(&self, gaze: &mut Gaze) -> Result<Box<[T]>, GazeErr> {
// let mut res: Vec<T> = Vec::new();
// for step in &*self.0 {
// let r = gaze.run(*step);
// match r {
// Ok(r) => res.push(r),
// Err(_) => return Err(GazeErr::NoMatch),
// }
// }
// Ok(res.into_boxed_slice())
// }
// }