ere-core 0.2.3

A compile-time alternative for POSIX extended regular expressions.
Documentation
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
//! Not exactly the PikeVM, but close enough that I am naming it that.
//! It works similarly, except that since we are building at compile-time, there are benefits from inlining splitting.
//!
//! Due to the optimizations done earlier for the [`WorkingNFA`], we also have a slightly different NFA structure.
//! Currently we flatten all epsilon transitions for the VM so that epsilon transitions are at most a single step between symbols.
//! I'll have to review to ensure we avoid this causing large binary size overhead,
//! but it should be worst-case `O(n^2)` in the number of states, and far fewer on average.

use crate::{
    nfa_static,
    working_nfa::{EpsilonType, WorkingNFA, WorkingTransition},
};
use quote::quote;
use std::fmt::Write;

#[derive(Clone)]
pub struct PikeVMThread<const N: usize, S: Send + Sync + Copy + Eq> {
    pub state: S,
    pub captures: [(usize, usize); N],
}
impl<const N: usize, S: Send + Sync + Copy + Eq + std::fmt::Debug> std::fmt::Debug
    for PikeVMThread<N, S>
{
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        struct CapturesDebug<'a, const N: usize>(&'a [(usize, usize); N]);
        impl<'a, const N: usize> std::fmt::Debug for CapturesDebug<'a, N> {
            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                f.write_char('[')?;
                for (i, endpoints) in self.0.iter().enumerate() {
                    if i != 0 {
                        f.write_str(", ")?;
                    }
                    match endpoints {
                        (usize::MAX, usize::MAX) => f.write_str("(_, _)")?,
                        (start, usize::MAX) => write!(f, "({start}, _)")?,
                        (usize::MAX, end) => write!(f, "(_, {end})")?,
                        (start, end) => write!(f, "({start}, {end})")?,
                    }
                }
                return f.write_char(']');
            }
        }
        return f
            .debug_struct("PikeVMThread")
            .field("state", &self.state)
            .field("captures", &CapturesDebug(&self.captures))
            .finish();
    }
}

fn vmstate_label(idx: usize) -> proc_macro2::Ident {
    let label = format!("State{idx}");
    return proc_macro2::Ident::new(&label, proc_macro2::Span::call_site());
}

/// Since we are shortcutting the epsilon transitions, we can skip printing
/// states that have only epsilon transitions and are not the start/end states
fn compute_excluded_states(nfa: &WorkingNFA) -> Vec<bool> {
    let mut out = vec![true; nfa.states.len()];
    out[0] = false;
    out[nfa.states.len() - 1] = false;
    for (from, state) in nfa.states.iter().enumerate() {
        for t in &state.transitions {
            out[from] = false;
            out[t.to] = false;
        }
    }

    return out;
}

/// Assumes the `VMStates` enum is already created locally in the token stream
///
/// Creates the function for running symbol transitions on the pike VM
fn serialize_pike_vm_symbol_propogation(
    nfa: &WorkingNFA,
) -> (proc_macro2::TokenStream, proc_macro2::TokenStream) {
    let WorkingNFA { states } = nfa;
    let capture_groups = nfa.num_capture_groups();
    let excluded_states = compute_excluded_states(nfa);

    fn make_symbol_transition_test(t: &WorkingTransition) -> proc_macro2::TokenStream {
        let WorkingTransition { symbol, to } = t;
        let symbol = nfa_static::AtomStatic::serialize_as_token_stream(symbol);
        return quote! {
            {
                let symbol = #symbol;
                if symbol.check(c) {
                    new_list[#to] = true;
                }
            }
        };
    }

    fn make_symbol_transition_exec(t: &WorkingTransition) -> proc_macro2::TokenStream {
        let WorkingTransition { symbol, to } = t;
        let symbol = nfa_static::AtomStatic::serialize_as_token_stream(symbol);
        let to_label = vmstate_label(*to);
        return quote! {
            {
                let symbol = #symbol;
                if symbol.check(c) {
                    out.push(
                        ::ere::pike_vm::PikeVMThread {
                            state: VMStates::#to_label,
                            captures: thread.captures.clone(),
                        },
                    );
                }
            }
        };
    }

    let transition_symbols_defs_test: proc_macro2::TokenStream = states
        .iter()
        .enumerate()
        .filter(|(i, _)| !excluded_states[*i])
        .map(|(i, state)| {
            let state_transitions: proc_macro2::TokenStream = state
                .transitions
                .iter()
                .map(make_symbol_transition_test)
                .collect();

            return quote! {
                if list[#i] {
                    #state_transitions
                }
            };
        })
        .collect();

    let transition_symbols_defs_exec: proc_macro2::TokenStream = states
        .iter()
        .enumerate()
        .filter(|(i, _)| !excluded_states[*i])
        .map(|(i, state)| {
            let label = vmstate_label(i);
            let state_transitions: proc_macro2::TokenStream = state
                .transitions
                .iter()
                .map(make_symbol_transition_exec)
                .collect();

            return quote! {
                VMStates::#label => {
                    #state_transitions
                }
            };
        })
        .collect();

    let transition_symbols_test = quote! {
        fn transition_symbols_test(
            list: &[bool],
            new_list: &mut [bool],
            c: char,
        ) {
            #transition_symbols_defs_test
        }
    };
    let transition_symbols_exec = quote! {
        fn transition_symbols_exec(
            threads: &[::ere::pike_vm::PikeVMThread<#capture_groups, VMStates>],
            c: char,
        ) -> ::std::vec::Vec<::ere::pike_vm::PikeVMThread<#capture_groups, VMStates>> {
            let mut out = ::std::vec::Vec::<::ere::pike_vm::PikeVMThread<#capture_groups, VMStates>>::new();
            for thread in threads {
                match thread.state {
                    #transition_symbols_defs_exec
                }
            }
            return out;
        }
    };

    return (transition_symbols_test, transition_symbols_exec);
}

#[derive(Clone, PartialEq, Eq)]
struct ThreadUpdates {
    pub state: usize,
    pub update_captures: Vec<(bool, bool)>,
    pub start_only: bool,
    pub end_only: bool,
}
impl ThreadUpdates {
    /// Creates a block which takes `list: &mut [bool; STATE_NUM]` from its local context, updates it in-place using `self` (compile-time).
    pub fn serialize_thread_update_test(&self) -> proc_macro2::TokenStream {
        let new_state = self.state;
        return quote! {{
            list[#new_state] = true;
        }};
    }
    /// Creates a block which takes `thread` from its local context, updates it using `self` (compile-time),
    /// and appends it to `out` from its local context.
    pub fn serialize_thread_update_exec(&self) -> proc_macro2::TokenStream {
        let new_state_idx = self.state;
        let new_state = vmstate_label(self.state);
        let mut capture_updates = proc_macro2::TokenStream::new();
        for (i, (start, end)) in self.update_captures.iter().cloned().enumerate() {
            if start {
                capture_updates.extend(quote! {
                    new_thread.captures[#i].0 = idx;
                });
            }
            if end {
                capture_updates.extend(quote! {
                    new_thread.captures[#i].1 = idx;
                });
            }
        }

        return quote! {
            if !occupied_states[#new_state_idx] {
                let mut new_thread = thread.clone();
                new_thread.state = VMStates::#new_state;

                #capture_updates

                out.push(new_thread);
                occupied_states[#new_state_idx] = true;
            }
        };
    }
}

fn calculate_epsilon_propogations(nfa: &WorkingNFA, state: usize) -> Vec<ThreadUpdates> {
    let WorkingNFA { states } = nfa;
    let capture_groups = nfa.num_capture_groups();
    // reduce epsilons to occur in a single step
    let mut new_threads = vec![];
    fn traverse(
        thread: ThreadUpdates,
        states: &Vec<crate::working_nfa::WorkingState>,
        out: &mut Vec<ThreadUpdates>,
    ) {
        out.push(thread.clone());
        for e in &states[thread.state].epsilons {
            let mut new_thread = thread.clone();
            new_thread.state = e.to;
            match e.special {
                EpsilonType::None => {}
                EpsilonType::StartAnchor => new_thread.start_only = true,
                EpsilonType::EndAnchor => new_thread.end_only = true,
                EpsilonType::StartCapture(capture_group) => {
                    new_thread.update_captures[capture_group].0 = true
                }
                EpsilonType::EndCapture(capture_group) => {
                    new_thread.update_captures[capture_group].1 = true
                }
            }

            if !out.contains(&new_thread) {
                traverse(new_thread, states, out);
            }
        }
    }
    traverse(
        ThreadUpdates {
            state,
            update_captures: vec![(false, false); capture_groups],
            start_only: false,
            end_only: false,
        },
        states,
        &mut new_threads,
    );
    return new_threads;
}

/// Assumes the `VMStates` enum is already created locally in the token stream
fn serialize_pike_vm_epsilon_propogation(
    nfa: &WorkingNFA,
) -> (proc_macro2::TokenStream, proc_macro2::TokenStream) {
    let WorkingNFA { states } = nfa;
    let capture_groups = nfa.num_capture_groups();
    let num_states = states.len();
    let excluded_states = compute_excluded_states(nfa);

    // Generate code to propogate/split a thread according to epsilon transitions
    let mut transition_epsilons_test = proc_macro2::TokenStream::new();
    let mut transition_epsilons_exec = proc_macro2::TokenStream::new();
    for (i, _) in states.iter().enumerate() {
        if excluded_states[i] {
            // since we propogate, some states are now useless if they are intermediate
            // with only epsilon transitions
            continue;
        }
        // all reachable states with next transition as epsilon
        let mut new_threads = calculate_epsilon_propogations(nfa, i);
        new_threads
            .retain(|t| !states[t.state].transitions.is_empty() || t.state + 1 == num_states);

        // Write epsilon-propogation of threads to the token stream for test
        let start_end_threads: proc_macro2::TokenStream = new_threads
            .iter()
            .filter(|t| t.start_only && t.end_only)
            .map(ThreadUpdates::serialize_thread_update_test)
            .collect();
        let start_threads: proc_macro2::TokenStream = new_threads
            .iter()
            .filter(|t| t.start_only && !t.end_only)
            .map(ThreadUpdates::serialize_thread_update_test)
            .collect();
        let end_threads: proc_macro2::TokenStream = new_threads
            .iter()
            .filter(|t| !t.start_only && t.end_only)
            .map(ThreadUpdates::serialize_thread_update_test)
            .collect();
        let normal_threads: proc_macro2::TokenStream = new_threads
            .iter()
            .filter(|t| !t.start_only && !t.end_only)
            .map(ThreadUpdates::serialize_thread_update_test)
            .collect();

        let label = vmstate_label(i);
        transition_epsilons_test.extend(quote! {
            if list[#i] {
                if is_start && is_end {
                    #start_end_threads
                }
                if is_start {
                    #start_threads
                }
                if is_end {
                    #end_threads
                }
                #normal_threads
            }
        });

        // Write epsilon-propogation of threads to the token stream for exec
        let start_end_threads: proc_macro2::TokenStream = new_threads
            .iter()
            .filter(|t| t.start_only && t.end_only)
            .map(ThreadUpdates::serialize_thread_update_exec)
            .collect();
        let start_threads: proc_macro2::TokenStream = new_threads
            .iter()
            .filter(|t| t.start_only && !t.end_only)
            .map(ThreadUpdates::serialize_thread_update_exec)
            .collect();
        let end_threads: proc_macro2::TokenStream = new_threads
            .iter()
            .filter(|t| !t.start_only && t.end_only)
            .map(ThreadUpdates::serialize_thread_update_exec)
            .collect();
        let normal_threads: proc_macro2::TokenStream = new_threads
            .iter()
            .filter(|t| !t.start_only && !t.end_only)
            .map(ThreadUpdates::serialize_thread_update_exec)
            .collect();

        transition_epsilons_exec.extend(quote! {
            VMStates::#label => {
                if is_start && is_end {
                    #start_end_threads
                }
                if is_start {
                    #start_threads
                }
                if is_end {
                    #end_threads
                }
                #normal_threads
            }
        });
    }

    let transition_epsilons_test = quote! {
        fn transition_epsilons_test(
            list: &mut [bool],
            idx: usize,
            len: usize,
        ) {
            let is_start = idx == 0;
            let is_end = idx == len;
            #transition_epsilons_test
        }
    };
    let transition_epsilons_exec = quote! {
        fn transition_epsilons_exec(
            threads: &[::ere::pike_vm::PikeVMThread<#capture_groups, VMStates>],
            idx: usize,
            len: usize,
        ) -> ::std::vec::Vec<::ere::pike_vm::PikeVMThread<#capture_groups, VMStates>> {
            let is_start = idx == 0;
            let is_end = idx == len;
            let mut occupied_states = ::std::vec![false; #num_states];
            let mut out = ::std::vec::Vec::<::ere::pike_vm::PikeVMThread<#capture_groups, VMStates>>::new();
            for thread in threads {
                match thread.state {
                    #transition_epsilons_exec
                }
            }
            return out;
        }
    };

    return (transition_epsilons_test, transition_epsilons_exec);
}

/// Converts a [`WorkingNFA`] into a format that, when returned by a proc macro, will
/// create the corresponding Pike VM.
///
/// Will evaluate to a `const` pair `(test_fn, exec_fn)`.
pub(crate) fn serialize_pike_vm_token_stream(nfa: &WorkingNFA) -> proc_macro2::TokenStream {
    let WorkingNFA { states, .. } = nfa;
    let capture_groups = nfa.num_capture_groups();
    let excluded_states = compute_excluded_states(nfa);

    let enum_states: proc_macro2::TokenStream = std::iter::IntoIterator::into_iter(0..states.len())
        .filter(|i| !excluded_states[*i])
        .map(|i| {
            let label = vmstate_label(i);
            return quote! { #label, };
        })
        .collect();
    let state_count = states.len(); // TODO: not all of these are used, so we may be able to slightly reduce usage.
    let accept_state = vmstate_label(states.len() - 1);

    let (transition_symbols_test, transition_symbols_exec) =
        serialize_pike_vm_symbol_propogation(nfa);
    let (transition_epsilons_test, transition_epsilons_exec) =
        serialize_pike_vm_epsilon_propogation(nfa);

    return quote! {{
        #[derive(Clone, Copy, PartialEq, Eq, Debug)]
        enum VMStates {
            #enum_states
        }

        #transition_symbols_test
        #transition_symbols_exec
        #transition_epsilons_test
        #transition_epsilons_exec

        fn test(text: &str) -> bool {
            let mut list = [false; #state_count];
            let mut new_list = [false; #state_count];
            list[0] = true;

            transition_epsilons_test(&mut list, 0, text.len());
            for (i, c) in text.char_indices() {
                transition_symbols_test(&list, &mut new_list, c);
                if new_list.iter().all(|b| !b) {
                    return false;
                }
                ::std::mem::swap(&mut list, &mut new_list);
                transition_epsilons_test(&mut list, i + c.len_utf8(), text.len());
                new_list.fill(false);
            }

            return list[#state_count - 1];
        }
        fn exec<'a>(text: &'a str) -> Option<[Option<&'a str>; #capture_groups]> {
            let mut threads = ::std::vec::Vec::<::ere::pike_vm::PikeVMThread<#capture_groups, VMStates>>::new();
            threads.push(::ere::pike_vm::PikeVMThread {
                state: VMStates::State0,
                captures: [(usize::MAX, usize::MAX); #capture_groups],
            });

            let new_threads = transition_epsilons_exec(&threads, 0, text.len());
            threads = new_threads;

            for (i, c) in text.char_indices() {
                let new_threads = transition_symbols_exec(&threads, c);
                threads = new_threads;
                let new_threads = transition_epsilons_exec(&threads, i + c.len_utf8(), text.len());
                threads = new_threads;
                if threads.is_empty() {
                    return None;
                }
            }

            let final_capture_bounds = threads
                .into_iter()
                .find(|t| t.state == VMStates::#accept_state)?
                .captures;
            let mut captures = [::core::option::Option::None; #capture_groups];
            for (i, (start, end)) in final_capture_bounds.into_iter().enumerate() {
                if start != usize::MAX {
                    assert_ne!(end, usize::MAX);
                    // assert!(start <= end);
                    captures[i] = text.get(start..end);
                    assert!(captures[i].is_some());
                } else {
                    assert_eq!(end, usize::MAX);
                }
            }
            return Some(captures);
        }

        (test, exec)
    }};
}