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 ;;; the value actually stored in the character macro table. As per
73 ;;; ANSI #'GET-MACRO-CHARACTER and #'SET-MACRO-CHARACTER, this can
74 ;;; be either a function or NIL.
75 (eval-when (:compile-toplevel
:execute
)
76 (sb!xc
:defmacro get-raw-cmt-entry
(char readtable
)
77 `(svref (character-macro-table ,readtable
)
80 ;;; the value represented by whatever is stored in the character macro
81 ;;; table. As per ANSI #'GET-MACRO-CHARACTER and #'SET-MACRO-CHARACTER,
82 ;;; a function value represents itself, and a NIL value represents the
84 (defun get-coerced-cmt-entry (char readtable
)
86 (or (get-raw-cmt-entry char readtable
)
89 (defun set-cmt-entry (char new-value-designator
&optional
(rt *readtable
*))
90 (setf (svref (character-macro-table rt
)
92 (and new-value-designator
93 (%coerce-callable-to-fun new-value-designator
))))
95 (defun undefined-macro-char (stream char
)
96 (unless *read-suppress
*
97 (%reader-error stream
"undefined read-macro character ~S" char
)))
99 ;;; The character attribute table is a CHAR-CODE-LIMIT vector of integers.
101 (defmacro test-attribute
(char whichclass rt
)
102 `(= (the fixnum
(get-cat-entry ,char
,rt
)) ,whichclass
))
104 ;;; predicates for testing character attributes
106 #!-sb-fluid
(declaim (inline whitespacep
))
107 (defun whitespacep (char &optional
(rt *readtable
*))
108 (test-attribute char
+char-attr-whitespace
+ rt
))
110 (defmacro constituentp
(char &optional
(rt '*readtable
*))
111 `(>= (get-cat-entry ,char
,rt
) +char-attr-constituent
+))
113 (defmacro terminating-macrop
(char &optional
(rt '*readtable
*))
114 `(test-attribute ,char
+char-attr-terminating-macro
+ ,rt
))
116 (defmacro escapep
(char &optional
(rt '*readtable
*))
117 `(test-attribute ,char
+char-attr-escape
+ ,rt
))
119 (defmacro multiple-escape-p
(char &optional
(rt '*readtable
*))
120 `(test-attribute ,char
+char-attr-multiple-escape
+ ,rt
))
122 (defmacro token-delimiterp
(char &optional
(rt '*readtable
*))
123 ;; depends on actual attribute numbering above.
124 `(<= (get-cat-entry ,char
,rt
) +char-attr-terminating-macro
+))
126 ;;;; secondary attribute table
128 ;;; There are a number of "secondary" attributes which are constant
129 ;;; properties of characters (as long as they are constituents).
131 (defvar *secondary-attribute-table
*)
132 (declaim (type attribute-table
*secondary-attribute-table
*))
134 (defun !set-secondary-attribute
(char attribute
)
135 (setf (elt *secondary-attribute-table
* (char-code char
))
138 (defun !cold-init-secondary-attribute-table
()
139 (setq *secondary-attribute-table
*
140 (make-array char-code-limit
:element-type
'(unsigned-byte 8)
141 :initial-element
+char-attr-constituent
+))
142 (!set-secondary-attribute
#\
: +char-attr-package-delimiter
+)
143 (!set-secondary-attribute
#\|
+char-attr-multiple-escape
+) ; |) [for EMACS]
144 (!set-secondary-attribute
#\.
+char-attr-constituent-dot
+)
145 (!set-secondary-attribute
#\
+ +char-attr-constituent-sign
+)
146 (!set-secondary-attribute
#\-
+char-attr-constituent-sign
+)
147 (!set-secondary-attribute
#\
/ +char-attr-constituent-slash
+)
148 (do ((i (char-code #\
0) (1+ i
)))
149 ((> i
(char-code #\
9)))
150 (!set-secondary-attribute
(code-char i
) +char-attr-constituent-digit
+))
151 (!set-secondary-attribute
#\E
+char-attr-constituent-expt
+)
152 (!set-secondary-attribute
#\F
+char-attr-constituent-expt
+)
153 (!set-secondary-attribute
#\D
+char-attr-constituent-expt
+)
154 (!set-secondary-attribute
#\S
+char-attr-constituent-expt
+)
155 (!set-secondary-attribute
#\L
+char-attr-constituent-expt
+)
156 (!set-secondary-attribute
#\e
+char-attr-constituent-expt
+)
157 (!set-secondary-attribute
#\f +char-attr-constituent-expt
+)
158 (!set-secondary-attribute
#\d
+char-attr-constituent-expt
+)
159 (!set-secondary-attribute
#\s
+char-attr-constituent-expt
+)
160 (!set-secondary-attribute
#\l
+char-attr-constituent-expt
+))
162 (defmacro get-secondary-attribute
(char)
163 `(elt *secondary-attribute-table
*
166 ;;;; readtable operations
168 (defun copy-readtable (&optional
(from-readtable *readtable
*)
170 (let ((really-from-readtable (or from-readtable
*standard-readtable
*))
171 (really-to-readtable (or to-readtable
(make-readtable))))
172 (replace (character-attribute-table really-to-readtable
)
173 (character-attribute-table really-from-readtable
))
174 (replace (character-macro-table really-to-readtable
)
175 (character-macro-table really-from-readtable
))
176 (setf (dispatch-tables really-to-readtable
)
177 (mapcar (lambda (pair) (cons (car pair
)
178 (copy-seq (cdr pair
))))
179 (dispatch-tables really-from-readtable
)))
180 (setf (readtable-case really-to-readtable
)
181 (readtable-case really-from-readtable
))
182 really-to-readtable
))
184 (defun set-syntax-from-char (to-char from-char
&optional
185 (to-readtable *readtable
*)
188 "Causes the syntax of TO-CHAR to be the same as FROM-CHAR in the
189 optional readtable (defaults to the current readtable). The
190 FROM-TABLE defaults to the standard Lisp readtable when NIL."
191 (let ((really-from-readtable (or from-readtable
*standard-readtable
*)))
192 ;; Copy FROM-CHAR entries to TO-CHAR entries, but make sure that if
193 ;; FROM-CHAR is a constituent you don't copy non-movable secondary
194 ;; attributes (constituent types), and that said attributes magically
195 ;; appear if you transform a non-constituent to a constituent.
196 (let ((att (get-cat-entry from-char really-from-readtable
)))
197 (if (constituentp from-char really-from-readtable
)
198 (setq att
(get-secondary-attribute to-char
)))
199 (set-cat-entry to-char att to-readtable
)
200 (set-cmt-entry to-char
201 (get-raw-cmt-entry from-char really-from-readtable
)
205 (defun set-macro-character (char function
&optional
206 (non-terminatingp nil
)
207 (readtable *readtable
*))
209 "Causes CHAR to be a macro character which invokes FUNCTION when seen
210 by the reader. The NON-TERMINATINGP flag can be used to make the macro
211 character non-terminating, i.e. embeddable in a symbol name."
212 (let ((designated-readtable (or readtable
*standard-readtable
*)))
215 (get-secondary-attribute char
)
216 +char-attr-terminating-macro
+)
217 designated-readtable
)
218 (set-cmt-entry char function designated-readtable
)
219 t
)) ; (ANSI-specified return value)
221 (defun get-macro-character (char &optional
(readtable *readtable
*))
223 "Return the function associated with the specified CHAR which is a macro
224 character, or NIL if there is no such function. As a second value, return
225 T if CHAR is a macro character which is non-terminating, i.e. which can
226 be embedded in a symbol name."
227 (let* ((designated-readtable (or readtable
*standard-readtable
*))
228 ;; the first return value: a FUNCTION if CHAR is a macro
229 ;; character, or NIL otherwise
230 (fun-value (get-raw-cmt-entry char designated-readtable
)))
232 ;; NON-TERMINATING-P return value:
234 (or (constituentp char
)
235 (not (terminating-macrop char
)))
236 ;; ANSI's definition of GET-MACRO-CHARACTER says this
237 ;; value is NIL when CHAR is not a macro character.
238 ;; I.e. this value means not just "non-terminating
239 ;; character?" but "non-terminating macro character?".
242 ;;;; definitions to support internal programming conventions
244 (defmacro eofp
(char)
245 `(eq ,char
*eof-object
*))
247 (defun flush-whitespace (stream)
248 ;; This flushes whitespace chars, returning the last char it read (a
249 ;; non-white one). It always gets an error on end-of-file.
250 (let ((stream (in-synonym-of stream
)))
251 (if (ansi-stream-p stream
)
252 (prepare-for-fast-read-char stream
253 (do ((attribute-table (character-attribute-table *readtable
*))
254 (char (fast-read-char t
) (fast-read-char t
)))
255 ((/= (the fixnum
(aref attribute-table
(char-code char
)))
256 +char-attr-whitespace
+)
257 (done-with-fast-read-char)
259 ;; fundamental-stream
260 (do ((attribute-table (character-attribute-table *readtable
*))
261 (char (stream-read-char stream
) (stream-read-char stream
)))
263 (/= (the fixnum
(aref attribute-table
(char-code char
)))
264 +char-attr-whitespace
+))
266 (error 'end-of-file
:stream stream
)
269 ;;;; temporary initialization hack
271 (defun !cold-init-standard-readtable
()
272 (setq *standard-readtable
* (make-readtable))
273 ;; All characters get boring defaults in MAKE-READTABLE. Now we
274 ;; override the boring defaults on characters which need more
275 ;; interesting behavior.
276 (let ((*readtable
* *standard-readtable
*))
278 (flet ((whitespaceify (char)
279 (set-cat-entry char
+char-attr-whitespace
+)))
280 (whitespaceify (code-char tab-char-code
))
281 (whitespaceify #\linefeed
)
282 (whitespaceify #\space
)
283 (whitespaceify (code-char form-feed-char-code
))
284 (whitespaceify (code-char return-char-code
)))
286 (set-cat-entry #\\ +char-attr-escape
+)
287 (set-cmt-entry #\\ #'read-token
)
289 ;; Easy macro-character definitions are in this source file.
290 (set-macro-character #\" #'read-string
)
291 (set-macro-character #\' #'read-quote
)
292 (set-macro-character #\
( #'read-list
)
293 (set-macro-character #\
) #'read-right-paren
)
294 (set-macro-character #\
; #'read-comment)
295 ;; (The hairier macro-character definitions, for #\# and #\`, are
296 ;; defined elsewhere, in their own source files.)
299 (do ((ichar 0 (1+ ichar
))
302 (setq char
(code-char ichar
))
303 (when (constituentp char
*standard-readtable
*)
304 (set-cat-entry char
(get-secondary-attribute char
))
305 (set-cmt-entry char nil
)))))
307 ;;;; implementation of the read buffer
309 (defvar *read-buffer
*)
310 (defvar *read-buffer-length
*)
311 ;;; FIXME: Is it really helpful to have *READ-BUFFER-LENGTH* be a
312 ;;; separate variable instead of just calculating it on the fly as
313 ;;; (LENGTH *READ-BUFFER*)?
318 (declaim (type index
*read-buffer-length
* *inch-ptr
* *ouch-ptr
*))
319 (declaim (simple-string *read-buffer
*))
321 (defmacro reset-read-buffer
()
322 ;; Turn *READ-BUFFER* into an empty read buffer.
324 ;; *OUCH-PTR* always points to next char to write.
326 ;; *INCH-PTR* always points to next char to read.
327 (setq *inch-ptr
* 0)))
329 (defun !cold-init-read-buffer
()
330 (setq *read-buffer
* (make-string 512)) ; initial bufsize
331 (setq *read-buffer-length
* 512)
334 ;;; FIXME I removed "THE FIXNUM"'s from OUCH-READ-BUFFER and
335 ;;; OUCH-UNREAD-BUFFER, check to make sure that Python really is smart
336 ;;; enough to make good code without them. And while I'm at it,
337 ;;; converting them from macros to inline functions might be good,
340 (defmacro ouch-read-buffer
(char)
342 ;; When buffer overflow
343 (when (>= *ouch-ptr
* *read-buffer-length
*)
344 ;; Size should be doubled.
346 (setf (elt (the simple-string
*read-buffer
*) *ouch-ptr
*) ,char
)
347 (setq *ouch-ptr
* (1+ *ouch-ptr
*))))
349 ;;; macro to move *ouch-ptr* back one.
350 (defmacro ouch-unread-buffer
()
351 '(when (> *ouch-ptr
* *inch-ptr
*)
352 (setq *ouch-ptr
* (1- (the fixnum
*ouch-ptr
*)))))
354 (defun grow-read-buffer ()
355 (let ((rbl (length (the simple-string
*read-buffer
*))))
357 (concatenate 'simple-string
360 (setq *read-buffer-length
* (* 2 rbl
))))
362 (defun inchpeek-read-buffer ()
363 (if (>= (the fixnum
*inch-ptr
*) (the fixnum
*ouch-ptr
*))
365 (elt *read-buffer
* *inch-ptr
*)))
367 (defun inch-read-buffer ()
368 (if (>= *inch-ptr
* *ouch-ptr
*)
371 (elt *read-buffer
* *inch-ptr
*)
374 (defmacro unread-buffer
()
377 (defun read-unwind-read-buffer ()
378 ;; Keep contents, but make next (INCH..) return first character.
381 (defun read-buffer-to-string ()
382 (subseq *read-buffer
* 0 *ouch-ptr
*))
384 ;;;; READ-PRESERVING-WHITESPACE, READ-DELIMITED-LIST, and READ
386 ;;; an alist for #=, used to keep track of objects with labels assigned that
387 ;;; have been completely read. Each entry is (integer-tag gensym-tag value).
389 ;;; KLUDGE: Should this really be an alist? It seems as though users
390 ;;; could reasonably expect N log N performance for large datasets.
391 ;;; On the other hand, it's probably very very seldom a problem in practice.
392 ;;; On the third hand, it might be just as easy to use a hash table
393 ;;; as an alist, so maybe we should. -- WHN 19991202
394 (defvar *sharp-equal-alist
* ())
396 (declaim (special *standard-input
*))
398 ;;; READ-PRESERVING-WHITESPACE behaves just like READ, only it makes
399 ;;; sure to leave terminating whitespace in the stream. (This is a
400 ;;; COMMON-LISP exported symbol.)
401 (defun read-preserving-whitespace (&optional
(stream *standard-input
*)
406 "Read from STREAM and return the value read, preserving any whitespace
407 that followed the object."
409 ;; a loop for repeating when a macro returns nothing
411 (let ((char (read-char stream eof-error-p
*eof-object
*)))
412 (cond ((eofp char
) (return eof-value
))
415 (let* ((macrofun (get-coerced-cmt-entry char
*readtable
*))
416 (result (multiple-value-list
417 (funcall macrofun stream char
))))
418 ;; Repeat if macro returned nothing.
419 (if result
(return (car result
))))))))
420 (let ((*sharp-equal-alist
* nil
))
421 (read-preserving-whitespace stream eof-error-p eof-value t
))))
423 ;;; Return NIL or a list with one thing, depending.
425 ;;; for functions that want comments to return so that they can look
426 ;;; past them. We assume CHAR is not whitespace.
427 (defun read-maybe-nothing (stream char
)
428 (let ((retval (multiple-value-list
429 (funcall (get-coerced-cmt-entry char
*readtable
*)
432 (if retval
(rplacd retval nil
))))
434 (defun read (&optional
(stream *standard-input
*)
439 "Read the next Lisp value from STREAM, and return it."
440 (let ((result (read-preserving-whitespace stream
444 ;; (This function generally discards trailing whitespace. If you
445 ;; don't want to discard trailing whitespace, call
446 ;; CL:READ-PRESERVING-WHITESPACE instead.)
447 (unless (or (eql result eof-value
) recursivep
)
448 (let ((next-char (read-char stream nil nil
)))
449 (unless (or (null next-char
)
450 (whitespacep next-char
))
451 (unread-char next-char stream
))))
454 ;;; (This is a COMMON-LISP exported symbol.)
455 (defun read-delimited-list (endchar &optional
456 (input-stream *standard-input
*)
459 "Read Lisp values from INPUT-STREAM until the next character after a
460 value's representation is ENDCHAR, and return the objects as a list."
461 (declare (ignore recursive-p
))
462 (do ((char (flush-whitespace input-stream
)
463 (flush-whitespace input-stream
))
465 ((char= char endchar
) (nreverse retlist
))
466 (setq retlist
(nconc (read-maybe-nothing input-stream char
) retlist
))))
468 ;;;; basic readmacro definitions
470 ;;;; Some large, hairy subsets of readmacro definitions (backquotes
471 ;;;; and sharp macros) are not here, but in their own source files.
473 (defun read-quote (stream ignore
)
474 (declare (ignore ignore
))
475 (list 'quote
(read stream t nil t
)))
477 (defun read-comment (stream ignore
)
478 (declare (ignore ignore
))
479 (let ((stream (in-synonym-of stream
)))
480 (if (ansi-stream-p stream
)
481 (prepare-for-fast-read-char stream
482 (do ((char (fast-read-char nil nil
)
483 (fast-read-char nil nil
)))
484 ((or (not char
) (char= char
#\newline
))
485 (done-with-fast-read-char))))
486 ;; FUNDAMENTAL-STREAM
487 (do ((char (stream-read-char stream
) (stream-read-char stream
)))
488 ((or (eq char
:eof
) (char= char
#\newline
))))))
489 ;; Don't return anything.
492 (defun read-list (stream ignore
)
493 (declare (ignore ignore
))
494 (let* ((thelist (list nil
))
496 (do ((firstchar (flush-whitespace stream
) (flush-whitespace stream
)))
497 ((char= firstchar
#\
) ) (cdr thelist
))
498 (when (char= firstchar
#\.
)
499 (let ((nextchar (read-char stream t
)))
500 (cond ((token-delimiterp nextchar
)
501 (cond ((eq listtail thelist
)
504 "Nothing appears before . in list."))
505 ((whitespacep nextchar
)
506 (setq nextchar
(flush-whitespace stream
))))
508 ;; Return list containing last thing.
509 (car (read-after-dot stream nextchar
)))
510 (return (cdr thelist
)))
511 ;; Put back NEXTCHAR so that we can read it normally.
512 (t (unread-char nextchar stream
)))))
513 ;; Next thing is not an isolated dot.
514 (let ((listobj (read-maybe-nothing stream firstchar
)))
515 ;; allows the possibility that a comment was read
517 (rplacd listtail listobj
)
518 (setq listtail listobj
))))))
520 (defun read-after-dot (stream firstchar
)
521 ;; FIRSTCHAR is non-whitespace!
523 (do ((char firstchar
(flush-whitespace stream
)))
525 (%reader-error stream
"Nothing appears after . in list."))
526 ;; See whether there's something there.
527 (setq lastobj
(read-maybe-nothing stream char
))
528 (when lastobj
(return t
)))
529 ;; At least one thing appears after the dot.
530 ;; Check for more than one thing following dot.
531 (do ((lastchar (flush-whitespace stream
)
532 (flush-whitespace stream
)))
533 ((char= lastchar
#\
) ) lastobj
) ;success!
534 ;; Try reading virtual whitespace.
535 (if (read-maybe-nothing stream lastchar
)
536 (%reader-error stream
"More than one object follows . in list.")))))
538 (defun read-string (stream closech
)
539 ;; This accumulates chars until it sees same char that invoked it.
540 ;; For a very long string, this could end up bloating the read buffer.
542 (let ((stream (in-synonym-of stream
)))
543 (if (ansi-stream-p stream
)
544 (prepare-for-fast-read-char stream
545 (do ((char (fast-read-char t
) (fast-read-char t
)))
546 ((char= char closech
)
547 (done-with-fast-read-char))
548 (if (escapep char
) (setq char
(fast-read-char t
)))
549 (ouch-read-buffer char
)))
550 ;; FUNDAMENTAL-STREAM
551 (do ((char (stream-read-char stream
) (stream-read-char stream
)))
552 ((or (eq char
:eof
) (char= char closech
))
554 (error 'end-of-file
:stream stream
)))
556 (setq char
(stream-read-char stream
))
558 (error 'end-of-file
:stream stream
)))
559 (ouch-read-buffer char
))))
560 (read-buffer-to-string))
562 (defun read-right-paren (stream ignore
)
563 (declare (ignore ignore
))
564 (%reader-error stream
"unmatched close parenthesis"))
566 ;;; Read from the stream up to the next delimiter. Leave the resulting
567 ;;; token in *READ-BUFFER*, and return two values:
568 ;;; -- a list of the escaped character positions, and
569 ;;; -- The position of the first package delimiter (or NIL).
570 (defun internal-read-extended-token (stream firstchar escape-firstchar
)
573 (when escape-firstchar
574 (push *ouch-ptr
* escapes
)
575 (ouch-read-buffer firstchar
)
576 (setq firstchar
(read-char stream nil
*eof-object
*)))
577 (do ((char firstchar
(read-char stream nil
*eof-object
*))
579 ((cond ((eofp char
) t
)
580 ((token-delimiterp char
)
581 (unread-char char stream
)
584 (values escapes colon
))
585 (cond ((escapep char
)
586 ;; It can't be a number, even if it's 1\23.
587 ;; Read next char here, so it won't be casified.
588 (push *ouch-ptr
* escapes
)
589 (let ((nextchar (read-char stream nil
*eof-object
*)))
591 (reader-eof-error stream
"after escape character")
592 (ouch-read-buffer nextchar
))))
593 ((multiple-escape-p char
)
594 ;; Read to next multiple-escape, escaping single chars
597 (let ((ch (read-char stream nil
*eof-object
*)))
600 (reader-eof-error stream
"inside extended token"))
601 ((multiple-escape-p ch
) (return))
603 (let ((nextchar (read-char stream nil
*eof-object
*)))
604 (cond ((eofp nextchar
)
605 (reader-eof-error stream
"after escape character"))
607 (push *ouch-ptr
* escapes
)
608 (ouch-read-buffer nextchar
)))))
610 (push *ouch-ptr
* escapes
)
611 (ouch-read-buffer ch
))))))
613 (when (and (constituentp char
)
614 (eql (get-secondary-attribute char
)
615 +char-attr-package-delimiter
+)
617 (setq colon
*ouch-ptr
*))
618 (ouch-read-buffer char
))))))
620 ;;;; character classes
622 ;;; Return the character class for CHAR.
623 (defmacro char-class
(char attable
)
624 `(let ((att (aref ,attable
(char-code ,char
))))
625 (declare (fixnum att
))
626 (if (<= att
+char-attr-terminating-macro
+)
627 +char-attr-delimiter
+
630 ;;; Return the character class for CHAR, which might be part of a
632 (defmacro char-class2
(char attable
)
633 `(let ((att (aref ,attable
(char-code ,char
))))
634 (declare (fixnum att
))
635 (if (<= att
+char-attr-terminating-macro
+)
636 +char-attr-delimiter
+
637 (if (digit-char-p ,char
*read-base
*)
638 +char-attr-constituent-digit
+
639 (if (= att
+char-attr-constituent-digit
+)
640 +char-attr-constituent
+
643 ;;; Return the character class for a char which might be part of a
644 ;;; rational or floating number. (Assume that it is a digit if it
646 (defmacro char-class3
(char attable
)
647 `(let ((att (aref ,attable
(char-code ,char
))))
648 (declare (fixnum att
))
649 (if possibly-rational
650 (setq possibly-rational
651 (or (digit-char-p ,char
*read-base
*)
652 (= att
+char-attr-constituent-slash
+))))
655 (or (digit-char-p ,char
10)
656 (= att
+char-attr-constituent-dot
+))))
657 (if (<= att
+char-attr-terminating-macro
+)
658 +char-attr-delimiter
+
659 (if (digit-char-p ,char
(max *read-base
* 10))
660 (if (digit-char-p ,char
*read-base
*)
661 +char-attr-constituent-digit
+
662 +char-attr-constituent
+)
667 (defvar *read-suppress
* nil
669 "Suppress most interpreting in the reader when T.")
671 (defvar *read-base
* 10
673 "the radix that Lisp reads numbers in")
674 (declaim (type (integer 2 36) *read-base
*))
676 ;;; Modify the read buffer according to READTABLE-CASE, ignoring
677 ;;; ESCAPES. ESCAPES is a list of the escaped indices, in reverse
679 (defun casify-read-buffer (escapes)
680 (let ((case (readtable-case *readtable
*)))
682 ((and (null escapes
) (eq case
:upcase
))
683 (dotimes (i *ouch-ptr
*)
684 (setf (schar *read-buffer
* i
)
685 (char-upcase (schar *read-buffer
* i
)))))
686 ((eq case
:preserve
))
688 (macrolet ((skip-esc (&body body
)
689 `(do ((i (1- *ouch-ptr
*) (1- i
))
693 (when (or (null escapes
)
694 (let ((esc (first escapes
)))
695 (declare (fixnum esc
))
701 (let ((ch (schar *read-buffer
* i
)))
704 (skip-esc (setf (schar *read-buffer
* i
) (char-downcase ch
))))
706 (skip-esc (setf (schar *read-buffer
* i
) (char-upcase ch
)))))
709 (:downcase
(lower-em))
714 (when (both-case-p ch
)
715 (if (upper-case-p ch
)
717 (setq all-upper nil
))))
718 (cond (all-lower (raise-em))
719 (all-upper (lower-em))))))))))))
721 (defun read-token (stream firstchar
)
723 "This function is just an fsm that recognizes numbers and symbols."
724 ;; Check explicitly whether FIRSTCHAR has an entry for
725 ;; NON-TERMINATING in CHARACTER-ATTRIBUTE-TABLE and
726 ;; READ-DOT-NUMBER-SYMBOL in CMT. Report an error if these are
727 ;; violated. (If we called this, we want something that is a
728 ;; legitimate token!) Read in the longest possible string satisfying
729 ;; the Backus-Naur form for "unqualified-token". Leave the result in
730 ;; the *READ-BUFFER*. Return next char after token (last char read).
731 (when *read-suppress
*
732 (internal-read-extended-token stream firstchar nil
)
733 (return-from read-token nil
))
734 (let ((attribute-table (character-attribute-table *readtable
*))
735 (package-designator nil
)
737 (possibly-rational t
)
741 (prog ((char firstchar
))
742 (case (char-class3 char attribute-table
)
743 (#.
+char-attr-constituent-sign
+ (go SIGN
))
744 (#.
+char-attr-constituent-digit
+ (go LEFTDIGIT
))
745 (#.
+char-attr-constituent-dot
+ (go FRONTDOT
))
746 (#.
+char-attr-escape
+ (go ESCAPE
))
747 (#.
+char-attr-package-delimiter
+ (go COLON
))
748 (#.
+char-attr-multiple-escape
+ (go MULT-ESCAPE
))
749 ;; can't have eof, whitespace, or terminating macro as first char!
752 (ouch-read-buffer char
)
753 (setq char
(read-char stream nil nil
))
754 (unless char
(go RETURN-SYMBOL
))
755 (setq possibly-rational t
757 (case (char-class3 char attribute-table
)
758 (#.
+char-attr-constituent-digit
+ (go LEFTDIGIT
))
759 (#.
+char-attr-constituent-dot
+ (go SIGNDOT
))
760 (#.
+char-attr-escape
+ (go ESCAPE
))
761 (#.
+char-attr-package-delimiter
+ (go COLON
))
762 (#.
+char-attr-multiple-escape
+ (go MULT-ESCAPE
))
763 (#.
+char-attr-delimiter
+ (unread-char char stream
) (go RETURN-SYMBOL
))
765 LEFTDIGIT
; saw "[sign] {digit}+"
766 (ouch-read-buffer char
)
767 (setq char
(read-char stream nil nil
))
768 (unless char
(return (make-integer)))
769 (case (char-class3 char attribute-table
)
770 (#.
+char-attr-constituent-digit
+ (go LEFTDIGIT
))
771 (#.
+char-attr-constituent-dot
+ (if possibly-float
774 (#.
+char-attr-constituent-expt
+ (go EXPONENT
))
775 (#.
+char-attr-constituent-slash
+ (if possibly-rational
778 (#.
+char-attr-delimiter
+ (unread-char char stream
)
779 (return (make-integer)))
780 (#.
+char-attr-escape
+ (go ESCAPE
))
781 (#.
+char-attr-multiple-escape
+ (go MULT-ESCAPE
))
782 (#.
+char-attr-package-delimiter
+ (go COLON
))
784 MIDDLEDOT
; saw "[sign] {digit}+ dot"
785 (ouch-read-buffer char
)
786 (setq char
(read-char stream nil nil
))
787 (unless char
(return (let ((*read-base
* 10))
789 (case (char-class char attribute-table
)
790 (#.
+char-attr-constituent-digit
+ (go RIGHTDIGIT
))
791 (#.
+char-attr-constituent-expt
+ (go EXPONENT
))
792 (#.
+char-attr-delimiter
+
793 (unread-char char stream
)
794 (return (let ((*read-base
* 10))
796 (#.
+char-attr-escape
+ (go ESCAPE
))
797 (#.
+char-attr-multiple-escape
+ (go MULT-ESCAPE
))
798 (#.
+char-attr-package-delimiter
+ (go COLON
))
800 RIGHTDIGIT
; saw "[sign] {digit}* dot {digit}+"
801 (ouch-read-buffer char
)
802 (setq char
(read-char stream nil nil
))
803 (unless char
(return (make-float stream
)))
804 (case (char-class char attribute-table
)
805 (#.
+char-attr-constituent-digit
+ (go RIGHTDIGIT
))
806 (#.
+char-attr-constituent-expt
+ (go EXPONENT
))
807 (#.
+char-attr-delimiter
+
808 (unread-char char stream
)
809 (return (make-float stream
)))
810 (#.
+char-attr-escape
+ (go ESCAPE
))
811 (#.
+char-attr-multiple-escape
+ (go MULT-ESCAPE
))
812 (#.
+char-attr-package-delimiter
+ (go COLON
))
814 SIGNDOT
; saw "[sign] dot"
815 (ouch-read-buffer char
)
816 (setq char
(read-char stream nil nil
))
817 (unless char
(go RETURN-SYMBOL
))
818 (case (char-class char attribute-table
)
819 (#.
+char-attr-constituent-digit
+ (go RIGHTDIGIT
))
820 (#.
+char-attr-delimiter
+ (unread-char char stream
) (go RETURN-SYMBOL
))
821 (#.
+char-attr-escape
+ (go ESCAPE
))
822 (#.
+char-attr-multiple-escape
+ (go MULT-ESCAPE
))
825 (ouch-read-buffer char
)
826 (setq char
(read-char stream nil nil
))
827 (unless char
(%reader-error stream
"dot context error"))
828 (case (char-class char attribute-table
)
829 (#.
+char-attr-constituent-digit
+ (go RIGHTDIGIT
))
830 (#.
+char-attr-constituent-dot
+ (go DOTS
))
831 (#.
+char-attr-delimiter
+ (%reader-error stream
"dot context error"))
832 (#.
+char-attr-escape
+ (go ESCAPE
))
833 (#.
+char-attr-multiple-escape
+ (go MULT-ESCAPE
))
834 (#.
+char-attr-package-delimiter
+ (go COLON
))
837 (ouch-read-buffer char
)
838 (setq char
(read-char stream nil nil
))
839 (unless char
(go RETURN-SYMBOL
))
840 (case (char-class char attribute-table
)
841 (#.
+char-attr-constituent-sign
+ (go EXPTSIGN
))
842 (#.
+char-attr-constituent-digit
+ (go EXPTDIGIT
))
843 (#.
+char-attr-delimiter
+ (unread-char char stream
) (go RETURN-SYMBOL
))
844 (#.
+char-attr-escape
+ (go ESCAPE
))
845 (#.
+char-attr-multiple-escape
+ (go MULT-ESCAPE
))
846 (#.
+char-attr-package-delimiter
+ (go COLON
))
848 EXPTSIGN
; got to EXPONENT, and saw a sign character
849 (ouch-read-buffer char
)
850 (setq char
(read-char stream nil nil
))
851 (unless char
(go RETURN-SYMBOL
))
852 (case (char-class char attribute-table
)
853 (#.
+char-attr-constituent-digit
+ (go EXPTDIGIT
))
854 (#.
+char-attr-delimiter
+ (unread-char char stream
) (go RETURN-SYMBOL
))
855 (#.
+char-attr-escape
+ (go ESCAPE
))
856 (#.
+char-attr-multiple-escape
+ (go MULT-ESCAPE
))
857 (#.
+char-attr-package-delimiter
+ (go COLON
))
859 EXPTDIGIT
; got to EXPONENT, saw "[sign] {digit}+"
860 (ouch-read-buffer char
)
861 (setq char
(read-char stream nil nil
))
862 (unless char
(return (make-float stream
)))
863 (case (char-class char attribute-table
)
864 (#.
+char-attr-constituent-digit
+ (go EXPTDIGIT
))
865 (#.
+char-attr-delimiter
+
866 (unread-char char stream
)
867 (return (make-float stream
)))
868 (#.
+char-attr-escape
+ (go ESCAPE
))
869 (#.
+char-attr-multiple-escape
+ (go MULT-ESCAPE
))
870 (#.
+char-attr-package-delimiter
+ (go COLON
))
872 RATIO
; saw "[sign] {digit}+ slash"
873 (ouch-read-buffer char
)
874 (setq char
(read-char stream nil nil
))
875 (unless char
(go RETURN-SYMBOL
))
876 (case (char-class2 char attribute-table
)
877 (#.
+char-attr-constituent-digit
+ (go RATIODIGIT
))
878 (#.
+char-attr-delimiter
+ (unread-char char stream
) (go RETURN-SYMBOL
))
879 (#.
+char-attr-escape
+ (go ESCAPE
))
880 (#.
+char-attr-multiple-escape
+ (go MULT-ESCAPE
))
881 (#.
+char-attr-package-delimiter
+ (go COLON
))
883 RATIODIGIT
; saw "[sign] {digit}+ slash {digit}+"
884 (ouch-read-buffer char
)
885 (setq char
(read-char stream nil nil
))
886 (unless char
(return (make-ratio stream
)))
887 (case (char-class2 char attribute-table
)
888 (#.
+char-attr-constituent-digit
+ (go RATIODIGIT
))
889 (#.
+char-attr-delimiter
+
890 (unread-char char stream
)
891 (return (make-ratio stream
)))
892 (#.
+char-attr-escape
+ (go ESCAPE
))
893 (#.
+char-attr-multiple-escape
+ (go MULT-ESCAPE
))
894 (#.
+char-attr-package-delimiter
+ (go COLON
))
896 DOTS
; saw "dot {dot}+"
897 (ouch-read-buffer char
)
898 (setq char
(read-char stream nil nil
))
899 (unless char
(%reader-error stream
"too many dots"))
900 (case (char-class char attribute-table
)
901 (#.
+char-attr-constituent-dot
+ (go DOTS
))
902 (#.
+char-attr-delimiter
+
903 (unread-char char stream
)
904 (%reader-error stream
"too many dots"))
905 (#.
+char-attr-escape
+ (go ESCAPE
))
906 (#.
+char-attr-multiple-escape
+ (go MULT-ESCAPE
))
907 (#.
+char-attr-package-delimiter
+ (go COLON
))
909 SYMBOL
; not a dot, dots, or number
910 (let ((stream (in-synonym-of stream
)))
911 (if (ansi-stream-p stream
)
912 (prepare-for-fast-read-char stream
915 (ouch-read-buffer char
)
916 (setq char
(fast-read-char nil nil
))
917 (unless char
(go RETURN-SYMBOL
))
918 (case (char-class char attribute-table
)
919 (#.
+char-attr-escape
+ (done-with-fast-read-char)
921 (#.
+char-attr-delimiter
+ (done-with-fast-read-char)
922 (unread-char char stream
)
924 (#.
+char-attr-multiple-escape
+ (done-with-fast-read-char)
926 (#.
+char-attr-package-delimiter
+ (done-with-fast-read-char)
928 (t (go SYMBOL-LOOP
)))))
929 ;; fundamental-stream
932 (ouch-read-buffer char
)
933 (setq char
(stream-read-char stream
))
934 (when (eq char
:eof
) (go RETURN-SYMBOL
))
935 (case (char-class char attribute-table
)
936 (#.
+char-attr-escape
+ (go ESCAPE
))
937 (#.
+char-attr-delimiter
+ (stream-unread-char stream char
)
939 (#.
+char-attr-multiple-escape
+ (go MULT-ESCAPE
))
940 (#.
+char-attr-package-delimiter
+ (go COLON
))
941 (t (go SYMBOL-LOOP
))))))
942 ESCAPE
; saw an escape
943 ;; Don't put the escape in the read buffer.
944 ;; READ-NEXT CHAR, put in buffer (no case conversion).
945 (let ((nextchar (read-char stream nil nil
)))
947 (reader-eof-error stream
"after escape character"))
948 (push *ouch-ptr
* escapes
)
949 (ouch-read-buffer nextchar
))
950 (setq char
(read-char stream nil nil
))
951 (unless char
(go RETURN-SYMBOL
))
952 (case (char-class char attribute-table
)
953 (#.
+char-attr-delimiter
+ (unread-char char stream
) (go RETURN-SYMBOL
))
954 (#.
+char-attr-escape
+ (go ESCAPE
))
955 (#.
+char-attr-multiple-escape
+ (go MULT-ESCAPE
))
956 (#.
+char-attr-package-delimiter
+ (go COLON
))
959 (do ((char (read-char stream t
) (read-char stream t
)))
960 ((multiple-escape-p char
))
961 (if (escapep char
) (setq char
(read-char stream t
)))
962 (push *ouch-ptr
* escapes
)
963 (ouch-read-buffer char
))
964 (setq char
(read-char stream nil nil
))
965 (unless char
(go RETURN-SYMBOL
))
966 (case (char-class char attribute-table
)
967 (#.
+char-attr-delimiter
+ (unread-char char stream
) (go RETURN-SYMBOL
))
968 (#.
+char-attr-escape
+ (go ESCAPE
))
969 (#.
+char-attr-multiple-escape
+ (go MULT-ESCAPE
))
970 (#.
+char-attr-package-delimiter
+ (go COLON
))
973 (casify-read-buffer escapes
)
974 (unless (zerop colons
)
975 (%reader-error stream
"too many colons in ~S"
976 (read-buffer-to-string)))
978 (setq package-designator
979 (if (plusp *ouch-ptr
*)
980 ;; FIXME: It seems inefficient to cons up a package
981 ;; designator string every time we read a symbol with an
982 ;; explicit package prefix. Perhaps we could implement
983 ;; a FIND-PACKAGE* function analogous to INTERN*
985 (read-buffer-to-string)
989 (setq char
(read-char stream nil nil
))
990 (unless char
(reader-eof-error stream
"after reading a colon"))
991 (case (char-class char attribute-table
)
992 (#.
+char-attr-delimiter
+
993 (unread-char char stream
)
994 (%reader-error stream
995 "illegal terminating character after a colon: ~S"
997 (#.
+char-attr-escape
+ (go ESCAPE
))
998 (#.
+char-attr-multiple-escape
+ (go MULT-ESCAPE
))
999 (#.
+char-attr-package-delimiter
+ (go INTERN
))
1003 (setq char
(read-char stream nil nil
))
1005 (reader-eof-error stream
"after reading a colon"))
1006 (case (char-class char attribute-table
)
1007 (#.
+char-attr-delimiter
+
1008 (unread-char char stream
)
1009 (%reader-error stream
1010 "illegal terminating character after a colon: ~S"
1012 (#.
+char-attr-escape
+ (go ESCAPE
))
1013 (#.
+char-attr-multiple-escape
+ (go MULT-ESCAPE
))
1014 (#.
+char-attr-package-delimiter
+
1015 (%reader-error stream
1016 "too many colons after ~S name"
1017 package-designator
))
1020 (casify-read-buffer escapes
)
1021 (let ((found (if package-designator
1022 (find-package package-designator
)
1025 (error 'reader-package-error
:stream stream
1026 :format-arguments
(list package-designator
)
1027 :format-control
"package ~S not found"))
1029 (if (or (zerop colons
) (= colons
2) (eq found
*keyword-package
*))
1030 (return (intern* *read-buffer
* *ouch-ptr
* found
))
1031 (multiple-value-bind (symbol test
)
1032 (find-symbol* *read-buffer
* *ouch-ptr
* found
)
1033 (when (eq test
:external
) (return symbol
))
1034 (let ((name (read-buffer-to-string)))
1035 (with-simple-restart (continue "Use symbol anyway.")
1036 (error 'reader-package-error
:stream stream
1037 :format-arguments
(list name
(package-name found
))
1040 "The symbol ~S is not external in the ~A package."
1041 "Symbol ~S not found in the ~A package.")))
1042 (return (intern name found
)))))))))
1044 ;;; for semi-external use:
1046 ;;; For semi-external use: Return 3 values: the string for the token,
1047 ;;; a flag for whether there was an escape char, and the position of
1048 ;;; any package delimiter.
1049 (defun read-extended-token (stream &optional
(*readtable
* *readtable
*))
1050 (let ((first-char (read-char stream nil nil t
)))
1052 (multiple-value-bind (escapes colon
)
1053 (internal-read-extended-token stream first-char nil
)
1054 (casify-read-buffer escapes
)
1055 (values (read-buffer-to-string) (not (null escapes
)) colon
)))
1057 (values "" nil nil
)))))
1059 ;;; for semi-external use:
1061 ;;; Read an extended token with the first character escaped. Return
1062 ;;; the string for the token.
1063 (defun read-extended-token-escaped (stream &optional
(*readtable
* *readtable
*))
1064 (let ((first-char (read-char stream nil nil
)))
1066 (let ((escapes (internal-read-extended-token stream first-char t
)))
1067 (casify-read-buffer escapes
)
1068 (read-buffer-to-string)))
1070 (reader-eof-error stream
"after escape")))))
1072 ;;;; number-reading functions
1074 (defmacro digit
* nil
1075 `(do ((ch char
(inch-read-buffer)))
1076 ((or (eofp ch
) (not (digit-char-p ch
))) (setq char ch
))
1077 ;; Report if at least one digit is seen.
1078 (setq one-digit t
)))
1080 (defmacro exponent-letterp
(letter)
1081 `(memq ,letter
'(#\E
#\S
#\F
#\L
#\D
#\e
#\s
#\f #\l
#\d
)))
1083 ;;; FIXME: It would be cleaner to have these generated automatically
1084 ;;; by compile-time code instead of having them hand-created like
1085 ;;; this. The !COLD-INIT-INTEGER-READER code below should be resurrected
1087 (defvar *integer-reader-safe-digits
*
1089 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)
1091 "the mapping of base to 'safe' number of digits to read for a fixnum")
1092 (defvar *integer-reader-base-power
*
1094 67108864 129140163 67108864 48828125 60466176 40353607
1095 16777216 43046721 100000000 19487171 35831808 62748517 105413504 11390625
1096 16777216 24137569 34012224 47045881 64000000 85766121 113379904 6436343
1097 7962624 9765625 11881376 14348907 17210368 20511149 24300000 28629151
1098 33554432 39135393 45435424 52521875 60466176)
1100 "the largest fixnum power of the base for MAKE-INTEGER")
1101 (declaim (simple-vector *integer-reader-safe-digits
*
1102 *integer-reader-base-power
*))
1104 (defun !cold-init-integer-reader
()
1105 (do ((base 2 (1+ base
)))
1108 (do ((fix (truncate most-positive-fixnum base
)
1109 (truncate fix base
))
1110 (digits 0 (1+ digits
)))
1111 ((zerop fix
) digits
))))
1112 (setf (aref *integer-reader-safe-digits
* base
)
1114 (aref *integer-reader-base-power
* base
)
1115 (expt base digits
)))))
1118 (defun make-integer ()
1120 "Minimizes bignum-fixnum multiplies by reading a 'safe' number of digits,
1121 then multiplying by a power of the base and adding."
1122 (let* ((base *read-base
*)
1123 (digits-per (aref *integer-reader-safe-digits
* base
))
1124 (base-power (aref *integer-reader-base-power
* base
))
1127 (declare (type index digits-per base-power
))
1128 (read-unwind-read-buffer)
1129 (let ((char (inch-read-buffer)))
1130 (cond ((char= char
#\-
)
1133 (t (unread-buffer))))
1136 (declare (type index num
))
1137 (dotimes (digit digits-per
)
1138 (let* ((ch (inch-read-buffer)))
1139 (cond ((or (eofp ch
) (char= ch
#\.
))
1140 (return-from make-integer
1142 (if (zerop number
) num
1144 (expt base digit
))))))
1145 (if negativep
(- res
) res
))))
1146 (t (setq num
(+ (digit-char-p ch base
)
1147 (the index
(* num base
))))))))
1148 (setq number
(+ num
(* number base-power
)))))))
1150 (defun make-float (stream)
1151 ;; Assume that the contents of *read-buffer* are a legal float, with nothing
1153 (read-unwind-read-buffer)
1154 (let ((negative-fraction nil
)
1157 (negative-exponent nil
)
1160 (char (inch-read-buffer)))
1161 (if (cond ((char= char
#\
+) t
)
1162 ((char= char
#\-
) (setq negative-fraction t
)))
1164 (setq char
(inch-read-buffer)))
1165 ;; Read digits before the dot.
1166 (do* ((ch char
(inch-read-buffer))
1167 (dig (digit-char-p ch
) (digit-char-p ch
)))
1168 ((not dig
) (setq char ch
))
1169 (setq number
(+ (* number
10) dig
)))
1170 ;; Deal with the dot, if it's there.
1171 (when (char= char
#\.
)
1172 (setq char
(inch-read-buffer))
1173 ;; Read digits after the dot.
1174 (do* ((ch char
(inch-read-buffer))
1175 (dig (and (not (eofp ch
)) (digit-char-p ch
))
1176 (and (not (eofp ch
)) (digit-char-p ch
))))
1177 ((not dig
) (setq char ch
))
1178 (setq divisor
(* divisor
10))
1179 (setq number
(+ (* number
10) dig
))))
1180 ;; Is there an exponent letter?
1182 ;; If not, we've read the whole number.
1183 (let ((num (make-float-aux number divisor
1184 *read-default-float-format
*
1186 (return-from make-float
(if negative-fraction
(- num
) num
))))
1187 ((exponent-letterp char
)
1188 (setq float-char char
)
1190 (setq char
(inch-read-buffer))
1191 ;; Check leading sign.
1192 (if (cond ((char= char
#\
+) t
)
1193 ((char= char
#\-
) (setq negative-exponent t
)))
1195 (setq char
(inch-read-buffer)))
1196 ;; Read digits for exponent.
1197 (do* ((ch char
(inch-read-buffer))
1198 (dig (and (not (eofp ch
)) (digit-char-p ch
))
1199 (and (not (eofp ch
)) (digit-char-p ch
))))
1201 (setq exponent
(if negative-exponent
(- exponent
) exponent
)))
1202 (setq exponent
(+ (* exponent
10) dig
)))
1203 ;; Generate and return the float, depending on FLOAT-CHAR:
1204 (let* ((float-format (case (char-upcase float-char
)
1205 (#\E
*read-default-float-format
*)
1211 ;; Raymond Toy writes: We need to watch out if the
1212 ;; exponent is too small or too large. We add enough to
1213 ;; EXPONENT to make it within range and scale NUMBER
1214 ;; appropriately. This should avoid any unnecessary
1215 ;; underflow or overflow problems.
1216 (multiple-value-bind (min-expo max-expo
)
1217 ;; FIXME: These forms are broken w.r.t.
1218 ;; cross-compilation portability, as the
1219 ;; cross-compiler will call the host's LOG function
1220 ;; while attempting to constant-fold. Maybe some sort
1221 ;; of load-time-form magic could be used instead?
1225 (log sb
!xc
:least-positive-normalized-short-float
10s0
)
1226 (log sb
!xc
:most-positive-short-float
10s0
)))
1229 (log sb
!xc
:least-positive-normalized-single-float
10f0
)
1230 (log sb
!xc
:most-positive-single-float
10f0
)))
1233 (log sb
!xc
:least-positive-normalized-double-float
10d0
)
1234 (log sb
!xc
:most-positive-double-float
10d0
)))
1237 (log sb
!xc
:least-positive-normalized-long-float
10L0)
1238 (log sb
!xc
:most-positive-long-float
10L0))))
1239 (let ((correction (cond ((<= exponent min-expo
)
1240 (ceiling (- min-expo exponent
)))
1241 ((>= exponent max-expo
)
1242 (floor (- max-expo exponent
)))
1245 (incf exponent correction
)
1246 (setf number
(/ number
(expt 10 correction
)))
1247 (setq num
(make-float-aux number divisor float-format stream
))
1248 (setq num
(* num
(expt 10 exponent
)))
1249 (return-from make-float
(if negative-fraction
1252 ;; should never happen
1253 (t (bug "bad fallthrough in floating point reader")))))
1255 (defun make-float-aux (number divisor float-format stream
)
1257 (coerce (/ number divisor
) float-format
)
1259 (error 'reader-impossible-number-error
1260 :error c
:stream stream
1261 :format-control
"failed to build float"))))
1263 (defun make-ratio (stream)
1264 ;; Assume *READ-BUFFER* contains a legal ratio. Build the number from
1267 ;; Look for optional "+" or "-".
1268 (let ((numerator 0) (denominator 0) (char ()) (negative-number nil
))
1269 (read-unwind-read-buffer)
1270 (setq char
(inch-read-buffer))
1271 (cond ((char= char
#\
+)
1272 (setq char
(inch-read-buffer)))
1274 (setq char
(inch-read-buffer))
1275 (setq negative-number t
)))
1277 (do* ((ch char
(inch-read-buffer))
1278 (dig (digit-char-p ch
*read-base
*)
1279 (digit-char-p ch
*read-base
*)))
1281 (setq numerator
(+ (* numerator
*read-base
*) dig
)))
1283 (do* ((ch (inch-read-buffer) (inch-read-buffer))
1285 ((or (eofp ch
) (not (setq dig
(digit-char-p ch
*read-base
*)))))
1286 (setq denominator
(+ (* denominator
*read-base
*) dig
)))
1287 (let ((num (handler-case
1288 (/ numerator denominator
)
1289 (arithmetic-error (c)
1290 (error 'reader-impossible-number-error
1291 :error c
:stream stream
1292 :format-control
"failed to build ratio")))))
1293 (if negative-number
(- num
) num
))))
1295 ;;;; cruft for dispatch macros
1297 (defun make-char-dispatch-table ()
1298 (make-array char-code-limit
:initial-element
#'dispatch-char-error
))
1300 (defun dispatch-char-error (stream sub-char ignore
)
1301 (declare (ignore ignore
))
1304 (%reader-error stream
"no dispatch function defined for ~S" sub-char
)))
1306 (defun make-dispatch-macro-character (char &optional
1307 (non-terminating-p nil
)
1310 "Cause CHAR to become a dispatching macro character in readtable (which
1311 defaults to the current readtable). If NON-TERMINATING-P, the char will
1312 be non-terminating."
1313 (set-macro-character char
#'read-dispatch-char non-terminating-p rt
)
1314 (let* ((dalist (dispatch-tables rt
))
1315 (dtable (cdr (find char dalist
:test
#'char
= :key
#'car
))))
1317 (error "The dispatch character ~S already exists." char
))
1319 (setf (dispatch-tables rt
)
1320 (push (cons char
(make-char-dispatch-table)) dalist
)))))
1323 (defun set-dispatch-macro-character (disp-char sub-char function
1324 &optional
(rt *readtable
*))
1326 "Cause FUNCTION to be called whenever the reader reads DISP-CHAR
1327 followed by SUB-CHAR."
1328 ;; Get the dispatch char for macro (error if not there), diddle
1329 ;; entry for sub-char.
1330 (when (digit-char-p sub-char
)
1331 (error "SUB-CHAR must not be a decimal digit: ~S" sub-char
))
1332 (let* ((sub-char (char-upcase sub-char
))
1333 (rt (or rt
*standard-readtable
*))
1334 (dpair (find disp-char
(dispatch-tables rt
)
1335 :test
#'char
= :key
#'car
)))
1337 (setf (elt (the simple-vector
(cdr dpair
))
1338 (char-code sub-char
))
1339 (coerce function
'function
))
1340 (error "~S is not a dispatch char." disp-char
))))
1342 (defun get-dispatch-macro-character (disp-char sub-char
1343 &optional
(rt *readtable
*))
1345 "Return the macro character function for SUB-CHAR under DISP-CHAR
1346 or NIL if there is no associated function."
1347 (let* ((sub-char (char-upcase sub-char
))
1348 (rt (or rt
*standard-readtable
*))
1349 (dpair (find disp-char
(dispatch-tables rt
)
1350 :test
#'char
= :key
#'car
)))
1352 (let ((dispatch-fun (elt (the simple-vector
(cdr dpair
))
1353 (char-code sub-char
))))
1354 ;; Digits are also initialized in a dispatch table to
1355 ;; #'dispatch-char-error; READ-DISPATCH-CHAR handles them
1356 ;; separately. - CSR, 2002-04-12
1357 (if (eq dispatch-fun
#'dispatch-char-error
)
1360 (error "~S is not a dispatch char." disp-char
))))
1362 (defun read-dispatch-char (stream char
)
1363 ;; Read some digits.
1367 (do* ((ch (read-char stream nil
*eof-object
*)
1368 (read-char stream nil
*eof-object
*))
1371 (not (setq dig
(digit-char-p ch
))))
1372 ;; Take care of the extra char.
1374 (reader-eof-error stream
"inside dispatch character")
1375 (setq sub-char
(char-upcase ch
))))
1377 (setq numarg
(+ (* numarg
10) dig
)))
1378 ;; Look up the function and call it.
1379 (let ((dpair (find char
(dispatch-tables *readtable
*)
1380 :test
#'char
= :key
#'car
)))
1382 (funcall (the function
1383 (elt (the simple-vector
(cdr dpair
))
1384 (char-code sub-char
)))
1385 stream sub-char
(if numargp numarg nil
))
1386 (%reader-error stream
"no dispatch table for dispatch char")))))
1388 ;;;; READ-FROM-STRING
1390 ;;; FIXME: Is it really worth keeping this pool?
1391 (defvar *read-from-string-spares
* ()
1393 "A resource of string streams for Read-From-String.")
1395 (defun read-from-string (string &optional eof-error-p eof-value
1397 preserve-whitespace
)
1399 "The characters of string are successively given to the lisp reader
1400 and the lisp object built by the reader is returned. Macro chars
1402 (declare (string string
))
1404 (with-array-data ((string string
)
1406 (end (%check-vector-sequence-bounds string start end
)))
1407 (unless *read-from-string-spares
*
1408 (push (internal-make-string-input-stream "" 0 0)
1409 *read-from-string-spares
*))
1410 (let ((stream (pop *read-from-string-spares
*)))
1411 (setf (string-input-stream-string stream
) string
)
1412 (setf (string-input-stream-current stream
) start
)
1413 (setf (string-input-stream-end stream
) end
)
1415 (values (if preserve-whitespace
1416 (read-preserving-whitespace stream eof-error-p eof-value
)
1417 (read stream eof-error-p eof-value
))
1418 (string-input-stream-current stream
))
1419 (push stream
*read-from-string-spares
*)))))
1423 (defun parse-integer (string &key
(start 0) end
(radix 10) junk-allowed
)
1425 "Examine the substring of string delimited by start and end
1426 (default to the beginning and end of the string) It skips over
1427 whitespace characters and then tries to parse an integer. The
1428 radix parameter must be between 2 and 36."
1429 (macrolet ((parse-error (format-control)
1430 `(error 'simple-parse-error
1431 :format-control
,format-control
1432 :format-arguments
(list string
))))
1433 (with-array-data ((string string
)
1435 (end (%check-vector-sequence-bounds string start end
)))
1436 (let ((index (do ((i start
(1+ i
)))
1439 (return-from parse-integer
(values nil end
))
1440 (parse-error "no non-whitespace characters in string ~S.")))
1441 (declare (fixnum i
))
1442 (unless (whitespacep (char string i
)) (return i
))))
1446 (declare (fixnum index
))
1447 (let ((char (char string index
)))
1448 (cond ((char= char
#\-
)
1454 (when (= index end
) (return nil
))
1455 (let* ((char (char string index
))
1456 (weight (digit-char-p char radix
)))
1458 (setq result
(+ weight
(* result radix
))
1460 (junk-allowed (return nil
))
1462 (do ((jndex (1+ index
) (1+ jndex
)))
1464 (declare (fixnum jndex
))
1465 (unless (whitespacep (char string jndex
))
1466 (parse-error "junk in string ~S")))
1469 (parse-error "junk in string ~S"))))
1473 (if minusp
(- result
) result
)
1476 (parse-error "no digits in string ~S")))
1479 ;;;; reader initialization code
1481 (defun !reader-cold-init
()
1482 (!cold-init-read-buffer
)
1483 (!cold-init-secondary-attribute-table
)
1484 (!cold-init-standard-readtable
)
1485 ;; FIXME: This was commented out, but should probably be restored.
1486 #+nil
(!cold-init-integer-reader
))
1488 (def!method print-object
((readtable readtable
) stream
)
1489 (print-unreadable-object (readtable stream
:identity t
:type t
)))