take ~at_least ~up_to ~sep_by p
repeatedly parses p
and returns the parsed values. The lower and upper bound of repetition is specified by arguments at_least
and up_to
respectively. The default value of at_least
is 0
. The default value of up_to
is unspecified, i.e. there is no upper limit. If sep_by
is specified then the evaluation of p
must be followed by a successful evaluation of sep_by
. The parsed value of sep_by
is discarded. The repetition ends when one of the following occurs:
p
evaluates to failuresep_by
evaluates to failureup_to
upper boudn value is reached The parser fails if the count of repetition of p
does not match the value specified by at_least
.
Examples
Default behaviour.
module P = Reparse.String
;;
let p = P.(take (char 'a')) in
let v = P.parse_string p "aaaaa" in
v = ['a'; 'a'; 'a'; 'a'; 'a']
Specify ~sep_by
.
module P = Reparse.String
;;
let p = P.(take ~sep_by:(char ',') (char 'a')) in
let v = P.parse_string p "a,a,a,a,a" in
v = ['a'; 'a'; 'a'; 'a'; 'a']
Specify lower bound argument at_least
.
module P = Reparse.String
;;
let p = P.(take ~at_least:3 ~sep_by:(char ',') (char 'a')) in
let v = P.parse_string p "a,a,a,a,a" in
v = ['a'; 'a'; 'a'; 'a'; 'a']
Lower bound not met results in error.
module P = Reparse.String
;;
let p = P.(take ~at_least:5 ~sep_by:(char ',') (char 'a')) in
let v =
try
let _ = P.parse_string p "a,a,a,a" in
false
with _ -> true in
v = true
Specify upper bound up_to
.
module P = Reparse.String
;;
let p = P.(take ~up_to:3 ~sep_by:(char ',') (char 'a')) in
let v = P.parse_string p "a,a,a,a,a" in
v = ['a'; 'a'; 'a']