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. This is for recovery from broken
30 ;;; read-tables (and for WITH-STANDARD-IO-SYNTAX), and should not
31 ;;; normally be user-visible.
32 (defvar *standard-readtable*)
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 (defun %reader-error (stream control &rest args)
55 :format-control control
56 :format-arguments args))
58 ;;;; macros and functions for character tables
60 ;;; FIXME: could be SB!XC:DEFMACRO inside EVAL-WHEN (COMPILE EVAL)
61 (defmacro get-cat-entry (char rt)
62 ;; KLUDGE: Only give this side-effect-free args.
63 ;; FIXME: should probably become inline function
64 `(if (typep ,char 'base-char)
65 (elt (character-attribute-array ,rt) (char-code ,char))
66 (gethash ,char (character-attribute-hash-table ,rt) +char-attr-constituent+)))
68 (defun set-cat-entry (char newvalue &optional (rt *readtable*))
69 (if (typep char 'base-char)
70 (setf (elt (character-attribute-array rt) (char-code char)) newvalue)
71 ;; FIXME: could REMHASH if we're setting to
72 ;; +CHAR-ATTR-CONSTITUENT+
73 (setf (gethash char (character-attribute-hash-table rt)) newvalue)))
75 ;;; the value actually stored in the character macro table. As per
76 ;;; ANSI #'GET-MACRO-CHARACTER and #'SET-MACRO-CHARACTER, this can
77 ;;; be either a function or NIL.
78 (eval-when (:compile-toplevel :execute)
79 (sb!xc:defmacro get-raw-cmt-entry (char readtable)
80 `(if (typep ,char 'base-char)
81 (svref (character-macro-array ,readtable) (char-code ,char))
82 ;; Note: DEFAULT here is NIL, not #'UNDEFINED-MACRO-CHAR, so
83 ;; that everything above the base-char range is a non-macro
84 ;; constituent by default.
85 (gethash ,char (character-macro-hash-table ,readtable) nil))))
87 ;;; the value represented by whatever is stored in the character macro
88 ;;; table. As per ANSI #'GET-MACRO-CHARACTER and #'SET-MACRO-CHARACTER,
89 ;;; a function value represents itself, and a NIL value represents the
91 (defun get-coerced-cmt-entry (char readtable)
93 (or (get-raw-cmt-entry char readtable)
96 (defun set-cmt-entry (char new-value-designator &optional (rt *readtable*))
97 (if (typep char 'base-char)
98 (setf (svref (character-macro-array rt) (char-code char))
99 (and new-value-designator
100 (%coerce-callable-to-fun new-value-designator)))
101 (setf (gethash char (character-macro-hash-table rt))
102 (and new-value-designator
103 (%coerce-callable-to-fun new-value-designator)))))
105 (defun undefined-macro-char (stream char)
106 (unless *read-suppress*
107 (%reader-error stream "undefined read-macro character ~S" char)))
109 ;;; The character attribute table is a CHAR-CODE-LIMIT vector of integers.
111 (defmacro test-attribute (char whichclass rt)
112 `(= (the fixnum (get-cat-entry ,char ,rt)) ,whichclass))
114 ;;; predicates for testing character attributes
116 #!-sb-fluid (declaim (inline whitespacep))
117 (defun whitespacep (char &optional (rt *readtable*))
118 (test-attribute char +char-attr-whitespace+ rt))
120 (defmacro constituentp (char &optional (rt '*readtable*))
121 `(test-attribute ,char +char-attr-constituent+ ,rt))
123 (defmacro terminating-macrop (char &optional (rt '*readtable*))
124 `(test-attribute ,char +char-attr-terminating-macro+ ,rt))
126 (defmacro single-escape-p (char &optional (rt '*readtable*))
127 `(test-attribute ,char +char-attr-single-escape+ ,rt))
129 (defmacro multiple-escape-p (char &optional (rt '*readtable*))
130 `(test-attribute ,char +char-attr-multiple-escape+ ,rt))
132 (defmacro token-delimiterp (char &optional (rt '*readtable*))
133 ;; depends on actual attribute numbering above.
134 `(<= (get-cat-entry ,char ,rt) +char-attr-terminating-macro+))
136 ;;;; constituent traits (see ANSI 2.1.4.2)
138 ;;; There are a number of "secondary" attributes which are constant
139 ;;; properties of characters (as long as they are constituents).
141 (defvar *constituent-trait-table*)
142 (declaim (type attribute-table *constituent-trait-table*))
144 (defun !set-constituent-trait (char trait)
145 (aver (typep char 'base-char))
146 (setf (elt *constituent-trait-table* (char-code char))
149 (defun !cold-init-constituent-trait-table ()
150 (setq *constituent-trait-table*
151 (make-array base-char-code-limit :element-type '(unsigned-byte 8)
152 :initial-element +char-attr-constituent+))
153 (!set-constituent-trait #\: +char-attr-package-delimiter+)
154 (!set-constituent-trait #\. +char-attr-constituent-dot+)
155 (!set-constituent-trait #\+ +char-attr-constituent-sign+)
156 (!set-constituent-trait #\- +char-attr-constituent-sign+)
157 (!set-constituent-trait #\/ +char-attr-constituent-slash+)
158 (do ((i (char-code #\0) (1+ i)))
159 ((> i (char-code #\9)))
160 (!set-constituent-trait (code-char i) +char-attr-constituent-digit+))
161 (!set-constituent-trait #\E +char-attr-constituent-expt+)
162 (!set-constituent-trait #\F +char-attr-constituent-expt+)
163 (!set-constituent-trait #\D +char-attr-constituent-expt+)
164 (!set-constituent-trait #\S +char-attr-constituent-expt+)
165 (!set-constituent-trait #\L +char-attr-constituent-expt+)
166 (!set-constituent-trait #\e +char-attr-constituent-expt+)
167 (!set-constituent-trait #\f +char-attr-constituent-expt+)
168 (!set-constituent-trait #\d +char-attr-constituent-expt+)
169 (!set-constituent-trait #\s +char-attr-constituent-expt+)
170 (!set-constituent-trait #\l +char-attr-constituent-expt+)
171 (!set-constituent-trait #\Space +char-attr-invalid+)
172 (!set-constituent-trait #\Newline +char-attr-invalid+)
173 (dolist (c (list backspace-char-code tab-char-code form-feed-char-code
174 return-char-code rubout-char-code))
175 (!set-constituent-trait (code-char c) +char-attr-invalid+)))
177 (defmacro get-constituent-trait (char)
178 `(if (typep ,char 'base-char)
179 (elt *constituent-trait-table* (char-code ,char))
180 +char-attr-constituent+))
182 ;;;; readtable operations
184 (defun shallow-replace/eql-hash-table (to from)
185 (maphash (lambda (k v) (setf (gethash k to) v)) from))
187 (defun copy-readtable (&optional (from-readtable *readtable*)
189 (let ((really-from-readtable (or from-readtable *standard-readtable*))
190 (really-to-readtable (or to-readtable (make-readtable))))
191 (replace (character-attribute-array really-to-readtable)
192 (character-attribute-array really-from-readtable))
193 (shallow-replace/eql-hash-table
194 (character-attribute-hash-table really-to-readtable)
195 (character-attribute-hash-table really-from-readtable))
196 (replace (character-macro-array really-to-readtable)
197 (character-macro-array really-from-readtable))
198 (shallow-replace/eql-hash-table
199 (character-macro-hash-table really-to-readtable)
200 (character-macro-hash-table really-from-readtable))
201 (setf (dispatch-tables really-to-readtable)
202 (mapcar (lambda (pair)
204 (let ((table (make-hash-table)))
205 (shallow-replace/eql-hash-table table (cdr pair))
207 (dispatch-tables really-from-readtable)))
208 (setf (readtable-case really-to-readtable)
209 (readtable-case really-from-readtable))
210 really-to-readtable))
212 (defun set-syntax-from-char (to-char from-char &optional
213 (to-readtable *readtable*)
216 "Causes the syntax of TO-CHAR to be the same as FROM-CHAR in the
217 optional readtable (defaults to the current readtable). The
218 FROM-TABLE defaults to the standard Lisp readtable when NIL."
219 (let ((really-from-readtable (or from-readtable *standard-readtable*)))
220 (let ((att (get-cat-entry from-char really-from-readtable))
221 (mac (get-raw-cmt-entry from-char really-from-readtable))
222 (from-dpair (find from-char (dispatch-tables really-from-readtable)
223 :test #'char= :key #'car))
224 (to-dpair (find to-char (dispatch-tables to-readtable)
225 :test #'char= :key #'car)))
226 (set-cat-entry to-char att to-readtable)
227 (set-cmt-entry to-char mac to-readtable)
231 (let ((table (cdr to-dpair)))
233 (shallow-replace/eql-hash-table table (cdr from-dpair))))
235 (let ((pair (cons to-char (make-hash-table))))
236 (shallow-replace/eql-hash-table (cdr pair) (cdr from-dpair))
237 (setf (dispatch-tables to-readtable)
238 (push pair (dispatch-tables to-readtable)))))))))
241 (defun set-macro-character (char function &optional
242 (non-terminatingp nil)
243 (readtable *readtable*))
245 "Causes CHAR to be a macro character which invokes FUNCTION when seen
246 by the reader. The NON-TERMINATINGP flag can be used to make the macro
247 character non-terminating, i.e. embeddable in a symbol name."
248 (let ((designated-readtable (or readtable *standard-readtable*)))
249 (set-cat-entry char (if non-terminatingp
250 +char-attr-constituent+
251 +char-attr-terminating-macro+)
252 designated-readtable)
253 (set-cmt-entry char function designated-readtable)
254 t)) ; (ANSI-specified return value)
256 (defun get-macro-character (char &optional (readtable *readtable*))
258 "Return the function associated with the specified CHAR which is a macro
259 character, or NIL if there is no such function. As a second value, return
260 T if CHAR is a macro character which is non-terminating, i.e. which can
261 be embedded in a symbol name."
262 (let* ((designated-readtable (or readtable *standard-readtable*))
263 ;; the first return value: a FUNCTION if CHAR is a macro
264 ;; character, or NIL otherwise
265 (fun-value (get-raw-cmt-entry char designated-readtable)))
267 ;; NON-TERMINATING-P return value:
269 (or (constituentp char)
270 (not (terminating-macrop char)))
271 ;; ANSI's definition of GET-MACRO-CHARACTER says this
272 ;; value is NIL when CHAR is not a macro character.
273 ;; I.e. this value means not just "non-terminating
274 ;; character?" but "non-terminating macro character?".
277 ;;;; definitions to support internal programming conventions
279 (defmacro eofp (char)
280 `(eq ,char *eof-object*))
282 (defun flush-whitespace (stream)
283 ;; This flushes whitespace chars, returning the last char it read (a
284 ;; non-white one). It always gets an error on end-of-file.
285 (let ((stream (in-synonym-of stream)))
286 (if (ansi-stream-p stream)
287 (prepare-for-fast-read-char stream
288 (do ((attribute-array (character-attribute-array *readtable*))
289 (attribute-hash-table
290 (character-attribute-hash-table *readtable*))
291 (char (fast-read-char t) (fast-read-char t)))
293 (if (typep char 'base-char)
294 (aref attribute-array (char-code char))
295 (gethash char attribute-hash-table +char-attr-constituent+)))
296 +char-attr-whitespace+)
297 (done-with-fast-read-char)
300 (do ((attribute-array (character-attribute-array *readtable*))
301 (attribute-hash-table
302 (character-attribute-hash-table *readtable*))
303 (char (read-char stream nil :eof) (read-char stream nil :eof)))
306 (if (typep char 'base-char)
307 (aref attribute-array (char-code char))
308 (gethash char attribute-hash-table +char-attr-constituent+)))
309 +char-attr-whitespace+))
311 (error 'end-of-file :stream stream)
314 ;;;; temporary initialization hack
316 (defun !cold-init-standard-readtable ()
317 (setq *standard-readtable* (make-readtable))
318 ;; All characters get boring defaults in MAKE-READTABLE. Now we
319 ;; override the boring defaults on characters which need more
320 ;; interesting behavior.
321 (let ((*readtable* *standard-readtable*))
323 (flet ((whitespaceify (char)
324 (set-cmt-entry char nil)
325 (set-cat-entry char +char-attr-whitespace+)))
326 (whitespaceify (code-char tab-char-code))
327 (whitespaceify #\Newline)
328 (whitespaceify #\Space)
329 (whitespaceify (code-char form-feed-char-code))
330 (whitespaceify (code-char return-char-code)))
332 (set-cat-entry #\\ +char-attr-single-escape+)
333 (set-cmt-entry #\\ nil)
335 (set-cat-entry #\| +char-attr-multiple-escape+)
336 (set-cmt-entry #\| nil)
338 ;; Easy macro-character definitions are in this source file.
339 (set-macro-character #\" #'read-string)
340 (set-macro-character #\' #'read-quote)
341 (set-macro-character #\( #'read-list)
342 (set-macro-character #\) #'read-right-paren)
343 (set-macro-character #\; #'read-comment)
344 ;; (The hairier macro-character definitions, for #\# and #\`, are
345 ;; defined elsewhere, in their own source files.)
348 (do ((ichar 0 (1+ ichar))
350 ((= ichar base-char-code-limit))
351 (setq char (code-char ichar))
352 (when (constituentp char *standard-readtable*)
353 (set-cmt-entry char nil)))))
355 ;;;; implementation of the read buffer
357 (defvar *read-buffer*)
358 (defvar *read-buffer-length*)
359 ;;; FIXME: Is it really helpful to have *READ-BUFFER-LENGTH* be a
360 ;;; separate variable instead of just calculating it on the fly as
361 ;;; (LENGTH *READ-BUFFER*)?
366 (declaim (type index *read-buffer-length* *inch-ptr* *ouch-ptr*))
367 (declaim (type (simple-array character (*)) *read-buffer*))
369 (defmacro reset-read-buffer ()
370 ;; Turn *READ-BUFFER* into an empty read buffer.
372 ;; *OUCH-PTR* always points to next char to write.
374 ;; *INCH-PTR* always points to next char to read.
375 (setq *inch-ptr* 0)))
377 (defun !cold-init-read-buffer ()
378 (setq *read-buffer* (make-string 512)) ; initial bufsize
379 (setq *read-buffer-length* 512)
382 ;;; FIXME I removed "THE FIXNUM"'s from OUCH-READ-BUFFER and
383 ;;; OUCH-UNREAD-BUFFER, check to make sure that Python really is smart
384 ;;; enough to make good code without them. And while I'm at it,
385 ;;; converting them from macros to inline functions might be good,
388 (defmacro ouch-read-buffer (char)
390 ;; When buffer overflow
391 (when (>= *ouch-ptr* *read-buffer-length*)
392 ;; Size should be doubled.
394 (setf (elt (the simple-string *read-buffer*) *ouch-ptr*) ,char)
395 (setq *ouch-ptr* (1+ *ouch-ptr*))))
397 ;;; macro to move *ouch-ptr* back one.
398 (defmacro ouch-unread-buffer ()
399 '(when (> *ouch-ptr* *inch-ptr*)
400 (setq *ouch-ptr* (1- (the fixnum *ouch-ptr*)))))
402 (defun grow-read-buffer ()
403 (let ((rbl (length (the simple-string *read-buffer*))))
405 (concatenate 'simple-string
408 (setq *read-buffer-length* (* 2 rbl))))
410 (defun inchpeek-read-buffer ()
411 (if (>= (the fixnum *inch-ptr*) (the fixnum *ouch-ptr*))
413 (elt *read-buffer* *inch-ptr*)))
415 (defun inch-read-buffer ()
416 (if (>= *inch-ptr* *ouch-ptr*)
419 (elt *read-buffer* *inch-ptr*)
422 (defmacro unread-buffer ()
425 (defun read-unwind-read-buffer ()
426 ;; Keep contents, but make next (INCH..) return first character.
429 (defun read-buffer-to-string ()
430 (subseq *read-buffer* 0 *ouch-ptr*))
432 ;;;; READ-PRESERVING-WHITESPACE, READ-DELIMITED-LIST, and READ
434 ;;; an alist for #=, used to keep track of objects with labels assigned that
435 ;;; have been completely read. Each entry is (integer-tag gensym-tag value).
437 ;;; KLUDGE: Should this really be an alist? It seems as though users
438 ;;; could reasonably expect N log N performance for large datasets.
439 ;;; On the other hand, it's probably very very seldom a problem in practice.
440 ;;; On the third hand, it might be just as easy to use a hash table
441 ;;; as an alist, so maybe we should. -- WHN 19991202
442 (defvar *sharp-equal-alist* ())
444 (declaim (special *standard-input*))
446 ;;; READ-PRESERVING-WHITESPACE behaves just like READ, only it makes
447 ;;; sure to leave terminating whitespace in the stream. (This is a
448 ;;; COMMON-LISP exported symbol.)
449 (defun read-preserving-whitespace (&optional (stream *standard-input*)
454 "Read from STREAM and return the value read, preserving any whitespace
455 that followed the object."
457 ;; a loop for repeating when a macro returns nothing
459 (let ((char (read-char stream eof-error-p *eof-object*)))
460 (cond ((eofp char) (return eof-value))
463 (let* ((macrofun (get-coerced-cmt-entry char *readtable*))
464 (result (multiple-value-list
465 (funcall macrofun stream char))))
466 ;; Repeat if macro returned nothing.
468 (return (unless *read-suppress* (car result)))))))))
469 (let ((*sharp-equal-alist* nil))
470 (read-preserving-whitespace stream eof-error-p eof-value t))))
472 ;;; Return NIL or a list with one thing, depending.
474 ;;; for functions that want comments to return so that they can look
475 ;;; past them. We assume CHAR is not whitespace.
476 (defun read-maybe-nothing (stream char)
477 (let ((retval (multiple-value-list
478 (funcall (get-coerced-cmt-entry char *readtable*)
481 (if retval (rplacd retval nil))))
483 (defun read (&optional (stream *standard-input*)
488 "Read the next Lisp value from STREAM, and return it."
489 (let ((result (read-preserving-whitespace stream
493 ;; This function generally discards trailing whitespace. If you
494 ;; don't want to discard trailing whitespace, call
495 ;; CL:READ-PRESERVING-WHITESPACE instead.
496 (unless (or (eql result eof-value) recursivep)
497 (let ((next-char (read-char stream nil nil)))
498 (unless (or (null next-char)
499 (whitespacep next-char))
500 (unread-char next-char stream))))
503 ;;; (This is a COMMON-LISP exported symbol.)
504 (defun read-delimited-list (endchar &optional
505 (input-stream *standard-input*)
508 "Read Lisp values from INPUT-STREAM until the next character after a
509 value's representation is ENDCHAR, and return the objects as a list."
510 (declare (ignore recursive-p))
511 (do ((char (flush-whitespace input-stream)
512 (flush-whitespace input-stream))
514 ((char= char endchar) (unless *read-suppress* (nreverse retlist)))
515 (setq retlist (nconc (read-maybe-nothing input-stream char) retlist))))
517 ;;;; basic readmacro definitions
519 ;;;; Some large, hairy subsets of readmacro definitions (backquotes
520 ;;;; and sharp macros) are not here, but in their own source files.
522 (defun read-quote (stream ignore)
523 (declare (ignore ignore))
524 (list 'quote (read stream t nil t)))
526 (defun read-comment (stream ignore)
527 (declare (ignore ignore))
529 ((character-decoding-error
530 #'(lambda (decoding-error)
531 (declare (ignorable decoding-error))
532 (style-warn "Character decoding error in a ;-comment at position ~A reading source file ~A, resyncing." (file-position stream) stream)
533 (invoke-restart 'attempt-resync))))
534 (let ((stream (in-synonym-of stream)))
535 (if (ansi-stream-p stream)
536 (prepare-for-fast-read-char stream
537 (do ((char (fast-read-char nil nil)
538 (fast-read-char nil nil)))
539 ((or (not char) (char= char #\newline))
540 (done-with-fast-read-char))))
542 (do ((char (read-char stream nil :eof) (read-char stream nil :eof)))
543 ((or (eq char :eof) (char= char #\newline)))))))
544 ;; Don't return anything.
547 (defun read-list (stream ignore)
548 (declare (ignore ignore))
549 (let* ((thelist (list nil))
551 (do ((firstchar (flush-whitespace stream) (flush-whitespace stream)))
552 ((char= firstchar #\) ) (cdr thelist))
553 (when (char= firstchar #\.)
554 (let ((nextchar (read-char stream t)))
555 (cond ((token-delimiterp nextchar)
556 (cond ((eq listtail thelist)
559 "Nothing appears before . in list."))
560 ((whitespacep nextchar)
561 (setq nextchar (flush-whitespace stream))))
563 ;; Return list containing last thing.
564 (car (read-after-dot stream nextchar)))
565 (return (cdr thelist)))
566 ;; Put back NEXTCHAR so that we can read it normally.
567 (t (unread-char nextchar stream)))))
568 ;; Next thing is not an isolated dot.
569 (let ((listobj (read-maybe-nothing stream firstchar)))
570 ;; allows the possibility that a comment was read
572 (rplacd listtail listobj)
573 (setq listtail listobj))))))
575 (defun read-after-dot (stream firstchar)
576 ;; FIRSTCHAR is non-whitespace!
578 (do ((char firstchar (flush-whitespace stream)))
580 (%reader-error stream "Nothing appears after . in list."))
581 ;; See whether there's something there.
582 (setq lastobj (read-maybe-nothing stream char))
583 (when lastobj (return t)))
584 ;; At least one thing appears after the dot.
585 ;; Check for more than one thing following dot.
586 (do ((lastchar (flush-whitespace stream)
587 (flush-whitespace stream)))
588 ((char= lastchar #\) ) lastobj) ;success!
589 ;; Try reading virtual whitespace.
590 (if (read-maybe-nothing stream lastchar)
591 (%reader-error stream "More than one object follows . in list.")))))
593 (defun read-string (stream closech)
594 ;; This accumulates chars until it sees same char that invoked it.
595 ;; For a very long string, this could end up bloating the read buffer.
597 (let ((stream (in-synonym-of stream)))
598 (if (ansi-stream-p stream)
599 (prepare-for-fast-read-char stream
600 (do ((char (fast-read-char t) (fast-read-char t)))
601 ((char= char closech)
602 (done-with-fast-read-char))
603 (if (single-escape-p char) (setq char (fast-read-char t)))
604 (ouch-read-buffer char)))
606 (do ((char (read-char stream nil :eof) (read-char stream nil :eof)))
607 ((or (eq char :eof) (char= char closech))
609 (error 'end-of-file :stream stream)))
610 (when (single-escape-p char)
611 (setq char (read-char stream nil :eof))
613 (error 'end-of-file :stream stream)))
614 (ouch-read-buffer char))))
615 (read-buffer-to-string))
617 (defun read-right-paren (stream ignore)
618 (declare (ignore ignore))
619 (%reader-error stream "unmatched close parenthesis"))
621 ;;; Read from the stream up to the next delimiter. Leave the resulting
622 ;;; token in *READ-BUFFER*, and return two values:
623 ;;; -- a list of the escaped character positions, and
624 ;;; -- The position of the first package delimiter (or NIL).
625 (defun internal-read-extended-token (stream firstchar escape-firstchar)
628 (when escape-firstchar
629 (push *ouch-ptr* escapes)
630 (ouch-read-buffer firstchar)
631 (setq firstchar (read-char stream nil *eof-object*)))
632 (do ((char firstchar (read-char stream nil *eof-object*))
634 ((cond ((eofp char) t)
635 ((token-delimiterp char)
636 (unread-char char stream)
639 (values escapes colon))
640 (cond ((single-escape-p char)
641 ;; It can't be a number, even if it's 1\23.
642 ;; Read next char here, so it won't be casified.
643 (push *ouch-ptr* escapes)
644 (let ((nextchar (read-char stream nil *eof-object*)))
646 (reader-eof-error stream "after escape character")
647 (ouch-read-buffer nextchar))))
648 ((multiple-escape-p char)
649 ;; Read to next multiple-escape, escaping single chars
652 (let ((ch (read-char stream nil *eof-object*)))
655 (reader-eof-error stream "inside extended token"))
656 ((multiple-escape-p ch) (return))
657 ((single-escape-p ch)
658 (let ((nextchar (read-char stream nil *eof-object*)))
659 (cond ((eofp nextchar)
660 (reader-eof-error stream "after escape character"))
662 (push *ouch-ptr* escapes)
663 (ouch-read-buffer nextchar)))))
665 (push *ouch-ptr* escapes)
666 (ouch-read-buffer ch))))))
668 (when (and (constituentp char)
669 (eql (get-constituent-trait char)
670 +char-attr-package-delimiter+)
672 (setq colon *ouch-ptr*))
673 (ouch-read-buffer char))))))
675 ;;;; character classes
677 ;;; Return the character class for CHAR.
679 ;;; FIXME: why aren't these ATT-getting forms using GET-CAT-ENTRY?
680 ;;; Because we've cached the readtable tables?
681 (defmacro char-class (char attarray atthash)
682 `(let ((att (if (typep ,char 'base-char)
683 (aref ,attarray (char-code ,char))
684 (gethash ,char ,atthash +char-attr-constituent+))))
685 (declare (fixnum att))
687 ((<= att +char-attr-terminating-macro+) +char-attr-delimiter+)
688 ((< att +char-attr-constituent+) att)
689 (t (setf att (get-constituent-trait ,char))
690 (if (= att +char-attr-invalid+)
691 (%reader-error stream "invalid constituent")
694 ;;; Return the character class for CHAR, which might be part of a
696 (defmacro char-class2 (char attarray atthash)
697 `(let ((att (if (typep ,char 'base-char)
698 (aref ,attarray (char-code ,char))
699 (gethash ,char ,atthash +char-attr-constituent+))))
700 (declare (fixnum att))
702 ((<= att +char-attr-terminating-macro+) +char-attr-delimiter+)
703 ((< att +char-attr-constituent+) att)
704 (t (setf att (get-constituent-trait ,char))
706 ((digit-char-p ,char *read-base*) +char-attr-constituent-digit+)
707 ((= att +char-attr-constituent-digit+) +char-attr-constituent+)
708 ((= att +char-attr-invalid+)
709 (%reader-error stream "invalid constituent"))
712 ;;; Return the character class for a char which might be part of a
713 ;;; rational or floating number. (Assume that it is a digit if it
715 (defmacro char-class3 (char attarray atthash)
716 `(let ((att (if (typep ,char 'base-char)
717 (aref ,attarray (char-code ,char))
718 (gethash ,char ,atthash +char-attr-constituent+))))
719 (declare (fixnum att))
721 ((<= att +char-attr-terminating-macro+) +char-attr-delimiter+)
722 ((< att +char-attr-constituent+) att)
723 (t (setf att (get-constituent-trait ,char))
724 (when possibly-rational
725 (setq possibly-rational
726 (or (digit-char-p ,char *read-base*)
727 (= att +char-attr-constituent-slash+))))
730 (or (digit-char-p ,char 10)
731 (= att +char-attr-constituent-dot+))))
733 ((digit-char-p ,char (max *read-base* 10))
734 (if (digit-char-p ,char *read-base*)
735 (if (= att +char-attr-constituent-expt+)
736 +char-attr-constituent-digit-or-expt+
737 +char-attr-constituent-digit+)
738 +char-attr-constituent-decimal-digit+))
739 ((= att +char-attr-invalid+)
740 (%reader-error stream "invalid constituent"))
745 (defvar *read-suppress* nil
747 "Suppress most interpreting in the reader when T.")
749 (defvar *read-base* 10
751 "the radix that Lisp reads numbers in")
752 (declaim (type (integer 2 36) *read-base*))
754 ;;; Modify the read buffer according to READTABLE-CASE, ignoring
755 ;;; ESCAPES. ESCAPES is a list of the escaped indices, in reverse
757 (defun casify-read-buffer (escapes)
758 (let ((case (readtable-case *readtable*)))
760 ((and (null escapes) (eq case :upcase))
761 (dotimes (i *ouch-ptr*)
762 (setf (schar *read-buffer* i)
763 (char-upcase (schar *read-buffer* i)))))
764 ((eq case :preserve))
766 (macrolet ((skip-esc (&body body)
767 `(do ((i (1- *ouch-ptr*) (1- i))
771 (when (or (null escapes)
772 (let ((esc (first escapes)))
773 (declare (fixnum esc))
779 (let ((ch (schar *read-buffer* i)))
782 (skip-esc (setf (schar *read-buffer* i) (char-downcase ch))))
784 (skip-esc (setf (schar *read-buffer* i) (char-upcase ch)))))
787 (:downcase (lower-em))
792 (when (both-case-p ch)
793 (if (upper-case-p ch)
795 (setq all-upper nil))))
796 (cond (all-lower (raise-em))
797 (all-upper (lower-em))))))))))))
799 (defun read-token (stream firstchar)
801 "This function is just an fsm that recognizes numbers and symbols."
802 ;; Check explicitly whether FIRSTCHAR has an entry for
803 ;; NON-TERMINATING in CHARACTER-ATTRIBUTE-TABLE and
804 ;; READ-DOT-NUMBER-SYMBOL in CMT. Report an error if these are
805 ;; violated. (If we called this, we want something that is a
806 ;; legitimate token!) Read in the longest possible string satisfying
807 ;; the Backus-Naur form for "unqualified-token". Leave the result in
808 ;; the *READ-BUFFER*. Return next char after token (last char read).
809 (when *read-suppress*
810 (internal-read-extended-token stream firstchar nil)
811 (return-from read-token nil))
812 (let ((attribute-array (character-attribute-array *readtable*))
813 (attribute-hash-table (character-attribute-hash-table *readtable*))
814 (package-designator nil)
816 (possibly-rational t)
817 (seen-digit-or-expt nil)
819 (was-possibly-float nil)
821 (seen-multiple-escapes nil))
823 (prog ((char firstchar))
824 (case (char-class3 char attribute-array attribute-hash-table)
825 (#.+char-attr-constituent-sign+ (go SIGN))
826 (#.+char-attr-constituent-digit+ (go LEFTDIGIT))
827 (#.+char-attr-constituent-digit-or-expt+
828 (setq seen-digit-or-expt t)
830 (#.+char-attr-constituent-decimal-digit+ (go LEFTDECIMALDIGIT))
831 (#.+char-attr-constituent-dot+ (go FRONTDOT))
832 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
833 (#.+char-attr-package-delimiter+ (go COLON))
834 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
835 (#.+char-attr-invalid+ (%reader-error stream "invalid constituent"))
836 ;; can't have eof, whitespace, or terminating macro as first char!
839 (ouch-read-buffer char)
840 (setq char (read-char stream nil nil))
841 (unless char (go RETURN-SYMBOL))
842 (setq possibly-rational t
844 (case (char-class3 char attribute-array attribute-hash-table)
845 (#.+char-attr-constituent-digit+ (go LEFTDIGIT))
846 (#.+char-attr-constituent-digit-or-expt+
847 (setq seen-digit-or-expt t)
849 (#.+char-attr-constituent-decimal-digit+ (go LEFTDECIMALDIGIT))
850 (#.+char-attr-constituent-dot+ (go SIGNDOT))
851 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
852 (#.+char-attr-package-delimiter+ (go COLON))
853 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
854 (#.+char-attr-delimiter+ (unread-char char stream) (go RETURN-SYMBOL))
856 LEFTDIGIT ; saw "[sign] {digit}+"
857 (ouch-read-buffer char)
858 (setq char (read-char stream nil nil))
859 (unless char (return (make-integer)))
860 (setq was-possibly-float possibly-float)
861 (case (char-class3 char attribute-array attribute-hash-table)
862 (#.+char-attr-constituent-digit+ (go LEFTDIGIT))
863 (#.+char-attr-constituent-decimal-digit+ (if possibly-float
864 (go LEFTDECIMALDIGIT)
866 (#.+char-attr-constituent-dot+ (if possibly-float
869 (#.+char-attr-constituent-digit-or-expt+
870 (if (or seen-digit-or-expt (not was-possibly-float))
871 (progn (setq seen-digit-or-expt t) (go LEFTDIGIT))
872 (progn (setq seen-digit-or-expt t) (go LEFTDIGIT-OR-EXPT))))
873 (#.+char-attr-constituent-expt+
874 (if was-possibly-float
877 (#.+char-attr-constituent-slash+ (if possibly-rational
880 (#.+char-attr-delimiter+ (unread-char char stream)
881 (return (make-integer)))
882 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
883 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
884 (#.+char-attr-package-delimiter+ (go COLON))
887 (ouch-read-buffer char)
888 (setq char (read-char stream nil nil))
889 (unless char (return (make-integer)))
890 (case (char-class3 char attribute-array attribute-hash-table)
891 (#.+char-attr-constituent-digit+ (go LEFTDIGIT))
892 (#.+char-attr-constituent-decimal-digit+ (bug "impossible!"))
893 (#.+char-attr-constituent-dot+ (go SYMBOL))
894 (#.+char-attr-constituent-digit-or-expt+ (go LEFTDIGIT))
895 (#.+char-attr-constituent-expt+ (go SYMBOL))
896 (#.+char-attr-constituent-sign+ (go EXPTSIGN))
897 (#.+char-attr-constituent-slash+ (if possibly-rational
900 (#.+char-attr-delimiter+ (unread-char char stream)
901 (return (make-integer)))
902 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
903 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
904 (#.+char-attr-package-delimiter+ (go COLON))
906 LEFTDECIMALDIGIT ; saw "[sign] {decimal-digit}+"
907 (aver possibly-float)
908 (ouch-read-buffer char)
909 (setq char (read-char stream nil nil))
910 (unless char (go RETURN-SYMBOL))
911 (case (char-class char attribute-array attribute-hash-table)
912 (#.+char-attr-constituent-digit+ (go LEFTDECIMALDIGIT))
913 (#.+char-attr-constituent-dot+ (go MIDDLEDOT))
914 (#.+char-attr-constituent-expt+ (go EXPONENT))
915 (#.+char-attr-constituent-slash+ (aver (not possibly-rational))
917 (#.+char-attr-delimiter+ (unread-char char stream)
919 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
920 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
921 (#.+char-attr-package-delimiter+ (go COLON))
923 MIDDLEDOT ; saw "[sign] {digit}+ dot"
924 (ouch-read-buffer char)
925 (setq char (read-char stream nil nil))
926 (unless char (return (let ((*read-base* 10))
928 (case (char-class char attribute-array attribute-hash-table)
929 (#.+char-attr-constituent-digit+ (go RIGHTDIGIT))
930 (#.+char-attr-constituent-expt+ (go EXPONENT))
931 (#.+char-attr-delimiter+
932 (unread-char char stream)
933 (return (let ((*read-base* 10))
935 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
936 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
937 (#.+char-attr-package-delimiter+ (go COLON))
939 RIGHTDIGIT ; saw "[sign] {decimal-digit}* dot {digit}+"
940 (ouch-read-buffer char)
941 (setq char (read-char stream nil nil))
942 (unless char (return (make-float stream)))
943 (case (char-class char attribute-array attribute-hash-table)
944 (#.+char-attr-constituent-digit+ (go RIGHTDIGIT))
945 (#.+char-attr-constituent-expt+ (go EXPONENT))
946 (#.+char-attr-delimiter+
947 (unread-char char stream)
948 (return (make-float stream)))
949 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
950 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
951 (#.+char-attr-package-delimiter+ (go COLON))
953 SIGNDOT ; saw "[sign] dot"
954 (ouch-read-buffer char)
955 (setq char (read-char stream nil nil))
956 (unless char (go RETURN-SYMBOL))
957 (case (char-class char attribute-array attribute-hash-table)
958 (#.+char-attr-constituent-digit+ (go RIGHTDIGIT))
959 (#.+char-attr-delimiter+ (unread-char char stream) (go RETURN-SYMBOL))
960 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
961 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
964 (ouch-read-buffer char)
965 (setq char (read-char stream nil nil))
966 (unless char (%reader-error stream "dot context error"))
967 (case (char-class char attribute-array attribute-hash-table)
968 (#.+char-attr-constituent-digit+ (go RIGHTDIGIT))
969 (#.+char-attr-constituent-dot+ (go DOTS))
970 (#.+char-attr-delimiter+ (%reader-error stream "dot context error"))
971 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
972 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
973 (#.+char-attr-package-delimiter+ (go COLON))
976 (ouch-read-buffer char)
977 (setq char (read-char stream nil nil))
978 (unless char (go RETURN-SYMBOL))
979 (setq possibly-float t)
980 (case (char-class char attribute-array attribute-hash-table)
981 (#.+char-attr-constituent-sign+ (go EXPTSIGN))
982 (#.+char-attr-constituent-digit+ (go EXPTDIGIT))
983 (#.+char-attr-delimiter+ (unread-char char stream) (go RETURN-SYMBOL))
984 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
985 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
986 (#.+char-attr-package-delimiter+ (go COLON))
988 EXPTSIGN ; got to EXPONENT, and saw a sign character
989 (ouch-read-buffer char)
990 (setq char (read-char stream nil nil))
991 (unless char (go RETURN-SYMBOL))
992 (case (char-class char attribute-array attribute-hash-table)
993 (#.+char-attr-constituent-digit+ (go EXPTDIGIT))
994 (#.+char-attr-delimiter+ (unread-char char stream) (go RETURN-SYMBOL))
995 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
996 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
997 (#.+char-attr-package-delimiter+ (go COLON))
999 EXPTDIGIT ; got to EXPONENT, saw "[sign] {digit}+"
1000 (ouch-read-buffer char)
1001 (setq char (read-char stream nil nil))
1002 (unless char (return (make-float stream)))
1003 (case (char-class char attribute-array attribute-hash-table)
1004 (#.+char-attr-constituent-digit+ (go EXPTDIGIT))
1005 (#.+char-attr-delimiter+
1006 (unread-char char stream)
1007 (return (make-float stream)))
1008 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1009 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1010 (#.+char-attr-package-delimiter+ (go COLON))
1012 RATIO ; saw "[sign] {digit}+ slash"
1013 (ouch-read-buffer char)
1014 (setq char (read-char stream nil nil))
1015 (unless char (go RETURN-SYMBOL))
1016 (case (char-class2 char attribute-array attribute-hash-table)
1017 (#.+char-attr-constituent-digit+ (go RATIODIGIT))
1018 (#.+char-attr-delimiter+ (unread-char char stream) (go RETURN-SYMBOL))
1019 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1020 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1021 (#.+char-attr-package-delimiter+ (go COLON))
1023 RATIODIGIT ; saw "[sign] {digit}+ slash {digit}+"
1024 (ouch-read-buffer char)
1025 (setq char (read-char stream nil nil))
1026 (unless char (return (make-ratio stream)))
1027 (case (char-class2 char attribute-array attribute-hash-table)
1028 (#.+char-attr-constituent-digit+ (go RATIODIGIT))
1029 (#.+char-attr-delimiter+
1030 (unread-char char stream)
1031 (return (make-ratio stream)))
1032 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1033 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1034 (#.+char-attr-package-delimiter+ (go COLON))
1036 DOTS ; saw "dot {dot}+"
1037 (ouch-read-buffer char)
1038 (setq char (read-char stream nil nil))
1039 (unless char (%reader-error stream "too many dots"))
1040 (case (char-class char attribute-array attribute-hash-table)
1041 (#.+char-attr-constituent-dot+ (go DOTS))
1042 (#.+char-attr-delimiter+
1043 (unread-char char stream)
1044 (%reader-error stream "too many dots"))
1045 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1046 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1047 (#.+char-attr-package-delimiter+ (go COLON))
1049 SYMBOL ; not a dot, dots, or number
1050 (let ((stream (in-synonym-of stream)))
1051 (if (ansi-stream-p stream)
1052 (prepare-for-fast-read-char stream
1055 (ouch-read-buffer char)
1056 (setq char (fast-read-char nil nil))
1057 (unless char (go RETURN-SYMBOL))
1058 (case (char-class char attribute-array attribute-hash-table)
1059 (#.+char-attr-single-escape+ (done-with-fast-read-char)
1061 (#.+char-attr-delimiter+ (done-with-fast-read-char)
1062 (unread-char char stream)
1064 (#.+char-attr-multiple-escape+ (done-with-fast-read-char)
1066 (#.+char-attr-package-delimiter+ (done-with-fast-read-char)
1068 (t (go SYMBOL-LOOP)))))
1072 (ouch-read-buffer char)
1073 (setq char (read-char stream nil :eof))
1074 (when (eq char :eof) (go RETURN-SYMBOL))
1075 (case (char-class char attribute-array attribute-hash-table)
1076 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1077 (#.+char-attr-delimiter+ (unread-char char stream)
1079 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1080 (#.+char-attr-package-delimiter+ (go COLON))
1081 (t (go SYMBOL-LOOP))))))
1082 SINGLE-ESCAPE ; saw a single-escape
1083 ;; Don't put the escape character in the read buffer.
1084 ;; READ-NEXT CHAR, put in buffer (no case conversion).
1085 (let ((nextchar (read-char stream nil nil)))
1087 (reader-eof-error stream "after single-escape character"))
1088 (push *ouch-ptr* escapes)
1089 (ouch-read-buffer nextchar))
1090 (setq char (read-char stream nil nil))
1091 (unless char (go RETURN-SYMBOL))
1092 (case (char-class char attribute-array attribute-hash-table)
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))
1099 (setq seen-multiple-escapes t)
1100 (do ((char (read-char stream t) (read-char stream t)))
1101 ((multiple-escape-p char))
1102 (if (single-escape-p char) (setq char (read-char stream t)))
1103 (push *ouch-ptr* escapes)
1104 (ouch-read-buffer char))
1105 (setq char (read-char stream nil nil))
1106 (unless char (go RETURN-SYMBOL))
1107 (case (char-class char attribute-array attribute-hash-table)
1108 (#.+char-attr-delimiter+ (unread-char char stream) (go RETURN-SYMBOL))
1109 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1110 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1111 (#.+char-attr-package-delimiter+ (go COLON))
1114 (casify-read-buffer escapes)
1115 (unless (zerop colons)
1116 (%reader-error stream "too many colons in ~S"
1117 (read-buffer-to-string)))
1119 (setq package-designator
1120 (if (plusp *ouch-ptr*)
1121 ;; FIXME: It seems inefficient to cons up a package
1122 ;; designator string every time we read a symbol with an
1123 ;; explicit package prefix. Perhaps we could implement
1124 ;; a FIND-PACKAGE* function analogous to INTERN*
1126 (read-buffer-to-string)
1127 (if seen-multiple-escapes
1128 (read-buffer-to-string)
1129 *keyword-package*)))
1132 (setq char (read-char stream nil nil))
1133 (unless char (reader-eof-error stream "after reading a colon"))
1134 (case (char-class char attribute-array attribute-hash-table)
1135 (#.+char-attr-delimiter+
1136 (unread-char char stream)
1137 (%reader-error stream
1138 "illegal terminating character after a colon: ~S"
1140 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1141 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1142 (#.+char-attr-package-delimiter+ (go INTERN))
1146 (setq char (read-char stream nil nil))
1148 (reader-eof-error stream "after reading a colon"))
1149 (case (char-class char attribute-array attribute-hash-table)
1150 (#.+char-attr-delimiter+
1151 (unread-char char stream)
1152 (%reader-error stream
1153 "illegal terminating character after a colon: ~S"
1155 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1156 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1157 (#.+char-attr-package-delimiter+
1158 (%reader-error stream
1159 "too many colons after ~S name"
1160 package-designator))
1163 (casify-read-buffer escapes)
1164 (let ((found (if package-designator
1165 (find-package package-designator)
1168 (error 'reader-package-error :stream stream
1169 :format-arguments (list package-designator)
1170 :format-control "package ~S not found"))
1172 (if (or (zerop colons) (= colons 2) (eq found *keyword-package*))
1173 (return (intern* *read-buffer* *ouch-ptr* found))
1174 (multiple-value-bind (symbol test)
1175 (find-symbol* *read-buffer* *ouch-ptr* found)
1176 (when (eq test :external) (return symbol))
1177 (let ((name (read-buffer-to-string)))
1178 (with-simple-restart (continue "Use symbol anyway.")
1179 (error 'reader-package-error :stream stream
1180 :format-arguments (list name (package-name found))
1183 "The symbol ~S is not external in the ~A package."
1184 "Symbol ~S not found in the ~A package.")))
1185 (return (intern name found)))))))))
1187 ;;; for semi-external use:
1189 ;;; For semi-external use: Return 3 values: the string for the token,
1190 ;;; a flag for whether there was an escape char, and the position of
1191 ;;; any package delimiter.
1192 (defun read-extended-token (stream &optional (*readtable* *readtable*))
1193 (let ((first-char (read-char stream nil nil t)))
1195 (multiple-value-bind (escapes colon)
1196 (internal-read-extended-token stream first-char nil)
1197 (casify-read-buffer escapes)
1198 (values (read-buffer-to-string) (not (null escapes)) colon)))
1200 (values "" nil nil)))))
1202 ;;; for semi-external use:
1204 ;;; Read an extended token with the first character escaped. Return
1205 ;;; the string for the token.
1206 (defun read-extended-token-escaped (stream &optional (*readtable* *readtable*))
1207 (let ((first-char (read-char stream nil nil)))
1209 (let ((escapes (internal-read-extended-token stream first-char t)))
1210 (casify-read-buffer escapes)
1211 (read-buffer-to-string)))
1213 (reader-eof-error stream "after escape")))))
1215 ;;;; number-reading functions
1217 (defmacro digit* nil
1218 `(do ((ch char (inch-read-buffer)))
1219 ((or (eofp ch) (not (digit-char-p ch))) (setq char ch))
1220 ;; Report if at least one digit is seen.
1221 (setq one-digit t)))
1223 (defmacro exponent-letterp (letter)
1224 `(memq ,letter '(#\E #\S #\F #\L #\D #\e #\s #\f #\l #\d)))
1226 ;;; FIXME: It would be cleaner to have these generated automatically
1227 ;;; by compile-time code instead of having them hand-created like
1228 ;;; this. The !COLD-INIT-INTEGER-READER code below should be resurrected
1230 (defvar *integer-reader-safe-digits*
1232 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)
1234 "the mapping of base to 'safe' number of digits to read for a fixnum")
1235 (defvar *integer-reader-base-power*
1237 67108864 129140163 67108864 48828125 60466176 40353607
1238 16777216 43046721 100000000 19487171 35831808 62748517 105413504 11390625
1239 16777216 24137569 34012224 47045881 64000000 85766121 113379904 6436343
1240 7962624 9765625 11881376 14348907 17210368 20511149 24300000 28629151
1241 33554432 39135393 45435424 52521875 60466176)
1243 "the largest fixnum power of the base for MAKE-INTEGER")
1244 (declaim (simple-vector *integer-reader-safe-digits*
1245 *integer-reader-base-power*))
1247 (defun !cold-init-integer-reader ()
1248 (do ((base 2 (1+ base)))
1251 (do ((fix (truncate most-positive-fixnum base)
1252 (truncate fix base))
1253 (digits 0 (1+ digits)))
1254 ((zerop fix) digits))))
1255 (setf (aref *integer-reader-safe-digits* base)
1257 (aref *integer-reader-base-power* base)
1258 (expt base digits)))))
1261 (defun make-integer ()
1263 "Minimizes bignum-fixnum multiplies by reading a 'safe' number of digits,
1264 then multiplying by a power of the base and adding."
1265 (let* ((base *read-base*)
1266 (digits-per (aref *integer-reader-safe-digits* base))
1267 (base-power (aref *integer-reader-base-power* base))
1270 (declare (type index digits-per base-power))
1271 (read-unwind-read-buffer)
1272 (let ((char (inch-read-buffer)))
1273 (cond ((char= char #\-)
1276 (t (unread-buffer))))
1279 (declare (type index num))
1280 (dotimes (digit digits-per)
1281 (let* ((ch (inch-read-buffer)))
1282 (cond ((or (eofp ch) (char= ch #\.))
1283 (return-from make-integer
1285 (if (zerop number) num
1287 (expt base digit))))))
1288 (if negativep (- res) res))))
1289 (t (setq num (+ (digit-char-p ch base)
1290 (the index (* num base))))))))
1291 (setq number (+ num (* number base-power)))))))
1293 (defun make-float (stream)
1294 ;; Assume that the contents of *read-buffer* are a legal float, with nothing
1296 (read-unwind-read-buffer)
1297 (let ((negative-fraction nil)
1300 (negative-exponent nil)
1303 (char (inch-read-buffer)))
1304 (if (cond ((char= char #\+) t)
1305 ((char= char #\-) (setq negative-fraction t)))
1307 (setq char (inch-read-buffer)))
1308 ;; Read digits before the dot.
1309 (do* ((ch char (inch-read-buffer))
1310 (dig (digit-char-p ch) (digit-char-p ch)))
1311 ((not dig) (setq char ch))
1312 (setq number (+ (* number 10) dig)))
1313 ;; Deal with the dot, if it's there.
1314 (when (char= char #\.)
1315 (setq char (inch-read-buffer))
1316 ;; Read digits after the dot.
1317 (do* ((ch char (inch-read-buffer))
1318 (dig (and (not (eofp ch)) (digit-char-p ch))
1319 (and (not (eofp ch)) (digit-char-p ch))))
1320 ((not dig) (setq char ch))
1321 (setq divisor (* divisor 10))
1322 (setq number (+ (* number 10) dig))))
1323 ;; Is there an exponent letter?
1325 ;; If not, we've read the whole number.
1326 (let ((num (make-float-aux number divisor
1327 *read-default-float-format*
1329 (return-from make-float (if negative-fraction (- num) num))))
1330 ((exponent-letterp char)
1331 (setq float-char char)
1333 (setq char (inch-read-buffer))
1334 ;; Check leading sign.
1335 (if (cond ((char= char #\+) t)
1336 ((char= char #\-) (setq negative-exponent t)))
1338 (setq char (inch-read-buffer)))
1339 ;; Read digits for exponent.
1340 (do* ((ch char (inch-read-buffer))
1341 (dig (and (not (eofp ch)) (digit-char-p ch))
1342 (and (not (eofp ch)) (digit-char-p ch))))
1344 (setq exponent (if negative-exponent (- exponent) exponent)))
1345 (setq exponent (+ (* exponent 10) dig)))
1346 ;; Generate and return the float, depending on FLOAT-CHAR:
1347 (let* ((float-format (case (char-upcase float-char)
1348 (#\E *read-default-float-format*)
1353 (result (make-float-aux (* (expt 10 exponent) number)
1354 divisor float-format stream)))
1355 (return-from make-float
1356 (if negative-fraction (- result) result))))
1357 (t (bug "bad fallthrough in floating point reader")))))
1359 (defun make-float-aux (number divisor float-format stream)
1361 (coerce (/ number divisor) float-format)
1363 (error 'reader-impossible-number-error
1364 :error c :stream stream
1365 :format-control "failed to build float"))))
1367 (defun make-ratio (stream)
1368 ;; Assume *READ-BUFFER* contains a legal ratio. Build the number from
1371 ;; Look for optional "+" or "-".
1372 (let ((numerator 0) (denominator 0) (char ()) (negative-number nil))
1373 (read-unwind-read-buffer)
1374 (setq char (inch-read-buffer))
1375 (cond ((char= char #\+)
1376 (setq char (inch-read-buffer)))
1378 (setq char (inch-read-buffer))
1379 (setq negative-number t)))
1381 (do* ((ch char (inch-read-buffer))
1382 (dig (digit-char-p ch *read-base*)
1383 (digit-char-p ch *read-base*)))
1385 (setq numerator (+ (* numerator *read-base*) dig)))
1387 (do* ((ch (inch-read-buffer) (inch-read-buffer))
1389 ((or (eofp ch) (not (setq dig (digit-char-p ch *read-base*)))))
1390 (setq denominator (+ (* denominator *read-base*) dig)))
1391 (let ((num (handler-case
1392 (/ numerator denominator)
1393 (arithmetic-error (c)
1394 (error 'reader-impossible-number-error
1395 :error c :stream stream
1396 :format-control "failed to build ratio")))))
1397 (if negative-number (- num) num))))
1399 ;;;; cruft for dispatch macros
1401 (defun make-char-dispatch-table ()
1404 (defun dispatch-char-error (stream sub-char ignore)
1405 (declare (ignore ignore))
1408 (%reader-error stream "no dispatch function defined for ~S" sub-char)))
1410 (defun make-dispatch-macro-character (char &optional
1411 (non-terminating-p nil)
1414 "Cause CHAR to become a dispatching macro character in readtable (which
1415 defaults to the current readtable). If NON-TERMINATING-P, the char will
1416 be non-terminating."
1417 (set-macro-character char #'read-dispatch-char non-terminating-p rt)
1418 (let* ((dalist (dispatch-tables rt))
1419 (dtable (cdr (find char dalist :test #'char= :key #'car))))
1421 (error "The dispatch character ~S already exists." char))
1423 (setf (dispatch-tables rt)
1424 (push (cons char (make-char-dispatch-table)) dalist)))))
1427 (defun set-dispatch-macro-character (disp-char sub-char function
1428 &optional (rt *readtable*))
1430 "Cause FUNCTION to be called whenever the reader reads DISP-CHAR
1431 followed by SUB-CHAR."
1432 ;; Get the dispatch char for macro (error if not there), diddle
1433 ;; entry for sub-char.
1434 (when (digit-char-p sub-char)
1435 (error "SUB-CHAR must not be a decimal digit: ~S" sub-char))
1436 (let* ((sub-char (char-upcase sub-char))
1437 (rt (or rt *standard-readtable*))
1438 (dpair (find disp-char (dispatch-tables rt)
1439 :test #'char= :key #'car)))
1441 (setf (gethash sub-char (cdr dpair)) (coerce function 'function))
1442 (error "~S is not a dispatch char." disp-char))))
1444 (defun get-dispatch-macro-character (disp-char sub-char
1445 &optional (rt *readtable*))
1447 "Return the macro character function for SUB-CHAR under DISP-CHAR
1448 or NIL if there is no associated function."
1449 (let* ((sub-char (char-upcase sub-char))
1450 (rt (or rt *standard-readtable*))
1451 (dpair (find disp-char (dispatch-tables rt)
1452 :test #'char= :key #'car)))
1454 (values (gethash sub-char (cdr dpair)))
1455 (error "~S is not a dispatch char." disp-char))))
1457 (defun read-dispatch-char (stream char)
1458 ;; Read some digits.
1462 (do* ((ch (read-char stream nil *eof-object*)
1463 (read-char stream nil *eof-object*))
1466 (not (setq dig (digit-char-p ch))))
1467 ;; Take care of the extra char.
1469 (reader-eof-error stream "inside dispatch character")
1470 (setq sub-char (char-upcase ch))))
1472 (setq numarg (+ (* numarg 10) dig)))
1473 ;; Look up the function and call it.
1474 (let ((dpair (find char (dispatch-tables *readtable*)
1475 :test #'char= :key #'car)))
1477 (funcall (the function
1478 (gethash sub-char (cdr dpair) #'dispatch-char-error))
1479 stream sub-char (if numargp numarg nil))
1480 (%reader-error stream "no dispatch table for dispatch char")))))
1482 ;;;; READ-FROM-STRING
1484 ;;; FIXME: Is it really worth keeping this pool?
1485 (defvar *read-from-string-spares* ()
1487 "A resource of string streams for Read-From-String.")
1489 (defun read-from-string (string &optional (eof-error-p t) eof-value
1491 preserve-whitespace)
1493 "The characters of string are successively given to the lisp reader
1494 and the lisp object built by the reader is returned. Macro chars
1496 (declare (string string))
1498 (with-array-data ((string string :offset-var offset)
1500 (end (%check-vector-sequence-bounds string start end)))
1501 (unless *read-from-string-spares*
1502 (push (make-string-input-stream "" 0 0) *read-from-string-spares*))
1503 (let ((stream (pop *read-from-string-spares*)))
1504 (setf (string-input-stream-string stream)
1505 (coerce string '(simple-array character (*))))
1506 (setf (string-input-stream-current stream) start)
1507 (setf (string-input-stream-end stream) end)
1509 (values (if preserve-whitespace
1510 (read-preserving-whitespace stream eof-error-p eof-value)
1511 (read stream eof-error-p eof-value))
1512 (- (string-input-stream-current stream) offset))
1513 (push stream *read-from-string-spares*)))))
1517 (defun parse-integer (string &key (start 0) end (radix 10) junk-allowed)
1519 "Examine the substring of string delimited by start and end
1520 (default to the beginning and end of the string) It skips over
1521 whitespace characters and then tries to parse an integer. The
1522 radix parameter must be between 2 and 36."
1523 (macrolet ((parse-error (format-control)
1524 `(error 'simple-parse-error
1525 :format-control ,format-control
1526 :format-arguments (list string))))
1527 (with-array-data ((string string :offset-var offset)
1529 (end (%check-vector-sequence-bounds string start end)))
1530 (let ((index (do ((i start (1+ i)))
1533 (return-from parse-integer (values nil end))
1534 (parse-error "no non-whitespace characters in string ~S.")))
1535 (declare (fixnum i))
1536 (unless (whitespacep (char string i)) (return i))))
1540 (declare (fixnum index))
1541 (let ((char (char string index)))
1542 (cond ((char= char #\-)
1548 (when (= index end) (return nil))
1549 (let* ((char (char string index))
1550 (weight (digit-char-p char radix)))
1552 (setq result (+ weight (* result radix))
1554 (junk-allowed (return nil))
1558 (when (= index end) (return))
1559 (unless (whitespacep (char string index))
1560 (parse-error "junk in string ~S")))
1563 (parse-error "junk in string ~S"))))
1567 (if minusp (- result) result)
1570 (parse-error "no digits in string ~S")))
1571 (- index offset))))))
1573 ;;;; reader initialization code
1575 (defun !reader-cold-init ()
1576 (!cold-init-read-buffer)
1577 (!cold-init-constituent-trait-table)
1578 (!cold-init-standard-readtable)
1579 ;; FIXME: This was commented out, but should probably be restored.
1580 #+nil (!cold-init-integer-reader))
1582 (def!method print-object ((readtable readtable) stream)
1583 (print-unreadable-object (readtable stream :identity t :type t)))