3 ;;;; This software is part of the SBCL system. See the README file for
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*))
24 (declaim (type readtable *readtable*))
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
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,
43 (declaim (special *package* *keyword-package* *read-base*))
47 (defun reader-eof-error (stream context)
48 (error 'reader-eof-error
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
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)))
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
95 (defun get-coerced-cmt-entry (char readtable)
97 (or (get-raw-cmt-entry char readtable)
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
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
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))
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)
229 (let ((table (make-hash-table)))
230 (shallow-replace/eql-hash-table table (cdr pair))
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))
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 (cond ((and (not from-dpair) (not to-dpair)))
254 ((and (not from-dpair) to-dpair)
255 (setf (dispatch-tables to-readtable)
256 (remove to-dpair (dispatch-tables to-readtable))))
258 (let ((table (cdr to-dpair)))
260 (shallow-replace/eql-hash-table table (cdr from-dpair))))
262 (let ((pair (cons to-char (make-hash-table))))
263 (shallow-replace/eql-hash-table (cdr pair) (cdr from-dpair))
264 (setf (dispatch-tables to-readtable)
265 (push pair (dispatch-tables to-readtable))))))))
268 (defun set-macro-character (char function &optional
269 (non-terminatingp nil)
270 (rt-designator *readtable*))
272 "Causes CHAR to be a macro character which invokes FUNCTION when seen
273 by the reader. The NON-TERMINATINGP flag can be used to make the macro
274 character non-terminating, i.e. embeddable in a symbol name."
275 (let ((designated-readtable (or rt-designator *standard-readtable*))
276 (function (%coerce-callable-to-fun function)))
277 (assert-not-standard-readtable designated-readtable 'set-macro-character)
278 (set-cat-entry char (if non-terminatingp
279 +char-attr-constituent+
280 +char-attr-terminating-macro+)
281 designated-readtable)
282 (set-cmt-entry char function designated-readtable)
283 t)) ; (ANSI-specified return value)
285 (defun get-macro-character (char &optional (rt-designator *readtable*))
287 "Return the function associated with the specified CHAR which is a macro
288 character, or NIL if there is no such function. As a second value, return
289 T if CHAR is a macro character which is non-terminating, i.e. which can
290 be embedded in a symbol name."
291 (let* ((designated-readtable (or rt-designator *standard-readtable*))
292 ;; the first return value: a FUNCTION if CHAR is a macro
293 ;; character, or NIL otherwise
294 (fun-value (get-raw-cmt-entry char designated-readtable)))
296 ;; NON-TERMINATING-P return value:
298 (or (constituentp char designated-readtable)
299 (not (terminating-macrop char designated-readtable)))
300 ;; ANSI's definition of GET-MACRO-CHARACTER says this
301 ;; value is NIL when CHAR is not a macro character.
302 ;; I.e. this value means not just "non-terminating
303 ;; character?" but "non-terminating macro character?".
307 (defun make-char-dispatch-table ()
310 (defun make-dispatch-macro-character (char &optional
311 (non-terminating-p nil)
314 "Cause CHAR to become a dispatching macro character in readtable (which
315 defaults to the current readtable). If NON-TERMINATING-P, the char will
317 ;; Checks already for standard readtable modification.
318 (set-macro-character char #'read-dispatch-char non-terminating-p rt)
319 (let* ((dalist (dispatch-tables rt))
320 (dtable (cdr (find char dalist :test #'char= :key #'car))))
322 (error "The dispatch character ~S already exists." char))
324 (setf (dispatch-tables rt)
325 (push (cons char (make-char-dispatch-table)) dalist)))))
328 (defun set-dispatch-macro-character (disp-char sub-char function
329 &optional (rt-designator *readtable*))
331 "Cause FUNCTION to be called whenever the reader reads DISP-CHAR
332 followed by SUB-CHAR."
333 ;; Get the dispatch char for macro (error if not there), diddle
334 ;; entry for sub-char.
335 (let* ((sub-char (char-upcase sub-char))
336 (readtable (or rt-designator *standard-readtable*)))
337 (assert-not-standard-readtable readtable 'set-dispatch-macro-character)
338 (when (digit-char-p sub-char)
339 (error "SUB-CHAR must not be a decimal digit: ~S" sub-char))
340 (let ((dpair (find disp-char (dispatch-tables readtable)
341 :test #'char= :key #'car)))
343 (setf (gethash sub-char (cdr dpair)) (coerce function 'function))
344 (error "~S is not a dispatch char." disp-char))))
347 (defun get-dispatch-macro-character (disp-char sub-char
348 &optional (rt-designator *readtable*))
350 "Return the macro character function for SUB-CHAR under DISP-CHAR
351 or NIL if there is no associated function."
352 (let* ((sub-char (char-upcase sub-char))
353 (readtable (or rt-designator *standard-readtable*))
354 (dpair (find disp-char (dispatch-tables readtable)
355 :test #'char= :key #'car)))
357 (values (gethash sub-char (cdr dpair)))
358 (error "~S is not a dispatch char." disp-char))))
361 ;;;; definitions to support internal programming conventions
363 (declaim (inline eofp))
365 (eq char *eof-object*))
367 (defun flush-whitespace (stream)
368 ;; This flushes whitespace chars, returning the last char it read (a
369 ;; non-white one). It always gets an error on end-of-file.
370 (let ((stream (in-synonym-of stream)))
371 (if (ansi-stream-p stream)
372 (prepare-for-fast-read-char stream
373 (do ((attribute-array (character-attribute-array *readtable*))
374 (attribute-hash-table
375 (character-attribute-hash-table *readtable*))
376 (char (fast-read-char t) (fast-read-char t)))
378 (if (typep char 'base-char)
379 (aref attribute-array (char-code char))
380 (gethash char attribute-hash-table
381 +char-attr-constituent+)))
382 +char-attr-whitespace+)
383 (done-with-fast-read-char)
386 (do ((attribute-array (character-attribute-array *readtable*))
387 (attribute-hash-table
388 (character-attribute-hash-table *readtable*))
389 (char (read-char stream nil :eof) (read-char stream nil :eof)))
392 (if (typep char 'base-char)
393 (aref attribute-array (char-code char))
394 (gethash char attribute-hash-table
395 +char-attr-constituent+)))
396 +char-attr-whitespace+))
398 (error 'end-of-file :stream stream)
401 ;;;; temporary initialization hack
403 ;; Install the (easy) standard macro-chars into *READTABLE*.
404 (defun !cold-init-standard-readtable ()
405 (/show0 "entering !cold-init-standard-readtable")
406 ;; All characters get boring defaults in MAKE-READTABLE. Now we
407 ;; override the boring defaults on characters which need more
408 ;; interesting behavior.
409 (flet ((whitespaceify (char)
410 (set-cmt-entry char nil)
411 (set-cat-entry char +char-attr-whitespace+)))
412 (whitespaceify (code-char tab-char-code))
413 (whitespaceify #\Newline)
414 (whitespaceify #\Space)
415 (whitespaceify (code-char form-feed-char-code))
416 (whitespaceify (code-char return-char-code)))
418 (set-cat-entry #\\ +char-attr-single-escape+)
419 (set-cmt-entry #\\ nil)
421 (set-cat-entry #\| +char-attr-multiple-escape+)
422 (set-cmt-entry #\| nil)
424 ;; Easy macro-character definitions are in this source file.
425 (set-macro-character #\" #'read-string)
426 (set-macro-character #\' #'read-quote)
427 (set-macro-character #\( #'read-list)
428 (set-macro-character #\) #'read-right-paren)
429 (set-macro-character #\; #'read-comment)
430 ;; (The hairier macro-character definitions, for #\# and #\`, are
431 ;; defined elsewhere, in their own source files.)
434 (do ((ichar 0 (1+ ichar))
436 ((= ichar base-char-code-limit))
437 (setq char (code-char ichar))
438 (when (constituentp char)
439 (set-cmt-entry char nil)))
441 (/show0 "leaving !cold-init-standard-readtable"))
443 ;;;; implementation of the read buffer
445 (defvar *read-buffer*)
447 (defvar *inch-ptr*) ; *OUCH-PTR* always points to next char to write.
448 (defvar *ouch-ptr*) ; *INCH-PTR* always points to next char to read.
450 (declaim (type index *inch-ptr* *ouch-ptr*))
451 (declaim (type (simple-array character (*)) *read-buffer*))
453 (declaim (inline reset-read-buffer))
454 (defun reset-read-buffer ()
455 ;; Turn *READ-BUFFER* into an empty read buffer.
459 (declaim (inline ouch-read-buffer))
460 (defun ouch-read-buffer (char)
461 ;; When buffer overflow
462 (let ((op *ouch-ptr*))
463 (declare (optimize (sb!c::insert-array-bounds-checks 0)))
464 (when (>= op (length *read-buffer*))
465 ;; Size should be doubled.
467 (setf (elt *read-buffer* op) char)
468 (setq *ouch-ptr* (1+ op))))
470 (defun grow-read-buffer ()
471 (let* ((rbl (length *read-buffer*))
472 (new-length (* 2 rbl))
473 (new-buffer (make-string new-length)))
474 (setq *read-buffer* (replace new-buffer *read-buffer*))))
476 (defun inch-read-buffer ()
477 (if (>= *inch-ptr* *ouch-ptr*)
480 (elt *read-buffer* *inch-ptr*)
483 (declaim (inline unread-buffer))
484 (defun unread-buffer ()
487 (declaim (inline read-unwind-read-buffer))
488 (defun read-unwind-read-buffer ()
489 ;; Keep contents, but make next (INCH..) return first character.
492 (defun read-buffer-to-string ()
493 (subseq *read-buffer* 0 *ouch-ptr*))
495 (defmacro with-read-buffer (() &body body)
496 `(let* ((*read-buffer* (make-string 128))
501 (declaim (inline read-buffer-boundp))
502 (defun read-buffer-boundp ()
503 (and (boundp '*read-buffer*)
505 (boundp '*inch-ptr*)))
507 (defun check-for-recursive-read (stream recursive-p operator-name)
508 (when (and recursive-p (not (read-buffer-boundp)))
511 "~A was invoked with RECURSIVE-P being true outside ~
512 of a recursive read operation."
515 ;;;; READ-PRESERVING-WHITESPACE, READ-DELIMITED-LIST, and READ
517 ;;; an alist for #=, used to keep track of objects with labels assigned that
518 ;;; have been completely read. Each entry is (integer-tag gensym-tag value).
520 ;;; KLUDGE: Should this really be an alist? It seems as though users
521 ;;; could reasonably expect N log N performance for large datasets.
522 ;;; On the other hand, it's probably very very seldom a problem in practice.
523 ;;; On the third hand, it might be just as easy to use a hash table
524 ;;; as an alist, so maybe we should. -- WHN 19991202
525 (defvar *sharp-equal-alist* ())
527 (declaim (special *standard-input*))
529 ;;; Like READ-PRESERVING-WHITESPACE, but doesn't check the read buffer
530 ;;; for being set up properly.
531 (defun %read-preserving-whitespace (stream eof-error-p eof-value recursive-p)
533 ;; a loop for repeating when a macro returns nothing
535 (let ((char (read-char stream eof-error-p *eof-object*)))
536 (cond ((eofp char) (return eof-value))
537 ((whitespace[2]p char))
539 (let* ((macrofun (get-coerced-cmt-entry char *readtable*))
540 (result (multiple-value-list
541 (funcall macrofun stream char))))
542 ;; Repeat if macro returned nothing.
544 (return (unless *read-suppress* (car result)))))))))
545 (let ((*sharp-equal-alist* nil))
547 (%read-preserving-whitespace stream eof-error-p eof-value t)))))
549 ;;; READ-PRESERVING-WHITESPACE behaves just like READ, only it makes
550 ;;; sure to leave terminating whitespace in the stream. (This is a
551 ;;; COMMON-LISP exported symbol.)
552 (defun read-preserving-whitespace (&optional (stream *standard-input*)
557 "Read from STREAM and return the value read, preserving any whitespace
558 that followed the object."
559 (check-for-recursive-read stream recursive-p 'read-preserving-whitespace)
560 (%read-preserving-whitespace stream eof-error-p eof-value recursive-p))
562 ;;; Return NIL or a list with one thing, depending.
564 ;;; for functions that want comments to return so that they can look
565 ;;; past them. We assume CHAR is not whitespace.
566 (defun read-maybe-nothing (stream char)
567 (let ((retval (multiple-value-list
568 (funcall (get-coerced-cmt-entry char *readtable*)
571 (if retval (rplacd retval nil))))
573 (defun read (&optional (stream *standard-input*)
578 "Read the next Lisp value from STREAM, and return it."
579 (check-for-recursive-read stream recursive-p 'read)
580 (let ((result (%read-preserving-whitespace stream eof-error-p eof-value
582 ;; This function generally discards trailing whitespace. If you
583 ;; don't want to discard trailing whitespace, call
584 ;; CL:READ-PRESERVING-WHITESPACE instead.
585 (unless (or (eql result eof-value) recursive-p)
586 (let ((next-char (read-char stream nil nil)))
587 (unless (or (null next-char)
588 (whitespace[2]p next-char))
589 (unread-char next-char stream))))
592 ;;; (This is a COMMON-LISP exported symbol.)
593 (defun read-delimited-list (endchar &optional
594 (input-stream *standard-input*)
597 "Read Lisp values from INPUT-STREAM until the next character after a
598 value's representation is ENDCHAR, and return the objects as a list."
599 (check-for-recursive-read input-stream recursive-p 'read-delimited-list)
600 (flet ((%read-delimited-list (endchar input-stream)
601 (do ((char (flush-whitespace input-stream)
602 (flush-whitespace input-stream))
604 ((char= char endchar)
605 (unless *read-suppress* (nreverse retlist)))
606 (setq retlist (nconc (read-maybe-nothing input-stream char)
608 (declare (inline %read-delimited-list))
610 (%read-delimited-list endchar input-stream)
612 (%read-delimited-list endchar input-stream)))))
614 ;;;; basic readmacro definitions
616 ;;;; Some large, hairy subsets of readmacro definitions (backquotes
617 ;;;; and sharp macros) are not here, but in their own source files.
619 (defun read-quote (stream ignore)
620 (declare (ignore ignore))
621 (list 'quote (read stream t nil t)))
623 (defun read-comment (stream ignore)
624 (declare (ignore ignore))
626 ((character-decoding-error
627 #'(lambda (decoding-error)
628 (declare (ignorable decoding-error))
630 'sb!kernel::character-decoding-error-in-macro-char-comment
631 :position (file-position stream) :stream stream)
632 (invoke-restart 'attempt-resync))))
633 (let ((stream (in-synonym-of stream)))
634 (if (ansi-stream-p stream)
635 (prepare-for-fast-read-char stream
636 (do ((char (fast-read-char nil nil)
637 (fast-read-char nil nil)))
638 ((or (not char) (char= char #\newline))
639 (done-with-fast-read-char))))
641 (do ((char (read-char stream nil :eof) (read-char stream nil :eof)))
642 ((or (eq char :eof) (char= char #\newline)))))))
643 ;; Don't return anything.
646 (defun read-list (stream ignore)
647 (declare (ignore ignore))
648 (let* ((thelist (list nil))
650 (do ((firstchar (flush-whitespace stream) (flush-whitespace stream)))
651 ((char= firstchar #\) ) (cdr thelist))
652 (when (char= firstchar #\.)
653 (let ((nextchar (read-char stream t)))
654 (cond ((token-delimiterp nextchar)
655 (cond ((eq listtail thelist)
656 (unless *read-suppress*
659 "Nothing appears before . in list.")))
660 ((whitespace[2]p nextchar)
661 (setq nextchar (flush-whitespace stream))))
663 ;; Return list containing last thing.
664 (car (read-after-dot stream nextchar)))
665 (return (cdr thelist)))
666 ;; Put back NEXTCHAR so that we can read it normally.
667 (t (unread-char nextchar stream)))))
668 ;; Next thing is not an isolated dot.
669 (let ((listobj (read-maybe-nothing stream firstchar)))
670 ;; allows the possibility that a comment was read
672 (rplacd listtail listobj)
673 (setq listtail listobj))))))
675 (defun read-after-dot (stream firstchar)
676 ;; FIRSTCHAR is non-whitespace!
678 (do ((char firstchar (flush-whitespace stream)))
681 (return-from read-after-dot nil)
682 (simple-reader-error stream "Nothing appears after . in list.")))
683 ;; See whether there's something there.
684 (setq lastobj (read-maybe-nothing stream char))
685 (when lastobj (return t)))
686 ;; At least one thing appears after the dot.
687 ;; Check for more than one thing following dot.
688 (do ((lastchar (flush-whitespace stream)
689 (flush-whitespace stream)))
690 ((char= lastchar #\) ) lastobj) ;success!
691 ;; Try reading virtual whitespace.
692 (if (and (read-maybe-nothing stream lastchar)
693 (not *read-suppress*))
694 (simple-reader-error stream
695 "More than one object follows . in list.")))))
697 (defun read-string (stream closech)
698 ;; This accumulates chars until it sees same char that invoked it.
699 ;; For a very long string, this could end up bloating the read buffer.
701 (let ((stream (in-synonym-of stream)))
702 (if (ansi-stream-p stream)
703 (prepare-for-fast-read-char stream
704 (do ((char (fast-read-char t) (fast-read-char t)))
705 ((char= char closech)
706 (done-with-fast-read-char))
707 (if (single-escape-p char) (setq char (fast-read-char t)))
708 (ouch-read-buffer char)))
710 (do ((char (read-char stream nil :eof) (read-char stream nil :eof)))
711 ((or (eq char :eof) (char= char closech))
713 (error 'end-of-file :stream stream)))
714 (when (single-escape-p char)
715 (setq char (read-char stream nil :eof))
717 (error 'end-of-file :stream stream)))
718 (ouch-read-buffer char))))
719 (read-buffer-to-string))
721 (defun read-right-paren (stream ignore)
722 (declare (ignore ignore))
723 (simple-reader-error stream "unmatched close parenthesis"))
725 ;;; Read from the stream up to the next delimiter. Leave the resulting
726 ;;; token in *READ-BUFFER*, and return two values:
727 ;;; -- a list of the escaped character positions, and
728 ;;; -- The position of the first package delimiter (or NIL).
729 (defun internal-read-extended-token (stream firstchar escape-firstchar)
732 (when escape-firstchar
733 (push *ouch-ptr* escapes)
734 (ouch-read-buffer firstchar)
735 (setq firstchar (read-char stream nil *eof-object*)))
736 (do ((char firstchar (read-char stream nil *eof-object*))
738 ((cond ((eofp char) t)
739 ((token-delimiterp char)
740 (unread-char char stream)
743 (values escapes colon))
744 (cond ((single-escape-p char)
745 ;; It can't be a number, even if it's 1\23.
746 ;; Read next char here, so it won't be casified.
747 (push *ouch-ptr* escapes)
748 (let ((nextchar (read-char stream nil *eof-object*)))
750 (reader-eof-error stream "after escape character")
751 (ouch-read-buffer nextchar))))
752 ((multiple-escape-p char)
753 ;; Read to next multiple-escape, escaping single chars
756 (let ((ch (read-char stream nil *eof-object*)))
759 (reader-eof-error stream "inside extended token"))
760 ((multiple-escape-p ch) (return))
761 ((single-escape-p ch)
762 (let ((nextchar (read-char stream nil *eof-object*)))
763 (cond ((eofp nextchar)
764 (reader-eof-error stream "after escape character"))
766 (push *ouch-ptr* escapes)
767 (ouch-read-buffer nextchar)))))
769 (push *ouch-ptr* escapes)
770 (ouch-read-buffer ch))))))
772 (when (and (constituentp char)
773 (eql (get-constituent-trait char)
774 +char-attr-package-delimiter+)
776 (setq colon *ouch-ptr*))
777 (ouch-read-buffer char))))))
779 ;;;; character classes
781 ;;; Return the character class for CHAR.
783 ;;; FIXME: why aren't these ATT-getting forms using GET-CAT-ENTRY?
784 ;;; Because we've cached the readtable tables?
785 (defmacro char-class (char attarray atthash)
786 `(let ((att (if (typep ,char 'base-char)
787 (aref ,attarray (char-code ,char))
788 (gethash ,char ,atthash +char-attr-constituent+))))
789 (declare (fixnum att))
791 ((<= att +char-attr-terminating-macro+) +char-attr-delimiter+)
792 ((< att +char-attr-constituent+) att)
793 (t (setf att (get-constituent-trait ,char))
794 (if (= att +char-attr-invalid+)
795 (simple-reader-error stream "invalid constituent")
798 ;;; Return the character class for CHAR, which might be part of a
800 (defmacro char-class2 (char attarray atthash)
801 `(let ((att (if (typep ,char 'base-char)
802 (aref ,attarray (char-code ,char))
803 (gethash ,char ,atthash +char-attr-constituent+))))
804 (declare (fixnum att))
806 ((<= att +char-attr-terminating-macro+) +char-attr-delimiter+)
807 ((< att +char-attr-constituent+) att)
808 (t (setf att (get-constituent-trait ,char))
810 ((digit-char-p ,char *read-base*) +char-attr-constituent-digit+)
811 ((= att +char-attr-constituent-digit+) +char-attr-constituent+)
812 ((= att +char-attr-invalid+)
813 (simple-reader-error stream "invalid constituent"))
816 ;;; Return the character class for a char which might be part of a
817 ;;; rational or floating number. (Assume that it is a digit if it
819 (defmacro char-class3 (char attarray atthash)
820 `(let ((att (if (typep ,char 'base-char)
821 (aref ,attarray (char-code ,char))
822 (gethash ,char ,atthash +char-attr-constituent+))))
823 (declare (fixnum att))
825 ((<= att +char-attr-terminating-macro+) +char-attr-delimiter+)
826 ((< att +char-attr-constituent+) att)
827 (t (setf att (get-constituent-trait ,char))
828 (when possibly-rational
829 (setq possibly-rational
830 (or (digit-char-p ,char *read-base*)
831 (= att +char-attr-constituent-slash+))))
834 (or (digit-char-p ,char 10)
835 (= att +char-attr-constituent-dot+))))
837 ((digit-char-p ,char (max *read-base* 10))
838 (if (digit-char-p ,char *read-base*)
839 (if (= att +char-attr-constituent-expt+)
840 +char-attr-constituent-digit-or-expt+
841 +char-attr-constituent-digit+)
842 +char-attr-constituent-decimal-digit+))
843 ((= att +char-attr-invalid+)
844 (simple-reader-error stream "invalid constituent"))
849 (defvar *read-suppress* nil
851 "Suppress most interpreting in the reader when T.")
853 (defvar *read-base* 10
855 "the radix that Lisp reads numbers in")
856 (declaim (type (integer 2 36) *read-base*))
858 ;;; Modify the read buffer according to READTABLE-CASE, ignoring
859 ;;; ESCAPES. ESCAPES is a list of the escaped indices, in reverse
861 (defun casify-read-buffer (escapes)
862 (let ((case (readtable-case *readtable*)))
864 ((and (null escapes) (eq case :upcase))
865 ;; Pull the special variable access out of the loop.
866 (let ((buffer *read-buffer*))
867 (dotimes (i *ouch-ptr*)
868 (declare (optimize (sb!c::insert-array-bounds-checks 0)))
869 (setf (schar buffer i) (char-upcase (schar buffer i))))))
870 ((eq case :preserve))
872 (macrolet ((skip-esc (&body body)
873 `(do ((i (1- *ouch-ptr*) (1- i))
874 (buffer *read-buffer*)
878 (optimize (sb!c::insert-array-bounds-checks 0)))
879 (when (or (null escapes)
880 (let ((esc (first escapes)))
881 (declare (fixnum esc))
887 (let ((ch (schar buffer i)))
890 (skip-esc (setf (schar buffer i) (char-downcase ch))))
892 (skip-esc (setf (schar buffer i) (char-upcase ch)))))
895 (:downcase (lower-em))
900 (when (both-case-p ch)
901 (if (upper-case-p ch)
903 (setq all-upper nil))))
904 (cond (all-lower (raise-em))
905 (all-upper (lower-em))))))))))))
907 (defun read-token (stream firstchar)
909 "This function is just an fsm that recognizes numbers and symbols."
910 ;; Check explicitly whether FIRSTCHAR has an entry for
911 ;; NON-TERMINATING in CHARACTER-ATTRIBUTE-TABLE and
912 ;; READ-DOT-NUMBER-SYMBOL in CMT. Report an error if these are
913 ;; violated. (If we called this, we want something that is a
914 ;; legitimate token!) Read in the longest possible string satisfying
915 ;; the Backus-Naur form for "unqualified-token". Leave the result in
916 ;; the *READ-BUFFER*. Return next char after token (last char read).
917 (when *read-suppress*
918 (internal-read-extended-token stream firstchar nil)
919 (return-from read-token nil))
920 (let ((attribute-array (character-attribute-array *readtable*))
921 (attribute-hash-table (character-attribute-hash-table *readtable*))
922 (package-designator nil)
924 (possibly-rational t)
925 (seen-digit-or-expt nil)
927 (was-possibly-float nil)
929 (seen-multiple-escapes nil))
931 (prog ((char firstchar))
932 (case (char-class3 char attribute-array attribute-hash-table)
933 (#.+char-attr-constituent-sign+ (go SIGN))
934 (#.+char-attr-constituent-digit+ (go LEFTDIGIT))
935 (#.+char-attr-constituent-digit-or-expt+
936 (setq seen-digit-or-expt t)
938 (#.+char-attr-constituent-decimal-digit+ (go LEFTDECIMALDIGIT))
939 (#.+char-attr-constituent-dot+ (go FRONTDOT))
940 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
941 (#.+char-attr-package-delimiter+ (go COLON))
942 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
943 (#.+char-attr-invalid+ (simple-reader-error stream
944 "invalid constituent"))
945 ;; can't have eof, whitespace, or terminating macro as first char!
948 (ouch-read-buffer char)
949 (setq char (read-char stream nil nil))
950 (unless char (go RETURN-SYMBOL))
951 (setq possibly-rational t
953 (case (char-class3 char attribute-array attribute-hash-table)
954 (#.+char-attr-constituent-digit+ (go LEFTDIGIT))
955 (#.+char-attr-constituent-digit-or-expt+
956 (setq seen-digit-or-expt t)
958 (#.+char-attr-constituent-decimal-digit+ (go LEFTDECIMALDIGIT))
959 (#.+char-attr-constituent-dot+ (go SIGNDOT))
960 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
961 (#.+char-attr-package-delimiter+ (go COLON))
962 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
963 (#.+char-attr-delimiter+ (unread-char char stream) (go RETURN-SYMBOL))
965 LEFTDIGIT ; saw "[sign] {digit}+"
966 (ouch-read-buffer char)
967 (setq char (read-char stream nil nil))
968 (unless char (return (make-integer)))
969 (setq was-possibly-float possibly-float)
970 (case (char-class3 char attribute-array attribute-hash-table)
971 (#.+char-attr-constituent-digit+ (go LEFTDIGIT))
972 (#.+char-attr-constituent-decimal-digit+ (if possibly-float
973 (go LEFTDECIMALDIGIT)
975 (#.+char-attr-constituent-dot+ (if possibly-float
978 (#.+char-attr-constituent-digit-or-expt+
979 (if (or seen-digit-or-expt (not was-possibly-float))
980 (progn (setq seen-digit-or-expt t) (go LEFTDIGIT))
981 (progn (setq seen-digit-or-expt t) (go LEFTDIGIT-OR-EXPT))))
982 (#.+char-attr-constituent-expt+
983 (if was-possibly-float
986 (#.+char-attr-constituent-slash+ (if possibly-rational
989 (#.+char-attr-delimiter+ (unread-char char stream)
990 (return (make-integer)))
991 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
992 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
993 (#.+char-attr-package-delimiter+ (go COLON))
996 (ouch-read-buffer char)
997 (setq char (read-char stream nil nil))
998 (unless char (return (make-integer)))
999 (case (char-class3 char attribute-array attribute-hash-table)
1000 (#.+char-attr-constituent-digit+ (go LEFTDIGIT))
1001 (#.+char-attr-constituent-decimal-digit+ (bug "impossible!"))
1002 (#.+char-attr-constituent-dot+ (go SYMBOL))
1003 (#.+char-attr-constituent-digit-or-expt+ (go LEFTDIGIT))
1004 (#.+char-attr-constituent-expt+ (go SYMBOL))
1005 (#.+char-attr-constituent-sign+ (go EXPTSIGN))
1006 (#.+char-attr-constituent-slash+ (if possibly-rational
1009 (#.+char-attr-delimiter+ (unread-char char stream)
1010 (return (make-integer)))
1011 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1012 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1013 (#.+char-attr-package-delimiter+ (go COLON))
1015 LEFTDECIMALDIGIT ; saw "[sign] {decimal-digit}+"
1016 (aver possibly-float)
1017 (ouch-read-buffer char)
1018 (setq char (read-char stream nil nil))
1019 (unless char (go RETURN-SYMBOL))
1020 (case (char-class char attribute-array attribute-hash-table)
1021 (#.+char-attr-constituent-digit+ (go LEFTDECIMALDIGIT))
1022 (#.+char-attr-constituent-dot+ (go MIDDLEDOT))
1023 (#.+char-attr-constituent-expt+ (go EXPONENT))
1024 (#.+char-attr-constituent-slash+ (aver (not possibly-rational))
1026 (#.+char-attr-delimiter+ (unread-char char stream)
1028 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1029 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1030 (#.+char-attr-package-delimiter+ (go COLON))
1032 MIDDLEDOT ; saw "[sign] {digit}+ dot"
1033 (ouch-read-buffer char)
1034 (setq char (read-char stream nil nil))
1035 (unless char (return (let ((*read-base* 10))
1037 (case (char-class char attribute-array attribute-hash-table)
1038 (#.+char-attr-constituent-digit+ (go RIGHTDIGIT))
1039 (#.+char-attr-constituent-expt+ (go EXPONENT))
1040 (#.+char-attr-delimiter+
1041 (unread-char char stream)
1042 (return (let ((*read-base* 10))
1044 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1045 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1046 (#.+char-attr-package-delimiter+ (go COLON))
1048 RIGHTDIGIT ; saw "[sign] {decimal-digit}* dot {digit}+"
1049 (ouch-read-buffer char)
1050 (setq char (read-char stream nil nil))
1051 (unless char (return (make-float stream)))
1052 (case (char-class char attribute-array attribute-hash-table)
1053 (#.+char-attr-constituent-digit+ (go RIGHTDIGIT))
1054 (#.+char-attr-constituent-expt+ (go EXPONENT))
1055 (#.+char-attr-delimiter+
1056 (unread-char char stream)
1057 (return (make-float stream)))
1058 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1059 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1060 (#.+char-attr-package-delimiter+ (go COLON))
1062 SIGNDOT ; saw "[sign] dot"
1063 (ouch-read-buffer char)
1064 (setq char (read-char stream nil nil))
1065 (unless char (go RETURN-SYMBOL))
1066 (case (char-class char attribute-array attribute-hash-table)
1067 (#.+char-attr-constituent-digit+ (go RIGHTDIGIT))
1068 (#.+char-attr-delimiter+ (unread-char char stream) (go RETURN-SYMBOL))
1069 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1070 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1072 FRONTDOT ; saw "dot"
1073 (ouch-read-buffer char)
1074 (setq char (read-char stream nil nil))
1075 (unless char (simple-reader-error stream "dot context error"))
1076 (case (char-class char attribute-array attribute-hash-table)
1077 (#.+char-attr-constituent-digit+ (go RIGHTDIGIT))
1078 (#.+char-attr-constituent-dot+ (go DOTS))
1079 (#.+char-attr-delimiter+ (simple-reader-error stream
1080 "dot context error"))
1081 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1082 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1083 (#.+char-attr-package-delimiter+ (go COLON))
1086 (ouch-read-buffer char)
1087 (setq char (read-char stream nil nil))
1088 (unless char (go RETURN-SYMBOL))
1089 (setq possibly-float t)
1090 (case (char-class char attribute-array attribute-hash-table)
1091 (#.+char-attr-constituent-sign+ (go EXPTSIGN))
1092 (#.+char-attr-constituent-digit+ (go EXPTDIGIT))
1093 (#.+char-attr-delimiter+ (unread-char char stream) (go RETURN-SYMBOL))
1094 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1095 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1096 (#.+char-attr-package-delimiter+ (go COLON))
1098 EXPTSIGN ; got to EXPONENT, and saw a sign character
1099 (ouch-read-buffer char)
1100 (setq char (read-char stream nil nil))
1101 (unless char (go RETURN-SYMBOL))
1102 (case (char-class char attribute-array attribute-hash-table)
1103 (#.+char-attr-constituent-digit+ (go EXPTDIGIT))
1104 (#.+char-attr-delimiter+ (unread-char char stream) (go RETURN-SYMBOL))
1105 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1106 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1107 (#.+char-attr-package-delimiter+ (go COLON))
1109 EXPTDIGIT ; got to EXPONENT, saw "[sign] {digit}+"
1110 (ouch-read-buffer char)
1111 (setq char (read-char stream nil nil))
1112 (unless char (return (make-float stream)))
1113 (case (char-class char attribute-array attribute-hash-table)
1114 (#.+char-attr-constituent-digit+ (go EXPTDIGIT))
1115 (#.+char-attr-delimiter+
1116 (unread-char char stream)
1117 (return (make-float stream)))
1118 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1119 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1120 (#.+char-attr-package-delimiter+ (go COLON))
1122 RATIO ; saw "[sign] {digit}+ slash"
1123 (ouch-read-buffer char)
1124 (setq char (read-char stream nil nil))
1125 (unless char (go RETURN-SYMBOL))
1126 (case (char-class2 char attribute-array attribute-hash-table)
1127 (#.+char-attr-constituent-digit+ (go RATIODIGIT))
1128 (#.+char-attr-delimiter+ (unread-char char stream) (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))
1133 RATIODIGIT ; saw "[sign] {digit}+ slash {digit}+"
1134 (ouch-read-buffer char)
1135 (setq char (read-char stream nil nil))
1136 (unless char (return (make-ratio stream)))
1137 (case (char-class2 char attribute-array attribute-hash-table)
1138 (#.+char-attr-constituent-digit+ (go RATIODIGIT))
1139 (#.+char-attr-delimiter+
1140 (unread-char char stream)
1141 (return (make-ratio stream)))
1142 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1143 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1144 (#.+char-attr-package-delimiter+ (go COLON))
1146 DOTS ; saw "dot {dot}+"
1147 (ouch-read-buffer char)
1148 (setq char (read-char stream nil nil))
1149 (unless char (simple-reader-error stream "too many dots"))
1150 (case (char-class char attribute-array attribute-hash-table)
1151 (#.+char-attr-constituent-dot+ (go DOTS))
1152 (#.+char-attr-delimiter+
1153 (unread-char char stream)
1154 (simple-reader-error stream "too many dots"))
1155 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1156 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1157 (#.+char-attr-package-delimiter+ (go COLON))
1159 SYMBOL ; not a dot, dots, or number
1160 (let ((stream (in-synonym-of stream)))
1161 (if (ansi-stream-p stream)
1162 (prepare-for-fast-read-char stream
1165 (ouch-read-buffer char)
1166 (setq char (fast-read-char nil nil))
1167 (unless char (go RETURN-SYMBOL))
1168 (case (char-class char attribute-array attribute-hash-table)
1169 (#.+char-attr-single-escape+ (done-with-fast-read-char)
1171 (#.+char-attr-delimiter+ (done-with-fast-read-char)
1172 (unread-char char stream)
1174 (#.+char-attr-multiple-escape+ (done-with-fast-read-char)
1176 (#.+char-attr-package-delimiter+ (done-with-fast-read-char)
1178 (t (go SYMBOL-LOOP)))))
1182 (ouch-read-buffer char)
1183 (setq char (read-char stream nil :eof))
1184 (when (eq char :eof) (go RETURN-SYMBOL))
1185 (case (char-class char attribute-array attribute-hash-table)
1186 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1187 (#.+char-attr-delimiter+ (unread-char char stream)
1189 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1190 (#.+char-attr-package-delimiter+ (go COLON))
1191 (t (go SYMBOL-LOOP))))))
1192 SINGLE-ESCAPE ; saw a single-escape
1193 ;; Don't put the escape character in the read buffer.
1194 ;; READ-NEXT CHAR, put in buffer (no case conversion).
1195 (let ((nextchar (read-char stream nil nil)))
1197 (reader-eof-error stream "after single-escape character"))
1198 (push *ouch-ptr* escapes)
1199 (ouch-read-buffer nextchar))
1200 (setq char (read-char stream nil nil))
1201 (unless char (go RETURN-SYMBOL))
1202 (case (char-class char attribute-array attribute-hash-table)
1203 (#.+char-attr-delimiter+ (unread-char char stream) (go RETURN-SYMBOL))
1204 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1205 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1206 (#.+char-attr-package-delimiter+ (go COLON))
1209 (setq seen-multiple-escapes t)
1210 (do ((char (read-char stream t) (read-char stream t)))
1211 ((multiple-escape-p char))
1212 (if (single-escape-p char) (setq char (read-char stream t)))
1213 (push *ouch-ptr* escapes)
1214 (ouch-read-buffer char))
1215 (setq char (read-char stream nil nil))
1216 (unless char (go RETURN-SYMBOL))
1217 (case (char-class char attribute-array attribute-hash-table)
1218 (#.+char-attr-delimiter+ (unread-char char stream) (go RETURN-SYMBOL))
1219 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1220 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1221 (#.+char-attr-package-delimiter+ (go COLON))
1224 (casify-read-buffer escapes)
1225 (unless (zerop colons)
1226 (simple-reader-error stream
1227 "too many colons in ~S"
1228 (read-buffer-to-string)))
1230 (setq package-designator
1231 (if (plusp *ouch-ptr*)
1232 ;; FIXME: It seems inefficient to cons up a package
1233 ;; designator string every time we read a symbol with an
1234 ;; explicit package prefix. Perhaps we could implement
1235 ;; a FIND-PACKAGE* function analogous to INTERN*
1237 (read-buffer-to-string)
1238 (if seen-multiple-escapes
1239 (read-buffer-to-string)
1240 *keyword-package*)))
1243 (setq char (read-char stream nil nil))
1244 (unless char (reader-eof-error stream "after reading a colon"))
1245 (case (char-class char attribute-array attribute-hash-table)
1246 (#.+char-attr-delimiter+
1247 (unread-char char stream)
1248 (simple-reader-error stream
1249 "illegal terminating character after a colon: ~S"
1251 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1252 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1253 (#.+char-attr-package-delimiter+ (go INTERN))
1257 (setq char (read-char stream nil nil))
1259 (reader-eof-error stream "after reading a colon"))
1260 (case (char-class char attribute-array attribute-hash-table)
1261 (#.+char-attr-delimiter+
1262 (unread-char char stream)
1263 (simple-reader-error stream
1264 "illegal terminating character after a colon: ~S"
1266 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1267 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1268 (#.+char-attr-package-delimiter+
1269 (simple-reader-error stream
1270 "too many colons after ~S name"
1271 package-designator))
1274 (casify-read-buffer escapes)
1275 (let ((found (if package-designator
1276 (find-package package-designator)
1279 (error 'simple-reader-package-error :stream stream
1280 :format-arguments (list package-designator)
1281 :format-control "package ~S not found"))
1283 (if (or (zerop colons) (= colons 2) (eq found *keyword-package*))
1284 (return (intern* *read-buffer* *ouch-ptr* found))
1285 (multiple-value-bind (symbol test)
1286 (find-symbol* *read-buffer* *ouch-ptr* found)
1287 (when (eq test :external) (return symbol))
1288 (let ((name (read-buffer-to-string)))
1289 (with-simple-restart (continue "Use symbol anyway.")
1290 (error 'simple-reader-package-error :stream stream
1291 :format-arguments (list name (package-name found))
1294 "The symbol ~S is not external in the ~A package."
1295 "Symbol ~S not found in the ~A package.")))
1296 (return (intern name found)))))))))
1298 ;;; for semi-external use:
1300 ;;; For semi-external use: Return 3 values: the string for the token,
1301 ;;; a flag for whether there was an escape char, and the position of
1302 ;;; any package delimiter.
1303 (defun read-extended-token (stream &optional (*readtable* *readtable*))
1304 (let ((first-char (read-char stream nil nil t)))
1306 (multiple-value-bind (escapes colon)
1307 (internal-read-extended-token stream first-char nil)
1308 (casify-read-buffer escapes)
1309 (values (read-buffer-to-string) (not (null escapes)) colon)))
1311 (values "" nil nil)))))
1313 ;;; for semi-external use:
1315 ;;; Read an extended token with the first character escaped. Return
1316 ;;; the string for the token.
1317 (defun read-extended-token-escaped (stream &optional (*readtable* *readtable*))
1318 (let ((first-char (read-char stream nil nil)))
1320 (let ((escapes (internal-read-extended-token stream first-char t)))
1321 (casify-read-buffer escapes)
1322 (read-buffer-to-string)))
1324 (reader-eof-error stream "after escape")))))
1326 ;;;; number-reading functions
1328 (defmacro digit* nil
1329 `(do ((ch char (inch-read-buffer)))
1330 ((or (eofp ch) (not (digit-char-p ch))) (setq char ch))
1331 ;; Report if at least one digit is seen.
1332 (setq one-digit t)))
1334 (defmacro exponent-letterp (letter)
1335 `(memq ,letter '(#\E #\S #\F #\L #\D #\e #\s #\f #\l #\d)))
1337 ;;; FIXME: It would be cleaner to have these generated automatically
1338 ;;; by compile-time code instead of having them hand-created like
1339 ;;; this. The !COLD-INIT-INTEGER-READER code below should be resurrected
1341 (defvar *integer-reader-safe-digits*
1343 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)
1345 "the mapping of base to 'safe' number of digits to read for a fixnum")
1346 (defvar *integer-reader-base-power*
1348 67108864 129140163 67108864 48828125 60466176 40353607
1349 16777216 43046721 100000000 19487171 35831808 62748517 105413504 11390625
1350 16777216 24137569 34012224 47045881 64000000 85766121 113379904 6436343
1351 7962624 9765625 11881376 14348907 17210368 20511149 24300000 28629151
1352 33554432 39135393 45435424 52521875 60466176)
1354 "the largest fixnum power of the base for MAKE-INTEGER")
1355 (declaim (simple-vector *integer-reader-safe-digits*
1356 *integer-reader-base-power*))
1358 (defun !cold-init-integer-reader ()
1359 (do ((base 2 (1+ base)))
1362 (do ((fix (truncate most-positive-fixnum base)
1363 (truncate fix base))
1364 (digits 0 (1+ digits)))
1365 ((zerop fix) digits))))
1366 (setf (aref *integer-reader-safe-digits* base)
1368 (aref *integer-reader-base-power* base)
1369 (expt base digits)))))
1372 (defun make-integer ()
1374 "Minimizes bignum-fixnum multiplies by reading a 'safe' number of digits,
1375 then multiplying by a power of the base and adding."
1376 (let* ((base *read-base*)
1377 (digits-per (aref *integer-reader-safe-digits* base))
1378 (base-power (aref *integer-reader-base-power* base))
1381 (declare (type index digits-per base-power))
1382 (read-unwind-read-buffer)
1383 (let ((char (inch-read-buffer)))
1384 (cond ((char= char #\-)
1387 (t (unread-buffer))))
1390 (declare (type index num))
1391 (dotimes (digit digits-per)
1392 (let* ((ch (inch-read-buffer)))
1393 (cond ((or (eofp ch) (char= ch #\.))
1394 (return-from make-integer
1396 (if (zerop number) num
1398 (expt base digit))))))
1399 (if negativep (- res) res))))
1400 (t (setq num (+ (digit-char-p ch base)
1401 (the index (* num base))))))))
1402 (setq number (+ num (* number base-power)))))))
1404 (defun make-float (stream)
1405 ;; Assume that the contents of *read-buffer* are a legal float, with nothing
1407 (read-unwind-read-buffer)
1408 (let ((negative-fraction nil)
1411 (negative-exponent nil)
1414 (char (inch-read-buffer)))
1415 (if (cond ((char= char #\+) t)
1416 ((char= char #\-) (setq negative-fraction t)))
1418 (setq char (inch-read-buffer)))
1419 ;; Read digits before the dot.
1420 (do* ((ch char (inch-read-buffer))
1421 (dig (digit-char-p ch) (digit-char-p ch)))
1422 ((not dig) (setq char ch))
1423 (setq number (+ (* number 10) dig)))
1424 ;; Deal with the dot, if it's there.
1425 (when (char= char #\.)
1426 (setq char (inch-read-buffer))
1427 ;; Read digits after the dot.
1428 (do* ((ch char (inch-read-buffer))
1429 (dig (and (not (eofp ch)) (digit-char-p ch))
1430 (and (not (eofp ch)) (digit-char-p ch))))
1431 ((not dig) (setq char ch))
1432 (setq divisor (* divisor 10))
1433 (setq number (+ (* number 10) dig))))
1434 ;; Is there an exponent letter?
1436 ;; If not, we've read the whole number.
1437 (let ((num (make-float-aux number divisor
1438 *read-default-float-format*
1440 (return-from make-float (if negative-fraction (- num) num))))
1441 ((exponent-letterp char)
1442 (setq float-char char)
1444 (setq char (inch-read-buffer))
1445 ;; Check leading sign.
1446 (if (cond ((char= char #\+) t)
1447 ((char= char #\-) (setq negative-exponent t)))
1449 (setq char (inch-read-buffer)))
1450 ;; Read digits for exponent.
1451 (do* ((ch char (inch-read-buffer))
1452 (dig (and (not (eofp ch)) (digit-char-p ch))
1453 (and (not (eofp ch)) (digit-char-p ch))))
1455 (setq exponent (if negative-exponent (- exponent) exponent)))
1456 (setq exponent (+ (* exponent 10) dig)))
1457 ;; Generate and return the float, depending on FLOAT-CHAR:
1458 (let* ((float-format (case (char-upcase float-char)
1459 (#\E *read-default-float-format*)
1464 (result (make-float-aux (* (expt 10 exponent) number)
1465 divisor float-format stream)))
1466 (return-from make-float
1467 (if negative-fraction (- result) result))))
1468 (t (bug "bad fallthrough in floating point reader")))))
1470 (defun make-float-aux (number divisor float-format stream)
1472 (coerce (/ number divisor) float-format)
1474 (error 'reader-impossible-number-error
1475 :error c :stream stream
1476 :format-control "failed to build float"))))
1478 (defun make-ratio (stream)
1479 ;; Assume *READ-BUFFER* contains a legal ratio. Build the number from
1482 ;; Look for optional "+" or "-".
1483 (let ((numerator 0) (denominator 0) (char ()) (negative-number nil))
1484 (read-unwind-read-buffer)
1485 (setq char (inch-read-buffer))
1486 (cond ((char= char #\+)
1487 (setq char (inch-read-buffer)))
1489 (setq char (inch-read-buffer))
1490 (setq negative-number t)))
1492 (do* ((ch char (inch-read-buffer))
1493 (dig (digit-char-p ch *read-base*)
1494 (digit-char-p ch *read-base*)))
1496 (setq numerator (+ (* numerator *read-base*) dig)))
1498 (do* ((ch (inch-read-buffer) (inch-read-buffer))
1500 ((or (eofp ch) (not (setq dig (digit-char-p ch *read-base*)))))
1501 (setq denominator (+ (* denominator *read-base*) dig)))
1502 (let ((num (handler-case
1503 (/ numerator denominator)
1504 (arithmetic-error (c)
1505 (error 'reader-impossible-number-error
1506 :error c :stream stream
1507 :format-control "failed to build ratio")))))
1508 (if negative-number (- num) num))))
1510 ;;;; General reader for dispatch macros
1512 (defun dispatch-char-error (stream sub-char ignore)
1513 (declare (ignore ignore))
1516 (simple-reader-error stream
1517 "no dispatch function defined for ~S"
1520 (defun read-dispatch-char (stream char)
1521 ;; Read some digits.
1525 (do* ((ch (read-char stream nil *eof-object*)
1526 (read-char stream nil *eof-object*))
1529 (not (setq dig (digit-char-p ch))))
1530 ;; Take care of the extra char.
1532 (reader-eof-error stream "inside dispatch character")
1533 (setq sub-char (char-upcase ch))))
1535 (setq numarg (+ (* numarg 10) dig)))
1536 ;; Look up the function and call it.
1537 (let ((dpair (find char (dispatch-tables *readtable*)
1538 :test #'char= :key #'car)))
1540 (funcall (the function
1541 (gethash sub-char (cdr dpair) #'dispatch-char-error))
1542 stream sub-char (if numargp numarg nil))
1543 (simple-reader-error stream
1544 "no dispatch table for dispatch char")))))
1546 ;;;; READ-FROM-STRING
1548 (defun maybe-note-read-from-string-signature-issue (eof-error-p)
1549 ;; The interface is so unintuitive that we explicitly check for the common
1551 (when (member eof-error-p '(:start :end :preserve-whitespace))
1552 (style-warn "~@<~S as EOF-ERROR-P argument to ~S: probable error. ~
1553 Two optional arguments must be provided before the ~
1554 first keyword argument.~:@>"
1555 eof-error-p 'read-from-string)
1558 (declaim (ftype (sfunction (string t t index (or null index) t) (values t index))
1560 (defun %read-from-string (string eof-error-p eof-value start end preserve-whitespace)
1561 (with-array-data ((string string :offset-var offset)
1564 :check-fill-pointer t)
1565 (let ((stream (make-string-input-stream string start end)))
1566 (values (if preserve-whitespace
1567 (%read-preserving-whitespace stream eof-error-p eof-value nil)
1568 (read stream eof-error-p eof-value))
1569 (- (string-input-stream-current stream) offset)))))
1571 (defun read-from-string (string &optional (eof-error-p t) eof-value
1572 &key (start 0) end preserve-whitespace)
1574 "The characters of string are successively given to the lisp reader
1575 and the lisp object built by the reader is returned. Macro chars
1577 (declare (string string))
1578 (maybe-note-read-from-string-signature-issue eof-error-p)
1579 (%read-from-string string eof-error-p eof-value start end preserve-whitespace))
1581 (define-compiler-macro read-from-string (&whole form string &rest args)
1582 ;; Check this at compile-time, and rewrite it so we're silent at runtime.
1583 (destructuring-bind (&optional (eof-error-p t) eof-value &rest keys)
1585 (cond ((maybe-note-read-from-string-signature-issue eof-error-p)
1586 `(read-from-string ,string t ,eof-value ,@keys))
1588 (let* ((start (gensym "START"))
1589 (end (gensym "END"))
1590 (preserve-whitespace (gensym "PRESERVE-WHITESPACE"))
1594 ;; Odd number of keys, punt.
1595 (when keys (return-from read-from-string form)))
1596 (let* ((key (pop keys))
1601 (:preserve-whitespace preserve-whitespace)
1603 (return-from read-from-string form)))))
1604 (when (member key seen)
1605 (setf var (gensym "IGNORE"))
1608 (push (list var value) bind)))
1609 (dolist (default (list (list start 0)
1611 (list preserve-whitespace nil)))
1612 (unless (assoc (car default) bind)
1613 (push default bind)))
1614 (once-only ((string string))
1615 `(let ,(nreverse bind)
1616 ,@(when ignore `((declare (ignore ,@ignore))))
1617 (%read-from-string ,string ,eof-error-p ,eof-value
1618 ,start ,end ,preserve-whitespace))))))))
1622 (defun parse-integer (string &key (start 0) end (radix 10) junk-allowed)
1624 "Examine the substring of string delimited by start and end
1625 (default to the beginning and end of the string) It skips over
1626 whitespace characters and then tries to parse an integer. The
1627 radix parameter must be between 2 and 36."
1628 (macrolet ((parse-error (format-control)
1629 `(error 'simple-parse-error
1630 :format-control ,format-control
1631 :format-arguments (list string))))
1632 (with-array-data ((string string :offset-var offset)
1635 :check-fill-pointer t)
1636 (let ((index (do ((i start (1+ i)))
1639 (return-from parse-integer (values nil end))
1640 (parse-error "no non-whitespace characters in string ~S.")))
1641 (declare (fixnum i))
1642 (unless (whitespace[1]p (char string i)) (return i))))
1646 (declare (fixnum index))
1647 (let ((char (char string index)))
1648 (cond ((char= char #\-)
1654 (when (= index end) (return nil))
1655 (let* ((char (char string index))
1656 (weight (digit-char-p char radix)))
1658 (setq result (+ weight (* result radix))
1660 (junk-allowed (return nil))
1661 ((whitespace[1]p char)
1664 (when (= index end) (return))
1665 (unless (whitespace[1]p (char string index))
1666 (parse-error "junk in string ~S")))
1669 (parse-error "junk in string ~S"))))
1673 (if minusp (- result) result)
1676 (parse-error "no digits in string ~S")))
1677 (- index offset))))))
1679 ;;;; reader initialization code
1681 (defun !reader-cold-init ()
1682 (!cold-init-constituent-trait-table)
1683 (!cold-init-standard-readtable)
1684 ;; FIXME: This was commented out, but should probably be restored.
1685 #+nil (!cold-init-integer-reader))
1687 (def!method print-object ((readtable readtable) stream)
1688 (print-unreadable-object (readtable stream :identity t :type t)))