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