-
Notifications
You must be signed in to change notification settings - Fork 23
/
safe-simple-read.lisp
181 lines (164 loc) · 7.35 KB
/
safe-simple-read.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
(in-package #:snooze-safe-simple-read)
(define-condition snooze-reader-error (simple-error reader-error) ())
(defmethod print-object ((c snooze-reader-error) s)
(print-unreadable-object (c s :type t)
(format s "~?"
(simple-condition-format-control c)
(simple-condition-format-arguments c))))
(defun parse-integer-then-float (string)
(let (retval nread)
(multiple-value-setq (retval nread)
(parse-integer string :junk-allowed t))
(cond ((/= nread (length string))
(multiple-value-setq (retval nread)
(ignore-errors
;; github#24: Sometimes SBCL will error here even with
;; :JUNK-ALLOWED. Swallow it.
(parse-float:parse-float string :junk-allowed t)))
(if (and retval
(/= nread (length string)))
(values nil nread)
(values retval nread)))
(t
(values retval nread)))))
(defun read-string (stream &optional (terminator #\") )
(unless (eql (read-char stream) terminator)
(error 'snooze-reader-error
:format-control "~a cannot read string that doesn't start with ~a"
:format-arguments (list 'parse-string terminator)))
(let ((nread 1))
(values
(handler-case
(with-output-to-string (*standard-output*)
(loop for c = (read-char stream) do
(incf nread)
(cond
((eql c terminator)
(return))
((eql c #\\)
(write-char (read-char stream)))
(t
(write-char c)))))
(end-of-file (e)
(error 'snooze-reader-error
:format-control "~a sees premature end reading string inside string (~a)"
:format-arguments (list 'parse-string e))))
nread)))
(defun read-name (stream)
(let* ((eof-sym (gensym))
(c (peek-char nil stream nil eof-sym)))
(cond ((eql c #\|)
(read-string stream #\|))
((eq c eof-sym)
(values nil 0))
(t
(let ((nread 0))
(values
(string-upcase
(with-output-to-string (*standard-output*)
(loop
with eof-sym = (gensym)
for c = (read-char stream nil eof-sym) do
(incf nread)
(cond ((eql c #\:)
(decf nread)
(unread-char c stream)
(return))
((eq c eof-sym) (decf nread) (return))
(t (write-char c))))))
nread))))))
(defun parse-symbol (string)
(let (symbol-name
(package *package*)
ncolons)
(with-input-from-string (stream string)
(multiple-value-bind (name nread)
(read-name stream)
(cond ((/= nread (length string))
(setq ncolons
(handler-case
(loop for c = (read-char stream)
while (eql c #\:)
count 1
finally (unread-char c stream))
(end-of-file (e)
(error 'snooze-reader-error
:format-control "~a sees a colon ending reading symbol from string ~a (~a)"
:format-arguments (list 'parse-symbol string e)))))
(cond ((= ncolons 0)
(error 'snooze-reader-error
:format-control "~a expecting colons reading symbol from string ~a"
:format-arguments (list 'parse-symbol string)))
((> ncolons 2)
(error 'snooze-reader-error
:format-control "~a sees too many colons reading symbol from string ~a"
:format-arguments (list 'parse-symbol string))))
(setq package (or (and (zerop (length name))
#.(find-package :keyword))
(find-package name)
(error 'snooze-reader-error
:format-control "~a does not recognize package ~a"
:format-arguments (list 'parse-symbol name))))
(multiple-value-bind (name more-nread)
(read-name stream)
(setq symbol-name name
nread (+ ncolons nread more-nread))
(when (/= nread (length string))
(error 'snooze-reader-error
:format-control "~a sees garbage reading symbol from string ~a"
:format-arguments (list 'parse-symbol string)))))
(t
(setq symbol-name name)))))
(multiple-value-bind (symbol status)
(find-symbol symbol-name package)
(when (and (eq status :internal)
ncolons
(/= ncolons 2))
(error 'snooze-reader-error
:format-control "~a sees a symbol ~a not external in the ~a package"
:format-arguments (list 'parse-symbol symbol-name package)))
(values symbol status symbol-name package))))
(defun safe-simple-read-from-string (string &optional make-symbol-p)
"Reads some objects represented by STRING.
Can only read in numbers, strings or existing symbols. Symbols may be
package-designated according to how they are written by of
WRITE-TO-STRING.
No new symbols are ever interned. A STRING value that would generate
interning of a symbol generates an error, but, if MAKE-SYMBOL-P is
non-NIL, a new uninterned symbol is returned with the name of the
would-be-interned symbol.
No macro-characters exist, not even #\(. So, where STRING would
normally represent a list to READ-FROM-STRING, it is taken as a
peculiar symbol name, that, at any rate, either exists in *PACKAGE* or
is never interned anywhere"
(cond
((zerop (length string))
(error 'snooze-reader-error
:format-control "Can't read from an empty string"))
((eql (aref string 0) #\")
(with-input-from-string (stream string)
(multiple-value-bind (obj nread)
(read-string stream #\")
(unless (= nread (length string))
(error 'snooze-reader-error
:format-control "~a sees junk reading string inside string ~s"
:format-arguments (list 'safe-read-simple-token-from-string string)))
(values obj nread))))
(t
(multiple-value-bind (number nread)
(parse-integer-then-float string)
(cond (number
(values number nread))
(t
(multiple-value-bind (symbol status symbol-name package)
(parse-symbol string)
(if status
(values symbol (length string))
(if make-symbol-p
(values (make-symbol symbol-name)
(length string))
(error 'snooze-reader-error
:format-control "~a refusing to intern a new symbol ~s in ~a"
:format-arguments `(safe-simple-read-from-string
,string
,package)))))))))))