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