trilean-json-shape-pattern 3.1.3

The pattern grammar of trilean-json-shape: the subset of ECMAScript regular expressions every JSON Schema validator reads alike, compiled to an automaton that matches in linear time.
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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
//! The regular expressions a `pattern` constraint may use: the subset of ECMAScript patterns whose meaning is the
//! same in every JSON Schema validator, so the validator of `trilean-json-shape` and one written for the schema it emits
//! agree. It is a crate of its own so that the derive, which refuses an unsupported pattern at compile time, and the
//! validator, which matches with it, compile one grammar and cannot disagree about which patterns exist. A pattern
//! is unanchored unless it says `^` and `$`, and is matched over Unicode scalar values, as ECMAScript does with the
//! `u` flag, which is how Ajv compiles a schema's patterns.
//!
//! Supported: literal characters, `.` (any character except a line terminator), escaped punctuation (`\.`, `\\`,
//! `\/` and the other syntax characters, and `\-` inside a class), classes with ranges and negation (`[a-z0-9._-]`,
//! `[^/]`), groups (`(...)` and `(?:...)`), alternation, the greedy quantifiers `*`, `+` and `?`, and the anchors `^`
//! and `$`. Anything else (counted repetition, lazy quantifiers, `\d`-style classes, lookaround) is refused, so a
//! pattern that would mean something else in another engine does not compile.

use std::fmt;

/// Why a pattern is not in the supported subset.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PatternError {
    pub pattern: String,
    pub reason: String,
}

/// The text as a JSON string literal, which is how every message writes a string. The escapes are the ones
/// `JSON.stringify` writes: `\"`, `\\`, the short forms of the control characters that have one, and `\u00XX` for
/// the rest below U+0020; everything else is written as it is.
pub fn json_quote(text: &str) -> String {
    let mut quoted = String::with_capacity(text.len() + 2);
    quoted.push('"');
    for character in text.chars() {
        match character {
            '"' => quoted.push_str("\\\""),
            '\\' => quoted.push_str("\\\\"),
            '\n' => quoted.push_str("\\n"),
            '\r' => quoted.push_str("\\r"),
            '\t' => quoted.push_str("\\t"),
            '\u{8}' => quoted.push_str("\\b"),
            '\u{c}' => quoted.push_str("\\f"),
            control if control < ' ' => quoted.push_str(&format!("\\u{:04x}", u32::from(control))),
            other => quoted.push(other),
        }
    }
    quoted.push('"');
    quoted
}

impl fmt::Display for PatternError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            formatter,
            "pattern {} is not supported: {}",
            json_quote(&self.pattern),
            self.reason
        )
    }
}

impl std::error::Error for PatternError {}

#[derive(Clone, Debug)]
enum Atom {
    Char(char),
    AnyChar,
    Class {
        negated: bool,
        ranges: Vec<(char, char)>,
    },
    Group(Vec<Vec<Item>>),
    Start,
    End,
}

#[derive(Clone, Debug)]
struct Item {
    atom: Atom,
    min: usize,
    max: Option<usize>,
}

/// One state of the automaton a pattern compiles to. A state that consumes a character names the state to go to next.
#[derive(Clone, Debug)]
enum State {
    Char(char, usize),
    Any(usize),
    Class {
        negated: bool,
        ranges: Vec<(char, char)>,
        next: usize,
    },
    Split(usize, usize),
    Start(usize),
    End(usize),
    Match,
}

/// A compiled pattern, matched by simulating a Thompson automaton: no backtracking and no recursion over the text,
/// so matching costs time linear in the text times the size of the pattern, and constant stack, whatever the text.
#[derive(Clone, Debug)]
pub struct Pattern {
    states: Vec<State>,
    entry: usize,
}

struct Parser<'a> {
    pattern: &'a str,
    chars: Vec<char>,
    position: usize,
}

impl Parser<'_> {
    fn fail<T>(&self, reason: &str) -> Result<T, PatternError> {
        Err(PatternError {
            pattern: self.pattern.to_owned(),
            reason: format!("{reason} (at character {})", self.position),
        })
    }

    fn peek(&self) -> Option<char> {
        self.chars.get(self.position).copied()
    }

    fn eat(&mut self, expected: char) -> bool {
        if self.peek() == Some(expected) {
            self.position += 1;
            true
        } else {
            false
        }
    }

    fn alternatives(&mut self, nested: bool) -> Result<Vec<Vec<Item>>, PatternError> {
        let mut alternatives = vec![self.sequence()?];
        while self.eat('|') {
            alternatives.push(self.sequence()?);
        }
        if nested && self.peek() != Some(')') {
            return self.fail("a group is not closed");
        }
        Ok(alternatives)
    }

    fn sequence(&mut self) -> Result<Vec<Item>, PatternError> {
        let mut items = Vec::new();
        while let Some(next) = self.peek() {
            if next == '|' || next == ')' {
                break;
            }
            let atom = self.atom()?;
            let (min, max) = if self.eat('*') {
                (0, None)
            } else if self.eat('+') {
                (1, None)
            } else if self.eat('?') {
                (0, Some(1))
            } else {
                (1, Some(1))
            };
            if matches!(self.peek(), Some('*' | '+' | '?' | '{')) {
                return self.fail("a second quantifier, a lazy quantifier and counted repetition are not supported");
            }
            if matches!(atom, Atom::Start | Atom::End) && (min, max) != (1, Some(1)) {
                return self.fail("an anchor cannot be quantified");
            }
            items.push(Item { atom, min, max });
        }
        Ok(items)
    }

    /// The character an escape after a backslash stands for: punctuation only, and `-` only inside a class, as the
    /// `u` flag allows.
    fn escaped(&mut self, in_class: bool) -> Result<char, PatternError> {
        let Some(escaped) = self.peek() else {
            return self.fail("a pattern ends with a backslash");
        };
        self.position += 1;
        match escaped {
            '^' | '$' | '\\' | '.' | '*' | '+' | '?' | '(' | ')' | '[' | ']' | '{' | '}' | '|'
            | '/' => Ok(escaped),
            '-' if in_class => Ok(escaped),
            _ => self.fail("only escaped punctuation is supported"),
        }
    }

    fn atom(&mut self) -> Result<Atom, PatternError> {
        let Some(next) = self.peek() else {
            return self.fail("a pattern ends where an atom is expected");
        };
        self.position += 1;
        match next {
            '.' => Ok(Atom::AnyChar),
            '^' => Ok(Atom::Start),
            '$' => Ok(Atom::End),
            '\\' => self.escaped(false).map(Atom::Char),
            '(' => {
                if self.eat('?') && !self.eat(':') {
                    return self.fail("only the (?: group form is supported");
                }
                let alternatives = self.alternatives(true)?;
                self.position += 1;
                Ok(Atom::Group(alternatives))
            }
            '[' => self.class(),
            '*' | '+' | '?' | '{' | '}' | ']' => {
                self.fail("a quantifier or bracket has nothing to apply to")
            }
            other => Ok(Atom::Char(other)),
        }
    }

    fn class_char(&mut self) -> Result<char, PatternError> {
        let Some(next) = self.peek() else {
            return self.fail("a class is not closed");
        };
        self.position += 1;
        if next == '\\' {
            return self.escaped(true);
        }
        Ok(next)
    }

    fn class(&mut self) -> Result<Atom, PatternError> {
        let negated = self.eat('^');
        let mut ranges = Vec::new();
        loop {
            match self.peek() {
                None => return self.fail("a class is not closed"),
                Some(']') => {
                    self.position += 1;
                    break;
                }
                Some(_) => {}
            }
            let from = self.class_char()?;
            let is_range = self.peek() == Some('-')
                && self
                    .chars
                    .get(self.position + 1)
                    .is_some_and(|next| *next != ']');
            if is_range {
                self.position += 1;
                let to = self.class_char()?;
                if from > to {
                    return self.fail("a class range runs backwards");
                }
                ranges.push((from, to));
            } else {
                ranges.push((from, from));
            }
        }
        if ranges.is_empty() {
            return self.fail("an empty class is not supported");
        }
        Ok(Atom::Class { negated, ranges })
    }
}

fn push(states: &mut Vec<State>, state: State) -> usize {
    states.push(state);
    states.len() - 1
}

fn compile_alternatives(alternatives: &[Vec<Item>], next: usize, states: &mut Vec<State>) -> usize {
    let mut entries: Vec<usize> = alternatives
        .iter()
        .map(|sequence| compile_sequence(sequence, next, states))
        .collect();
    let mut entry = entries.pop().unwrap_or(next);
    while let Some(earlier) = entries.pop() {
        entry = push(states, State::Split(earlier, entry));
    }
    entry
}

fn compile_sequence(items: &[Item], next: usize, states: &mut Vec<State>) -> usize {
    items.iter().rev().fold(next, |following, item| {
        compile_item(item, following, states)
    })
}

fn compile_item(item: &Item, next: usize, states: &mut Vec<State>) -> usize {
    match (item.min, item.max) {
        (0, Some(1)) => {
            let atom = compile_atom(&item.atom, next, states);
            push(states, State::Split(atom, next))
        }
        (0, None) => {
            let split = push(states, State::Split(next, next));
            let atom = compile_atom(&item.atom, split, states);
            states[split] = State::Split(atom, next);
            split
        }
        (1, None) => {
            let split = push(states, State::Split(next, next));
            let atom = compile_atom(&item.atom, split, states);
            states[split] = State::Split(atom, next);
            atom
        }
        _ => compile_atom(&item.atom, next, states),
    }
}

fn compile_atom(atom: &Atom, next: usize, states: &mut Vec<State>) -> usize {
    match atom {
        Atom::Char(expected) => push(states, State::Char(*expected, next)),
        Atom::AnyChar => push(states, State::Any(next)),
        Atom::Class { negated, ranges } => push(
            states,
            State::Class {
                negated: *negated,
                ranges: ranges.clone(),
                next,
            },
        ),
        Atom::Group(alternatives) => compile_alternatives(alternatives, next, states),
        Atom::Start => push(states, State::Start(next)),
        Atom::End => push(states, State::End(next)),
    }
}

/// The states reached so far at one position in the text.
struct Frontier {
    stamp: Vec<usize>,
    generation: usize,
    consuming: Vec<usize>,
    matched: bool,
}

impl Frontier {
    fn new(size: usize) -> Self {
        Frontier {
            stamp: vec![0; size],
            generation: 1,
            consuming: Vec::new(),
            matched: false,
        }
    }

    fn clear(&mut self) {
        self.generation += 1;
        self.consuming.clear();
        self.matched = false;
    }
}

impl Pattern {
    /// Compiles `pattern`, or says which part of it is outside the supported subset.
    pub fn new(pattern: &str) -> Result<Pattern, PatternError> {
        let mut parser = Parser {
            pattern,
            chars: pattern.chars().collect(),
            position: 0,
        };
        let alternatives = parser.alternatives(false)?;
        if parser.position != parser.chars.len() {
            return parser.fail("an unmatched closing parenthesis");
        }
        let mut states = vec![State::Match];
        let entry = compile_alternatives(&alternatives, 0, &mut states);
        Ok(Pattern { states, entry })
    }

    /// Whether the pattern matches anywhere in `text`, as `new RegExp(pattern, "u").test(text)` does.
    pub fn is_match(&self, text: &str) -> bool {
        self.is_match_with_work(text).0
    }

    /// [`Pattern::is_match`] and the number of automaton states it visited, which is at most a constant times the
    /// text's length plus one, times the number of states of the pattern.
    pub fn is_match_with_work(&self, text: &str) -> (bool, u64) {
        let input: Vec<char> = text.chars().collect();
        let mut work = 0;
        let mut stack = Vec::new();
        let mut current = Frontier::new(self.states.len());
        let mut following = Frontier::new(self.states.len());
        for position in 0..=input.len() {
            // A match may start at any position, so the start of the pattern joins the states already reached.
            self.close(
                self.entry,
                position,
                input.len(),
                &mut current,
                &mut stack,
                &mut work,
            );
            if current.matched {
                return (true, work);
            }
            let Some(&found) = input.get(position) else {
                break;
            };
            following.clear();
            for &state in &current.consuming {
                work += 1;
                let next = match &self.states[state] {
                    State::Char(expected, next) if *expected == found => Some(*next),
                    State::Any(next) if !matches!(found, '\n' | '\r' | '\u{2028}' | '\u{2029}') => {
                        Some(*next)
                    }
                    State::Class {
                        negated,
                        ranges,
                        next,
                    } if ranges
                        .iter()
                        .any(|(from, to)| (*from..=*to).contains(&found))
                        != *negated =>
                    {
                        Some(*next)
                    }
                    _ => None,
                };
                if let Some(next) = next {
                    self.close(
                        next,
                        position + 1,
                        input.len(),
                        &mut following,
                        &mut stack,
                        &mut work,
                    );
                }
            }
            std::mem::swap(&mut current, &mut following);
        }
        (false, work)
    }

    /// Adds `entry` and every state reachable from it without consuming a character to `frontier`.
    fn close(
        &self,
        entry: usize,
        position: usize,
        length: usize,
        frontier: &mut Frontier,
        stack: &mut Vec<usize>,
        work: &mut u64,
    ) {
        stack.push(entry);
        while let Some(state) = stack.pop() {
            if frontier.stamp[state] == frontier.generation {
                continue;
            }
            frontier.stamp[state] = frontier.generation;
            *work += 1;
            match &self.states[state] {
                State::Split(first, second) => {
                    stack.push(*second);
                    stack.push(*first);
                }
                State::Start(next) if position == 0 => stack.push(*next),
                State::End(next) if position == length => stack.push(*next),
                State::Start(_) | State::End(_) => {}
                State::Match => frontier.matched = true,
                State::Char(..) | State::Any(_) | State::Class { .. } => {
                    frontier.consuming.push(state)
                }
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::Pattern;

    fn matches(pattern: &str, text: &str) -> bool {
        Pattern::new(pattern)
            .expect("a supported pattern")
            .is_match(text)
    }

    #[test]
    fn anchors_and_classes() {
        assert!(matches("^[a-z0-9][a-z0-9._-]*$", "a.b_c-1"));
        assert!(!matches("^[a-z0-9][a-z0-9._-]*$", "A"));
        assert!(!matches("^[a-z0-9][a-z0-9._-]*$", "-a"));
        assert!(!matches("^[a-z0-9][a-z0-9._-]*$", "a\n"));
        assert!(matches("[0-9]", "x7y"));
    }

    #[test]
    fn groups_and_alternation() {
        let source = r"^(recorded: .+|[a-z0-9._-]+\.test\.ts: .+)$";
        assert!(matches(source, "recorded: x"));
        assert!(matches(source, "a.test.ts: a > b"));
        assert!(!matches(source, "a.test.ts:"));
        assert!(!matches(source, "recorded: "));
        assert!(matches("^(?:ab)+$", "abab"));
        assert!(!matches("^(?:ab)+$", "aba"));
    }

    #[test]
    fn dot_stops_at_a_line_terminator_and_counts_characters() {
        assert!(!matches("^.$", "\n"));
        assert!(!matches("^.$", "\u{2028}"));
        assert!(matches("^.$", "\u{1F600}"));
        assert!(!matches("^..$", "\u{1F600}"));
        assert!(matches("^[^/]$", "\u{1F600}"));
        assert!(matches("^\u{1F600}$", "\u{1F600}"));
    }

    #[test]
    fn unsupported_syntax_is_refused() {
        for source in [
            "a{2}", "a*?", r"\d", r"a\-b", "(?=a)", "(a", "a)", "[", "[]", "[z-a]", "*", "^*",
        ] {
            assert!(Pattern::new(source).is_err(), "{source} must be refused");
        }
        assert!(Pattern::new(r"[a\-z]").is_ok());
    }

    #[test]
    fn matching_is_linear_in_the_text_and_uses_no_stack_per_character() {
        let long = "a".repeat(200_000);
        assert!(matches("^[a-z]*$", &long));
        assert!(!matches("^[a-z]*$", &format!("{long}!")));
        let pattern = Pattern::new("^(?:a|a)*$").expect("a supported pattern");
        let (_, short) = pattern.is_match_with_work(&format!("{}b", "a".repeat(1_000)));
        let (_, double) = pattern.is_match_with_work(&format!("{}b", "a".repeat(2_000)));
        assert!(
            short <= 12 * 1_001,
            "{short} state visits for 1001 characters"
        );
        assert!(
            double <= 12 * 2_001,
            "{double} state visits for 2001 characters"
        );
        assert!(double < 3 * short, "{double} is not about twice {short}");
    }

    #[test]
    fn an_unanchored_search_is_linear_too() {
        let pattern = Pattern::new("[0-9]+x").expect("a supported pattern");
        let (_, short) = pattern.is_match_with_work(&"1".repeat(1_000));
        let (_, double) = pattern.is_match_with_work(&"1".repeat(2_000));
        assert!(double < 3 * short, "{double} is not about twice {short}");
    }

    #[test]
    fn nested_empty_loops_terminate() {
        assert!(matches("^(?:a*)*$", "aaa"));
        assert!(!matches("^(?:a*)*b$", "aaa"));
    }
}