From 5fe400c68825b4eb4baaa6307d842ef958b60f8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren=20Tempel?= Date: Mon, 11 Apr 2022 18:03:24 +0200 Subject: [PATCH] (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 --- lib/chibi/parse/parse.scm | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/chibi/parse/parse.scm b/lib/chibi/parse/parse.scm index cc52150f..514c85ba 100644 --- a/lib/chibi/parse/parse.scm +++ b/lib/chibi/parse/parse.scm @@ -520,10 +520,15 @@ ;;> Parse with \var{f} once, keep the first result, and commit to the ;;> 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) - (lambda (source index sk fk) - (f source index (lambda (res s i fk) (sk res s i (lambda (s i r) #f))) fk))) +(define (parse-commit f . o) + (let ((commit-fk (if (pair? o) (car o) (lambda (s i r) #f)))) + (lambda (source index sk fk) + (f source index (lambda (res s i fk) (sk res s i commit-fk)) fk)))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;