summaryrefslogtreecommitdiff
path: root/core.lisp
diff options
context:
space:
mode:
authorJuan Manuel Tomás <jtomas1815@gmail.com>2026-03-17 17:48:03 -0300
committerJuan Manuel Tomás <jtomas1815@gmail.com>2026-03-17 17:48:03 -0300
commitd08d5b232d74f3a75a833b231c4ef5e80870c993 (patch)
treec123d61d35a992f5ea460a96eb89d1550c646b25 /core.lisp
parentd78ef10ad3ffe0eeaee0cd2a8f6b58e403085d48 (diff)
downloadmonparser-d08d5b232d74f3a75a833b231c4ef5e80870c993.tar.gz
monparser-d08d5b232d74f3a75a833b231c4ef5e80870c993.zip
Unify cursor start and endHEADmain
Diffstat (limited to 'core.lisp')
-rw-r--r--core.lisp42
1 files changed, 18 insertions, 24 deletions
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))))))))