Skip to content

Commit

Permalink
choice-error: add an optional failure_msg argument to choice
Browse files Browse the repository at this point in the history
This can be used to quickly identify which choice resulted in the
failure of the parser. In the absence of `commit`, it's always the first
choice encountered, but it gets trickier when a parser does use choice.
In that case it's the first choice encountered after the last `commit`.
  • Loading branch information
seliopou committed Apr 15, 2018
1 parent 6bbc940 commit fc007f9
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 @@ -425,8 +425,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 fc007f9

Please sign in to comment.