@@ -362,7 +362,12 @@ val is_white : char -> bool
362
362
363
363
val suspend : (unit -> 'a t ) -> 'a t
364
364
(* * [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
+ *)
366
371
367
372
val string : string -> string t
368
373
(* * [string s] parses exactly the string [s], and nothing else. *)
@@ -423,12 +428,15 @@ val try_or_l :
423
428
'a t
424
429
(* * [try_or_l ?else_ l] tries each pair [(test, p)] in order.
425
430
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].
427
433
If they all fail, and [else_] is defined, then it behaves like [else_].
428
434
If all fail, and [else_] is [None], then it fails as well.
429
435
430
436
This is a performance optimization compared to {!(<|>)}. We commit to a
431
437
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].
432
440
433
441
See {!lookahead_ignore} for a convenient way of writing the test conditions.
434
442
@@ -481,7 +489,17 @@ val lookahead_ignore : 'a t -> unit t
481
489
@since 3.6 *)
482
490
483
491
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
+ *)
485
503
486
504
val line : slice t
487
505
(* * Parse a line, ['\n'] excluded, and position the cursor after the ['\n'].
@@ -634,9 +652,7 @@ module Infix : sig
634
652
635
653
[a <|> b] tries to parse [a], and if [a] fails without
636
654
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]. *)
640
656
641
657
val (< ?> ) : 'a t -> string -> 'a t
642
658
(* * [a <?> msg] behaves like [a], but if [a] fails,
0 commit comments