diff options
author | JSDurand <mmemmew@gmail.com> | 2022-12-14 23:48:22 +0800 |
---|---|---|
committer | JSDurand <mmemmew@gmail.com> | 2022-12-14 23:48:22 +0800 |
commit | 9f1c88b863e247da3cd60d2792a7a13b18e25e53 (patch) | |
tree | d29c0e19793a88a1de6898fdfd2a376fca21378f /nfa/src/desrec.rs | |
parent | cb7bcfad4ab0041aaf3fde3185e27ee46bb37788 (diff) |
a temporary check point
just to save things in a commit
Diffstat (limited to 'nfa/src/desrec.rs')
-rw-r--r-- | nfa/src/desrec.rs | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/nfa/src/desrec.rs b/nfa/src/desrec.rs new file mode 100644 index 0000000..c57d313 --- /dev/null +++ b/nfa/src/desrec.rs @@ -0,0 +1,50 @@ +//! 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; + + /// Auxiliary data when parsing + type Aux; + + /// 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>: FnMut(&'a str) -> Result<Option<(usize, Self::Label, Self::Aux)>, Self::Error>; + + /// 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>( + input: &'a str, + scanner: Self::Scanner<'a>, + post_p: bool, + ) -> Result<ParseOutput<'a, Self::Regex>, Self::Error>; +} |