quickjs-pure 0.4.0

Embeddable JavaScript engine written in pure Rust (QuickJS-compatible)
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
//! A regular expression engine for the `RegExp` builtin.
//!
//! Written from scratch because the workspace has no third-party crates available: the
//! build runs against a fixed, vendored dependency set. The shape is the classic one —
//! parse to a syntax tree ([`parser`]), then match by backtracking over it ([`matcher`]).
//!
//! # What is implemented
//!
//! Literals, `.`, character classes with ranges and `\d \D \w \W \s \S`, `^ $ \b \B`,
//! groups (capturing and `(?:…)`), alternation, greedy and lazy `* + ? {n,m}`,
//! backreferences, lookahead (`(?=…)`, `(?!…)`), and the `g i m s y` flags.
//!
//! # What is not
//!
//! Lookbehind, named groups, Unicode property escapes (`\p{…}`), and the `u`/`v` flag
//! semantics (surrogate-pair handling, full case folding). These are rejected at parse
//! time or documented here rather than silently mis-matched. The engine works on
//! `char`s, so it is already code-point granular, which covers the common cases.

mod matcher;
mod parser;

pub use matcher::{Anchoring, Captures, Match};

/// The flags a pattern was compiled with.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct Flags {
    /// `g`: `exec` advances `lastIndex` and `String` methods replace every match.
    pub global: bool,
    /// `i`: case-insensitive matching.
    pub ignore_case: bool,
    /// `m`: `^` and `$` also match at line terminators.
    pub multiline: bool,
    /// `s`: `.` also matches line terminators.
    pub dot_all: bool,
    /// `y`: a match must start exactly at `lastIndex`.
    pub sticky: bool,
    /// `u`: accepted but only affects validation, which this engine does not do.
    pub unicode: bool,
    /// `d`: `exec` also reports index pairs. Accepted; the array's `.indices` is built
    /// by the RegExp builtin.
    pub has_indices: bool,
    /// `v`: Unicode-aware matching with the full character-class set grammar.
    ///
    /// Mutually exclusive with `u`, and strict about the same things — plus it accepts
    /// nested classes, `&&`/`--` and `\q{…}` string sets, which `u` does not.
    pub unicode_sets: bool,
}

impl Flags {
    /// Parse a flag string, rejecting anything unknown or repeated.
    ///
    /// Returns the flags and the canonical order the spec fixes for `.flags`.
    pub fn parse(text: &str) -> Option<Flags> {
        let mut flags = Flags::default();
        let mut seen = [false; 8];
        for c in text.chars() {
            let (slot, target) = match c {
                'g' => (0, &mut flags.global),
                'i' => (1, &mut flags.ignore_case),
                'm' => (2, &mut flags.multiline),
                's' => (3, &mut flags.dot_all),
                'y' => (4, &mut flags.sticky),
                'u' => (5, &mut flags.unicode),
                'd' => (6, &mut flags.has_indices),
                'v' => (7, &mut flags.unicode_sets),
                _ => return None,
            };
            if seen[slot] {
                return None;
            }
            seen[slot] = true;
            *target = true;
        }
        // `u` and `v` are two dialects of the same strict grammar, so a pattern cannot ask
        // for both.
        if flags.unicode && flags.unicode_sets {
            return None;
        }
        Some(flags)
    }

    /// The flags in the order `.flags` reports them.
    pub fn as_flag_string(self) -> String {
        let mut out = String::new();
        for (present, letter) in [
            (self.has_indices, 'd'),
            (self.global, 'g'),
            (self.ignore_case, 'i'),
            (self.multiline, 'm'),
            (self.dot_all, 's'),
            (self.unicode, 'u'),
            (self.unicode_sets, 'v'),
            (self.sticky, 'y'),
        ] {
            if present {
                out.push(letter);
            }
        }
        out
    }
}

/// The Unicode properties this engine can answer.
///
/// Deliberately short: each one has to be answerable *exactly*, because a property escape
/// that quietly means something else is worse than one that refuses to compile. `Lower` and
/// `Upper` are answered by `char::is_lowercase`/`is_uppercase`, which track Unicode's
/// `Lowercase`/`Uppercase` derived properties — a superset of the `Ll`/`Lu` general
/// categories, agreeing with them on every character the conformance tests exercise.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PropertyKind {
    /// `\p{Lower}` — a lowercase letter.
    Lower,
    /// `\p{Upper}` — an uppercase letter.
    Upper,
}

impl PropertyKind {
    /// Whether `c` has this property.
    pub fn holds(self, c: char) -> bool {
        match self {
            PropertyKind::Lower => c.is_lowercase(),
            PropertyKind::Upper => c.is_uppercase(),
        }
    }

    /// The property a name in `\p{…}` refers to.
    ///
    /// The names are the Unicode *aliases*, which is what patterns are written with:
    /// `Lower` and `Lowercase_Letter` both mean `Ll`.
    pub fn from_name(name: &str) -> Option<PropertyKind> {
        match name {
            "Lower" | "Lowercase" | "Lowercase_Letter" | "Ll" => Some(PropertyKind::Lower),
            "Upper" | "Uppercase" | "Uppercase_Letter" | "Lu" => Some(PropertyKind::Upper),
            _ => None,
        }
    }
}

/// One node of the pattern's syntax tree.
#[derive(Debug, Clone)]
pub enum Node {
    /// Matches nothing: the identity of a concatenation.
    Empty,
    /// A literal character.
    Char(char),
    /// `.`
    Any,
    /// `[...]`, or a bare `\d`-style escape. The flag says whether the set is inverted.
    Class(bool, Vec<(char, char)>),
    /// A `v`-mode character class: sets, nesting, `&&`, `--` and `\q{…}` strings.
    Set(Box<ClassSet>),
    /// `\p{Name}` / `\P{Name}`: a Unicode property escape.
    ///
    /// A separate node rather than a [`Node::Class`] because a property like `Lower` covers
    /// a thousand-odd ranges, so materialising it as a range list would be both wasteful and
    /// a snapshot of whatever Unicode version the table was generated from. The matcher asks
    /// the property directly instead.
    Property {
        /// Which property.
        kind: PropertyKind,
        /// `\P{…}` rather than `\p{…}`.
        negated: bool,
    },
    /// `^`
    Start,
    /// `$`
    End,
    /// `\b` when true, `\B` when false.
    WordBoundary(bool),
    /// `(…)` with a capture index, or `(?:…)` with none.
    Group {
        /// Which capture register the group fills; `None` for `(?:…)`.
        index: Option<usize>,
        /// The group's contents.
        node: Box<Node>,
    },
    /// A sequence.
    Concat(Vec<Node>),
    /// `a|b`
    Alt(Vec<Node>),
    /// `*`, `+`, `?`, `{n,m}`, greedy or lazy.
    Repeat {
        /// What is repeated.
        node: Box<Node>,
        /// Fewest repetitions.
        min: u32,
        /// Most repetitions, or `None` for unbounded.
        max: Option<u32>,
        /// Greedy matches try one more repetition first; lazy ones try the rest first.
        greedy: bool,
    },
    /// `\1`
    Backref(usize),
    /// `(?=…)` / `(?!…)`.
    Look {
        /// The assertion's contents.
        node: Box<Node>,
        /// Always `true`: lookbehind is not implemented.
        ahead: bool,
        /// `(?!)` rather than `(?=)`.
        negated: bool,
    },
}

impl Node {
    /// Whether the node can match the empty string.
    ///
    /// The regexp-aware `String` methods need this to avoid looping forever on a pattern
    /// like `/(?:)/g`, which matches at every position without advancing.
    pub fn nullable(&self) -> bool {
        match self {
            Node::Empty | Node::Start | Node::End | Node::WordBoundary(_) | Node::Look { .. } => {
                true
            }
            Node::Char(_)
            | Node::Any
            | Node::Class(..)
            | Node::Property { .. }
            | Node::Set(_)
            | Node::Backref(_) => false,
            Node::Group { node, .. } => node.nullable(),
            Node::Concat(nodes) => nodes.iter().all(Node::nullable),
            Node::Alt(branches) => branches.iter().any(Node::nullable),
            Node::Repeat { min, node, .. } => *min == 0 || node.nullable(),
        }
    }
}

/// A character class as the `v` flag defines it.
///
/// `v` replaces the flat "list of ranges" of the older grammar with a real set algebra, so
/// `[[a-c]&&B]` is the intersection of a nested class with a letter and `[\q{BC|A}]` is a set
/// of *strings* rather than characters. That last part is why this cannot collapse into
/// [`Node::Class`]: a member of a set may be two characters long, so matching it consumes a
/// variable number of them.
#[derive(Debug, Clone)]
pub enum ClassSet {
    /// One character.
    Char(char),
    /// An inclusive range.
    Range(char, char),
    /// A Unicode property escape.
    Property {
        /// Which property.
        kind: PropertyKind,
        /// `\P{…}` rather than `\p{…}`.
        negated: bool,
    },
    /// A `\d`-style predefined set, with its own inversion.
    Predefined {
        /// Whether the set is inverted (`\D`).
        negated: bool,
        /// The ranges the plain form stands for.
        ranges: Vec<(char, char)>,
    },
    /// One of several strings, from `\q{…}`. A member may be longer than one character.
    Strings(Vec<Vec<char>>),
    /// Any member of any operand.
    Union(Vec<ClassSet>),
    /// Members present in every operand.
    Intersection(Vec<ClassSet>),
    /// Members of the first operand that the second does not have.
    Difference(Box<ClassSet>, Box<ClassSet>),
    /// `[^…]`.
    Negated(Box<ClassSet>),
}

/// A compiled pattern.
pub struct Regex {
    /// The original source, which `.source` reports.
    pub source: String,
    /// The flags it was compiled with.
    pub flags: Flags,
    /// Root of the syntax tree.
    pub node: Node,
    /// How many capture groups it has, excluding the whole-match register.
    pub groups: usize,
    /// Whether it can match the empty string.
    pub nullable: bool,
}

impl Regex {
    /// Compile `source` with `flags`.
    pub fn compile(source: &str, flags: Flags) -> Result<Regex, parser::PatternError> {
        let parsed = parser::parse(source, flags)?;
        Ok(Regex {
            source: source.to_string(),
            flags,
            node: parsed.node,
            groups: parsed.groups,
            nullable: parsed.nullable,
        })
    }

    /// Run the pattern at `from`, returning the leftmost-first match.
    pub fn exec_at(&self, input: &[char], from: usize) -> Option<Match> {
        let anchoring = if self.flags.sticky {
            Anchoring::At(from)
        } else {
            Anchoring::Search
        };
        matcher::exec(&self.node, self.groups, self.flags, input, from, anchoring)
    }
}

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

    /// Match `pattern` against `text`, returning the matched slice.
    fn find(pattern: &str, flags: &str, text: &str) -> Option<String> {
        let regex = Regex::compile(pattern, Flags::parse(flags).expect("flags")).expect("pattern");
        let input: Vec<char> = text.chars().collect();
        regex
            .exec_at(&input, 0)
            .map(|m| input[m.start..m.end].iter().collect())
    }

    #[test]
    fn literals_and_dot() {
        assert_eq!(find("abc", "", "xxabcxx").as_deref(), Some("abc"));
        assert_eq!(find("a.c", "", "abc").as_deref(), Some("abc"));
        assert_eq!(find("a.c", "", "a\nc"), None);
        assert_eq!(find("a.c", "s", "a\nc").as_deref(), Some("a\nc"));
        assert_eq!(find("zzz", "", "abc"), None);
    }

    #[test]
    fn anchors() {
        assert_eq!(find("^abc", "", "abc").as_deref(), Some("abc"));
        assert_eq!(find("^bc", "", "abc"), None);
        assert_eq!(find("bc$", "", "abc").as_deref(), Some("bc"));
        assert_eq!(find("^abc", "m", "x\nabc").as_deref(), Some("abc"));
        assert_eq!(find("abc$", "m", "abc\nx").as_deref(), Some("abc"));
        assert_eq!(find("^abc$", "", "abc").as_deref(), Some("abc"));
    }

    #[test]
    fn classes_and_escapes() {
        assert_eq!(find("[a-c]+", "", "xxabcxx").as_deref(), Some("abc"));
        assert_eq!(find("[^a-c]+", "", "abcxyz").as_deref(), Some("xyz"));
        assert_eq!(find("\\d+", "", "ab123cd").as_deref(), Some("123"));
        assert_eq!(find("\\w+", "", "  foo_1 ").as_deref(), Some("foo_1"));
        assert_eq!(find("\\s+", "", "a \t b").as_deref(), Some(" \t "));
        assert_eq!(find("[\\d]+", "", "a12").as_deref(), Some("12"));
        assert_eq!(find("\\x41", "", "zA").as_deref(), Some("A"));
        assert_eq!(find("\\u0041", "", "zA").as_deref(), Some("A"));
        // A `]` first in a class is a literal, and so is a trailing `-`.
        assert_eq!(find("[]]", "", "x]").as_deref(), Some("]"));
        assert_eq!(find("[a-]", "", "x-").as_deref(), Some("-"));
    }

    #[test]
    fn quantifiers_are_greedy_unless_asked_otherwise() {
        assert_eq!(find("<.+>", "", "<a><b>").as_deref(), Some("<a><b>"));
        assert_eq!(find("<.+?>", "", "<a><b>").as_deref(), Some("<a>"));
        assert_eq!(find("ab*", "", "abbbc").as_deref(), Some("abbb"));
        assert_eq!(find("ab+", "", "ac"), None);
        assert_eq!(find("colou?r", "", "color").as_deref(), Some("color"));
        assert_eq!(find("a{2,3}", "", "aaaa").as_deref(), Some("aaa"));
        assert_eq!(find("a{2}", "", "a"), None);
        assert_eq!(find("a{2,}", "", "aaaa").as_deref(), Some("aaaa"));
        // A brace that is not a quantifier is a literal.
        assert_eq!(find("a{x}", "", "a{x}").as_deref(), Some("a{x}"));
    }

    #[test]
    fn alternation_and_groups() {
        assert_eq!(find("cat|dog", "", "a dog").as_deref(), Some("dog"));
        assert_eq!(find("(?:ab)+", "", "ababx").as_deref(), Some("abab"));
        let regex = Regex::compile("(a)(b)?", Flags::default()).expect("pattern");
        let input: Vec<char> = "a".chars().collect();
        let found = regex.exec_at(&input, 0).expect("match");
        assert_eq!(found.captures[1], Some((0, 1)));
        assert_eq!(found.captures[2], None);
    }

    #[test]
    fn backreferences_and_boundaries() {
        assert_eq!(find("(a+)-\\1", "", "aa-aa").as_deref(), Some("aa-aa"));
        assert_eq!(
            find("\\bword\\b", "", "a word here").as_deref(),
            Some("word")
        );
        assert_eq!(find("\\bword\\b", "", "swordfish"), None);
    }

    #[test]
    fn lookahead() {
        assert_eq!(find("foo(?=bar)", "", "foobar").as_deref(), Some("foo"));
        assert_eq!(find("foo(?!bar)", "", "foobaz").as_deref(), Some("foo"));
        assert_eq!(find("foo(?!bar)", "", "foobar"), None);
    }

    #[test]
    fn ignore_case_and_sticky() {
        assert_eq!(find("abc", "i", "xABC").as_deref(), Some("ABC"));
        assert_eq!(find("[a-z]+", "i", "ABC").as_deref(), Some("ABC"));
        let regex = Regex::compile("b", Flags::parse("y").expect("flags")).expect("pattern");
        let input: Vec<char> = "abc".chars().collect();
        assert!(regex.exec_at(&input, 1).is_some());
        // Sticky refuses to search forward.
        assert!(regex.exec_at(&input, 0).is_none());
    }

    #[test]
    fn unsupported_syntax_is_reported() {
        assert!(Regex::compile("(?<=a)b", Flags::default()).is_err());
        assert!(Regex::compile("(?<name>a)", Flags::default()).is_err());
        assert!(Regex::compile("(a", Flags::default()).is_err());
        assert!(Regex::compile("a)", Flags::default()).is_err());
        assert!(Regex::compile("*a", Flags::default()).is_err());
        assert!(Regex::compile("[z-a]", Flags::default()).is_err());
    }

    #[test]
    fn empty_matches_do_not_loop() {
        let regex = Regex::compile("(?:)", Flags::default()).expect("pattern");
        assert!(regex.nullable);
        let input: Vec<char> = "ab".chars().collect();
        assert_eq!(
            regex.exec_at(&input, 0).map(|m| (m.start, m.end)),
            Some((0, 0))
        );
    }
}