-
Notifications
You must be signed in to change notification settings - Fork 13
/
smug.lisp
278 lines (230 loc) · 7.44 KB
/
smug.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
(defpackage :smug/smug
(:nicknames :smug)
(:use :cl)
(:export
#:.identity
#:.fail
#:.item
#:.bind
#:input-empty-p
#:input-first
#:input-rest
#:replace-invalid
#:run
#:parse
#:.plus
#:.or
#:.not
#:.let*
#:.map
#:.concatenate
#:.is
#:.is-not
#:.char=
#:.char-equal
#:.string-equal
#:.string=
#:.progn
#:.prog1
#:.prog2
#:.and
#:.or
#:.not
#:.first
#:.optional
#:.read-line))
(in-package :smug/smug)
(defmacro .let* (bindings &body body)
(if bindings
(let ((symbol (first (first bindings))))
`(.bind ,@(cdr (first bindings))
(lambda (,symbol)
,@(when (or (string-equal (symbol-name symbol) "_")
(null (symbol-package symbol)))
`((declare (ignorable ,symbol))))
(.let* ,(cdr bindings)
,@body))))
`(progn ,@body)))
(defun replace-subseq (needle haystack replacement)
(let* ((haystack (copy-seq haystack))
(idx (search needle haystack :test 'equal)))
(when idx
(replace haystack replacement :start1 idx))
haystack))
(defun replace-invalid (old new)
(let ((replace-invalid (find-restart 'replace-invalid)))
(when replace-invalid
(invoke-restart replace-invalid old new))))
(defun run (parser input)
(restart-case
(progn
(funcall parser input))
(replace-invalid (old new)
(let ((next-replace-invalid (find-restart 'replace-invalid)))
(if (and next-replace-invalid (not (search old input)))
(invoke-restart 'replace-invalid old new)
(funcall parser (replace-subseq old (copy-seq input) new)))))))
(defun parse (parser input)
(let ((result (run parser input)))
(when result
(destructuring-bind ((result . input) &rest rest)
result
(apply #'values result input rest)))))
(defun .fail ()
(lambda (input) (declare (ignore input)) nil))
(defun .plus (first-parser second-parser)
(lambda (input)
(append (funcall first-parser input) (funcall second-parser input))))
(defun .identity (value)
(lambda (input)
(list (cons value input))))
(defun .bind (parser function)
(lambda (input)
(loop :for (value . input) :in (run parser input)
:append (run (funcall function value) input))))
(defun .or (parser &rest parsers)
(lambda (input)
(or (funcall parser input)
(when parsers
(funcall (apply #'.or parsers) input)))))
(defun .not (parser)
(lambda (input)
(let ((result (funcall parser input)))
(if result
nil
(list (cons t input))))))
(defgeneric input-empty-p (input)
(:method ((input string)) (zerop (length input))))
(defgeneric input-first (input)
(:method ((input string)) (aref input 0)))
(defgeneric input-rest (input)
(:method ((input string))
(multiple-value-bind (string displacement)
(array-displacement input)
(make-array (1- (length input))
:displaced-to (or string input)
:displaced-index-offset (1+ displacement)
:element-type (array-element-type input)))))
(defun .item ()
(lambda (input)
(unless (input-empty-p input)
(list (cons (input-first input)
(input-rest input))))))
(defun .satisfies (predicate &rest args)
(.bind (.item)
(lambda (x)
(if (apply predicate x args)
(.identity x)
(.fail)))))
(defun .optional (parser)
(.or parser (.identity nil)))
(defun .and (p1 &rest ps)
(.let* ((result p1))
(if ps
(apply #'.and ps)
(.identity result))))
(defmacro .progn (&rest parsers)
(if (rest parsers)
(let ((name (gensym)))
`(.let* ((,name ,(first parsers)))
(.progn ,@(rest parsers))))
(first parsers)))
(defmacro .prog1 (parser &rest parsers)
(let ((name (gensym))
(ignore (gensym)))
`(.let* ((,name ,parser)
(,ignore (.progn ,@parsers)))
(.identity ,name))))
(defmacro .prog2 (parser1 parser2 &rest parsers)
(let ((name (gensym))
(ignore (gensym)))
`(.let* ((,ignore ,parser1)
(,name ,parser2)
(,ignore (.progn ,@parsers)))
(.identity ,name))))
(defun .is-not (predicate &rest args)
(.satisfies (lambda (i)
(cl:not (apply predicate i args)))))
(defun .is (predicate &rest args)
(apply #'.satisfies predicate args))
(defun .mapcar (parser)
(.plus (.let* ((x parser)
(xs (.mapcar parser)))
(.identity (cons x xs)))
(.identity ())))
(defun .mapc (parser)
(.plus (.let* ((_ parser)
(_ (.mapc parser)))
(.identity parser))
(.identity parser)))
(defun .make-list (size &key (initial-element (.item)))
(if (zerop size)
(.identity nil)
(.let* ((first initial-element)
(rest (.make-list (1- size)
:initial-element initial-element)))
(.identity (list* first rest)))))
(defun .concatenate (output-type-spec &rest parsers)
(if (not parsers)
(.fail)
(.let* ((first (first parsers))
(rest (if (rest parsers)
(apply
#'.concatenate output-type-spec (rest parsers))
(.identity nil))))
(.identity (cl:concatenate output-type-spec first rest)))))
(defun .map (result-type parser
&key
(at-least 1))
"=> a ~result-type~ of /parser/ results."
(.let* ((list-1 (.make-list at-least :initial-element parser))
(list-2 (funcall (if result-type #'.mapcar #'.mapc) parser)))
(.identity (when result-type (concatenate result-type list-1 list-2)))))
(defun .char= (x)
(.is #'cl:char= x))
(defun .digit-char-p ()
(.is #'cl:digit-char-p))
(defun .lower-case-p ()
(.is #'cl:lower-case-p))
(defun .upper-case-p ()
(.is #'cl:upper-case-p))
(defun .read-line (&optional
(eof-error-p t)
eof-value)
(.let* ((text (.optional
(.first (.map 'list (.is-not #'char= #\Newline)))))
(newline (.or (.char= #\Newline)
(.and (.not (.item))
(.identity '())))))
(if (or text newline)
(.identity (concatenate 'string text (when newline (string newline))))
(if eof-error-p
(.fail)
(.identity eof-value)))))
(defun .string= (string)
(if (string= string "")
(.identity nil)
(.let*
((_ (.is 'char= (aref string 0)))
(_ (.string= (subseq string 1))))
(.identity string))))
(defun .char-equal (char)
(.is #'cl:char-equal char))
(defun .string-equal (string)
(labels ((%string-equal (string)
(.let* ((first (.char-equal (aref string 0)))
(rest (if (> (length string) 1)
(%string-equal (subseq string 1))
(.identity nil))))
(.identity (cons first rest)))))
(.let* ((list (%string-equal string)))
(.identity (coerce list 'string)))))
(defun .first (parser)
(lambda (input)
(let ((results (run parser input)))
(when results (list (cl:first results))))))
(defun .one-of (parser list)
(if (null list)
(.identity nil)
(.or (funcall parser (first list))
(.one-of parser (rest list)))))