summaryrefslogtreecommitdiff
path: root/base.lisp
blob: 6887f0a64823d23fe3b6841d10bb34f45467c4b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
(in-package #:monparser)

(defstruct parsing
  tree
  left)

(defstruct failure
  place
  (message "")
  (priority 0))

(defmethod print-object ((obj failure) stream)
  (if (failure-place obj)
    (multiple-value-bind (line column) (line-and-column (failure-place obj))
      (format stream "~a:~a: ~a~&~a~&"
              line column (failure-message obj) (failure-place obj)))
    (format stream "~a~&" (failure-message obj))))

(defun new (tree)
  (lambda (input)
    (make-parsing :tree tree :left input)))

(defun bind (parser f)
  (lambda (input)
    (let ((r (funcall parser input)))
      (cond ((parsing-p r)
             (funcall (funcall f (parsing-tree r) input)
                      (parsing-left r)))
            ((failure-p r)
             r)
            (t (error (format nil "Invalid return value: ~a" r)))))))