changeset 2:24646d806b4d

Implement prototype of first combinator... will need to use macros later
author Lewin Bormann <lewin@lewin-bormann.info>
date Thu, 30 May 2019 00:40:34 +0200
parents 0ad857b005ae
children ff95584ff3c9
files src/combinators.rs src/lib.rs
diffstat 2 files changed, 43 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/combinators.rs	Thu May 30 00:40:34 2019 +0200
@@ -0,0 +1,41 @@
+use crate::parser::{ParseError, ParseResult, Parser};
+use crate::state::ParseState;
+
+pub struct Sequence<T> {
+    t: T,
+}
+
+impl<P1: Parser, P2: Parser> Parser for Sequence<(P1, P2)> {
+    type Result = (P1::Result, P2::Result);
+    fn parse(
+        &mut self,
+        st: &mut ParseState<impl Iterator<Item = char>>,
+    ) -> ParseResult<Self::Result> {
+        let hold = st.hold();
+        let r1 = self.t.0.parse(st);
+        if r1.is_err() {
+            st.reset(hold);
+            return Err(r1.err().unwrap());
+        }
+        let r2 = self.t.1.parse(st);
+        if r2.is_err() {
+            st.reset(hold);
+            return Err(r2.err().unwrap());
+        }
+        st.release(hold);
+        return Ok((r1.unwrap(), r2.unwrap()));
+    }
+}
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+    use crate::primitives::*;
+
+    #[test]
+    fn test_pair() {
+        let mut p = (Int, StringParser(" aaa".to_string()));
+        let mut ps = ParseState::new("123 aba");
+        assert_eq!(Ok((123, " aaa".to_string())), p.parse(&mut ps));
+    }
+}
--- a/src/lib.rs	Wed May 29 23:49:47 2019 +0200
+++ b/src/lib.rs	Thu May 30 00:40:34 2019 +0200
@@ -1,5 +1,6 @@
 #![allow(dead_code)]
 
-mod state;
+mod combinators;
 mod parser;
 mod primitives;
+mod state;