Skip to content

Commit a127e13

Browse files
committed
doc for CCParse
close #392
1 parent 946ac4e commit a127e13

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

src/core/CCParse.mli

+22-6
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,12 @@ val is_white : char -> bool
362362

363363
val suspend : (unit -> 'a t) -> 'a t
364364
(** [suspend f] is the same as [f ()], but evaluates [f ()] only
365-
when needed. *)
365+
when needed.
366+
367+
A practical use case is to implement recursive parsers manually,
368+
as described in {!fix}. The parser is [let rec p () = …],
369+
and [suspend p] can be used in the definition to use [p].
370+
*)
366371

367372
val string : string -> string t
368373
(** [string s] parses exactly the string [s], and nothing else. *)
@@ -423,12 +428,15 @@ val try_or_l :
423428
'a t
424429
(** [try_or_l ?else_ l] tries each pair [(test, p)] in order.
425430
If the n-th [test] succeeds, then [try_or_l l] behaves like n-th [p],
426-
whether [p] fails or not.
431+
whether [p] fails or not. If [test] consumes input, the state is restored
432+
before calling [p].
427433
If they all fail, and [else_] is defined, then it behaves like [else_].
428434
If all fail, and [else_] is [None], then it fails as well.
429435
430436
This is a performance optimization compared to {!(<|>)}. We commit to a
431437
branch if the test succeeds, without backtracking at all.
438+
It can also provide better error messages, because failures in the parser
439+
will not be reported as failures in [try_or_l].
432440
433441
See {!lookahead_ignore} for a convenient way of writing the test conditions.
434442
@@ -481,7 +489,17 @@ val lookahead_ignore : 'a t -> unit t
481489
@since 3.6 *)
482490

483491
val fix : ('a t -> 'a t) -> 'a t
484-
(** Fixpoint combinator. *)
492+
(** Fixpoint combinator. [fix (fun self -> p)] is the parser [p],
493+
in which [self] refers to the parser [p] itself (which is useful to
494+
parse recursive structures.
495+
496+
An alternative, manual implementation to [let p = fix (fun self -> q)]
497+
is:
498+
{[ let rec p () =
499+
let self = suspend p in
500+
q
501+
]}
502+
*)
485503

486504
val line : slice t
487505
(** Parse a line, ['\n'] excluded, and position the cursor after the ['\n'].
@@ -634,9 +652,7 @@ module Infix : sig
634652
635653
[a <|> b] tries to parse [a], and if [a] fails without
636654
consuming any input, backtracks and tries
637-
to parse [b], otherwise it fails as [a].
638-
See {!try_} to ensure [a] does not consume anything (but it is best
639-
to avoid wrapping large parsers with {!try_}). *)
655+
to parse [b], otherwise it fails as [a]. *)
640656

641657
val (<?>) : 'a t -> string -> 'a t
642658
(** [a <?> msg] behaves like [a], but if [a] fails,

0 commit comments

Comments
 (0)