From d08d5b232d74f3a75a833b231c4ef5e80870c993 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Manuel=20Tom=C3=A1s?= Date: Tue, 17 Mar 2026 17:48:03 -0300 Subject: Unify cursor start and end --- core.lisp | 42 ++++++++++++++++++------------------------ 1 file changed, 18 insertions(+), 24 deletions(-) (limited to 'core.lisp') diff --git a/core.lisp b/core.lisp index 9fef78f..b7b204a 100644 --- a/core.lisp +++ b/core.lisp @@ -28,14 +28,12 @@ (let ((start (gensym)) (input (gensym))) `(the parser - (lambda (,start ,input) - (declare (ignore ,start)) - (if (has-data? ,input) - (let ((it (peek ,input))) + (lambda (,input) + (if (cursor-has-data? ,input) + (let ((it (cursor-peek ,input))) (if ,predicate - (make-parsing :tree it - :start ,input - :end (advance ,input)) + (make-parsing :place (cursor-advance ,input) + :tree it) (make-failure :place ,input :message (format nil "Expected: ~a, Got: ~:c." ',predicate it)))) (make-failure :place ,input @@ -43,16 +41,15 @@ (declaim (ftype (function (parser parser &rest parser) parser) one-of)) (defun one-of (first-parser second-parser &rest other-parsers) - (lambda (start input) - (declare (ignore start)) + (lambda (input) (let ((parsers (cons first-parser (cons second-parser other-parsers))) (result (make-failure :place input))) (dolist (p parsers) - (let ((r (funcall p input input))) + (let ((r (funcall p (cursor-rebase input)))) (cond ((parsing-p r) (when (or (not (parsing-p result)) - (> (distance (parsing-end result) - (parsing-end r)) + (> (distance (result-place result) + (result-place r)) 0)) (setf result r))) ((failure-p r) @@ -62,7 +59,7 @@ (when (or (> priority-cmp 0) (and (= priority-cmp 0) (>= (distance (failure-place result) - (failure-place r)) + (failure-place r)) 0))) (setf result r))))) (t (error (format nil "Invalid return value: ~a." r)))))) @@ -74,22 +71,19 @@ (declaim (ftype (function (parser &key (:all t)) parser) many)) (defun many (p &key all) - (lambda (start input) - (declare (ignore start)) + (lambda (input) (let* ((result '())) - (do ((r (funcall p input input) - (funcall p (parsing-end r) (parsing-end r)))) + (do ((r (funcall p (cursor-rebase input)) + (funcall p (cursor-rebase (result-place r))))) ((or (failure-p r) - (= (index (parsing-start r)) - (index (parsing-end r)))) + (cursor-at-start? (result-place r))) nil) (push r result)) (cond ((not result) (make-failure :place input :message "No matches.")) - ((and all (has-data? (parsing-end (first result)))) - (make-failure :place (parsing-end (first result)) + ((and all (cursor-has-data? (result-place (first result)))) + (make-failure :place (result-place (first result)) :message "Input not exausted.")) - (t (make-parsing :tree (reverse (mapcar (lambda (x) (parsing-tree x)) result)) - :start input - :end (parsing-end (first result)))))))) + (t (make-parsing :place (result-place (first result)) + :tree (reverse (mapcar (lambda (x) (parsing-tree x)) result)))))))) -- cgit v1.2.3