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