From c0e6ab7f1be115c2f3079d74eaa247cab89d1ef0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Manuel=20Tom=C3=A1s?= Date: Sat, 5 Oct 2024 12:08:08 -0300 Subject: Keep the one-of option that parsed the most --- parser.lisp | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'parser.lisp') diff --git a/parser.lisp b/parser.lisp index 2fe6f67..b231f14 100644 --- a/parser.lisp +++ b/parser.lisp @@ -85,14 +85,19 @@ (declare (ignore lazy)) (labels ((one-of-rec (parsers) (let ((intermediate-parsers '()) - (result (make-failure :place input - :message "Exausted options."))) + (result nil)) (dolist (p parsers) (let ((r (funcall p input (> (length parsers) 1)))) (cond ((functionp r) (push r intermediate-parsers)) ((parsing-p r) - (setf result r))))) + (when (or (not (parsing-p result)) + (> (cursor (parsing-input r)) + (cursor (parsing-input result)))) + (setf result r))) + ((failure-p r) + (when (failure-p result) + (setf result r)))))) (if intermediate-parsers (one-of-rec intermediate-parsers) result)))) -- cgit v1.2.3