diff options
| author | John Turner <jturner.usa@gmail.com> | 2025-10-26 01:10:37 -0400 |
|---|---|---|
| committer | John Turner <jturner.usa@gmail.com> | 2025-10-26 01:10:37 -0400 |
| commit | 157c1db358bf31dc9a409a1a9d9b5d4e57936f31 (patch) | |
| tree | f3d637a96e84d15ca65ddc24f74e3cb214df9903 | |
| parent | 1426e5b7e3bee4ddc569e4cce5abfd59523b9ee8 (diff) | |
| download | mon-157c1db358bf31dc9a409a1a9d9b5d4e57936f31.tar.gz | |
impl Regex parser
| -rw-r--r-- | Cargo.lock | 47 | ||||
| -rw-r--r-- | Cargo.toml | 5 | ||||
| -rw-r--r-- | src/lib.rs | 42 |
3 files changed, 93 insertions, 1 deletions
@@ -3,5 +3,52 @@ version = 4 [[package]] +name = "aho-corasick" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" +dependencies = [ + "memchr", +] + +[[package]] +name = "memchr" +version = "2.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f52b00d39961fc5b2736ea853c9cc86238e165017a493d1d5c8eac6bdc4cc273" + +[[package]] name = "mon" version = "0.1.0" +dependencies = [ + "regex", +] + +[[package]] +name = "regex" +version = "1.12.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "843bc0191f75f3e22651ae5f1e72939ab2f72a4bc30fa80a066bd66edefc24d4" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5276caf25ac86c8d810222b3dbb938e512c55c6831a10f3e6ed1c93b84041f1c" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a2d987857b319362043e95f5353c0535c1f58eec5336fdfcf626430af7def58" @@ -1,4 +1,7 @@ [package] name = "mon" version = "0.1.0" -edition = "2024"
\ No newline at end of file +edition = "2024" + +[dependencies] +regex = "1.12.2"
\ No newline at end of file @@ -1002,6 +1002,35 @@ where take_while1(r#if(|c: &I::Item| c.is_whitespace())) } +pub mod str { + + use super::*; + + struct Regex(regex::Regex); + + impl<'a> Parser<&'a str> for Regex { + type Output = &'a str; + + fn run<OM: Mode, EM: Mode, Tracer: Trace<&'a str>>( + &self, + it: InputIter<&'a str>, + ) -> ParserResult<&'a str, Self::Output, OM, EM> { + match self.0.find(it.rest()) { + Some(m) => { + let output = OM::bind(|| it.rest().slice(m.range())); + let (rest, _) = take(m.end()).parse(it).unwrap(); + Ok((rest, output)) + } + None => return Err(EM::bind(|| it.clone())), + } + } + } + + pub fn regex<'a>(pattern: &'a str) -> impl Parser<&'a str, Output = &'a str> { + Regex(regex::Regex::new(pattern).unwrap()) + } +} + #[cfg(test)] mod test { use super::*; @@ -1016,4 +1045,17 @@ mod test { .check_finished(it) .unwrap(); } + + #[test] + fn test_regex_parser() { + let it = InputIter::new("abc 123"); + + match crate::str::regex("[abc]+").parse(it) { + Ok((rest, output)) => { + assert_eq!(output, "abc"); + assert_eq!(rest.rest(), " 123"); + } + _ => panic!(), + } + } } |
