summaryrefslogtreecommitdiff
path: root/parser.lisp
blob: 5495f74a3479eaa3ed7a77b5c6e1bf2ffcbb2acd (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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
(in-package #:monparser)

(defun parse-string (p input)
  (let ((result (funcall p (from-string input))))
    (if (parsing-p result)
      (parsing-tree result)
      result)))

(defun parse-file (p input)
  (let ((result (funcall p (from-file input))))
    (if (parsing-p result)
      (parsing-tree result)
      result)))

(defstruct parsing
  tree
  left)

(defstruct failure
  place
  message)

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

(defun new (tree)
  (lambda (input &optional lazy)
    (declare (ignore lazy))
    (make-parsing :tree tree :left input)))

(defun bind-with-input (p f)
  (lambda (input &optional lazy)
    (let ((r (funcall p input)))
      (if (parsing-p r)
        (if lazy
          (lambda (ignored-input &optional lazy)
            (declare (ignore ignored-input))
            (funcall (funcall f (parsing-tree r) input) (parsing-left r) lazy))
          (funcall (funcall f (parsing-tree r) input) (parsing-left r)))
        r))))

(defun bind (p f)
  (lambda (input &optional lazy)
    (let ((r (funcall p input)))
      (if (parsing-p r)
        (if lazy
          (lambda (ignored-input &optional lazy)
            (declare (ignore ignored-input))
            (funcall (funcall f (parsing-tree r)) (parsing-left r) lazy))
          (funcall (funcall f (parsing-tree r)) (parsing-left r)))
        r))))

(defun discarding-bind (p f)
  (lambda (input &optional lazy)
    (let ((r (funcall p input)))
      (if (parsing-p r)
        (if lazy
          (lambda (ignored-input &optional lazy)
            (declare (ignore ignored-input))
            (funcall (funcall f) (parsing-left r) lazy))
          (funcall (funcall f) (parsing-left r)))
        r))))

(defmacro comp (bindings &body body)
  (if (null bindings)
    `(new (progn ,@body))
    (let ((v (first (car bindings)))
          (p (second (car bindings))))
      (if (symbolp v)
        (if (string= (symbol-name v) "_")
          `(discarding-bind ,p (lambda () (comp ,(cdr bindings) ,@body)))
          `(bind ,p (lambda (,v) (comp ,(cdr bindings) ,@body))))
        (if (and (consp v) (symbolp (car v)) (symbolp (cdr v)))
          `(bind-with-input ,p (lambda (,(car v) ,(cdr v)) (comp ,(cdr bindings) ,@body)))
          (error "Binding must be either a symbol or a cons of symbols."))))))

(defun one-of (first-parser second-parser &rest other-parsers)
  (lambda (input &optional lazy)
    (declare (ignore lazy))
    (labels ((one-of-rec (parsers)
                         (let ((intermediate-parsers '())
                               (result nil))
                           (dolist (p parsers)
                             (let ((r (funcall p input (> (length parsers) 1))))
                               (cond ((functionp r)
                                      (push r intermediate-parsers))
                                     ((parsing-p r)
                                      (when (or (not (parsing-p result))
                                                (> (cursor (parsing-input r))
                                                   (cursor (parsing-input result))))
                                        (setf result r)))
                                     ((failure-p r)
                                      (when (or (failure-p result)
                                                 (= (length parsers) 1))
                                        (setf result r))))))
                           (if intermediate-parsers
                             (one-of-rec intermediate-parsers)
                             result))))
      (one-of-rec (cons first-parser (cons second-parser other-parsers))))))

(defmacro unit (&optional predicate)
  (cond ((null predicate)
         (setf predicate '(characterp it)))
        ((symbolp predicate)
         (setf predicate `(,predicate it)))
        ((characterp predicate)
         (setf predicate `(char-equal ,predicate it)))
        (t (setf predicate
                 (nsubst-if 'it
                            (lambda (x)
                              (and (symbolp x)
                                   (string-equal (symbol-name x) "IT"))) predicate))))
  `(lambda (input &optional lazy)
     (declare (ignore lazy))
     (if (has-data? input)
       (let ((it (peek input)))
         (if ,predicate
           (make-parsing :tree it :left (advance input))
           (make-failure :place input
                         :message (format nil "Expected: ~a, Got: ~a" ',predicate it))))
       (make-failure :place input :message "Reached end of input."))))

(defun literal (target)
  (lambda (input &optional lazy)
    (declare (ignore lazy))
    (if (has-data? input)
      (if (prefix? target input)
        (make-parsing :tree target :left (advance input (length target)))
        (make-failure :place input :message "Predicate not satisfied."))
      (make-failure :place input :message "Reached end of input."))))

(defparameter nothing
  (new nil))

(defun optional (p)
  (one-of p nothing))

(defun many (p)
  (comp ((x p)
         (xs (optional (many p))))
        (cons x xs)))

(defun repeat (p min &optional (max 0))
  (if (> min 0)
    (comp ((x p)
           (xs (repeat p (1- min) (1- max))))
          (cons x xs))
    (if (> max 0)
      (comp ((x (optional p))
             (xs (repeat p 0 (if x (1- max) 0))))
            (if x (cons x xs) x))
      nothing)))

(defun whitespace? (x)
  (some (lambda (y) (char= x y)) '(#\Space #\Newline #\Tab)))

(defparameter whitespace
  (comp ((_ (optional (many (unit whitespace?)))))
        nil))

(defun separated-list (p separator &key include-separator)
  (comp ((v p)
         (sep (optional separator))
         (vn (if sep
               (crit (separated-list p separator))
               nothing)))
        (if include-separator
          (cons v (cons sep vn))
          (cons v vn))))