summaryrefslogtreecommitdiff
path: root/base.lisp
diff options
context:
space:
mode:
authorJuan Manuel Tomás <jtomas1815@gmail.com>2025-11-24 07:01:47 -0300
committerJuan Manuel Tomás <jtomas1815@gmail.com>2025-11-24 07:01:47 -0300
commit2ebab36f8c689fa3e6f88cfc25cecd83848ca129 (patch)
tree63208a8be028b19974b8a5e686470bd4fb3fc657 /base.lisp
parent1c1162747d8d7e12140329a105c0776d5555a351 (diff)
downloadmonparser-2ebab36f8c689fa3e6f88cfc25cecd83848ca129.tar.gz
monparser-2ebab36f8c689fa3e6f88cfc25cecd83848ca129.zip
Big update
Diffstat (limited to 'base.lisp')
-rw-r--r--base.lisp32
1 files changed, 22 insertions, 10 deletions
diff --git a/base.lisp b/base.lisp
index 12ef7fb..e8aac7d 100644
--- a/base.lisp
+++ b/base.lisp
@@ -10,27 +10,39 @@
(message "")
(priority 0))
+(defun line-and-column (str index)
+ (let ((line 1) (column 1))
+ (dotimes (i index)
+ (let ((c (char str i)))
+ (case c
+ (#\Newline
+ (incf line)
+ (setf column 1))
+ (t (incf column)))))
+ (cons line column)))
+
(defmethod print-object ((obj failure) stream)
(if (failure-place obj)
- (let ((linecol (str:line-and-column (cursed:data (failure-place obj))
- (cursed:index (failure-place obj)))))
+ (let ((linecol (line-and-column (data (failure-place obj))
+ (index (failure-place obj)))))
(format stream "~a:~a: ~a~&~a~&"
(car linecol) (cdr linecol) (failure-message obj) (failure-place obj)))
(format stream "~a~&" (failure-message obj))))
-(defun new (tree)
- (lambda (input)
- (make-parsing :tree tree :start input :end input)))
-
(defun fail (message &key (priority 1))
- (lambda (input)
+ (lambda (start input)
+ (declare (ignore start))
(make-failure :place input :message message :priority priority)))
+(defun new (tree)
+ (lambda (start input)
+ (make-parsing :tree tree :start start :end input)))
+
(defun bind (parser f)
- (lambda (input)
- (let ((r (funcall parser input)))
+ (lambda (start input)
+ (let ((r (funcall parser input input)))
(cond ((parsing-p r)
- (funcall (funcall f r) (parsing-end r)))
+ (funcall (funcall f r) start (parsing-end r)))
((failure-p r) r)
(t (error (format nil "Invalid return value: ~a" r)))))))