Skip to content
Browse files

Added repeat1While primitive and tests for lists and array segments.

  • Loading branch information...
1 parent 1144417 commit f111576164de08b1b881b7d3c33a08b904c4cfe0 @panesofglass panesofglass committed Mar 6, 2011
View
16 src/Cashel.Tests/ArraySegmentPrimitivesTest.fs
@@ -117,6 +117,22 @@ let test_repeat1_no_matches () =
repeat1 (matchToken 'e'B) !!!ttttest ===> None
[<Test>]
+let test_repeat1While () =
+ repeat1While (fun xs -> xs.Length < 4) (matchToken 't'B) !!!ttttest ===> (Some(!!"tttt"B, !!+ttttest 4))
+
+[<Test>]
+let test_repeat1While_one_match() =
+ repeat1While (fun xs -> xs.Length < 4) (matchToken 't'B) !!!test ===> (Some(['t'B], !!+test 1))
+
+[<Test>]
+let test_repeat1While_no_matches () =
+ repeat1While (fun xs -> xs.Length < 4) (matchToken 'e'B) !!!ttttest ===> None
+
+[<Test>]
+let test_repeat1While_too_many_matches() =
+ repeat1While (fun xs -> xs.Length < 3) (matchToken 't'B) !!!ttttest ===> None
+
+[<Test>]
let test_failure_predicate_parser_success() =
!~ (matchToken 't'B) !!!test ===> None
View
16 src/Cashel.Tests/ListPrimitivesTest.fs
@@ -96,8 +96,24 @@ let test_repeat1_one_match() =
[<Test>]
let test_repeat1_no_matches () =
repeat1 (matchToken 'e') !!"ttttest" |> should equal None
+
+[<Test>]
+let test_repeat1While () =
+ repeat1While (fun xs -> xs.Length < 4) (matchToken 't') !!"ttttest" |> should equal (Some(!!"tttt", !!"est"))
[<Test>]
+let test_repeat1While_one_match() =
+ repeat1While (fun xs -> xs.Length < 4) (matchToken 't') !!"test" |> should equal (Some(['t'], !!"est"))
+
+[<Test>]
+let test_repeat1While_no_matches () =
+ repeat1While (fun xs -> xs.Length < 4) (matchToken 'e') !!"ttttest" |> should equal None
+
+[<Test>]
+let test_repeat1While_too_many_matches() =
+ repeat1While (fun xs -> xs.Length < 3) (matchToken 't') !!"ttttest" |> should equal None
+
+[<Test>]
let test_failure_predicate_parser_success() =
!~ (matchToken 't') !!"test" |> should equal None
View
3 src/Cashel/primitives.fs
@@ -28,6 +28,9 @@ module Primitives =
///repeat1 looks for one or more instances of the parsing function p
and repeat1 p = p >>= (fun x -> repeat p >>= (fun xs -> result (x::xs)))
+ ///repeat1While looks for one or more instances of the parsing function p while the result of f is true
+ and repeat1While f p = p >>= (fun x -> repeat p >>= (fun xs -> if f xs then result (x::xs) else zero))
+
///Success Predicate
let (!&) f =
fun input ->

0 comments on commit f111576

Please sign in to comment.
Something went wrong with that request. Please try again.