From 157c1db358bf31dc9a409a1a9d9b5d4e57936f31 Mon Sep 17 00:00:00 2001 From: John Turner Date: Sun, 26 Oct 2025 01:10:37 -0400 Subject: impl Regex parser --- Cargo.lock | 47 +++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 5 ++++- src/lib.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index f8fafb9..5ab13d1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,53 @@ # It is not intended for manual editing. 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" diff --git a/Cargo.toml b/Cargo.toml index e265454..dc005d8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 diff --git a/src/lib.rs b/src/lib.rs index 930acfe..050ed52 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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>( + &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!(), + } + } } -- cgit v1.2.3