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