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