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