From 8203d8f2f8bc7f31c98ac4d926de9b35c4bd1669 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Manuel=20Tom=C3=A1s?= Date: Sun, 25 Oct 2026 17:11:22 -0300 Subject: Add a until variant to many --- core.lisp | 10 ++++++---- test.lisp | 17 ++++++----------- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/core.lisp b/core.lisp index 502507b..316b5c4 100644 --- a/core.lisp +++ b/core.lisp @@ -69,13 +69,15 @@ (defun optional (p) (one-of p nothing)) -(declaim (ftype (function (parser &key (:all t)) parser) many)) -(defun many (p &key all) +(declaim (ftype (function (parser &key (:all t) (:until parser)) parser) many)) +(defun many (p &key all until) (lambda (input) (let ((result '())) - (do ((r (funcall p (cursor-rebase input)) (funcall p (result-place r)))) + (do ((end? (if until (funcall until (cursor-rebase input))) (if until (funcall until (result-place r)))) + (r (funcall p (cursor-rebase input)) (funcall p (result-place r)))) ((or (failure-p r) - (cursor-at-start? (result-place r))) + (cursor-at-start? (result-place r)) + (parsing-p end?)) (cond ((or (null result) (and all (cursor-has-data? (result-place (first result))))) (if (failure-p r) diff --git a/test.lisp b/test.lisp index ea50ca8..7fe0e0e 100644 --- a/test.lisp +++ b/test.lisp @@ -34,17 +34,12 @@ (optional (many (unit #'whitespace?)))) (defparser lazy-test () - (within whitespace - (one-of (unit #\h) - (comp ((_ (unit #\{)) - (body (optional (many (lazy lazy-test)))) - (_ (unit #\}))) - body) - (comp ((_ (unit #\()) - (body (optional (many (lazy lazy-test)))) - (_ (unit #\)))) - body) - (unit)))) + (one-of (unit #\h) + (comp ((_ (unit #\()) + (body (optional (many (lazy lazy-test) :until (unit #\))))) + (_ (unit #\)))) + body) + (unit))) (define-tests ((unit) "hello" #\h) -- cgit v1.2.3