6f042923347384611206da9d14a7fc304f209d90
[sbcl.git] / src / code / reader.lisp
1 ;;;; READ and friends
2
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.
11
12 (in-package "SB!IMPL")
13 \f
14 ;;;; miscellaneous global variables
15
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*))
22
23 (defvar *readtable*)
24 (declaim (type readtable *readtable*))
25 #!+sb-doc
26 (setf (fdocumentation '*readtable* 'variable)
27       "Variable bound to current readtable.")
28
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 (defvar *standard-readtable* nil)
33
34 (defvar *old-package* nil
35   #!+sb-doc
36   "the value of *PACKAGE* at the start of the last read, or NIL")
37
38 ;;; In case we get an error trying to parse a symbol, we want to rebind the
39 ;;; above stuff so it's cool.
40
41 ;;; FIXME: These forward declarations should be moved somewhere earlier,
42 ;;; or discarded.
43 (declaim (special *package* *keyword-package* *read-base*))
44 \f
45 ;;;; reader errors
46
47 (defun reader-eof-error (stream context)
48   (error 'reader-eof-error
49          :stream stream
50          :context context))
51
52 ;;; If The Gods didn't intend for us to use multiple namespaces, why
53 ;;; did They specify them?
54 (defun simple-reader-error (stream control &rest args)
55   (error 'simple-reader-error
56          :stream stream
57          :format-control control
58          :format-arguments args))
59 \f
60 ;;;; macros and functions for character tables
61
62 (defun get-cat-entry (char rt)
63   (declare (readtable rt))
64   (if (typep char 'base-char)
65       (elt (character-attribute-array rt) (char-code char))
66       (values (gethash char (character-attribute-hash-table rt)
67                        +char-attr-constituent+))))
68
69 (defun set-cat-entry (char newvalue &optional (rt *readtable*))
70   (declare (readtable rt))
71   (if (typep char 'base-char)
72       (setf (elt (character-attribute-array rt) (char-code char)) newvalue)
73       (if (= newvalue +char-attr-constituent+)
74           ;; Default value for the C-A-HASH-TABLE is +CHAR-ATTR-CONSTITUENT+.
75           (%remhash char (character-attribute-hash-table rt))
76           (setf (gethash char (character-attribute-hash-table rt)) newvalue)))
77   (values))
78
79 ;;; the value actually stored in the character macro table. As per
80 ;;; ANSI #'GET-MACRO-CHARACTER and #'SET-MACRO-CHARACTER, this can
81 ;;; be either a function or NIL.
82 (defun get-raw-cmt-entry (char readtable)
83   (declare (readtable readtable))
84   (if (typep char 'base-char)
85       (svref (character-macro-array readtable) (char-code char))
86       ;; Note: DEFAULT here is NIL, not #'UNDEFINED-MACRO-CHAR, so
87       ;; that everything above the base-char range is a non-macro
88       ;; constituent by default.
89       (values (gethash char (character-macro-hash-table readtable) nil))))
90
91 ;;; the value represented by whatever is stored in the character macro
92 ;;; table. As per ANSI #'GET-MACRO-CHARACTER and #'SET-MACRO-CHARACTER,
93 ;;; a function value represents itself, and a NIL value represents the
94 ;;; default behavior.
95 (defun get-coerced-cmt-entry (char readtable)
96   (the function
97     (or (get-raw-cmt-entry char readtable)
98         #'read-token)))
99
100 (defun set-cmt-entry (char new-value-designator &optional (rt *readtable*))
101   (let ((new (when new-value-designator
102                (%coerce-callable-to-fun new-value-designator))))
103     (if (typep char 'base-char)
104         (setf (svref (character-macro-array rt) (char-code char)) new)
105         (setf (gethash char (character-macro-hash-table rt)) new))))
106
107 (defun undefined-macro-char (stream char)
108   (unless *read-suppress*
109     (simple-reader-error stream "undefined read-macro character ~S" char)))
110
111 ;;; The character attribute table is a CHAR-CODE-LIMIT vector of integers.
112
113 (defmacro test-attribute (char whichclass rt)
114   `(= (the fixnum (get-cat-entry ,char ,rt)) ,whichclass))
115
116 ;;; predicates for testing character attributes
117
118 #!-sb-fluid
119 (progn
120   (declaim (inline whitespace[1]p whitespace[2]p))
121   (declaim (inline constituentp terminating-macrop))
122   (declaim (inline single-escape-p multiple-escape-p))
123   (declaim (inline token-delimiterp)))
124
125 ;;; the [1] and [2] here refer to ANSI glossary entries for
126 ;;; "whitespace".
127 (defun whitespace[1]p (char)
128   (test-attribute char +char-attr-whitespace+ *standard-readtable*))
129 (defun whitespace[2]p (char &optional (rt *readtable*))
130   (test-attribute char +char-attr-whitespace+ rt))
131
132 (defun constituentp (char &optional (rt *readtable*))
133   (test-attribute char +char-attr-constituent+ rt))
134
135 (defun terminating-macrop (char &optional (rt *readtable*))
136   (test-attribute char +char-attr-terminating-macro+ rt))
137
138 (defun single-escape-p (char &optional (rt *readtable*))
139   (test-attribute char +char-attr-single-escape+ rt))
140
141 (defun multiple-escape-p (char &optional (rt *readtable*))
142   (test-attribute char +char-attr-multiple-escape+ rt))
143
144 (defun token-delimiterp (char &optional (rt *readtable*))
145   ;; depends on actual attribute numbering in readtable.lisp.
146   (<= (get-cat-entry char rt) +char-attr-terminating-macro+))
147 \f
148 ;;;; constituent traits (see ANSI 2.1.4.2)
149
150 ;;; There are a number of "secondary" attributes which are constant
151 ;;; properties of characters (as long as they are constituents).
152
153 (defvar *constituent-trait-table*)
154 (declaim (type attribute-table *constituent-trait-table*))
155
156 (defun !set-constituent-trait (char trait)
157   (aver (typep char 'base-char))
158   (setf (elt *constituent-trait-table* (char-code char))
159         trait))
160
161 (defun !cold-init-constituent-trait-table ()
162   (setq *constituent-trait-table*
163         (make-array base-char-code-limit :element-type '(unsigned-byte 8)
164                     :initial-element +char-attr-constituent+))
165   (!set-constituent-trait #\: +char-attr-package-delimiter+)
166   (!set-constituent-trait #\. +char-attr-constituent-dot+)
167   (!set-constituent-trait #\+ +char-attr-constituent-sign+)
168   (!set-constituent-trait #\- +char-attr-constituent-sign+)
169   (!set-constituent-trait #\/ +char-attr-constituent-slash+)
170   (do ((i (char-code #\0) (1+ i)))
171       ((> i (char-code #\9)))
172     (!set-constituent-trait (code-char i) +char-attr-constituent-digit+))
173   (!set-constituent-trait #\E +char-attr-constituent-expt+)
174   (!set-constituent-trait #\F +char-attr-constituent-expt+)
175   (!set-constituent-trait #\D +char-attr-constituent-expt+)
176   (!set-constituent-trait #\S +char-attr-constituent-expt+)
177   (!set-constituent-trait #\L +char-attr-constituent-expt+)
178   (!set-constituent-trait #\e +char-attr-constituent-expt+)
179   (!set-constituent-trait #\f +char-attr-constituent-expt+)
180   (!set-constituent-trait #\d +char-attr-constituent-expt+)
181   (!set-constituent-trait #\s +char-attr-constituent-expt+)
182   (!set-constituent-trait #\l +char-attr-constituent-expt+)
183   (!set-constituent-trait #\Space +char-attr-invalid+)
184   (!set-constituent-trait #\Newline +char-attr-invalid+)
185   (dolist (c (list backspace-char-code tab-char-code form-feed-char-code
186                    return-char-code rubout-char-code))
187     (!set-constituent-trait (code-char c) +char-attr-invalid+)))
188
189 (declaim (inline get-constituent-trait))
190 (defun get-constituent-trait (char)
191   (if (typep char 'base-char)
192       (elt *constituent-trait-table* (char-code char))
193       +char-attr-constituent+))
194 \f
195 ;;;; Readtable Operations
196
197 (defun assert-not-standard-readtable (readtable operation)
198   (when (eq readtable *standard-readtable*)
199     (cerror "Frob it anyway!" 'standard-readtable-modified-error
200             :operation operation)))
201
202 (defun readtable-case (readtable)
203   (%readtable-case readtable))
204
205 (defun (setf readtable-case) (case readtable)
206   (assert-not-standard-readtable readtable '(setf readtable-case))
207   (setf (%readtable-case readtable) case))
208
209 (defun shallow-replace/eql-hash-table (to from)
210   (maphash (lambda (k v) (setf (gethash k to) v)) from))
211
212 (defun copy-readtable (&optional (from-readtable *readtable*) to-readtable)
213   (assert-not-standard-readtable to-readtable 'copy-readtable)
214   (let ((really-from-readtable (or from-readtable *standard-readtable*))
215         (really-to-readtable (or to-readtable (make-readtable))))
216     (replace (character-attribute-array really-to-readtable)
217              (character-attribute-array really-from-readtable))
218     (shallow-replace/eql-hash-table
219      (character-attribute-hash-table really-to-readtable)
220      (character-attribute-hash-table really-from-readtable))
221     (replace (character-macro-array really-to-readtable)
222              (character-macro-array really-from-readtable))
223     (shallow-replace/eql-hash-table
224      (character-macro-hash-table really-to-readtable)
225      (character-macro-hash-table really-from-readtable))
226     (setf (dispatch-tables really-to-readtable)
227           (mapcar (lambda (pair)
228                     (cons (car pair)
229                           (let ((table (make-hash-table)))
230                             (shallow-replace/eql-hash-table table (cdr pair))
231                             table)))
232                   (dispatch-tables really-from-readtable)))
233     (setf (readtable-case really-to-readtable)
234           (readtable-case really-from-readtable))
235     really-to-readtable))
236
237 (defun set-syntax-from-char (to-char from-char &optional
238                              (to-readtable *readtable*) (from-readtable nil))
239   #!+sb-doc
240   "Causes the syntax of TO-CHAR to be the same as FROM-CHAR in the optional
241 readtable (defaults to the current readtable). The FROM-TABLE defaults to the
242 standard Lisp readtable when NIL."
243   (assert-not-standard-readtable to-readtable 'set-syntax-from-char)
244   (let ((really-from-readtable (or from-readtable *standard-readtable*)))
245     (let ((att (get-cat-entry from-char really-from-readtable))
246           (mac (get-raw-cmt-entry from-char really-from-readtable))
247           (from-dpair (find from-char (dispatch-tables really-from-readtable)
248                             :test #'char= :key #'car))
249           (to-dpair (find to-char (dispatch-tables to-readtable)
250                           :test #'char= :key #'car)))
251       (set-cat-entry to-char att to-readtable)
252       (set-cmt-entry to-char mac to-readtable)
253       (when from-dpair
254         (cond
255           (to-dpair
256            (let ((table (cdr to-dpair)))
257              (clrhash table)
258              (shallow-replace/eql-hash-table table (cdr from-dpair))))
259           (t
260            (let ((pair (cons to-char (make-hash-table))))
261              (shallow-replace/eql-hash-table (cdr pair) (cdr from-dpair))
262              (setf (dispatch-tables to-readtable)
263                    (push pair (dispatch-tables to-readtable)))))))))
264   t)
265
266 (defun set-macro-character (char function &optional
267                                  (non-terminatingp nil)
268                                  (rt-designator *readtable*))
269   #!+sb-doc
270   "Causes CHAR to be a macro character which invokes FUNCTION when seen
271    by the reader. The NON-TERMINATINGP flag can be used to make the macro
272    character non-terminating, i.e. embeddable in a symbol name."
273   (let ((designated-readtable (or rt-designator *standard-readtable*)))
274     (assert-not-standard-readtable designated-readtable 'set-macro-character)
275     (set-cat-entry char (if non-terminatingp
276                             +char-attr-constituent+
277                             +char-attr-terminating-macro+)
278                    designated-readtable)
279     (set-cmt-entry char function designated-readtable)
280     t)) ; (ANSI-specified return value)
281
282 (defun get-macro-character (char &optional (rt-designator *readtable*))
283   #!+sb-doc
284   "Return the function associated with the specified CHAR which is a macro
285   character, or NIL if there is no such function. As a second value, return
286   T if CHAR is a macro character which is non-terminating, i.e. which can
287   be embedded in a symbol name."
288   (let* ((designated-readtable (or rt-designator *standard-readtable*))
289          ;; the first return value: a FUNCTION if CHAR is a macro
290          ;; character, or NIL otherwise
291          (fun-value (get-raw-cmt-entry char designated-readtable)))
292     (values fun-value
293             ;; NON-TERMINATING-P return value:
294             (if fun-value
295                 (or (constituentp char)
296                     (not (terminating-macrop char)))
297                 ;; ANSI's definition of GET-MACRO-CHARACTER says this
298                 ;; value is NIL when CHAR is not a macro character.
299                 ;; I.e. this value means not just "non-terminating
300                 ;; character?" but "non-terminating macro character?".
301                 nil))))
302
303
304 (defun make-char-dispatch-table ()
305   (make-hash-table))
306
307 (defun make-dispatch-macro-character (char &optional
308                                       (non-terminating-p nil)
309                                       (rt *readtable*))
310   #!+sb-doc
311   "Cause CHAR to become a dispatching macro character in readtable (which
312    defaults to the current readtable). If NON-TERMINATING-P, the char will
313    be non-terminating."
314   ;; Checks already for standard readtable modification.
315   (set-macro-character char #'read-dispatch-char non-terminating-p rt)
316   (let* ((dalist (dispatch-tables rt))
317          (dtable (cdr (find char dalist :test #'char= :key #'car))))
318     (cond (dtable
319            (error "The dispatch character ~S already exists." char))
320           (t
321            (setf (dispatch-tables rt)
322                  (push (cons char (make-char-dispatch-table)) dalist)))))
323   t)
324
325 (defun set-dispatch-macro-character (disp-char sub-char function
326                                      &optional (rt-designator *readtable*))
327   #!+sb-doc
328   "Cause FUNCTION to be called whenever the reader reads DISP-CHAR
329    followed by SUB-CHAR."
330   ;; Get the dispatch char for macro (error if not there), diddle
331   ;; entry for sub-char.
332   (let* ((sub-char (char-upcase sub-char))
333          (readtable (or rt-designator *standard-readtable*)))
334     (assert-not-standard-readtable readtable 'set-dispatch-macro-character)
335     (when (digit-char-p sub-char)
336       (error "SUB-CHAR must not be a decimal digit: ~S" sub-char))
337     (let ((dpair (find disp-char (dispatch-tables readtable)
338                        :test #'char= :key #'car)))
339       (if dpair
340           (setf (gethash sub-char (cdr dpair)) (coerce function 'function))
341           (error "~S is not a dispatch char." disp-char))))
342   t)
343
344 (defun get-dispatch-macro-character (disp-char sub-char
345                                      &optional (rt-designator *readtable*))
346   #!+sb-doc
347   "Return the macro character function for SUB-CHAR under DISP-CHAR
348    or NIL if there is no associated function."
349   (let* ((sub-char  (char-upcase sub-char))
350          (readtable (or rt-designator *standard-readtable*))
351          (dpair     (find disp-char (dispatch-tables readtable)
352                           :test #'char= :key #'car)))
353     (if dpair
354         (values (gethash sub-char (cdr dpair)))
355         (error "~S is not a dispatch char." disp-char))))
356
357 \f
358 ;;;; definitions to support internal programming conventions
359
360 (declaim (inline eofp))
361 (defun eofp (char)
362   (eq char *eof-object*))
363
364 (defun flush-whitespace (stream)
365   ;; This flushes whitespace chars, returning the last char it read (a
366   ;; non-white one). It always gets an error on end-of-file.
367   (let ((stream (in-synonym-of stream)))
368     (if (ansi-stream-p stream)
369         (prepare-for-fast-read-char stream
370           (do ((attribute-array (character-attribute-array *readtable*))
371                (attribute-hash-table
372                 (character-attribute-hash-table *readtable*))
373                (char (fast-read-char t) (fast-read-char t)))
374               ((/= (the fixnum
375                      (if (typep char 'base-char)
376                          (aref attribute-array (char-code char))
377                          (gethash char attribute-hash-table
378                                   +char-attr-constituent+)))
379                    +char-attr-whitespace+)
380                (done-with-fast-read-char)
381                char)))
382         ;; CLOS stream
383         (do ((attribute-array (character-attribute-array *readtable*))
384              (attribute-hash-table
385               (character-attribute-hash-table *readtable*))
386              (char (read-char stream nil :eof) (read-char stream nil :eof)))
387             ((or (eq char :eof)
388                  (/= (the fixnum
389                        (if (typep char 'base-char)
390                            (aref attribute-array (char-code char))
391                            (gethash char attribute-hash-table
392                                     +char-attr-constituent+)))
393                      +char-attr-whitespace+))
394              (if (eq char :eof)
395                  (error 'end-of-file :stream stream)
396                  char))))))
397 \f
398 ;;;; temporary initialization hack
399
400 ;; Install the (easy) standard macro-chars into *READTABLE*.
401 (defun !cold-init-standard-readtable ()
402   (/show0 "entering !cold-init-standard-readtable")
403   ;; All characters get boring defaults in MAKE-READTABLE. Now we
404   ;; override the boring defaults on characters which need more
405   ;; interesting behavior.
406   (flet ((whitespaceify (char)
407            (set-cmt-entry char nil)
408            (set-cat-entry char +char-attr-whitespace+)))
409     (whitespaceify (code-char tab-char-code))
410     (whitespaceify #\Newline)
411     (whitespaceify #\Space)
412     (whitespaceify (code-char form-feed-char-code))
413     (whitespaceify (code-char return-char-code)))
414
415   (set-cat-entry #\\ +char-attr-single-escape+)
416   (set-cmt-entry #\\ nil)
417
418   (set-cat-entry #\| +char-attr-multiple-escape+)
419   (set-cmt-entry #\| nil)
420
421   ;; Easy macro-character definitions are in this source file.
422   (set-macro-character #\" #'read-string)
423   (set-macro-character #\' #'read-quote)
424   (set-macro-character #\( #'read-list)
425   (set-macro-character #\) #'read-right-paren)
426   (set-macro-character #\; #'read-comment)
427   ;; (The hairier macro-character definitions, for #\# and #\`, are
428   ;; defined elsewhere, in their own source files.)
429
430   ;; all constituents
431   (do ((ichar 0 (1+ ichar))
432        (char))
433       ((= ichar base-char-code-limit))
434     (setq char (code-char ichar))
435     (when (constituentp char)
436       (set-cmt-entry char nil)))
437
438   (/show0 "leaving !cold-init-standard-readtable"))
439 \f
440 ;;;; implementation of the read buffer
441
442 (defvar *read-buffer*)
443 (defvar *read-buffer-length*)
444 ;;; FIXME: Is it really helpful to have *READ-BUFFER-LENGTH* be a
445 ;;; separate variable instead of just calculating it on the fly as
446 ;;; (LENGTH *READ-BUFFER*)?
447
448 (defvar *inch-ptr*) ; *OUCH-PTR* always points to next char to write.
449 (defvar *ouch-ptr*) ; *INCH-PTR* always points to next char to read.
450
451 (declaim (type index *read-buffer-length* *inch-ptr* *ouch-ptr*))
452 (declaim (type (simple-array character (*)) *read-buffer*))
453
454 (declaim (inline reset-read-buffer))
455 (defun reset-read-buffer ()
456   ;; Turn *READ-BUFFER* into an empty read buffer.
457   (setq *ouch-ptr* 0)
458   (setq *inch-ptr* 0))
459
460 (declaim (inline ouch-read-buffer))
461 (defun ouch-read-buffer (char)
462   ;; When buffer overflow
463   (when (>= *ouch-ptr* *read-buffer-length*)
464     ;; Size should be doubled.
465     (grow-read-buffer))
466   (setf (elt *read-buffer* *ouch-ptr*) char)
467   (setq *ouch-ptr* (1+ *ouch-ptr*)))
468
469 (defun grow-read-buffer ()
470   (let* ((rbl (length *read-buffer*))
471          (new-length (* 2 rbl))
472          (new-buffer (make-string new-length)))
473     (setq *read-buffer* (replace new-buffer *read-buffer*))
474     (setq *read-buffer-length* new-length)))
475
476 (defun inch-read-buffer ()
477   (if (>= *inch-ptr* *ouch-ptr*)
478       *eof-object*
479       (prog1
480           (elt *read-buffer* *inch-ptr*)
481         (incf *inch-ptr*))))
482
483 (declaim (inline unread-buffer))
484 (defun unread-buffer ()
485   (decf *inch-ptr*))
486
487 (declaim (inline read-unwind-read-buffer))
488 (defun read-unwind-read-buffer ()
489   ;; Keep contents, but make next (INCH..) return first character.
490   (setq *inch-ptr* 0))
491
492 (defun read-buffer-to-string ()
493   (subseq *read-buffer* 0 *ouch-ptr*))
494
495 (defmacro with-read-buffer (() &body body)
496   `(let* ((*read-buffer* (make-string 128))
497           (*read-buffer-length* 128)
498           (*ouch-ptr* 0)
499           (*inch-ptr* 0))
500      ,@body))
501
502 (declaim (inline read-buffer-boundp))
503 (defun read-buffer-boundp ()
504   (and (boundp '*read-buffer*)
505        (boundp '*read-buffer-length*)
506        (boundp '*ouch-ptr*)
507        (boundp '*inch-ptr*)))
508
509 (defun check-for-recursive-read (stream recursive-p operator-name)
510   (when (and recursive-p (not (read-buffer-boundp)))
511     (simple-reader-error
512      stream
513      "~A was invoked with RECURSIVE-P being true outside ~
514       of a recursive read operation."
515      `(,operator-name))))
516 \f
517 ;;;; READ-PRESERVING-WHITESPACE, READ-DELIMITED-LIST, and READ
518
519 ;;; an alist for #=, used to keep track of objects with labels assigned that
520 ;;; have been completely read. Each entry is (integer-tag gensym-tag value).
521 ;;;
522 ;;; KLUDGE: Should this really be an alist? It seems as though users
523 ;;; could reasonably expect N log N performance for large datasets.
524 ;;; On the other hand, it's probably very very seldom a problem in practice.
525 ;;; On the third hand, it might be just as easy to use a hash table
526 ;;; as an alist, so maybe we should. -- WHN 19991202
527 (defvar *sharp-equal-alist* ())
528
529 (declaim (special *standard-input*))
530
531 ;;; Like READ-PRESERVING-WHITESPACE, but doesn't check the read buffer
532 ;;; for being set up properly.
533 (defun %read-preserving-whitespace (stream eof-error-p eof-value recursive-p)
534   (if recursive-p
535       ;; a loop for repeating when a macro returns nothing
536       (loop
537        (let ((char (read-char stream eof-error-p *eof-object*)))
538          (cond ((eofp char) (return eof-value))
539                ((whitespace[2]p char))
540                (t
541                 (let* ((macrofun (get-coerced-cmt-entry char *readtable*))
542                        (result (multiple-value-list
543                                 (funcall macrofun stream char))))
544                   ;; Repeat if macro returned nothing.
545                   (when result
546                     (return (unless *read-suppress* (car result)))))))))
547       (let ((*sharp-equal-alist* nil))
548         (with-read-buffer ()
549           (%read-preserving-whitespace stream eof-error-p eof-value t)))))
550
551 ;;; READ-PRESERVING-WHITESPACE behaves just like READ, only it makes
552 ;;; sure to leave terminating whitespace in the stream. (This is a
553 ;;; COMMON-LISP exported symbol.)
554 (defun read-preserving-whitespace (&optional (stream *standard-input*)
555                                              (eof-error-p t)
556                                              (eof-value nil)
557                                              (recursive-p nil))
558   #!+sb-doc
559   "Read from STREAM and return the value read, preserving any whitespace
560    that followed the object."
561   (check-for-recursive-read stream recursive-p 'read-preserving-whitespace)
562   (%read-preserving-whitespace stream eof-error-p eof-value recursive-p))
563
564 ;;; Return NIL or a list with one thing, depending.
565 ;;;
566 ;;; for functions that want comments to return so that they can look
567 ;;; past them. We assume CHAR is not whitespace.
568 (defun read-maybe-nothing (stream char)
569   (let ((retval (multiple-value-list
570                  (funcall (get-coerced-cmt-entry char *readtable*)
571                           stream
572                           char))))
573     (if retval (rplacd retval nil))))
574
575 (defun read (&optional (stream *standard-input*)
576                        (eof-error-p t)
577                        (eof-value nil)
578                        (recursive-p nil))
579   #!+sb-doc
580   "Read the next Lisp value from STREAM, and return it."
581   (check-for-recursive-read stream recursive-p 'read)
582   (let ((result (%read-preserving-whitespace stream eof-error-p eof-value
583                                              recursive-p)))
584     ;; This function generally discards trailing whitespace. If you
585     ;; don't want to discard trailing whitespace, call
586     ;; CL:READ-PRESERVING-WHITESPACE instead.
587     (unless (or (eql result eof-value) recursive-p)
588       (let ((next-char (read-char stream nil nil)))
589         (unless (or (null next-char)
590                     (whitespace[2]p next-char))
591           (unread-char next-char stream))))
592     result))
593
594 ;;; (This is a COMMON-LISP exported symbol.)
595 (defun read-delimited-list (endchar &optional
596                                     (input-stream *standard-input*)
597                                     recursive-p)
598   #!+sb-doc
599   "Read Lisp values from INPUT-STREAM until the next character after a
600    value's representation is ENDCHAR, and return the objects as a list."
601   (check-for-recursive-read input-stream recursive-p 'read-delimited-list)
602   (flet ((%read-delimited-list (endchar input-stream)
603            (do ((char (flush-whitespace input-stream)
604                       (flush-whitespace input-stream))
605                 (retlist ()))
606                ((char= char endchar)
607                 (unless *read-suppress* (nreverse retlist)))
608              (setq retlist (nconc (read-maybe-nothing input-stream char)
609                                   retlist)))))
610     (declare (inline %read-delimited-list))
611     (if recursive-p
612         (%read-delimited-list endchar input-stream)
613         (with-read-buffer ()
614           (%read-delimited-list endchar input-stream)))))
615 \f
616 ;;;; basic readmacro definitions
617 ;;;;
618 ;;;; Some large, hairy subsets of readmacro definitions (backquotes
619 ;;;; and sharp macros) are not here, but in their own source files.
620
621 (defun read-quote (stream ignore)
622   (declare (ignore ignore))
623   (list 'quote (read stream t nil t)))
624
625 (defun read-comment (stream ignore)
626   (declare (ignore ignore))
627   (handler-bind
628       ((character-decoding-error
629         #'(lambda (decoding-error)
630             (declare (ignorable decoding-error))
631             (style-warn
632              'sb!kernel::character-decoding-error-in-macro-char-comment
633              :position (file-position stream) :stream stream)
634             (invoke-restart 'attempt-resync))))
635     (let ((stream (in-synonym-of stream)))
636       (if (ansi-stream-p stream)
637           (prepare-for-fast-read-char stream
638            (do ((char (fast-read-char nil nil)
639                       (fast-read-char nil nil)))
640                ((or (not char) (char= char #\newline))
641                 (done-with-fast-read-char))))
642           ;; CLOS stream
643           (do ((char (read-char stream nil :eof) (read-char stream nil :eof)))
644               ((or (eq char :eof) (char= char #\newline)))))))
645   ;; Don't return anything.
646   (values))
647
648 (defun read-list (stream ignore)
649   (declare (ignore ignore))
650   (let* ((thelist (list nil))
651          (listtail thelist))
652     (do ((firstchar (flush-whitespace stream) (flush-whitespace stream)))
653         ((char= firstchar #\) ) (cdr thelist))
654       (when (char= firstchar #\.)
655             (let ((nextchar (read-char stream t)))
656               (cond ((token-delimiterp nextchar)
657                      (cond ((eq listtail thelist)
658                             (unless *read-suppress*
659                               (simple-reader-error
660                                stream
661                                "Nothing appears before . in list.")))
662                            ((whitespace[2]p nextchar)
663                             (setq nextchar (flush-whitespace stream))))
664                      (rplacd listtail
665                              ;; Return list containing last thing.
666                              (car (read-after-dot stream nextchar)))
667                      (return (cdr thelist)))
668                     ;; Put back NEXTCHAR so that we can read it normally.
669                     (t (unread-char nextchar stream)))))
670       ;; Next thing is not an isolated dot.
671       (let ((listobj (read-maybe-nothing stream firstchar)))
672         ;; allows the possibility that a comment was read
673         (when listobj
674               (rplacd listtail listobj)
675               (setq listtail listobj))))))
676
677 (defun read-after-dot (stream firstchar)
678   ;; FIRSTCHAR is non-whitespace!
679   (let ((lastobj ()))
680     (do ((char firstchar (flush-whitespace stream)))
681         ((char= char #\) )
682          (if *read-suppress*
683              (return-from read-after-dot nil)
684              (simple-reader-error stream "Nothing appears after . in list.")))
685       ;; See whether there's something there.
686       (setq lastobj (read-maybe-nothing stream char))
687       (when lastobj (return t)))
688     ;; At least one thing appears after the dot.
689     ;; Check for more than one thing following dot.
690     (do ((lastchar (flush-whitespace stream)
691                    (flush-whitespace stream)))
692         ((char= lastchar #\) ) lastobj) ;success!
693       ;; Try reading virtual whitespace.
694       (if (and (read-maybe-nothing stream lastchar)
695                (not *read-suppress*))
696           (simple-reader-error stream
697                                "More than one object follows . in list.")))))
698
699 (defun read-string (stream closech)
700   ;; This accumulates chars until it sees same char that invoked it.
701   ;; For a very long string, this could end up bloating the read buffer.
702   (reset-read-buffer)
703   (let ((stream (in-synonym-of stream)))
704     (if (ansi-stream-p stream)
705         (prepare-for-fast-read-char stream
706           (do ((char (fast-read-char t) (fast-read-char t)))
707               ((char= char closech)
708                (done-with-fast-read-char))
709             (if (single-escape-p char) (setq char (fast-read-char t)))
710             (ouch-read-buffer char)))
711         ;; CLOS stream
712         (do ((char (read-char stream nil :eof) (read-char stream nil :eof)))
713             ((or (eq char :eof) (char= char closech))
714              (if (eq char :eof)
715                  (error 'end-of-file :stream stream)))
716           (when (single-escape-p char)
717             (setq char (read-char stream nil :eof))
718             (if (eq char :eof)
719                 (error 'end-of-file :stream stream)))
720           (ouch-read-buffer char))))
721   (read-buffer-to-string))
722
723 (defun read-right-paren (stream ignore)
724   (declare (ignore ignore))
725   (simple-reader-error stream "unmatched close parenthesis"))
726
727 ;;; Read from the stream up to the next delimiter. Leave the resulting
728 ;;; token in *READ-BUFFER*, and return two values:
729 ;;; -- a list of the escaped character positions, and
730 ;;; -- The position of the first package delimiter (or NIL).
731 (defun internal-read-extended-token (stream firstchar escape-firstchar)
732   (reset-read-buffer)
733   (let ((escapes '()))
734     (when escape-firstchar
735       (push *ouch-ptr* escapes)
736       (ouch-read-buffer firstchar)
737       (setq firstchar (read-char stream nil *eof-object*)))
738   (do ((char firstchar (read-char stream nil *eof-object*))
739        (colon nil))
740       ((cond ((eofp char) t)
741              ((token-delimiterp char)
742               (unread-char char stream)
743               t)
744              (t nil))
745        (values escapes colon))
746     (cond ((single-escape-p char)
747            ;; It can't be a number, even if it's 1\23.
748            ;; Read next char here, so it won't be casified.
749            (push *ouch-ptr* escapes)
750            (let ((nextchar (read-char stream nil *eof-object*)))
751              (if (eofp nextchar)
752                  (reader-eof-error stream "after escape character")
753                  (ouch-read-buffer nextchar))))
754           ((multiple-escape-p char)
755            ;; Read to next multiple-escape, escaping single chars
756            ;; along the way.
757            (loop
758              (let ((ch (read-char stream nil *eof-object*)))
759                (cond
760                 ((eofp ch)
761                  (reader-eof-error stream "inside extended token"))
762                 ((multiple-escape-p ch) (return))
763                 ((single-escape-p ch)
764                  (let ((nextchar (read-char stream nil *eof-object*)))
765                    (cond ((eofp nextchar)
766                           (reader-eof-error stream "after escape character"))
767                          (t
768                           (push *ouch-ptr* escapes)
769                           (ouch-read-buffer nextchar)))))
770                 (t
771                  (push *ouch-ptr* escapes)
772                  (ouch-read-buffer ch))))))
773           (t
774            (when (and (constituentp char)
775                       (eql (get-constituent-trait char)
776                            +char-attr-package-delimiter+)
777                       (not colon))
778              (setq colon *ouch-ptr*))
779            (ouch-read-buffer char))))))
780 \f
781 ;;;; character classes
782
783 ;;; Return the character class for CHAR.
784 ;;;
785 ;;; FIXME: why aren't these ATT-getting forms using GET-CAT-ENTRY?
786 ;;; Because we've cached the readtable tables?
787 (defmacro char-class (char attarray atthash)
788   `(let ((att (if (typep ,char 'base-char)
789                   (aref ,attarray (char-code ,char))
790                   (gethash ,char ,atthash +char-attr-constituent+))))
791      (declare (fixnum att))
792      (cond
793        ((<= att +char-attr-terminating-macro+) +char-attr-delimiter+)
794        ((< att +char-attr-constituent+) att)
795        (t (setf att (get-constituent-trait ,char))
796           (if (= att +char-attr-invalid+)
797               (simple-reader-error stream "invalid constituent")
798               att)))))
799
800 ;;; Return the character class for CHAR, which might be part of a
801 ;;; rational number.
802 (defmacro char-class2 (char attarray atthash)
803   `(let ((att (if (typep ,char 'base-char)
804                   (aref ,attarray (char-code ,char))
805                   (gethash ,char ,atthash +char-attr-constituent+))))
806      (declare (fixnum att))
807      (cond
808        ((<= att +char-attr-terminating-macro+) +char-attr-delimiter+)
809        ((< att +char-attr-constituent+) att)
810        (t (setf att (get-constituent-trait ,char))
811           (cond
812             ((digit-char-p ,char *read-base*) +char-attr-constituent-digit+)
813             ((= att +char-attr-constituent-digit+) +char-attr-constituent+)
814             ((= att +char-attr-invalid+)
815              (simple-reader-error stream "invalid constituent"))
816             (t att))))))
817
818 ;;; Return the character class for a char which might be part of a
819 ;;; rational or floating number. (Assume that it is a digit if it
820 ;;; could be.)
821 (defmacro char-class3 (char attarray atthash)
822   `(let ((att (if (typep ,char 'base-char)
823                   (aref ,attarray (char-code ,char))
824                   (gethash ,char ,atthash +char-attr-constituent+))))
825      (declare (fixnum att))
826      (cond
827        ((<= att +char-attr-terminating-macro+) +char-attr-delimiter+)
828        ((< att +char-attr-constituent+) att)
829        (t (setf att (get-constituent-trait ,char))
830           (when possibly-rational
831             (setq possibly-rational
832                   (or (digit-char-p ,char *read-base*)
833                       (= att +char-attr-constituent-slash+))))
834           (when possibly-float
835             (setq possibly-float
836                   (or (digit-char-p ,char 10)
837                       (= att +char-attr-constituent-dot+))))
838           (cond
839             ((digit-char-p ,char (max *read-base* 10))
840              (if (digit-char-p ,char *read-base*)
841                  (if (= att +char-attr-constituent-expt+)
842                      +char-attr-constituent-digit-or-expt+
843                      +char-attr-constituent-digit+)
844                  +char-attr-constituent-decimal-digit+))
845             ((= att +char-attr-invalid+)
846              (simple-reader-error stream "invalid constituent"))
847             (t att))))))
848 \f
849 ;;;; token fetching
850
851 (defvar *read-suppress* nil
852   #!+sb-doc
853   "Suppress most interpreting in the reader when T.")
854
855 (defvar *read-base* 10
856   #!+sb-doc
857   "the radix that Lisp reads numbers in")
858 (declaim (type (integer 2 36) *read-base*))
859
860 ;;; Modify the read buffer according to READTABLE-CASE, ignoring
861 ;;; ESCAPES. ESCAPES is a list of the escaped indices, in reverse
862 ;;; order.
863 (defun casify-read-buffer (escapes)
864   (let ((case (readtable-case *readtable*)))
865     (cond
866      ((and (null escapes) (eq case :upcase))
867       ;; Pull the special variable access out of the loop.
868       (let ((buffer *read-buffer*))
869         (dotimes (i *ouch-ptr*)
870           (declare (optimize (sb!c::insert-array-bounds-checks 0)))
871           (setf (schar buffer i) (char-upcase (schar buffer i))))))
872      ((eq case :preserve))
873      (t
874       (macrolet ((skip-esc (&body body)
875                    `(do ((i (1- *ouch-ptr*) (1- i))
876                          (buffer *read-buffer*)
877                          (escapes escapes))
878                         ((minusp i))
879                       (declare (fixnum i)
880                                (optimize (sb!c::insert-array-bounds-checks 0)))
881                       (when (or (null escapes)
882                                 (let ((esc (first escapes)))
883                                   (declare (fixnum esc))
884                                   (cond ((< esc i) t)
885                                         (t
886                                          (aver (= esc i))
887                                          (pop escapes)
888                                          nil))))
889                         (let ((ch (schar buffer i)))
890                           ,@body)))))
891         (flet ((lower-em ()
892                  (skip-esc (setf (schar buffer i) (char-downcase ch))))
893                (raise-em ()
894                  (skip-esc (setf (schar buffer i) (char-upcase ch)))))
895           (ecase case
896             (:upcase (raise-em))
897             (:downcase (lower-em))
898             (:invert
899              (let ((all-upper t)
900                    (all-lower t))
901                (skip-esc
902                  (when (both-case-p ch)
903                    (if (upper-case-p ch)
904                        (setq all-lower nil)
905                        (setq all-upper nil))))
906                (cond (all-lower (raise-em))
907                      (all-upper (lower-em))))))))))))
908
909 (defun read-token (stream firstchar)
910   #!+sb-doc
911   "This function is just an fsm that recognizes numbers and symbols."
912   ;; Check explicitly whether FIRSTCHAR has an entry for
913   ;; NON-TERMINATING in CHARACTER-ATTRIBUTE-TABLE and
914   ;; READ-DOT-NUMBER-SYMBOL in CMT. Report an error if these are
915   ;; violated. (If we called this, we want something that is a
916   ;; legitimate token!) Read in the longest possible string satisfying
917   ;; the Backus-Naur form for "unqualified-token". Leave the result in
918   ;; the *READ-BUFFER*. Return next char after token (last char read).
919   (when *read-suppress*
920     (internal-read-extended-token stream firstchar nil)
921     (return-from read-token nil))
922   (let ((attribute-array (character-attribute-array *readtable*))
923         (attribute-hash-table (character-attribute-hash-table *readtable*))
924         (package-designator nil)
925         (colons 0)
926         (possibly-rational t)
927         (seen-digit-or-expt nil)
928         (possibly-float t)
929         (was-possibly-float nil)
930         (escapes ())
931         (seen-multiple-escapes nil))
932     (reset-read-buffer)
933     (prog ((char firstchar))
934       (case (char-class3 char attribute-array attribute-hash-table)
935         (#.+char-attr-constituent-sign+ (go SIGN))
936         (#.+char-attr-constituent-digit+ (go LEFTDIGIT))
937         (#.+char-attr-constituent-digit-or-expt+
938          (setq seen-digit-or-expt t)
939          (go LEFTDIGIT))
940         (#.+char-attr-constituent-decimal-digit+ (go LEFTDECIMALDIGIT))
941         (#.+char-attr-constituent-dot+ (go FRONTDOT))
942         (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
943         (#.+char-attr-package-delimiter+ (go COLON))
944         (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
945         (#.+char-attr-invalid+ (simple-reader-error stream
946                                                     "invalid constituent"))
947         ;; can't have eof, whitespace, or terminating macro as first char!
948         (t (go SYMBOL)))
949      SIGN ; saw "sign"
950       (ouch-read-buffer char)
951       (setq char (read-char stream nil nil))
952       (unless char (go RETURN-SYMBOL))
953       (setq possibly-rational t
954             possibly-float t)
955       (case (char-class3 char attribute-array attribute-hash-table)
956         (#.+char-attr-constituent-digit+ (go LEFTDIGIT))
957         (#.+char-attr-constituent-digit-or-expt+
958          (setq seen-digit-or-expt t)
959          (go LEFTDIGIT))
960         (#.+char-attr-constituent-decimal-digit+ (go LEFTDECIMALDIGIT))
961         (#.+char-attr-constituent-dot+ (go SIGNDOT))
962         (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
963         (#.+char-attr-package-delimiter+ (go COLON))
964         (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
965         (#.+char-attr-delimiter+ (unread-char char stream) (go RETURN-SYMBOL))
966         (t (go SYMBOL)))
967      LEFTDIGIT ; saw "[sign] {digit}+"
968       (ouch-read-buffer char)
969       (setq char (read-char stream nil nil))
970       (unless char (return (make-integer)))
971       (setq was-possibly-float possibly-float)
972       (case (char-class3 char attribute-array attribute-hash-table)
973         (#.+char-attr-constituent-digit+ (go LEFTDIGIT))
974         (#.+char-attr-constituent-decimal-digit+ (if possibly-float
975                                                      (go LEFTDECIMALDIGIT)
976                                                      (go SYMBOL)))
977         (#.+char-attr-constituent-dot+ (if possibly-float
978                                            (go MIDDLEDOT)
979                                            (go SYMBOL)))
980         (#.+char-attr-constituent-digit-or-expt+
981          (if (or seen-digit-or-expt (not was-possibly-float))
982              (progn (setq seen-digit-or-expt t) (go LEFTDIGIT))
983              (progn (setq seen-digit-or-expt t) (go LEFTDIGIT-OR-EXPT))))
984         (#.+char-attr-constituent-expt+
985          (if was-possibly-float
986              (go EXPONENT)
987              (go SYMBOL)))
988         (#.+char-attr-constituent-slash+ (if possibly-rational
989                                              (go RATIO)
990                                              (go SYMBOL)))
991         (#.+char-attr-delimiter+ (unread-char char stream)
992                                  (return (make-integer)))
993         (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
994         (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
995         (#.+char-attr-package-delimiter+ (go COLON))
996         (t (go SYMBOL)))
997      LEFTDIGIT-OR-EXPT
998       (ouch-read-buffer char)
999       (setq char (read-char stream nil nil))
1000       (unless char (return (make-integer)))
1001       (case (char-class3 char attribute-array attribute-hash-table)
1002         (#.+char-attr-constituent-digit+ (go LEFTDIGIT))
1003         (#.+char-attr-constituent-decimal-digit+ (bug "impossible!"))
1004         (#.+char-attr-constituent-dot+ (go SYMBOL))
1005         (#.+char-attr-constituent-digit-or-expt+ (go LEFTDIGIT))
1006         (#.+char-attr-constituent-expt+ (go SYMBOL))
1007         (#.+char-attr-constituent-sign+ (go EXPTSIGN))
1008         (#.+char-attr-constituent-slash+ (if possibly-rational
1009                                              (go RATIO)
1010                                              (go SYMBOL)))
1011         (#.+char-attr-delimiter+ (unread-char char stream)
1012                                  (return (make-integer)))
1013         (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1014         (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1015         (#.+char-attr-package-delimiter+ (go COLON))
1016         (t (go SYMBOL)))
1017      LEFTDECIMALDIGIT ; saw "[sign] {decimal-digit}+"
1018       (aver possibly-float)
1019       (ouch-read-buffer char)
1020       (setq char (read-char stream nil nil))
1021       (unless char (go RETURN-SYMBOL))
1022       (case (char-class char attribute-array attribute-hash-table)
1023         (#.+char-attr-constituent-digit+ (go LEFTDECIMALDIGIT))
1024         (#.+char-attr-constituent-dot+ (go MIDDLEDOT))
1025         (#.+char-attr-constituent-expt+ (go EXPONENT))
1026         (#.+char-attr-constituent-slash+ (aver (not possibly-rational))
1027                                          (go SYMBOL))
1028         (#.+char-attr-delimiter+ (unread-char char stream)
1029                                  (go RETURN-SYMBOL))
1030         (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1031         (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1032         (#.+char-attr-package-delimiter+ (go COLON))
1033         (t (go SYMBOL)))
1034      MIDDLEDOT ; saw "[sign] {digit}+ dot"
1035       (ouch-read-buffer char)
1036       (setq char (read-char stream nil nil))
1037       (unless char (return (let ((*read-base* 10))
1038                              (make-integer))))
1039       (case (char-class char attribute-array attribute-hash-table)
1040         (#.+char-attr-constituent-digit+ (go RIGHTDIGIT))
1041         (#.+char-attr-constituent-expt+ (go EXPONENT))
1042         (#.+char-attr-delimiter+
1043          (unread-char char stream)
1044          (return (let ((*read-base* 10))
1045                    (make-integer))))
1046         (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1047         (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1048         (#.+char-attr-package-delimiter+ (go COLON))
1049         (t (go SYMBOL)))
1050      RIGHTDIGIT ; saw "[sign] {decimal-digit}* dot {digit}+"
1051       (ouch-read-buffer char)
1052       (setq char (read-char stream nil nil))
1053       (unless char (return (make-float stream)))
1054       (case (char-class char attribute-array attribute-hash-table)
1055         (#.+char-attr-constituent-digit+ (go RIGHTDIGIT))
1056         (#.+char-attr-constituent-expt+ (go EXPONENT))
1057         (#.+char-attr-delimiter+
1058          (unread-char char stream)
1059          (return (make-float stream)))
1060         (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1061         (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1062         (#.+char-attr-package-delimiter+ (go COLON))
1063         (t (go SYMBOL)))
1064      SIGNDOT ; saw "[sign] dot"
1065       (ouch-read-buffer char)
1066       (setq char (read-char stream nil nil))
1067       (unless char (go RETURN-SYMBOL))
1068       (case (char-class char attribute-array attribute-hash-table)
1069         (#.+char-attr-constituent-digit+ (go RIGHTDIGIT))
1070         (#.+char-attr-delimiter+ (unread-char char stream) (go RETURN-SYMBOL))
1071         (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1072         (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1073         (t (go SYMBOL)))
1074      FRONTDOT ; saw "dot"
1075       (ouch-read-buffer char)
1076       (setq char (read-char stream nil nil))
1077       (unless char (simple-reader-error stream "dot context error"))
1078       (case (char-class char attribute-array attribute-hash-table)
1079         (#.+char-attr-constituent-digit+ (go RIGHTDIGIT))
1080         (#.+char-attr-constituent-dot+ (go DOTS))
1081         (#.+char-attr-delimiter+  (simple-reader-error stream
1082                                                        "dot context error"))
1083         (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1084         (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1085         (#.+char-attr-package-delimiter+ (go COLON))
1086         (t (go SYMBOL)))
1087      EXPONENT
1088       (ouch-read-buffer char)
1089       (setq char (read-char stream nil nil))
1090       (unless char (go RETURN-SYMBOL))
1091       (setq possibly-float t)
1092       (case (char-class char attribute-array attribute-hash-table)
1093         (#.+char-attr-constituent-sign+ (go EXPTSIGN))
1094         (#.+char-attr-constituent-digit+ (go EXPTDIGIT))
1095         (#.+char-attr-delimiter+ (unread-char char stream) (go RETURN-SYMBOL))
1096         (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1097         (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1098         (#.+char-attr-package-delimiter+ (go COLON))
1099         (t (go SYMBOL)))
1100      EXPTSIGN ; got to EXPONENT, and saw a sign character
1101       (ouch-read-buffer char)
1102       (setq char (read-char stream nil nil))
1103       (unless char (go RETURN-SYMBOL))
1104       (case (char-class char attribute-array attribute-hash-table)
1105         (#.+char-attr-constituent-digit+ (go EXPTDIGIT))
1106         (#.+char-attr-delimiter+ (unread-char char stream) (go RETURN-SYMBOL))
1107         (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1108         (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1109         (#.+char-attr-package-delimiter+ (go COLON))
1110         (t (go SYMBOL)))
1111      EXPTDIGIT ; got to EXPONENT, saw "[sign] {digit}+"
1112       (ouch-read-buffer char)
1113       (setq char (read-char stream nil nil))
1114       (unless char (return (make-float stream)))
1115       (case (char-class char attribute-array attribute-hash-table)
1116         (#.+char-attr-constituent-digit+ (go EXPTDIGIT))
1117         (#.+char-attr-delimiter+
1118          (unread-char char stream)
1119          (return (make-float stream)))
1120         (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1121         (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1122         (#.+char-attr-package-delimiter+ (go COLON))
1123         (t (go SYMBOL)))
1124      RATIO ; saw "[sign] {digit}+ slash"
1125       (ouch-read-buffer char)
1126       (setq char (read-char stream nil nil))
1127       (unless char (go RETURN-SYMBOL))
1128       (case (char-class2 char attribute-array attribute-hash-table)
1129         (#.+char-attr-constituent-digit+ (go RATIODIGIT))
1130         (#.+char-attr-delimiter+ (unread-char char stream) (go RETURN-SYMBOL))
1131         (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1132         (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1133         (#.+char-attr-package-delimiter+ (go COLON))
1134         (t (go SYMBOL)))
1135      RATIODIGIT ; saw "[sign] {digit}+ slash {digit}+"
1136       (ouch-read-buffer char)
1137       (setq char (read-char stream nil nil))
1138       (unless char (return (make-ratio stream)))
1139       (case (char-class2 char attribute-array attribute-hash-table)
1140         (#.+char-attr-constituent-digit+ (go RATIODIGIT))
1141         (#.+char-attr-delimiter+
1142          (unread-char char stream)
1143          (return (make-ratio stream)))
1144         (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1145         (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1146         (#.+char-attr-package-delimiter+ (go COLON))
1147         (t (go SYMBOL)))
1148      DOTS ; saw "dot {dot}+"
1149       (ouch-read-buffer char)
1150       (setq char (read-char stream nil nil))
1151       (unless char (simple-reader-error stream "too many dots"))
1152       (case (char-class char attribute-array attribute-hash-table)
1153         (#.+char-attr-constituent-dot+ (go DOTS))
1154         (#.+char-attr-delimiter+
1155          (unread-char char stream)
1156          (simple-reader-error stream "too many dots"))
1157         (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1158         (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1159         (#.+char-attr-package-delimiter+ (go COLON))
1160         (t (go SYMBOL)))
1161      SYMBOL ; not a dot, dots, or number
1162       (let ((stream (in-synonym-of stream)))
1163         (if (ansi-stream-p stream)
1164             (prepare-for-fast-read-char stream
1165               (prog ()
1166                SYMBOL-LOOP
1167                (ouch-read-buffer char)
1168                (setq char (fast-read-char nil nil))
1169                (unless char (go RETURN-SYMBOL))
1170                (case (char-class char attribute-array attribute-hash-table)
1171                  (#.+char-attr-single-escape+ (done-with-fast-read-char)
1172                                               (go SINGLE-ESCAPE))
1173                  (#.+char-attr-delimiter+ (done-with-fast-read-char)
1174                                           (unread-char char stream)
1175                                           (go RETURN-SYMBOL))
1176                  (#.+char-attr-multiple-escape+ (done-with-fast-read-char)
1177                                                 (go MULT-ESCAPE))
1178                  (#.+char-attr-package-delimiter+ (done-with-fast-read-char)
1179                                                   (go COLON))
1180                  (t (go SYMBOL-LOOP)))))
1181             ;; CLOS stream
1182             (prog ()
1183              SYMBOL-LOOP
1184              (ouch-read-buffer char)
1185              (setq char (read-char stream nil :eof))
1186              (when (eq char :eof) (go RETURN-SYMBOL))
1187              (case (char-class char attribute-array attribute-hash-table)
1188                (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1189                (#.+char-attr-delimiter+ (unread-char char stream)
1190                             (go RETURN-SYMBOL))
1191                (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1192                (#.+char-attr-package-delimiter+ (go COLON))
1193                (t (go SYMBOL-LOOP))))))
1194      SINGLE-ESCAPE ; saw a single-escape
1195       ;; Don't put the escape character in the read buffer.
1196       ;; READ-NEXT CHAR, put in buffer (no case conversion).
1197       (let ((nextchar (read-char stream nil nil)))
1198         (unless nextchar
1199           (reader-eof-error stream "after single-escape character"))
1200         (push *ouch-ptr* escapes)
1201         (ouch-read-buffer nextchar))
1202       (setq char (read-char stream nil nil))
1203       (unless char (go RETURN-SYMBOL))
1204       (case (char-class char attribute-array attribute-hash-table)
1205         (#.+char-attr-delimiter+ (unread-char char stream) (go RETURN-SYMBOL))
1206         (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1207         (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1208         (#.+char-attr-package-delimiter+ (go COLON))
1209         (t (go SYMBOL)))
1210       MULT-ESCAPE
1211       (setq seen-multiple-escapes t)
1212       (do ((char (read-char stream t) (read-char stream t)))
1213           ((multiple-escape-p char))
1214         (if (single-escape-p char) (setq char (read-char stream t)))
1215         (push *ouch-ptr* escapes)
1216         (ouch-read-buffer char))
1217       (setq char (read-char stream nil nil))
1218       (unless char (go RETURN-SYMBOL))
1219       (case (char-class char attribute-array attribute-hash-table)
1220         (#.+char-attr-delimiter+ (unread-char char stream) (go RETURN-SYMBOL))
1221         (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1222         (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1223         (#.+char-attr-package-delimiter+ (go COLON))
1224         (t (go SYMBOL)))
1225       COLON
1226       (casify-read-buffer escapes)
1227       (unless (zerop colons)
1228         (simple-reader-error stream
1229                              "too many colons in ~S"
1230                              (read-buffer-to-string)))
1231       (setq colons 1)
1232       (setq package-designator
1233             (if (plusp *ouch-ptr*)
1234                 ;; FIXME: It seems inefficient to cons up a package
1235                 ;; designator string every time we read a symbol with an
1236                 ;; explicit package prefix. Perhaps we could implement
1237                 ;; a FIND-PACKAGE* function analogous to INTERN*
1238                 ;; and friends?
1239                 (read-buffer-to-string)
1240                 (if seen-multiple-escapes
1241                     (read-buffer-to-string)
1242                     *keyword-package*)))
1243       (reset-read-buffer)
1244       (setq escapes ())
1245       (setq char (read-char stream nil nil))
1246       (unless char (reader-eof-error stream "after reading a colon"))
1247       (case (char-class char attribute-array attribute-hash-table)
1248         (#.+char-attr-delimiter+
1249          (unread-char char stream)
1250          (simple-reader-error stream
1251                               "illegal terminating character after a colon: ~S"
1252                               char))
1253         (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1254         (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1255         (#.+char-attr-package-delimiter+ (go INTERN))
1256         (t (go SYMBOL)))
1257       INTERN
1258       (setq colons 2)
1259       (setq char (read-char stream nil nil))
1260       (unless char
1261         (reader-eof-error stream "after reading a colon"))
1262       (case (char-class char attribute-array attribute-hash-table)
1263         (#.+char-attr-delimiter+
1264          (unread-char char stream)
1265          (simple-reader-error stream
1266                               "illegal terminating character after a colon: ~S"
1267                               char))
1268         (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1269         (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1270         (#.+char-attr-package-delimiter+
1271          (simple-reader-error stream
1272                               "too many colons after ~S name"
1273                               package-designator))
1274         (t (go SYMBOL)))
1275       RETURN-SYMBOL
1276       (casify-read-buffer escapes)
1277       (let ((found (if package-designator
1278                        (find-package package-designator)
1279                        (sane-package))))
1280         (unless found
1281           (error 'simple-reader-package-error :stream stream
1282                  :format-arguments (list package-designator)
1283                  :format-control "package ~S not found"))
1284
1285         (if (or (zerop colons) (= colons 2) (eq found *keyword-package*))
1286             (return (intern* *read-buffer* *ouch-ptr* found))
1287             (multiple-value-bind (symbol test)
1288                 (find-symbol* *read-buffer* *ouch-ptr* found)
1289               (when (eq test :external) (return symbol))
1290               (let ((name (read-buffer-to-string)))
1291                 (with-simple-restart (continue "Use symbol anyway.")
1292                   (error 'simple-reader-package-error :stream stream
1293                          :format-arguments (list name (package-name found))
1294                          :format-control
1295                          (if test
1296                              "The symbol ~S is not external in the ~A package."
1297                              "Symbol ~S not found in the ~A package.")))
1298                 (return (intern name found)))))))))
1299
1300 ;;; for semi-external use:
1301 ;;;
1302 ;;; For semi-external use: Return 3 values: the string for the token,
1303 ;;; a flag for whether there was an escape char, and the position of
1304 ;;; any package delimiter.
1305 (defun read-extended-token (stream &optional (*readtable* *readtable*))
1306   (let ((first-char (read-char stream nil nil t)))
1307     (cond (first-char
1308            (multiple-value-bind (escapes colon)
1309                (internal-read-extended-token stream first-char nil)
1310              (casify-read-buffer escapes)
1311              (values (read-buffer-to-string) (not (null escapes)) colon)))
1312           (t
1313            (values "" nil nil)))))
1314
1315 ;;; for semi-external use:
1316 ;;;
1317 ;;; Read an extended token with the first character escaped. Return
1318 ;;; the string for the token.
1319 (defun read-extended-token-escaped (stream &optional (*readtable* *readtable*))
1320   (let ((first-char (read-char stream nil nil)))
1321     (cond (first-char
1322             (let ((escapes (internal-read-extended-token stream first-char t)))
1323               (casify-read-buffer escapes)
1324               (read-buffer-to-string)))
1325           (t
1326             (reader-eof-error stream "after escape")))))
1327 \f
1328 ;;;; number-reading functions
1329
1330 (defmacro digit* nil
1331   `(do ((ch char (inch-read-buffer)))
1332        ((or (eofp ch) (not (digit-char-p ch))) (setq char ch))
1333      ;; Report if at least one digit is seen.
1334      (setq one-digit t)))
1335
1336 (defmacro exponent-letterp (letter)
1337   `(memq ,letter '(#\E #\S #\F #\L #\D #\e #\s #\f #\l #\d)))
1338
1339 ;;; FIXME: It would be cleaner to have these generated automatically
1340 ;;; by compile-time code instead of having them hand-created like
1341 ;;; this. The !COLD-INIT-INTEGER-READER code below should be resurrected
1342 ;;; and tested.
1343 (defvar *integer-reader-safe-digits*
1344   #(nil nil
1345     26 17 13 11 10 9 8 8 8 7 7 7 7 6 6 6 6 6 6 6 6 5 5 5 5 5 5 5 5 5 5 5 5 5 5)
1346   #!+sb-doc
1347   "the mapping of base to 'safe' number of digits to read for a fixnum")
1348 (defvar *integer-reader-base-power*
1349   #(nil nil
1350     67108864 129140163 67108864 48828125 60466176 40353607
1351     16777216 43046721 100000000 19487171 35831808 62748517 105413504 11390625
1352     16777216 24137569 34012224 47045881 64000000 85766121 113379904 6436343
1353     7962624 9765625 11881376 14348907 17210368 20511149 24300000 28629151
1354     33554432 39135393 45435424 52521875 60466176)
1355   #!+sb-doc
1356   "the largest fixnum power of the base for MAKE-INTEGER")
1357 (declaim (simple-vector *integer-reader-safe-digits*
1358                         *integer-reader-base-power*))
1359 #|
1360 (defun !cold-init-integer-reader ()
1361   (do ((base 2 (1+ base)))
1362       ((> base 36))
1363     (let ((digits
1364           (do ((fix (truncate most-positive-fixnum base)
1365                     (truncate fix base))
1366                (digits 0 (1+ digits)))
1367               ((zerop fix) digits))))
1368       (setf (aref *integer-reader-safe-digits* base)
1369             digits
1370             (aref *integer-reader-base-power* base)
1371             (expt base digits)))))
1372 |#
1373
1374 (defun make-integer ()
1375   #!+sb-doc
1376   "Minimizes bignum-fixnum multiplies by reading a 'safe' number of digits,
1377   then multiplying by a power of the base and adding."
1378   (let* ((base *read-base*)
1379          (digits-per (aref *integer-reader-safe-digits* base))
1380          (base-power (aref *integer-reader-base-power* base))
1381          (negativep nil)
1382          (number 0))
1383     (declare (type index digits-per base-power))
1384     (read-unwind-read-buffer)
1385     (let ((char (inch-read-buffer)))
1386       (cond ((char= char #\-)
1387              (setq negativep t))
1388             ((char= char #\+))
1389             (t (unread-buffer))))
1390     (loop
1391      (let ((num 0))
1392        (declare (type index num))
1393        (dotimes (digit digits-per)
1394          (let* ((ch (inch-read-buffer)))
1395            (cond ((or (eofp ch) (char= ch #\.))
1396                   (return-from make-integer
1397                                (let ((res
1398                                       (if (zerop number) num
1399                                           (+ num (* number
1400                                                     (expt base digit))))))
1401                                  (if negativep (- res) res))))
1402                  (t (setq num (+ (digit-char-p ch base)
1403                                  (the index (* num base))))))))
1404        (setq number (+ num (* number base-power)))))))
1405
1406 (defun make-float (stream)
1407   ;; Assume that the contents of *read-buffer* are a legal float, with nothing
1408   ;; else after it.
1409   (read-unwind-read-buffer)
1410   (let ((negative-fraction nil)
1411         (number 0)
1412         (divisor 1)
1413         (negative-exponent nil)
1414         (exponent 0)
1415         (float-char ())
1416         (char (inch-read-buffer)))
1417     (if (cond ((char= char #\+) t)
1418               ((char= char #\-) (setq negative-fraction t)))
1419         ;; Flush it.
1420         (setq char (inch-read-buffer)))
1421     ;; Read digits before the dot.
1422     (do* ((ch char (inch-read-buffer))
1423           (dig (digit-char-p ch) (digit-char-p ch)))
1424          ((not dig) (setq char ch))
1425       (setq number (+ (* number 10) dig)))
1426     ;; Deal with the dot, if it's there.
1427     (when (char= char #\.)
1428       (setq char (inch-read-buffer))
1429       ;; Read digits after the dot.
1430       (do* ((ch char (inch-read-buffer))
1431             (dig (and (not (eofp ch)) (digit-char-p ch))
1432                  (and (not (eofp ch)) (digit-char-p ch))))
1433            ((not dig) (setq char ch))
1434         (setq divisor (* divisor 10))
1435         (setq number (+ (* number 10) dig))))
1436     ;; Is there an exponent letter?
1437     (cond ((eofp char)
1438            ;; If not, we've read the whole number.
1439            (let ((num (make-float-aux number divisor
1440                                       *read-default-float-format*
1441                                       stream)))
1442              (return-from make-float (if negative-fraction (- num) num))))
1443           ((exponent-letterp char)
1444            (setq float-char char)
1445            ;; Build exponent.
1446            (setq char (inch-read-buffer))
1447            ;; Check leading sign.
1448            (if (cond ((char= char #\+) t)
1449                      ((char= char #\-) (setq negative-exponent t)))
1450                ;; Flush sign.
1451                (setq char (inch-read-buffer)))
1452            ;; Read digits for exponent.
1453            (do* ((ch char (inch-read-buffer))
1454                  (dig (and (not (eofp ch)) (digit-char-p ch))
1455                       (and (not (eofp ch)) (digit-char-p ch))))
1456                 ((not dig)
1457                  (setq exponent (if negative-exponent (- exponent) exponent)))
1458              (setq exponent (+ (* exponent 10) dig)))
1459            ;; Generate and return the float, depending on FLOAT-CHAR:
1460            (let* ((float-format (case (char-upcase float-char)
1461                                   (#\E *read-default-float-format*)
1462                                   (#\S 'short-float)
1463                                   (#\F 'single-float)
1464                                   (#\D 'double-float)
1465                                   (#\L 'long-float)))
1466                   (result (make-float-aux (* (expt 10 exponent) number)
1467                                           divisor float-format stream)))
1468              (return-from make-float
1469                (if negative-fraction (- result) result))))
1470           (t (bug "bad fallthrough in floating point reader")))))
1471
1472 (defun make-float-aux (number divisor float-format stream)
1473   (handler-case
1474       (coerce (/ number divisor) float-format)
1475     (type-error (c)
1476       (error 'reader-impossible-number-error
1477              :error c :stream stream
1478              :format-control "failed to build float"))))
1479
1480 (defun make-ratio (stream)
1481   ;; Assume *READ-BUFFER* contains a legal ratio. Build the number from
1482   ;; the string.
1483   ;;
1484   ;; Look for optional "+" or "-".
1485   (let ((numerator 0) (denominator 0) (char ()) (negative-number nil))
1486     (read-unwind-read-buffer)
1487     (setq char (inch-read-buffer))
1488     (cond ((char= char #\+)
1489            (setq char (inch-read-buffer)))
1490           ((char= char #\-)
1491            (setq char (inch-read-buffer))
1492            (setq negative-number t)))
1493     ;; Get numerator.
1494     (do* ((ch char (inch-read-buffer))
1495           (dig (digit-char-p ch *read-base*)
1496                (digit-char-p ch *read-base*)))
1497          ((not dig))
1498          (setq numerator (+ (* numerator *read-base*) dig)))
1499     ;; Get denominator.
1500     (do* ((ch (inch-read-buffer) (inch-read-buffer))
1501           (dig ()))
1502          ((or (eofp ch) (not (setq dig (digit-char-p ch *read-base*)))))
1503          (setq denominator (+ (* denominator *read-base*) dig)))
1504     (let ((num (handler-case
1505                    (/ numerator denominator)
1506                  (arithmetic-error (c)
1507                    (error 'reader-impossible-number-error
1508                           :error c :stream stream
1509                           :format-control "failed to build ratio")))))
1510       (if negative-number (- num) num))))
1511 \f
1512 ;;;; General reader for dispatch macros
1513
1514 (defun dispatch-char-error (stream sub-char ignore)
1515   (declare (ignore ignore))
1516   (if *read-suppress*
1517       (values)
1518       (simple-reader-error stream
1519                            "no dispatch function defined for ~S"
1520                            sub-char)))
1521
1522 (defun read-dispatch-char (stream char)
1523   ;; Read some digits.
1524   (let ((numargp nil)
1525         (numarg 0)
1526         (sub-char ()))
1527     (do* ((ch (read-char stream nil *eof-object*)
1528               (read-char stream nil *eof-object*))
1529           (dig ()))
1530          ((or (eofp ch)
1531               (not (setq dig (digit-char-p ch))))
1532           ;; Take care of the extra char.
1533           (if (eofp ch)
1534               (reader-eof-error stream "inside dispatch character")
1535               (setq sub-char (char-upcase ch))))
1536       (setq numargp t)
1537       (setq numarg (+ (* numarg 10) dig)))
1538     ;; Look up the function and call it.
1539     (let ((dpair (find char (dispatch-tables *readtable*)
1540                        :test #'char= :key #'car)))
1541       (if dpair
1542           (funcall (the function
1543                      (gethash sub-char (cdr dpair) #'dispatch-char-error))
1544                    stream sub-char (if numargp numarg nil))
1545           (simple-reader-error stream
1546                                "no dispatch table for dispatch char")))))
1547 \f
1548 ;;;; READ-FROM-STRING
1549
1550 (defun read-from-string (string &optional (eof-error-p t) eof-value
1551                                 &key (start 0) end
1552                                 preserve-whitespace)
1553   #!+sb-doc
1554   "The characters of string are successively given to the lisp reader
1555    and the lisp object built by the reader is returned. Macro chars
1556    will take effect."
1557   (declare (string string))
1558   (with-array-data ((string string :offset-var offset)
1559                     (start start)
1560                     (end end)
1561                     :check-fill-pointer t)
1562     (let ((stream (make-string-input-stream string start end)))
1563       (values (if preserve-whitespace
1564                   (%read-preserving-whitespace stream eof-error-p eof-value nil)
1565                   (read stream eof-error-p eof-value))
1566               (- (string-input-stream-current stream) offset)))))
1567 \f
1568 ;;;; PARSE-INTEGER
1569
1570 (defun parse-integer (string &key (start 0) end (radix 10) junk-allowed)
1571   #!+sb-doc
1572   "Examine the substring of string delimited by start and end
1573   (default to the beginning and end of the string)  It skips over
1574   whitespace characters and then tries to parse an integer. The
1575   radix parameter must be between 2 and 36."
1576   (macrolet ((parse-error (format-control)
1577                `(error 'simple-parse-error
1578                        :format-control ,format-control
1579                        :format-arguments (list string))))
1580     (with-array-data ((string string :offset-var offset)
1581                       (start start)
1582                       (end end)
1583                       :check-fill-pointer t)
1584       (let ((index (do ((i start (1+ i)))
1585                        ((= i end)
1586                         (if junk-allowed
1587                             (return-from parse-integer (values nil end))
1588                             (parse-error "no non-whitespace characters in string ~S.")))
1589                      (declare (fixnum i))
1590                      (unless (whitespace[1]p (char string i)) (return i))))
1591             (minusp nil)
1592             (found-digit nil)
1593             (result 0))
1594         (declare (fixnum index))
1595         (let ((char (char string index)))
1596           (cond ((char= char #\-)
1597                  (setq minusp t)
1598                  (incf index))
1599                 ((char= char #\+)
1600                  (incf index))))
1601         (loop
1602          (when (= index end) (return nil))
1603          (let* ((char (char string index))
1604                 (weight (digit-char-p char radix)))
1605            (cond (weight
1606                   (setq result (+ weight (* result radix))
1607                         found-digit t))
1608                  (junk-allowed (return nil))
1609                  ((whitespace[1]p char)
1610                   (loop
1611                    (incf index)
1612                    (when (= index end) (return))
1613                    (unless (whitespace[1]p (char string index))
1614                       (parse-error "junk in string ~S")))
1615                   (return nil))
1616                  (t
1617                   (parse-error "junk in string ~S"))))
1618          (incf index))
1619         (values
1620          (if found-digit
1621              (if minusp (- result) result)
1622              (if junk-allowed
1623                  nil
1624                  (parse-error "no digits in string ~S")))
1625          (- index offset))))))
1626 \f
1627 ;;;; reader initialization code
1628
1629 (defun !reader-cold-init ()
1630   (!cold-init-constituent-trait-table)
1631   (!cold-init-standard-readtable)
1632   ;; FIXME: This was commented out, but should probably be restored.
1633   #+nil (!cold-init-integer-reader))
1634 \f
1635 (def!method print-object ((readtable readtable) stream)
1636   (print-unreadable-object (readtable stream :identity t :type t)))