summaryrefslogtreecommitdiff
path: root/nfa/src/desrec.rs
blob: ac45c2dd1b4dd43959e7c60ebe1510db31952c24 (plain)
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
//! This file defines the expected behaviours of a recursive descent
//! parser.

use super::Regex;
use graph::GraphLabel;

/// A thin wrapper of the parse output, to simplify the function
/// signature a little.
pub type ParseOutput<'a, T> = Option<(T, &'a str)>;

/// Types implementing this trait provide a method to be recursively
/// parsed.
pub trait DesRec {
    /// The type of labels of the resulting regular expression.
    type Label: GraphLabel;

    /// The type of the resulting regular expression.
    type Regex: Regex<Self::Label>;

    /// The type of errors encountered when parsing.
    type Error: std::error::Error;

    /// Intermediate data when parsing
    type Inter;

    /// The type of a scanner that classifies inputs into tokens.
    ///
    /// The return type indicates the result of classification:
    ///
    /// - `Err(_)`: the classification fails
    ///
    /// - `Ok(None)`: the classification succeeds, and the parsing
    /// should stop here
    ///
    /// - `Ok(Some(_))`: the classification succeeds and the parsing
    /// should continue.
    type Scanner<'a, 'b>: FnMut(
        &'b Self,
        &'a str,
    )
        -> Result<Option<(usize, Self::Label, Self::Inter)>, Self::Error>
    where
        Self: 'b,
        Self::Label: 'b;

    /// Parse a string into a regular expression with the aid of this
    /// type.
    ///
    /// Accept a slice of string and return either a parsing error, or
    /// a pair of correctly parsed regular expression and the
    /// remaining slice.
    fn parse<'a, 'b>(
        &'b self,
        input: &'a str,
        scanner: Self::Scanner<'a, 'b>,
        post_p: bool,
    ) -> Result<ParseOutput<'a, Self::Regex>, Self::Error>
    where
        Self::Label: 'b;
}