diff options
Diffstat (limited to 'base.lisp')
| -rw-r--r-- | base.lisp | 32 |
1 files changed, 22 insertions, 10 deletions
@@ -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))))))) |
