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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
use super::*;
/// Examples: Attr, AttrEq
pub trait XFind<'l> {
/// Usually must be [Copy] so it's compatible with any multi-matchers.
/// The Out must be copied in the event of "cartesian" product scenarios where multi-matchers
/// return multiple combinations of their inner matchers' Out.
type Out: Copy;
fn find<M>(&self, direction: &M, ll_line: &'l LLLine) -> Vec<(Self::Out, LLSelection)>
where
M: XDirection<'l>;
}
pub struct AttrEq<'a, Attr> {
attr: &'a Attr,
}
pub fn attr_eq<A>(attr: &A) -> AttrEq<'_, A> {
AttrEq { attr }
}
pub struct TokenHasAny<'a, Attr: PartialEq> {
one_of: &'a [Attr],
}
pub fn token_has_any<A: PartialEq>(attrs: &[A]) -> TokenHasAny<'_, A> {
TokenHasAny { one_of: attrs }
}
pub struct Attr<Attr>(std::marker::PhantomData<Attr>);
pub fn attr<A>() -> Attr<A> {
Attr(Default::default())
}
#[derive(PartialEq, Clone, Copy)]
pub struct NextIdx(pub(crate) usize);
impl<'l, Attr: PartialEq + 'static> XFind<'l> for AttrEq<'_, Attr> {
type Out = ();
fn go<M>(&self, direction: &M, ll_line: &'l LLLine) -> Vec<(Self::Out, NextIdx)>
where
M: XDirection<'l>,
{
direction.next_attr_eq(self.attr, ll_line)
}
}
impl<'l, Attr: PartialEq + 'static> XFind<'l> for TokenHasAny<'_, Attr> {
type Out = &'l Attr;
fn go<M>(&self, direction: &M, ll_line: &'l LLLine) -> Vec<(Self::Out, NextIdx)>
where
M: XDirection<'l>,
{
direction.next_token_attr_one_of(self.one_of, ll_line)
}
}
impl<'l, A: 'static> XFind<'l> for Attr<A> {
type Out = &'l A;
fn go<M>(&self, direction: &M, ll_line: &'l LLLine) -> Vec<(Self::Out, NextIdx)>
where
M: XDirection<'l>,
{
direction.next_attr::<A>(ll_line)
}
}
// impl<'l, A: XMatchNext<'l>, B: XMatchNext<'l>> XMatchNext<'l> for (A, B) {
// type Out = (A::Out, B::Out);
// fn go<M>(&self, direction: &M, ll_line: &'l LLLine) -> Vec<(Self::Out, NextIdx)>
// where
// M: XDirection<'l>,
// {
// // ╰─╯A(1)
// // ╰─╯A(2)
// // ╰──╯A(3)
// // ╰─╯B(1)
// // ╰─╯B(2)
// // ╰──╯B(3)
// let bs = self.1.go(direction, ll_line);
// self.0
// .go(direction, ll_line)
// .into_iter()
// .flat_map(|(a, a_end_idx)| {
// bs.iter().filter_map(move |(b, b_end_idx)| {
// if a_end_idx == *b_end_idx {
// Some(((a, *b), a_end_idx.clone()))
// } else {
// None
// }
// })
// })
// .collect()
// // .0 = (&'m A(1), EndIdx(3)); (&'m A(2), EndIdx(3)); (&'m A(3), EndIdx(4))
// // .1 = (&'m B(1), EndIdx(3)); (&'m B(2), EndIdx(3)); (&'m B(3), EndIdx(4))
// // Out[0] = (&'m A(1), &'m B(1)), EndIdx(3)
// // Out[1] = (&'m A(2), &'m B(1)), EndIdx(3)
// // Out[2] = (&'m A(1), &'m B(2)), EndIdx(3)
// // Out[3] = (&'m A(2), &'m B(2)), EndIdx(3)
// // Out[4] = (&'m A(3), &'m B(3)), EndIdx(4)
// // match up the EndIdx values...
// }
// }
// impl<'l, A: XMatchNext<'l>, B: XMatchNext<'l>, C: XMatchNext<'l>> XMatchNext<'l> for (A, B, C)
// where
// A::Out: Copy,
// B::Out: Copy,
// C::Out: Copy,
// {
// type Out = (A::Out, B::Out, C::Out);
// fn go<M>(&self, direction: &M, ll_line: &'l LLLine) -> Vec<(Self::Out, NextIdx)>
// where
// M: XDirection<'l>,
// {
// // match up the EndIdx values...
// let bs = self.1.go(direction, ll_line);
// let cs = self.2.go(direction, ll_line);
// self.0
// .go(direction, ll_line)
// .into_iter()
// .flat_map(|(a, a_end_idx)| {
// let cs_iter = cs.iter();
// bs.iter().flat_map(move |(b, b_end_idx)| {
// cs_iter.clone().filter_map(move |(c, c_end_idx)| {
// if &a_end_idx == b_end_idx && &a_end_idx == c_end_idx {
// Some(((a, *b, *c), a_end_idx))
// } else {
// None
// }
// })
// })
// })
// .collect()
// }
// }
pub trait XDirection<'l>
where
Self: Sized,
{
fn attr<T: 'static>(&self, ll_line: &'l LLLine) -> Vec<(&'l T, LLSelection)>;
fn attr_eq<T: 'static + PartialEq>(
&self,
equals: &T,
ll_line: &'l LLLine,
) -> Vec<((), LLSelection)>;
fn token_attr_one_of<T: 'static + PartialEq>(
&self,
set: &[T],
ll_line: &'l LLLine,
) -> Vec<(&'l T, LLSelection)>;
}
pub(crate) struct XForwards {
pub(super) idx: usize,
}
// TODO:
// ╰─╯Amount(1000.25)
// ╰───╯Person(A)
// ╰───╯Person(B)
// ╰───╯VerbPhrase
// selection
// .find_by(attr::<Clause>())
// .iter ... .map
// .contains_in_any_order(&(multiple::<Person, 2>(), attr::<Amount>(), attr::<VerbPhrase>()))
// -> Vec of 2 -> Full original selection
// - ([&Person(A), &Person(B)], &Amount, &VerbPhrase)
// pub(crate) struct XContains {
// pub(super) start_idx: usize,
// pub(super) end_idx: usize,
// }
impl<'l> XDirection<'l> for XForwards {
fn next_attr_eq<T: 'static + PartialEq>(
&self,
equals: &T,
ll_line: &'l LLLine,
) -> Vec<((), NextIdx)> {
// [ ... ] - Current Cursor
// [ ... ] - Trying to match Attr
if self.idx + 1 == ll_line.ll_tokens.len() {
return Vec::new();
}
ll_line
.attrs
.starts_at
.get(self.idx + 1)
.expect("Huh... match_forwards was at the end")
.get::<T>()
.iter()
.flat_map(|range| {
ll_line
.attrs
.values
.get(&range)
.unwrap()
.get::<T>()
.iter()
.filter_map(move |val| {
if val == equals {
Some(((), NextIdx(range.1)))
} else {
None
}
})
})
.collect()
}
fn next_attr<T: 'static>(&self, ll_line: &'l LLLine) -> Vec<(&'l T, NextIdx)> {
// [ ... ] - Current Cursor
// [ ... ] - Trying to match Attr
if self.idx + 1 == ll_line.ll_tokens.len() {
return Vec::new();
}
ll_line
.attrs
.starts_at
.get(self.idx + 1)
.expect("Huh... match_forwards was at the end")
.get::<T>()
.iter()
.flat_map(|range| {
ll_line
.attrs
.values
.get(&range)
.unwrap()
.get::<T>()
.iter()
.map(move |val| (val, NextIdx(range.1)))
})
.collect()
}
fn next_token_attr_one_of<T: 'static + PartialEq>(
&self,
set: &[T],
ll_line: &'l LLLine,
) -> Vec<(&'l T, NextIdx)> {
// [ ... ] - Current Cursor
// [ ... ] - Trying to match Attr
let next_token_idx = self.idx + 1;
if next_token_idx == ll_line.ll_tokens.len() {
return Vec::new();
}
ll_line
.attrs
.values
.get(&(next_token_idx, next_token_idx))
.expect("Huh... match_forwards was at the end")
.get::<T>()
.iter()
.filter_map(|value| {
if set.contains(&value) {
Some((value, NextIdx(next_token_idx)))
} else {
None
}
})
.collect()
}
}
pub(crate) struct XBackwards {
pub(super) idx: usize,
}
impl<'l> XDirection<'l> for XBackwards {
fn next_attr_eq<T: 'static + PartialEq>(
&self,
equals: &T,
ll_line: &'l LLLine,
) -> Vec<((), NextIdx)> {
// [ ... ] - Current Cursor
// [ ... ] - Trying to match Attr
// [...] - Trying to match Attr
if self.idx == 0 {
return Vec::new();
}
let next_token_idx = self.idx - 1;
ll_line
.attrs
.ends_at
.get(next_token_idx)
.expect("Huh... match_backwards was at the end")
.get::<T>()
.iter()
.flat_map(|range| {
ll_line
.attrs
.values
.get(&range)
.unwrap()
.get::<T>()
.iter()
.filter_map(move |val| {
if val == equals {
Some(((), NextIdx(range.0)))
} else {
None
}
})
})
.collect()
}
fn next_attr<T: 'static>(&self, ll_line: &'l LLLine) -> Vec<(&'l T, NextIdx)> {
// [ ... ] - Current Cursor
// [ ... ] - Trying to match Attr
// [...] - Trying to match Attr
if self.idx == 0 {
return Vec::new();
}
let next_token_idx = self.idx - 1;
ll_line
.attrs
.ends_at
.get(next_token_idx)
.expect("Huh... Backwards::next_attr was at the start")
.get::<T>()
.iter()
.flat_map(|range| {
ll_line
.attrs
.values
.get(&range)
.unwrap()
.get::<T>()
.iter()
.map(move |val| (val, NextIdx(range.0)))
})
.collect()
}
fn next_token_attr_one_of<T: 'static + PartialEq>(
&self,
set: &[T],
ll_line: &'l LLLine,
) -> Vec<(&'l T, NextIdx)> {
// [ ... ] - Current Cursor
// [ ... ] - Trying to match Attr
// [...] - Trying to match Attr
if self.idx == 0 {
return Vec::new();
}
let next_token_idx = self.idx - 1;
ll_line
.attrs
.ends_at
.get(next_token_idx)
.expect("Huh... Backwards::next_attr was at the start")
.get::<T>()
.iter()
.flat_map(|range| {
ll_line
.attrs
.values
.get(&range)
.unwrap()
.get::<T>()
.iter()
.filter_map(move |val| {
if set.contains(&val) {
Some((val, NextIdx(range.0)))
} else {
None
}
})
})
.collect()
}
}