0.9.0.13:
[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   (handler-bind
529       ((character-decoding-error
530         #'(lambda (decoding-error)
531             (declare (ignorable decoding-error))
532             (style-warn "Character decoding error in a ;-comment at position ~A reading source file ~A, resyncing." (file-position stream) stream)
533             (invoke-restart 'attempt-resync))))
534     (let ((stream (in-synonym-of stream)))
535       (if (ansi-stream-p stream)
536           (prepare-for-fast-read-char stream
537            (do ((char (fast-read-char nil nil)
538                       (fast-read-char nil nil)))
539                ((or (not char) (char= char #\newline))
540                 (done-with-fast-read-char))))
541           ;; CLOS stream
542           (do ((char (read-char stream nil :eof) (read-char stream nil :eof)))
543               ((or (eq char :eof) (char= char #\newline)))))))
544   ;; Don't return anything.
545   (values))
546
547 (defun read-list (stream ignore)
548   (declare (ignore ignore))
549   (let* ((thelist (list nil))
550          (listtail thelist))
551     (do ((firstchar (flush-whitespace stream) (flush-whitespace stream)))
552         ((char= firstchar #\) ) (cdr thelist))
553       (when (char= firstchar #\.)
554             (let ((nextchar (read-char stream t)))
555               (cond ((token-delimiterp nextchar)
556                      (cond ((eq listtail thelist)
557                             (%reader-error
558                              stream
559                              "Nothing appears before . in list."))
560                            ((whitespacep nextchar)
561                             (setq nextchar (flush-whitespace stream))))
562                      (rplacd listtail
563                              ;; Return list containing last thing.
564                              (car (read-after-dot stream nextchar)))
565                      (return (cdr thelist)))
566                     ;; Put back NEXTCHAR so that we can read it normally.
567                     (t (unread-char nextchar stream)))))
568       ;; Next thing is not an isolated dot.
569       (let ((listobj (read-maybe-nothing stream firstchar)))
570         ;; allows the possibility that a comment was read
571         (when listobj
572               (rplacd listtail listobj)
573               (setq listtail listobj))))))
574
575 (defun read-after-dot (stream firstchar)
576   ;; FIRSTCHAR is non-whitespace!
577   (let ((lastobj ()))
578     (do ((char firstchar (flush-whitespace stream)))
579         ((char= char #\) )
580          (%reader-error stream "Nothing appears after . in list."))
581       ;; See whether there's something there.
582       (setq lastobj (read-maybe-nothing stream char))
583       (when lastobj (return t)))
584     ;; At least one thing appears after the dot.
585     ;; Check for more than one thing following dot.
586     (do ((lastchar (flush-whitespace stream)
587                    (flush-whitespace stream)))
588         ((char= lastchar #\) ) lastobj) ;success!
589       ;; Try reading virtual whitespace.
590       (if (read-maybe-nothing stream lastchar)
591           (%reader-error stream "More than one object follows . in list.")))))
592
593 (defun read-string (stream closech)
594   ;; This accumulates chars until it sees same char that invoked it.
595   ;; For a very long string, this could end up bloating the read buffer.
596   (reset-read-buffer)
597   (let ((stream (in-synonym-of stream)))
598     (if (ansi-stream-p stream)
599         (prepare-for-fast-read-char stream
600           (do ((char (fast-read-char t) (fast-read-char t)))
601               ((char= char closech)
602                (done-with-fast-read-char))
603             (if (single-escape-p char) (setq char (fast-read-char t)))
604             (ouch-read-buffer char)))
605         ;; CLOS stream
606         (do ((char (read-char stream nil :eof) (read-char stream nil :eof)))
607             ((or (eq char :eof) (char= char closech))
608              (if (eq char :eof)
609                  (error 'end-of-file :stream stream)))
610           (when (single-escape-p char)
611             (setq char (read-char stream nil :eof))
612             (if (eq char :eof)
613                 (error 'end-of-file :stream stream)))
614           (ouch-read-buffer char))))
615   (read-buffer-to-string))
616
617 (defun read-right-paren (stream ignore)
618   (declare (ignore ignore))
619   (%reader-error stream "unmatched close parenthesis"))
620
621 ;;; Read from the stream up to the next delimiter. Leave the resulting
622 ;;; token in *READ-BUFFER*, and return two values:
623 ;;; -- a list of the escaped character positions, and
624 ;;; -- The position of the first package delimiter (or NIL).
625 (defun internal-read-extended-token (stream firstchar escape-firstchar)
626   (reset-read-buffer)
627   (let ((escapes '()))
628     (when escape-firstchar
629       (push *ouch-ptr* escapes)
630       (ouch-read-buffer firstchar)
631       (setq firstchar (read-char stream nil *eof-object*)))
632   (do ((char firstchar (read-char stream nil *eof-object*))
633        (colon nil))
634       ((cond ((eofp char) t)
635              ((token-delimiterp char)
636               (unread-char char stream)
637               t)
638              (t nil))
639        (values escapes colon))
640     (cond ((single-escape-p char)
641            ;; It can't be a number, even if it's 1\23.
642            ;; Read next char here, so it won't be casified.
643            (push *ouch-ptr* escapes)
644            (let ((nextchar (read-char stream nil *eof-object*)))
645              (if (eofp nextchar)
646                  (reader-eof-error stream "after escape character")
647                  (ouch-read-buffer nextchar))))
648           ((multiple-escape-p char)
649            ;; Read to next multiple-escape, escaping single chars
650            ;; along the way.
651            (loop
652              (let ((ch (read-char stream nil *eof-object*)))
653                (cond
654                 ((eofp ch)
655                  (reader-eof-error stream "inside extended token"))
656                 ((multiple-escape-p ch) (return))
657                 ((single-escape-p ch)
658                  (let ((nextchar (read-char stream nil *eof-object*)))
659                    (cond ((eofp nextchar)
660                           (reader-eof-error stream "after escape character"))
661                          (t
662                           (push *ouch-ptr* escapes)
663                           (ouch-read-buffer nextchar)))))
664                 (t
665                  (push *ouch-ptr* escapes)
666                  (ouch-read-buffer ch))))))
667           (t
668            (when (and (constituentp char)
669                       (eql (get-constituent-trait char)
670                            +char-attr-package-delimiter+)
671                       (not colon))
672              (setq colon *ouch-ptr*))
673            (ouch-read-buffer char))))))
674 \f
675 ;;;; character classes
676
677 ;;; Return the character class for CHAR.
678 ;;;
679 ;;; FIXME: why aren't these ATT-getting forms using GET-CAT-ENTRY?
680 ;;; Because we've cached the readtable tables?
681 (defmacro char-class (char attarray atthash)
682   `(let ((att (if (typep ,char 'base-char)
683                   (aref ,attarray (char-code ,char))
684                   (gethash ,char ,atthash +char-attr-constituent+))))
685      (declare (fixnum att))
686      (cond
687        ((<= att +char-attr-terminating-macro+) +char-attr-delimiter+)
688        ((< att +char-attr-constituent+) att)
689        (t (setf att (get-constituent-trait ,char))
690           (if (= att +char-attr-invalid+) 
691               (%reader-error stream "invalid constituent")
692               att)))))
693
694 ;;; Return the character class for CHAR, which might be part of a
695 ;;; rational number.
696 (defmacro char-class2 (char attarray atthash)
697   `(let ((att (if (typep ,char 'base-char)
698                   (aref ,attarray (char-code ,char))
699                   (gethash ,char ,atthash +char-attr-constituent+))))
700      (declare (fixnum att))
701      (cond
702        ((<= att +char-attr-terminating-macro+) +char-attr-delimiter+)
703        ((< att +char-attr-constituent+) att)
704        (t (setf att (get-constituent-trait ,char))
705           (cond
706             ((digit-char-p ,char *read-base*) +char-attr-constituent-digit+)
707             ((= att +char-attr-constituent-digit+) +char-attr-constituent+)
708             ((= att +char-attr-invalid+) 
709              (%reader-error stream "invalid constituent"))
710             (t att))))))
711
712 ;;; Return the character class for a char which might be part of a
713 ;;; rational or floating number. (Assume that it is a digit if it
714 ;;; could be.)
715 (defmacro char-class3 (char attarray atthash)
716   `(let ((att (if (typep ,char 'base-char)
717                   (aref ,attarray (char-code ,char))
718                   (gethash ,char ,atthash +char-attr-constituent+))))
719      (declare (fixnum att))
720      (cond
721        ((<= att +char-attr-terminating-macro+) +char-attr-delimiter+)
722        ((< att +char-attr-constituent+) att)
723        (t (setf att (get-constituent-trait ,char))
724           (when possibly-rational
725             (setq possibly-rational
726                   (or (digit-char-p ,char *read-base*)
727                       (= att +char-attr-constituent-slash+))))
728           (when possibly-float
729             (setq possibly-float
730                   (or (digit-char-p ,char 10)
731                       (= att +char-attr-constituent-dot+))))
732           (cond
733             ((digit-char-p ,char (max *read-base* 10))
734              (if (digit-char-p ,char *read-base*)
735                  (if (= att +char-attr-constituent-expt+)
736                      +char-attr-constituent-digit-or-expt+
737                      +char-attr-constituent-digit+)
738                  +char-attr-constituent-decimal-digit+))
739             ((= att +char-attr-invalid+)
740              (%reader-error stream "invalid constituent"))
741             (t att))))))
742 \f
743 ;;;; token fetching
744
745 (defvar *read-suppress* nil
746   #!+sb-doc
747   "Suppress most interpreting in the reader when T.")
748
749 (defvar *read-base* 10
750   #!+sb-doc
751   "the radix that Lisp reads numbers in")
752 (declaim (type (integer 2 36) *read-base*))
753
754 ;;; Modify the read buffer according to READTABLE-CASE, ignoring
755 ;;; ESCAPES. ESCAPES is a list of the escaped indices, in reverse
756 ;;; order.
757 (defun casify-read-buffer (escapes)
758   (let ((case (readtable-case *readtable*)))
759     (cond
760      ((and (null escapes) (eq case :upcase))
761       (dotimes (i *ouch-ptr*)
762         (setf (schar *read-buffer* i)
763               (char-upcase (schar *read-buffer* i)))))
764      ((eq case :preserve))
765      (t
766       (macrolet ((skip-esc (&body body)
767                    `(do ((i (1- *ouch-ptr*) (1- i))
768                          (escapes escapes))
769                         ((minusp i))
770                       (declare (fixnum i))
771                       (when (or (null escapes)
772                                 (let ((esc (first escapes)))
773                                   (declare (fixnum esc))
774                                   (cond ((< esc i) t)
775                                         (t
776                                          (aver (= esc i))
777                                          (pop escapes)
778                                          nil))))
779                         (let ((ch (schar *read-buffer* i)))
780                           ,@body)))))
781         (flet ((lower-em ()
782                  (skip-esc (setf (schar *read-buffer* i) (char-downcase ch))))
783                (raise-em ()
784                  (skip-esc (setf (schar *read-buffer* i) (char-upcase ch)))))
785           (ecase case
786             (:upcase (raise-em))
787             (:downcase (lower-em))
788             (:invert
789              (let ((all-upper t)
790                    (all-lower t))
791                (skip-esc
792                  (when (both-case-p ch)
793                    (if (upper-case-p ch)
794                        (setq all-lower nil)
795                        (setq all-upper nil))))
796                (cond (all-lower (raise-em))
797                      (all-upper (lower-em))))))))))))
798
799 (defun read-token (stream firstchar)
800   #!+sb-doc
801   "This function is just an fsm that recognizes numbers and symbols."
802   ;; Check explicitly whether FIRSTCHAR has an entry for
803   ;; NON-TERMINATING in CHARACTER-ATTRIBUTE-TABLE and
804   ;; READ-DOT-NUMBER-SYMBOL in CMT. Report an error if these are
805   ;; violated. (If we called this, we want something that is a
806   ;; legitimate token!) Read in the longest possible string satisfying
807   ;; the Backus-Naur form for "unqualified-token". Leave the result in
808   ;; the *READ-BUFFER*. Return next char after token (last char read).
809   (when *read-suppress*
810     (internal-read-extended-token stream firstchar nil)
811     (return-from read-token nil))
812   (let ((attribute-array (character-attribute-array *readtable*))
813         (attribute-hash-table (character-attribute-hash-table *readtable*))
814         (package-designator nil)
815         (colons 0)
816         (possibly-rational t)
817         (seen-digit-or-expt nil)
818         (possibly-float t)
819         (was-possibly-float nil)
820         (escapes ())
821         (seen-multiple-escapes nil))
822     (reset-read-buffer)
823     (prog ((char firstchar))
824       (case (char-class3 char attribute-array attribute-hash-table)
825         (#.+char-attr-constituent-sign+ (go SIGN))
826         (#.+char-attr-constituent-digit+ (go LEFTDIGIT))
827         (#.+char-attr-constituent-digit-or-expt+
828          (setq seen-digit-or-expt t)
829          (go LEFTDIGIT))
830         (#.+char-attr-constituent-decimal-digit+ (go LEFTDECIMALDIGIT))
831         (#.+char-attr-constituent-dot+ (go FRONTDOT))
832         (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
833         (#.+char-attr-package-delimiter+ (go COLON))
834         (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
835         (#.+char-attr-invalid+ (%reader-error stream "invalid constituent"))
836         ;; can't have eof, whitespace, or terminating macro as first char!
837         (t (go SYMBOL)))
838      SIGN ; saw "sign"
839       (ouch-read-buffer char)
840       (setq char (read-char stream nil nil))
841       (unless char (go RETURN-SYMBOL))
842       (setq possibly-rational t
843             possibly-float t)
844       (case (char-class3 char attribute-array attribute-hash-table)
845         (#.+char-attr-constituent-digit+ (go LEFTDIGIT))
846         (#.+char-attr-constituent-digit-or-expt+
847          (setq seen-digit-or-expt t)
848          (go LEFTDIGIT))
849         (#.+char-attr-constituent-decimal-digit+ (go LEFTDECIMALDIGIT))
850         (#.+char-attr-constituent-dot+ (go SIGNDOT))
851         (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
852         (#.+char-attr-package-delimiter+ (go COLON))
853         (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))        
854         (#.+char-attr-delimiter+ (unread-char char stream) (go RETURN-SYMBOL))
855         (t (go SYMBOL)))
856      LEFTDIGIT ; saw "[sign] {digit}+"
857       (ouch-read-buffer char)
858       (setq char (read-char stream nil nil))
859       (unless char (return (make-integer)))
860       (setq was-possibly-float possibly-float)
861       (case (char-class3 char attribute-array attribute-hash-table)
862         (#.+char-attr-constituent-digit+ (go LEFTDIGIT))
863         (#.+char-attr-constituent-decimal-digit+ (if possibly-float
864                                                      (go LEFTDECIMALDIGIT)
865                                                      (go SYMBOL)))
866         (#.+char-attr-constituent-dot+ (if possibly-float
867                                            (go MIDDLEDOT)
868                                            (go SYMBOL)))
869         (#.+char-attr-constituent-digit-or-expt+
870          (if (or seen-digit-or-expt (not was-possibly-float))
871              (progn (setq seen-digit-or-expt t) (go LEFTDIGIT))
872              (progn (setq seen-digit-or-expt t) (go LEFTDIGIT-OR-EXPT))))
873         (#.+char-attr-constituent-expt+
874          (if was-possibly-float
875              (go EXPONENT)
876              (go SYMBOL)))
877         (#.+char-attr-constituent-slash+ (if possibly-rational
878                                              (go RATIO)
879                                              (go SYMBOL)))
880         (#.+char-attr-delimiter+ (unread-char char stream)
881                                  (return (make-integer)))
882         (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
883         (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
884         (#.+char-attr-package-delimiter+ (go COLON))
885         (t (go SYMBOL)))
886      LEFTDIGIT-OR-EXPT
887       (ouch-read-buffer char)
888       (setq char (read-char stream nil nil))
889       (unless char (return (make-integer)))
890       (case (char-class3 char attribute-array attribute-hash-table)
891         (#.+char-attr-constituent-digit+ (go LEFTDIGIT))
892         (#.+char-attr-constituent-decimal-digit+ (bug "impossible!"))
893         (#.+char-attr-constituent-dot+ (go SYMBOL))
894         (#.+char-attr-constituent-digit-or-expt+ (go LEFTDIGIT))
895         (#.+char-attr-constituent-expt+ (go SYMBOL))
896         (#.+char-attr-constituent-sign+ (go EXPTSIGN))
897         (#.+char-attr-constituent-slash+ (if possibly-rational
898                                              (go RATIO)
899                                              (go SYMBOL)))
900         (#.+char-attr-delimiter+ (unread-char char stream)
901                                  (return (make-integer)))
902         (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
903         (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
904         (#.+char-attr-package-delimiter+ (go COLON))
905         (t (go SYMBOL)))
906      LEFTDECIMALDIGIT ; saw "[sign] {decimal-digit}+"
907       (aver possibly-float)
908       (ouch-read-buffer char)
909       (setq char (read-char stream nil nil))
910       (unless char (go RETURN-SYMBOL))
911       (case (char-class char attribute-array attribute-hash-table)
912         (#.+char-attr-constituent-digit+ (go LEFTDECIMALDIGIT))
913         (#.+char-attr-constituent-dot+ (go MIDDLEDOT))
914         (#.+char-attr-constituent-expt+ (go EXPONENT))
915         (#.+char-attr-constituent-slash+ (aver (not possibly-rational))
916                                          (go SYMBOL))
917         (#.+char-attr-delimiter+ (unread-char char stream)
918                                  (go RETURN-SYMBOL))
919         (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
920         (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
921         (#.+char-attr-package-delimiter+ (go COLON))
922         (t (go SYMBOL)))
923      MIDDLEDOT ; saw "[sign] {digit}+ dot"
924       (ouch-read-buffer char)
925       (setq char (read-char stream nil nil))
926       (unless char (return (let ((*read-base* 10))
927                              (make-integer))))
928       (case (char-class char attribute-array attribute-hash-table)
929         (#.+char-attr-constituent-digit+ (go RIGHTDIGIT))
930         (#.+char-attr-constituent-expt+ (go EXPONENT))
931         (#.+char-attr-delimiter+
932          (unread-char char stream)
933          (return (let ((*read-base* 10))
934                    (make-integer))))
935         (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
936         (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
937         (#.+char-attr-package-delimiter+ (go COLON))
938         (t (go SYMBOL)))
939      RIGHTDIGIT ; saw "[sign] {decimal-digit}* dot {digit}+"
940       (ouch-read-buffer char)
941       (setq char (read-char stream nil nil))
942       (unless char (return (make-float stream)))
943       (case (char-class char attribute-array attribute-hash-table)
944         (#.+char-attr-constituent-digit+ (go RIGHTDIGIT))
945         (#.+char-attr-constituent-expt+ (go EXPONENT))
946         (#.+char-attr-delimiter+
947          (unread-char char stream)
948          (return (make-float stream)))
949         (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
950         (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
951         (#.+char-attr-package-delimiter+ (go COLON))
952         (t (go SYMBOL)))
953      SIGNDOT ; saw "[sign] dot"
954       (ouch-read-buffer char)
955       (setq char (read-char stream nil nil))
956       (unless char (go RETURN-SYMBOL))
957       (case (char-class char attribute-array attribute-hash-table)
958         (#.+char-attr-constituent-digit+ (go RIGHTDIGIT))
959         (#.+char-attr-delimiter+ (unread-char char stream) (go RETURN-SYMBOL))
960         (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
961         (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
962         (t (go SYMBOL)))
963      FRONTDOT ; saw "dot"
964       (ouch-read-buffer char)
965       (setq char (read-char stream nil nil))
966       (unless char (%reader-error stream "dot context error"))
967       (case (char-class char attribute-array attribute-hash-table)
968         (#.+char-attr-constituent-digit+ (go RIGHTDIGIT))
969         (#.+char-attr-constituent-dot+ (go DOTS))
970         (#.+char-attr-delimiter+  (%reader-error stream "dot context error"))
971         (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
972         (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
973         (#.+char-attr-package-delimiter+ (go COLON))
974         (t (go SYMBOL)))
975      EXPONENT
976       (ouch-read-buffer char)
977       (setq char (read-char stream nil nil))
978       (unless char (go RETURN-SYMBOL))
979       (setq possibly-float t)
980       (case (char-class char attribute-array attribute-hash-table)
981         (#.+char-attr-constituent-sign+ (go EXPTSIGN))
982         (#.+char-attr-constituent-digit+ (go EXPTDIGIT))
983         (#.+char-attr-delimiter+ (unread-char char stream) (go RETURN-SYMBOL))
984         (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
985         (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
986         (#.+char-attr-package-delimiter+ (go COLON))
987         (t (go SYMBOL)))
988      EXPTSIGN ; got to EXPONENT, and saw a sign character
989       (ouch-read-buffer char)
990       (setq char (read-char stream nil nil))
991       (unless char (go RETURN-SYMBOL))
992       (case (char-class char attribute-array attribute-hash-table)
993         (#.+char-attr-constituent-digit+ (go EXPTDIGIT))
994         (#.+char-attr-delimiter+ (unread-char char stream) (go RETURN-SYMBOL))
995         (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
996         (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
997         (#.+char-attr-package-delimiter+ (go COLON))
998         (t (go SYMBOL)))
999      EXPTDIGIT ; got to EXPONENT, saw "[sign] {digit}+"
1000       (ouch-read-buffer char)
1001       (setq char (read-char stream nil nil))
1002       (unless char (return (make-float stream)))
1003       (case (char-class char attribute-array attribute-hash-table)
1004         (#.+char-attr-constituent-digit+ (go EXPTDIGIT))
1005         (#.+char-attr-delimiter+
1006          (unread-char char stream)
1007          (return (make-float stream)))
1008         (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1009         (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1010         (#.+char-attr-package-delimiter+ (go COLON))
1011         (t (go SYMBOL)))
1012      RATIO ; saw "[sign] {digit}+ slash"
1013       (ouch-read-buffer char)
1014       (setq char (read-char stream nil nil))
1015       (unless char (go RETURN-SYMBOL))
1016       (case (char-class2 char attribute-array attribute-hash-table)
1017         (#.+char-attr-constituent-digit+ (go RATIODIGIT))
1018         (#.+char-attr-delimiter+ (unread-char char stream) (go RETURN-SYMBOL))
1019         (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1020         (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1021         (#.+char-attr-package-delimiter+ (go COLON))
1022         (t (go SYMBOL)))
1023      RATIODIGIT ; saw "[sign] {digit}+ slash {digit}+"
1024       (ouch-read-buffer char)
1025       (setq char (read-char stream nil nil))
1026       (unless char (return (make-ratio stream)))
1027       (case (char-class2 char attribute-array attribute-hash-table)
1028         (#.+char-attr-constituent-digit+ (go RATIODIGIT))
1029         (#.+char-attr-delimiter+
1030          (unread-char char stream)
1031          (return (make-ratio stream)))
1032         (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1033         (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1034         (#.+char-attr-package-delimiter+ (go COLON))
1035         (t (go SYMBOL)))
1036      DOTS ; saw "dot {dot}+"
1037       (ouch-read-buffer char)
1038       (setq char (read-char stream nil nil))
1039       (unless char (%reader-error stream "too many dots"))
1040       (case (char-class char attribute-array attribute-hash-table)
1041         (#.+char-attr-constituent-dot+ (go DOTS))
1042         (#.+char-attr-delimiter+
1043          (unread-char char stream)
1044          (%reader-error stream "too many dots"))
1045         (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1046         (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1047         (#.+char-attr-package-delimiter+ (go COLON))
1048         (t (go SYMBOL)))
1049      SYMBOL ; not a dot, dots, or number
1050       (let ((stream (in-synonym-of stream)))
1051         (if (ansi-stream-p stream)
1052             (prepare-for-fast-read-char stream
1053               (prog ()
1054                SYMBOL-LOOP
1055                (ouch-read-buffer char)
1056                (setq char (fast-read-char nil nil))
1057                (unless char (go RETURN-SYMBOL))
1058                (case (char-class char attribute-array attribute-hash-table)
1059                  (#.+char-attr-single-escape+ (done-with-fast-read-char)
1060                                               (go SINGLE-ESCAPE))
1061                  (#.+char-attr-delimiter+ (done-with-fast-read-char)
1062                                           (unread-char char stream)
1063                                           (go RETURN-SYMBOL))
1064                  (#.+char-attr-multiple-escape+ (done-with-fast-read-char)
1065                                                 (go MULT-ESCAPE))
1066                  (#.+char-attr-package-delimiter+ (done-with-fast-read-char)
1067                                                   (go COLON))
1068                  (t (go SYMBOL-LOOP)))))
1069             ;; CLOS stream
1070             (prog ()
1071              SYMBOL-LOOP
1072              (ouch-read-buffer char)
1073              (setq char (read-char stream nil :eof))
1074              (when (eq char :eof) (go RETURN-SYMBOL))
1075              (case (char-class char attribute-array attribute-hash-table)
1076                (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1077                (#.+char-attr-delimiter+ (unread-char char stream)
1078                             (go RETURN-SYMBOL))
1079                (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1080                (#.+char-attr-package-delimiter+ (go COLON))
1081                (t (go SYMBOL-LOOP))))))
1082      SINGLE-ESCAPE ; saw a single-escape
1083       ;; Don't put the escape character in the read buffer.
1084       ;; READ-NEXT CHAR, put in buffer (no case conversion).
1085       (let ((nextchar (read-char stream nil nil)))
1086         (unless nextchar
1087           (reader-eof-error stream "after single-escape character"))
1088         (push *ouch-ptr* escapes)
1089         (ouch-read-buffer nextchar))
1090       (setq char (read-char stream nil nil))
1091       (unless char (go RETURN-SYMBOL))
1092       (case (char-class char attribute-array attribute-hash-table)
1093         (#.+char-attr-delimiter+ (unread-char char stream) (go RETURN-SYMBOL))
1094         (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1095         (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1096         (#.+char-attr-package-delimiter+ (go COLON))
1097         (t (go SYMBOL)))
1098       MULT-ESCAPE
1099       (setq seen-multiple-escapes t)
1100       (do ((char (read-char stream t) (read-char stream t)))
1101           ((multiple-escape-p char))
1102         (if (single-escape-p char) (setq char (read-char stream t)))
1103         (push *ouch-ptr* escapes)
1104         (ouch-read-buffer char))
1105       (setq char (read-char stream nil nil))
1106       (unless char (go RETURN-SYMBOL))
1107       (case (char-class char attribute-array attribute-hash-table)
1108         (#.+char-attr-delimiter+ (unread-char char stream) (go RETURN-SYMBOL))
1109         (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1110         (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1111         (#.+char-attr-package-delimiter+ (go COLON))
1112         (t (go SYMBOL)))
1113       COLON
1114       (casify-read-buffer escapes)
1115       (unless (zerop colons)
1116         (%reader-error stream "too many colons in ~S"
1117                       (read-buffer-to-string)))
1118       (setq colons 1)
1119       (setq package-designator
1120             (if (plusp *ouch-ptr*)
1121                 ;; FIXME: It seems inefficient to cons up a package
1122                 ;; designator string every time we read a symbol with an
1123                 ;; explicit package prefix. Perhaps we could implement
1124                 ;; a FIND-PACKAGE* function analogous to INTERN*
1125                 ;; and friends?
1126                 (read-buffer-to-string)
1127                 (if seen-multiple-escapes
1128                     (read-buffer-to-string)
1129                     *keyword-package*)))
1130       (reset-read-buffer)
1131       (setq escapes ())
1132       (setq char (read-char stream nil nil))
1133       (unless char (reader-eof-error stream "after reading a colon"))
1134       (case (char-class char attribute-array attribute-hash-table)
1135         (#.+char-attr-delimiter+
1136          (unread-char char stream)
1137          (%reader-error stream
1138                         "illegal terminating character after a colon: ~S"
1139                         char))
1140         (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1141         (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1142         (#.+char-attr-package-delimiter+ (go INTERN))
1143         (t (go SYMBOL)))
1144       INTERN
1145       (setq colons 2)
1146       (setq char (read-char stream nil nil))
1147       (unless char
1148         (reader-eof-error stream "after reading a colon"))
1149       (case (char-class char attribute-array attribute-hash-table)
1150         (#.+char-attr-delimiter+
1151          (unread-char char stream)
1152          (%reader-error stream
1153                         "illegal terminating character after a colon: ~S"
1154                         char))
1155         (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1156         (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1157         (#.+char-attr-package-delimiter+
1158          (%reader-error stream
1159                         "too many colons after ~S name"
1160                         package-designator))
1161         (t (go SYMBOL)))
1162       RETURN-SYMBOL
1163       (casify-read-buffer escapes)
1164       (let ((found (if package-designator
1165                        (find-package package-designator)
1166                        (sane-package))))
1167         (unless found
1168           (error 'reader-package-error :stream stream
1169                  :format-arguments (list package-designator)
1170                  :format-control "package ~S not found"))
1171
1172         (if (or (zerop colons) (= colons 2) (eq found *keyword-package*))
1173             (return (intern* *read-buffer* *ouch-ptr* found))
1174             (multiple-value-bind (symbol test)
1175                 (find-symbol* *read-buffer* *ouch-ptr* found)
1176               (when (eq test :external) (return symbol))
1177               (let ((name (read-buffer-to-string)))
1178                 (with-simple-restart (continue "Use symbol anyway.")
1179                   (error 'reader-package-error :stream stream
1180                          :format-arguments (list name (package-name found))
1181                          :format-control
1182                          (if test
1183                              "The symbol ~S is not external in the ~A package."
1184                              "Symbol ~S not found in the ~A package.")))
1185                 (return (intern name found)))))))))
1186
1187 ;;; for semi-external use:
1188 ;;;
1189 ;;; For semi-external use: Return 3 values: the string for the token,
1190 ;;; a flag for whether there was an escape char, and the position of
1191 ;;; any package delimiter.
1192 (defun read-extended-token (stream &optional (*readtable* *readtable*))
1193   (let ((first-char (read-char stream nil nil t)))
1194     (cond (first-char
1195            (multiple-value-bind (escapes colon)
1196                (internal-read-extended-token stream first-char nil)
1197              (casify-read-buffer escapes)
1198              (values (read-buffer-to-string) (not (null escapes)) colon)))
1199           (t
1200            (values "" nil nil)))))
1201
1202 ;;; for semi-external use:
1203 ;;;
1204 ;;; Read an extended token with the first character escaped. Return
1205 ;;; the string for the token.
1206 (defun read-extended-token-escaped (stream &optional (*readtable* *readtable*))
1207   (let ((first-char (read-char stream nil nil)))
1208     (cond (first-char
1209             (let ((escapes (internal-read-extended-token stream first-char t)))
1210               (casify-read-buffer escapes)
1211               (read-buffer-to-string)))
1212           (t
1213             (reader-eof-error stream "after escape")))))
1214 \f
1215 ;;;; number-reading functions
1216
1217 (defmacro digit* nil
1218   `(do ((ch char (inch-read-buffer)))
1219        ((or (eofp ch) (not (digit-char-p ch))) (setq char ch))
1220      ;; Report if at least one digit is seen.
1221      (setq one-digit t)))
1222
1223 (defmacro exponent-letterp (letter)
1224   `(memq ,letter '(#\E #\S #\F #\L #\D #\e #\s #\f #\l #\d)))
1225
1226 ;;; FIXME: It would be cleaner to have these generated automatically
1227 ;;; by compile-time code instead of having them hand-created like
1228 ;;; this. The !COLD-INIT-INTEGER-READER code below should be resurrected
1229 ;;; and tested.
1230 (defvar *integer-reader-safe-digits*
1231   #(nil nil
1232     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)
1233   #!+sb-doc
1234   "the mapping of base to 'safe' number of digits to read for a fixnum")
1235 (defvar *integer-reader-base-power*
1236   #(nil nil
1237     67108864 129140163 67108864 48828125 60466176 40353607
1238     16777216 43046721 100000000 19487171 35831808 62748517 105413504 11390625
1239     16777216 24137569 34012224 47045881 64000000 85766121 113379904 6436343
1240     7962624 9765625 11881376 14348907 17210368 20511149 24300000 28629151
1241     33554432 39135393 45435424 52521875 60466176)
1242   #!+sb-doc
1243   "the largest fixnum power of the base for MAKE-INTEGER")
1244 (declaim (simple-vector *integer-reader-safe-digits*
1245                         *integer-reader-base-power*))
1246 #|
1247 (defun !cold-init-integer-reader ()
1248   (do ((base 2 (1+ base)))
1249       ((> base 36))
1250     (let ((digits
1251           (do ((fix (truncate most-positive-fixnum base)
1252                     (truncate fix base))
1253                (digits 0 (1+ digits)))
1254               ((zerop fix) digits))))
1255       (setf (aref *integer-reader-safe-digits* base)
1256             digits
1257             (aref *integer-reader-base-power* base)
1258             (expt base digits)))))
1259 |#
1260
1261 (defun make-integer ()
1262   #!+sb-doc
1263   "Minimizes bignum-fixnum multiplies by reading a 'safe' number of digits,
1264   then multiplying by a power of the base and adding."
1265   (let* ((base *read-base*)
1266          (digits-per (aref *integer-reader-safe-digits* base))
1267          (base-power (aref *integer-reader-base-power* base))
1268          (negativep nil)
1269          (number 0))
1270     (declare (type index digits-per base-power))
1271     (read-unwind-read-buffer)
1272     (let ((char (inch-read-buffer)))
1273       (cond ((char= char #\-)
1274              (setq negativep t))
1275             ((char= char #\+))
1276             (t (unread-buffer))))
1277     (loop
1278      (let ((num 0))
1279        (declare (type index num))
1280        (dotimes (digit digits-per)
1281          (let* ((ch (inch-read-buffer)))
1282            (cond ((or (eofp ch) (char= ch #\.))
1283                   (return-from make-integer
1284                                (let ((res
1285                                       (if (zerop number) num
1286                                           (+ num (* number
1287                                                     (expt base digit))))))
1288                                  (if negativep (- res) res))))
1289                  (t (setq num (+ (digit-char-p ch base)
1290                                  (the index (* num base))))))))
1291        (setq number (+ num (* number base-power)))))))
1292
1293 (defun make-float (stream)
1294   ;; Assume that the contents of *read-buffer* are a legal float, with nothing
1295   ;; else after it.
1296   (read-unwind-read-buffer)
1297   (let ((negative-fraction nil)
1298         (number 0)
1299         (divisor 1)
1300         (negative-exponent nil)
1301         (exponent 0)
1302         (float-char ())
1303         (char (inch-read-buffer)))
1304     (if (cond ((char= char #\+) t)
1305               ((char= char #\-) (setq negative-fraction t)))
1306         ;; Flush it.
1307         (setq char (inch-read-buffer)))
1308     ;; Read digits before the dot.
1309     (do* ((ch char (inch-read-buffer))
1310           (dig (digit-char-p ch) (digit-char-p ch)))
1311          ((not dig) (setq char ch))
1312       (setq number (+ (* number 10) dig)))
1313     ;; Deal with the dot, if it's there.
1314     (when (char= char #\.)
1315       (setq char (inch-read-buffer))
1316       ;; Read digits after the dot.
1317       (do* ((ch char (inch-read-buffer))
1318             (dig (and (not (eofp ch)) (digit-char-p ch))
1319                  (and (not (eofp ch)) (digit-char-p ch))))
1320            ((not dig) (setq char ch))
1321         (setq divisor (* divisor 10))
1322         (setq number (+ (* number 10) dig))))
1323     ;; Is there an exponent letter?
1324     (cond ((eofp char)
1325            ;; If not, we've read the whole number.
1326            (let ((num (make-float-aux number divisor
1327                                       *read-default-float-format*
1328                                       stream)))
1329              (return-from make-float (if negative-fraction (- num) num))))
1330           ((exponent-letterp char)
1331            (setq float-char char)
1332            ;; Build exponent.
1333            (setq char (inch-read-buffer))
1334            ;; Check leading sign.
1335            (if (cond ((char= char #\+) t)
1336                      ((char= char #\-) (setq negative-exponent t)))
1337                ;; Flush sign.
1338                (setq char (inch-read-buffer)))
1339            ;; Read digits for exponent.
1340            (do* ((ch char (inch-read-buffer))
1341                  (dig (and (not (eofp ch)) (digit-char-p ch))
1342                       (and (not (eofp ch)) (digit-char-p ch))))
1343                 ((not dig)
1344                  (setq exponent (if negative-exponent (- exponent) exponent)))
1345              (setq exponent (+ (* exponent 10) dig)))
1346            ;; Generate and return the float, depending on FLOAT-CHAR:
1347            (let* ((float-format (case (char-upcase float-char)
1348                                   (#\E *read-default-float-format*)
1349                                   (#\S 'short-float)
1350                                   (#\F 'single-float)
1351                                   (#\D 'double-float)
1352                                   (#\L 'long-float)))
1353                   (result (make-float-aux (* (expt 10 exponent) number)
1354                                           divisor float-format stream)))
1355              (return-from make-float
1356                (if negative-fraction (- result) result))))
1357           (t (bug "bad fallthrough in floating point reader")))))
1358
1359 (defun make-float-aux (number divisor float-format stream)
1360   (handler-case
1361       (coerce (/ number divisor) float-format)
1362     (type-error (c)
1363       (error 'reader-impossible-number-error
1364              :error c :stream stream
1365              :format-control "failed to build float"))))
1366
1367 (defun make-ratio (stream)
1368   ;; Assume *READ-BUFFER* contains a legal ratio. Build the number from
1369   ;; the string.
1370   ;;
1371   ;; Look for optional "+" or "-".
1372   (let ((numerator 0) (denominator 0) (char ()) (negative-number nil))
1373     (read-unwind-read-buffer)
1374     (setq char (inch-read-buffer))
1375     (cond ((char= char #\+)
1376            (setq char (inch-read-buffer)))
1377           ((char= char #\-)
1378            (setq char (inch-read-buffer))
1379            (setq negative-number t)))
1380     ;; Get numerator.
1381     (do* ((ch char (inch-read-buffer))
1382           (dig (digit-char-p ch *read-base*)
1383                (digit-char-p ch *read-base*)))
1384          ((not dig))
1385          (setq numerator (+ (* numerator *read-base*) dig)))
1386     ;; Get denominator.
1387     (do* ((ch (inch-read-buffer) (inch-read-buffer))
1388           (dig ()))
1389          ((or (eofp ch) (not (setq dig (digit-char-p ch *read-base*)))))
1390          (setq denominator (+ (* denominator *read-base*) dig)))
1391     (let ((num (handler-case
1392                    (/ numerator denominator)
1393                  (arithmetic-error (c)
1394                    (error 'reader-impossible-number-error
1395                           :error c :stream stream
1396                           :format-control "failed to build ratio")))))
1397       (if negative-number (- num) num))))
1398 \f
1399 ;;;; cruft for dispatch macros
1400
1401 (defun make-char-dispatch-table ()
1402   (make-hash-table))
1403
1404 (defun dispatch-char-error (stream sub-char ignore)
1405   (declare (ignore ignore))
1406   (if *read-suppress*
1407       (values)
1408       (%reader-error stream "no dispatch function defined for ~S" sub-char)))
1409
1410 (defun make-dispatch-macro-character (char &optional
1411                                            (non-terminating-p nil)
1412                                            (rt *readtable*))
1413   #!+sb-doc
1414   "Cause CHAR to become a dispatching macro character in readtable (which
1415    defaults to the current readtable). If NON-TERMINATING-P, the char will
1416    be non-terminating."
1417   (set-macro-character char #'read-dispatch-char non-terminating-p rt)
1418   (let* ((dalist (dispatch-tables rt))
1419          (dtable (cdr (find char dalist :test #'char= :key #'car))))
1420     (cond (dtable
1421            (error "The dispatch character ~S already exists." char))
1422           (t
1423            (setf (dispatch-tables rt)
1424                  (push (cons char (make-char-dispatch-table)) dalist)))))
1425   t)
1426
1427 (defun set-dispatch-macro-character (disp-char sub-char function
1428                                                &optional (rt *readtable*))
1429   #!+sb-doc
1430   "Cause FUNCTION to be called whenever the reader reads DISP-CHAR
1431    followed by SUB-CHAR."
1432   ;; Get the dispatch char for macro (error if not there), diddle
1433   ;; entry for sub-char.
1434   (when (digit-char-p sub-char)
1435     (error "SUB-CHAR must not be a decimal digit: ~S" sub-char))
1436   (let* ((sub-char (char-upcase sub-char))
1437          (rt (or rt *standard-readtable*))
1438          (dpair (find disp-char (dispatch-tables rt)
1439                       :test #'char= :key #'car)))
1440     (if dpair
1441         (setf (gethash sub-char (cdr dpair)) (coerce function 'function))
1442         (error "~S is not a dispatch char." disp-char))))
1443
1444 (defun get-dispatch-macro-character (disp-char sub-char
1445                                      &optional (rt *readtable*))
1446   #!+sb-doc
1447   "Return the macro character function for SUB-CHAR under DISP-CHAR
1448    or NIL if there is no associated function."
1449   (let* ((sub-char (char-upcase sub-char))
1450          (rt (or rt *standard-readtable*))
1451          (dpair (find disp-char (dispatch-tables rt)
1452                       :test #'char= :key #'car)))
1453     (if dpair
1454         (values (gethash sub-char (cdr dpair)))
1455         (error "~S is not a dispatch char." disp-char))))
1456
1457 (defun read-dispatch-char (stream char)
1458   ;; Read some digits.
1459   (let ((numargp nil)
1460         (numarg 0)
1461         (sub-char ()))
1462     (do* ((ch (read-char stream nil *eof-object*)
1463               (read-char stream nil *eof-object*))
1464           (dig ()))
1465          ((or (eofp ch)
1466               (not (setq dig (digit-char-p ch))))
1467           ;; Take care of the extra char.
1468           (if (eofp ch)
1469               (reader-eof-error stream "inside dispatch character")
1470               (setq sub-char (char-upcase ch))))
1471       (setq numargp t)
1472       (setq numarg (+ (* numarg 10) dig)))
1473     ;; Look up the function and call it.
1474     (let ((dpair (find char (dispatch-tables *readtable*)
1475                        :test #'char= :key #'car)))
1476       (if dpair
1477           (funcall (the function
1478                      (gethash sub-char (cdr dpair) #'dispatch-char-error))
1479                    stream sub-char (if numargp numarg nil))
1480           (%reader-error stream "no dispatch table for dispatch char")))))
1481 \f
1482 ;;;; READ-FROM-STRING
1483
1484 ;;; FIXME: Is it really worth keeping this pool?
1485 (defvar *read-from-string-spares* ()
1486   #!+sb-doc
1487   "A resource of string streams for Read-From-String.")
1488
1489 (defun read-from-string (string &optional (eof-error-p t) eof-value
1490                                 &key (start 0) end
1491                                 preserve-whitespace)
1492   #!+sb-doc
1493   "The characters of string are successively given to the lisp reader
1494    and the lisp object built by the reader is returned. Macro chars
1495    will take effect."
1496   (declare (string string))
1497   
1498   (with-array-data ((string string :offset-var offset)
1499                     (start start)
1500                     (end (%check-vector-sequence-bounds string start end)))
1501     (unless *read-from-string-spares*
1502       (push (make-string-input-stream "" 0 0) *read-from-string-spares*))
1503     (let ((stream (pop *read-from-string-spares*)))
1504       (setf (string-input-stream-string stream)
1505             (coerce string '(simple-array character (*))))
1506       (setf (string-input-stream-current stream) start)
1507       (setf (string-input-stream-end stream) end)
1508       (unwind-protect
1509           (values (if preserve-whitespace
1510                       (read-preserving-whitespace stream eof-error-p eof-value)
1511                       (read stream eof-error-p eof-value))
1512                   (- (string-input-stream-current stream) offset))
1513         (push stream *read-from-string-spares*)))))
1514 \f
1515 ;;;; PARSE-INTEGER
1516
1517 (defun parse-integer (string &key (start 0) end (radix 10) junk-allowed)
1518   #!+sb-doc
1519   "Examine the substring of string delimited by start and end
1520   (default to the beginning and end of the string)  It skips over
1521   whitespace characters and then tries to parse an integer. The
1522   radix parameter must be between 2 and 36."
1523   (macrolet ((parse-error (format-control)
1524                `(error 'simple-parse-error
1525                        :format-control ,format-control
1526                        :format-arguments (list string))))
1527     (with-array-data ((string string :offset-var offset)
1528                       (start start)
1529                       (end (%check-vector-sequence-bounds string start end)))
1530       (let ((index (do ((i start (1+ i)))
1531                        ((= i end)
1532                         (if junk-allowed
1533                             (return-from parse-integer (values nil end))
1534                             (parse-error "no non-whitespace characters in string ~S.")))
1535                      (declare (fixnum i))
1536                      (unless (whitespacep (char string i)) (return i))))
1537             (minusp nil)
1538             (found-digit nil)
1539             (result 0))
1540         (declare (fixnum index))
1541         (let ((char (char string index)))
1542           (cond ((char= char #\-)
1543                  (setq minusp t)
1544                  (incf index))
1545                 ((char= char #\+)
1546                  (incf index))))
1547         (loop
1548          (when (= index end) (return nil))
1549          (let* ((char (char string index))
1550                 (weight (digit-char-p char radix)))
1551            (cond (weight
1552                   (setq result (+ weight (* result radix))
1553                         found-digit t))
1554                  (junk-allowed (return nil))
1555                  ((whitespacep char)
1556                   (loop
1557                    (incf index)
1558                    (when (= index end) (return))
1559                    (unless (whitespacep (char string index))
1560                       (parse-error "junk in string ~S")))
1561                   (return nil))
1562                  (t
1563                   (parse-error "junk in string ~S"))))
1564          (incf index))
1565         (values
1566          (if found-digit
1567              (if minusp (- result) result)
1568              (if junk-allowed
1569                  nil
1570                  (parse-error "no digits in string ~S")))
1571          (- index offset))))))
1572 \f
1573 ;;;; reader initialization code
1574
1575 (defun !reader-cold-init ()
1576   (!cold-init-read-buffer)
1577   (!cold-init-constituent-trait-table)
1578   (!cold-init-standard-readtable)
1579   ;; FIXME: This was commented out, but should probably be restored.
1580   #+nil (!cold-init-integer-reader))
1581 \f
1582 (def!method print-object ((readtable readtable) stream)
1583   (print-unreadable-object (readtable stream :identity t :type t)))