Skip to content

Commit

Permalink
Merge pull request #143 from inhabitedtype/choice-error
Browse files Browse the repository at this point in the history
choice-error: add an optional `failure_msg` argument to `choice`
  • Loading branch information
seliopou authored Apr 21, 2018
2 parents 4954725 + fc007f9 commit 165e993
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
4 changes: 2 additions & 2 deletions lib/angstrom.ml
Original file line number Diff line number Diff line change
Expand Up @@ -438,8 +438,8 @@ let take_while1 f =
let take_till f =
take_while (fun c -> not (f c))

let choice ps =
List.fold_right (<|>) ps (fail "empty")
let choice ?(failure_msg="no more choices") ps =
List.fold_right (<|>) ps (fail failure_msg)

let fix f =
let rec p = lazy (f r)
Expand Down
8 changes: 5 additions & 3 deletions lib/angstrom.mli
Original file line number Diff line number Diff line change
Expand Up @@ -338,9 +338,11 @@ val (<|>) : 'a t -> 'a t -> 'a t
(** [p <|> q] runs [p] and returns the result if succeeds. If [p] fails, then
the input will be reset and [q] will run instead. *)

val choice : 'a t list -> 'a t
(** [choice ts] runs each parser in [ts] in order until one succeeds and
returns that result. *)
val choice : ?failure_msg:string -> 'a t list -> 'a t
(** [choice ?message ts] runs each parser in [ts] in order until one succeeds
and returns that result. In the case that none of the parser succeeds, then
the parser will fail with the message [failure_msg], if provided, or a
much less informative message otherwise. *)

val (<?>) : 'a t -> string -> 'a t
(** [p <?> name] associates [name] with the parser [p], which will be reported
Expand Down

0 comments on commit 165e993

Please sign in to comment.