-
-
Notifications
You must be signed in to change notification settings - Fork 648
/
main.rkt
412 lines (397 loc) · 16.3 KB
/
main.rkt
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#lang racket/base
(require "config.rkt"
"special.rkt"
"wrap.rkt"
"coerce.rkt"
"readtable.rkt"
"whitespace.rkt"
"delimiter.rkt"
"closer.rkt"
"consume.rkt"
"location.rkt"
"accum-string.rkt"
"error.rkt"
"indentation.rkt"
"parameter.rkt"
"primitive-parameter.rkt"
"special-comment.rkt"
"sequence.rkt"
"vector.rkt"
"struct.rkt"
"graph.rkt"
"hash.rkt"
"symbol-or-number.rkt"
"string.rkt"
"char.rkt"
"quote.rkt"
"constant.rkt"
"box.rkt"
"regexp.rkt"
"extension.rkt"
"language.rkt"
"number.rkt")
(provide read
read-language
current-readtable
make-readtable
readtable?
readtable-mapping
string->number
(all-from-out "primitive-parameter.rkt")
(all-from-out "special-comment.rkt"))
;; This is not the `read` to be exposed from `racket/base`, but a
;; general entry to point implement `read` and variants like
;; `read-syntax` and `read/recursive`. To support syntax objects, the
;; caller should provide the `dynamic-require`, `read-compiled`,
;; `module-declared?`, and `corece` functions, even when implementing
;; a plain `read`, since those might be needed by a
;; `read-syntax/recursive`.
(define (read in
#:wrap [wrap #f]
#:init-c [init-c #f]
#:next-readtable [next-readtable (current-readtable)]
#:readtable [readtable next-readtable]
#:recursive? [recursive? #f]
#:local-graph? [local-graph? #f] ; ignored unless `recursive?`
#:source [source #f]
#:for-syntax? [for-syntax? #f]
#:read-compiled [read-compiled #f] ; see "config.rkt"
#:call-with-root-namespace [call-with-root-namespace #f] ; see "config.rkt"
#:dynamic-require [dynamic-require #f] ; see "config.rkt"
#:module-declared? [module-declared? #f] ; see "config.rkt"
#:coerce [coerce #f] ; see "config.rkt"
#:coerce-key [coerce-key #f] ; see "config.rkt"
#:keep-comment? [keep-comment? recursive?])
(define config
(cond
[(and recursive?
(current-read-config))
=> (lambda (config)
(read-config-update config
#:for-syntax? for-syntax?
#:wrap wrap
#:readtable readtable
#:next-readtable next-readtable
#:reset-graph? local-graph?
#:keep-comment? keep-comment?))]
[else
(make-read-config #:readtable readtable
#:next-readtable next-readtable
#:source source
#:for-syntax? for-syntax?
#:wrap wrap
#:read-compiled read-compiled
#:call-with-root-namespace call-with-root-namespace
#:dynamic-require dynamic-require
#:module-declared? module-declared?
#:coerce coerce
#:coerce-key coerce-key
#:keep-comment? keep-comment?)]))
(define v (read-one init-c in config))
(cond
[(and (or (not recursive?) local-graph?)
(read-config-state-graph (read-config-st config)))
(catch-and-reraise-as-reader
#f config
(make-reader-graph v))]
[(and recursive?
(not local-graph?)
(not for-syntax?)
(not (eof-object? v))
(not (special-comment? v)))
(get-graph-hash config) ; to trigger placeholder resolution
v]
[else v]))
(define (read-language in fail-k
#:for-syntax? [for-syntax? #f]
#:wrap [wrap #f]
#:read-compiled [read-compiled #f]
#:dynamic-require [dynamic-require #f]
#:call-with-root-namespace [call-with-root-namespace #f]
#:module-declared? [module-declared? #f]
#:coerce [coerce #f]
#:coerce-key [coerce-key #f])
(define config (make-read-config #:readtable #f
#:next-readtable #f
#:for-syntax? for-syntax?
#:wrap wrap
#:read-compiled read-compiled
#:call-with-root-namespace call-with-root-namespace
#:dynamic-require dynamic-require
#:module-declared? module-declared?
#:coerce coerce
#:coerce-key coerce-key))
(define l-config (override-parameter read-accept-reader config #f))
(read-language/get-info read-undotted in config fail-k))
;; ----------------------------------------
;; The top-level reading layer that takes care of parsing into
;; `#%cdot`.
(define (read-one init-c in config)
(cond
[(not (check-parameter read-cdot config))
;; No parsing of `.` as `#%dot`
(read-undotted init-c in config)]
[(check-parameter read-cdot config)
;; Look for `<something> . <something>`
(define-values (line col pos) (port-next-location in))
(define v (read-undotted init-c in config))
(cond
[(special-comment? v) v]
[else
(let loop ([v v])
(define c (peek-char/special in config))
(define ec (effective-char c config))
(cond
[(not (char? ec)) v]
[(char-whitespace? ec)
(consume-char in c)
(loop v)]
[(char=? ec #\.)
(define-values (dot-line dot-col dot-pos) (port-next-location in))
(consume-char in c)
(define pos-config (reading-at config dot-line dot-col dot-pos))
(define cdot (wrap '#%dot in pos-config #\.))
(define post-v (read-undotted #f in config))
(when (eof-object? post-v)
(reader-error in pos-config #:due-to eof "expected a datum after cdot, found end-of-file"))
(loop (wrap (list cdot v post-v) in (reading-at config line col pos) #\.))]
[else v]))])]))
;; ----------------------------------------
;; The top-level reading layer within `#%dot` handling --- which is
;; the reader's main dispatch layer.
(define (read-undotted init-c in config)
(define c (read-char/skip-whitespace-and-comments init-c read-one in config))
(define-values (line col pos) (port-next-location* in c))
(cond
[(eof-object? c) eof]
[(not (char? c))
(define v (special-value c))
(cond
[(special-comment? v)
(if (read-config-keep-comment? config)
v
(read-undotted #f in config))]
[else (coerce v in (reading-at config line col pos))])]
[(readtable-handler config c)
=> (lambda (handler)
(define v (readtable-apply handler c in config line col pos))
(retry-special-comment v in config))]
[else
;; Map character via readtable:
(define ec (effective-char c config))
;; Track indentation, unless it's a spurious closer:
(when (not (char-closer? ec config))
(track-indentation! config line col))
(define r-config (reading-at (discard-comment config) line col pos))
(define-syntax-rule (guard-legal e body ...)
(cond
[e body ...]
[else (reader-error in r-config "illegal use of `~a`" c)]))
;; Dispatch on character:
(case ec
[(#\#)
(read-dispatch c in r-config config)]
[(#\')
(read-quote read-one 'quote "quoting \"'\"" c in r-config)]
[(#\`)
(guard-legal
(check-parameter read-accept-quasiquote config)
(read-quote read-one 'quasiquote "quasiquoting \"`\"" c in r-config))]
[(#\,)
(guard-legal
(check-parameter read-accept-quasiquote config)
(define c2 (peek-char/special in config))
(if (eqv? c2 #\@)
(begin
(consume-char in c2)
(read-quote read-one 'unquote-splicing "unquoting `,@`" c in r-config))
(read-quote read-one 'unquote "unquoting `,`" c in r-config)))]
[(#\()
(wrap (read-unwrapped-sequence read-one ec #\( #\) in r-config #:shape-tag? #t) in r-config ec)]
[(#\))
(reader-error in r-config "~a" (indentation-unexpected-closer-message ec c r-config))]
[(#\[)
(guard-legal
(or (check-parameter read-square-bracket-as-paren config)
(check-parameter read-square-bracket-with-tag config))
(wrap (read-unwrapped-sequence read-one ec #\[ #\] in r-config #:shape-tag? #t) in r-config ec))]
[(#\])
(guard-legal
(or (check-parameter read-square-bracket-as-paren config)
(check-parameter read-square-bracket-with-tag config))
(reader-error in r-config "~a" (indentation-unexpected-closer-message ec c r-config)))]
[(#\{)
(guard-legal
(or (check-parameter read-curly-brace-as-paren config)
(check-parameter read-curly-brace-with-tag config))
(wrap (read-unwrapped-sequence read-one ec #\{ #\} in r-config #:shape-tag? #t) in r-config ec))]
[(#\})
(guard-legal
(or (check-parameter read-curly-brace-as-paren config)
(check-parameter read-curly-brace-with-tag config))
(reader-error in r-config "~a" (indentation-unexpected-closer-message ec c r-config)))]
[(#\")
(read-string in r-config)]
[(#\|)
(read-symbol-or-number c in r-config #:mode 'symbol)]
[else
(define v
(read-symbol-or-number c in r-config
;; Don't read as a number if the effective char
;; is non-numeric:
#:mode (if (or (eq? c ec)
(and ((char->integer ec) . < . 128)
(char-numeric? ec)))
'symbol-or-number
'symbol/indirect)))
(retry-special-comment v in config)])]))
;; Dispatch on `#` character
(define (read-dispatch dispatch-c in config orig-config)
(define c (read-char/special in config))
(cond
[(eof-object? c)
(reader-error in config #:due-to c "bad syntax `~a`" dispatch-c)]
[(not (char? c))
(reader-error in config #:due-to c "bad syntax `~a`" dispatch-c)]
[(readtable-dispatch-handler orig-config c)
=> (lambda (handler)
(define line (read-config-line config))
(define col (read-config-col config))
(define pos (read-config-pos config))
(define v (readtable-apply handler c in config line col pos))
(retry-special-comment v in orig-config))]
[else
(define-syntax-rule (guard-legal e c body ...)
(cond
[e body ...]
[else (bad-syntax-error in config (format "~a~a" dispatch-c c))]))
(case c
[(#\0 #\1 #\2 #\3 #\4 #\5 #\6 #\7 #\8 #\9)
;; Vector, graph definition, or graph reference
(read-vector-or-graph read-one dispatch-c c in config)]
[(#\()
(read-vector read-one #\( #\( #\) in config)]
[(#\[)
(guard-legal
(check-parameter read-square-bracket-as-paren config)
c
(read-vector read-one #\[ #\[ #\] in config))]
[(#\{)
(guard-legal
(check-parameter read-curly-brace-as-paren config)
c
(read-vector read-one #\{ #\{ #\} in config))]
[(#\s)
(read-struct read-one dispatch-c in config)]
[(#\&)
(read-box read-one dispatch-c in config)]
[(#\')
(read-quote read-one 'syntax "quoting #'" c in config)]
[(#\`)
(read-quote read-one 'quasisyntax "quasiquoting #`" c in config)]
[(#\,)
(define c2 (peek-char/special in config))
(if (eqv? c2 #\@)
(begin
(consume-char in c2)
(read-quote read-one 'unsyntax-splicing "unquoting #,@" c in config))
(read-quote read-one 'unsyntax "unquoting #," c in config))]
[(#\\)
(read-character in config)]
[(#\")
(read-string in config #:mode '|byte string|)]
[(#\<)
(define c2 (peek-char/special in config))
(cond
[(eqv? #\< c2)
(consume-char in #\<)
(read-here-string in config)]
[else
(reader-error in config #:due-to c2 "bad syntax `~a<`" dispatch-c)])]
[(#\%)
(read-symbol-or-number c in config #:extra-prefix dispatch-c #:mode 'symbol)]
[(#\:)
(read-symbol-or-number #f in config #:mode 'keyword)]
[(#\t #\T)
(define c2 (peek-char/special in config))
(cond
[(char-delimiter? c2 config) (wrap #t in config c)]
[else (read-delimited-constant c (char=? c #\t) '(#\r #\u #\e) #t in config)])]
[(#\f #\F)
(define c2 (peek-char/special in config))
(cond
[(char-delimiter? c2 config) (wrap #f in config c)]
[(or (char=? c2 #\x) (char=? c2 #\l))
(read-fixnum-or-flonum-vector read-one dispatch-c c c2 in config)]
[else (read-delimited-constant c (char=? c #\f) '(#\a #\l #\s #\e) #f in config)])]
[(#\e) (read-symbol-or-number #f in config #:mode "#e")]
[(#\E) (read-symbol-or-number #f in config #:mode "#E")]
[(#\i) (read-symbol-or-number #f in config #:mode "#i")]
[(#\I) (read-symbol-or-number #f in config #:mode "#I")]
[(#\d) (read-symbol-or-number #f in config #:mode "#d")]
[(#\B) (read-symbol-or-number #f in config #:mode "#B")]
[(#\o) (read-symbol-or-number #f in config #:mode "#o")]
[(#\O) (read-symbol-or-number #f in config #:mode "#O")]
[(#\D) (read-symbol-or-number #f in config #:mode "#D")]
[(#\b) (read-symbol-or-number #f in config #:mode "#b")]
[(#\x) (read-symbol-or-number #f in config #:mode "#x")]
[(#\X) (read-symbol-or-number #f in config #:mode "#X")]
[(#\c #\C)
(define c2 (read-char/special in config))
(case c2
[(#\s #\S) (read-one #f in (override-parameter read-case-sensitive config #t))]
[(#\i #\I) (read-one #f in (override-parameter read-case-sensitive config #f))]
[else
(reader-error in config #:due-to c2
"expected `s', `S`, `i`, or `I` after `~a~a`"
dispatch-c c)])]
[(#\h #\H) (read-hash read-one dispatch-c c in config)]
[(#\r)
;; Maybe regexp or `#reader`
(define accum-str (accum-string-init! config))
(accum-string-add! accum-str dispatch-c)
(accum-string-add! accum-str c)
(define c2 (read-char/special in config))
(when (char? c2) (accum-string-add! accum-str c2))
(case c2
[(#\x) (read-regexp c accum-str in config)]
[(#\e) (read-extension-reader read-one read-undotted dispatch-c in config)]
[else
(bad-syntax-error in config
#:due-to c2
(accum-string-get! accum-str config))])]
[(#\p)
;; Maybe pregexp
(define accum-str (accum-string-init! config))
(accum-string-add! accum-str dispatch-c)
(accum-string-add! accum-str c)
(define c2 (read-char/special in config))
(when (char? c2) (accum-string-add! accum-str c2))
(case c2
[(#\x) (read-regexp c accum-str in config)]
[else (bad-syntax-error in config #:due-to c2
(accum-string-get! accum-str config))])]
[(#\l)
;; Maybe `#lang`
(read-extension-lang read-undotted dispatch-c in config)]
[(#\!)
;; Maybe `#lang`
(read-extension-#! read-undotted dispatch-c in config)]
[(#\~)
;; Compiled code
(cond
[(check-parameter read-accept-compiled config)
(wrap ((read-config-read-compiled config) in) in config c)]
[else
(reader-error in config
"`~a~~` compiled expressions not enabled"
dispatch-c)])]
[else
(reader-error in config "bad syntax `~a~a`" dispatch-c c)])]))
(define (retry-special-comment v in config)
(cond
[(special-comment? v)
(if (read-config-keep-comment? config)
v
(read-undotted #f in config))]
[else v]))