Browse Source

Add take_while combinator

Isaac Woods 5 years ago
parent
commit
3721671093
1 changed files with 21 additions and 0 deletions
  1. 21 0
      aml/src/parser.rs

+ 21 - 0
aml/src/parser.rs

@@ -191,6 +191,27 @@ where
     }
 }
 
+pub fn take_while<'a, 'c, P, R>(parser: P) -> impl Parser<'a, 'c, usize>
+where
+    'c: 'a,
+    P: Parser<'a, 'c, R>,
+{
+    move |mut input: &'a [u8], mut context: &'c mut AmlContext| {
+        let mut num_passed = 0;
+        loop {
+            match parser.parse(input, context) {
+                Ok((new_input, new_context, _)) => {
+                    input = new_input;
+                    context = new_context;
+                    num_passed += 1;
+                }
+                Err((_, context, AmlError::WrongParser)) => return Ok((input, context, num_passed)),
+                Err((_, context, err)) => return Err((input, context, err)),
+            }
+        }
+    }
+}
+
 pub fn consume<'a, 'c, F>(condition: F) -> impl Parser<'a, 'c, u8>
 where
     'c: 'a,