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 `(>= (get-cat-entry ,char ,rt) +char-attr-constituent+))
123 (defmacro terminating-macrop (char &optional (rt '*readtable*))
124 `(test-attribute ,char +char-attr-terminating-macro+ ,rt))
126 (defmacro escapep (char &optional (rt '*readtable*))
127 `(test-attribute ,char +char-attr-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 ;;;; secondary attribute table
138 ;;; There are a number of "secondary" attributes which are constant
139 ;;; properties of characters (as long as they are constituents).
141 (defvar *secondary-attribute-table*)
142 (declaim (type attribute-table *secondary-attribute-table*))
144 (defun !set-secondary-attribute (char attribute)
145 (setf (elt *secondary-attribute-table* (char-code char))
148 (defun !cold-init-secondary-attribute-table ()
149 (setq *secondary-attribute-table*
150 (make-array base-char-code-limit :element-type '(unsigned-byte 8)
151 :initial-element +char-attr-constituent+))
152 (!set-secondary-attribute #\: +char-attr-package-delimiter+)
153 (!set-secondary-attribute #\| +char-attr-multiple-escape+) ; |) [for EMACS]
154 (!set-secondary-attribute #\. +char-attr-constituent-dot+)
155 (!set-secondary-attribute #\+ +char-attr-constituent-sign+)
156 (!set-secondary-attribute #\- +char-attr-constituent-sign+)
157 (!set-secondary-attribute #\/ +char-attr-constituent-slash+)
158 (do ((i (char-code #\0) (1+ i)))
159 ((> i (char-code #\9)))
160 (!set-secondary-attribute (code-char i) +char-attr-constituent-digit+))
161 (!set-secondary-attribute #\E +char-attr-constituent-expt+)
162 (!set-secondary-attribute #\F +char-attr-constituent-expt+)
163 (!set-secondary-attribute #\D +char-attr-constituent-expt+)
164 (!set-secondary-attribute #\S +char-attr-constituent-expt+)
165 (!set-secondary-attribute #\L +char-attr-constituent-expt+)
166 (!set-secondary-attribute #\e +char-attr-constituent-expt+)
167 (!set-secondary-attribute #\f +char-attr-constituent-expt+)
168 (!set-secondary-attribute #\d +char-attr-constituent-expt+)
169 (!set-secondary-attribute #\s +char-attr-constituent-expt+)
170 (!set-secondary-attribute #\l +char-attr-constituent-expt+))
172 (defmacro get-secondary-attribute (char)
173 `(elt *secondary-attribute-table*
176 ;;;; readtable operations
178 (defun shallow-replace/eql-hash-table (to from)
179 (maphash (lambda (k v) (setf (gethash k to) v)) from))
181 (defun copy-readtable (&optional (from-readtable *readtable*)
183 (let ((really-from-readtable (or from-readtable *standard-readtable*))
184 (really-to-readtable (or to-readtable (make-readtable))))
185 (replace (character-attribute-array really-to-readtable)
186 (character-attribute-array really-from-readtable))
187 (shallow-replace/eql-hash-table
188 (character-attribute-hash-table really-to-readtable)
189 (character-attribute-hash-table really-from-readtable))
190 (replace (character-macro-array really-to-readtable)
191 (character-macro-array really-from-readtable))
192 (shallow-replace/eql-hash-table
193 (character-macro-hash-table really-to-readtable)
194 (character-macro-hash-table really-from-readtable))
195 (setf (dispatch-tables really-to-readtable)
196 (mapcar (lambda (pair)
198 (let ((table (make-hash-table)))
199 (shallow-replace/eql-hash-table table (cdr pair))
201 (dispatch-tables really-from-readtable)))
202 (setf (readtable-case really-to-readtable)
203 (readtable-case really-from-readtable))
204 really-to-readtable))
206 (defun set-syntax-from-char (to-char from-char &optional
207 (to-readtable *readtable*)
210 "Causes the syntax of TO-CHAR to be the same as FROM-CHAR in the
211 optional readtable (defaults to the current readtable). The
212 FROM-TABLE defaults to the standard Lisp readtable when NIL."
213 (let ((really-from-readtable (or from-readtable *standard-readtable*)))
214 ;; Copy FROM-CHAR entries to TO-CHAR entries, but make sure that if
215 ;; FROM-CHAR is a constituent you don't copy non-movable secondary
216 ;; attributes (constituent types), and that said attributes magically
217 ;; appear if you transform a non-constituent to a constituent.
218 (let ((att (get-cat-entry from-char really-from-readtable)))
219 (if (constituentp from-char really-from-readtable)
220 (setq att (get-secondary-attribute to-char)))
221 (set-cat-entry to-char att to-readtable)
222 (set-cmt-entry to-char
223 (get-raw-cmt-entry from-char really-from-readtable)
227 (defun set-macro-character (char function &optional
228 (non-terminatingp nil)
229 (readtable *readtable*))
231 "Causes CHAR to be a macro character which invokes FUNCTION when seen
232 by the reader. The NON-TERMINATINGP flag can be used to make the macro
233 character non-terminating, i.e. embeddable in a symbol name."
234 (let ((designated-readtable (or readtable *standard-readtable*)))
237 (get-secondary-attribute char)
238 +char-attr-terminating-macro+)
239 designated-readtable)
240 (set-cmt-entry char function designated-readtable)
241 t)) ; (ANSI-specified return value)
243 (defun get-macro-character (char &optional (readtable *readtable*))
245 "Return the function associated with the specified CHAR which is a macro
246 character, or NIL if there is no such function. As a second value, return
247 T if CHAR is a macro character which is non-terminating, i.e. which can
248 be embedded in a symbol name."
249 (let* ((designated-readtable (or readtable *standard-readtable*))
250 ;; the first return value: a FUNCTION if CHAR is a macro
251 ;; character, or NIL otherwise
252 (fun-value (get-raw-cmt-entry char designated-readtable)))
254 ;; NON-TERMINATING-P return value:
256 (or (constituentp char)
257 (not (terminating-macrop char)))
258 ;; ANSI's definition of GET-MACRO-CHARACTER says this
259 ;; value is NIL when CHAR is not a macro character.
260 ;; I.e. this value means not just "non-terminating
261 ;; character?" but "non-terminating macro character?".
264 ;;;; definitions to support internal programming conventions
266 (defmacro eofp (char)
267 `(eq ,char *eof-object*))
269 (defun flush-whitespace (stream)
270 ;; This flushes whitespace chars, returning the last char it read (a
271 ;; non-white one). It always gets an error on end-of-file.
272 (let ((stream (in-synonym-of stream)))
273 (if (ansi-stream-p stream)
274 (prepare-for-fast-read-char stream
275 (do ((attribute-array (character-attribute-array *readtable*))
276 (attribute-hash-table
277 (character-attribute-hash-table *readtable*))
278 (char (fast-read-char t) (fast-read-char t)))
280 (if (typep char 'base-char)
281 (aref attribute-array (char-code char))
282 (gethash char attribute-hash-table +char-attr-constituent+)))
283 +char-attr-whitespace+)
284 (done-with-fast-read-char)
287 (do ((attribute-array (character-attribute-array *readtable*))
288 (attribute-hash-table
289 (character-attribute-hash-table *readtable*))
290 (char (read-char stream nil :eof) (read-char stream nil :eof)))
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+))
298 (error 'end-of-file :stream stream)
301 ;;;; temporary initialization hack
303 (defun !cold-init-standard-readtable ()
304 (setq *standard-readtable* (make-readtable))
305 ;; All characters get boring defaults in MAKE-READTABLE. Now we
306 ;; override the boring defaults on characters which need more
307 ;; interesting behavior.
308 (let ((*readtable* *standard-readtable*))
310 (flet ((whitespaceify (char)
311 (set-cmt-entry char nil)
312 (set-cat-entry char +char-attr-whitespace+)))
313 (whitespaceify (code-char tab-char-code))
314 (whitespaceify #\linefeed)
315 (whitespaceify #\space)
316 (whitespaceify (code-char form-feed-char-code))
317 (whitespaceify (code-char return-char-code)))
319 (set-cat-entry #\\ +char-attr-escape+)
320 (set-cmt-entry #\\ nil)
322 ;; Easy macro-character definitions are in this source file.
323 (set-macro-character #\" #'read-string)
324 (set-macro-character #\' #'read-quote)
325 (set-macro-character #\( #'read-list)
326 (set-macro-character #\) #'read-right-paren)
327 (set-macro-character #\; #'read-comment)
328 ;; (The hairier macro-character definitions, for #\# and #\`, are
329 ;; defined elsewhere, in their own source files.)
332 (do ((ichar 0 (1+ ichar))
334 ((= ichar base-char-code-limit))
335 (setq char (code-char ichar))
336 (when (constituentp char *standard-readtable*)
337 (set-cat-entry char (get-secondary-attribute char))
338 (set-cmt-entry char nil)))))
340 ;;;; implementation of the read buffer
342 (defvar *read-buffer*)
343 (defvar *read-buffer-length*)
344 ;;; FIXME: Is it really helpful to have *READ-BUFFER-LENGTH* be a
345 ;;; separate variable instead of just calculating it on the fly as
346 ;;; (LENGTH *READ-BUFFER*)?
351 (declaim (type index *read-buffer-length* *inch-ptr* *ouch-ptr*))
352 (declaim (type (simple-array character (*)) *read-buffer*))
354 (defmacro reset-read-buffer ()
355 ;; Turn *READ-BUFFER* into an empty read buffer.
357 ;; *OUCH-PTR* always points to next char to write.
359 ;; *INCH-PTR* always points to next char to read.
360 (setq *inch-ptr* 0)))
362 (defun !cold-init-read-buffer ()
363 (setq *read-buffer* (make-string 512)) ; initial bufsize
364 (setq *read-buffer-length* 512)
367 ;;; FIXME I removed "THE FIXNUM"'s from OUCH-READ-BUFFER and
368 ;;; OUCH-UNREAD-BUFFER, check to make sure that Python really is smart
369 ;;; enough to make good code without them. And while I'm at it,
370 ;;; converting them from macros to inline functions might be good,
373 (defmacro ouch-read-buffer (char)
375 ;; When buffer overflow
376 (when (>= *ouch-ptr* *read-buffer-length*)
377 ;; Size should be doubled.
379 (setf (elt (the simple-string *read-buffer*) *ouch-ptr*) ,char)
380 (setq *ouch-ptr* (1+ *ouch-ptr*))))
382 ;;; macro to move *ouch-ptr* back one.
383 (defmacro ouch-unread-buffer ()
384 '(when (> *ouch-ptr* *inch-ptr*)
385 (setq *ouch-ptr* (1- (the fixnum *ouch-ptr*)))))
387 (defun grow-read-buffer ()
388 (let ((rbl (length (the simple-string *read-buffer*))))
390 (concatenate 'simple-string
393 (setq *read-buffer-length* (* 2 rbl))))
395 (defun inchpeek-read-buffer ()
396 (if (>= (the fixnum *inch-ptr*) (the fixnum *ouch-ptr*))
398 (elt *read-buffer* *inch-ptr*)))
400 (defun inch-read-buffer ()
401 (if (>= *inch-ptr* *ouch-ptr*)
404 (elt *read-buffer* *inch-ptr*)
407 (defmacro unread-buffer ()
410 (defun read-unwind-read-buffer ()
411 ;; Keep contents, but make next (INCH..) return first character.
414 (defun read-buffer-to-string ()
415 (subseq *read-buffer* 0 *ouch-ptr*))
417 ;;;; READ-PRESERVING-WHITESPACE, READ-DELIMITED-LIST, and READ
419 ;;; an alist for #=, used to keep track of objects with labels assigned that
420 ;;; have been completely read. Each entry is (integer-tag gensym-tag value).
422 ;;; KLUDGE: Should this really be an alist? It seems as though users
423 ;;; could reasonably expect N log N performance for large datasets.
424 ;;; On the other hand, it's probably very very seldom a problem in practice.
425 ;;; On the third hand, it might be just as easy to use a hash table
426 ;;; as an alist, so maybe we should. -- WHN 19991202
427 (defvar *sharp-equal-alist* ())
429 (declaim (special *standard-input*))
431 ;;; READ-PRESERVING-WHITESPACE behaves just like READ, only it makes
432 ;;; sure to leave terminating whitespace in the stream. (This is a
433 ;;; COMMON-LISP exported symbol.)
434 (defun read-preserving-whitespace (&optional (stream *standard-input*)
439 "Read from STREAM and return the value read, preserving any whitespace
440 that followed the object."
442 ;; a loop for repeating when a macro returns nothing
444 (let ((char (read-char stream eof-error-p *eof-object*)))
445 (cond ((eofp char) (return eof-value))
448 (let* ((macrofun (get-coerced-cmt-entry char *readtable*))
449 (result (multiple-value-list
450 (funcall macrofun stream char))))
451 ;; Repeat if macro returned nothing.
453 (return (unless *read-suppress* (car result)))))))))
454 (let ((*sharp-equal-alist* nil))
455 (read-preserving-whitespace stream eof-error-p eof-value t))))
457 ;;; Return NIL or a list with one thing, depending.
459 ;;; for functions that want comments to return so that they can look
460 ;;; past them. We assume CHAR is not whitespace.
461 (defun read-maybe-nothing (stream char)
462 (let ((retval (multiple-value-list
463 (funcall (get-coerced-cmt-entry char *readtable*)
466 (if retval (rplacd retval nil))))
468 (defun read (&optional (stream *standard-input*)
473 "Read the next Lisp value from STREAM, and return it."
474 (let ((result (read-preserving-whitespace stream
478 ;; This function generally discards trailing whitespace. If you
479 ;; don't want to discard trailing whitespace, call
480 ;; CL:READ-PRESERVING-WHITESPACE instead.
481 (unless (or (eql result eof-value) recursivep)
482 (let ((next-char (read-char stream nil nil)))
483 (unless (or (null next-char)
484 (whitespacep next-char))
485 (unread-char next-char stream))))
488 ;;; (This is a COMMON-LISP exported symbol.)
489 (defun read-delimited-list (endchar &optional
490 (input-stream *standard-input*)
493 "Read Lisp values from INPUT-STREAM until the next character after a
494 value's representation is ENDCHAR, and return the objects as a list."
495 (declare (ignore recursive-p))
496 (do ((char (flush-whitespace input-stream)
497 (flush-whitespace input-stream))
499 ((char= char endchar) (unless *read-suppress* (nreverse retlist)))
500 (setq retlist (nconc (read-maybe-nothing input-stream char) retlist))))
502 ;;;; basic readmacro definitions
504 ;;;; Some large, hairy subsets of readmacro definitions (backquotes
505 ;;;; and sharp macros) are not here, but in their own source files.
507 (defun read-quote (stream ignore)
508 (declare (ignore ignore))
509 (list 'quote (read stream t nil t)))
511 (defun read-comment (stream ignore)
512 (declare (ignore ignore))
513 (let ((stream (in-synonym-of stream)))
514 (if (ansi-stream-p stream)
515 (prepare-for-fast-read-char stream
516 (do ((char (fast-read-char nil nil)
517 (fast-read-char nil nil)))
518 ((or (not char) (char= char #\newline))
519 (done-with-fast-read-char))))
521 (do ((char (read-char stream nil :eof) (read-char stream nil :eof)))
522 ((or (eq char :eof) (char= char #\newline))))))
523 ;; Don't return anything.
526 (defun read-list (stream ignore)
527 (declare (ignore ignore))
528 (let* ((thelist (list nil))
530 (do ((firstchar (flush-whitespace stream) (flush-whitespace stream)))
531 ((char= firstchar #\) ) (cdr thelist))
532 (when (char= firstchar #\.)
533 (let ((nextchar (read-char stream t)))
534 (cond ((token-delimiterp nextchar)
535 (cond ((eq listtail thelist)
538 "Nothing appears before . in list."))
539 ((whitespacep nextchar)
540 (setq nextchar (flush-whitespace stream))))
542 ;; Return list containing last thing.
543 (car (read-after-dot stream nextchar)))
544 (return (cdr thelist)))
545 ;; Put back NEXTCHAR so that we can read it normally.
546 (t (unread-char nextchar stream)))))
547 ;; Next thing is not an isolated dot.
548 (let ((listobj (read-maybe-nothing stream firstchar)))
549 ;; allows the possibility that a comment was read
551 (rplacd listtail listobj)
552 (setq listtail listobj))))))
554 (defun read-after-dot (stream firstchar)
555 ;; FIRSTCHAR is non-whitespace!
557 (do ((char firstchar (flush-whitespace stream)))
559 (%reader-error stream "Nothing appears after . in list."))
560 ;; See whether there's something there.
561 (setq lastobj (read-maybe-nothing stream char))
562 (when lastobj (return t)))
563 ;; At least one thing appears after the dot.
564 ;; Check for more than one thing following dot.
565 (do ((lastchar (flush-whitespace stream)
566 (flush-whitespace stream)))
567 ((char= lastchar #\) ) lastobj) ;success!
568 ;; Try reading virtual whitespace.
569 (if (read-maybe-nothing stream lastchar)
570 (%reader-error stream "More than one object follows . in list.")))))
572 (defun read-string (stream closech)
573 ;; This accumulates chars until it sees same char that invoked it.
574 ;; For a very long string, this could end up bloating the read buffer.
576 (let ((stream (in-synonym-of stream)))
577 (if (ansi-stream-p stream)
578 (prepare-for-fast-read-char stream
579 (do ((char (fast-read-char t) (fast-read-char t)))
580 ((char= char closech)
581 (done-with-fast-read-char))
582 (if (escapep char) (setq char (fast-read-char t)))
583 (ouch-read-buffer char)))
585 (do ((char (read-char stream nil :eof) (read-char stream nil :eof)))
586 ((or (eq char :eof) (char= char closech))
588 (error 'end-of-file :stream stream)))
590 (setq char (read-char stream nil :eof))
592 (error 'end-of-file :stream stream)))
593 (ouch-read-buffer char))))
594 (read-buffer-to-string))
596 (defun read-right-paren (stream ignore)
597 (declare (ignore ignore))
598 (%reader-error stream "unmatched close parenthesis"))
600 ;;; Read from the stream up to the next delimiter. Leave the resulting
601 ;;; token in *READ-BUFFER*, and return two values:
602 ;;; -- a list of the escaped character positions, and
603 ;;; -- The position of the first package delimiter (or NIL).
604 (defun internal-read-extended-token (stream firstchar escape-firstchar)
607 (when escape-firstchar
608 (push *ouch-ptr* escapes)
609 (ouch-read-buffer firstchar)
610 (setq firstchar (read-char stream nil *eof-object*)))
611 (do ((char firstchar (read-char stream nil *eof-object*))
613 ((cond ((eofp char) t)
614 ((token-delimiterp char)
615 (unread-char char stream)
618 (values escapes colon))
619 (cond ((escapep char)
620 ;; It can't be a number, even if it's 1\23.
621 ;; Read next char here, so it won't be casified.
622 (push *ouch-ptr* escapes)
623 (let ((nextchar (read-char stream nil *eof-object*)))
625 (reader-eof-error stream "after escape character")
626 (ouch-read-buffer nextchar))))
627 ((multiple-escape-p char)
628 ;; Read to next multiple-escape, escaping single chars
631 (let ((ch (read-char stream nil *eof-object*)))
634 (reader-eof-error stream "inside extended token"))
635 ((multiple-escape-p ch) (return))
637 (let ((nextchar (read-char stream nil *eof-object*)))
638 (cond ((eofp nextchar)
639 (reader-eof-error stream "after escape character"))
641 (push *ouch-ptr* escapes)
642 (ouch-read-buffer nextchar)))))
644 (push *ouch-ptr* escapes)
645 (ouch-read-buffer ch))))))
647 (when (and (constituentp char)
648 (eql (get-secondary-attribute char)
649 +char-attr-package-delimiter+)
651 (setq colon *ouch-ptr*))
652 (ouch-read-buffer char))))))
654 ;;;; character classes
656 ;;; Return the character class for CHAR.
658 ;;; FIXME: why aren't these ATT-getting forms using GET-CAT-ENTRY?
659 ;;; Because we've cached the readtable tables?
660 (defmacro char-class (char attarray atthash)
661 `(let ((att (if (typep ,char 'base-char)
662 (aref ,attarray (char-code ,char))
663 (gethash ,char ,atthash +char-attr-constituent+))))
664 (declare (fixnum att))
665 (if (<= att +char-attr-terminating-macro+)
666 +char-attr-delimiter+
669 ;;; Return the character class for CHAR, which might be part of a
671 (defmacro char-class2 (char attarray atthash)
672 `(let ((att (if (typep ,char 'base-char)
673 (aref ,attarray (char-code ,char))
674 (gethash ,char ,atthash +char-attr-constituent+))))
675 (declare (fixnum att))
676 (if (<= att +char-attr-terminating-macro+)
677 +char-attr-delimiter+
678 (if (digit-char-p ,char *read-base*)
679 +char-attr-constituent-digit+
680 (if (= att +char-attr-constituent-digit+)
681 +char-attr-constituent+
684 ;;; Return the character class for a char which might be part of a
685 ;;; rational or floating number. (Assume that it is a digit if it
687 (defmacro char-class3 (char attarray atthash)
688 `(let ((att (if (typep ,char 'base-char)
689 (aref ,attarray (char-code ,char))
690 (gethash ,char ,atthash +char-attr-constituent+))))
691 (declare (fixnum att))
692 (if possibly-rational
693 (setq possibly-rational
694 (or (digit-char-p ,char *read-base*)
695 (= att +char-attr-constituent-slash+))))
698 (or (digit-char-p ,char 10)
699 (= att +char-attr-constituent-dot+))))
700 (if (<= att +char-attr-terminating-macro+)
701 +char-attr-delimiter+
702 (if (digit-char-p ,char (max *read-base* 10))
703 (if (digit-char-p ,char *read-base*)
704 (if (= att +char-attr-constituent-expt+)
705 +char-attr-constituent-digit-or-expt+
706 +char-attr-constituent-digit+)
707 +char-attr-constituent-decimal-digit+)
712 (defvar *read-suppress* nil
714 "Suppress most interpreting in the reader when T.")
716 (defvar *read-base* 10
718 "the radix that Lisp reads numbers in")
719 (declaim (type (integer 2 36) *read-base*))
721 ;;; Modify the read buffer according to READTABLE-CASE, ignoring
722 ;;; ESCAPES. ESCAPES is a list of the escaped indices, in reverse
724 (defun casify-read-buffer (escapes)
725 (let ((case (readtable-case *readtable*)))
727 ((and (null escapes) (eq case :upcase))
728 (dotimes (i *ouch-ptr*)
729 (setf (schar *read-buffer* i)
730 (char-upcase (schar *read-buffer* i)))))
731 ((eq case :preserve))
733 (macrolet ((skip-esc (&body body)
734 `(do ((i (1- *ouch-ptr*) (1- i))
738 (when (or (null escapes)
739 (let ((esc (first escapes)))
740 (declare (fixnum esc))
746 (let ((ch (schar *read-buffer* i)))
749 (skip-esc (setf (schar *read-buffer* i) (char-downcase ch))))
751 (skip-esc (setf (schar *read-buffer* i) (char-upcase ch)))))
754 (:downcase (lower-em))
759 (when (both-case-p ch)
760 (if (upper-case-p ch)
762 (setq all-upper nil))))
763 (cond (all-lower (raise-em))
764 (all-upper (lower-em))))))))))))
766 (defun read-token (stream firstchar)
768 "This function is just an fsm that recognizes numbers and symbols."
769 ;; Check explicitly whether FIRSTCHAR has an entry for
770 ;; NON-TERMINATING in CHARACTER-ATTRIBUTE-TABLE and
771 ;; READ-DOT-NUMBER-SYMBOL in CMT. Report an error if these are
772 ;; violated. (If we called this, we want something that is a
773 ;; legitimate token!) Read in the longest possible string satisfying
774 ;; the Backus-Naur form for "unqualified-token". Leave the result in
775 ;; the *READ-BUFFER*. Return next char after token (last char read).
776 (when *read-suppress*
777 (internal-read-extended-token stream firstchar nil)
778 (return-from read-token nil))
779 (let ((attribute-array (character-attribute-array *readtable*))
780 (attribute-hash-table (character-attribute-hash-table *readtable*))
781 (package-designator nil)
783 (possibly-rational t)
784 (seen-digit-or-expt nil)
786 (was-possibly-float nil)
788 (seen-multiple-escapes nil))
790 (prog ((char firstchar))
791 (case (char-class3 char attribute-array attribute-hash-table)
792 (#.+char-attr-constituent-sign+ (go SIGN))
793 (#.+char-attr-constituent-digit+ (go LEFTDIGIT))
794 (#.+char-attr-constituent-digit-or-expt+
795 (setq seen-digit-or-expt t)
797 (#.+char-attr-constituent-decimal-digit+ (go LEFTDECIMALDIGIT))
798 (#.+char-attr-constituent-dot+ (go FRONTDOT))
799 (#.+char-attr-escape+ (go ESCAPE))
800 (#.+char-attr-package-delimiter+ (go COLON))
801 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
802 ;; can't have eof, whitespace, or terminating macro as first char!
805 (ouch-read-buffer char)
806 (setq char (read-char stream nil nil))
807 (unless char (go RETURN-SYMBOL))
808 (setq possibly-rational t
810 (case (char-class3 char attribute-array attribute-hash-table)
811 (#.+char-attr-constituent-digit+ (go LEFTDIGIT))
812 (#.+char-attr-constituent-digit-or-expt+
813 (setq seen-digit-or-expt t)
815 (#.+char-attr-constituent-decimal-digit+ (go LEFTDECIMALDIGIT))
816 (#.+char-attr-constituent-dot+ (go SIGNDOT))
817 (#.+char-attr-escape+ (go ESCAPE))
818 (#.+char-attr-package-delimiter+ (go COLON))
819 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
820 (#.+char-attr-delimiter+ (unread-char char stream) (go RETURN-SYMBOL))
822 LEFTDIGIT ; saw "[sign] {digit}+"
823 (ouch-read-buffer char)
824 (setq char (read-char stream nil nil))
825 (unless char (return (make-integer)))
826 (setq was-possibly-float possibly-float)
827 (case (char-class3 char attribute-array attribute-hash-table)
828 (#.+char-attr-constituent-digit+ (go LEFTDIGIT))
829 (#.+char-attr-constituent-decimal-digit+ (if possibly-float
830 (go LEFTDECIMALDIGIT)
832 (#.+char-attr-constituent-dot+ (if possibly-float
835 (#.+char-attr-constituent-digit-or-expt+
836 (if (or seen-digit-or-expt (not was-possibly-float))
837 (progn (setq seen-digit-or-expt t) (go LEFTDIGIT))
838 (progn (setq seen-digit-or-expt t) (go LEFTDIGIT-OR-EXPT))))
839 (#.+char-attr-constituent-expt+
840 (if was-possibly-float
843 (#.+char-attr-constituent-slash+ (if possibly-rational
846 (#.+char-attr-delimiter+ (unread-char char stream)
847 (return (make-integer)))
848 (#.+char-attr-escape+ (go ESCAPE))
849 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
850 (#.+char-attr-package-delimiter+ (go COLON))
853 (ouch-read-buffer char)
854 (setq char (read-char stream nil nil))
855 (unless char (return (make-integer)))
856 (case (char-class3 char attribute-array attribute-hash-table)
857 (#.+char-attr-constituent-digit+ (go LEFTDIGIT))
858 (#.+char-attr-constituent-decimal-digit+ (bug "impossible!"))
859 (#.+char-attr-constituent-dot+ (go SYMBOL))
860 (#.+char-attr-constituent-digit-or-expt+ (go LEFTDIGIT))
861 (#.+char-attr-constituent-expt+ (go SYMBOL))
862 (#.+char-attr-constituent-sign+ (go EXPTSIGN))
863 (#.+char-attr-constituent-slash+ (if possibly-rational
866 (#.+char-attr-delimiter+ (unread-char char stream)
867 (return (make-integer)))
868 (#.+char-attr-escape+ (go ESCAPE))
869 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
870 (#.+char-attr-package-delimiter+ (go COLON))
872 LEFTDECIMALDIGIT ; saw "[sign] {decimal-digit}+"
873 (aver possibly-float)
874 (ouch-read-buffer char)
875 (setq char (read-char stream nil nil))
876 (unless char (go RETURN-SYMBOL))
877 (case (char-class char attribute-array attribute-hash-table)
878 (#.+char-attr-constituent-digit+ (go LEFTDECIMALDIGIT))
879 (#.+char-attr-constituent-dot+ (go MIDDLEDOT))
880 (#.+char-attr-constituent-expt+ (go EXPONENT))
881 (#.+char-attr-constituent-slash+ (aver (not possibly-rational))
883 (#.+char-attr-delimiter+ (unread-char char stream)
885 (#.+char-attr-escape+ (go ESCAPE))
886 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
887 (#.+char-attr-package-delimiter+ (go COLON))
889 MIDDLEDOT ; saw "[sign] {digit}+ dot"
890 (ouch-read-buffer char)
891 (setq char (read-char stream nil nil))
892 (unless char (return (let ((*read-base* 10))
894 (case (char-class char attribute-array attribute-hash-table)
895 (#.+char-attr-constituent-digit+ (go RIGHTDIGIT))
896 (#.+char-attr-constituent-expt+ (go EXPONENT))
897 (#.+char-attr-delimiter+
898 (unread-char char stream)
899 (return (let ((*read-base* 10))
901 (#.+char-attr-escape+ (go ESCAPE))
902 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
903 (#.+char-attr-package-delimiter+ (go COLON))
905 RIGHTDIGIT ; saw "[sign] {decimal-digit}* dot {digit}+"
906 (ouch-read-buffer char)
907 (setq char (read-char stream nil nil))
908 (unless char (return (make-float stream)))
909 (case (char-class char attribute-array attribute-hash-table)
910 (#.+char-attr-constituent-digit+ (go RIGHTDIGIT))
911 (#.+char-attr-constituent-expt+ (go EXPONENT))
912 (#.+char-attr-delimiter+
913 (unread-char char stream)
914 (return (make-float stream)))
915 (#.+char-attr-escape+ (go ESCAPE))
916 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
917 (#.+char-attr-package-delimiter+ (go COLON))
919 SIGNDOT ; saw "[sign] dot"
920 (ouch-read-buffer char)
921 (setq char (read-char stream nil nil))
922 (unless char (go RETURN-SYMBOL))
923 (case (char-class char attribute-array attribute-hash-table)
924 (#.+char-attr-constituent-digit+ (go RIGHTDIGIT))
925 (#.+char-attr-delimiter+ (unread-char char stream) (go RETURN-SYMBOL))
926 (#.+char-attr-escape+ (go ESCAPE))
927 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
930 (ouch-read-buffer char)
931 (setq char (read-char stream nil nil))
932 (unless char (%reader-error stream "dot context error"))
933 (case (char-class char attribute-array attribute-hash-table)
934 (#.+char-attr-constituent-digit+ (go RIGHTDIGIT))
935 (#.+char-attr-constituent-dot+ (go DOTS))
936 (#.+char-attr-delimiter+ (%reader-error stream "dot context error"))
937 (#.+char-attr-escape+ (go ESCAPE))
938 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
939 (#.+char-attr-package-delimiter+ (go COLON))
942 (ouch-read-buffer char)
943 (setq char (read-char stream nil nil))
944 (unless char (go RETURN-SYMBOL))
945 (setq possibly-float t)
946 (case (char-class char attribute-array attribute-hash-table)
947 (#.+char-attr-constituent-sign+ (go EXPTSIGN))
948 (#.+char-attr-constituent-digit+ (go EXPTDIGIT))
949 (#.+char-attr-delimiter+ (unread-char char stream) (go RETURN-SYMBOL))
950 (#.+char-attr-escape+ (go ESCAPE))
951 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
952 (#.+char-attr-package-delimiter+ (go COLON))
954 EXPTSIGN ; got to EXPONENT, and saw a sign character
955 (ouch-read-buffer char)
956 (setq char (read-char stream nil nil))
957 (unless char (go RETURN-SYMBOL))
958 (case (char-class char attribute-array attribute-hash-table)
959 (#.+char-attr-constituent-digit+ (go EXPTDIGIT))
960 (#.+char-attr-delimiter+ (unread-char char stream) (go RETURN-SYMBOL))
961 (#.+char-attr-escape+ (go ESCAPE))
962 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
963 (#.+char-attr-package-delimiter+ (go COLON))
965 EXPTDIGIT ; got to EXPONENT, saw "[sign] {digit}+"
966 (ouch-read-buffer char)
967 (setq char (read-char stream nil nil))
968 (unless char (return (make-float stream)))
969 (case (char-class char attribute-array attribute-hash-table)
970 (#.+char-attr-constituent-digit+ (go EXPTDIGIT))
971 (#.+char-attr-delimiter+
972 (unread-char char stream)
973 (return (make-float stream)))
974 (#.+char-attr-escape+ (go ESCAPE))
975 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
976 (#.+char-attr-package-delimiter+ (go COLON))
978 RATIO ; saw "[sign] {digit}+ slash"
979 (ouch-read-buffer char)
980 (setq char (read-char stream nil nil))
981 (unless char (go RETURN-SYMBOL))
982 (case (char-class2 char attribute-array attribute-hash-table)
983 (#.+char-attr-constituent-digit+ (go RATIODIGIT))
984 (#.+char-attr-delimiter+ (unread-char char stream) (go RETURN-SYMBOL))
985 (#.+char-attr-escape+ (go ESCAPE))
986 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
987 (#.+char-attr-package-delimiter+ (go COLON))
989 RATIODIGIT ; saw "[sign] {digit}+ slash {digit}+"
990 (ouch-read-buffer char)
991 (setq char (read-char stream nil nil))
992 (unless char (return (make-ratio stream)))
993 (case (char-class2 char attribute-array attribute-hash-table)
994 (#.+char-attr-constituent-digit+ (go RATIODIGIT))
995 (#.+char-attr-delimiter+
996 (unread-char char stream)
997 (return (make-ratio stream)))
998 (#.+char-attr-escape+ (go ESCAPE))
999 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1000 (#.+char-attr-package-delimiter+ (go COLON))
1002 DOTS ; saw "dot {dot}+"
1003 (ouch-read-buffer char)
1004 (setq char (read-char stream nil nil))
1005 (unless char (%reader-error stream "too many dots"))
1006 (case (char-class char attribute-array attribute-hash-table)
1007 (#.+char-attr-constituent-dot+ (go DOTS))
1008 (#.+char-attr-delimiter+
1009 (unread-char char stream)
1010 (%reader-error stream "too many dots"))
1011 (#.+char-attr-escape+ (go ESCAPE))
1012 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1013 (#.+char-attr-package-delimiter+ (go COLON))
1015 SYMBOL ; not a dot, dots, or number
1016 (let ((stream (in-synonym-of stream)))
1017 (if (ansi-stream-p stream)
1018 (prepare-for-fast-read-char stream
1021 (ouch-read-buffer char)
1022 (setq char (fast-read-char nil nil))
1023 (unless char (go RETURN-SYMBOL))
1024 (case (char-class char attribute-array attribute-hash-table)
1025 (#.+char-attr-escape+ (done-with-fast-read-char)
1027 (#.+char-attr-delimiter+ (done-with-fast-read-char)
1028 (unread-char char stream)
1030 (#.+char-attr-multiple-escape+ (done-with-fast-read-char)
1032 (#.+char-attr-package-delimiter+ (done-with-fast-read-char)
1034 (t (go SYMBOL-LOOP)))))
1038 (ouch-read-buffer char)
1039 (setq char (read-char stream nil :eof))
1040 (when (eq char :eof) (go RETURN-SYMBOL))
1041 (case (char-class char attribute-array attribute-hash-table)
1042 (#.+char-attr-escape+ (go ESCAPE))
1043 (#.+char-attr-delimiter+ (unread-char char stream)
1045 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1046 (#.+char-attr-package-delimiter+ (go COLON))
1047 (t (go SYMBOL-LOOP))))))
1048 ESCAPE ; saw an escape
1049 ;; Don't put the escape in the read buffer.
1050 ;; READ-NEXT CHAR, put in buffer (no case conversion).
1051 (let ((nextchar (read-char stream nil nil)))
1053 (reader-eof-error stream "after escape character"))
1054 (push *ouch-ptr* escapes)
1055 (ouch-read-buffer nextchar))
1056 (setq char (read-char stream nil nil))
1057 (unless char (go RETURN-SYMBOL))
1058 (case (char-class char attribute-array attribute-hash-table)
1059 (#.+char-attr-delimiter+ (unread-char char stream) (go RETURN-SYMBOL))
1060 (#.+char-attr-escape+ (go ESCAPE))
1061 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1062 (#.+char-attr-package-delimiter+ (go COLON))
1065 (setq seen-multiple-escapes t)
1066 (do ((char (read-char stream t) (read-char stream t)))
1067 ((multiple-escape-p char))
1068 (if (escapep char) (setq char (read-char stream t)))
1069 (push *ouch-ptr* escapes)
1070 (ouch-read-buffer char))
1071 (setq char (read-char stream nil nil))
1072 (unless char (go RETURN-SYMBOL))
1073 (case (char-class char attribute-array attribute-hash-table)
1074 (#.+char-attr-delimiter+ (unread-char char stream) (go RETURN-SYMBOL))
1075 (#.+char-attr-escape+ (go ESCAPE))
1076 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1077 (#.+char-attr-package-delimiter+ (go COLON))
1080 (casify-read-buffer escapes)
1081 (unless (zerop colons)
1082 (%reader-error stream "too many colons in ~S"
1083 (read-buffer-to-string)))
1085 (setq package-designator
1086 (if (plusp *ouch-ptr*)
1087 ;; FIXME: It seems inefficient to cons up a package
1088 ;; designator string every time we read a symbol with an
1089 ;; explicit package prefix. Perhaps we could implement
1090 ;; a FIND-PACKAGE* function analogous to INTERN*
1092 (read-buffer-to-string)
1093 (if seen-multiple-escapes
1094 (read-buffer-to-string)
1095 *keyword-package*)))
1098 (setq char (read-char stream nil nil))
1099 (unless char (reader-eof-error stream "after reading a colon"))
1100 (case (char-class char attribute-array attribute-hash-table)
1101 (#.+char-attr-delimiter+
1102 (unread-char char stream)
1103 (%reader-error stream
1104 "illegal terminating character after a colon: ~S"
1106 (#.+char-attr-escape+ (go ESCAPE))
1107 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1108 (#.+char-attr-package-delimiter+ (go INTERN))
1112 (setq char (read-char stream nil nil))
1114 (reader-eof-error stream "after reading a colon"))
1115 (case (char-class char attribute-array attribute-hash-table)
1116 (#.+char-attr-delimiter+
1117 (unread-char char stream)
1118 (%reader-error stream
1119 "illegal terminating character after a colon: ~S"
1121 (#.+char-attr-escape+ (go ESCAPE))
1122 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1123 (#.+char-attr-package-delimiter+
1124 (%reader-error stream
1125 "too many colons after ~S name"
1126 package-designator))
1129 (casify-read-buffer escapes)
1130 (let ((found (if package-designator
1131 (find-package package-designator)
1134 (error 'reader-package-error :stream stream
1135 :format-arguments (list package-designator)
1136 :format-control "package ~S not found"))
1138 (if (or (zerop colons) (= colons 2) (eq found *keyword-package*))
1139 (return (intern* *read-buffer* *ouch-ptr* found))
1140 (multiple-value-bind (symbol test)
1141 (find-symbol* *read-buffer* *ouch-ptr* found)
1142 (when (eq test :external) (return symbol))
1143 (let ((name (read-buffer-to-string)))
1144 (with-simple-restart (continue "Use symbol anyway.")
1145 (error 'reader-package-error :stream stream
1146 :format-arguments (list name (package-name found))
1149 "The symbol ~S is not external in the ~A package."
1150 "Symbol ~S not found in the ~A package.")))
1151 (return (intern name found)))))))))
1153 ;;; for semi-external use:
1155 ;;; For semi-external use: Return 3 values: the string for the token,
1156 ;;; a flag for whether there was an escape char, and the position of
1157 ;;; any package delimiter.
1158 (defun read-extended-token (stream &optional (*readtable* *readtable*))
1159 (let ((first-char (read-char stream nil nil t)))
1161 (multiple-value-bind (escapes colon)
1162 (internal-read-extended-token stream first-char nil)
1163 (casify-read-buffer escapes)
1164 (values (read-buffer-to-string) (not (null escapes)) colon)))
1166 (values "" nil nil)))))
1168 ;;; for semi-external use:
1170 ;;; Read an extended token with the first character escaped. Return
1171 ;;; the string for the token.
1172 (defun read-extended-token-escaped (stream &optional (*readtable* *readtable*))
1173 (let ((first-char (read-char stream nil nil)))
1175 (let ((escapes (internal-read-extended-token stream first-char t)))
1176 (casify-read-buffer escapes)
1177 (read-buffer-to-string)))
1179 (reader-eof-error stream "after escape")))))
1181 ;;;; number-reading functions
1183 (defmacro digit* nil
1184 `(do ((ch char (inch-read-buffer)))
1185 ((or (eofp ch) (not (digit-char-p ch))) (setq char ch))
1186 ;; Report if at least one digit is seen.
1187 (setq one-digit t)))
1189 (defmacro exponent-letterp (letter)
1190 `(memq ,letter '(#\E #\S #\F #\L #\D #\e #\s #\f #\l #\d)))
1192 ;;; FIXME: It would be cleaner to have these generated automatically
1193 ;;; by compile-time code instead of having them hand-created like
1194 ;;; this. The !COLD-INIT-INTEGER-READER code below should be resurrected
1196 (defvar *integer-reader-safe-digits*
1198 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)
1200 "the mapping of base to 'safe' number of digits to read for a fixnum")
1201 (defvar *integer-reader-base-power*
1203 67108864 129140163 67108864 48828125 60466176 40353607
1204 16777216 43046721 100000000 19487171 35831808 62748517 105413504 11390625
1205 16777216 24137569 34012224 47045881 64000000 85766121 113379904 6436343
1206 7962624 9765625 11881376 14348907 17210368 20511149 24300000 28629151
1207 33554432 39135393 45435424 52521875 60466176)
1209 "the largest fixnum power of the base for MAKE-INTEGER")
1210 (declaim (simple-vector *integer-reader-safe-digits*
1211 *integer-reader-base-power*))
1213 (defun !cold-init-integer-reader ()
1214 (do ((base 2 (1+ base)))
1217 (do ((fix (truncate most-positive-fixnum base)
1218 (truncate fix base))
1219 (digits 0 (1+ digits)))
1220 ((zerop fix) digits))))
1221 (setf (aref *integer-reader-safe-digits* base)
1223 (aref *integer-reader-base-power* base)
1224 (expt base digits)))))
1227 (defun make-integer ()
1229 "Minimizes bignum-fixnum multiplies by reading a 'safe' number of digits,
1230 then multiplying by a power of the base and adding."
1231 (let* ((base *read-base*)
1232 (digits-per (aref *integer-reader-safe-digits* base))
1233 (base-power (aref *integer-reader-base-power* base))
1236 (declare (type index digits-per base-power))
1237 (read-unwind-read-buffer)
1238 (let ((char (inch-read-buffer)))
1239 (cond ((char= char #\-)
1242 (t (unread-buffer))))
1245 (declare (type index num))
1246 (dotimes (digit digits-per)
1247 (let* ((ch (inch-read-buffer)))
1248 (cond ((or (eofp ch) (char= ch #\.))
1249 (return-from make-integer
1251 (if (zerop number) num
1253 (expt base digit))))))
1254 (if negativep (- res) res))))
1255 (t (setq num (+ (digit-char-p ch base)
1256 (the index (* num base))))))))
1257 (setq number (+ num (* number base-power)))))))
1259 (defun make-float (stream)
1260 ;; Assume that the contents of *read-buffer* are a legal float, with nothing
1262 (read-unwind-read-buffer)
1263 (let ((negative-fraction nil)
1266 (negative-exponent nil)
1269 (char (inch-read-buffer)))
1270 (if (cond ((char= char #\+) t)
1271 ((char= char #\-) (setq negative-fraction t)))
1273 (setq char (inch-read-buffer)))
1274 ;; Read digits before the dot.
1275 (do* ((ch char (inch-read-buffer))
1276 (dig (digit-char-p ch) (digit-char-p ch)))
1277 ((not dig) (setq char ch))
1278 (setq number (+ (* number 10) dig)))
1279 ;; Deal with the dot, if it's there.
1280 (when (char= char #\.)
1281 (setq char (inch-read-buffer))
1282 ;; Read digits after the dot.
1283 (do* ((ch char (inch-read-buffer))
1284 (dig (and (not (eofp ch)) (digit-char-p ch))
1285 (and (not (eofp ch)) (digit-char-p ch))))
1286 ((not dig) (setq char ch))
1287 (setq divisor (* divisor 10))
1288 (setq number (+ (* number 10) dig))))
1289 ;; Is there an exponent letter?
1291 ;; If not, we've read the whole number.
1292 (let ((num (make-float-aux number divisor
1293 *read-default-float-format*
1295 (return-from make-float (if negative-fraction (- num) num))))
1296 ((exponent-letterp char)
1297 (setq float-char char)
1299 (setq char (inch-read-buffer))
1300 ;; Check leading sign.
1301 (if (cond ((char= char #\+) t)
1302 ((char= char #\-) (setq negative-exponent t)))
1304 (setq char (inch-read-buffer)))
1305 ;; Read digits for exponent.
1306 (do* ((ch char (inch-read-buffer))
1307 (dig (and (not (eofp ch)) (digit-char-p ch))
1308 (and (not (eofp ch)) (digit-char-p ch))))
1310 (setq exponent (if negative-exponent (- exponent) exponent)))
1311 (setq exponent (+ (* exponent 10) dig)))
1312 ;; Generate and return the float, depending on FLOAT-CHAR:
1313 (let* ((float-format (case (char-upcase float-char)
1314 (#\E *read-default-float-format*)
1319 (result (make-float-aux (* (expt 10 exponent) number)
1320 divisor float-format stream)))
1321 (return-from make-float
1322 (if negative-fraction (- result) result))))
1323 (t (bug "bad fallthrough in floating point reader")))))
1325 (defun make-float-aux (number divisor float-format stream)
1327 (coerce (/ number divisor) float-format)
1329 (error 'reader-impossible-number-error
1330 :error c :stream stream
1331 :format-control "failed to build float"))))
1333 (defun make-ratio (stream)
1334 ;; Assume *READ-BUFFER* contains a legal ratio. Build the number from
1337 ;; Look for optional "+" or "-".
1338 (let ((numerator 0) (denominator 0) (char ()) (negative-number nil))
1339 (read-unwind-read-buffer)
1340 (setq char (inch-read-buffer))
1341 (cond ((char= char #\+)
1342 (setq char (inch-read-buffer)))
1344 (setq char (inch-read-buffer))
1345 (setq negative-number t)))
1347 (do* ((ch char (inch-read-buffer))
1348 (dig (digit-char-p ch *read-base*)
1349 (digit-char-p ch *read-base*)))
1351 (setq numerator (+ (* numerator *read-base*) dig)))
1353 (do* ((ch (inch-read-buffer) (inch-read-buffer))
1355 ((or (eofp ch) (not (setq dig (digit-char-p ch *read-base*)))))
1356 (setq denominator (+ (* denominator *read-base*) dig)))
1357 (let ((num (handler-case
1358 (/ numerator denominator)
1359 (arithmetic-error (c)
1360 (error 'reader-impossible-number-error
1361 :error c :stream stream
1362 :format-control "failed to build ratio")))))
1363 (if negative-number (- num) num))))
1365 ;;;; cruft for dispatch macros
1367 (defun make-char-dispatch-table ()
1370 (defun dispatch-char-error (stream sub-char ignore)
1371 (declare (ignore ignore))
1374 (%reader-error stream "no dispatch function defined for ~S" sub-char)))
1376 (defun make-dispatch-macro-character (char &optional
1377 (non-terminating-p nil)
1380 "Cause CHAR to become a dispatching macro character in readtable (which
1381 defaults to the current readtable). If NON-TERMINATING-P, the char will
1382 be non-terminating."
1383 (set-macro-character char #'read-dispatch-char non-terminating-p rt)
1384 (let* ((dalist (dispatch-tables rt))
1385 (dtable (cdr (find char dalist :test #'char= :key #'car))))
1387 (error "The dispatch character ~S already exists." char))
1389 (setf (dispatch-tables rt)
1390 (push (cons char (make-char-dispatch-table)) dalist)))))
1393 (defun set-dispatch-macro-character (disp-char sub-char function
1394 &optional (rt *readtable*))
1396 "Cause FUNCTION to be called whenever the reader reads DISP-CHAR
1397 followed by SUB-CHAR."
1398 ;; Get the dispatch char for macro (error if not there), diddle
1399 ;; entry for sub-char.
1400 (when (digit-char-p sub-char)
1401 (error "SUB-CHAR must not be a decimal digit: ~S" sub-char))
1402 (let* ((sub-char (char-upcase sub-char))
1403 (rt (or rt *standard-readtable*))
1404 (dpair (find disp-char (dispatch-tables rt)
1405 :test #'char= :key #'car)))
1407 (setf (gethash sub-char (cdr dpair)) (coerce function 'function))
1408 (error "~S is not a dispatch char." disp-char))))
1410 (defun get-dispatch-macro-character (disp-char sub-char
1411 &optional (rt *readtable*))
1413 "Return the macro character function for SUB-CHAR under DISP-CHAR
1414 or NIL if there is no associated function."
1415 (let* ((sub-char (char-upcase sub-char))
1416 (rt (or rt *standard-readtable*))
1417 (dpair (find disp-char (dispatch-tables rt)
1418 :test #'char= :key #'car)))
1420 (values (gethash sub-char (cdr dpair)))
1421 (error "~S is not a dispatch char." disp-char))))
1423 (defun read-dispatch-char (stream char)
1424 ;; Read some digits.
1428 (do* ((ch (read-char stream nil *eof-object*)
1429 (read-char stream nil *eof-object*))
1432 (not (setq dig (digit-char-p ch))))
1433 ;; Take care of the extra char.
1435 (reader-eof-error stream "inside dispatch character")
1436 (setq sub-char (char-upcase ch))))
1438 (setq numarg (+ (* numarg 10) dig)))
1439 ;; Look up the function and call it.
1440 (let ((dpair (find char (dispatch-tables *readtable*)
1441 :test #'char= :key #'car)))
1443 (funcall (the function
1444 (gethash sub-char (cdr dpair) #'dispatch-char-error))
1445 stream sub-char (if numargp numarg nil))
1446 (%reader-error stream "no dispatch table for dispatch char")))))
1448 ;;;; READ-FROM-STRING
1450 ;;; FIXME: Is it really worth keeping this pool?
1451 (defvar *read-from-string-spares* ()
1453 "A resource of string streams for Read-From-String.")
1455 (defun read-from-string (string &optional (eof-error-p t) eof-value
1457 preserve-whitespace)
1459 "The characters of string are successively given to the lisp reader
1460 and the lisp object built by the reader is returned. Macro chars
1462 (declare (string string))
1464 (with-array-data ((string string)
1466 (end (%check-vector-sequence-bounds string start end)))
1467 (unless *read-from-string-spares*
1468 (push (make-string-input-stream "" 0 0) *read-from-string-spares*))
1469 (let ((stream (pop *read-from-string-spares*)))
1470 (setf (string-input-stream-string stream)
1471 (coerce string '(simple-array character (*))))
1472 (setf (string-input-stream-current stream) start)
1473 (setf (string-input-stream-end stream) end)
1475 (values (if preserve-whitespace
1476 (read-preserving-whitespace stream eof-error-p eof-value)
1477 (read stream eof-error-p eof-value))
1478 (string-input-stream-current stream))
1479 (push stream *read-from-string-spares*)))))
1483 (defun parse-integer (string &key (start 0) end (radix 10) junk-allowed)
1485 "Examine the substring of string delimited by start and end
1486 (default to the beginning and end of the string) It skips over
1487 whitespace characters and then tries to parse an integer. The
1488 radix parameter must be between 2 and 36."
1489 (macrolet ((parse-error (format-control)
1490 `(error 'simple-parse-error
1491 :format-control ,format-control
1492 :format-arguments (list string))))
1493 (with-array-data ((string string :offset-var offset)
1495 (end (%check-vector-sequence-bounds string start end)))
1496 (let ((index (do ((i start (1+ i)))
1499 (return-from parse-integer (values nil end))
1500 (parse-error "no non-whitespace characters in string ~S.")))
1501 (declare (fixnum i))
1502 (unless (whitespacep (char string i)) (return i))))
1506 (declare (fixnum index))
1507 (let ((char (char string index)))
1508 (cond ((char= char #\-)
1514 (when (= index end) (return nil))
1515 (let* ((char (char string index))
1516 (weight (digit-char-p char radix)))
1518 (setq result (+ weight (* result radix))
1520 (junk-allowed (return nil))
1524 (when (= index end) (return))
1525 (unless (whitespacep (char string index))
1526 (parse-error "junk in string ~S")))
1529 (parse-error "junk in string ~S"))))
1533 (if minusp (- result) result)
1536 (parse-error "no digits in string ~S")))
1537 (- index offset))))))
1539 ;;;; reader initialization code
1541 (defun !reader-cold-init ()
1542 (!cold-init-read-buffer)
1543 (!cold-init-secondary-attribute-table)
1544 (!cold-init-standard-readtable)
1545 ;; FIXME: This was commented out, but should probably be restored.
1546 #+nil (!cold-init-integer-reader))
1548 (def!method print-object ((readtable readtable) stream)
1549 (print-unreadable-object (readtable stream :identity t :type t)))