-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscsh-read.scm
443 lines (380 loc) · 15.2 KB
/
scsh-read.scm
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
; -*- Mode: Scheme; Syntax: Scheme; Package: Scheme; -*-
; Copyright (c) 1993-1999 by Richard Kelsey and Jonathan Rees. See file COPYING.
; A little Scheme reader.
; Nonstandard things used:
; Ascii stuff: char->ascii, ascii->char, ascii-whitespaces, ascii-limit
; (for dispatch table; portable definitions in alt/ascii.scm)
; reverse-list->string -- ok to define as follows:
; (define (reverse-list->string l n)
; (list->string (reverse l)))
; make-immutable! -- ok to define as follows:
; (define (make-immutable! x) x)
; signal (only for use by reading-error; easily excised)
(define-structure scsh-reader (export scsh-read)
(open scheme-level-1
number-i/o
i/o-internal ;input-port-option
ascii ;for dispatch table
signals ;warn, signal-condition, make-condition
conditions ;define-condition-type
primitives ;make-immutable!
silly) ;reverse-list->string
(begin
(define preferred-case (lambda (x) x))
(define (script-skip c port)
(read-char port)
(let lp ((state 0))
(let ((advance-if (lambda (look-for)
(let ((c (read-char port)))
(if (eof-object? c)
(reading-error port
"EOF inside block comment -- #! missing a closing !#")
(lp (cond ((char=? c look-for) (+ state 1))
((char=? c #\newline) 1)
((char=? c cr) state)
(else 0))))))))
(case state
((0) (advance-if #\newline))
((1) (advance-if #\!)) ; Found \n
((2) (advance-if #\#)) ; Found \n!
((3) (advance-if #\newline)) ; Found \n!#
((4) (scsh-read port))
(else
(reading-error port "case other")))))) ; Found \n!#\n -- done.
; was sub-read ^
(define (multi-line-comment-skip c port)
(read-char port)
(let lp ((state 0) (nested? #f))
(let* ((advance-one-of-two
(lambda (look-for1 state1 look-for2 state2 nested?)
(let ((c (read-char port)))
(if (eof-object? c)
(error
"EOF inside block comment -- #| missing a closing |#")
(lp (cond ((char=? c look-for1) state1)
((char=? c look-for2) state2)
(else 0)) nested?)))))
(advance-if (lambda (look-for state nested?)
(advance-one-of-two look-for state
look-for state
nested?))))
(case state
((0) (advance-one-of-two #\| 1 #\# 5 nested?))
((1) (advance-if #\# 2 nested?))
((2) (if nested? #f (sub-read port)))
((5) (advance-if #\| 6 nested?))
((6) (lp 0 #t) (lp 0 nested?))))))
; scsh stop
(define (scsh-read . port-option)
(let ((port (input-port-option port-option)))
(let loop ()
(let ((form (sub-read port)))
(cond ((not (reader-token? form)) form)
((eq? form close-paren)
;; Too many right parens.
(warn "discarding extraneous right parenthesis")
(loop))
(else
(reading-error port (cdr form))))))))
(define (sub-read-carefully port)
(let ((form (sub-read port)))
(cond ((eof-object? form)
(reading-error port "unexpected end of file"))
((reader-token? form) (reading-error port (cdr form)))
(else form))))
(define reader-token-marker (list 'reader-token))
(define (make-reader-token message) (cons reader-token-marker message))
(define (reader-token? form)
(and (pair? form) (eq? (car form) reader-token-marker)))
(define close-paren (make-reader-token "unexpected right parenthesis"))
(define dot (make-reader-token "unexpected \" . \""))
; Main dispatch
(define (sub-read port)
(let ((c (read-char port)))
(if (eof-object? c)
c
((vector-ref read-dispatch-vector (char->ascii c))
c port))))
(define read-dispatch-vector
(make-vector ascii-limit
(lambda (c port)
(reading-error port "illegal character read" c))))
(define read-terminating?-vector
(make-vector ascii-limit #t))
(define (set-standard-syntax! char terminating? reader)
(vector-set! read-dispatch-vector (char->ascii char) reader)
(vector-set! read-terminating?-vector (char->ascii char) terminating?))
(let ((sub-read-whitespace
(lambda (c port)
c ;ignored
(sub-read port))))
(for-each (lambda (c)
(vector-set! read-dispatch-vector c sub-read-whitespace))
ascii-whitespaces))
(let ((sub-read-constituent
(lambda (c port)
(parse-token (sub-read-token c port) port))))
(for-each (lambda (c)
(set-standard-syntax! c #f sub-read-constituent))
(string->list
(string-append "!$%&*+-./0123456789:<=>?@^_~ABCDEFGHIJKLM"
"NOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"))))
; Usual read macros
(define (set-standard-read-macro! c terminating? proc)
(set-standard-syntax! c terminating? proc))
(define (sub-read-list c port)
(let ((form (sub-read port)))
(if (eq? form dot)
(reading-error port
"missing car -- ( immediately followed by .")
(let recur ((form form))
(cond ((eof-object? form)
(reading-error port
"end of file inside list -- unbalanced parentheses"))
((eq? form close-paren) '())
((eq? form dot)
(let* ((last-form (sub-read-carefully port))
(another-form (sub-read port)))
(if (eq? another-form close-paren)
last-form
(reading-error port
"randomness after form after dot"
another-form))))
(else
(cons form (recur (sub-read port)))))))))
(set-standard-read-macro! #\( #t sub-read-list)
(set-standard-read-macro! #\) #t
(lambda (c port)
c port
close-paren))
(set-standard-read-macro! #\' #t
(lambda (c port)
c
(list 'quote (sub-read-carefully port))))
(set-standard-read-macro! #\` #t
(lambda (c port)
c
(list 'quasiquote (sub-read-carefully port))))
(set-standard-read-macro! #\, #t
(lambda (c port)
c
(let* ((next (peek-char port))
;; DO NOT beta-reduce!
(keyword (cond ((eof-object? next)
(reading-error port "end of file after ,"))
((char=? next #\@)
(read-char port)
'unquote-splicing)
(else 'unquote))))
(list keyword
(sub-read-carefully port)))))
;(set-standard-read-macro! #\" #t
; (lambda (c port)
; c ;ignored
; (let loop ((l '()) (i 0))
; (let ((c (read-char port)))
; (cond ((eof-object? c)
; (reading-error port "end of file within a string"))
; ((char=? c #\\)
; (let ((c (read-char port)))
; (cond ((eof-object? c)
; (reading-error port "end of file within a string"))
; ((or (char=? c #\\) (char=? c #\"))
; (loop (cons c l) (+ i 1)))
; (else
; (reading-error port
; "invalid escaped character in string"
; c)))))
; ((char=? c #\")
; (reverse-list->string l i))
; (else
; (loop (cons c l) (+ i 1))))))))
(set-standard-read-macro! #\; #t
(lambda (c port)
c ;ignored
(gobble-line port)
(sub-read port)))
(define (gobble-line port)
(let loop ()
(let ((c (read-char port)))
(cond ((eof-object? c) c)
((char=? c #\newline) #f)
(else (loop))))))
(define *sharp-macros* '())
(define (define-sharp-macro c proc)
(set! *sharp-macros* (cons (cons c proc) *sharp-macros*)))
(set-standard-read-macro! #\# #f
(lambda (c port)
c ;ignored
(let* ((c (peek-char port))
(c (if (eof-object? c)
(reading-error port "end of file after #")
(char-downcase c)))
(probe (assq c *sharp-macros*)))
(if probe
((cdr probe) c port)
(reading-error port "unknown # syntax" c)))))
(define-sharp-macro #\f
(lambda (c port) (read-char port) #f))
(define-sharp-macro #\t
(lambda (c port) (read-char port) #t))
(define-sharp-macro #\\
(lambda (c port)
(read-char port)
(let ((c (peek-char port)))
(cond ((eof-object? c)
(reading-error port "end of file after #\\"))
((char-alphabetic? c)
(let ((name (sub-read-carefully port)))
(cond ((= (string-length (symbol->string name)) 1)
c)
((assq name '((space #\space)
(newline #\newline)))
=> cadr)
(else
(reading-error port "unknown #\\ name" name)))))
(else
(read-char port)
c)))))
(define-sharp-macro #\(
(lambda (c port)
(read-char port)
(list->vector (sub-read-list c port))))
(let ((number-sharp-macro
(lambda (c port)
(let ((string (sub-read-token #\# port)))
(or (string->number string)
(reading-error port "unsupported number syntax" string))))))
(for-each (lambda (c)
(define-sharp-macro c number-sharp-macro))
'(#\b #\o #\d #\x #\i #\e)))
(define-sharp-macro #\! script-skip)
(define-sharp-macro #\| multi-line-comment-skip)
; Tokens
(define (sub-read-token c port)
(let loop ((l (list (preferred-case c))) (n 1))
(let ((c (peek-char port)))
(cond ((or (eof-object? c)
(vector-ref read-terminating?-vector (char->ascii c)))
(reverse-list->string l n))
(else
(read-char port)
(loop (cons (preferred-case c) l)
(+ n 1)))))))
;(define (parse-token string port)
; (if (let ((c (string-ref string 0)))
; (or (char-numeric? c) (char=? c #\+) (char=? c #\-) (char=? c #\.)))
; (cond ((string->number string))
; ((member string strange-symbol-names)
; (string->symbol (make-immutable! string)))
; ((string=? string ".")
; dot)
; (else
; (reading-error port "unsupported number syntax" string)))
; (string->symbol (make-immutable! string))))
; scsh start
(define (parse-token string port)
(if (let ((c (string-ref string 0)))
(or (char-numeric? c) (char=? c #\+) (char=? c #\-) (char=? c #\.)))
(cond ((string->number string))
((string=? string ".") dot)
(else (string->symbol (make-immutable! string))))
(string->symbol (make-immutable! string))))
(set-standard-syntax! #\| #f
(lambda (c port)
(parse-token (sub-read-token c port) port)))
(define bel (ascii->char 7))
(define bs (ascii->char 8))
(define ff (ascii->char 12))
(define cr (ascii->char 13))
(define ht (ascii->char 9))
(define vt (ascii->char 11))
;;; Full ANSI C strings:
;;; - read as themselves: \\ \? \" \'
;;; - control chars:
;;; \a alert (bell -- ^g)
;;; \b backspace (^h)
;;; \f form feed (^l)
;;; \n newline (^j)
;;; \r carriage return (^m)
;;; \t tab (^i)
;;; \v vertical tab (^k)
;;; - octal escapes \nnn
;;; - hex escapes \xnn
;;; Is this the elegant thing to do? Too much might make it hard to shift
;;; to Unicode implementations. How about \^g for embedding control chars?
;;; And I haven't done anything about chars (as opposed to strings).
(set-standard-read-macro! #\" #t
(lambda (c port)
c ;ignored
(let* ((readc (lambda ()
(let ((c (read-char port)))
(if (eof-object? c)
(reading-error port "end of file within a string")
c))))
(read-digit (lambda (base base-name)
(let* ((c (readc))
(d (- (char->ascii c) (char->ascii #\0))))
(if (and (<= 0 d) (< d base)) d
(reading-error port
(string-append "invalid "
base-name
" code in string.")
d))))))
(let loop ((l '()) (i 0))
(let ((c (readc)))
(cond ((char=? c #\\)
(let* ((c (readc))
(rc (case c
((#\\ #\" #\? #\') c)
((#\a) bel)
((#\b) bs)
((#\f) ff)
((#\n) #\newline)
((#\r) cr)
((#\t) ht)
((#\v) vt)
((#\0 #\1 #\2 #\3)
(let* ((d1 (- (char->ascii c) (char->ascii #\0)))
(d2 (read-digit 8 "octal"))
(d3 (read-digit 8 "octal")))
(ascii->char (+ (* 64 d1) (+ (* 8 d2) d3)))))
((#\x)
(let ((d1 (read-digit 16 "hex"))
(d2 (read-digit 16 "hex")))
(ascii->char (+ (* 16 d1) d2))))
(else
(reading-error port
"invalid escapedcharacter in string"
c)))))
(loop (cons rc l) (+ i 1))))
((char=? c #\")
(reverse-list->string l i))
(else
(loop (cons c l) (+ i 1)))))))))
;scsh stop
(define strange-symbol-names
'("+" "-" "..."
"1+" "-1+" ;Only for S&ICP support
"->" ;Only for JAR's thesis
))
;--- This loses because the compiler won't in-line it. Hacked by hand
; because it is in READ's inner loop.
;(define preferred-case
; (if (char=? (string-ref (symbol->string 't) 0) #\T)
; char-upcase
; char-downcase))
(define p-c-v (make-string ascii-limit #\0))
(let ((p-c (if (char=? (string-ref (symbol->string 't) 0) #\T)
char-upcase
char-downcase)))
(do ((i 0 (+ i 1)))
((>= i ascii-limit))
(string-set! p-c-v i (p-c (ascii->char i)))))
;(define (preferred-case c)
; (string-ref p-c-v (char->ascii c)))
; Reader errors
(define (reading-error port message . irritants)
(apply signal 'read-error message
(append irritants (list port))))
))