structex 0.6.0

A structural regular expression engine
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
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
//! Ast parsing for structural regular expressions.
use crate::parse::{ParseInput, Span};
use std::fmt;

/// An error that can arise during expression parsing.
pub type ParseError = crate::parse::Error<ErrorKind>;

/// A list specifying the different categories of errors that can be encountered while parsing a
/// structural regular expression.
///
/// It is used with the [ParseError] type.
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ErrorKind {
    /// A parallel group was provided without any branches.
    EmptyGroup,
    /// A branch in a parallel group contained no expressions.
    EmptyGroupBranch,
    /// The provided expression was empty.
    EmptyExpression,
    /// The provided top level expression was a single action.
    /// Such an expression would only ever run the action once for all inputs.
    TopLevelAction,
    /// Comilation options require an action for all matches but none was specified.
    MissingAction,
    /// An expected delimiter was not found.
    ///
    /// Typically this is encountered when an parallel group or delimited string is missing its
    /// closing delimiter.
    MissingDelimiter(char),
    /// Multiple distinct expressions were present in the provided input.
    ///
    /// To run multiple expressions over a haystack you should provide a top level parallel group.
    MultipleBranches,
    /// An unexpected character was encountered in the input expression.
    UnexpectedChar(char),
    /// End of file was encountered before a full expression could be parsed.
    UnexpectedEof,
    /// An action tag was found that did not match the allowed single argument tags specified as
    /// part of a [StructexBuilder][crate::StructexBuilder].
    UnknownArgumentTag(char),
    /// An action tag was found that did not match the allowed zero argument tags specified as
    /// part of a [StructexBuilder][crate::StructexBuilder].
    UnknownTag(char),
}

impl fmt::Display for ErrorKind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::EmptyGroup => write!(f, "empty group"),
            Self::EmptyGroupBranch => write!(f, "empty group branch"),
            Self::EmptyExpression => write!(f, "empty program"),
            Self::MissingAction => write!(f, "missing action"),
            Self::MissingDelimiter(ch) => write!(f, "missing delimiter '{ch}'"),
            Self::MultipleBranches => write!(f, "multiple branches when one was expected"),
            Self::TopLevelAction => write!(f, "top level expression can not be an action"),
            Self::UnexpectedChar(ch) => write!(f, "unexpected character in input '{ch}'"),
            Self::UnexpectedEof => write!(f, "unexpected EOF"),
            Self::UnknownArgumentTag(ch) => {
                write!(f, "{ch} is not a registered single argument tag")
            }
            Self::UnknownTag(ch) => write!(f, "{ch} is not a registered bare tag"),
        }
    }
}

#[derive(Debug, PartialEq, Eq)]
pub(super) enum Ast {
    /// If the current dot narrows to the given regex, replace dot and run the given nodes in
    /// series.
    Narrow(ReNode),

    /// Extract all non-overlapping matches of a regex from dot and
    /// then run the the provided nodes over each match.
    Extract(ReNode),
    /// Extract all non-overlapping matches of a regex from dot and
    /// then run the the provided nodes over the regions between
    /// each match.
    ExtractBetween(ReNode),

    /// If the current dot matches the given regex run the given
    /// nodes in parallel over dot.
    Guard(ReNode),
    /// If the current dot doesn't match the given regex run the given
    /// nodes in parallel over dot.
    InvGuard(ReNode),

    /// Run each node in parallel over the current dot.
    Parallel(Sequence),

    /// An action to be emitted alongside the corresponding match.
    Action(Action),

    /// Comments are captured to correctly track spans but are
    /// dropped when compiling
    Comment(Span),
}

impl Ast {
    fn is_comment(&self) -> bool {
        matches!(self, Ast::Comment(_))
    }

    pub fn span(&self) -> &Span {
        match self {
            Ast::Narrow(n)
            | Ast::Extract(n)
            | Ast::ExtractBetween(n)
            | Ast::Guard(n)
            | Ast::InvGuard(n) => &n.span,
            Ast::Parallel(s) => &s.span,
            Ast::Action(a) => &a.span,
            Ast::Comment(s) => s,
        }
    }
}

// Spanned Ast nodes

#[derive(Debug, PartialEq, Eq)]
pub(super) struct ReNode {
    pub span: Span,
    pub re: String,
    pub node: Box<Ast>,
}

#[derive(Debug, PartialEq, Eq)]
pub(super) struct Sequence {
    pub span: Span,
    pub nodes: Vec<Ast>,
}

#[derive(Debug, PartialEq, Eq)]
pub(super) struct Action {
    pub span: Span,
    pub tag: Option<char>,
    pub s: Option<String>,
}

#[derive(Debug)]
pub(super) struct Parser<'i, 'c> {
    input: ParseInput<'i>,
    require_actions: bool,
    allow_top_level_actions: bool,
    allowed_argless_tags: Option<&'c str>,
    allowed_single_arg_tags: Option<&'c str>,
}

impl<'i, 'c> Parser<'i, 'c> {
    pub fn new(prog: &'i str) -> Self {
        Self {
            input: ParseInput::new(prog),
            require_actions: false,
            allow_top_level_actions: false,
            allowed_argless_tags: None,
            allowed_single_arg_tags: None,
        }
    }

    pub fn require_actions(mut self, require_actions: bool) -> Self {
        self.require_actions = require_actions;
        self
    }

    pub fn allow_top_level_actions(mut self, allow: bool) -> Self {
        self.allow_top_level_actions = allow;
        self
    }

    pub fn with_allowed_argless_tags(mut self, allowed: Option<&'c str>) -> Self {
        self.allowed_argless_tags = allowed;
        self
    }

    pub fn with_allowed_single_arg_tags(mut self, allowed: Option<&'c str>) -> Self {
        self.allowed_single_arg_tags = allowed;
        self
    }

    pub fn parse(&self) -> Result<Ast, ParseError> {
        self.reset();
        let mut seq = Sequence {
            span: self.input.span(),
            nodes: vec![],
        };

        while !self.input.at_eof() {
            let node = self.parse1(false)?;
            if !node.is_comment() {
                seq.nodes.push(node);
            }
            self.input.consume_whitespace();
        }

        match seq.nodes.len() {
            0 => Err(self.error(ErrorKind::EmptyExpression)),
            1 => match seq.nodes.remove(0) {
                Ast::Action(_) if !self.allow_top_level_actions => {
                    Err(self.error(ErrorKind::TopLevelAction))
                }
                Ast::Comment(_) => unreachable!(),
                node => Ok(node),
            },
            _ => Err(self.error(ErrorKind::MultipleBranches)),
        }
    }

    fn parse1(&self, in_group: bool) -> Result<Ast, ParseError> {
        self.input.consume_whitespace();

        while !self.input.at_eof() {
            match self.parse1_inner(in_group)? {
                Ast::Comment(_) => self.input.consume_whitespace(),
                node => return Ok(node),
            }
        }

        Err(self.error(ErrorKind::UnexpectedEof))
    }

    /// Must be called after consuming whitespace and comments
    fn parse1_inner(&self, in_group: bool) -> Result<Ast, ParseError> {
        let ast = match self.input.char() {
            '#' => Ast::Comment(self.parse_comment()),

            // Braced group
            '{' => Ast::Parallel(self.parse_group()?),

            // Narrow
            'n' => Ast::Narrow(self.parse_narrow(in_group)?),

            // Extract
            'x' => Ast::Extract(self.parse_extract(in_group)?),
            'y' => Ast::ExtractBetween(self.parse_extract(in_group)?),

            // Guard
            'g' => Ast::Guard(self.parse_guard(in_group)?),
            'v' => Ast::InvGuard(self.parse_guard(in_group)?),

            'X' => {
                let start = self.input.pos();
                self.input.advance();
                let node = Box::new(self.parse1(in_group)?);

                Ast::Extract(ReNode {
                    span: self.span().with_start(start),
                    re: ".*\n".to_string(),
                    node,
                })
            }

            ';' => return Err(self.error(ErrorKind::UnexpectedChar(';'))),
            '}' => return Err(self.error(ErrorKind::UnexpectedChar('}'))),

            // Default to trying to parse an action
            _ => Ast::Action(self.parse_action(in_group)?),
        };

        Ok(ast)
    }

    fn reset(&self) {
        self.input.reset();
    }

    /// Crate a null span at the current parser position
    fn span(&self) -> Span {
        self.input.span()
    }

    fn error(&self, kind: ErrorKind) -> ParseError {
        ParseError::new(kind, self.input.text(), self.input.span_char())
    }

    fn emit_match(&self) -> Ast {
        Ast::Action(Action {
            span: self.span(),
            tag: None,
            s: None,
        })
    }

    fn parse1_or_emit_match(&self, in_group: bool) -> Result<Ast, ParseError> {
        if self.input.at_eof() {
            return if self.require_actions {
                Err(self.error(ErrorKind::MissingAction))
            } else {
                Ok(self.emit_match())
            };
        }

        match self.parse1(in_group) {
            Ok(node) => Ok(node),

            Err(e) if e.kind == ErrorKind::UnexpectedEof => {
                if self.require_actions {
                    Err(self.error(ErrorKind::MissingAction))
                } else {
                    Ok(self.emit_match())
                }
            }

            Err(e) if in_group && e.kind == ErrorKind::UnexpectedChar(';') => {
                if self.require_actions {
                    Err(self.error(ErrorKind::MissingAction))
                } else {
                    Ok(self.emit_match())
                }
            }

            Err(e) => Err(e),
        }
    }

    fn parse_comment(&self) -> Span {
        assert_eq!(self.input.char(), '#');
        let span = self.input.span();
        let mut end = self.input.pos();

        loop {
            if self.input.try_consume("#") {
                self.input.consume_until('\n');
                end = self.input.pos();
                self.input.consume_whitespace();
            } else {
                break;
            }
        }

        span.with_end(end)
    }

    fn parse_action(&self, in_group: bool) -> Result<Action, ParseError> {
        let start = self.input.pos();
        let tag = self.input.char();
        self.input.advance();

        let should_parse_arg = match self.input.try_char() {
            Some(';') if !in_group => return Err(self.error(ErrorKind::UnexpectedChar(';'))),
            Some(';') => false,
            Some(ch) if ch.is_whitespace() => false,
            None => false,
            Some(_) => true,
        };

        let s = if should_parse_arg {
            if let Some(allowed) = self.allowed_single_arg_tags.as_ref()
                && !allowed.contains(tag)
            {
                return Err(self.error(ErrorKind::UnknownArgumentTag(tag)));
            }

            Some(self.parse_delimited_str()?)
        } else {
            if let Some(allowed) = self.allowed_argless_tags.as_ref()
                && !allowed.contains(tag)
            {
                return Err(self.error(ErrorKind::UnknownTag(tag)));
            }

            None
        };

        Ok(Action {
            span: self.span().with_start(start),
            tag: Some(tag),
            s,
        })
    }

    fn parse_narrow(&self, in_group: bool) -> Result<ReNode, ParseError> {
        assert_eq!(self.input.char(), 'n');

        let start = self.input.pos();
        self.input.advance();
        let re = self.parse_delimited_str()?;
        let node = Box::new(self.parse1_or_emit_match(in_group)?);
        let end = node.span().end;

        Ok(ReNode {
            span: Span::new(start, end),
            re,
            node,
        })
    }

    fn parse_extract(&self, in_group: bool) -> Result<ReNode, ParseError> {
        assert!("xy".contains(self.input.char()));

        let start = self.input.pos();
        self.input.advance();
        let re = self.parse_delimited_str()?;
        let node = Box::new(self.parse1_or_emit_match(in_group)?);
        let end = node.span().end;

        Ok(ReNode {
            span: Span::new(start, end),
            re,
            node,
        })
    }

    fn parse_guard(&self, in_group: bool) -> Result<ReNode, ParseError> {
        assert!("gv".contains(self.input.char()));

        let start = self.input.pos();
        self.input.advance();
        let re = self.parse_delimited_str()?;
        let node = Box::new(self.parse1_or_emit_match(in_group)?);
        let end = node.span().end;

        Ok(ReNode {
            span: Span::new(start, end),
            re,
            node,
        })
    }

    fn parse_group(&self) -> Result<Sequence, ParseError> {
        assert_eq!(self.input.char(), '{');
        let mut seq = Sequence {
            span: self.input.span(),
            nodes: vec![],
        };
        let mut branch = Vec::new();
        self.input.advance();

        loop {
            self.input.consume_whitespace();
            match self.input.try_char() {
                Some('}') => {
                    self.input.advance(); // consume the '}'

                    return if seq.nodes.is_empty() {
                        Err(self.error(ErrorKind::EmptyGroup))
                    } else {
                        seq.span = seq.span.with_end(self.input.pos());
                        Ok(seq)
                    };
                }
                Some(_) => (),
                None => return Err(self.error(ErrorKind::MissingDelimiter('}'))),
            }

            let node = self.parse1(true)?;
            if !node.is_comment() {
                branch.push(node);
            }

            self.input.consume_whitespace();
            if self.input.char() == ';' {
                self.input.advance(); // consume the ';'

                let node = match branch.len() {
                    0 => return Err(self.error(ErrorKind::EmptyGroupBranch)),
                    1 => branch.remove(0),
                    _ => return Err(self.error(ErrorKind::MultipleBranches)),
                };

                seq.nodes.push(node);
                branch = Vec::new();
            }
        }
    }

    /// Can't use ParseInput::read_until for this (or return &str) as we need to handle
    /// escaping the delimiter inside of the string.
    fn parse_delimited_str(&self) -> Result<String, ParseError> {
        let delim = match self.input.try_char() {
            Some(';') => return Err(self.error(ErrorKind::UnexpectedChar(';'))),
            None => return Err(self.error(ErrorKind::UnexpectedEof)),
            Some(ch) => ch,
        };

        if !self.input.advance() {
            return Err(self.error(ErrorKind::UnexpectedEof));
        }

        let mut s = String::new();
        let mut prev = delim;

        while !self.input.at_eof() {
            let ch = self.input.char();
            if ch == delim {
                if prev == '\\' {
                    s.push(ch);
                } else {
                    self.input.advance(); // consume the delimiter
                    return Ok(s);
                }
            } else {
                if prev == '\\' {
                    s.push('\\');
                }
                if ch != '\\' {
                    s.push(ch);
                }
            }
            prev = ch;
            self.input.advance();
        }

        Err(self.error(ErrorKind::MissingDelimiter(delim)))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::parse::Position;
    use simple_test_case::test_case;

    fn span(start: (usize, usize, usize), end: (usize, usize, usize)) -> Span {
        Span::new(
            Position::new(start.0, start.1, start.2),
            Position::new(end.0, end.1, end.2),
        )
    }

    fn action(tag: char, span: Span) -> Ast {
        Ast::Action(Action {
            span,
            tag: Some(tag),
            s: None,
        })
    }

    fn template_action(tag: char, s: &str, span: Span) -> Ast {
        Ast::Action(Action {
            span,
            tag: Some(tag),
            s: Some(s.to_string()),
        })
    }

    #[test_case("/foo/", "foo"; "slash")]
    #[test_case(r"/foo\/bar/", "foo/bar"; "slash with escape")]
    #[test_case(":foo:", "foo"; "colon")]
    #[test_case(r":foo\:bar:", "foo:bar"; "colon with escape")]
    #[test_case(".foo.", "foo"; "period")]
    #[test_case(r".foo\.bar.", "foo.bar"; "period with escape")]
    #[test_case("|foo|", "foo"; "pipe")]
    #[test_case(r"|foo\|bar|", "foo|bar"; "pipe with escape")]
    #[test]
    fn parse_delimited_str_accepts_different_delimiters(input: &str, expected: &str) {
        let p = Parser::new(input);
        let s = p.parse_delimited_str().unwrap();

        assert_eq!(s, expected);
    }

    #[test]
    fn parse_delimited_str_rejects_semicolon_as_a_delimiter() {
        let p = Parser::new(";foo;");
        let err = p.parse_delimited_str().unwrap_err();

        assert_eq!(err.kind, ErrorKind::UnexpectedChar(';'));
    }

    #[test]
    fn parse_action_returns_correct_span() {
        let p = Parser::new("c/bar/  # comment");
        let action = p.parse_action(false).unwrap();

        assert_eq!(action.tag, Some('c'));

        let substr = p.input.span_text(&action.span);
        assert_eq!(substr, "c/bar/");
    }

    #[test]
    fn parse_action_returns_correct_span_without_a_template() {
        let p = Parser::new("a # comment");
        let action = p.parse_action(false).unwrap();

        assert_eq!(action.tag, Some('a'));

        let substr = p.input.span_text(&action.span);
        assert_eq!(substr, "a");
    }

    #[test]
    fn parse_extract_returns_correct_span() {
        let p = Parser::new("x/foo/ a/baz/  # comment");
        let extract = p.parse_extract(false).unwrap();
        let substr = p.input.span_text(&extract.span);

        assert_eq!(substr, "x/foo/ a/baz/");
    }

    #[test]
    fn parse_guard_returns_correct_span() {
        let p = Parser::new("g/foo/ c/bar/   ");
        let guard = p.parse_guard(false).unwrap();
        let substr = p.input.span_text(&guard.span);

        assert_eq!(substr, "g/foo/ c/bar/");
    }

    #[test]
    fn parse_group_returns_correct_span() {
        let p = Parser::new("  { x/foo/ c/bar/;\nx/bar/ c/foo/; }    ");
        p.input.consume_whitespace();
        let seq = p.parse_group().unwrap();
        let substr = p.input.span_text(&seq.span);

        assert_eq!(substr, "{ x/foo/ c/bar/;\nx/bar/ c/foo/; }");
    }

    #[test_case("n/foo/ a",
        Ast::Narrow(ReNode {
            span: span((0, 1, 1), (8, 1, 9)),
            re: "foo".to_string(),
            node: Box::new(action('a', span((7, 1, 8), (8, 1, 9)))),
        });
        "narrow"
    )]
    #[test_case("x/foo/ a",
        Ast::Extract(ReNode {
            span: span((0, 1, 1), (8, 1, 9)),
            re: "foo".to_string(),
            node: Box::new(action('a', span((7, 1, 8), (8, 1, 9)))),
        });
        "extract"
    )]
    #[test_case("y/foo/ a",
        Ast::ExtractBetween(ReNode {
            span: span((0, 1, 1), (8, 1, 9)),
            re: "foo".to_string(),
            node: Box::new(action('a', span((7, 1, 8), (8, 1, 9)))),
        });
        "extract between"
    )]
    #[test_case("X a",
        Ast::Extract(ReNode {
            span: span((0, 1, 1), (3, 1, 4)),
            re: ".*\n".to_string(),
            node: Box::new(action('a', span((2, 1, 3), (3, 1, 4)))),
        });
        "extract lines"
    )]
    #[test_case("g/foo/ a",
        Ast::Guard(ReNode {
            span: span((0, 1, 1), (8, 1, 9)),
            re: "foo".to_string(),
            node: Box::new(action('a', span((7, 1, 8), (8, 1, 9)))),
        });
        "guard"
    )]
    #[test_case("v/foo/ a",
        Ast::InvGuard(ReNode {
            span: span((0, 1, 1), (8, 1, 9)),
            re: "foo".to_string(),
            node: Box::new(action('a', span((7, 1, 8), (8, 1, 9)))),
        });
        "inv-guard"
    )]
    #[test_case("{ a; b/foo/; }",
        Ast::Parallel(Sequence {
            span: span((0, 1, 1), (14, 1, 15)),
            nodes: vec![
                action('a', span((2, 1, 3), (3, 1, 4))),
                template_action('b', "foo", span((5, 1, 6), (11, 1, 12))),
            ],
        });
        "group"
    )]
    #[test]
    fn parse1_works(input: &str, expected: Ast) {
        let p = Parser::new(input);
        let ast = p.parse1(false).unwrap();
        assert_eq!(ast, expected);
    }
}