From c5b351e8eeccd624820e08564c2e42dbb0811c4e Mon Sep 17 00:00:00 2001 From: Greg Shuflin Date: Sun, 16 Oct 2022 17:33:10 -0700 Subject: [PATCH] Move seq into separate module --- src/lib.rs | 26 ++------------------------ src/sequence.rs | 31 +++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 24 deletions(-) create mode 100644 src/sequence.rs diff --git a/src/lib.rs b/src/lib.rs index 27148c2..68fce03 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,7 @@ #![feature(assert_matches)] #![allow(dead_code)] //TODO eventually turn this off mod bnf; +mod sequence; use bnf::Bnf; use std::rc::Rc; @@ -45,7 +46,7 @@ trait Parser { E: 'a, P: Parser + 'a, { - BoxedParser::new(seq(self, next_parser)) + BoxedParser::new(sequence::seq(self, next_parser)) } } @@ -107,20 +108,6 @@ where } } -fn seq(parser1: P1, parser2: P2) -> impl Parser -where - P1: Parser, - P2: Parser, -{ - move |input| { - parser1.parse(input).and_then(|(result1, rest1)| { - parser2 - .parse(rest1) - .map(|(result2, rest2)| ((result1, result2), rest2)) - }) - } -} - fn pred(parser: P, pred_fn: F) -> impl Parser where P: Parser, @@ -237,15 +224,6 @@ mod tests { assert_matches!(output.unwrap(), (s, " yolo") if s == "A"); } - #[test] - fn test_seq() { - let p = seq(identifier, seq(literal(" "), literal("ruts"))); - assert_matches!(p.parse("fort1 ruts"), Ok((r, "")) if r.0 == "fort1" && r.1 == (" ", "ruts") ); - - let p = identifier.then(literal(" ")).then(literal("ruts")); - assert_matches!(p.parse("fort1 ruts"), Ok((r, "")) if r.0.0 == "fort1" && r.0.1== " " && r.1 == "ruts"); - } - #[test] fn test_one_or_more() { let p = one_or_more(literal("bongo ")); diff --git a/src/sequence.rs b/src/sequence.rs new file mode 100644 index 0000000..f4db4e2 --- /dev/null +++ b/src/sequence.rs @@ -0,0 +1,31 @@ +use crate::Parser; + +pub(crate) fn seq(parser1: P1, parser2: P2) -> impl Parser +where + P1: Parser, + P2: Parser, +{ + move |input| { + parser1.parse(input).and_then(|(result1, rest1)| { + parser2 + .parse(rest1) + .map(|(result2, rest2)| ((result1, result2), rest2)) + }) + } +} + +#[cfg(test)] +mod test { + use super::*; + use crate::{identifier, literal}; + use std::assert_matches::assert_matches; + + #[test] + fn test_seq() { + let p = seq(identifier, seq(literal(" "), literal("ruts"))); + assert_matches!(p.parse("fort1 ruts"), Ok((r, "")) if r.0 == "fort1" && r.1 == (" ", "ruts") ); + + let p = identifier.then(literal(" ")).then(literal("ruts")); + assert_matches!(p.parse("fort1 ruts"), Ok((r, "")) if r.0.0 == "fort1" && r.0.1== " " && r.1 == "ruts"); + } +}