(chibi parse): allow (optionally) passing custom fk to parse-commit

Without this patch, parse-commit will unconditionally use a faillure
continuation which simply returns `#f`. This may be undesirable in
some situations. As such, this commit allows (optionally) passing
a custom failure continuation as a second argument. If none is passed
the old behavior is used, hence this commit doesn't cause any backwards
incompatible API changes.

See #822
This commit is contained in:
Sören Tempel 2022-04-11 18:03:24 +02:00
parent b4471ad6fd
commit 5fe400c688

View file

@ -520,10 +520,15 @@
;;> Parse with \var{f} once, keep the first result, and commit to the ;;> Parse with \var{f} once, keep the first result, and commit to the
;;> current parse path, discarding any prior backtracking options. ;;> current parse path, discarding any prior backtracking options.
;;> Since prior backtracking options are discarded, prior failure
;;> continuations are also not used. By default, \scheme{#f} is
;;> returned on failure, a custom failure continuation can be passed
;;> as the second argument.
(define (parse-commit f) (define (parse-commit f . o)
(lambda (source index sk fk) (let ((commit-fk (if (pair? o) (car o) (lambda (s i r) #f))))
(f source index (lambda (res s i fk) (sk res s i (lambda (s i r) #f))) fk))) (lambda (source index sk fk)
(f source index (lambda (res s i fk) (sk res s i commit-fk)) fk))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;