Signal floating-point-overflow from bignum-to-float.
[sbcl.git] / src / code / reader.lisp
blobb71475cccca1e79846eb5335e2342250ef4fc248
1 ;;;; READ and friends
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
12 (in-package "SB!IMPL")
14 ;;;; miscellaneous global variables
16 ;;; ANSI: "the floating-point format that is to be used when reading a
17 ;;; floating-point number that has no exponent marker or that has e or
18 ;;; E for an exponent marker"
19 (!defvar *read-default-float-format* 'single-float)
20 (declaim (type (member short-float single-float double-float long-float)
21 *read-default-float-format*))
23 (defvar *readtable*)
24 (declaim (type readtable *readtable*))
26 (setf (fdocumentation '*readtable* 'variable)
27 "Variable bound to current readtable.")
29 ;;; A standard Lisp readtable (once cold-init is through). This is for
30 ;;; recovery from broken read-tables (and for
31 ;;; WITH-STANDARD-IO-SYNTAX), and should not normally be user-visible.
32 ;;; If the initial value is changed from NIL to something more interesting,
33 ;;; be sure to update the duplicated definition in "src/code/print.lisp"
34 (defglobal *standard-readtable* nil)
36 ;;; In case we get an error trying to parse a symbol, we want to rebind the
37 ;;; above stuff so it's cool.
40 ;;;; reader errors
42 (defun reader-eof-error (stream context)
43 (declare (optimize allow-non-returning-tail-call))
44 (error 'reader-eof-error
45 :stream stream
46 :context context))
48 ;;; If The Gods didn't intend for us to use multiple namespaces, why
49 ;;; did They specify them?
50 (defun simple-reader-error (stream control &rest args)
51 (declare (optimize allow-non-returning-tail-call))
52 (error 'simple-reader-error
53 :stream stream
54 :format-control control
55 :format-arguments args))
57 ;;;; macros and functions for character tables
59 (declaim (ftype (sfunction (character readtable) (unsigned-byte 8))
60 get-cat-entry))
61 (defun get-cat-entry (char rt)
62 (if (typep char 'base-char)
63 (elt (character-attribute-array rt) (char-code char))
64 (values (gethash char (character-attribute-hash-table rt)
65 +char-attr-constituent+))))
67 (defun set-cat-entry (char newvalue &optional (rt *readtable*))
68 (declare (character char) (type (unsigned-byte 8) newvalue) (readtable rt))
69 (if (typep char 'base-char)
70 (setf (elt (character-attribute-array rt) (char-code char)) newvalue)
71 (if (= newvalue +char-attr-constituent+)
72 ;; Default value for the C-A-HASH-TABLE is +CHAR-ATTR-CONSTITUENT+.
73 (%remhash char (character-attribute-hash-table rt))
74 (setf (gethash char (character-attribute-hash-table rt)) newvalue)))
75 (values))
77 ;; Set the character-macro-table entry without coercing NEW-VALUE.
78 ;; As used by set-syntax-from-char it must always process "raw" values.
79 (defun set-cmt-entry (char new-value &optional (rt *readtable*))
80 (declare (character char)
81 (type (or null function fdefn) new-value)
82 (type readtable rt))
83 (if (typep char 'base-char)
84 (setf (svref (character-macro-array rt) (char-code char)) new-value)
85 (if new-value ; never store NILs
86 (setf (gethash char (character-macro-hash-table rt)) new-value)
87 (remhash char (character-macro-hash-table rt)))))
89 ;;; the value actually stored in the character macro table. As per
90 ;;; ANSI #'GET-MACRO-CHARACTER and #'SET-MACRO-CHARACTER, this can
91 ;;; be either a function-designator or NIL, except that we store
92 ;;; symbols not as themselves but as their #<fdefn>.
93 (defun get-raw-cmt-entry (char readtable)
94 (declare (character char) (readtable readtable))
95 (if (typep char 'base-char)
96 (svref (character-macro-array readtable) (char-code char))
97 (values (gethash char (character-macro-hash-table readtable) nil))))
99 ;; As above but get the entry for SUB-CHAR in a dispatching macro table.
100 (defun get-raw-cmt-dispatch-entry (sub-char sub-table)
101 (declare (character sub-char))
102 (if (typep sub-char 'base-char)
103 (svref (truly-the (simple-vector #.base-char-code-limit)
104 (cdr (truly-the cons sub-table)))
105 (char-code sub-char))
106 (awhen (car sub-table)
107 (gethash sub-char it))))
109 ;; Coerce THING to a character-macro-table entry
110 (defmacro !coerce-to-cmt-entry (thing)
111 `(let ((x ,thing))
112 (if (typep x '(or null function)) x (find-or-create-fdefn x))))
114 ;; Return a callable function given a character-macro-table entry.
115 (defmacro !cmt-entry-to-function (val fallback)
116 `(let ((x ,val))
117 (truly-the
118 function
119 (cond ((functionp x) x)
120 ((null x) ,fallback)
121 (t (sb!c:safe-fdefn-fun x))))))
123 ;; Return a function-designator given a character-macro-table entry.
124 (defmacro !cmt-entry-to-fun-designator (val)
125 `(let ((x ,val))
126 (if (fdefn-p x) (fdefn-name x) x)))
128 ;;; The character attribute table is a BASE-CHAR-CODE-LIMIT vector
129 ;;; of (unsigned-byte 8) plus a hashtable to handle higher character codes.
131 (defmacro test-attribute (char whichclass rt)
132 `(= (get-cat-entry ,char ,rt) ,whichclass))
134 ;;; predicates for testing character attributes
136 #!-sb-fluid
137 (progn
138 (declaim (inline whitespace[1]p whitespace[2]p))
139 (declaim (inline constituentp terminating-macrop))
140 (declaim (inline single-escape-p multiple-escape-p))
141 (declaim (inline token-delimiterp)))
143 ;;; the [1] and [2] here refer to ANSI glossary entries for
144 ;;; "whitespace".
145 ;; whitespace[2]p is the only predicate whose readtable is optional
146 ;; - other than whitespace[1]p which has a fixed readtable - due to
147 ;; callers not otherwise needing a readtable at all, and so not binding
148 ;; *READTABLE* into a local variable throughout their lifetime.
149 (defun whitespace[1]p (char)
150 (test-attribute char +char-attr-whitespace+ *standard-readtable*))
151 (defun whitespace[2]p (char &optional (rt *readtable*))
152 (test-attribute char +char-attr-whitespace+ rt))
154 (defun constituentp (char rt)
155 (test-attribute char +char-attr-constituent+ rt))
157 (defun terminating-macrop (char rt)
158 (test-attribute char +char-attr-terminating-macro+ rt))
160 (defun single-escape-p (char rt)
161 (test-attribute char +char-attr-single-escape+ rt))
163 (defun multiple-escape-p (char rt)
164 (test-attribute char +char-attr-multiple-escape+ rt))
166 (defun token-delimiterp (char &optional (rt *readtable*))
167 ;; depends on actual attribute numbering in readtable.lisp.
168 (<= (get-cat-entry char rt) +char-attr-terminating-macro+))
170 ;;;; constituent traits (see ANSI 2.1.4.2)
172 ;;; There are a number of "secondary" attributes which are constant
173 ;;; properties of characters (as long as they are constituents).
175 (defconstant +constituent-trait-table+
176 #.(let ((a (!make-specialized-array base-char-code-limit '(unsigned-byte 8))))
177 (fill a +char-attr-constituent+)
178 (flet ((!set-constituent-trait (char trait)
179 (aver (typep char 'base-char))
180 (setf (elt a (char-code char)) trait)))
181 (!set-constituent-trait #\: +char-attr-package-delimiter+)
182 (!set-constituent-trait #\. +char-attr-constituent-dot+)
183 (!set-constituent-trait #\+ +char-attr-constituent-sign+)
184 (!set-constituent-trait #\- +char-attr-constituent-sign+)
185 (!set-constituent-trait #\/ +char-attr-constituent-slash+)
186 (do ((i (char-code #\0) (1+ i)))
187 ((> i (char-code #\9)))
188 (!set-constituent-trait (code-char i) +char-attr-constituent-digit+))
189 (!set-constituent-trait #\E +char-attr-constituent-expt+)
190 (!set-constituent-trait #\F +char-attr-constituent-expt+)
191 (!set-constituent-trait #\D +char-attr-constituent-expt+)
192 (!set-constituent-trait #\S +char-attr-constituent-expt+)
193 (!set-constituent-trait #\L +char-attr-constituent-expt+)
194 (!set-constituent-trait #\e +char-attr-constituent-expt+)
195 (!set-constituent-trait #\f +char-attr-constituent-expt+)
196 (!set-constituent-trait #\d +char-attr-constituent-expt+)
197 (!set-constituent-trait #\s +char-attr-constituent-expt+)
198 (!set-constituent-trait #\l +char-attr-constituent-expt+)
199 (!set-constituent-trait #\Space +char-attr-invalid+)
200 (!set-constituent-trait #\Newline +char-attr-invalid+)
201 (dolist (c (list backspace-char-code tab-char-code form-feed-char-code
202 return-char-code rubout-char-code))
203 (!set-constituent-trait (code-char c) +char-attr-invalid+)))
206 (declaim (inline get-constituent-trait))
207 (defun get-constituent-trait (char)
208 (if (typep char 'base-char)
209 (elt +constituent-trait-table+ (char-code char))
210 +char-attr-constituent+))
212 ;;;; Readtable Operations
214 (defun assert-not-standard-readtable (readtable operation)
215 (when (eq readtable *standard-readtable*)
216 (cerror "Frob it anyway!" 'standard-readtable-modified-error
217 :operation operation)))
219 (defun readtable-case (readtable)
220 (aref #(:upcase :downcase :preserve :invert) (%readtable-case readtable)))
222 (defun (setf readtable-case) (case readtable)
223 ;; This function does not accept a readtable designator, only a readtable.
224 (assert-not-standard-readtable readtable '(setf readtable-case))
225 (setf (%readtable-case readtable)
226 (ecase case (:upcase 0) (:downcase 1) (:preserve 2) (:invert 3)))
227 case)
229 (defun readtable-normalization (readtable)
230 "Returns T if READTABLE normalizes strings to NFKC, and NIL otherwise.
231 The READTABLE-NORMALIZATION of the standard readtable is T."
232 (%readtable-normalization readtable))
234 (defun (setf readtable-normalization) (new-value readtable)
235 "Sets the READTABLE-NORMALIZATION of the given READTABLE to NEW-VALUE.
236 Pass T to make READTABLE normalize symbols to NFKC (the default behavior),
237 and NIL to suppress normalization."
238 ;; This function does not accept a readtable designator, only a readtable.
239 (assert-not-standard-readtable readtable '(setf readtable-normalization))
240 (setf (%readtable-normalization readtable) new-value))
242 (defun readtable-base-char-preference (readtable)
243 "Returns :SYMBOLS, :STRINGS, :BOTH, or NIL, depending on whether the
244 reader should try to intern a base-string when reading a symbol name,
245 respectively produce a base-string when reading a quoted string, or in both
246 cases, or neither. The preference applies when a symbol-name or string
247 contains only BASE-CHAR characters. An (ARRAY CHARACTER (*)) can always
248 be interned (returned, respectively) as required. The default is :SYMBOLS."
249 ;; For efficiency the single preference occupies two slots internally.
250 (let ((symbols (eq (%readtable-symbol-preference readtable) 'base-char))
251 (strings (eq (%readtable-string-preference readtable) 'base-char)))
252 (cond ((and strings symbols) :both)
253 (symbols :symbols)
254 (strings :strings))))
256 (defun (setf readtable-base-char-preference) (new-value readtable)
257 (declare (type (member :symbols :strings :both nil) new-value))
258 "Sets the READTABLE-BASE-CHAR-PREFERENCE of the given READTABLE."
259 (setf (%readtable-symbol-preference readtable)
260 (if (member new-value '(:symbols :both)) 'base-char 'character)
261 (%readtable-string-preference readtable)
262 (if (member new-value '(:strings :both)) 'base-char 'character))
263 new-value)
265 (defun replace/eql-hash-table (to from &optional (transform #'identity))
266 (maphash (lambda (k v) (setf (gethash k to) (funcall transform v))) from)
269 (defun %make-dispatch-macro-char (dtable)
270 (lambda (stream char)
271 (declare (ignore char))
272 (read-dispatch-char stream dtable)))
274 (defun %dispatch-macro-char-table (fun)
275 (and (closurep fun)
276 (eq (%closure-fun fun)
277 (load-time-value (%closure-fun (%make-dispatch-macro-char nil))
279 (find-if-in-closure #'consp fun)))
281 ;; If ENTRY is a dispatching macro, copy its dispatch table.
282 ;; Otherwise return it without alteration.
283 (defun copy-cmt-entry (entry)
284 (let ((dtable (%dispatch-macro-char-table entry)))
285 (if dtable
286 (%make-dispatch-macro-char
287 (cons (awhen (car dtable)
288 (replace/eql-hash-table (make-hash-table) it))
289 (copy-seq (cdr dtable))))
290 entry)))
292 (defun copy-readtable (&optional (from-readtable *readtable*) to-readtable)
293 "Copies FROM-READTABLE and returns the result. Uses TO-READTABLE as a target
294 for the copy when provided, otherwise a new readtable is created. The
295 FROM-READTABLE defaults to the standard readtable when NIL and to the current
296 readtable when not provided."
297 (assert-not-standard-readtable to-readtable 'copy-readtable)
298 (let ((really-from-readtable (or from-readtable *standard-readtable*))
299 (really-to-readtable (or to-readtable (make-readtable))))
300 (replace (character-attribute-array really-to-readtable)
301 (character-attribute-array really-from-readtable))
302 (replace/eql-hash-table
303 (clrhash (character-attribute-hash-table really-to-readtable))
304 (character-attribute-hash-table really-from-readtable))
305 (map-into (character-macro-array really-to-readtable)
306 #'copy-cmt-entry
307 (character-macro-array really-from-readtable))
308 (replace/eql-hash-table
309 (clrhash (character-macro-hash-table really-to-readtable))
310 (character-macro-hash-table really-from-readtable)
311 #'copy-cmt-entry)
312 (setf (readtable-case really-to-readtable)
313 (readtable-case really-from-readtable))
314 (setf (%readtable-string-preference really-to-readtable)
315 (%readtable-string-preference really-from-readtable)
316 (%readtable-symbol-preference really-to-readtable)
317 (%readtable-symbol-preference really-from-readtable))
318 (setf (readtable-normalization really-to-readtable)
319 (readtable-normalization really-from-readtable))
320 really-to-readtable))
322 (defun set-syntax-from-char (to-char from-char &optional
323 (to-readtable *readtable*) (from-readtable nil))
324 "Causes the syntax of TO-CHAR to be the same as FROM-CHAR in the optional
325 readtable (defaults to the current readtable). The FROM-TABLE defaults to the
326 standard Lisp readtable when NIL."
327 ;; TO-READTABLE is a readtable, not a readtable-designator
328 (assert-not-standard-readtable to-readtable 'set-syntax-from-char)
329 (let* ((really-from-readtable (or from-readtable *standard-readtable*))
330 (att (get-cat-entry from-char really-from-readtable))
331 (mac (get-raw-cmt-entry from-char really-from-readtable)))
332 (set-cat-entry to-char att to-readtable)
333 (set-cmt-entry to-char (copy-cmt-entry mac) to-readtable))
336 (defun set-macro-character (char function &optional
337 (non-terminatingp nil)
338 (rt-designator *readtable*))
339 "Causes CHAR to be a macro character which invokes FUNCTION when seen
340 by the reader. The NON-TERMINATINGP flag can be used to make the macro
341 character non-terminating, i.e. embeddable in a symbol name."
342 (let ((designated-readtable (or rt-designator *standard-readtable*)))
343 (assert-not-standard-readtable designated-readtable 'set-macro-character)
344 (set-cat-entry char (if non-terminatingp
345 +char-attr-constituent+
346 +char-attr-terminating-macro+)
347 designated-readtable)
348 (set-cmt-entry char (!coerce-to-cmt-entry function) designated-readtable)
349 t)) ; (ANSI-specified return value)
351 (defun get-macro-character (char &optional (rt-designator *readtable*))
352 "Return the function associated with the specified CHAR which is a macro
353 character, or NIL if there is no such function. As a second value, return
354 T if CHAR is a macro character which is non-terminating, i.e. which can
355 be embedded in a symbol name."
356 (let* ((designated-readtable (or rt-designator *standard-readtable*))
357 ;; the first return value: (OR FUNCTION SYMBOL) if CHAR is a macro
358 ;; character, or NIL otherwise
359 (fun-value (!cmt-entry-to-fun-designator
360 (get-raw-cmt-entry char designated-readtable))))
361 (values fun-value
362 ;; NON-TERMINATING-P return value:
363 (if fun-value
364 (or (constituentp char designated-readtable)
365 (not (terminating-macrop char designated-readtable)))
366 ;; ANSI's definition of GET-MACRO-CHARACTER says this
367 ;; value is NIL when CHAR is not a macro character.
368 ;; I.e. this value means not just "non-terminating
369 ;; character?" but "non-terminating macro character?".
370 nil))))
372 (defun get-dispatch-macro-char-table (disp-char readtable &optional (errorp t))
373 (cond ((%dispatch-macro-char-table (get-raw-cmt-entry disp-char readtable)))
374 (errorp (error "~S is not a dispatching macro character." disp-char))))
376 (defun make-dispatch-macro-character (char &optional
377 (non-terminating-p nil)
378 (rt *readtable*))
379 "Cause CHAR to become a dispatching macro character in readtable (which
380 defaults to the current readtable). If NON-TERMINATING-P, the char will
381 be non-terminating."
382 ;; This used to call ERROR if the character was already a dispatching
383 ;; macro but I saw no evidence of that in other implementations except cmucl.
384 ;; Without a portable way to inquire whether a character is dispatching,
385 ;; a file that frobs *READTABLE* can't be repeatedly loaded except
386 ;; by catching the error, so I removed it.
387 ;; RT is a readtable, not a readtable-designator, as per CLHS.
388 (unless (get-dispatch-macro-char-table char rt nil)
389 ;; The dtable is a cons whose whose CAR is initially NIL but upgraded
390 ;; to a hashtable if required, and whose CDR is a vector indexed by
391 ;; char-code up to the maximum base-char.
392 (let ((dtable (cons nil (make-array base-char-code-limit
393 :initial-element nil))))
394 (set-macro-character char (%make-dispatch-macro-char dtable)
395 non-terminating-p rt)))
398 (defun set-dispatch-macro-character (disp-char sub-char function
399 &optional (rt-designator *readtable*))
400 "Cause FUNCTION to be called whenever the reader reads DISP-CHAR
401 followed by SUB-CHAR."
402 ;; Get the dispatch char for macro (error if not there), diddle
403 ;; entry for sub-char.
404 (let* ((sub-char (char-upcase sub-char))
405 (readtable (or rt-designator *standard-readtable*)))
406 (assert-not-standard-readtable readtable 'set-dispatch-macro-character)
407 (when (digit-char-p sub-char)
408 (error "SUB-CHAR must not be a decimal digit: ~S" sub-char))
409 (let ((dtable (get-dispatch-macro-char-table disp-char readtable))
410 (function (!coerce-to-cmt-entry function)))
411 ;; (SET-MACRO-CHARACTER #\$ (GET-MACRO-CHARACTER #\#)) will share
412 ;; the dispatch table. Perhaps it should be copy-on-write?
413 (if (typep sub-char 'base-char)
414 (setf (svref (cdr dtable) (char-code sub-char)) function)
415 (let ((hashtable (car dtable)))
416 (cond (function ; allocate the hashtable if it wasn't made yet
417 (setf (gethash sub-char
418 (or hashtable (setf (car dtable)
419 (make-hash-table))))
420 function))
421 (hashtable ; remove an existing entry
422 (remhash sub-char hashtable)))))))
425 (defun get-dispatch-macro-character (disp-char sub-char
426 &optional (rt-designator *readtable*))
427 "Return the macro character function for SUB-CHAR under DISP-CHAR
428 or NIL if there is no associated function."
429 (let ((dtable (get-dispatch-macro-char-table
430 disp-char (or rt-designator *standard-readtable*))))
431 (!cmt-entry-to-fun-designator
432 (get-raw-cmt-dispatch-entry (char-upcase sub-char) dtable))))
435 ;;;; definitions to support internal programming conventions
437 (defconstant +EOF+ 0)
439 (defun flush-whitespace (stream)
440 ;; This flushes whitespace chars, returning the last char it read (a
441 ;; non-white one). It always gets an error on end-of-file.
442 (let* ((stream (in-stream-from-designator stream))
443 (rt *readtable*)
444 (attribute-array (character-attribute-array rt))
445 (attribute-hash-table (character-attribute-hash-table rt)))
446 (macrolet ((done-p ()
447 '(not (eql (if (typep char 'base-char)
448 (aref attribute-array (char-code char))
449 (gethash char attribute-hash-table
450 +char-attr-constituent+))
451 +char-attr-whitespace+))))
452 (if (ansi-stream-p stream)
453 (prepare-for-fast-read-char stream
454 (loop (let ((char (fast-read-char t)))
455 (cond ((done-p)
456 (done-with-fast-read-char)
457 (return char))))))
458 ;; CLOS stream
459 (loop (let ((char (read-char stream nil +EOF+)))
460 ;; (THE) should not be needed if DONE-P, but it was not
461 ;; being derived to return a character, causing an extra
462 ;; check in consumers of flush-whitespace despite the
463 ;; promise to return a character or else signal EOF.
464 (cond ((eq char +EOF+) (error 'end-of-file :stream stream))
465 ((done-p) (return (the character char))))))))))
467 ;;;; temporary initialization hack
469 ;; Install the (easy) standard macro-chars into *READTABLE*.
470 (defun !cold-init-standard-readtable ()
471 (/show0 "entering !cold-init-standard-readtable")
472 ;; All characters get boring defaults in MAKE-READTABLE. Now we
473 ;; override the boring defaults on characters which need more
474 ;; interesting behavior.
475 (flet ((whitespaceify (char)
476 (set-cmt-entry char nil)
477 (set-cat-entry char +char-attr-whitespace+)))
478 (whitespaceify (code-char tab-char-code))
479 (whitespaceify #\Newline)
480 (whitespaceify #\Space)
481 (whitespaceify (code-char form-feed-char-code))
482 (whitespaceify (code-char return-char-code)))
484 (set-cat-entry #\\ +char-attr-single-escape+)
485 (set-cmt-entry #\\ nil)
487 (set-cat-entry #\| +char-attr-multiple-escape+)
488 (set-cmt-entry #\| nil)
490 ;; Easy macro-character definitions are in this source file.
491 (set-macro-character #\" #'read-string)
492 (set-macro-character #\' #'read-quote)
493 ;; Using symbols makes these traceable and redefineable with ease,
494 ;; as well as avoids a forward-referenced function (from "backq")
495 (set-macro-character #\( 'read-list)
496 (set-macro-character #\) 'read-right-paren)
497 (set-macro-character #\; #'read-comment)
498 ;; (The hairier macro-character definitions, for #\# and #\`, are
499 ;; defined elsewhere, in their own source files.)
501 ;; all constituents
502 (do ((ichar 0 (1+ ichar))
503 (char))
504 ((= ichar base-char-code-limit))
505 (setq char (code-char ichar))
506 (when (constituentp char *readtable*)
507 (set-cmt-entry char nil)))
509 (/show0 "leaving !cold-init-standard-readtable"))
511 ;;;; implementation of the read buffer
513 (defstruct (token-buf (:predicate nil) (:copier nil)
514 (:constructor
515 make-token-buf
516 (&aux
517 (initial-string (make-string 128))
518 (string initial-string)
519 (adjustable-string
520 (make-array 0
521 :element-type 'character
522 :fill-pointer nil
523 :displaced-to string)))))
524 ;; The string accumulated during reading of tokens.
525 ;; Always starts out EQ to 'initial-string'.
526 (string nil :type (simple-array character (*)))
527 ;; Counter advanced as characters are placed into 'string'
528 (fill-ptr 0 :type index)
529 ;; Counter advanced as characters are consumed from 'string' on re-scan
530 ;; by auxilliary functions MAKE-{INTEGER,FLOAT,RATIONAL} etc.
531 (cursor 0 :type index)
532 ;; A string used only for FIND-PACKAGE calls in package-qualified
533 ;; symbols so that we don't need to call SUBSEQ on the 'string'.
534 (adjustable-string nil :type (and (array character (*)) (not simple-array)))
535 ;; A small string that is permanently assigned into this token-buf.
536 (initial-string nil :type (simple-array character (128))
537 :read-only t)
538 (escapes (make-array 10 :element-type 'fixnum :fill-pointer 0 :adjustable t)
539 :type (and (vector fixnum) (not simple-array)) :read-only t)
540 ;; Link to next TOKEN-BUF, to chain the *TOKEN-BUF-POOL* together.
541 (next nil :type (or null token-buf))
542 (only-base-chars t :type boolean))
543 (declaim (freeze-type token-buf))
545 (defmethod print-object ((self token-buf) stream)
546 (print-unreadable-object (self stream :identity t :type t)
547 (format stream "~@[next=~S~]" (token-buf-next self))))
549 ;; The current TOKEN-BUF
550 (declaim (type token-buf *read-buffer*))
551 (defvar *read-buffer*)
553 ;; A list of available TOKEN-BUFs
554 (declaim (type (or null token-buf) *token-buf-pool*))
555 (!define-thread-local *token-buf-pool* nil)
557 (defun reset-read-buffer (buffer)
558 ;; Turn BUFFER into an empty read buffer.
559 (setf (fill-pointer (token-buf-escapes buffer)) 0)
560 (setf (token-buf-fill-ptr buffer) 0)
561 (setf (token-buf-cursor buffer) 0)
562 (setf (token-buf-only-base-chars buffer) t)
563 buffer)
565 ;; "Output" a character into the reader's buffer.
566 (declaim (inline ouch-read-buffer))
567 (defun ouch-read-buffer (char buffer)
568 ;; When buffer overflow
569 (let ((op (token-buf-fill-ptr buffer)))
570 (declare (optimize (sb!c::insert-array-bounds-checks 0)))
571 (when (>= op (length (token-buf-string buffer)))
572 ;; an out-of-line call for the uncommon case avoids bloat.
573 ;; Size should be doubled.
574 (grow-read-buffer))
575 (unless (typep char 'base-char)
576 (setf (token-buf-only-base-chars buffer) nil))
577 (setf (elt (token-buf-string buffer) op) char)
578 (setf (token-buf-fill-ptr buffer) (1+ op))))
580 (defun ouch-read-buffer-escaped (char buf)
581 (vector-push-extend (token-buf-fill-ptr buf) (token-buf-escapes buf))
582 (ouch-read-buffer char buf))
584 (defun grow-read-buffer ()
585 (let* ((b *read-buffer*)
586 (string (token-buf-string b)))
587 (setf (token-buf-string b)
588 (replace (make-string (* 2 (length string))) string))))
590 ;; Retun the next character from the buffered token, or NIL.
591 (declaim (maybe-inline token-buf-getchar))
592 (defun token-buf-getchar (b)
593 (declare (optimize (sb!c::insert-array-bounds-checks 0)))
594 (let ((i (token-buf-cursor (truly-the token-buf b))))
595 (and (< i (token-buf-fill-ptr b))
596 (prog1 (elt (token-buf-string b) i)
597 (setf (token-buf-cursor b) (1+ i))))))
599 ;; Grab a buffer off the token-buf pool if there is one, or else make one.
600 ;; This does not need to be protected against other threads because the
601 ;; pool is thread-local, or against async interrupts. An async signal
602 ;; delivered anywhere in the midst of the code sequence below can not
603 ;; corrupt the buffer given to the caller of ACQUIRE-TOKEN-BUF.
604 ;; Additionally the cleanup is on a "best effort" basis. Async unwinds
605 ;; through WITH-READ-BUFFER fail to recycle token-bufs, but that's ok.
606 (defun acquire-token-buf ()
607 (let ((this-buffer *token-buf-pool*))
608 (cond (this-buffer
609 (shiftf *token-buf-pool* (token-buf-next this-buffer) nil)
610 this-buffer)
612 (make-token-buf)))))
614 (defun release-token-buf (chain)
615 (named-let free ((buffer chain))
616 ;; If 'adjustable-string' was displaced to 'string',
617 ;; adjust it back down to allow GC of the abnormally large string.
618 (unless (eq (%array-data (token-buf-adjustable-string buffer))
619 (token-buf-initial-string buffer))
620 (adjust-array (token-buf-adjustable-string buffer) '(0)
621 :displaced-to (token-buf-initial-string buffer)))
622 ;; 'initial-string' is assigned into 'string'
623 ;; so not to preserve huge buffers in the pool indefinitely.
624 (setf (token-buf-string buffer) (token-buf-initial-string buffer))
625 (if (token-buf-next buffer)
626 (free (token-buf-next buffer))
627 (setf (token-buf-next buffer) *token-buf-pool*)))
628 (setf *token-buf-pool* chain))
630 ;; Return a fresh copy of BUFFER's string
631 (defun copy-token-buf-string (buffer)
632 (subseq (token-buf-string buffer) 0 (token-buf-fill-ptr buffer)))
634 ;; Return a string displaced to BUFFER's string.
635 ;; The string should not be held onto - either a copy must be made
636 ;; by the receiver, or it should be parsed into something else.
637 (defun sized-token-buf-string (buffer)
638 ;; It would in theory be faster to make the adjustable array have
639 ;; a fill-pointer, and just set that most of the time. Except we still
640 ;; need the ability to displace to a different string if a package name
641 ;; has >128 characters, so then there'd be two modes of sharing, one of
642 ;; which is rarely exercised and most likely to be subtly wrong.
643 ;; At any rate, SET-ARRAY-HEADER is faster than ADJUST-ARRAY.
644 ;; TODO: find evidence that it is/is-not worth having complicated
645 ;; mechanism involving a fill-pointer or not.
646 (set-array-header
647 (token-buf-adjustable-string buffer) ; the array
648 (token-buf-string buffer) ; the underlying data
649 (token-buf-fill-ptr buffer) ; total size
650 nil ; fill-pointer
651 0 ; displacement
652 (token-buf-fill-ptr buffer) ; dimension 0
653 t nil)) ; displacedp / newp
655 ;; Acquire a TOKEN-BUF from the pool and execute the body, returning only
656 ;; the primary value therefrom. Recycle the buffer when done.
657 ;; No UNWIND-PROTECT - recycling is designed to help with the common case
658 ;; of normal return and is not intended to be resilient against nonlocal exit.
659 (defmacro with-read-buffer (() &body body)
660 `(let* ((*read-buffer* (acquire-token-buf))
661 (result (progn ,@body)))
662 (release-token-buf *read-buffer*)
663 result))
665 (defun check-for-recursive-read (stream recursive-p operator-name)
666 (when (and recursive-p (not (boundp '*read-buffer*)))
667 (simple-reader-error
668 stream
669 "~A was invoked with RECURSIVE-P being true outside ~
670 of a recursive read operation."
671 `(,operator-name))))
673 ;;;; READ-PRESERVING-WHITESPACE, READ-DELIMITED-LIST, and READ
675 ;;; A list for #=, used to keep track of objects with labels assigned that
676 ;;; have been completely read. Each entry is a SHARP-EQUAL-WRAPPER object.
678 ;;; KLUDGE: Should this really be a list? It seems as though users
679 ;;; could reasonably expect N log N performance for large datasets.
680 ;;; On the other hand, it's probably very very seldom a problem in practice.
681 ;;; On the third hand, it might be just as easy to use a hash table,
682 ;;; so maybe we should. -- WHN 19991202
683 (defvar *sharp-equal* ())
685 (declaim (ftype (sfunction (t t) (values bit t)) read-maybe-nothing))
687 ;;; Like READ-PRESERVING-WHITESPACE, but doesn't check the read buffer
688 ;;; for being set up properly.
689 (defun %read-preserving-whitespace (stream eof-error-p eof-value recursive-p)
690 (declare (optimize (sb!c::check-tag-existence 0)))
691 (if recursive-p
692 ;; a loop for repeating when a macro returns nothing
693 (let* ((tracking-p (form-tracking-stream-p stream))
694 (outermost-p
695 (and tracking-p
696 (null (form-tracking-stream-form-start-char-pos stream)))))
697 (loop
698 (let ((char (read-char stream eof-error-p +EOF+)))
699 (cond ((eq char +EOF+) (return eof-value))
700 ((whitespace[2]p char))
702 (when outermost-p
703 ;; Calling FILE-POSITION at each token seems to slow down
704 ;; the reader by somewhere between 8x to 10x.
705 ;; Once per outermost form is acceptably fast though.
706 (setf (form-tracking-stream-form-start-byte-pos stream)
707 ;; pretend we queried the position before reading CHAR
708 (- (file-position stream)
709 (or (file-string-length stream (string char)) 0))
710 (form-tracking-stream-form-start-char-pos stream)
711 ;; likewise
712 (1- (form-tracking-stream-input-char-pos stream))))
713 (multiple-value-bind (result-p result)
714 (read-maybe-nothing stream char)
715 (unless (zerop result-p)
716 (return (unless *read-suppress* result)))
717 ;; Repeat if macro returned nothing.
718 (when tracking-p
719 (funcall (form-tracking-stream-observer stream)
720 :reset nil nil))))))))
721 (let ((*sharp-equal* nil))
722 (with-read-buffer ()
723 (%read-preserving-whitespace stream eof-error-p eof-value t)))))
725 ;;; READ-PRESERVING-WHITESPACE behaves just like READ, only it makes
726 ;;; sure to leave terminating whitespace in the stream. (This is a
727 ;;; COMMON-LISP exported symbol.)
728 (defun read-preserving-whitespace (&optional (stream *standard-input*)
729 (eof-error-p t)
730 (eof-value nil)
731 (recursive-p nil))
732 "Read from STREAM and return the value read, preserving any whitespace
733 that followed the object."
734 (declare (explicit-check))
735 (check-for-recursive-read stream recursive-p 'read-preserving-whitespace)
736 (%read-preserving-whitespace stream eof-error-p eof-value recursive-p))
738 ;;; Read from STREAM given starting CHAR, returning 1 and the resulting
739 ;;; object, unless CHAR is a macro yielding no value, then 0 and NIL,
740 ;;; for functions that want comments to return so that they can look
741 ;;; past them. CHAR must not be whitespace.
742 (defun read-maybe-nothing (stream char)
743 (truly-the
744 (values bit t) ; avoid a type-check. M-V-CALL is lame
745 (multiple-value-call
746 (lambda (stream start-pos &optional (result nil supplied-p) &rest junk)
747 (declare (ignore junk)) ; is this ANSI-specified?
748 (when (and supplied-p start-pos)
749 (funcall (form-tracking-stream-observer stream)
750 start-pos
751 (form-tracking-stream-input-char-pos stream) result))
752 (values (if supplied-p 1 0) result))
753 ;; KLUDGE: not capturing anything in the lambda avoids closure consing
754 stream
755 (and (form-tracking-stream-p stream)
756 ;; Subtract 1 because the position points _after_ CHAR.
757 (1- (form-tracking-stream-input-char-pos stream)))
758 (funcall (!cmt-entry-to-function
759 (get-raw-cmt-entry char *readtable*) #'read-token)
760 stream char))))
762 (defun read (&optional (stream *standard-input*)
763 (eof-error-p t)
764 (eof-value nil)
765 (recursive-p nil))
766 "Read the next Lisp value from STREAM, and return it."
767 (declare (explicit-check))
768 (check-for-recursive-read stream recursive-p 'read)
769 (let* ((local-eof-val (load-time-value (cons nil nil) t))
770 (result (%read-preserving-whitespace
771 stream eof-error-p local-eof-val recursive-p)))
772 ;; This function generally discards trailing whitespace. If you
773 ;; don't want to discard trailing whitespace, call
774 ;; CL:READ-PRESERVING-WHITESPACE instead.
775 (unless (or (eql result local-eof-val) recursive-p)
776 (let ((next-char (read-char stream nil +EOF+)))
777 (unless (or (eq next-char +EOF+)
778 (whitespace[2]p next-char))
779 (unread-char next-char stream))))
780 (if (eq result local-eof-val) eof-value result)))
783 ;;;; basic readmacro definitions
784 ;;;;
785 ;;;; Some large, hairy subsets of readmacro definitions (backquotes
786 ;;;; and sharp macros) are not here, but in their own source files.
788 (defun read-quote (stream ignore)
789 (declare (ignore ignore))
790 (list 'quote (read stream t nil t)))
792 (defun read-comment (stream ignore)
793 (declare (ignore ignore))
794 (handler-bind
795 ((character-decoding-error
796 #'(lambda (decoding-error)
797 (declare (ignorable decoding-error))
798 (style-warn
799 'sb!kernel::character-decoding-error-in-macro-char-comment
800 :position (file-position stream) :stream stream)
801 (invoke-restart 'attempt-resync))))
802 (let ((stream (in-stream-from-designator stream)))
803 (if (ansi-stream-p stream)
804 (prepare-for-fast-read-char stream
805 (loop (let ((char (fast-read-char nil +EOF+)))
806 (when (or (eq char +EOF+) (char= char #\newline))
807 (return (done-with-fast-read-char))))))
808 ;; CLOS stream
809 (loop (let ((char (read-char stream nil +EOF+)))
810 (when (or (eq char +EOF+) (char= char #\newline))
811 (return)))))))
812 ;; Don't return anything.
813 (values))
815 ;;; FIXME: for these two macro chars, if STREAM is a FORM-TRACKING-STREAM,
816 ;;; every cons cell should generate a notification so that the readtable
817 ;;; manipulation in SB-COVER can be eliminated in favor of a stream observer.
818 ;;; It is cheap to add events- it won't increase consing in the compiler
819 ;;; because it the extra events can simply be ignored.
820 (macrolet
821 ((with-list-reader ((streamvar delimiter) &body body)
822 `(let* ((thelist (list nil))
823 (listtail thelist)
824 (collectp (if *read-suppress* 0 -1)))
825 (declare (dynamic-extent thelist))
826 (loop (let ((firstchar (flush-whitespace ,streamvar)))
827 (when (eq firstchar ,delimiter)
828 (return (cdr thelist)))
829 ,@body))))
830 (read-list-item (streamvar)
831 `(multiple-value-bind (winp obj)
832 (read-maybe-nothing ,streamvar firstchar)
833 ;; allow for a character macro return to return nothing
834 (unless (zerop (logand winp collectp))
835 (setq listtail
836 (cdr (rplacd (truly-the cons listtail) (list obj))))))))
838 ;;; The character macro handler for left paren
839 (defun read-list (stream ignore)
840 (declare (ignore ignore))
841 (with-list-reader (stream #\))
842 (when (eq firstchar #\.)
843 (let ((nextchar (read-char stream t)))
844 (cond ((token-delimiterp nextchar)
845 (cond ((eq listtail thelist)
846 (unless (zerop collectp)
847 (simple-reader-error
848 stream "Nothing appears before . in list.")))
849 ((whitespace[2]p nextchar)
850 (setq nextchar (flush-whitespace stream))))
851 (rplacd (truly-the cons listtail)
852 (read-after-dot stream nextchar collectp))
853 ;; Check for improper ". ,@" or ". ,." now rather than
854 ;; in the #\` reader. The resulting QUASIQUOTE macro might
855 ;; never be exapanded, but nonetheless could be erroneous.
856 (unless (zerop (logand *backquote-depth* collectp))
857 (let ((lastcdr (cdr (last listtail))))
858 (when (and (comma-p lastcdr) (comma-splicing-p lastcdr))
859 (simple-reader-error
860 stream "~S contains a splicing comma after a dot"
861 (cdr thelist)))))
862 (return (cdr thelist)))
863 ;; Put back NEXTCHAR so that we can read it normally.
864 (t (unread-char nextchar stream)))))
865 ;; Next thing is not an isolated dot.
866 (read-list-item stream)))
868 ;;; (This is a COMMON-LISP exported symbol.)
869 (defun read-delimited-list (endchar &optional
870 (input-stream *standard-input*)
871 recursive-p)
872 "Read Lisp values from INPUT-STREAM until the next character after a
873 value's representation is ENDCHAR, and return the objects as a list."
874 (declare (explicit-check))
875 (check-for-recursive-read input-stream recursive-p 'read-delimited-list)
876 (flet ((%read-delimited-list ()
877 (with-list-reader (input-stream endchar)
878 (read-list-item input-stream))))
879 (if recursive-p
880 (%read-delimited-list)
881 (with-read-buffer () (%read-delimited-list)))))) ; end MACROLET
883 (defun read-after-dot (stream firstchar collectp)
884 ;; FIRSTCHAR is non-whitespace!
885 (let ((lastobj ()))
886 (do ((char firstchar (flush-whitespace stream)))
887 ((eq char #\))
888 (if (zerop collectp)
889 (return-from read-after-dot nil)
890 (simple-reader-error stream "Nothing appears after . in list.")))
891 ;; See whether there's something there.
892 (multiple-value-bind (winp obj) (read-maybe-nothing stream char)
893 (unless (zerop winp) (return (setq lastobj obj)))))
894 ;; At least one thing appears after the dot.
895 ;; Check for more than one thing following dot.
896 (loop
897 (let ((char (flush-whitespace stream)))
898 (cond ((eq char #\)) (return lastobj)) ;success!
899 ;; Try reading virtual whitespace.
900 ((not (zerop (logand (read-maybe-nothing stream char)
901 (truly-the fixnum collectp))))
902 (simple-reader-error
903 stream "More than one object follows . in list.")))))))
905 (defun read-string (stream closech)
906 ;; This accumulates chars until it sees same char that invoked it.
907 ;; We avoid copying any given input character more than twice-
908 ;; once to a temp buffer and then to the result. In the worst case,
909 ;; we can waste space equal the unwasted space, if the final character
910 ;; causes allocation of a new buffer for just that character,
911 ;; because the buffer size is doubled each time it overflows.
912 ;; (Would be better to peek at the frc-buffer if the stream has one.)
913 ;; Scratch vectors are GC-able as soon as this function returns though.
914 (declare (character closech))
915 (macrolet ((scan (read-a-char eofp &optional finish)
916 `(loop (let ((char ,read-a-char))
917 (declare (optimize (sb!c::insert-array-bounds-checks 0)))
918 (cond (,eofp (error 'end-of-file :stream stream))
919 ((eql char closech)
920 (return ,finish))
921 ((single-escape-p char rt)
922 (setq char ,read-a-char)
923 (when ,eofp
924 (error 'end-of-file :stream stream))))
925 (when (>= ptr lim)
926 (unless suppress
927 (push buf chain)
928 (setq lim (the index (ash lim 1))
929 buf (make-array lim :element-type 'character)))
930 (setq ptr 0))
931 (setf (schar buf ptr) (truly-the character char))
932 #!+sb-unicode ; BASE-CHAR-P does not exist if not
933 (unless (base-char-p char) (setq only-base-chars nil))
934 (incf ptr)))))
935 (let* ((token-buf *read-buffer*)
936 (buf (token-buf-string token-buf))
937 (rt *readtable*)
938 (stream (in-stream-from-designator stream))
939 (suppress *read-suppress*)
940 (lim (length buf))
941 (ptr 0)
942 (only-base-chars t)
943 (chain))
944 (declare (type (simple-array character (*)) buf))
945 (reset-read-buffer token-buf)
946 (if (ansi-stream-p stream)
947 (prepare-for-fast-read-char stream
948 (scan (fast-read-char t) nil (done-with-fast-read-char)))
949 ;; CLOS stream
950 (scan (read-char stream nil +EOF+) (eq char +EOF+)))
951 (if suppress
953 (let* ((sum (loop for buf in chain sum (length buf)))
954 (result
955 (make-array (+ sum ptr)
956 :element-type (if only-base-chars
957 (%readtable-string-preference rt)
958 'character))))
959 (setq ptr sum)
960 ;; Now work backwards from the end
961 (replace result buf :start1 ptr)
962 (dolist (buf chain result)
963 (declare (type (simple-array character (*)) buf))
964 (let ((len (length buf)))
965 (decf ptr len)
966 (replace result buf :start1 ptr))))))))
968 (defun read-right-paren (stream ignore)
969 (declare (ignore ignore))
970 (simple-reader-error stream "unmatched close parenthesis"))
972 ;;; Read from the stream up to the next delimiter. Leave the resulting
973 ;;; token in *READ-BUFFER*, and return three values:
974 ;;; -- a TOKEN-BUF
975 ;;; -- whether any escape character was seen (even if no character is escaped)
976 ;;; -- whether a package delimiter character was seen
977 ;;; Normalizes the input to NFKC before returning
978 (defun internal-read-extended-token (stream firstchar escape-firstchar
979 &aux (read-buffer *read-buffer*))
980 (reset-read-buffer read-buffer)
981 (when escape-firstchar
982 (ouch-read-buffer-escaped firstchar read-buffer)
983 (setq firstchar (read-char stream nil +EOF+)))
984 (do ((char firstchar (read-char stream nil +EOF+))
985 (seen-multiple-escapes nil)
986 (rt *readtable*)
987 (colon nil))
988 ((cond ((eq char +EOF+) t)
989 ((token-delimiterp char rt)
990 (unread-char char stream)
992 (t nil))
993 (progn
994 (multiple-value-setq (read-buffer colon)
995 (normalize-read-buffer read-buffer colon))
996 (values read-buffer
997 (or (plusp (fill-pointer (token-buf-escapes read-buffer)))
998 seen-multiple-escapes)
999 colon)))
1000 (flet ((escape-1-char ()
1001 ;; It can't be a number, even if it's 1\23.
1002 ;; Read next char here, so it won't be casified.
1003 (let ((nextchar (read-char stream nil +EOF+)))
1004 (if (eq nextchar +EOF+)
1005 (reader-eof-error stream "after escape character")
1006 (ouch-read-buffer-escaped nextchar read-buffer)))))
1007 (cond ((single-escape-p char rt) (escape-1-char))
1008 ((multiple-escape-p char rt)
1009 (setq seen-multiple-escapes t)
1010 ;; Read to next multiple-escape, escaping single chars
1011 ;; along the way.
1012 (loop
1013 (let ((ch (read-char stream nil +EOF+)))
1014 (cond ((eq ch +EOF+)
1015 (reader-eof-error stream "inside extended token"))
1016 ((multiple-escape-p ch rt) (return))
1017 ((single-escape-p ch rt) (escape-1-char))
1018 (t (ouch-read-buffer-escaped ch read-buffer))))))
1020 (when (and (not colon) ; easiest test first
1021 (constituentp char rt)
1022 (eql (get-constituent-trait char)
1023 +char-attr-package-delimiter+))
1024 (setq colon t))
1025 (ouch-read-buffer char read-buffer))))))
1027 ;;;; character classes
1029 ;;; Return the character class for CHAR.
1031 ;;; FIXME: why aren't these ATT-getting forms using GET-CAT-ENTRY?
1032 ;;; Because we've cached the readtable tables?
1033 (defmacro char-class (char attarray atthash)
1034 `(let ((att (if (typep (truly-the character ,char) 'base-char)
1035 (aref ,attarray (char-code ,char))
1036 (gethash ,char ,atthash +char-attr-constituent+))))
1037 (declare (fixnum att))
1038 (cond
1039 ((<= att +char-attr-terminating-macro+) +char-attr-delimiter+)
1040 ((< att +char-attr-constituent+) att)
1041 (t (setf att (get-constituent-trait ,char))
1042 (if (= att +char-attr-invalid+)
1043 (simple-reader-error stream "invalid constituent")
1044 att)))))
1046 ;;; Return the character class for CHAR, which might be part of a
1047 ;;; rational number.
1048 (defmacro char-class2 (char attarray atthash read-base)
1049 `(let ((att (if (typep (truly-the character ,char) 'base-char)
1050 (aref ,attarray (char-code ,char))
1051 (gethash ,char ,atthash +char-attr-constituent+))))
1052 (declare (fixnum att))
1053 (cond
1054 ((<= att +char-attr-terminating-macro+) +char-attr-delimiter+)
1055 ((< att +char-attr-constituent+) att)
1056 (t (setf att (get-constituent-trait ,char))
1057 (cond
1058 ((digit-char-p ,char ,read-base) +char-attr-constituent-digit+)
1059 ((= att +char-attr-constituent-digit+) +char-attr-constituent+)
1060 ((= att +char-attr-invalid+)
1061 (simple-reader-error stream "invalid constituent"))
1062 (t att))))))
1064 ;;; Return the character class for a char which might be part of a
1065 ;;; rational or floating number. (Assume that it is a digit if it
1066 ;;; could be.)
1067 (defmacro char-class3 (char attarray atthash read-base)
1068 `(let ((att (if (typep (truly-the character ,char) 'base-char)
1069 (aref ,attarray (char-code ,char))
1070 (gethash ,char ,atthash +char-attr-constituent+))))
1071 (declare (fixnum att))
1072 (cond
1073 ((<= att +char-attr-terminating-macro+) +char-attr-delimiter+)
1074 ((< att +char-attr-constituent+) att)
1075 (t (setf att (get-constituent-trait ,char))
1076 (when possibly-rational
1077 (setq possibly-rational
1078 (or (digit-char-p ,char ,read-base)
1079 (= att +char-attr-constituent-slash+))))
1080 (when possibly-float
1081 (setq possibly-float
1082 (or (digit-char-p ,char 10)
1083 (= att +char-attr-constituent-dot+))))
1084 (cond
1085 ((digit-char-p ,char (max ,read-base 10))
1086 (if (digit-char-p ,char ,read-base)
1087 (if (= att +char-attr-constituent-expt+)
1088 +char-attr-constituent-digit-or-expt+
1089 +char-attr-constituent-digit+)
1090 +char-attr-constituent-decimal-digit+))
1091 ((= att +char-attr-invalid+)
1092 (simple-reader-error stream "invalid constituent"))
1093 (t att))))))
1095 ;;;; token fetching
1097 (defvar *read-suppress* nil
1098 "Suppress most interpreting in the reader when T.")
1100 (defvar *read-base* 10
1101 "the radix that Lisp reads numbers in")
1102 (declaim (type (integer 2 36) *read-base*))
1104 ;;; Normalize TOKEN-BUF to NFKC, returning a new TOKEN-BUF and the
1105 ;;; COLON value
1106 (defun normalize-read-buffer (token-buf &optional colon)
1107 (when (or (token-buf-only-base-chars token-buf)
1108 (not (readtable-normalization *readtable*)))
1109 (return-from normalize-read-buffer (values token-buf colon)))
1110 (let ((current-buffer (copy-token-buf-string token-buf))
1111 (old-escapes (copy-seq (token-buf-escapes token-buf)))
1112 (str-to-normalize (make-string (token-buf-fill-ptr token-buf)))
1113 (normalize-ptr 0) (escapes-ptr 0))
1114 (reset-read-buffer token-buf)
1115 (macrolet ((clear-str-to-normalize ()
1116 `(progn
1117 (loop for char across (sb!unicode:normalize-string
1118 (subseq str-to-normalize 0 normalize-ptr)
1119 :nfkc) do
1120 (ouch-read-buffer char token-buf))
1121 (setf normalize-ptr 0)))
1122 (push-to-normalize (ch)
1123 (let ((ch-gen (gensym)))
1124 `(let ((,ch-gen ,ch))
1125 (setf (char str-to-normalize normalize-ptr) ,ch-gen)
1126 (incf normalize-ptr)))))
1127 (loop for c across current-buffer
1128 for i from 0
1130 (if (and (< escapes-ptr (length old-escapes))
1131 (eql i (aref old-escapes escapes-ptr)))
1132 (progn
1133 (clear-str-to-normalize)
1134 (ouch-read-buffer-escaped c token-buf)
1135 (incf escapes-ptr))
1136 (push-to-normalize c)))
1137 (clear-str-to-normalize)
1138 (values token-buf colon))))
1140 ;;; Modify the read buffer according to READTABLE-CASE, ignoring
1141 ;;; ESCAPES. ESCAPES is a vector of the escaped indices.
1142 (defun casify-read-buffer (token-buf)
1143 (let ((case (readtable-case *readtable*))
1144 (escapes (token-buf-escapes token-buf)))
1145 (cond
1146 ((and (zerop (length escapes)) (eq case :upcase))
1147 (let ((buffer (token-buf-string token-buf)))
1148 (dotimes (i (token-buf-fill-ptr token-buf))
1149 (declare (optimize (sb!c::insert-array-bounds-checks 0)))
1150 (setf (schar buffer i) (char-upcase (schar buffer i))))))
1151 ((eq case :preserve))
1153 (macrolet ((skip-esc (&body body)
1154 `(do ((i (1- (token-buf-fill-ptr token-buf)) (1- i))
1155 (buffer (token-buf-string token-buf))
1156 (esc (if (zerop (fill-pointer escapes))
1157 -1 (vector-pop escapes))))
1158 ((minusp i))
1159 (declare (fixnum i)
1160 (optimize (sb!c::insert-array-bounds-checks 0)))
1161 (if (< esc i)
1162 (let ((ch (schar buffer i)))
1163 ,@body)
1164 (progn
1165 (aver (= esc i))
1166 (setq esc (if (zerop (fill-pointer escapes))
1167 -1 (vector-pop escapes))))))))
1168 (flet ((lower-em ()
1169 (skip-esc (setf (schar buffer i) (char-downcase ch))))
1170 (raise-em ()
1171 (skip-esc (setf (schar buffer i) (char-upcase ch)))))
1172 (ecase case
1173 (:upcase (raise-em))
1174 (:downcase (lower-em))
1175 (:invert
1176 (let ((all-upper t)
1177 (all-lower t)
1178 (fillptr (fill-pointer escapes)))
1179 (skip-esc
1180 (when (both-case-p ch)
1181 (if (upper-case-p ch)
1182 (setq all-lower nil)
1183 (setq all-upper nil))))
1184 (setf (fill-pointer escapes) fillptr)
1185 (cond (all-lower (raise-em))
1186 (all-upper (lower-em))))))))))))
1188 (eval-when (:compile-toplevel :load-toplevel :execute)
1189 (defvar *reader-package* nil))
1190 (declaim (type (or null package) *reader-package*)
1191 (always-bound *reader-package*))
1193 (defun reader-find-package (package-designator stream)
1194 (if (%instancep package-designator)
1195 package-designator
1196 (let ((package (find-package package-designator)))
1197 (cond (package
1198 ;; Release the token-buf that was used for the designator
1199 (release-token-buf (shiftf (token-buf-next *read-buffer*) nil))
1200 package)
1202 (error 'simple-reader-package-error
1203 :package package-designator
1204 :stream stream
1205 :format-control "Package ~A does not exist."
1206 :format-arguments (list package-designator)))))))
1208 (defun read-token (stream firstchar)
1209 "Default readmacro function. Handles numbers, symbols, and SBCL's
1210 extended <package-name>::<form-in-package> syntax."
1211 ;; Check explicitly whether FIRSTCHAR has an entry for
1212 ;; NON-TERMINATING in CHARACTER-ATTRIBUTE-TABLE and
1213 ;; READ-DOT-NUMBER-SYMBOL in CMT. Report an error if these are
1214 ;; violated. (If we called this, we want something that is a
1215 ;; legitimate token!) Read in the longest possible string satisfying
1216 ;; the Backus-Naur form for "unqualified-token". Leave the result in
1217 ;; the *READ-BUFFER*. Return next char after token (last char read).
1218 (when *read-suppress*
1219 (internal-read-extended-token stream firstchar nil)
1220 (return-from read-token nil))
1221 (let* ((rt *readtable*)
1222 (base *read-base*)
1223 (attribute-array (character-attribute-array rt))
1224 (attribute-hash-table (character-attribute-hash-table rt))
1225 (buf *read-buffer*)
1226 (package-designator nil)
1227 (colons 0)
1228 (possibly-rational t)
1229 (seen-digit-or-expt nil)
1230 (possibly-float t)
1231 (was-possibly-float nil)
1232 (seen-multiple-escapes nil))
1233 (declare (token-buf buf))
1234 (reset-read-buffer buf)
1235 (macrolet ((getchar-or-else (what)
1236 `(when (eq (setq char (read-char stream nil +EOF+)) +EOF+)
1237 ,what)))
1238 (prog ((char firstchar))
1239 (case (char-class3 char attribute-array attribute-hash-table base)
1240 (#.+char-attr-constituent-sign+ (go SIGN))
1241 (#.+char-attr-constituent-digit+ (go LEFTDIGIT))
1242 (#.+char-attr-constituent-digit-or-expt+
1243 (setq seen-digit-or-expt t)
1244 (go LEFTDIGIT))
1245 (#.+char-attr-constituent-decimal-digit+ (go LEFTDECIMALDIGIT))
1246 (#.+char-attr-constituent-dot+ (go FRONTDOT))
1247 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1248 (#.+char-attr-package-delimiter+ (go COLON))
1249 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1250 (#.+char-attr-invalid+ (simple-reader-error stream
1251 "invalid constituent"))
1252 ;; can't have eof, whitespace, or terminating macro as first char!
1253 (t (go SYMBOL)))
1254 SIGN ; saw "sign"
1255 (ouch-read-buffer char buf)
1256 (getchar-or-else (go RETURN-SYMBOL))
1257 (setq possibly-rational t
1258 possibly-float t)
1259 (case (char-class3 char attribute-array attribute-hash-table base)
1260 (#.+char-attr-constituent-digit+ (go LEFTDIGIT))
1261 (#.+char-attr-constituent-digit-or-expt+
1262 (setq seen-digit-or-expt t)
1263 (go LEFTDIGIT))
1264 (#.+char-attr-constituent-decimal-digit+ (go LEFTDECIMALDIGIT))
1265 (#.+char-attr-constituent-dot+ (go SIGNDOT))
1266 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1267 (#.+char-attr-package-delimiter+ (go COLON))
1268 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1269 (#.+char-attr-delimiter+ (unread-char char stream) (go RETURN-SYMBOL))
1270 (t (go SYMBOL)))
1271 LEFTDIGIT ; saw "[sign] {digit}+"
1272 (ouch-read-buffer char buf)
1273 (getchar-or-else (return (make-integer)))
1274 (setq was-possibly-float possibly-float)
1275 (case (char-class3 char attribute-array attribute-hash-table base)
1276 (#.+char-attr-constituent-digit+ (go LEFTDIGIT))
1277 (#.+char-attr-constituent-decimal-digit+ (if possibly-float
1278 (go LEFTDECIMALDIGIT)
1279 (go SYMBOL)))
1280 (#.+char-attr-constituent-dot+ (if possibly-float
1281 (go MIDDLEDOT)
1282 (go SYMBOL)))
1283 (#.+char-attr-constituent-digit-or-expt+
1284 (if (or seen-digit-or-expt (not was-possibly-float))
1285 (progn (setq seen-digit-or-expt t) (go LEFTDIGIT))
1286 (progn (setq seen-digit-or-expt t) (go LEFTDIGIT-OR-EXPT))))
1287 (#.+char-attr-constituent-expt+
1288 (if was-possibly-float
1289 (go EXPONENT)
1290 (go SYMBOL)))
1291 (#.+char-attr-constituent-slash+ (if possibly-rational
1292 (go RATIO)
1293 (go SYMBOL)))
1294 (#.+char-attr-delimiter+ (unread-char char stream)
1295 (return (make-integer)))
1296 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1297 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1298 (#.+char-attr-package-delimiter+ (go COLON))
1299 (t (go SYMBOL)))
1300 LEFTDIGIT-OR-EXPT
1301 (ouch-read-buffer char buf)
1302 (getchar-or-else (return (make-integer)))
1303 (case (char-class3 char attribute-array attribute-hash-table base)
1304 (#.+char-attr-constituent-digit+ (go LEFTDIGIT))
1305 (#.+char-attr-constituent-decimal-digit+ (bug "impossible!"))
1306 (#.+char-attr-constituent-dot+ (go SYMBOL))
1307 (#.+char-attr-constituent-digit-or-expt+ (go LEFTDIGIT))
1308 (#.+char-attr-constituent-expt+ (go SYMBOL))
1309 (#.+char-attr-constituent-sign+ (go EXPTSIGN))
1310 (#.+char-attr-constituent-slash+ (if possibly-rational
1311 (go RATIO)
1312 (go SYMBOL)))
1313 (#.+char-attr-delimiter+ (unread-char char stream)
1314 (return (make-integer)))
1315 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1316 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1317 (#.+char-attr-package-delimiter+ (go COLON))
1318 (t (go SYMBOL)))
1319 LEFTDECIMALDIGIT ; saw "[sign] {decimal-digit}+"
1320 (aver possibly-float)
1321 (ouch-read-buffer char buf)
1322 (getchar-or-else (go RETURN-SYMBOL))
1323 (case (char-class char attribute-array attribute-hash-table)
1324 (#.+char-attr-constituent-digit+ (go LEFTDECIMALDIGIT))
1325 (#.+char-attr-constituent-dot+ (go MIDDLEDOT))
1326 (#.+char-attr-constituent-expt+ (go EXPONENT))
1327 (#.+char-attr-constituent-slash+ (aver (not possibly-rational))
1328 (go SYMBOL))
1329 (#.+char-attr-delimiter+ (unread-char char stream)
1330 (go RETURN-SYMBOL))
1331 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1332 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1333 (#.+char-attr-package-delimiter+ (go COLON))
1334 (t (go SYMBOL)))
1335 MIDDLEDOT ; saw "[sign] {digit}+ dot"
1336 (ouch-read-buffer char buf)
1337 (getchar-or-else (return (make-integer 10)))
1338 (case (char-class char attribute-array attribute-hash-table)
1339 (#.+char-attr-constituent-digit+ (go RIGHTDIGIT))
1340 (#.+char-attr-constituent-expt+ (go EXPONENT))
1341 (#.+char-attr-delimiter+
1342 (unread-char char stream)
1343 (return (make-integer 10)))
1344 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1345 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1346 (#.+char-attr-package-delimiter+ (go COLON))
1347 (t (go SYMBOL)))
1348 RIGHTDIGIT ; saw "[sign] {decimal-digit}* dot {digit}+"
1349 (ouch-read-buffer char buf)
1350 (getchar-or-else (return (make-float stream)))
1351 (case (char-class char attribute-array attribute-hash-table)
1352 (#.+char-attr-constituent-digit+ (go RIGHTDIGIT))
1353 (#.+char-attr-constituent-expt+ (go EXPONENT))
1354 (#.+char-attr-delimiter+
1355 (unread-char char stream)
1356 (return (make-float stream)))
1357 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1358 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1359 (#.+char-attr-package-delimiter+ (go COLON))
1360 (t (go SYMBOL)))
1361 SIGNDOT ; saw "[sign] dot"
1362 (ouch-read-buffer char buf)
1363 (getchar-or-else (go RETURN-SYMBOL))
1364 (case (char-class char attribute-array attribute-hash-table)
1365 (#.+char-attr-constituent-digit+ (go RIGHTDIGIT))
1366 (#.+char-attr-delimiter+ (unread-char char stream) (go RETURN-SYMBOL))
1367 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1368 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1369 (t (go SYMBOL)))
1370 FRONTDOT ; saw "dot"
1371 (ouch-read-buffer char buf)
1372 (getchar-or-else (simple-reader-error stream "dot context error"))
1373 (case (char-class char attribute-array attribute-hash-table)
1374 (#.+char-attr-constituent-digit+ (go RIGHTDIGIT))
1375 (#.+char-attr-constituent-dot+ (go DOTS))
1376 (#.+char-attr-delimiter+ (simple-reader-error stream
1377 "dot context error"))
1378 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1379 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1380 (#.+char-attr-package-delimiter+ (go COLON))
1381 (t (go SYMBOL)))
1382 EXPONENT
1383 (ouch-read-buffer char buf)
1384 (getchar-or-else (go RETURN-SYMBOL))
1385 (setq possibly-float t)
1386 (case (char-class char attribute-array attribute-hash-table)
1387 (#.+char-attr-constituent-sign+ (go EXPTSIGN))
1388 (#.+char-attr-constituent-digit+ (go EXPTDIGIT))
1389 (#.+char-attr-delimiter+ (unread-char char stream) (go RETURN-SYMBOL))
1390 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1391 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1392 (#.+char-attr-package-delimiter+ (go COLON))
1393 (t (go SYMBOL)))
1394 EXPTSIGN ; got to EXPONENT, and saw a sign character
1395 (ouch-read-buffer char buf)
1396 (getchar-or-else (go RETURN-SYMBOL))
1397 (case (char-class char attribute-array attribute-hash-table)
1398 (#.+char-attr-constituent-digit+ (go EXPTDIGIT))
1399 (#.+char-attr-delimiter+ (unread-char char stream) (go RETURN-SYMBOL))
1400 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1401 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1402 (#.+char-attr-package-delimiter+ (go COLON))
1403 (t (go SYMBOL)))
1404 EXPTDIGIT ; got to EXPONENT, saw "[sign] {digit}+"
1405 (ouch-read-buffer char buf)
1406 (getchar-or-else (return (make-float stream)))
1407 (case (char-class char attribute-array attribute-hash-table)
1408 (#.+char-attr-constituent-digit+ (go EXPTDIGIT))
1409 (#.+char-attr-delimiter+
1410 (unread-char char stream)
1411 (return (make-float stream)))
1412 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1413 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1414 (#.+char-attr-package-delimiter+ (go COLON))
1415 (t (go SYMBOL)))
1416 RATIO ; saw "[sign] {digit}+ slash"
1417 (ouch-read-buffer char buf)
1418 (getchar-or-else (go RETURN-SYMBOL))
1419 (case (char-class2 char attribute-array attribute-hash-table base)
1420 (#.+char-attr-constituent-digit+ (go RATIODIGIT))
1421 (#.+char-attr-delimiter+ (unread-char char stream) (go RETURN-SYMBOL))
1422 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1423 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1424 (#.+char-attr-package-delimiter+ (go COLON))
1425 (t (go SYMBOL)))
1426 RATIODIGIT ; saw "[sign] {digit}+ slash {digit}+"
1427 (ouch-read-buffer char buf)
1428 (getchar-or-else (return (make-ratio stream)))
1429 (case (char-class2 char attribute-array attribute-hash-table base)
1430 (#.+char-attr-constituent-digit+ (go RATIODIGIT))
1431 (#.+char-attr-delimiter+
1432 (unread-char char stream)
1433 (return (make-ratio stream)))
1434 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1435 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1436 (#.+char-attr-package-delimiter+ (go COLON))
1437 (t (go SYMBOL)))
1438 DOTS ; saw "dot {dot}+"
1439 (ouch-read-buffer char buf)
1440 (getchar-or-else (simple-reader-error stream "too many dots"))
1441 (case (char-class char attribute-array attribute-hash-table)
1442 (#.+char-attr-constituent-dot+ (go DOTS))
1443 (#.+char-attr-delimiter+
1444 (unread-char char stream)
1445 (simple-reader-error stream "too many dots"))
1446 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1447 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1448 (#.+char-attr-package-delimiter+ (go COLON))
1449 (t (go SYMBOL)))
1450 SYMBOL ; not a dot, dots, or number
1451 (let ((stream (in-stream-from-designator stream)))
1452 (macrolet
1453 ((scan (read-a-char &optional finish)
1454 `(prog ()
1455 SYMBOL-LOOP
1456 (ouch-read-buffer char buf)
1457 (setq char ,read-a-char)
1458 (when (eq char +EOF+) (go RETURN-SYMBOL))
1459 (case (char-class char attribute-array attribute-hash-table)
1460 (#.+char-attr-single-escape+ ,finish (go SINGLE-ESCAPE))
1461 (#.+char-attr-delimiter+ ,finish
1462 (unread-char char stream)
1463 (go RETURN-SYMBOL))
1464 (#.+char-attr-multiple-escape+ ,finish (go MULT-ESCAPE))
1465 (#.+char-attr-package-delimiter+ ,finish (go COLON))
1466 (t (go SYMBOL-LOOP))))))
1467 (if (ansi-stream-p stream)
1468 (prepare-for-fast-read-char stream
1469 (scan (fast-read-char nil +EOF+) (done-with-fast-read-char)))
1470 ;; CLOS stream
1471 (scan (read-char stream nil +EOF+)))))
1472 SINGLE-ESCAPE ; saw a single-escape
1473 ;; Don't put the escape character in the read buffer.
1474 ;; READ-NEXT CHAR, put in buffer (no case conversion).
1475 (let ((nextchar (read-char stream nil +EOF+)))
1476 (when (eq nextchar +EOF+)
1477 (reader-eof-error stream "after single-escape character"))
1478 (ouch-read-buffer-escaped nextchar buf))
1479 (getchar-or-else (go RETURN-SYMBOL))
1480 (case (char-class char attribute-array attribute-hash-table)
1481 (#.+char-attr-delimiter+ (unread-char char stream) (go RETURN-SYMBOL))
1482 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1483 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1484 (#.+char-attr-package-delimiter+ (go COLON))
1485 (t (go SYMBOL)))
1486 MULT-ESCAPE
1487 (setq seen-multiple-escapes t)
1488 ;; sometimes we pass eof-error=nil but check. here we just let it err.
1489 ;; should pick one style and stick with it.
1490 (do ((char (read-char stream t) (read-char stream t)))
1491 ((multiple-escape-p char rt))
1492 (if (single-escape-p char rt) (setq char (read-char stream t)))
1493 (ouch-read-buffer-escaped char buf))
1494 (getchar-or-else (go RETURN-SYMBOL))
1495 (case (char-class char attribute-array attribute-hash-table)
1496 (#.+char-attr-delimiter+ (unread-char char stream) (go RETURN-SYMBOL))
1497 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1498 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1499 (#.+char-attr-package-delimiter+ (go COLON))
1500 (t (go SYMBOL)))
1501 COLON
1502 (unless (zerop colons)
1503 (simple-reader-error
1504 stream "too many colons in ~S" (copy-token-buf-string buf)))
1505 (setf buf (normalize-read-buffer buf))
1506 (casify-read-buffer buf)
1507 (setq colons 1)
1508 (setq package-designator
1509 (if (or (plusp (token-buf-fill-ptr buf)) seen-multiple-escapes)
1510 (prog1 (sized-token-buf-string buf)
1511 (let ((new (acquire-token-buf)))
1512 (setf (token-buf-next new) buf ; new points to old
1513 buf new *read-buffer* new)))
1514 *keyword-package*))
1515 (reset-read-buffer buf)
1516 (getchar-or-else (reader-eof-error stream "after reading a colon"))
1517 (case (char-class char attribute-array attribute-hash-table)
1518 (#.+char-attr-delimiter+
1519 (unread-char char stream)
1520 (simple-reader-error stream
1521 "illegal terminating character after a colon: ~S"
1522 char))
1523 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1524 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1525 (#.+char-attr-package-delimiter+ (go INTERN))
1526 (t (go SYMBOL)))
1527 INTERN
1528 (setq colons 2)
1529 (getchar-or-else (reader-eof-error stream "after reading a colon"))
1530 (case (char-class char attribute-array attribute-hash-table)
1531 (#.+char-attr-delimiter+
1532 (unread-char char stream)
1533 (if package-designator
1534 (let* ((*reader-package*
1535 (reader-find-package package-designator stream)))
1536 (return (read stream t nil t)))
1537 (simple-reader-error stream
1538 "illegal terminating character after a double-colon: ~S"
1539 char)))
1540 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1541 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1542 (#.+char-attr-package-delimiter+
1543 (simple-reader-error stream
1544 "too many colons after ~S name"
1545 package-designator))
1546 (t (go SYMBOL)))
1547 RETURN-SYMBOL
1548 (setf buf (normalize-read-buffer buf))
1549 (casify-read-buffer buf)
1550 (let* ((pkg (if package-designator
1551 (reader-find-package package-designator stream)
1552 (or *reader-package* (sane-package))))
1553 (intern-p (or (/= colons 1) (eq pkg *keyword-package*))))
1554 (unless intern-p ; Try %FIND-SYMBOL
1555 (multiple-value-bind (symbol accessibility)
1556 (%find-symbol (token-buf-string buf) (token-buf-fill-ptr buf) pkg)
1557 (when (eq accessibility :external) (return symbol))
1558 (with-simple-restart (continue "Use symbol anyway.")
1559 (error 'simple-reader-package-error
1560 :package pkg
1561 :stream stream
1562 :format-arguments
1563 (list (copy-token-buf-string buf) (package-name pkg))
1564 :format-control
1565 (if accessibility
1566 "The symbol ~S is not external in the ~A package."
1567 "Symbol ~S not found in the ~A package.")))))
1568 (return (%intern (token-buf-string buf)
1569 (token-buf-fill-ptr buf)
1571 (if (token-buf-only-base-chars buf)
1572 (%readtable-symbol-preference rt)
1573 'character))))))))
1575 ;;; For semi-external use: Return 3 values: the token-buf,
1576 ;;; a flag for whether there was an escape char, and the position of
1577 ;;; any package delimiter. The returned token-buf is not case-converted.
1578 (defun read-extended-token (stream)
1579 ;; recursive-p = T is basically irrelevant.
1580 (let ((first-char (read-char stream nil +EOF+ t)))
1581 (if (neq first-char +EOF+)
1582 (internal-read-extended-token stream first-char nil)
1583 (values (reset-read-buffer *read-buffer*) nil nil))))
1585 ;;; for semi-external use:
1587 ;;; Read an extended token with the first character escaped. Return
1588 ;;; the token-buf. The returned token-buf is not case-converted.
1589 (defun read-extended-token-escaped (stream)
1590 (let ((first-char (read-char stream nil +EOF+)))
1591 (if (neq first-char +EOF+)
1592 (values (internal-read-extended-token stream first-char t))
1593 (reader-eof-error stream "after escape"))))
1595 ;;;; number-reading functions
1597 ;; Mapping of read-base to the max input characters in a positive fixnum.
1598 (eval-when (:compile-toplevel :execute)
1599 (defun integer-reader-safe-digits ()
1600 (do ((a (make-array 35 :element-type '(unsigned-byte 8)))
1601 (base 2 (1+ base)))
1602 ((> base 36) a)
1603 (do ((total (1- base) (+ (* total base) (1- base)))
1604 (n-digits 0 (1+ n-digits)))
1605 ((sb!xc:typep total 'bignum)
1606 (setf (aref a (- base 2)) n-digits))
1607 ;; empty DO body
1610 ;; self-test
1611 (do ((maxdigits (integer-reader-safe-digits))
1612 (base 2 (1+ base)))
1613 ((> base 36))
1614 (let* ((n-digits (aref maxdigits (- base 2)))
1615 (d (char (write-to-string (1- base) :base base) 0))
1616 (string (make-string (1+ n-digits) :initial-element d))) ; 1 extra
1617 (assert (not (typep (parse-integer string :radix base)
1618 `(unsigned-byte ,sb!vm:n-positive-fixnum-bits))))
1619 (assert (typep (parse-integer string :end n-digits :radix base)
1620 `(unsigned-byte ,sb!vm:n-positive-fixnum-bits))))))
1622 (defmacro !setq-optional-leading-sign (sign-flag token-buf rewind)
1623 ;; guaranteed to have at least one character in buffer at the start
1624 ;; or immediately following [ESFDL] marker depending on 'rewind' flag.
1625 `(locally (declare (optimize (sb!c::insert-array-bounds-checks 0)))
1626 (,(if rewind 'setf 'incf)
1627 (token-buf-cursor ,token-buf)
1628 (case (elt (token-buf-string ,token-buf)
1629 ,(if rewind 0 `(token-buf-cursor ,token-buf)))
1630 (#\- (setq ,sign-flag t) 1)
1631 (#\+ 1)
1632 (t 0)))))
1634 (defun make-integer (&optional (base *read-base*))
1635 "Minimizes bignum-fixnum multiplies by reading a 'safe' number of digits,
1636 then multiplying by a power of the base and adding."
1637 (declare ((integer 2 36) base)
1638 (inline token-buf-getchar)) ; makes for smaller code
1639 (let* ((fixnum-max-digits
1640 (macrolet ((maxdigits ()
1641 (!coerce-to-specialized (integer-reader-safe-digits)
1642 '(unsigned-byte 8))))
1643 (aref (maxdigits) (- base 2))))
1644 (base-power
1645 (macrolet ((base-powers ()
1646 (do ((maxdigits (integer-reader-safe-digits))
1647 (a (make-array 35))
1648 (base 2 (1+ base)))
1649 ((> base 36) a)
1650 (setf (aref a (- base 2))
1651 (expt base (aref maxdigits (- base 2)))))))
1652 (truly-the integer (aref (base-powers) (- base 2)))))
1653 (negativep nil)
1654 (result 0)
1655 (buf *read-buffer*))
1656 (!setq-optional-leading-sign negativep buf t)
1657 (loop
1658 (let ((acc 0))
1659 (declare (type (and fixnum unsigned-byte) acc))
1660 (dotimes (digit-count fixnum-max-digits)
1661 (let ((ch (token-buf-getchar buf)))
1662 (if (or (not ch) (eql ch #\.))
1663 (return-from make-integer
1664 (let ((result
1665 (if (zerop result) acc
1666 (+ (* result (expt base digit-count)) acc))))
1667 (if negativep (- result) result)))
1668 (setq acc (truly-the fixnum
1669 (+ (digit-char-p ch base)
1670 (truly-the fixnum (* acc base))))))))
1671 (setq result (+ (* result base-power) acc))))))
1673 (defun truncate-exponent (exponent number divisor)
1674 "Truncate exponent if it's too large for a float"
1675 ;; Work with base-2 logarithms to avoid conversions to floats,
1676 ;; and convert to base-10 conservatively at the end.
1677 ;; Use the least positive float, because denormalized exponent
1678 ;; can be larger than normalized.
1679 (let* ((max-exponent
1680 #!-long-float
1681 (+ sb!vm:double-float-digits sb!vm:double-float-bias))
1682 (number-magnitude (integer-length number))
1683 (divisor-magnitude (1- (integer-length divisor)))
1684 (magnitude (- number-magnitude divisor-magnitude)))
1685 (if (minusp exponent)
1686 (max exponent (ceiling (- (+ max-exponent magnitude))
1687 #.(floor (log 10 2))))
1688 (min exponent (floor (- max-exponent magnitude)
1689 #.(floor (log 10 2)))))))
1691 (defun make-float (stream)
1692 ;; Assume that the contents of *read-buffer* are a legal float, with nothing
1693 ;; else after it.
1694 (let ((buf *read-buffer*)
1695 (negative-fraction nil)
1696 (number 0)
1697 (divisor 1)
1698 (negative-exponent nil)
1699 (exponent 0)
1700 (float-char ())
1701 char)
1702 (!setq-optional-leading-sign negative-fraction buf t)
1703 ;; Read digits before the dot.
1704 (macrolet ((accumulate (expr)
1705 `(let (digit)
1706 (loop (if (and (setq char (token-buf-getchar buf))
1707 (setq digit (digit-char-p char)))
1708 ,expr
1709 (return))))))
1710 (accumulate (setq number (+ (* number 10) digit)))
1711 ;; Deal with the dot, if it's there.
1712 (when (char= char #\.)
1713 ;; Read digits after the dot.
1714 (accumulate (setq divisor (* divisor 10)
1715 number (+ (* number 10) digit))))
1716 ;; Is there an exponent letter?
1717 (cond
1718 ((null char)
1719 ;; If not, we've read the whole number.
1720 (let ((num (make-float-aux number divisor
1721 *read-default-float-format*
1722 stream)))
1723 (return-from make-float (if negative-fraction (- num) num))))
1724 ((= (get-constituent-trait char) +char-attr-constituent-expt+)
1725 (setq float-char char)
1726 ;; Check leading sign.
1727 (!setq-optional-leading-sign negative-exponent buf nil)
1728 ;; Read digits for exponent.
1729 (accumulate (setq exponent (+ (* exponent 10) digit)))
1730 (setq exponent (if negative-exponent (- exponent) exponent))
1731 ;; Generate and return the float, depending on FLOAT-CHAR:
1732 (let* ((float-format (case (char-upcase float-char)
1733 (#\E *read-default-float-format*)
1734 (#\S 'short-float)
1735 (#\F 'single-float)
1736 (#\D 'double-float)
1737 (#\L 'long-float)))
1738 (exponent (truncate-exponent exponent number divisor))
1739 (result (make-float-aux (* (expt 10 exponent) number)
1740 divisor float-format stream)))
1741 (return-from make-float
1742 (if negative-fraction (- result) result))))
1743 (t (bug "bad fallthrough in floating point reader"))))))
1745 (defun make-float-aux (number divisor float-format stream)
1746 (handler-case
1747 (coerce (/ number divisor) float-format)
1748 (arithmetic-error (c)
1749 (error 'reader-impossible-number-error
1750 :error c :stream stream
1751 :format-control "failed to build float from ~a"
1752 :format-arguments (list (copy-token-buf-string *read-buffer*))))))
1754 (defun make-ratio (stream)
1755 ;; Assume *READ-BUFFER* contains a legal ratio. Build the number from
1756 ;; the string.
1757 ;; This code is inferior to that of MAKE-INTEGER because it makes no
1758 ;; attempt to perform as few bignum multiplies as possible.
1760 (let ((numerator 0) (denominator 0) (negativep nil)
1761 (base *read-base*) (buf *read-buffer*))
1762 (!setq-optional-leading-sign negativep buf t)
1763 ;; Get numerator.
1764 (loop (let ((dig (digit-char-p (token-buf-getchar buf) base)))
1765 (if dig
1766 (setq numerator (+ (* numerator base) dig))
1767 (return))))
1768 ;; Get denominator.
1769 (do* ((ch (token-buf-getchar buf) (token-buf-getchar buf))
1770 (dig ()))
1771 ((or (null ch) (not (setq dig (digit-char-p ch base)))))
1772 (setq denominator (+ (* denominator base) dig)))
1773 (let ((num (handler-case
1774 (/ numerator denominator)
1775 (arithmetic-error (c)
1776 (error 'reader-impossible-number-error
1777 :error c :stream stream
1778 :format-control "failed to build ratio")))))
1779 (if negativep (- num) num))))
1781 ;;;; General reader for dispatch macros
1783 (defun dispatch-char-error (stream sub-char ignore)
1784 (declare (optimize allow-non-returning-tail-call))
1785 (declare (ignore ignore))
1786 (if *read-suppress*
1787 (values)
1788 (simple-reader-error stream
1789 "no dispatch function defined for ~S"
1790 sub-char)))
1792 (defun read-dispatch-char (stream dispatch-table)
1793 ;; Read some digits.
1794 (let ((numargp nil)
1795 (numarg 0)
1796 (sub-char ()))
1797 (loop
1798 (let ((ch (read-char stream nil +EOF+)))
1799 (if (eq ch +EOF+)
1800 (reader-eof-error stream "inside dispatch character")
1801 ;; Take care of the extra char.
1802 (let ((dig (digit-char-p ch)))
1803 (if dig
1804 (setq numargp t numarg (+ (* numarg 10) dig))
1805 (return (setq sub-char (char-upcase ch))))))))
1806 ;; Look up the function and call it.
1807 (let ((fn (get-raw-cmt-dispatch-entry sub-char dispatch-table)))
1808 (funcall (!cmt-entry-to-function fn #'dispatch-char-error)
1809 stream sub-char (if numargp numarg nil)))))
1811 ;;;; READ-FROM-STRING
1813 (declaim (ftype (sfunction (string t t index (or null index) t) (values t index))
1814 %read-from-string))
1815 (defun %read-from-string (string eof-error-p eof-value start end preserve-whitespace)
1816 (with-array-data ((string string :offset-var offset)
1817 (start start)
1818 (end end)
1819 :check-fill-pointer t)
1820 (let ((stream (make-string-input-stream string start end)))
1821 (values (if preserve-whitespace
1822 (%read-preserving-whitespace stream eof-error-p eof-value nil)
1823 (read stream eof-error-p eof-value))
1824 (- (string-input-stream-current stream) offset)))))
1826 (locally
1827 (declare (muffle-conditions style-warning))
1828 (defun read-from-string (string &optional (eof-error-p t) eof-value
1829 &key (start 0) end preserve-whitespace)
1830 "The characters of string are successively given to the lisp reader
1831 and the lisp object built by the reader is returned. Macro chars
1832 will take effect."
1833 (declare (string string))
1834 (maybe-note-read-from-string-signature-issue eof-error-p)
1835 (%read-from-string string eof-error-p eof-value start end preserve-whitespace)))
1837 ;;;; PARSE-INTEGER
1839 (defun parse-integer (string &key (start 0) end (radix 10) junk-allowed)
1840 "Examine the substring of string delimited by start and end
1841 (default to the beginning and end of the string) It skips over
1842 whitespace characters and then tries to parse an integer. The
1843 radix parameter must be between 2 and 36."
1844 (flet ((parse-error (format-control)
1845 (declare (optimize allow-non-returning-tail-call))
1846 (error 'simple-parse-error
1847 :format-control format-control
1848 :format-arguments (list string))))
1849 (with-array-data ((string string :offset-var offset)
1850 (start start)
1851 (end end)
1852 :check-fill-pointer t)
1853 (let ((index (do ((i start (1+ i)))
1854 ((= i end)
1855 (if junk-allowed
1856 (return-from parse-integer (values nil end))
1857 (parse-error "no non-whitespace characters in string ~S.")))
1858 (declare (fixnum i))
1859 (unless (whitespace[1]p (char string i)) (return i))))
1860 (minusp nil)
1861 (found-digit nil)
1862 (result 0))
1863 (declare (fixnum index))
1864 (let ((char (char string index)))
1865 (cond ((char= char #\-)
1866 (setq minusp t)
1867 (incf index))
1868 ((char= char #\+)
1869 (incf index))))
1870 (loop
1871 (when (= index end) (return nil))
1872 (let* ((char (char string index))
1873 (weight (digit-char-p char radix)))
1874 (cond (weight
1875 (setq result (+ weight (* result radix))
1876 found-digit t))
1877 (junk-allowed (return nil))
1878 ((whitespace[1]p char)
1879 (loop
1880 (incf index)
1881 (when (= index end) (return))
1882 (unless (whitespace[1]p (char string index))
1883 (parse-error "junk in string ~S")))
1884 (return nil))
1886 (parse-error "junk in string ~S"))))
1887 (incf index))
1888 (values
1889 (if found-digit
1890 (if minusp (- result) result)
1891 (if junk-allowed
1893 (parse-error "no digits in string ~S")))
1894 (- index offset))))))
1896 ;;;; reader initialization code
1898 (defun !reader-cold-init ()
1899 (!cold-init-standard-readtable))
1901 (defmethod print-object ((readtable readtable) stream)
1902 (print-unreadable-object (readtable stream :identity t :type t)))
1904 ;; Backward-compatibility adapter. The "named-readtables" system in
1905 ;; Quicklisp expects this interface, and it's a reasonable thing to support.
1906 ;; What is silly however is that DISPATCH-TABLES was an alist each of whose
1907 ;; values was a hashtable which got immediately coerced to an alist.
1908 ;; In anticipation of perhaps not doing an extra re-shaping, if HASH-TABLE-P
1909 ;; is NIL then return nested alists: ((#\# (#\R . #<FUNCTION SHARP-R>) ...))
1910 (defun dispatch-tables (readtable &optional (hash-table-p t))
1911 (let (alist)
1912 (flet ((process (char fn &aux (dtable (%dispatch-macro-char-table fn)))
1913 (when dtable
1914 (let ((output (awhen (car dtable) (%hash-table-alist it))))
1915 (loop for fn across (the simple-vector (cdr dtable))
1916 and ch from 0
1917 when fn do (push (cons (code-char ch) fn) output))
1918 (dolist (cell output) ; coerce values to function-designator
1919 (rplacd cell (!cmt-entry-to-fun-designator (cdr cell))))
1920 (when hash-table-p ; caller wants hash-tables
1921 (setq output (%stuff-hash-table (make-hash-table) output)))
1922 (push (cons char output) alist)))))
1923 (loop for fn across (character-macro-array readtable) and ch from 0
1924 do (process (code-char ch) fn))
1925 (maphash #'process (character-macro-hash-table readtable)))
1926 alist))
1928 ;; Stub - should never get called with anything but NIL
1929 ;; and only after all macros have been changed to constituents already.
1930 (defun (setf dispatch-tables) (new-alist readtable)
1931 (declare (ignore readtable))
1932 (unless (null new-alist)
1933 (error "Assignment to virtual DISPATCH-TABLES slot not allowed"))
1934 new-alist)
1936 ;;; like LISTEN, but any whitespace in the input stream will be flushed
1937 (defun listen-skip-whitespace (&optional (stream *standard-input*))
1938 (do ((char (read-char-no-hang stream nil nil nil)
1939 (read-char-no-hang stream nil nil nil)))
1940 ((null char) nil)
1941 (cond ((not (whitespace[1]p char))
1942 (unread-char char stream)
1943 (return t)))))