-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathreadtable.lisp
334 lines (290 loc) · 10.5 KB
/
readtable.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
(in-package #:cloture)
(defunit eof)
(defconst %splicing-conditional '%splicing-conditional)
(defmacro #.%splicing-conditional (&rest forms)
`(|clojure.core|:|do| ,@forms))
(defun subread (stream)
(read stream t nil t))
(defun read-meta (stream char)
(declare (ignore char))
(let* ((meta (clojurize (subread stream)))
(value (clojurize (subread stream))))
(merge-meta! value (ensure-meta meta))
value))
(defun read-var (stream char arg)
(declare (ignore char arg))
(let ((sym (subread stream)))
`(|clojure.core|:|var| ,sym)))
(defun read-conditional (stream char arg)
(declare (ignore char arg))
(let* ((splicing?
(and (eql #\@ (peek-char nil stream))
(read-char stream)
t)))
(flet ((maybe-splice (form)
(if splicing?
(ematch form
((list* '[] subforms)
`(,%splicing-conditional ,@subforms)))
form)))
(let* ((forms (subread stream))
(missing "missing")
(cl (getf forms :|cl| missing)))
(if (eql cl missing)
(let ((default (getf forms :|default| missing)))
(if (eql default missing)
(values)
(maybe-splice default)))
(maybe-splice cl))))))
(defun read-nothing (stream char arg)
(declare (ignore char arg))
(let ((*read-suppress* t))
(subread stream)
(values)))
(defunion regex
(raw-regex (string string))
(compiled-regex (string string) (function function)))
(defmethod fset:compare ((r1 <regex>) (r2 <regex>))
(fset:compare (regex-string r1) (regex-string r2)))
(fset:define-cross-type-compare-methods <regex>)
(defun regex-string (r)
(match-of regex r
((raw-regex s) s)
((compiled-regex s _) s)))
(defun read-regex (stream char arg)
(declare (ignore arg))
(unread-char char stream)
(let ((string (assure string
;; Don't interpret backslash escapes.
(let ((cloture.interpol:*regex-delimiters* '(#\")))
(cloture.interpol:interpol-reader stream nil nil)))))
(raw-regex string)))
(defun read-quote (stream char)
(declare (ignore char))
`(|clojure.core|:|quote| ,(subread stream)))
(defvar *anon*)
(defconst max-anon-arg 20)
(defunion %arg
%&
(%n
(pos (integer 0 #.max-anon-arg))))
(defconst anon-arg-syms
(coerce (loop for i from 1 to max-anon-arg
collect (intern (string+ "%" i)))
'vector))
(defun %arg-sym (arg)
(ematch arg
((eql %&) '%&)
((%n n) (aref anon-arg-syms (1- n)))))
(defclass function-literal ()
((args :initform nil :accessor function-literal-args)))
(defmethod function-literal-lambda ((anon function-literal) forms)
(with-slots (args) anon
(multiple-value-bind (max-offset variadic?)
(mvfold (lambda (max variadic? arg)
(ematch arg
((eql %&) (values max t))
((%n n) (values (max max n) variadic?))))
args
0 nil)
(let ((pos-args (coerce (take max-offset anon-arg-syms) 'list))
(rest (and variadic? '(&rest %&))))
;; NB This must return a lambda so it can be used in function position.
`(lambda (,@pos-args ,@rest)
,forms)))))
(defun read-anon (stream char arg)
(declare (ignore char arg))
(if (boundp '*anon*)
(error (|clojure.core|:|IllegalStateException.|
"Anonymous function literals cannot be nested."))
(let* ((anon (make 'function-literal))
(*anon* anon)
(forms
(let ((*readtable* (find-readtable 'function-literal)))
(read-delimited-list #\) stream t))))
(function-literal-lambda anon forms))))
(defun read-%arg (stream char)
(declare (ignore char))
(assert (boundp '*anon*))
(let* ((next (read-char stream nil nil t))
(arg
(cond
((eql next #\&) %&)
((digit-char-p next)
(let* ((chars
(cons next
(loop while (digit-char-p (peek-char nil stream))
collect (read-char stream nil nil t))))
(chars (coerce 'string chars))
(n (parse-integer chars)))
(%n n)))
(t
(unread-char next stream)
(%n 1)))))
(push arg (function-literal-args *anon*))
(%arg-sym arg)))
(defun read-deref (stream char)
(declare (ignore char))
`(|clojure.core|:|deref| ,(subread stream)))
(defalias read-eval
(get-dispatch-macro-character #\# #\.))
(defun read-string-with-escapes (stream &optional (char #\"))
(unread-char char stream)
(let ((cloture.interpol:*regex-delimiters* nil))
(cloture.interpol:interpol-reader stream nil nil)))
(defun read-delimited-string (stream)
(let ((stream
(make-concatenated-stream
(make-string-input-stream "\"")
stream)))
(read stream)))
(def char-names
'(("newline" . #\Newline)
("space" . #\Space)
("tab" . #\Tab)
("formfeed" . #\Formfeed)
("backspace" . #\Backspace)
("return" . #\Return)))
(defun read-clojure-char (stream char)
(let ((next-char (peek-char nil stream nil nil t)))
(cond ((eql char next-char)
(read-char stream nil nil t)
#\\)
((alpha-char-p next-char)
(let* ((name
(loop for c = (peek-char nil stream nil nil t)
while (and c (alphanumericp c))
collect (read-char stream nil nil t)))
(name (coerce name 'string)))
(cond ((= (length name) 1)
(character name))
((string^= "u" name)
(code-char (parse-integer name :start 1 :radix 16)))
(t
(or (assocdr name char-names :test #'equal)
(error "Invalid char: ~s" name))))))
(t
(read-char stream nil nil t)))))
(defun subread-with-prefix (prefix stream)
(let ((stream (make-concatenated-stream
(make-string-input-stream prefix)
stream))
(*readtable* (find-readtable :standard)))
(subread stream)))
(defun read-clojure-number (stream char)
(declare (ignore char))
(let ((next (read-char stream nil nil t)))
(cond ((whitespacep next) 0)
((eql next #\x)
(subread-with-prefix "#x" stream))
((digit-char-p next 8)
(subread-with-prefix (fmt "#o~a" next) stream))
(t (unread-char next stream)
0))))
(defreadtable cloture
(:fuze :standard cloture.qq:quasiquote-mixin)
(:case :preserve)
;; Clojure quote.
(:macro-char #\' 'read-quote)
;; Clojure characters.
(:macro-char #\\ 'read-clojure-char)
;; Clojure numbers.
(:macro-char #\0 'read-clojure-number t)
;; Strings with escapes.
(:macro-char #\" 'read-string-with-escapes)
;; Supress.
(:dispatch-macro-char #\# #\_ 'read-nothing)
;; Metadata.
(:macro-char #\^ 'read-meta)
;; Reading regexes.
(:dispatch-macro-char #\# #\" 'read-regex)
;; Reading maps and sets.
(:syntax-from :standard #\) #\})
;; Reading seqs.
(:syntax-from :standard #\) #\])
;; ~ instead of ,.
(:syntax-from 'cloture.qq:quasiquote-mixin #\, #\~)
;; , is whitespace.
(:syntax-from :standard #\Space #\,)
;; Reader conditionals.
(:dispatch-macro-char #\# #\? 'read-conditional)
;; Vars.
(:dispatch-macro-char #\# #\' 'read-var)
;; Deref.
(:macro-char #\@ #'read-deref)
;; Anonymous function.
(:dispatch-macro-char #\# #\( 'read-anon)
;; Read eval.
(:dispatch-macro-char #\# #\= 'read-eval))
(defreadtable function-literal
(:merge cloture)
(:case :preserve)
(:macro-char #\% 'read-%arg t))
(defreadtable clojure-shortcut
(:merge :standard)
(:dispatch-macro-char #\# #\_ 'subread-clojure))
(defun call/clojure-reader (fn)
(let ((*readtable* (find-readtable 'cloture))
(*read-default-float-format* 'double-float))
(funcall fn)))
(defmacro with-clojure-reader ((&key) &body body)
(with-thunk (body)
`(call/clojure-reader ,body)))
(defun read-clojure (stream
&key (eof-error-p t)
eof-value
recursive)
(with-clojure-reader ()
(read stream eof-error-p eof-value recursive)))
(defun subread-clojure (stream char arg)
(declare (ignore char arg))
(let* ((*package* (find-package :cloture.impl))
(form (read-clojure stream :recursive t)))
(if (and (symbolp form)
(eql (symbol-package form)
(find-package :cloture.impl)))
(error "Read non-inherited symbol ~a in implementation package." form)
form)))
(defun read-clojure-from-string (string
&key (eof-error-p t)
eof-value
(start 0)
end
((:package *package*)
(find-package "user")))
(with-input-from-string (in string :start start :end end)
(read-clojure in :eof-error-p eof-error-p
:eof-value eof-value)))
(defun slurp-clojure-stream (stream
&key ((:package *package*)
(find-package "user")))
(loop for form
= (read-clojure stream
:eof-error-p nil
:eof-value eof)
until (eql form eof)
collect form))
(defun slurp-clojure-file (file &key ((:package *package*)
(find-package "user")))
(with-input-from-file (stream file)
(slurp-clojure-stream stream)))
(defun load-clojure (file &rest args
&key ((:package *package*)
(find-package "user"))
&allow-other-keys)
(let ((*package* (find-package "user"))))
(with-clojure-reader ()
(apply #'load file args)))
(defun compile-clojure (file &rest args
&key ((:package *package*)
(find-package "user"))
&allow-other-keys)
(let ((*package* (find-package "user")))
(with-clojure-reader ()
(apply #'compile-file file args))))
(defun compile-load-clojure (file &rest args
&key
((:package *package*)
(find-package "user"))
&allow-other-keys)
(load (compile-clojure file)))