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