-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathprimitives.lisp
317 lines (245 loc) · 8.07 KB
/
primitives.lisp
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
;;;; Primitive parsers and combinators—axioms, so to speak.
(in-package :maxpc)
(defun ?end ()
"*Description:*
{?end} matches the end of input.
*Examples:*
#code#
(parse '() (?end)) → NIL, T, T
(parse '(a) (?end)) → NIL, NIL, NIL
#"
(lambda (input)
(when (input-empty-p input)
input)))
(defun =element ()
"*Description:*
{=element} matches the next element and produces that element it as its
result value.
*Examples:*
#code#
(parse '(a) (=element)) → A, T, T
(parse '() (=element)) → NIL, NIL, T
#"
(lambda (input)
(unless (input-empty-p input)
(values (input-rest input) (input-first input) t))))
(defmacro ?fail (&body forms)
"*Arguments and Values:*
_forms_—_forms_.
*Description:*
{?fail} always fails to match, and evaluates _forms_ when it is applied.
*Examples:*
#code#
(parse '(a b c) (?fail (format t \"Position: ~a~%\"
(get-input-position))))
▷ Position: 0
→ NIL, NIL, NIL
#"
`(lambda (*input-fail*) ,@forms nil))
(defun ?satisfies (test &optional (parser (=element)))
"*Arguments and Values:*
_test_—a _designator_ for a _function_ of one argument that returns a
_generalized boolean_.
_parser_—a _parser_. The default is {(=element)}.
*Description:*
{?satisfies} matches _parser_ if its result value _satisfies the test_.
*Examples:*
#code#
(parse '(1) (?satisfies 'numberp)) → NIL, T, T
(parse '(a) (?satisfies 'numberp)) → NIL, NIL, NIL
(parse '(a b c)
(?satisfies (lambda (s)
(intersection s '(b c d)))
(%any (=element))))
⇒ NIL, T, T
#"
(lambda (input)
(multiple-value-bind (rest value) (funcall parser input)
(when (and rest (funcall test value))
rest))))
(defun =subseq (parser)
"*Arguments and Values:*
_parser_—a _parser_.
*Description:*
{=subseq} matches _parser_, and produces the subsequence matched by _parser_
as its result value.
*Examples:*
#code#
(parse '(1 2 3) (=subseq (%any (?satisfies 'numberp))))
→ (1 2 3), T, T
(parse \"123\" (=subseq (%any (?satisfies 'digit-char-p))))
→ \"123\" T, T
#"
(lambda (input)
(let ((rest (funcall parser input)))
(when rest
(values rest
(input-sequence
input (- (input-position rest) (input-position input)))
t)))))
(defun ?seq (&rest parsers)
"*Arguments and Values:*
_parsers_—_parsers_.
*Description:*
{?seq} matches _parsers_ in sequence.
*Examples:*
#code#
(parse '(a) (?seq (=element) (?end)))
→ NIL, T, T
(parse '(a b) (?seq (=element) (?end)))
→ NIL, NIL, NIL
(parse '(a) (?seq))
→ NIL, T, NIL
#"
(lambda (input)
(loop for parser in parsers
do (setf input (funcall parser input))
unless input return nil
finally (return input))))
(defun =list (&rest parsers)
"*Arguments and Values:*
_parsers_—_parsers_.
*Description:*
{=list} matches _parsers_ in sequence, and produces a _list_ of the result
values of _parsers_ as its result value.
*Examples:*
#code#
(parse '(a) (=list (=element) (?end)))
→ (A NIL), T, T
(parse '(a b) (=list (=element) (?end)))
→ NIL, NIL, NIL
(parse '(a) (=list))
→ NIL, T, NIL
#"
(lambda (input)
(loop for parser in parsers
for value =
(multiple-value-bind (rest value) (funcall parser input)
(unless rest (return))
(setf input rest)
value)
collect value into list
finally (return (values input list t)))))
(defun %any (parser)
"*Arguments and Values:*
_parser_—a _parser_.
*Description:*
{%any} matches _parser_ in sequence any number of times. If _parser_
produces a result value and matches at least once then {%any} produces a
_list_ of the values as its result value.
*Examples:*
#code#
(parse '(a b c) (%any (=element))) → (A B C), T, T
(parse '() (%any (=element))) → NIL, T, T
#"
(lambda (input)
(let (rest value present-p)
(loop do (setf (values rest value present-p) (funcall parser input))
if rest do (setf input rest)
else return (values input list (not (null list)))
when present-p collect value into list))))
;; Set union
(defun %or (&rest parsers)
"*Arguments and Values:*
_parsers_—_parsers_.
*Description:*
{%or} attempts to successfully apply _parsers_, and matches the first
succeeding _parser_, if any. If that _parser_ produces a result value then
{%or} produces that value as its result value. It can be said that {%or}
forms the set union of _parsers_.
*Examples:*
#code#
(parse '(a) (%or (?eq 'a) (?eq 'b))) → NIL, T, T
(parse '(b) (%or (?eq 'a) (?eq 'b))) → NIL, T, T
(parse '(a) (%or)) → NIL, NIL, NIL
(parse '(a) (%or (=element)
(?fail (format t \"No element.~%\"))))
→ A, T, T
(parse '() (%or (?fail (princ 'foo))
(?fail (princ 'bar))
(?fail (princ 'baz))))
▷ FOOBARBAZ
→ NIL, NIL, T
#"
(lambda (input)
(loop for parser in parsers do
(multiple-value-bind (rest value present-p) (funcall parser input)
(when rest
(return (values rest value present-p)))))))
;; Set intersection
(defun %and (&rest parsers)
"*Arguments and Values:*
_parsers_—_parsers_.
*Description:*
{%and} applies _parsers_, and matches the last _parser_ only if all previous
_parsers_ succeed. If the last _parser_ produces a result value then {%and}
produces that value as its result value. It can be said that {%and} forms
the set intersection of _parsers_.
*Examples:*
#code#
(parse '(:foo) (%and (?satisfies 'symbolp)
(?satisfies 'keywordp)))
→ NIL, T, T
(parse '(foo) (%and (?satisfies 'symbolp)
(?satisfies 'keywordp)))
→ NIL, NIL, NIL
(parse '(foo) (%and))
→ NIL, NIL, NIL
(parse '(foo) (%and (?satisfies 'symbolp)
(=element)))
→ FOO, T, T
(parse '() (%and (%maybe (?fail (princ 'foo)))
(%maybe (?fail (princ 'bar)))
(%maybe (?fail (princ 'baz)))))
▷ FOOBARBAZ
→ NIL, T, T
#"
(lambda (input)
(let (rest value present-p)
(loop for parser in parsers do
(setf (values rest value present-p) (funcall parser input))
unless rest return nil
finally (return (values rest value present-p))))))
;; Set difference
(defun %diff (parser &rest not-parsers)
"*Arguments and Values:*
_parser_—a _parser_.
_not‑parsers_—_parsers_.
*Description:*
{%diff} matches _parser_ only if applying _not‑parsers_ fails. If _parser_
produces a result value then {%diff} produces that value as its result
value. It can be said that {%diff} forms the set difference of _parser_ and
the union of _not‑parsers_.
*Examples:*
#code#
(parse '(foo) (%diff (?satisfies 'symbolp)
(?satisfies 'keywordp)))
→ NIL, T, T
(parse '(:foo) (%diff (?satisfies 'symbolp)
(?satisfies 'keywordp)))
→ NIL, NIL, NIL
(parse '(foo) (%diff (?satisfies 'symbolp)))
→ NIL, T, T
(parse '(:foo) (%diff (?satisfies 'symbolp)))
→ NIL, T, T
#"
(let ((punion (apply '%or not-parsers)))
(lambda (input)
(unless (funcall punion input)
(funcall parser input)))))
(defun =transform (parser function)
"*Arguments and Values:*
_parser_—a _parser_.
_function_—a _designator_ for a _function_ of one argument.
*Description:*
{=transform} matches _parser_ and produces the result of applying _function_
to the result value of _parser_ as its result value.
*Examples:*
#code#
(parse '(41) (=transform (=element) '1+)) → 42, T, T
(parse '() (=transform (=element) '1+)) → NIL, NIL, T
#"
(lambda (input)
(multiple-value-bind (rest value) (funcall parser input)
(when rest
(values rest (funcall function value) t)))))