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 `(elt (character-attribute-table ,rt)
67 (defun set-cat-entry (char newvalue &optional (rt *readtable*))
68 (setf (elt (character-attribute-table rt)
72 ;;; FIXME: could be SB!XC:DEFMACRO inside EVAL-WHEN (COMPILE EVAL)
73 (defmacro get-cmt-entry (char rt)
75 (elt (the simple-vector (character-macro-table ,rt))
78 (defun set-cmt-entry (char newvalue &optional (rt *readtable*))
79 (setf (elt (the simple-vector (character-macro-table rt))
81 (coerce newvalue 'function)))
83 (defun undefined-macro-char (stream char)
84 (unless *read-suppress*
85 (%reader-error stream "undefined read-macro character ~S" char)))
87 ;;; The character attribute table is a CHAR-CODE-LIMIT vector of integers.
89 (defmacro test-attribute (char whichclass rt)
90 `(= (the fixnum (get-cat-entry ,char ,rt)) ,whichclass))
92 ;;; predicates for testing character attributes
94 #!-sb-fluid (declaim (inline whitespacep))
95 (defun whitespacep (char &optional (rt *readtable*))
96 (test-attribute char +char-attr-whitespace+ rt))
98 (defmacro constituentp (char &optional (rt '*readtable*))
99 `(>= (get-cat-entry ,char ,rt) +char-attr-constituent+))
101 (defmacro terminating-macrop (char &optional (rt '*readtable*))
102 `(test-attribute ,char +char-attr-terminating-macro+ ,rt))
104 (defmacro escapep (char &optional (rt '*readtable*))
105 `(test-attribute ,char +char-attr-escape+ ,rt))
107 (defmacro multiple-escape-p (char &optional (rt '*readtable*))
108 `(test-attribute ,char +char-attr-multiple-escape+ ,rt))
110 (defmacro token-delimiterp (char &optional (rt '*readtable*))
111 ;; depends on actual attribute numbering above.
112 `(<= (get-cat-entry ,char ,rt) +char-attr-terminating-macro+))
114 ;;;; secondary attribute table
116 ;;; There are a number of "secondary" attributes which are constant
117 ;;; properties of characters (as long as they are constituents).
119 (defvar *secondary-attribute-table*)
120 (declaim (type attribute-table *secondary-attribute-table*))
122 (defun !set-secondary-attribute (char attribute)
123 (setf (elt *secondary-attribute-table* (char-code char))
126 (defun !cold-init-secondary-attribute-table ()
127 (setq *secondary-attribute-table*
128 (make-array char-code-limit :element-type '(unsigned-byte 8)
129 :initial-element +char-attr-constituent+))
130 (!set-secondary-attribute #\: +char-attr-package-delimiter+)
131 (!set-secondary-attribute #\| +char-attr-multiple-escape+) ; |) [for EMACS]
132 (!set-secondary-attribute #\. +char-attr-constituent-dot+)
133 (!set-secondary-attribute #\+ +char-attr-constituent-sign+)
134 (!set-secondary-attribute #\- +char-attr-constituent-sign+)
135 (!set-secondary-attribute #\/ +char-attr-constituent-slash+)
136 (do ((i (char-code #\0) (1+ i)))
137 ((> i (char-code #\9)))
138 (!set-secondary-attribute (code-char i) +char-attr-constituent-digit+))
139 (!set-secondary-attribute #\E +char-attr-constituent-expt+)
140 (!set-secondary-attribute #\F +char-attr-constituent-expt+)
141 (!set-secondary-attribute #\D +char-attr-constituent-expt+)
142 (!set-secondary-attribute #\S +char-attr-constituent-expt+)
143 (!set-secondary-attribute #\L +char-attr-constituent-expt+)
144 (!set-secondary-attribute #\e +char-attr-constituent-expt+)
145 (!set-secondary-attribute #\f +char-attr-constituent-expt+)
146 (!set-secondary-attribute #\d +char-attr-constituent-expt+)
147 (!set-secondary-attribute #\s +char-attr-constituent-expt+)
148 (!set-secondary-attribute #\l +char-attr-constituent-expt+))
150 (defmacro get-secondary-attribute (char)
151 `(elt *secondary-attribute-table*
154 ;;;; readtable operations
156 (defun copy-readtable (&optional (from-readtable *readtable*)
158 (let ((really-from-readtable (or from-readtable *standard-readtable*))
159 (really-to-readtable (or to-readtable (make-readtable))))
160 (replace (character-attribute-table really-to-readtable)
161 (character-attribute-table really-from-readtable))
162 (replace (character-macro-table really-to-readtable)
163 (character-macro-table really-from-readtable))
164 (setf (dispatch-tables really-to-readtable)
165 (mapcar #'(lambda (pair) (cons (car pair)
166 (copy-seq (cdr pair))))
167 (dispatch-tables really-from-readtable)))
168 (setf (readtable-case really-to-readtable)
169 (readtable-case really-from-readtable))
170 really-to-readtable))
172 (defun set-syntax-from-char (to-char from-char &optional
173 (to-readtable *readtable*)
176 "Causes the syntax of TO-CHAR to be the same as FROM-CHAR in the
177 optional readtable (defaults to the current readtable). The
178 FROM-TABLE defaults to the standard Lisp readtable when NIL."
179 (let ((really-from-readtable (or from-readtable *standard-readtable*)))
180 ;; Copy FROM-CHAR entries to TO-CHAR entries, but make sure that if
181 ;; from char is a constituent you don't copy non-movable secondary
182 ;; attributes (constituent types), and that said attributes magically
183 ;; appear if you transform a non-constituent to a constituent.
184 (let ((att (get-cat-entry from-char really-from-readtable)))
185 (if (constituentp from-char really-from-readtable)
186 (setq att (get-secondary-attribute to-char)))
187 (set-cat-entry to-char att to-readtable)
188 (set-cmt-entry to-char
189 (get-cmt-entry from-char really-from-readtable)
193 (defun set-macro-character (char function &optional
194 (non-terminatingp nil) (rt *readtable*))
196 "Causes CHAR to be a macro character which invokes FUNCTION when
197 seen by the reader. The NON-TERMINATINGP flag can be used to
198 make the macro character non-terminating. The optional readtable
199 argument defaults to the current readtable. SET-MACRO-CHARACTER
201 (let ((rt (or rt *standard-readtable*)))
203 (set-cat-entry char (get-secondary-attribute char) rt)
204 (set-cat-entry char +char-attr-terminating-macro+ rt))
205 (set-cmt-entry char function rt)
208 (defun get-macro-character (char &optional (rt *readtable*))
210 "Return the function associated with the specified CHAR which is a macro
211 character. The optional readtable argument defaults to the current
213 (let ((rt (or rt *standard-readtable*)))
214 ;; Check macro syntax, return associated function if it's there.
215 ;; Returns a value for all constituents.
216 (cond ((constituentp char)
217 (values (get-cmt-entry char rt) t))
218 ((terminating-macrop char)
219 (values (get-cmt-entry char rt) nil))
222 ;;;; definitions to support internal programming conventions
224 (defmacro eofp (char) `(eq ,char *eof-object*))
226 (defun flush-whitespace (stream)
227 ;; This flushes whitespace chars, returning the last char it read (a
228 ;; non-white one). It always gets an error on end-of-file.
229 (let ((stream (in-synonym-of stream)))
230 (if (ansi-stream-p stream)
231 (prepare-for-fast-read-char stream
232 (do ((attribute-table (character-attribute-table *readtable*))
233 (char (fast-read-char t) (fast-read-char t)))
234 ((/= (the fixnum (aref attribute-table (char-code char)))
235 +char-attr-whitespace+)
236 (done-with-fast-read-char)
238 ;; fundamental-stream
239 (do ((attribute-table (character-attribute-table *readtable*))
240 (char (stream-read-char stream) (stream-read-char stream)))
242 (/= (the fixnum (aref attribute-table (char-code char)))
243 +char-attr-whitespace+))
245 (error 'end-of-file :stream stream)
248 ;;;; temporary initialization hack
250 (defun !cold-init-standard-readtable ()
251 (setq *standard-readtable* (make-readtable))
252 ;; All characters default to "constituent" in MAKE-READTABLE.
253 ;; *** un-constituent-ize some of these ***
254 (let ((*readtable* *standard-readtable*))
255 (set-cat-entry (code-char tab-char-code) +char-attr-whitespace+)
256 (set-cat-entry #\linefeed +char-attr-whitespace+)
257 (set-cat-entry #\space +char-attr-whitespace+)
258 (set-cat-entry (code-char form-feed-char-code) +char-attr-whitespace+)
259 (set-cat-entry (code-char return-char-code) +char-attr-whitespace+)
260 (set-cat-entry #\\ +char-attr-escape+)
261 (set-cmt-entry #\\ #'read-token)
262 (set-cat-entry (code-char rubout-char-code) +char-attr-whitespace+)
263 (set-cmt-entry #\: #'read-token)
264 (set-cmt-entry #\| #'read-token)
266 (set-macro-character #\" #'read-string)
268 (set-macro-character #\' #'read-quote)
269 (set-macro-character #\( #'read-list)
270 (set-macro-character #\) #'read-right-paren)
271 (set-macro-character #\; #'read-comment)
274 (do ((ichar 0 (1+ ichar))
277 (setq char (code-char ichar))
278 (when (constituentp char *standard-readtable*)
279 (set-cat-entry char (get-secondary-attribute char))
280 (set-cmt-entry char #'read-token)))))
282 ;;;; implementation of the read buffer
284 (defvar *read-buffer*)
285 (defvar *read-buffer-length*)
286 ;;; FIXME: Is it really helpful to have *READ-BUFFER-LENGTH* be a
287 ;;; separate variable instead of just calculating it on the fly as
288 ;;; (LENGTH *READ-BUFFER*)?
293 (declaim (type index *read-buffer-length* *inch-ptr* *ouch-ptr*))
294 (declaim (simple-string *read-buffer*))
296 (defmacro reset-read-buffer ()
297 ;; Turn *READ-BUFFER* into an empty read buffer.
299 ;; *OUCH-PTR* always points to next char to write.
301 ;; *INCH-PTR* always points to next char to read.
302 (setq *inch-ptr* 0)))
304 (defun !cold-init-read-buffer ()
305 (setq *read-buffer* (make-string 512)) ; initial bufsize
306 (setq *read-buffer-length* 512)
309 ;;; FIXME I removed "THE FIXNUM"'s from OUCH-READ-BUFFER and
310 ;;; OUCH-UNREAD-BUFFER, check to make sure that Python really is smart
311 ;;; enough to make good code without them. And while I'm at it,
312 ;;; converting them from macros to inline functions might be good,
315 (defmacro ouch-read-buffer (char)
317 ;; When buffer overflow
318 (when (>= *ouch-ptr* *read-buffer-length*)
319 ;; Size should be doubled.
321 (setf (elt (the simple-string *read-buffer*) *ouch-ptr*) ,char)
322 (setq *ouch-ptr* (1+ *ouch-ptr*))))
324 ;;; macro to move *ouch-ptr* back one.
325 (defmacro ouch-unread-buffer ()
326 '(when (> *ouch-ptr* *inch-ptr*)
327 (setq *ouch-ptr* (1- (the fixnum *ouch-ptr*)))))
329 (defun grow-read-buffer ()
330 (let ((rbl (length (the simple-string *read-buffer*))))
332 (concatenate 'simple-string
335 (setq *read-buffer-length* (* 2 rbl))))
337 (defun inchpeek-read-buffer ()
338 (if (>= (the fixnum *inch-ptr*) (the fixnum *ouch-ptr*))
340 (elt *read-buffer* *inch-ptr*)))
342 (defun inch-read-buffer ()
343 (if (>= *inch-ptr* *ouch-ptr*)
346 (elt *read-buffer* *inch-ptr*)
349 (defmacro unread-buffer ()
352 (defun read-unwind-read-buffer ()
353 ;; Keep contents, but make next (INCH..) return first character.
356 (defun read-buffer-to-string ()
357 (subseq *read-buffer* 0 *ouch-ptr*))
359 ;;;; READ-PRESERVING-WHITESPACE, READ-DELIMITED-LIST, and READ
361 ;;; an alist for #=, used to keep track of objects with labels assigned that
362 ;;; have been completely read. Each entry is (integer-tag gensym-tag value).
364 ;;; KLUDGE: Should this really be an alist? It seems as though users
365 ;;; could reasonably expect N log N performance for large datasets.
366 ;;; On the other hand, it's probably very very seldom a problem in practice.
367 ;;; On the third hand, it might be just as easy to use a hash table
368 ;;; as an alist, so maybe we should. -- WHN 19991202
369 (defvar *sharp-equal-alist* ())
371 (declaim (special *standard-input*))
373 ;;; READ-PRESERVING-WHITESPACE behaves just like READ, only it makes
374 ;;; sure to leave terminating whitespace in the stream. (This is a
375 ;;; COMMON-LISP exported symbol.)
376 (defun read-preserving-whitespace (&optional (stream *standard-input*)
381 "Read from STREAM and return the value read, preserving any whitespace
382 that followed the object."
384 ;; a loop for repeating when a macro returns nothing
386 (let ((char (read-char stream eof-error-p *eof-object*)))
387 (cond ((eofp char) (return eof-value))
390 (let* ((macrofun (get-cmt-entry char *readtable*))
391 (result (multiple-value-list
392 (funcall macrofun stream char))))
393 ;; Repeat if macro returned nothing.
394 (if result (return (car result))))))))
395 (let ((*sharp-equal-alist* nil))
396 (read-preserving-whitespace stream eof-error-p eof-value t))))
398 ;;; Return NIL or a list with one thing, depending.
400 ;;; for functions that want comments to return so that they can look
401 ;;; past them. We assume CHAR is not whitespace.
402 (defun read-maybe-nothing (stream char)
403 (let ((retval (multiple-value-list
404 (funcall (get-cmt-entry char *readtable*) stream char))))
405 (if retval (rplacd retval nil))))
407 (defun read (&optional (stream *standard-input*)
412 "Read the next Lisp value from STREAM, and return it."
413 (let ((result (read-preserving-whitespace stream
417 ;; (This function generally discards trailing whitespace. If you
418 ;; don't want to discard trailing whitespace, call
419 ;; CL:READ-PRESERVING-WHITESPACE instead.)
420 (unless (or (eql result eof-value) recursivep)
421 (let ((next-char (read-char stream nil nil)))
422 (unless (or (null next-char)
423 (whitespacep next-char))
424 (unread-char next-char stream))))
427 ;;; (This is a COMMON-LISP exported symbol.)
428 (defun read-delimited-list (endchar &optional
429 (input-stream *standard-input*)
432 "Read Lisp values from INPUT-STREAM until the next character after a
433 value's representation is ENDCHAR, and return the objects as a list."
434 (declare (ignore recursive-p))
435 (do ((char (flush-whitespace input-stream)
436 (flush-whitespace input-stream))
438 ((char= char endchar) (nreverse retlist))
439 (setq retlist (nconc (read-maybe-nothing input-stream char) retlist))))
441 ;;;; basic readmacro definitions
443 ;;;; Some large, hairy subsets of readmacro definitions (backquotes
444 ;;;; and sharp macros) are not here, but in their own source files.
446 (defun read-quote (stream ignore)
447 (declare (ignore ignore))
448 (list 'quote (read stream t nil t)))
450 (defun read-comment (stream ignore)
451 (declare (ignore ignore))
452 (let ((stream (in-synonym-of stream)))
453 (if (ansi-stream-p stream)
454 (prepare-for-fast-read-char stream
455 (do ((char (fast-read-char nil nil)
456 (fast-read-char nil nil)))
457 ((or (not char) (char= char #\newline))
458 (done-with-fast-read-char))))
459 ;; FUNDAMENTAL-STREAM
460 (do ((char (stream-read-char stream) (stream-read-char stream)))
461 ((or (eq char :eof) (char= char #\newline))))))
462 ;; Don't return anything.
465 (defun read-list (stream ignore)
466 (declare (ignore ignore))
467 (let* ((thelist (list nil))
469 (do ((firstchar (flush-whitespace stream) (flush-whitespace stream)))
470 ((char= firstchar #\) ) (cdr thelist))
471 (when (char= firstchar #\.)
472 (let ((nextchar (read-char stream t)))
473 (cond ((token-delimiterp nextchar)
474 (cond ((eq listtail thelist)
477 "Nothing appears before . in list."))
478 ((whitespacep nextchar)
479 (setq nextchar (flush-whitespace stream))))
481 ;; Return list containing last thing.
482 (car (read-after-dot stream nextchar)))
483 (return (cdr thelist)))
484 ;; Put back NEXTCHAR so that we can read it normally.
485 (t (unread-char nextchar stream)))))
486 ;; Next thing is not an isolated dot.
487 (let ((listobj (read-maybe-nothing stream firstchar)))
488 ;; allows the possibility that a comment was read
490 (rplacd listtail listobj)
491 (setq listtail listobj))))))
493 (defun read-after-dot (stream firstchar)
494 ;; FIRSTCHAR is non-whitespace!
496 (do ((char firstchar (flush-whitespace stream)))
498 (%reader-error stream "Nothing appears after . in list."))
499 ;; See whether there's something there.
500 (setq lastobj (read-maybe-nothing stream char))
501 (when lastobj (return t)))
502 ;; At least one thing appears after the dot.
503 ;; Check for more than one thing following dot.
504 (do ((lastchar (flush-whitespace stream)
505 (flush-whitespace stream)))
506 ((char= lastchar #\) ) lastobj) ;success!
507 ;; Try reading virtual whitespace.
508 (if (read-maybe-nothing stream lastchar)
509 (%reader-error stream "More than one object follows . in list.")))))
511 (defun read-string (stream closech)
512 ;; This accumulates chars until it sees same char that invoked it.
513 ;; For a very long string, this could end up bloating the read buffer.
515 (let ((stream (in-synonym-of stream)))
516 (if (ansi-stream-p stream)
517 (prepare-for-fast-read-char stream
518 (do ((char (fast-read-char t) (fast-read-char t)))
519 ((char= char closech)
520 (done-with-fast-read-char))
521 (if (escapep char) (setq char (fast-read-char t)))
522 (ouch-read-buffer char)))
523 ;; FUNDAMENTAL-STREAM
524 (do ((char (stream-read-char stream) (stream-read-char stream)))
525 ((or (eq char :eof) (char= char closech))
527 (error 'end-of-file :stream stream)))
529 (setq char (stream-read-char stream))
531 (error 'end-of-file :stream stream)))
532 (ouch-read-buffer char))))
533 (read-buffer-to-string))
535 (defun read-right-paren (stream ignore)
536 (declare (ignore ignore))
537 (%reader-error stream "unmatched close parenthesis"))
539 ;;; Read from the stream up to the next delimiter. Leave the resulting
540 ;;; token in *READ-BUFFER*, and return two values:
541 ;;; -- a list of the escaped character positions, and
542 ;;; -- The position of the first package delimiter (or NIL).
543 (defun internal-read-extended-token (stream firstchar escape-firstchar)
546 (when escape-firstchar
547 (push *ouch-ptr* escapes)
548 (ouch-read-buffer firstchar)
549 (setq firstchar (read-char stream nil *eof-object*)))
550 (do ((char firstchar (read-char stream nil *eof-object*))
552 ((cond ((eofp char) t)
553 ((token-delimiterp char)
554 (unread-char char stream)
557 (values escapes colon))
558 (cond ((escapep char)
559 ;; It can't be a number, even if it's 1\23.
560 ;; Read next char here, so it won't be casified.
561 (push *ouch-ptr* escapes)
562 (let ((nextchar (read-char stream nil *eof-object*)))
564 (reader-eof-error stream "after escape character")
565 (ouch-read-buffer nextchar))))
566 ((multiple-escape-p char)
567 ;; Read to next multiple-escape, escaping single chars
570 (let ((ch (read-char stream nil *eof-object*)))
573 (reader-eof-error stream "inside extended token"))
574 ((multiple-escape-p ch) (return))
576 (let ((nextchar (read-char stream nil *eof-object*)))
577 (cond ((eofp nextchar)
578 (reader-eof-error stream "after escape character"))
580 (push *ouch-ptr* escapes)
581 (ouch-read-buffer nextchar)))))
583 (push *ouch-ptr* escapes)
584 (ouch-read-buffer ch))))))
586 (when (and (constituentp char)
587 (eql (get-secondary-attribute char)
588 +char-attr-package-delimiter+)
590 (setq colon *ouch-ptr*))
591 (ouch-read-buffer char))))))
593 ;;;; character classes
595 ;;; Return the character class for CHAR.
596 (defmacro char-class (char attable)
597 `(let ((att (aref ,attable (char-code ,char))))
598 (declare (fixnum att))
599 (if (<= att +char-attr-terminating-macro+)
600 +char-attr-delimiter+
603 ;;; Return the character class for CHAR, which might be part of a
605 (defmacro char-class2 (char attable)
606 `(let ((att (aref ,attable (char-code ,char))))
607 (declare (fixnum att))
608 (if (<= att +char-attr-terminating-macro+)
609 +char-attr-delimiter+
610 (if (digit-char-p ,char *read-base*)
611 +char-attr-constituent-digit+
612 (if (= att +char-attr-constituent-digit+)
613 +char-attr-constituent+
616 ;;; Return the character class for a char which might be part of a
617 ;;; rational or floating number. (Assume that it is a digit if it
619 (defmacro char-class3 (char attable)
620 `(let ((att (aref ,attable (char-code ,char))))
621 (declare (fixnum att))
622 (if possibly-rational
623 (setq possibly-rational
624 (or (digit-char-p ,char *read-base*)
625 (= att +char-attr-constituent-slash+))))
628 (or (digit-char-p ,char 10)
629 (= att +char-attr-constituent-dot+))))
630 (if (<= att +char-attr-terminating-macro+)
631 +char-attr-delimiter+
632 (if (digit-char-p ,char (max *read-base* 10))
633 (if (digit-char-p ,char *read-base*)
634 +char-attr-constituent-digit+
635 +char-attr-constituent+)
640 (defvar *read-suppress* nil
642 "Suppress most interpreting in the reader when T.")
644 (defvar *read-base* 10
646 "the radix that Lisp reads numbers in")
647 (declaim (type (integer 2 36) *read-base*))
649 ;;; Modify the read buffer according to READTABLE-CASE, ignoring
650 ;;; ESCAPES. ESCAPES is a list of the escaped indices, in reverse
652 (defun casify-read-buffer (escapes)
653 (let ((case (readtable-case *readtable*)))
655 ((and (null escapes) (eq case :upcase))
656 (dotimes (i *ouch-ptr*)
657 (setf (schar *read-buffer* i)
658 (char-upcase (schar *read-buffer* i)))))
659 ((eq case :preserve))
661 (macrolet ((skip-esc (&body body)
662 `(do ((i (1- *ouch-ptr*) (1- i))
666 (when (or (null escapes)
667 (let ((esc (first escapes)))
668 (declare (fixnum esc))
674 (let ((ch (schar *read-buffer* i)))
677 (skip-esc (setf (schar *read-buffer* i) (char-downcase ch))))
679 (skip-esc (setf (schar *read-buffer* i) (char-upcase ch)))))
682 (:downcase (lower-em))
687 (when (both-case-p ch)
688 (if (upper-case-p ch)
690 (setq all-upper nil))))
691 (cond (all-lower (raise-em))
692 (all-upper (lower-em))))))))))))
694 (defun read-token (stream firstchar)
696 "This function is just an fsm that recognizes numbers and symbols."
697 ;; Check explicitly whether FIRSTCHAR has an entry for
698 ;; NON-TERMINATING in CHARACTER-ATTRIBUTE-TABLE and
699 ;; READ-DOT-NUMBER-SYMBOL in CMT. Report an error if these are
700 ;; violated. (If we called this, we want something that is a
701 ;; legitimate token!) Read in the longest possible string satisfying
702 ;; the Backus-Naur form for "unqualified-token". Leave the result in
703 ;; the *READ-BUFFER*. Return next char after token (last char read).
704 (when *read-suppress*
705 (internal-read-extended-token stream firstchar nil)
706 (return-from read-token nil))
707 (let ((attribute-table (character-attribute-table *readtable*))
708 (package-designator nil)
710 (possibly-rational t)
714 (prog ((char firstchar))
715 (case (char-class3 char attribute-table)
716 (#.+char-attr-constituent-sign+ (go SIGN))
717 (#.+char-attr-constituent-digit+ (go LEFTDIGIT))
718 (#.+char-attr-constituent-dot+ (go FRONTDOT))
719 (#.+char-attr-escape+ (go ESCAPE))
720 (#.+char-attr-package-delimiter+ (go COLON))
721 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
722 ;; can't have eof, whitespace, or terminating macro as first char!
725 (ouch-read-buffer char)
726 (setq char (read-char stream nil nil))
727 (unless char (go RETURN-SYMBOL))
728 (setq possibly-rational t
730 (case (char-class3 char attribute-table)
731 (#.+char-attr-constituent-digit+ (go LEFTDIGIT))
732 (#.+char-attr-constituent-dot+ (go SIGNDOT))
733 (#.+char-attr-escape+ (go ESCAPE))
734 (#.+char-attr-package-delimiter+ (go COLON))
735 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
736 (#.+char-attr-delimiter+ (unread-char char stream) (go RETURN-SYMBOL))
738 LEFTDIGIT ; saw "[sign] {digit}+"
739 (ouch-read-buffer char)
740 (setq char (read-char stream nil nil))
741 (unless char (return (make-integer)))
742 (case (char-class3 char attribute-table)
743 (#.+char-attr-constituent-digit+ (go LEFTDIGIT))
744 (#.+char-attr-constituent-dot+ (if possibly-float
747 (#.+char-attr-constituent-expt+ (go EXPONENT))
748 (#.+char-attr-constituent-slash+ (if possibly-rational
751 (#.+char-attr-delimiter+ (unread-char char stream)
752 (return (make-integer)))
753 (#.+char-attr-escape+ (go ESCAPE))
754 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
755 (#.+char-attr-package-delimiter+ (go COLON))
757 MIDDLEDOT ; saw "[sign] {digit}+ dot"
758 (ouch-read-buffer char)
759 (setq char (read-char stream nil nil))
760 (unless char (return (let ((*read-base* 10))
762 (case (char-class char attribute-table)
763 (#.+char-attr-constituent-digit+ (go RIGHTDIGIT))
764 (#.+char-attr-constituent-expt+ (go EXPONENT))
765 (#.+char-attr-delimiter+
766 (unread-char char stream)
767 (return (let ((*read-base* 10))
769 (#.+char-attr-escape+ (go ESCAPE))
770 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
771 (#.+char-attr-package-delimiter+ (go COLON))
773 RIGHTDIGIT ; saw "[sign] {digit}* dot {digit}+"
774 (ouch-read-buffer char)
775 (setq char (read-char stream nil nil))
776 (unless char (return (make-float)))
777 (case (char-class char attribute-table)
778 (#.+char-attr-constituent-digit+ (go RIGHTDIGIT))
779 (#.+char-attr-constituent-expt+ (go EXPONENT))
780 (#.+char-attr-delimiter+
781 (unread-char char stream)
782 (return (make-float)))
783 (#.+char-attr-escape+ (go ESCAPE))
784 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
785 (#.+char-attr-package-delimiter+ (go COLON))
787 SIGNDOT ; saw "[sign] dot"
788 (ouch-read-buffer char)
789 (setq char (read-char stream nil nil))
790 (unless char (go RETURN-SYMBOL))
791 (case (char-class char attribute-table)
792 (#.+char-attr-constituent-digit+ (go RIGHTDIGIT))
793 (#.+char-attr-delimiter+ (unread-char char stream) (go RETURN-SYMBOL))
794 (#.+char-attr-escape+ (go ESCAPE))
795 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
798 (ouch-read-buffer char)
799 (setq char (read-char stream nil nil))
800 (unless char (%reader-error stream "dot context error"))
801 (case (char-class char attribute-table)
802 (#.+char-attr-constituent-digit+ (go RIGHTDIGIT))
803 (#.+char-attr-constituent-dot+ (go DOTS))
804 (#.+char-attr-delimiter+ (%reader-error stream "dot context error"))
805 (#.+char-attr-escape+ (go ESCAPE))
806 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
807 (#.+char-attr-package-delimiter+ (go COLON))
810 (ouch-read-buffer char)
811 (setq char (read-char stream nil nil))
812 (unless char (go RETURN-SYMBOL))
813 (case (char-class char attribute-table)
814 (#.+char-attr-constituent-sign+ (go EXPTSIGN))
815 (#.+char-attr-constituent-digit+ (go EXPTDIGIT))
816 (#.+char-attr-delimiter+ (unread-char char stream) (go RETURN-SYMBOL))
817 (#.+char-attr-escape+ (go ESCAPE))
818 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
819 (#.+char-attr-package-delimiter+ (go COLON))
821 EXPTSIGN ; got to EXPONENT, and saw a sign character
822 (ouch-read-buffer char)
823 (setq char (read-char stream nil nil))
824 (unless char (go RETURN-SYMBOL))
825 (case (char-class char attribute-table)
826 (#.+char-attr-constituent-digit+ (go EXPTDIGIT))
827 (#.+char-attr-delimiter+ (unread-char char stream) (go RETURN-SYMBOL))
828 (#.+char-attr-escape+ (go ESCAPE))
829 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
830 (#.+char-attr-package-delimiter+ (go COLON))
832 EXPTDIGIT ; got to EXPONENT, saw "[sign] {digit}+"
833 (ouch-read-buffer char)
834 (setq char (read-char stream nil nil))
835 (unless char (return (make-float)))
836 (case (char-class char attribute-table)
837 (#.+char-attr-constituent-digit+ (go EXPTDIGIT))
838 (#.+char-attr-delimiter+
839 (unread-char char stream)
840 (return (make-float)))
841 (#.+char-attr-escape+ (go ESCAPE))
842 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
843 (#.+char-attr-package-delimiter+ (go COLON))
845 RATIO ; saw "[sign] {digit}+ slash"
846 (ouch-read-buffer char)
847 (setq char (read-char stream nil nil))
848 (unless char (go RETURN-SYMBOL))
849 (case (char-class2 char attribute-table)
850 (#.+char-attr-constituent-digit+ (go RATIODIGIT))
851 (#.+char-attr-delimiter+ (unread-char char stream) (go RETURN-SYMBOL))
852 (#.+char-attr-escape+ (go ESCAPE))
853 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
854 (#.+char-attr-package-delimiter+ (go COLON))
856 RATIODIGIT ; saw "[sign] {digit}+ slash {digit}+"
857 (ouch-read-buffer char)
858 (setq char (read-char stream nil nil))
859 (unless char (return (make-ratio)))
860 (case (char-class2 char attribute-table)
861 (#.+char-attr-constituent-digit+ (go RATIODIGIT))
862 (#.+char-attr-delimiter+
863 (unread-char char stream)
864 (return (make-ratio)))
865 (#.+char-attr-escape+ (go ESCAPE))
866 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
867 (#.+char-attr-package-delimiter+ (go COLON))
869 DOTS ; saw "dot {dot}+"
870 (ouch-read-buffer char)
871 (setq char (read-char stream nil nil))
872 (unless char (%reader-error stream "too many dots"))
873 (case (char-class char attribute-table)
874 (#.+char-attr-constituent-dot+ (go DOTS))
875 (#.+char-attr-delimiter+
876 (unread-char char stream)
877 (%reader-error stream "too many dots"))
878 (#.+char-attr-escape+ (go ESCAPE))
879 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
880 (#.+char-attr-package-delimiter+ (go COLON))
882 SYMBOL ; not a dot, dots, or number
883 (let ((stream (in-synonym-of stream)))
884 (if (ansi-stream-p stream)
885 (prepare-for-fast-read-char stream
888 (ouch-read-buffer char)
889 (setq char (fast-read-char nil nil))
890 (unless char (go RETURN-SYMBOL))
891 (case (char-class char attribute-table)
892 (#.+char-attr-escape+ (done-with-fast-read-char)
894 (#.+char-attr-delimiter+ (done-with-fast-read-char)
895 (unread-char char stream)
897 (#.+char-attr-multiple-escape+ (done-with-fast-read-char)
899 (#.+char-attr-package-delimiter+ (done-with-fast-read-char)
901 (t (go SYMBOL-LOOP)))))
902 ;; fundamental-stream
905 (ouch-read-buffer char)
906 (setq char (stream-read-char stream))
907 (when (eq char :eof) (go RETURN-SYMBOL))
908 (case (char-class char attribute-table)
909 (#.+char-attr-escape+ (go ESCAPE))
910 (#.+char-attr-delimiter+ (stream-unread-char stream char)
912 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
913 (#.+char-attr-package-delimiter+ (go COLON))
914 (t (go SYMBOL-LOOP))))))
915 ESCAPE ; saw an escape
916 ;; Don't put the escape in the read buffer.
917 ;; READ-NEXT CHAR, put in buffer (no case conversion).
918 (let ((nextchar (read-char stream nil nil)))
920 (reader-eof-error stream "after escape character"))
921 (push *ouch-ptr* escapes)
922 (ouch-read-buffer nextchar))
923 (setq char (read-char stream nil nil))
924 (unless char (go RETURN-SYMBOL))
925 (case (char-class char attribute-table)
926 (#.+char-attr-delimiter+ (unread-char char stream) (go RETURN-SYMBOL))
927 (#.+char-attr-escape+ (go ESCAPE))
928 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
929 (#.+char-attr-package-delimiter+ (go COLON))
932 (do ((char (read-char stream t) (read-char stream t)))
933 ((multiple-escape-p char))
934 (if (escapep char) (setq char (read-char stream t)))
935 (push *ouch-ptr* escapes)
936 (ouch-read-buffer char))
937 (setq char (read-char stream nil nil))
938 (unless char (go RETURN-SYMBOL))
939 (case (char-class char attribute-table)
940 (#.+char-attr-delimiter+ (unread-char char stream) (go RETURN-SYMBOL))
941 (#.+char-attr-escape+ (go ESCAPE))
942 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
943 (#.+char-attr-package-delimiter+ (go COLON))
946 (casify-read-buffer escapes)
947 (unless (zerop colons)
948 (%reader-error stream "too many colons in ~S"
949 (read-buffer-to-string)))
951 (setq package-designator
952 (if (plusp *ouch-ptr*)
953 ;; FIXME: It seems inefficient to cons up a package
954 ;; designator string every time we read a symbol with an
955 ;; explicit package prefix. Perhaps we could implement
956 ;; a FIND-PACKAGE* function analogous to INTERN*
958 (read-buffer-to-string)
962 (setq char (read-char stream nil nil))
963 (unless char (reader-eof-error stream "after reading a colon"))
964 (case (char-class char attribute-table)
965 (#.+char-attr-delimiter+
966 (unread-char char stream)
967 (%reader-error stream
968 "illegal terminating character after a colon: ~S"
970 (#.+char-attr-escape+ (go ESCAPE))
971 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
972 (#.+char-attr-package-delimiter+ (go INTERN))
976 (setq char (read-char stream nil nil))
978 (reader-eof-error stream "after reading a colon"))
979 (case (char-class char attribute-table)
980 (#.+char-attr-delimiter+
981 (unread-char char stream)
982 (%reader-error stream
983 "illegal terminating character after a colon: ~S"
985 (#.+char-attr-escape+ (go ESCAPE))
986 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
987 (#.+char-attr-package-delimiter+
988 (%reader-error stream
989 "too many colons after ~S name"
993 (casify-read-buffer escapes)
994 (let ((found (if package-designator
995 (find-package package-designator)
998 (error 'reader-package-error :stream stream
999 :format-arguments (list package-designator)
1000 :format-control "package ~S not found"))
1002 (if (or (zerop colons) (= colons 2) (eq found *keyword-package*))
1003 (return (intern* *read-buffer* *ouch-ptr* found))
1004 (multiple-value-bind (symbol test)
1005 (find-symbol* *read-buffer* *ouch-ptr* found)
1006 (when (eq test :external) (return symbol))
1007 (let ((name (read-buffer-to-string)))
1008 (with-simple-restart (continue "Use symbol anyway.")
1009 (error 'reader-package-error :stream stream
1010 :format-arguments (list name (package-name found))
1013 "The symbol ~S is not external in the ~A package."
1014 "Symbol ~S not found in the ~A package.")))
1015 (return (intern name found)))))))))
1017 ;;; for semi-external use:
1019 ;;; For semi-external use: Return 3 values: the string for the token,
1020 ;;; a flag for whether there was an escape char, and the position of
1021 ;;; any package delimiter.
1022 (defun read-extended-token (stream &optional (*readtable* *readtable*))
1023 (let ((first-char (read-char stream nil nil t)))
1025 (multiple-value-bind (escapes colon)
1026 (internal-read-extended-token stream first-char nil)
1027 (casify-read-buffer escapes)
1028 (values (read-buffer-to-string) (not (null escapes)) colon)))
1030 (values "" nil nil)))))
1032 ;;; for semi-external use:
1034 ;;; Read an extended token with the first character escaped. Return
1035 ;;; the string for the token.
1036 (defun read-extended-token-escaped (stream &optional (*readtable* *readtable*))
1037 (let ((first-char (read-char stream nil nil)))
1039 (let ((escapes (internal-read-extended-token stream first-char t)))
1040 (casify-read-buffer escapes)
1041 (read-buffer-to-string)))
1043 (reader-eof-error stream "after escape")))))
1045 ;;;; number-reading functions
1047 (defmacro digit* nil
1048 `(do ((ch char (inch-read-buffer)))
1049 ((or (eofp ch) (not (digit-char-p ch))) (setq char ch))
1050 ;; Report if at least one digit is seen.
1051 (setq one-digit t)))
1053 (defmacro exponent-letterp (letter)
1054 `(memq ,letter '(#\E #\S #\F #\L #\D #\e #\s #\f #\l #\d)))
1056 ;;; FIXME: It would be cleaner to have these generated automatically
1057 ;;; by compile-time code instead of having them hand-created like
1058 ;;; this. The !COLD-INIT-INTEGER-READER code below should be resurrected
1060 (defvar *integer-reader-safe-digits*
1062 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)
1064 "the mapping of base to 'safe' number of digits to read for a fixnum")
1065 (defvar *integer-reader-base-power*
1067 67108864 129140163 67108864 48828125 60466176 40353607
1068 16777216 43046721 100000000 19487171 35831808 62748517 105413504 11390625
1069 16777216 24137569 34012224 47045881 64000000 85766121 113379904 6436343
1070 7962624 9765625 11881376 14348907 17210368 20511149 24300000 28629151
1071 33554432 39135393 45435424 52521875 60466176)
1073 "the largest fixnum power of the base for MAKE-INTEGER")
1074 (declaim (simple-vector *integer-reader-safe-digits*
1075 *integer-reader-base-power*))
1077 (defun !cold-init-integer-reader ()
1078 (do ((base 2 (1+ base)))
1081 (do ((fix (truncate most-positive-fixnum base)
1082 (truncate fix base))
1083 (digits 0 (1+ digits)))
1084 ((zerop fix) digits))))
1085 (setf (aref *integer-reader-safe-digits* base)
1087 (aref *integer-reader-base-power* base)
1088 (expt base digits)))))
1091 (defun make-integer ()
1093 "Minimizes bignum-fixnum multiplies by reading a 'safe' number of digits,
1094 then multiplying by a power of the base and adding."
1095 (let* ((base *read-base*)
1096 (digits-per (aref *integer-reader-safe-digits* base))
1097 (base-power (aref *integer-reader-base-power* base))
1100 (declare (type index digits-per base-power))
1101 (read-unwind-read-buffer)
1102 (let ((char (inch-read-buffer)))
1103 (cond ((char= char #\-)
1106 (t (unread-buffer))))
1109 (declare (type index num))
1110 (dotimes (digit digits-per)
1111 (let* ((ch (inch-read-buffer)))
1112 (cond ((or (eofp ch) (char= ch #\.))
1113 (return-from make-integer
1115 (if (zerop number) num
1117 (expt base digit))))))
1118 (if negativep (- res) res))))
1119 (t (setq num (+ (digit-char-p ch base)
1120 (the index (* num base))))))))
1121 (setq number (+ num (* number base-power)))))))
1123 (defun make-float ()
1124 ;; Assume that the contents of *read-buffer* are a legal float, with nothing
1126 (read-unwind-read-buffer)
1127 (let ((negative-fraction nil)
1130 (negative-exponent nil)
1133 (char (inch-read-buffer)))
1134 (if (cond ((char= char #\+) t)
1135 ((char= char #\-) (setq negative-fraction t)))
1137 (setq char (inch-read-buffer)))
1138 ;; Read digits before the dot.
1139 (do* ((ch char (inch-read-buffer))
1140 (dig (digit-char-p ch) (digit-char-p ch)))
1141 ((not dig) (setq char ch))
1142 (setq number (+ (* number 10) dig)))
1143 ;; Deal with the dot, if it's there.
1144 (when (char= char #\.)
1145 (setq char (inch-read-buffer))
1146 ;; Read digits after the dot.
1147 (do* ((ch char (inch-read-buffer))
1148 (dig (and (not (eofp ch)) (digit-char-p ch))
1149 (and (not (eofp ch)) (digit-char-p ch))))
1150 ((not dig) (setq char ch))
1151 (setq divisor (* divisor 10))
1152 (setq number (+ (* number 10) dig))))
1153 ;; Is there an exponent letter?
1155 ;; If not, we've read the whole number.
1156 (let ((num (make-float-aux number divisor
1157 *read-default-float-format*)))
1158 (return-from make-float (if negative-fraction (- num) num))))
1159 ((exponent-letterp char)
1160 (setq float-char char)
1162 (setq char (inch-read-buffer))
1163 ;; Check leading sign.
1164 (if (cond ((char= char #\+) t)
1165 ((char= char #\-) (setq negative-exponent t)))
1167 (setq char (inch-read-buffer)))
1168 ;; Read digits for exponent.
1169 (do* ((ch char (inch-read-buffer))
1170 (dig (and (not (eofp ch)) (digit-char-p ch))
1171 (and (not (eofp ch)) (digit-char-p ch))))
1173 (setq exponent (if negative-exponent (- exponent) exponent)))
1174 (setq exponent (+ (* exponent 10) dig)))
1175 ;; Generate and return the float, depending on float-char:
1176 (let* ((float-format (case (char-upcase float-char)
1177 (#\E *read-default-float-format*)
1183 ;; toy@rtp.ericsson.se: We need to watch out if the
1184 ;; exponent is too small or too large. We add enough to
1185 ;; EXPONENT to make it within range and scale NUMBER
1186 ;; appropriately. This should avoid any unnecessary
1187 ;; underflow or overflow problems.
1188 (multiple-value-bind (min-expo max-expo)
1189 ;; FIXME: These #. forms are broken w.r.t.
1190 ;; cross-compilation portability. Maybe expressions
1192 ;; (LOG SB!XC:MOST-POSITIVE-SHORT-FLOAT 10s0)
1193 ;; could be used instead? Or perhaps some sort of
1194 ;; load-time-form magic?
1198 #.(log least-positive-normalized-short-float 10s0)
1199 #.(log most-positive-short-float 10s0)))
1202 #.(log least-positive-normalized-single-float 10f0)
1203 #.(log most-positive-single-float 10f0)))
1206 #.(log least-positive-normalized-double-float 10d0)
1207 #.(log most-positive-double-float 10d0)))
1210 #.(log least-positive-normalized-long-float 10L0)
1211 #.(log most-positive-long-float 10L0))))
1212 (let ((correction (cond ((<= exponent min-expo)
1213 (ceiling (- min-expo exponent)))
1214 ((>= exponent max-expo)
1215 (floor (- max-expo exponent)))
1218 (incf exponent correction)
1219 (setf number (/ number (expt 10 correction)))
1220 (setq num (make-float-aux number divisor float-format))
1221 (setq num (* num (expt 10 exponent)))
1222 (return-from make-float (if negative-fraction
1225 ;; should never happen:
1226 (t (error "internal error in floating point reader")))))
1228 (defun make-float-aux (number divisor float-format)
1229 (coerce (/ number divisor) float-format))
1231 (defun make-ratio ()
1232 ;; Assume *READ-BUFFER* contains a legal ratio. Build the number from
1235 ;; Look for optional "+" or "-".
1236 (let ((numerator 0) (denominator 0) (char ()) (negative-number nil))
1237 (read-unwind-read-buffer)
1238 (setq char (inch-read-buffer))
1239 (cond ((char= char #\+)
1240 (setq char (inch-read-buffer)))
1242 (setq char (inch-read-buffer))
1243 (setq negative-number t)))
1245 (do* ((ch char (inch-read-buffer))
1246 (dig (digit-char-p ch *read-base*)
1247 (digit-char-p ch *read-base*)))
1249 (setq numerator (+ (* numerator *read-base*) dig)))
1251 (do* ((ch (inch-read-buffer) (inch-read-buffer))
1253 ((or (eofp ch) (not (setq dig (digit-char-p ch *read-base*)))))
1254 (setq denominator (+ (* denominator *read-base*) dig)))
1255 (let ((num (/ numerator denominator)))
1256 (if negative-number (- num) num))))
1258 ;;;; cruft for dispatch macros
1260 (defun make-char-dispatch-table ()
1261 (make-array char-code-limit :initial-element #'dispatch-char-error))
1263 (defun dispatch-char-error (stream sub-char ignore)
1264 (declare (ignore ignore))
1267 (%reader-error stream "no dispatch function defined for ~S" sub-char)))
1269 (defun make-dispatch-macro-character (char &optional
1270 (non-terminating-p nil)
1273 "Cause CHAR to become a dispatching macro character in readtable (which
1274 defaults to the current readtable). If NON-TERMINATING-P, the char will
1275 be non-terminating."
1276 (set-macro-character char #'read-dispatch-char non-terminating-p rt)
1277 (let* ((dalist (dispatch-tables rt))
1278 (dtable (cdr (find char dalist :test #'char= :key #'car))))
1280 (error "The dispatch character ~S already exists." char))
1282 (setf (dispatch-tables rt)
1283 (push (cons char (make-char-dispatch-table)) dalist)))))
1286 (defun set-dispatch-macro-character (disp-char sub-char function
1287 &optional (rt *readtable*))
1289 "Cause FUNCTION to be called whenever the reader reads DISP-CHAR
1290 followed by SUB-CHAR."
1291 ;; Get the dispatch char for macro (error if not there), diddle
1292 ;; entry for sub-char.
1293 (when (digit-char-p sub-char)
1294 (error "SUB-CHAR must not be a decimal digit: ~S" sub-char))
1295 (let* ((sub-char (char-upcase sub-char))
1296 (rt (or rt *standard-readtable*))
1297 (dpair (find disp-char (dispatch-tables rt)
1298 :test #'char= :key #'car)))
1300 (setf (elt (the simple-vector (cdr dpair))
1301 (char-code sub-char))
1302 (coerce function 'function))
1303 (error "~S is not a dispatch char." disp-char))))
1305 (defun get-dispatch-macro-character (disp-char sub-char
1306 &optional (rt *readtable*))
1308 "Return the macro character function for SUB-CHAR under DISP-CHAR
1309 or NIL if there is no associated function."
1310 (unless (digit-char-p sub-char)
1311 (let* ((sub-char (char-upcase sub-char))
1312 (rt (or rt *standard-readtable*))
1313 (dpair (find disp-char (dispatch-tables rt)
1314 :test #'char= :key #'car)))
1316 (elt (the simple-vector (cdr dpair))
1317 (char-code sub-char))
1318 (error "~S is not a dispatch char." disp-char)))))
1320 (defun read-dispatch-char (stream char)
1321 ;; Read some digits.
1325 (do* ((ch (read-char stream nil *eof-object*)
1326 (read-char stream nil *eof-object*))
1329 (not (setq dig (digit-char-p ch))))
1330 ;; Take care of the extra char.
1332 (reader-eof-error stream "inside dispatch character")
1333 (setq sub-char (char-upcase ch))))
1335 (setq numarg (+ (* numarg 10) dig)))
1336 ;; Look up the function and call it.
1337 (let ((dpair (find char (dispatch-tables *readtable*)
1338 :test #'char= :key #'car)))
1340 (funcall (the function
1341 (elt (the simple-vector (cdr dpair))
1342 (char-code sub-char)))
1343 stream sub-char (if numargp numarg nil))
1344 (%reader-error stream "no dispatch table for dispatch char")))))
1346 ;;;; READ-FROM-STRING
1348 ;;; FIXME: Is it really worth keeping this pool?
1349 (defvar *read-from-string-spares* ()
1351 "A resource of string streams for Read-From-String.")
1353 (defun read-from-string (string &optional eof-error-p eof-value
1355 preserve-whitespace)
1357 "The characters of string are successively given to the lisp reader
1358 and the lisp object built by the reader is returned. Macro chars
1360 (declare (string string))
1361 (with-array-data ((string string)
1363 (end (or end (length string))))
1364 (unless *read-from-string-spares*
1365 (push (internal-make-string-input-stream "" 0 0)
1366 *read-from-string-spares*))
1367 (let ((stream (pop *read-from-string-spares*)))
1368 (setf (string-input-stream-string stream) string)
1369 (setf (string-input-stream-current stream) start)
1370 (setf (string-input-stream-end stream) end)
1372 (values (if preserve-whitespace
1373 (read-preserving-whitespace stream eof-error-p eof-value)
1374 (read stream eof-error-p eof-value))
1375 (string-input-stream-current stream))
1376 (push stream *read-from-string-spares*)))))
1380 (defun parse-integer (string &key (start 0) end (radix 10) junk-allowed)
1382 "Examine the substring of string delimited by start and end
1383 (default to the beginning and end of the string) It skips over
1384 whitespace characters and then tries to parse an integer. The
1385 radix parameter must be between 2 and 36."
1386 (with-array-data ((string string)
1388 (end (or end (length string))))
1389 (let ((index (do ((i start (1+ i)))
1392 (return-from parse-integer (values nil end))
1393 (error "no non-whitespace characters in number")))
1394 (declare (fixnum i))
1395 (unless (whitespacep (char string i)) (return i))))
1399 (declare (fixnum index))
1400 (let ((char (char string index)))
1401 (cond ((char= char #\-)
1407 (when (= index end) (return nil))
1408 (let* ((char (char string index))
1409 (weight (digit-char-p char radix)))
1411 (setq result (+ weight (* result radix))
1413 (junk-allowed (return nil))
1415 (do ((jndex (1+ index) (1+ jndex)))
1417 (declare (fixnum jndex))
1418 (unless (whitespacep (char string jndex))
1419 (error "junk in string ~S" string)))
1422 (error "junk in string ~S" string))))
1426 (if minusp (- result) result)
1429 (error "no digits in string ~S" string)))
1432 ;;;; reader initialization code
1434 (defun !reader-cold-init ()
1435 (!cold-init-read-buffer)
1436 (!cold-init-secondary-attribute-table)
1437 (!cold-init-standard-readtable)
1438 ;; FIXME: This was commented out, but should probably be restored.
1439 #+nil (!cold-init-integer-reader))
1441 (def!method print-object ((readtable readtable) stream)
1442 (print-unreadable-object (readtable stream :identity t :type t)))