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
extern crate regex;
extern crate serde_regex;
use crate::grammar::Grammar;
use crate::util::{Expression, Flags};
use regex::{Captures, Error, Regex};
use std::cmp::{Eq, Ord, Ordering, PartialEq, PartialOrd};
use std::collections::HashMap;

/// This is functionally equivalent to a [`Regex`]: you can use it repeatedly to
/// search a string. It cannot itself be used directly to split strings, but
/// its regular expression is public and may be so used. It improves on regular
/// expressions in that the [`Match`] object it returns is the root node in a
/// parse tree, so its matches preserve parse structure.
///
/// [`Regex`]: ../regex/struct.Regex.html
/// [`Match`]: ../pidgin/struct.Match.html
#[derive(Debug, Serialize, Deserialize)]
pub struct Matcher {
    /// The `Regex` used for parsing.
    #[serde(with = "serde_regex")]
    pub rx: Regex,
    root: String,
    translation: HashMap<String, String>,
    parentage: HashMap<String, Vec<String>>,
}

impl Matcher {
    /// Returns `Some(Match)` if the grammar can parse the string. Note that
    /// unless the grammar is string-bounded, this only means it can parse
    /// the string at some point.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # #[macro_use] extern crate pidgin;
    /// # use std::error::Error;
    /// # fn demo() -> Result<(), Box<Error>> {
    /// let m = grammar!{
    ///
    /// 	(?bB)
    ///
    /// 	S -> r(r"\A") <subject> <VP> r(r"\.\z")
    ///
    /// 	subject           => [["Amy", "Bob", "Carter", "Dianne"]]
    /// 	VP                -> <verb_intransitive> | <verb_transitive> <object>
    /// 	verb_intransitive => [["naps", "doodles", "exercises", "meditates"]]
    /// 	verb_transitive   => [["eats", "programs", "sees", "throws"]]
    /// 	object            => (?w) [["a sandwich", "eggs", "the sunset"]]
    ///
    /// }.matcher()?;
    ///
    /// let parse_tree = m.parse("Amy programs the sunset.").unwrap();
    ///
    /// println!("{}", parse_tree);
    ///
    /// // S (0, 24): "Amy programs the sunset."
    /// //   subject (0, 3): "Amy"
    /// //   VP (4, 23): "programs the sunset"
    /// //     verb_transitive (4, 12): "programs"
    /// //     object (13, 23): "the sunset"
    /// # Ok(()) }
    /// ```
    pub fn parse<'t>(&self, s: &'t str) -> Option<Match<'t>> {
        match self.rx.captures(s) {
            Some(captures) => {
                let c = captures.get(0).unwrap();
                let mut m = Match {
                    rule: self.root.clone(),
                    text: s,
                    start: c.start(),
                    end: c.end(),
                    children: None,
                };
                self.build_tree(&mut m, &self.root, s, &captures);
                let mut m = self.simplify_tree(m);
                self.sort_tree(&mut m);
                Some(m)
            }
            None => None,
        }
    }
    // put the children in match order
    fn sort_tree<'t>(&self, m: &mut Match) {
        if let Some(ref mut children) = m.children {
            children.sort();
            for c in children {
                self.sort_tree(c);
            }
        }
    }
    fn simplify_tree<'t>(&self, m: Match<'t>) -> Match<'t> {
        if m.children.is_some() {
            let mut clone = m.clone();
            let children: Vec<Match> = m
                .children
                .unwrap()
                .into_iter()
                .map(|c| self.simplify_tree(c))
                .collect();
            if children.len() == 1 && m.rule == children[0].rule {
                children[0].clone()
            } else {
                clone.children = Some(children);
                clone
            }
        } else {
            m
        }
    }
    /// Returns whether the grammar can parse the string. This is a cheaper
    /// operation than parsing.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # #[macro_use] extern crate pidgin;
    /// # use std::error::Error;
    /// # fn demo() -> Result<(), Box<Error>> {
    /// let m = grammar!{
    ///
    /// 	(?bB)
    ///
    /// 	S -> r(r"\A") <subject> <VP> r(r"\.\z")
    ///
    /// 	subject           => [["Amy", "Bob", "Carter", "Dianne"]]
    /// 	VP                -> <verb_intransitive> | <verb_transitive> <object>
    /// 	verb_intransitive => [["naps", "doodles", "exercises", "meditates"]]
    /// 	verb_transitive   => [["eats", "programs", "sees", "throws"]]
    /// 	object            => (?w) [["a sandwich", "eggs", "the sunset"]]
    ///
    /// }.matcher()?;
    ///
    /// assert!(m.is_match("Bob doodles."));
    /// # Ok(()) }
    /// ```
    pub fn is_match(&self, text: &str) -> bool {
        self.rx.is_match(text)
    }
    // recursively build a parse tree showing the groups matched
    fn build_tree<'t>(&self, m: &mut Match<'t>, parent: &str, text: &'t str, captures: &Captures) {
        if let Some(children) = self.parentage.get(parent) {
            for c in children {
                if let Some(n) = captures.name(c) {
                    let name = self.translation.get(c).unwrap();
                    let mut child = Match {
                        text,
                        rule: name.clone(),
                        start: n.start(),
                        end: n.end(),
                        children: None,
                    };
                    self.build_tree(&mut child, &c, text, captures);
                    m.children.get_or_insert_with(|| Vec::new()).push(child);
                }
            }
        }
    }
    pub(crate) fn new(g: &Grammar) -> Result<Matcher, Error> {
        let mut idx = 0;
        let mut translation = HashMap::new();
        let mut parentage = HashMap::new();
        let root = g.name.clone().unwrap_or(String::from("0"));
        let mut g = g.clone();
        g.name = Some(root.clone());
        let mut g = Expression::Grammar(g.clone(), false);
        Matcher::walk(&mut idx, &root, &mut g, &mut translation, &mut parentage);
        if let Expression::Grammar(g, _) = g {
            match Regex::new(&g.to_s(&Flags::defaults(), false, false)) {
                Ok(rx) => Ok(Matcher {
                    translation,
                    parentage,
                    rx,
                    root,
                }),
                Err(e) => Err(e),
            }
        } else {
            unreachable!()
        }
    }
    fn walk(
        idx: &mut usize,
        parent: &str,
        e: &mut Expression,
        translation: &mut HashMap<String, String>,
        parentage: &mut HashMap<String, Vec<String>>,
    ) {
        match e {
            Expression::Sequence(v, _) => {
                for e in v {
                    Matcher::walk(idx, parent, e, translation, parentage);
                }
            }
            Expression::Repetition(e, _, _) => {
                Matcher::walk(idx, parent, e, translation, parentage)
            }
            Expression::Alternation(v, _) => {
                for e in v {
                    Matcher::walk(idx, parent, e, translation, parentage);
                }
            }
            Expression::Grammar(g, _) => {
                if let Some(n) = g.name.clone() {
                    let new_name = format!("m{}", idx);
                    *idx += 1;
                    translation.insert(new_name.clone(), n);
                    parentage
                        .entry(parent.to_string())
                        .or_insert_with(|| Vec::new())
                        .push(new_name.clone());
                    for e in &mut g.sequence {
                        Matcher::walk(idx, &new_name, &mut *e, translation, parentage);
                    }
                    g.name = Some(new_name.clone());
                } else {
                    for ref mut e in &mut g.sequence {
                        Matcher::walk(idx, parent, e, translation, parentage);
                    }
                }
            }
            _ => (),
        }
    }
}

/// This is a node in a parse tree. It is functionally similar to [`regex::Match`],
/// in fact providing much the same API, but unlike a `regex::Match` a `pidgin::Match`
/// always corresponds to some rule, it knows what rule it corresponds to,
/// and it records any sub-matches involved in its parsing.
///
/// The lifetime parameter `'t` represents the lifetime of the `&str` matched
/// against.
///
/// [`regex::Match`]: ../regex/struct.Match.html
#[derive(Debug, Clone)]
pub struct Match<'t> {
    rule: String,
    text: &'t str,
    start: usize,
    end: usize,
    children: Option<Vec<Match<'t>>>,
}

impl<'t> Match<'t> {
    /// Returns the matched text.
    ///
    /// # Examples
    ///  
    /// ```rust
    /// # #[macro_use] extern crate pidgin;
    /// # use std::error::Error;
    /// # fn demo() -> Result<(), Box<Error>> {
    /// let m = grammar!{
    /// 	foo => ("bar")
    /// }.matcher()?.parse("   bar   ").unwrap();
    ///
    /// assert_eq!("bar", m.as_str());
    /// # Ok(()) }
    /// ```
    pub fn as_str(&self) -> &'t str {
        &self.text[self.start..self.end]
    }
    /// Returns the starting offset of the match.
    ///
    /// # Examples
    ///  
    /// ```rust
    /// # #[macro_use] extern crate pidgin;
    /// # use std::error::Error;
    /// # fn demo() -> Result<(), Box<Error>> {
    /// let m = grammar!{
    /// 	foo => ("bar")
    /// }.matcher()?.parse("   bar   ").unwrap();
    ///
    /// assert_eq!(3, m.start());
    /// # Ok(()) }
    /// ```
    pub fn start(&self) -> usize {
        self.start
    }
    /// Returns the ending offset of the match.
    ///
    /// # Examples
    ///  
    /// ```rust
    /// # #[macro_use] extern crate pidgin;
    /// # use std::error::Error;
    /// # fn demo() -> Result<(), Box<Error>> {
    /// let m = grammar!{
    /// 	foo => ("bar")
    /// }.matcher()?.parse("   bar   ").unwrap();
    ///
    /// assert_eq!(6, m.end());
    /// # Ok(()) }
    /// ```
    pub fn end(&self) -> usize {
        self.end
    }
    /// Returns the grammar rule matched.
    ///
    /// # Examples
    ///  
    /// ```rust
    /// # #[macro_use] extern crate pidgin;
    /// # use std::error::Error;
    /// # fn demo() -> Result<(), Box<Error>> {
    /// let m = grammar!{
    /// 	foo => ("bar")
    /// }.matcher()?.parse("   bar   ").unwrap();
    ///
    /// assert_eq!("foo", m.rule());
    /// # Ok(()) }
    /// ```
    pub fn rule(&self) -> &str {
        &self.rule
    }
    /// Returns the sub-matches of this match, if any.
    ///
    /// # Examples
    ///  
    /// ```rust
    /// # #[macro_use] extern crate pidgin;
    /// # use std::error::Error;
    /// # fn demo() -> Result<(), Box<Error>> {
    /// let m = grammar!{
    /// 	TOP -> <foo> <bar> <baz>
    /// 	foo => (1)
    /// 	bar => (2)
    /// 	baz => (3)
    /// }.matcher()?.parse(" 1  2  3 ").unwrap();
    ///
    /// let children = m.children().unwrap();
    ///
    /// assert_eq!(3, children.len());
    /// assert_eq!("1", children[0].as_str());
    /// assert_eq!("foo", children[0].rule());
    /// assert_eq!("2", children[1].as_str());
    /// assert_eq!("bar", children[1].rule());
    /// assert_eq!("3", children[2].as_str());
    /// assert_eq!("baz", children[2].rule());
    /// # Ok(()) }
    /// ```
    pub fn children(&self) -> Option<&[Match<'t>]> {
        match self.children.as_ref() {
            Some(v) => Some(v),
            None => None,
        }
    }
    /// Returns the first `Match` defined by the given rule under this parse
    /// node searching recursively, depth-first, left-to-right.
    ///
    /// # Examples
    ///  
    /// ```rust
    /// # #[macro_use] extern crate pidgin;
    /// # use std::error::Error;
    /// # fn demo() -> Result<(), Box<Error>> {
    /// let matcher = grammar!{
    /// 	TOP -> <foo> <bar>
    /// 	foo -> (1) <baz>
    /// 	bar -> (2) <baz>
    /// 	baz -> [["prawn", "shrimp", "crevette"]]
    /// }.matcher()?;
    ///
    /// let p = matcher.parse("1 crevette 2 shrimp").unwrap();
    /// let baz = p.name("baz").unwrap();
    ///
    /// assert_eq!("crevette", baz.as_str());
    /// # Ok(()) }
    /// ```
    pub fn name(&self, name: &str) -> Option<&Match> {
        if self.rule == name {
            Some(self)
        } else {
            if self.children.is_some() {
                for m in self.children.as_ref().unwrap() {
                    if let Some(n) = m.name(name) {
                        return Some(n);
                    }
                }
            }
            None
        }
    }
    /// Returns all `Match`es matching the given rule in the parse tree under
    /// this node. Matches are ordered as found by a depth-first left-to-right
    /// search of the parse tree.
    ///
    /// # Examples
    ///  
    /// ```rust
    /// # #[macro_use] extern crate pidgin;
    /// # use std::error::Error;
    /// # fn demo() -> Result<(), Box<Error>> {
    /// let matcher = grammar!{
    /// 	TOP -> <foo> <bar>
    /// 	foo -> (1) <baz>
    /// 	bar -> (2) <baz>
    /// 	baz -> [["prawn", "shrimp", "crevette"]]
    /// }.matcher()?;
    ///
    /// let p = matcher.parse("1 crevette 2 shrimp").unwrap();
    /// let names = p.all_names("baz");
    ///
    /// assert_eq!("crevette", names[0].as_str());
    /// assert_eq!("shrimp", names[1].as_str());
    /// # Ok(()) }
    /// ```
    pub fn all_names(&self, name: &str) -> Vec<&Match> {
        let mut v = Vec::new();
        self.collect(name, &mut v);
        v
    }
    /// Returns whether the given rule matched for any node in the parse tree.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # #[macro_use] extern crate pidgin;
    /// # use std::error::Error;
    /// # fn demo() -> Result<(), Box<Error>> {
    /// let g = grammar!{
    ///     TOP => <animal> | <thing>
    ///     animal => [["cat", "dog", "camel"]]
    ///     thing  => [["carpet", "crate", "cartoon"]]
    /// };
    ///
    /// let m = g.matcher()?;
    ///
    /// assert!(m.parse("cat").unwrap().has("animal"));
    /// # Ok(())}
    /// ```
    pub fn has(&self, name: &str) -> bool {
        self.rule == name
            || self.children.is_some()
                && self
                    .children
                    .as_ref()
                    .unwrap()
                    .iter()
                    .any(|ref m| m.has(name))
    }
    fn collect(&'t self, name: &str, names: &mut Vec<&'t Match<'t>>) {
        if self.rule == name {
            names.push(self);
        }
        if self.children.is_some() {
            for m in self.children.as_ref().unwrap() {
                m.collect(name, names);
            }
        }
    }
    // display the parse tree with indentation
    fn _fmt(&self, depth: usize, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        let own = format!(
            "{}{} ({}, {}): {}\n",
            "  ".repeat(depth),
            self.rule,
            self.start,
            self.end,
            format!("{:?}", self.as_str())
        );
        if let Some(ref children) = &self.children {
            let mut r = write!(f, "{}", own);
            for c in children {
                r = c._fmt(depth + 1, f);
            }
            r
        } else {
            write!(f, "{}", own)
        }
    }
}

impl<'t> std::fmt::Display for Match<'t> {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        self._fmt(0, f)
    }
}

impl<'t> PartialEq for Match<'t> {
    fn eq(&self, other: &Match<'t>) -> bool {
        self.cmp(other) == Ordering::Equal && self.rule == other.rule
    }
}

impl<'t> Eq for Match<'t> {}

impl<'t> PartialOrd for Match<'t> {
    fn partial_cmp(&self, other: &Match<'t>) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl<'t> Ord for Match<'t> {
    fn cmp(&self, other: &Match<'t>) -> Ordering {
        let o = self.start.cmp(&other.start);
        match o {
            Ordering::Equal => self.end.cmp(&other.end),
            _ => o,
        }
    }
}