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
*))
25 (setf (fdocumentation '*readtable
* 'variable
)
26 "Variable bound to current readtable.")
28 ;;; A standard Lisp readtable (once cold-init is through). This is for
29 ;;; recovery from broken read-tables (and for
30 ;;; WITH-STANDARD-IO-SYNTAX), and should not normally be user-visible.
31 ;;; If the initial value is changed from NIL to something more interesting,
32 ;;; be sure to update the duplicated definition in "src/code/print.lisp"
33 (defglobal *standard-readtable
* nil
)
35 ;;; In case we get an error trying to parse a symbol, we want to rebind the
36 ;;; above stuff so it's cool.
41 (defun reader-eof-error (stream context
)
42 (declare (optimize allow-non-returning-tail-call
))
43 (error 'reader-eof-error
47 ;;; If The Gods didn't intend for us to use multiple namespaces, why
48 ;;; did They specify them?
49 (defun simple-reader-error (stream control
&rest args
)
50 (declare (optimize allow-non-returning-tail-call
))
51 (error 'simple-reader-error
53 :format-control control
54 :format-arguments args
))
56 ;;;; macros and functions for character tables
58 (declaim (ftype (sfunction (character readtable
) (unsigned-byte 8))
60 (defun get-cat-entry (char rt
)
61 (if (typep char
'base-char
)
62 (elt (character-attribute-array rt
) (char-code char
))
63 (values (gethash char
(character-attribute-hash-table rt
)
64 +char-attr-constituent
+))))
66 (defun set-cat-entry (char newvalue
&optional
(rt *readtable
*))
67 (declare (character char
) (type (unsigned-byte 8) newvalue
) (readtable rt
))
68 (if (typep char
'base-char
)
69 (setf (elt (character-attribute-array rt
) (char-code char
)) newvalue
)
70 (if (= newvalue
+char-attr-constituent
+)
71 ;; Default value for the C-A-HASH-TABLE is +CHAR-ATTR-CONSTITUENT+.
72 (%remhash char
(character-attribute-hash-table rt
))
73 (setf (gethash char
(character-attribute-hash-table rt
)) newvalue
)))
76 ;; Set the character-macro-table entry without coercing NEW-VALUE.
77 ;; As used by set-syntax-from-char it must always process "raw" values.
78 (defun set-cmt-entry (char new-value
&optional
(rt *readtable
*))
79 (declare (character char
)
80 (type (or null function fdefn
) new-value
)
82 (if (typep char
'base-char
)
83 (setf (svref (character-macro-array rt
) (char-code char
)) new-value
)
84 (if new-value
; never store NILs
85 (setf (gethash char
(character-macro-hash-table rt
)) new-value
)
86 (remhash char
(character-macro-hash-table rt
)))))
88 ;;; the value actually stored in the character macro table. As per
89 ;;; ANSI #'GET-MACRO-CHARACTER and #'SET-MACRO-CHARACTER, this can
90 ;;; be either a function-designator or NIL, except that we store
91 ;;; symbols not as themselves but as their #<fdefn>.
92 (defun get-raw-cmt-entry (char readtable
)
93 (declare (character char
) (readtable readtable
))
94 (if (typep char
'base-char
)
95 (svref (character-macro-array readtable
) (char-code char
))
96 (values (gethash char
(character-macro-hash-table readtable
) nil
))))
98 ;; As above but get the entry for SUB-CHAR in a dispatching macro table.
99 (defun get-raw-cmt-dispatch-entry (sub-char sub-table
)
100 (declare (character sub-char
))
101 (if (typep sub-char
'base-char
)
102 (svref (truly-the (simple-vector #.base-char-code-limit
)
103 (cdr (truly-the cons sub-table
)))
104 (char-code sub-char
))
105 (awhen (car sub-table
)
106 (gethash sub-char it
))))
108 ;; Coerce THING to a character-macro-table entry
109 (defmacro !coerce-to-cmt-entry
(thing)
111 (if (typep x
'(or null function
)) x
(find-or-create-fdefn x
))))
113 ;; Return a callable function given a character-macro-table entry.
114 (defmacro !cmt-entry-to-function
(val fallback
)
118 (cond ((functionp x
) x
)
120 (t (sb!c
:safe-fdefn-fun x
))))))
122 ;; Return a function-designator given a character-macro-table entry.
123 (defmacro !cmt-entry-to-fun-designator
(val)
125 (if (fdefn-p x
) (fdefn-name x
) x
)))
127 ;;; The character attribute table is a BASE-CHAR-CODE-LIMIT vector
128 ;;; of (unsigned-byte 8) plus a hashtable to handle higher character codes.
130 (defmacro test-attribute
(char whichclass rt
)
131 `(= (get-cat-entry ,char
,rt
) ,whichclass
))
133 ;;; predicates for testing character attributes
137 (declaim (inline whitespace
[1]p whitespace[2]p))
138 (declaim (inline constituentp terminating-macrop))
139 (declaim (inline single-escape-p multiple-escape-p))
140 (declaim (inline token-delimiterp)))
142 ;;; the [1] and
[2] here refer to ANSI glossary entries for
144 ;; whitespace[2]p is the only predicate whose readtable is optional
145 ;; - other than whitespace[1]p which has a fixed readtable - due to
146 ;; callers not otherwise needing a readtable at all, and so not binding
147 ;; *READTABLE* into a local variable throughout their lifetime.
148 (defun whitespace[1]p (char)
149 (test-attribute char +char-attr-whitespace+ *standard-readtable*))
150 (defun whitespace[2]p (char &optional (rt *readtable*))
151 (test-attribute char +char-attr-whitespace+ rt))
153 (defun constituentp (char rt)
154 (test-attribute char +char-attr-constituent+ rt))
156 (defun terminating-macrop (char rt)
157 (test-attribute char +char-attr-terminating-macro+ rt))
159 (defun single-escape-p (char rt)
160 (test-attribute char +char-attr-single-escape+ rt))
162 (defun multiple-escape-p (char rt)
163 (test-attribute char +char-attr-multiple-escape+ rt))
165 (defun token-delimiterp (char &optional (rt *readtable*))
166 ;; depends on actual attribute numbering in readtable.lisp.
167 (<= (get-cat-entry char rt) +char-attr-terminating-macro+))
169 ;;;; constituent traits (see ANSI 2.1.4.2)
171 ;;; There are a number of "secondary" attributes which are constant
172 ;;; properties of characters (as long as they are constituents).
174 (declaim (type attribute-table *constituent-trait-table*))
175 (defglobal *constituent-trait-table*
176 (make-array base-char-code-limit
177 :element-type '(unsigned-byte 8)
178 :initial-element +char-attr-constituent+))
180 (defun !set-constituent-trait (char trait)
181 (aver (typep char 'base-char))
182 (setf (elt *constituent-trait-table* (char-code char))
185 (defun !cold-init-constituent-trait-table ()
186 (!set-constituent-trait #\: +char-attr-package-delimiter+)
187 (!set-constituent-trait #\. +char-attr-constituent-dot+)
188 (!set-constituent-trait #\+ +char-attr-constituent-sign+)
189 (!set-constituent-trait #\- +char-attr-constituent-sign+)
190 (!set-constituent-trait #\/ +char-attr-constituent-slash+)
191 (do ((i (char-code #\0) (1+ i)))
192 ((> i (char-code #\9)))
193 (!set-constituent-trait (code-char i) +char-attr-constituent-digit+))
194 (!set-constituent-trait #\E +char-attr-constituent-expt+)
195 (!set-constituent-trait #\F +char-attr-constituent-expt+)
196 (!set-constituent-trait #\D +char-attr-constituent-expt+)
197 (!set-constituent-trait #\S +char-attr-constituent-expt+)
198 (!set-constituent-trait #\L +char-attr-constituent-expt+)
199 (!set-constituent-trait #\e +char-attr-constituent-expt+)
200 (!set-constituent-trait #\f +char-attr-constituent-expt+)
201 (!set-constituent-trait #\d +char-attr-constituent-expt+)
202 (!set-constituent-trait #\s +char-attr-constituent-expt+)
203 (!set-constituent-trait #\l +char-attr-constituent-expt+)
204 (!set-constituent-trait #\Space +char-attr-invalid+)
205 (!set-constituent-trait #\Newline +char-attr-invalid+)
206 (dolist (c (list backspace-char-code tab-char-code form-feed-char-code
207 return-char-code rubout-char-code))
208 (!set-constituent-trait (code-char c) +char-attr-invalid+)))
210 (declaim (inline get-constituent-trait))
211 (defun get-constituent-trait (char)
212 (if (typep char 'base-char)
213 (elt *constituent-trait-table* (char-code char))
214 +char-attr-constituent+))
216 ;;;; Readtable Operations
218 (defun assert-not-standard-readtable (readtable operation)
219 (when (eq readtable *standard-readtable*)
220 (cerror "Frob it anyway!" 'standard-readtable-modified-error
221 :operation operation)))
223 (defun readtable-case (readtable)
224 (aref #(:upcase :downcase :preserve :invert) (%readtable-case readtable)))
226 (defun (setf readtable-case) (case readtable)
227 ;; This function does not accept a readtable designator, only a readtable.
228 (assert-not-standard-readtable readtable '(setf readtable-case))
229 (setf (%readtable-case readtable)
230 (ecase case (:upcase 0) (:downcase 1) (:preserve 2) (:invert 3)))
233 (defun readtable-normalization (readtable)
234 "Returns T if READTABLE normalizes strings to NFKC, and NIL otherwise.
235 The READTABLE-NORMALIZATION of the standard readtable is T."
236 (%readtable-normalization readtable))
238 (defun (setf readtable-normalization) (new-value readtable)
239 "Sets the READTABLE-NORMALIZATION of the given READTABLE to NEW-VALUE.
240 Pass T to make READTABLE normalize symbols to NFKC (the default behavior),
241 and NIL to suppress normalization."
242 ;; This function does not accept a readtable designator, only a readtable.
243 (assert-not-standard-readtable readtable '(setf readtable-normalization))
244 (setf (%readtable-normalization readtable) new-value))
246 (defun readtable-base-char-preference (readtable)
247 "Returns :SYMBOLS, :STRINGS, :BOTH, or NIL, depending on whether the
248 reader should try to intern a base-string when reading a symbol name,
249 respectively produce a base-string when reading a quoted string, or in both
250 cases, or neither. The preference applies when a symbol-name or string
251 contains only BASE-CHAR characters. An (ARRAY CHARACTER (*)) can always
252 be interned (returned, respectively) as required. The default is :SYMBOLS."
253 ;; For efficiency the single preference occupies two slots internally.
254 (let ((symbols (eq (%readtable-symbol-preference readtable) 'base-char))
255 (strings (eq (%readtable-string-preference readtable) 'base-char)))
256 (cond ((and strings symbols) :both)
258 (strings :strings))))
260 (defun (setf readtable-base-char-preference) (new-value readtable)
261 (declare (type (member :symbols :strings :both nil) new-value))
262 "Sets the READTABLE-BASE-CHAR-PREFERENCE of the given READTABLE."
263 (setf (%readtable-symbol-preference readtable)
264 (if (member new-value '(:symbols :both)) 'base-char 'character)
265 (%readtable-string-preference readtable)
266 (if (member new-value '(:strings :both)) 'base-char 'character))
269 (defun replace/eql-hash-table (to from &optional (transform #'identity))
270 (maphash (lambda (k v) (setf (gethash k to) (funcall transform v))) from)
273 (defun %make-dispatch-macro-char (dtable)
274 (lambda (stream char)
275 (declare (ignore char))
276 (read-dispatch-char stream dtable)))
278 (defun %dispatch-macro-char-table (fun)
280 (eq (%closure-fun fun)
281 (load-time-value (%closure-fun (%make-dispatch-macro-char nil))
283 (find-if-in-closure #'consp fun)))
285 ;; If ENTRY is a dispatching macro, copy its dispatch table.
286 ;; Otherwise return it without alteration.
287 (defun copy-cmt-entry (entry)
288 (let ((dtable (%dispatch-macro-char-table entry)))
290 (%make-dispatch-macro-char
291 (cons (awhen (car dtable)
292 (replace/eql-hash-table (make-hash-table) it))
293 (copy-seq (cdr dtable))))
296 (defun copy-readtable (&optional (from-readtable *readtable*) to-readtable)
297 "Copies FROM-READTABLE and returns the result. Uses TO-READTABLE as a target
298 for the copy when provided, otherwise a new readtable is created. The
299 FROM-READTABLE defaults to the standard readtable when NIL and to the current
300 readtable when not provided."
301 (assert-not-standard-readtable to-readtable 'copy-readtable)
302 (let ((really-from-readtable (or from-readtable *standard-readtable*))
303 (really-to-readtable (or to-readtable (make-readtable))))
304 (replace (character-attribute-array really-to-readtable)
305 (character-attribute-array really-from-readtable))
306 (replace/eql-hash-table
307 (clrhash (character-attribute-hash-table really-to-readtable))
308 (character-attribute-hash-table really-from-readtable))
309 (map-into (character-macro-array really-to-readtable)
311 (character-macro-array really-from-readtable))
312 (replace/eql-hash-table
313 (clrhash (character-macro-hash-table really-to-readtable))
314 (character-macro-hash-table really-from-readtable)
316 (setf (readtable-case really-to-readtable)
317 (readtable-case really-from-readtable))
318 (setf (%readtable-string-preference really-to-readtable)
319 (%readtable-string-preference really-from-readtable)
320 (%readtable-symbol-preference really-to-readtable)
321 (%readtable-symbol-preference really-from-readtable))
322 (setf (readtable-normalization really-to-readtable)
323 (readtable-normalization really-from-readtable))
324 really-to-readtable))
326 (defun set-syntax-from-char (to-char from-char &optional
327 (to-readtable *readtable*) (from-readtable nil))
328 "Causes the syntax of TO-CHAR to be the same as FROM-CHAR in the optional
329 readtable (defaults to the current readtable). The FROM-TABLE defaults to the
330 standard Lisp readtable when NIL."
331 ;; TO-READTABLE is a readtable, not a readtable-designator
332 (assert-not-standard-readtable to-readtable 'set-syntax-from-char)
333 (let* ((really-from-readtable (or from-readtable *standard-readtable*))
334 (att (get-cat-entry from-char really-from-readtable))
335 (mac (get-raw-cmt-entry from-char really-from-readtable)))
336 (set-cat-entry to-char att to-readtable)
337 (set-cmt-entry to-char (copy-cmt-entry mac) to-readtable))
340 (defun set-macro-character (char function &optional
341 (non-terminatingp nil)
342 (rt-designator *readtable*))
343 "Causes CHAR to be a macro character which invokes FUNCTION when seen
344 by the reader. The NON-TERMINATINGP flag can be used to make the macro
345 character non-terminating, i.e. embeddable in a symbol name."
346 (let ((designated-readtable (or rt-designator *standard-readtable*)))
347 (assert-not-standard-readtable designated-readtable 'set-macro-character)
348 (set-cat-entry char (if non-terminatingp
349 +char-attr-constituent+
350 +char-attr-terminating-macro+)
351 designated-readtable)
352 (set-cmt-entry char (!coerce-to-cmt-entry function) designated-readtable)
353 t)) ; (ANSI-specified return value)
355 (defun get-macro-character (char &optional (rt-designator *readtable*))
356 "Return the function associated with the specified CHAR which is a macro
357 character, or NIL if there is no such function. As a second value, return
358 T if CHAR is a macro character which is non-terminating, i.e. which can
359 be embedded in a symbol name."
360 (let* ((designated-readtable (or rt-designator *standard-readtable*))
361 ;; the first return value: (OR FUNCTION SYMBOL) if CHAR is a macro
362 ;; character, or NIL otherwise
363 (fun-value (!cmt-entry-to-fun-designator
364 (get-raw-cmt-entry char designated-readtable))))
366 ;; NON-TERMINATING-P return value:
368 (or (constituentp char designated-readtable)
369 (not (terminating-macrop char designated-readtable)))
370 ;; ANSI's definition of GET-MACRO-CHARACTER says this
371 ;; value is NIL when CHAR is not a macro character.
372 ;; I.e. this value means not just "non-terminating
373 ;; character?" but "non-terminating macro character?".
376 (defun get-dispatch-macro-char-table (disp-char readtable &optional (errorp t))
377 (cond ((%dispatch-macro-char-table (get-raw-cmt-entry disp-char readtable)))
378 (errorp (error "~S is not a dispatching macro character." disp-char))))
380 (defun make-dispatch-macro-character (char &optional
381 (non-terminating-p nil)
383 "Cause CHAR to become a dispatching macro character in readtable (which
384 defaults to the current readtable). If NON-TERMINATING-P, the char will
386 ;; This used to call ERROR if the character was already a dispatching
387 ;; macro but I saw no evidence of that in other implementations except cmucl.
388 ;; Without a portable way to inquire whether a character is dispatching,
389 ;; a file that frobs *READTABLE* can't be repeatedly loaded except
390 ;; by catching the error, so I removed it.
391 ;; RT is a readtable, not a readtable-designator, as per CLHS.
392 (unless (get-dispatch-macro-char-table char rt nil)
393 ;; The dtable is a cons whose whose CAR is initially NIL but upgraded
394 ;; to a hashtable if required, and whose CDR is a vector indexed by
395 ;; char-code up to the maximum base-char.
396 (let ((dtable (cons nil (make-array base-char-code-limit
397 :initial-element nil))))
398 (set-macro-character char (%make-dispatch-macro-char dtable)
399 non-terminating-p rt)))
402 (defun set-dispatch-macro-character (disp-char sub-char function
403 &optional (rt-designator *readtable*))
404 "Cause FUNCTION to be called whenever the reader reads DISP-CHAR
405 followed by SUB-CHAR."
406 ;; Get the dispatch char for macro (error if not there), diddle
407 ;; entry for sub-char.
408 (let* ((sub-char (char-upcase sub-char))
409 (readtable (or rt-designator *standard-readtable*)))
410 (assert-not-standard-readtable readtable 'set-dispatch-macro-character)
411 (when (digit-char-p sub-char)
412 (error "SUB-CHAR must not be a decimal digit: ~S" sub-char))
413 (let ((dtable (get-dispatch-macro-char-table disp-char readtable))
414 (function (!coerce-to-cmt-entry function)))
415 ;; (SET-MACRO-CHARACTER #\$ (GET-MACRO-CHARACTER #\#)) will share
416 ;; the dispatch table. Perhaps it should be copy-on-write?
417 (if (typep sub-char 'base-char)
418 (setf (svref (cdr dtable) (char-code sub-char)) function)
419 (let ((hashtable (car dtable)))
420 (cond (function ; allocate the hashtable if it wasn't made yet
421 (setf (gethash sub-char
422 (or hashtable (setf (car dtable)
425 (hashtable ; remove an existing entry
426 (remhash sub-char hashtable)))))))
429 (defun get-dispatch-macro-character (disp-char sub-char
430 &optional (rt-designator *readtable*))
431 "Return the macro character function for SUB-CHAR under DISP-CHAR
432 or NIL if there is no associated function."
433 (let ((dtable (get-dispatch-macro-char-table
434 disp-char (or rt-designator *standard-readtable*))))
435 (!cmt-entry-to-fun-designator
436 (get-raw-cmt-dispatch-entry (char-upcase sub-char) dtable))))
439 ;;;; definitions to support internal programming conventions
441 (defconstant +EOF+ 0)
443 (defun flush-whitespace (stream)
444 ;; This flushes whitespace chars, returning the last char it read (a
445 ;; non-white one). It always gets an error on end-of-file.
446 (let* ((stream (in-stream-from-designator stream))
448 (attribute-array (character-attribute-array rt))
449 (attribute-hash-table (character-attribute-hash-table rt)))
450 (macrolet ((done-p ()
451 '(not (eql (if (typep char 'base-char)
452 (aref attribute-array (char-code char))
453 (gethash char attribute-hash-table
454 +char-attr-constituent+))
455 +char-attr-whitespace+))))
456 (if (ansi-stream-p stream)
457 (prepare-for-fast-read-char stream
458 (loop (let ((char (fast-read-char t)))
460 (done-with-fast-read-char)
463 (loop (let ((char (read-char stream nil +EOF+)))
464 ;; (THE) should not be needed if DONE-P, but it was not
465 ;; being derived to return a character, causing an extra
466 ;; check in consumers of flush-whitespace despite the
467 ;; promise to return a character or else signal EOF.
468 (cond ((eq char +EOF+) (error 'end-of-file :stream stream))
469 ((done-p) (return (the character char))))))))))
471 ;;;; temporary initialization hack
473 ;; Install the (easy) standard macro-chars into *READTABLE*.
474 (defun !cold-init-standard-readtable ()
475 (/show0 "entering !cold-init-standard-readtable")
476 ;; All characters get boring defaults in MAKE-READTABLE. Now we
477 ;; override the boring defaults on characters which need more
478 ;; interesting behavior.
479 (flet ((whitespaceify (char)
480 (set-cmt-entry char nil)
481 (set-cat-entry char +char-attr-whitespace+)))
482 (whitespaceify (code-char tab-char-code))
483 (whitespaceify #\Newline)
484 (whitespaceify #\Space)
485 (whitespaceify (code-char form-feed-char-code))
486 (whitespaceify (code-char return-char-code)))
488 (set-cat-entry #\\ +char-attr-single-escape+)
489 (set-cmt-entry #\\ nil)
491 (set-cat-entry #\| +char-attr-multiple-escape+)
492 (set-cmt-entry #\| nil)
494 ;; Easy macro-character definitions are in this source file.
495 (set-macro-character #\" #'read-string)
496 (set-macro-character #\' #'read-quote)
497 ;; Using symbols makes these traceable and redefineable with ease,
498 ;; as well as avoids a forward-referenced function (from "backq")
499 (set-macro-character #\( 'read-list)
500 (set-macro-character #\) 'read-right-paren)
501 (set-macro-character #\; #'read-comment)
502 ;; (The hairier macro-character definitions, for #\# and #\`, are
503 ;; defined elsewhere, in their own source files.)
506 (do ((ichar 0 (1+ ichar))
508 ((= ichar base-char-code-limit))
509 (setq char (code-char ichar))
510 (when (constituentp char *readtable*)
511 (set-cmt-entry char nil)))
513 (/show0 "leaving !cold-init-standard-readtable"))
515 ;;;; implementation of the read buffer
517 (defstruct (token-buf (:predicate nil) (:copier nil)
521 (initial-string (make-string 128))
522 (string initial-string)
525 :element-type 'character
527 :displaced-to string)))))
528 ;; The string accumulated during reading of tokens.
529 ;; Always starts out EQ to 'initial-string'.
530 (string nil :type (simple-array character (*)))
531 ;; Counter advanced as characters are placed into 'string'
532 (fill-ptr 0 :type index)
533 ;; Counter advanced as characters are consumed from 'string' on re-scan
534 ;; by auxilliary functions MAKE-{INTEGER,FLOAT,RATIONAL} etc.
535 (cursor 0 :type index)
536 ;; A string used only for FIND-PACKAGE calls in package-qualified
537 ;; symbols so that we don't need to call SUBSEQ on the 'string'.
538 (adjustable-string nil :type (and (array character (*)) (not simple-array)))
539 ;; A small string that is permanently assigned into this token-buf.
540 (initial-string nil :type (simple-array character (128))
542 (escapes (make-array 10 :element-type 'fixnum :fill-pointer 0 :adjustable t)
543 :type (and (vector fixnum) (not simple-array)) :read-only t)
544 ;; Link to next TOKEN-BUF, to chain the *TOKEN-BUF-POOL* together.
545 (next nil :type (or null token-buf))
546 (only-base-chars t :type boolean))
547 (declaim (freeze-type token-buf))
549 (defmethod print-object ((self token-buf) stream)
550 (print-unreadable-object (self stream :identity t :type t)
551 (format stream "~@[next=~S~]" (token-buf-next self))))
553 ;; The current TOKEN-BUF
554 (declaim (type token-buf *read-buffer*))
555 (defvar *read-buffer*)
557 ;; A list of available TOKEN-BUFs
558 ;; Should need no toplevel binding if multi-threaded,
559 ;; but doesn't really matter, as INITIAL-THREAD-FUNCTION-TRAMPOLINE
561 (declaim (type (or null token-buf) *token-buf-pool*))
562 (defvar *token-buf-pool* nil)
564 (defun reset-read-buffer (buffer)
565 ;; Turn BUFFER into an empty read buffer.
566 (setf (fill-pointer (token-buf-escapes buffer)) 0)
567 (setf (token-buf-fill-ptr buffer) 0)
568 (setf (token-buf-cursor buffer) 0)
569 (setf (token-buf-only-base-chars buffer) t)
572 ;; "Output" a character into the reader's buffer.
573 (declaim (inline ouch-read-buffer))
574 (defun ouch-read-buffer (char buffer)
575 ;; When buffer overflow
576 (let ((op (token-buf-fill-ptr buffer)))
577 (declare (optimize (sb!c::insert-array-bounds-checks 0)))
578 (when (>= op (length (token-buf-string buffer)))
579 ;; an out-of-line call for the uncommon case avoids bloat.
580 ;; Size should be doubled.
582 (unless (typep char 'base-char)
583 (setf (token-buf-only-base-chars buffer) nil))
584 (setf (elt (token-buf-string buffer) op) char)
585 (setf (token-buf-fill-ptr buffer) (1+ op))))
587 (defun ouch-read-buffer-escaped (char buf)
588 (vector-push-extend (token-buf-fill-ptr buf) (token-buf-escapes buf))
589 (ouch-read-buffer char buf))
591 (defun grow-read-buffer ()
592 (let* ((b *read-buffer*)
593 (string (token-buf-string b)))
594 (setf (token-buf-string b)
595 (replace (make-string (* 2 (length string))) string))))
597 ;; Retun the next character from the buffered token, or NIL.
598 (declaim (maybe-inline token-buf-getchar))
599 (defun token-buf-getchar (b)
600 (declare (optimize (sb!c::insert-array-bounds-checks 0)))
601 (let ((i (token-buf-cursor (truly-the token-buf b))))
602 (and (< i (token-buf-fill-ptr b))
603 (prog1 (elt (token-buf-string b) i)
604 (setf (token-buf-cursor b) (1+ i))))))
606 ;; Grab a buffer off the token-buf pool if there is one, or else make one.
607 ;; This does not need to be protected against other threads because the
608 ;; pool is thread-local, or against async interrupts. An async signal
609 ;; delivered anywhere in the midst of the code sequence below can not
610 ;; corrupt the buffer given to the caller of ACQUIRE-TOKEN-BUF.
611 ;; Additionally the cleanup is on a "best effort" basis. Async unwinds
612 ;; through WITH-READ-BUFFER fail to recycle token-bufs, but that's ok.
613 (defun acquire-token-buf ()
614 (let ((this-buffer *token-buf-pool*))
616 (shiftf *token-buf-pool* (token-buf-next this-buffer) nil)
621 (defun release-token-buf (chain)
622 (named-let free ((buffer chain))
623 ;; If 'adjustable-string' was displaced to 'string',
624 ;; adjust it back down to allow GC of the abnormally large string.
625 (unless (eq (%array-data (token-buf-adjustable-string buffer))
626 (token-buf-initial-string buffer))
627 (adjust-array (token-buf-adjustable-string buffer) '(0)
628 :displaced-to (token-buf-initial-string buffer)))
629 ;; 'initial-string' is assigned into 'string'
630 ;; so not to preserve huge buffers in the pool indefinitely.
631 (setf (token-buf-string buffer) (token-buf-initial-string buffer))
632 (if (token-buf-next buffer)
633 (free (token-buf-next buffer))
634 (setf (token-buf-next buffer) *token-buf-pool*)))
635 (setf *token-buf-pool* chain))
637 ;; Return a fresh copy of BUFFER's string
638 (defun copy-token-buf-string (buffer)
639 (subseq (token-buf-string buffer) 0 (token-buf-fill-ptr buffer)))
641 ;; Return a string displaced to BUFFER's string.
642 ;; The string should not be held onto - either a copy must be made
643 ;; by the receiver, or it should be parsed into something else.
644 (defun sized-token-buf-string (buffer)
645 ;; It would in theory be faster to make the adjustable array have
646 ;; a fill-pointer, and just set that most of the time. Except we still
647 ;; need the ability to displace to a different string if a package name
648 ;; has >128 characters, so then there'd be two modes of sharing, one of
649 ;; which is rarely exercised and most likely to be subtly wrong.
650 ;; At any rate, SET-ARRAY-HEADER is faster than ADJUST-ARRAY.
651 ;; TODO: find evidence that it is/is-not worth having complicated
652 ;; mechanism involving a fill-pointer or not.
654 (token-buf-adjustable-string buffer) ; the array
655 (token-buf-string buffer) ; the underlying data
656 (token-buf-fill-ptr buffer) ; total size
659 (token-buf-fill-ptr buffer) ; dimension 0
660 t nil)) ; displacedp / newp
662 ;; Acquire a TOKEN-BUF from the pool and execute the body, returning only
663 ;; the primary value therefrom. Recycle the buffer when done.
664 ;; No UNWIND-PROTECT - recycling is designed to help with the common case
665 ;; of normal return and is not intended to be resilient against nonlocal exit.
666 (defmacro with-read-buffer (() &body body)
667 `(let* ((*read-buffer* (acquire-token-buf))
668 (result (progn ,@body)))
669 (release-token-buf *read-buffer*)
672 (defun check-for-recursive-read (stream recursive-p operator-name)
673 (when (and recursive-p (not (boundp '*read-buffer*)))
676 "~A was invoked with RECURSIVE-P being true outside ~
677 of a recursive read operation."
680 ;;;; READ-PRESERVING-WHITESPACE, READ-DELIMITED-LIST, and READ
682 ;;; A list for #=, used to keep track of objects with labels assigned that
683 ;;; have been completely read. Each entry is a SHARP-EQUAL-WRAPPER object.
685 ;;; KLUDGE: Should this really be a list? It seems as though users
686 ;;; could reasonably expect N log N performance for large datasets.
687 ;;; On the other hand, it's probably very very seldom a problem in practice.
688 ;;; On the third hand, it might be just as easy to use a hash table,
689 ;;; so maybe we should. -- WHN 19991202
690 (defvar *sharp-equal* ())
692 (declaim (ftype (sfunction (t t) (values bit t)) read-maybe-nothing))
694 ;;; Like READ-PRESERVING-WHITESPACE, but doesn't check the read buffer
695 ;;; for being set up properly.
696 (defun %read-preserving-whitespace (stream eof-error-p eof-value recursive-p)
697 (declare (optimize (sb!c::check-tag-existence 0)))
699 ;; a loop for repeating when a macro returns nothing
700 (let* ((tracking-p (form-tracking-stream-p stream))
703 (null (form-tracking-stream-form-start-char-pos stream)))))
705 (let ((char (read-char stream eof-error-p +EOF+)))
706 (cond ((eq char +EOF+) (return eof-value))
707 ((whitespace[2]p char))
710 ;; Calling FILE-POSITION at each token seems to slow down
711 ;; the reader by somewhere between 8x to 10x.
712 ;; Once per outermost form is acceptably fast though.
713 (setf (form-tracking-stream-form-start-byte-pos stream)
714 ;; pretend we queried the position before reading CHAR
715 (- (file-position stream)
716 (or (file-string-length stream (string char)) 0))
717 (form-tracking-stream-form-start-char-pos stream)
719 (1- (form-tracking-stream-input-char-pos stream))))
720 (multiple-value-bind (result-p result)
721 (read-maybe-nothing stream char)
722 (unless (zerop result-p)
723 (return (unless *read-suppress* result)))
724 ;; Repeat if macro returned nothing.
726 (funcall (form-tracking-stream-observer stream)
727 :reset nil nil))))))))
728 (let ((*sharp-equal* nil))
730 (%read-preserving-whitespace stream eof-error-p eof-value t)))))
732 ;;; READ-PRESERVING-WHITESPACE behaves just like READ, only it makes
733 ;;; sure to leave terminating whitespace in the stream. (This is a
734 ;;; COMMON-LISP exported symbol.)
735 (defun read-preserving-whitespace (&optional (stream *standard-input*)
739 "Read from STREAM and return the value read, preserving any whitespace
740 that followed the object."
741 (declare (explicit-check))
742 (check-for-recursive-read stream recursive-p 'read-preserving-whitespace)
743 (%read-preserving-whitespace stream eof-error-p eof-value recursive-p))
745 ;;; Read from STREAM given starting CHAR, returning 1 and the resulting
746 ;;; object, unless CHAR is a macro yielding no value, then 0 and NIL,
747 ;;; for functions that want comments to return so that they can look
748 ;;; past them. CHAR must not be whitespace.
749 (defun read-maybe-nothing (stream char)
751 (values bit t) ; avoid a type-check. M-V-CALL is lame
753 (lambda (stream start-pos &optional (result nil supplied-p) &rest junk)
754 (declare (ignore junk)) ; is this ANSI-specified?
755 (when (and supplied-p start-pos)
756 (funcall (form-tracking-stream-observer stream)
758 (form-tracking-stream-input-char-pos stream) result))
759 (values (if supplied-p 1 0) result))
760 ;; KLUDGE: not capturing anything in the lambda avoids closure consing
762 (and (form-tracking-stream-p stream)
763 ;; Subtract 1 because the position points _after_ CHAR.
764 (1- (form-tracking-stream-input-char-pos stream)))
765 (funcall (!cmt-entry-to-function
766 (get-raw-cmt-entry char *readtable*) #'read-token)
769 (defun read (&optional (stream *standard-input*)
773 "Read the next Lisp value from STREAM, and return it."
774 (declare (explicit-check))
775 (check-for-recursive-read stream recursive-p 'read)
776 (let* ((local-eof-val (load-time-value (cons nil nil) t))
777 (result (%read-preserving-whitespace
778 stream eof-error-p local-eof-val recursive-p)))
779 ;; This function generally discards trailing whitespace. If you
780 ;; don't want to discard trailing whitespace, call
781 ;; CL:READ-PRESERVING-WHITESPACE instead.
782 (unless (or (eql result local-eof-val) recursive-p)
783 (let ((next-char (read-char stream nil +EOF+)))
784 (unless (or (eq next-char +EOF+)
785 (whitespace[2]p next-char))
786 (unread-char next-char stream))))
787 (if (eq result local-eof-val) eof-value result)))
790 ;;;; basic readmacro definitions
792 ;;;; Some large, hairy subsets of readmacro definitions (backquotes
793 ;;;; and sharp macros) are not here, but in their own source files.
795 (defun read-quote (stream ignore)
796 (declare (ignore ignore))
797 (list 'quote (read stream t nil t)))
799 (defun read-comment (stream ignore)
800 (declare (ignore ignore))
802 ((character-decoding-error
803 #'(lambda (decoding-error)
804 (declare (ignorable decoding-error))
806 'sb!kernel::character-decoding-error-in-macro-char-comment
807 :position (file-position stream) :stream stream)
808 (invoke-restart 'attempt-resync))))
809 (let ((stream (in-stream-from-designator stream)))
810 (if (ansi-stream-p stream)
811 (prepare-for-fast-read-char stream
812 (loop (let ((char (fast-read-char nil +EOF+)))
813 (when (or (eq char +EOF+) (char= char #\newline))
814 (return (done-with-fast-read-char))))))
816 (loop (let ((char (read-char stream nil +EOF+)))
817 (when (or (eq char +EOF+) (char= char #\newline))
819 ;; Don't return anything.
822 ;;; FIXME: for these two macro chars, if STREAM is a FORM-TRACKING-STREAM,
823 ;;; every cons cell should generate a notification so that the readtable
824 ;;; manipulation in SB-COVER can be eliminated in favor of a stream observer.
825 ;;; It is cheap to add events- it won't increase consing in the compiler
826 ;;; because it the extra events can simply be ignored.
828 ((with-list-reader ((streamvar delimiter) &body body)
829 `(let* ((thelist (list nil))
831 (collectp (if *read-suppress* 0 -1)))
832 (declare (dynamic-extent thelist))
833 (loop (let ((firstchar (flush-whitespace ,streamvar)))
834 (when (eq firstchar ,delimiter)
835 (return (cdr thelist)))
837 (read-list-item (streamvar)
838 `(multiple-value-bind (winp obj)
839 (read-maybe-nothing ,streamvar firstchar)
840 ;; allow for a character macro return to return nothing
841 (unless (zerop (logand winp collectp))
843 (cdr (rplacd (truly-the cons listtail) (list obj))))))))
845 ;;; The character macro handler for left paren
846 (defun read-list (stream ignore)
847 (declare (ignore ignore))
848 (with-list-reader (stream #\))
849 (when (eq firstchar #\.)
850 (let ((nextchar (read-char stream t)))
851 (cond ((token-delimiterp nextchar)
852 (cond ((eq listtail thelist)
853 (unless (zerop collectp)
855 stream "Nothing appears before . in list.")))
856 ((whitespace[2]p nextchar)
857 (setq nextchar (flush-whitespace stream))))
858 (rplacd (truly-the cons listtail)
859 (read-after-dot stream nextchar collectp))
860 ;; Check for improper ". ,@" or ". ,." now rather than
861 ;; in the #\` reader. The resulting QUASIQUOTE macro might
862 ;; never be exapanded, but nonetheless could be erroneous.
863 (unless (zerop (logand *backquote-depth* collectp))
864 (let ((lastcdr (cdr (last listtail))))
865 (when (and (comma-p lastcdr) (comma-splicing-p lastcdr))
867 stream "~S contains a splicing comma after a dot"
869 (return (cdr thelist)))
870 ;; Put back NEXTCHAR so that we can read it normally.
871 (t (unread-char nextchar stream)))))
872 ;; Next thing is not an isolated dot.
873 (read-list-item stream)))
875 ;;; (This is a COMMON-LISP exported symbol.)
876 (defun read-delimited-list (endchar &optional
877 (input-stream *standard-input*)
879 "Read Lisp values from INPUT-STREAM until the next character after a
880 value's representation is ENDCHAR, and return the objects as a list."
881 (declare (explicit-check))
882 (check-for-recursive-read input-stream recursive-p 'read-delimited-list)
883 (flet ((%read-delimited-list ()
884 (with-list-reader (input-stream endchar)
885 (read-list-item input-stream))))
887 (%read-delimited-list)
888 (with-read-buffer () (%read-delimited-list)))))) ; end MACROLET
890 (defun read-after-dot (stream firstchar collectp)
891 ;; FIRSTCHAR is non-whitespace!
893 (do ((char firstchar (flush-whitespace stream)))
896 (return-from read-after-dot nil)
897 (simple-reader-error stream "Nothing appears after . in list.")))
898 ;; See whether there's something there.
899 (multiple-value-bind (winp obj) (read-maybe-nothing stream char)
900 (unless (zerop winp) (return (setq lastobj obj)))))
901 ;; At least one thing appears after the dot.
902 ;; Check for more than one thing following dot.
904 (let ((char (flush-whitespace stream)))
905 (cond ((eq char #\)) (return lastobj)) ;success!
906 ;; Try reading virtual whitespace.
907 ((not (zerop (logand (read-maybe-nothing stream char)
908 (truly-the fixnum collectp))))
910 stream "More than one object follows . in list.")))))))
912 (defun read-string (stream closech)
913 ;; This accumulates chars until it sees same char that invoked it.
914 ;; We avoid copying any given input character more than twice-
915 ;; once to a temp buffer and then to the result. In the worst case,
916 ;; we can waste space equal the unwasted space, if the final character
917 ;; causes allocation of a new buffer for just that character,
918 ;; because the buffer size is doubled each time it overflows.
919 ;; (Would be better to peek at the frc-buffer if the stream has one.)
920 ;; Scratch vectors are GC-able as soon as this function returns though.
921 (declare (character closech))
922 (macrolet ((scan (read-a-char eofp &optional finish)
923 `(loop (let ((char ,read-a-char))
924 (declare (optimize (sb!c::insert-array-bounds-checks 0)))
925 (cond (,eofp (error 'end-of-file :stream stream))
928 ((single-escape-p char rt)
929 (setq char ,read-a-char)
931 (error 'end-of-file :stream stream))))
935 (setq lim (the index (ash lim 1))
936 buf (make-array lim :element-type 'character)))
938 (setf (schar buf ptr) (truly-the character char))
939 #!+sb-unicode ; BASE-CHAR-P does not exist if not
940 (unless (base-char-p char) (setq only-base-chars nil))
942 (let* ((token-buf *read-buffer*)
943 (buf (token-buf-string token-buf))
945 (stream (in-stream-from-designator stream))
946 (suppress *read-suppress*)
951 (declare (type (simple-array character (*)) buf))
952 (reset-read-buffer token-buf)
953 (if (ansi-stream-p stream)
954 (prepare-for-fast-read-char stream
955 (scan (fast-read-char t) nil (done-with-fast-read-char)))
957 (scan (read-char stream nil +EOF+) (eq char +EOF+)))
960 (let* ((sum (loop for buf in chain sum (length buf)))
962 (make-array (+ sum ptr)
963 :element-type (if only-base-chars
964 (%readtable-string-preference rt)
967 ;; Now work backwards from the end
968 (replace result buf :start1 ptr)
969 (dolist (buf chain result)
970 (declare (type (simple-array character (*)) buf))
971 (let ((len (length buf)))
973 (replace result buf :start1 ptr))))))))
975 (defun read-right-paren (stream ignore)
976 (declare (ignore ignore))
977 (simple-reader-error stream "unmatched close parenthesis"))
979 ;;; Read from the stream up to the next delimiter. Leave the resulting
980 ;;; token in *READ-BUFFER*, and return three values:
982 ;;; -- whether any escape character was seen (even if no character is escaped)
983 ;;; -- whether a package delimiter character was seen
984 ;;; Normalizes the input to NFKC before returning
985 (defun internal-read-extended-token (stream firstchar escape-firstchar
986 &aux (read-buffer *read-buffer*))
987 (reset-read-buffer read-buffer)
988 (when escape-firstchar
989 (ouch-read-buffer-escaped firstchar read-buffer)
990 (setq firstchar (read-char stream nil +EOF+)))
991 (do ((char firstchar (read-char stream nil +EOF+))
992 (seen-multiple-escapes nil)
995 ((cond ((eq char +EOF+) t)
996 ((token-delimiterp char rt)
997 (unread-char char stream)
1001 (multiple-value-setq (read-buffer colon)
1002 (normalize-read-buffer read-buffer colon))
1004 (or (plusp (fill-pointer (token-buf-escapes read-buffer)))
1005 seen-multiple-escapes)
1007 (flet ((escape-1-char ()
1008 ;; It can't be a number, even if it's 1\23.
1009 ;; Read next char here, so it won't be casified.
1010 (let ((nextchar (read-char stream nil +EOF+)))
1011 (if (eq nextchar +EOF+)
1012 (reader-eof-error stream "after escape character")
1013 (ouch-read-buffer-escaped nextchar read-buffer)))))
1014 (cond ((single-escape-p char rt) (escape-1-char))
1015 ((multiple-escape-p char rt)
1016 (setq seen-multiple-escapes t)
1017 ;; Read to next multiple-escape, escaping single chars
1020 (let ((ch (read-char stream nil +EOF+)))
1021 (cond ((eq ch +EOF+)
1022 (reader-eof-error stream "inside extended token"))
1023 ((multiple-escape-p ch rt) (return))
1024 ((single-escape-p ch rt) (escape-1-char))
1025 (t (ouch-read-buffer-escaped ch read-buffer))))))
1027 (when (and (not colon) ; easiest test first
1028 (constituentp char rt)
1029 (eql (get-constituent-trait char)
1030 +char-attr-package-delimiter+))
1032 (ouch-read-buffer char read-buffer))))))
1034 ;;;; character classes
1036 ;;; Return the character class for CHAR.
1038 ;;; FIXME: why aren't these ATT-getting forms using GET-CAT-ENTRY?
1039 ;;; Because we've cached the readtable tables?
1040 (defmacro char-class (char attarray atthash)
1041 `(let ((att (if (typep (truly-the character ,char) 'base-char)
1042 (aref ,attarray (char-code ,char))
1043 (gethash ,char ,atthash +char-attr-constituent+))))
1044 (declare (fixnum att))
1046 ((<= att +char-attr-terminating-macro+) +char-attr-delimiter+)
1047 ((< att +char-attr-constituent+) att)
1048 (t (setf att (get-constituent-trait ,char))
1049 (if (= att +char-attr-invalid+)
1050 (simple-reader-error stream "invalid constituent")
1053 ;;; Return the character class for CHAR, which might be part of a
1054 ;;; rational number.
1055 (defmacro char-class2 (char attarray atthash read-base)
1056 `(let ((att (if (typep (truly-the character ,char) 'base-char)
1057 (aref ,attarray (char-code ,char))
1058 (gethash ,char ,atthash +char-attr-constituent+))))
1059 (declare (fixnum att))
1061 ((<= att +char-attr-terminating-macro+) +char-attr-delimiter+)
1062 ((< att +char-attr-constituent+) att)
1063 (t (setf att (get-constituent-trait ,char))
1065 ((digit-char-p ,char ,read-base) +char-attr-constituent-digit+)
1066 ((= att +char-attr-constituent-digit+) +char-attr-constituent+)
1067 ((= att +char-attr-invalid+)
1068 (simple-reader-error stream "invalid constituent"))
1071 ;;; Return the character class for a char which might be part of a
1072 ;;; rational or floating number. (Assume that it is a digit if it
1074 (defmacro char-class3 (char attarray atthash read-base)
1075 `(let ((att (if (typep (truly-the character ,char) 'base-char)
1076 (aref ,attarray (char-code ,char))
1077 (gethash ,char ,atthash +char-attr-constituent+))))
1078 (declare (fixnum att))
1080 ((<= att +char-attr-terminating-macro+) +char-attr-delimiter+)
1081 ((< att +char-attr-constituent+) att)
1082 (t (setf att (get-constituent-trait ,char))
1083 (when possibly-rational
1084 (setq possibly-rational
1085 (or (digit-char-p ,char ,read-base)
1086 (= att +char-attr-constituent-slash+))))
1087 (when possibly-float
1088 (setq possibly-float
1089 (or (digit-char-p ,char 10)
1090 (= att +char-attr-constituent-dot+))))
1092 ((digit-char-p ,char (max ,read-base 10))
1093 (if (digit-char-p ,char ,read-base)
1094 (if (= att +char-attr-constituent-expt+)
1095 +char-attr-constituent-digit-or-expt+
1096 +char-attr-constituent-digit+)
1097 +char-attr-constituent-decimal-digit+))
1098 ((= att +char-attr-invalid+)
1099 (simple-reader-error stream "invalid constituent"))
1104 (defvar *read-suppress* nil
1105 "Suppress most interpreting in the reader when T.")
1107 (defvar *read-base* 10
1108 "the radix that Lisp reads numbers in")
1109 (declaim (type (integer 2 36) *read-base*))
1111 ;;; Normalize TOKEN-BUF to NFKC, returning a new TOKEN-BUF and the
1113 (defun normalize-read-buffer (token-buf &optional colon)
1114 (when (or (token-buf-only-base-chars token-buf)
1115 (not (readtable-normalization *readtable*)))
1116 (return-from normalize-read-buffer (values token-buf colon)))
1117 (let ((current-buffer (copy-token-buf-string token-buf))
1118 (old-escapes (copy-seq (token-buf-escapes token-buf)))
1119 (str-to-normalize (make-string (token-buf-fill-ptr token-buf)))
1120 (normalize-ptr 0) (escapes-ptr 0))
1121 (reset-read-buffer token-buf)
1122 (macrolet ((clear-str-to-normalize ()
1124 (loop for char across (sb!unicode:normalize-string
1125 (subseq str-to-normalize 0 normalize-ptr)
1127 (ouch-read-buffer char token-buf))
1128 (setf normalize-ptr 0)))
1129 (push-to-normalize (ch)
1130 (let ((ch-gen (gensym)))
1131 `(let ((,ch-gen ,ch))
1132 (setf (char str-to-normalize normalize-ptr) ,ch-gen)
1133 (incf normalize-ptr)))))
1134 (loop for c across current-buffer
1137 (if (and (< escapes-ptr (length old-escapes))
1138 (eql i (aref old-escapes escapes-ptr)))
1140 (clear-str-to-normalize)
1141 (ouch-read-buffer-escaped c token-buf)
1143 (push-to-normalize c)))
1144 (clear-str-to-normalize)
1145 (values token-buf colon))))
1147 ;;; Modify the read buffer according to READTABLE-CASE, ignoring
1148 ;;; ESCAPES. ESCAPES is a vector of the escaped indices.
1149 (defun casify-read-buffer (token-buf)
1150 (let ((case (readtable-case *readtable*))
1151 (escapes (token-buf-escapes token-buf)))
1153 ((and (zerop (length escapes)) (eq case :upcase))
1154 (let ((buffer (token-buf-string token-buf)))
1155 (dotimes (i (token-buf-fill-ptr token-buf))
1156 (declare (optimize (sb!c::insert-array-bounds-checks 0)))
1157 (setf (schar buffer i) (char-upcase (schar buffer i))))))
1158 ((eq case :preserve))
1160 (macrolet ((skip-esc (&body body)
1161 `(do ((i (1- (token-buf-fill-ptr token-buf)) (1- i))
1162 (buffer (token-buf-string token-buf))
1163 (esc (if (zerop (fill-pointer escapes))
1164 -1 (vector-pop escapes))))
1167 (optimize (sb!c::insert-array-bounds-checks 0)))
1169 (let ((ch (schar buffer i)))
1173 (setq esc (if (zerop (fill-pointer escapes))
1174 -1 (vector-pop escapes))))))))
1176 (skip-esc (setf (schar buffer i) (char-downcase ch))))
1178 (skip-esc (setf (schar buffer i) (char-upcase ch)))))
1180 (:upcase (raise-em))
1181 (:downcase (lower-em))
1185 (fillptr (fill-pointer escapes)))
1187 (when (both-case-p ch)
1188 (if (upper-case-p ch)
1189 (setq all-lower nil)
1190 (setq all-upper nil))))
1191 (setf (fill-pointer escapes) fillptr)
1192 (cond (all-lower (raise-em))
1193 (all-upper (lower-em))))))))))))
1195 (eval-when (:compile-toplevel :load-toplevel :execute)
1196 (defvar *reader-package* nil))
1197 (declaim (type (or null package) *reader-package*)
1198 (always-bound *reader-package*))
1200 (defun reader-find-package (package-designator stream)
1201 (if (%instancep package-designator)
1203 (let ((package (find-package package-designator)))
1205 ;; Release the token-buf that was used for the designator
1206 (release-token-buf (shiftf (token-buf-next *read-buffer*) nil))
1209 (error 'simple-reader-package-error
1210 :package package-designator
1212 :format-control "Package ~A does not exist."
1213 :format-arguments (list package-designator)))))))
1215 (defun read-token (stream firstchar)
1216 "Default readmacro function. Handles numbers, symbols, and SBCL's
1217 extended <package-name>::<form-in-package> syntax."
1218 ;; Check explicitly whether FIRSTCHAR has an entry for
1219 ;; NON-TERMINATING in CHARACTER-ATTRIBUTE-TABLE and
1220 ;; READ-DOT-NUMBER-SYMBOL in CMT. Report an error if these are
1221 ;; violated. (If we called this, we want something that is a
1222 ;; legitimate token!) Read in the longest possible string satisfying
1223 ;; the Backus-Naur form for "unqualified-token". Leave the result in
1224 ;; the *READ-BUFFER*. Return next char after token (last char read).
1225 (when *read-suppress*
1226 (internal-read-extended-token stream firstchar nil)
1227 (return-from read-token nil))
1228 (let* ((rt *readtable*)
1230 (attribute-array (character-attribute-array rt))
1231 (attribute-hash-table (character-attribute-hash-table rt))
1233 (package-designator nil)
1235 (possibly-rational t)
1236 (seen-digit-or-expt nil)
1238 (was-possibly-float nil)
1239 (seen-multiple-escapes nil))
1240 (declare (token-buf buf))
1241 (reset-read-buffer buf)
1242 (macrolet ((getchar-or-else (what)
1243 `(when (eq (setq char (read-char stream nil +EOF+)) +EOF+)
1245 (prog ((char firstchar))
1246 (case (char-class3 char attribute-array attribute-hash-table base)
1247 (#.+char-attr-constituent-sign+ (go SIGN))
1248 (#.+char-attr-constituent-digit+ (go LEFTDIGIT))
1249 (#.+char-attr-constituent-digit-or-expt+
1250 (setq seen-digit-or-expt t)
1252 (#.+char-attr-constituent-decimal-digit+ (go LEFTDECIMALDIGIT))
1253 (#.+char-attr-constituent-dot+ (go FRONTDOT))
1254 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1255 (#.+char-attr-package-delimiter+ (go COLON))
1256 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1257 (#.+char-attr-invalid+ (simple-reader-error stream
1258 "invalid constituent"))
1259 ;; can't have eof, whitespace, or terminating macro as first char!
1262 (ouch-read-buffer char buf)
1263 (getchar-or-else (go RETURN-SYMBOL))
1264 (setq possibly-rational t
1266 (case (char-class3 char attribute-array attribute-hash-table base)
1267 (#.+char-attr-constituent-digit+ (go LEFTDIGIT))
1268 (#.+char-attr-constituent-digit-or-expt+
1269 (setq seen-digit-or-expt t)
1271 (#.+char-attr-constituent-decimal-digit+ (go LEFTDECIMALDIGIT))
1272 (#.+char-attr-constituent-dot+ (go SIGNDOT))
1273 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1274 (#.+char-attr-package-delimiter+ (go COLON))
1275 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1276 (#.+char-attr-delimiter+ (unread-char char stream) (go RETURN-SYMBOL))
1278 LEFTDIGIT ; saw "[sign] {digit}+"
1279 (ouch-read-buffer char buf)
1280 (getchar-or-else (return (make-integer)))
1281 (setq was-possibly-float possibly-float)
1282 (case (char-class3 char attribute-array attribute-hash-table base)
1283 (#.+char-attr-constituent-digit+ (go LEFTDIGIT))
1284 (#.+char-attr-constituent-decimal-digit+ (if possibly-float
1285 (go LEFTDECIMALDIGIT)
1287 (#.+char-attr-constituent-dot+ (if possibly-float
1290 (#.+char-attr-constituent-digit-or-expt+
1291 (if (or seen-digit-or-expt (not was-possibly-float))
1292 (progn (setq seen-digit-or-expt t) (go LEFTDIGIT))
1293 (progn (setq seen-digit-or-expt t) (go LEFTDIGIT-OR-EXPT))))
1294 (#.+char-attr-constituent-expt+
1295 (if was-possibly-float
1298 (#.+char-attr-constituent-slash+ (if possibly-rational
1301 (#.+char-attr-delimiter+ (unread-char char stream)
1302 (return (make-integer)))
1303 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1304 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1305 (#.+char-attr-package-delimiter+ (go COLON))
1308 (ouch-read-buffer char buf)
1309 (getchar-or-else (return (make-integer)))
1310 (case (char-class3 char attribute-array attribute-hash-table base)
1311 (#.+char-attr-constituent-digit+ (go LEFTDIGIT))
1312 (#.+char-attr-constituent-decimal-digit+ (bug "impossible!"))
1313 (#.+char-attr-constituent-dot+ (go SYMBOL))
1314 (#.+char-attr-constituent-digit-or-expt+ (go LEFTDIGIT))
1315 (#.+char-attr-constituent-expt+ (go SYMBOL))
1316 (#.+char-attr-constituent-sign+ (go EXPTSIGN))
1317 (#.+char-attr-constituent-slash+ (if possibly-rational
1320 (#.+char-attr-delimiter+ (unread-char char stream)
1321 (return (make-integer)))
1322 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1323 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1324 (#.+char-attr-package-delimiter+ (go COLON))
1326 LEFTDECIMALDIGIT ; saw "[sign] {decimal-digit}+"
1327 (aver possibly-float)
1328 (ouch-read-buffer char buf)
1329 (getchar-or-else (go RETURN-SYMBOL))
1330 (case (char-class char attribute-array attribute-hash-table)
1331 (#.+char-attr-constituent-digit+ (go LEFTDECIMALDIGIT))
1332 (#.+char-attr-constituent-dot+ (go MIDDLEDOT))
1333 (#.+char-attr-constituent-expt+ (go EXPONENT))
1334 (#.+char-attr-constituent-slash+ (aver (not possibly-rational))
1336 (#.+char-attr-delimiter+ (unread-char char stream)
1338 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1339 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1340 (#.+char-attr-package-delimiter+ (go COLON))
1342 MIDDLEDOT ; saw "[sign] {digit}+ dot"
1343 (ouch-read-buffer char buf)
1344 (getchar-or-else (return (make-integer 10)))
1345 (case (char-class char attribute-array attribute-hash-table)
1346 (#.+char-attr-constituent-digit+ (go RIGHTDIGIT))
1347 (#.+char-attr-constituent-expt+ (go EXPONENT))
1348 (#.+char-attr-delimiter+
1349 (unread-char char stream)
1350 (return (make-integer 10)))
1351 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1352 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1353 (#.+char-attr-package-delimiter+ (go COLON))
1355 RIGHTDIGIT ; saw "[sign] {decimal-digit}* dot {digit}+"
1356 (ouch-read-buffer char buf)
1357 (getchar-or-else (return (make-float stream)))
1358 (case (char-class char attribute-array attribute-hash-table)
1359 (#.+char-attr-constituent-digit+ (go RIGHTDIGIT))
1360 (#.+char-attr-constituent-expt+ (go EXPONENT))
1361 (#.+char-attr-delimiter+
1362 (unread-char char stream)
1363 (return (make-float stream)))
1364 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1365 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1366 (#.+char-attr-package-delimiter+ (go COLON))
1368 SIGNDOT ; saw "[sign] dot"
1369 (ouch-read-buffer char buf)
1370 (getchar-or-else (go RETURN-SYMBOL))
1371 (case (char-class char attribute-array attribute-hash-table)
1372 (#.+char-attr-constituent-digit+ (go RIGHTDIGIT))
1373 (#.+char-attr-delimiter+ (unread-char char stream) (go RETURN-SYMBOL))
1374 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1375 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1377 FRONTDOT ; saw "dot"
1378 (ouch-read-buffer char buf)
1379 (getchar-or-else (simple-reader-error stream "dot context error"))
1380 (case (char-class char attribute-array attribute-hash-table)
1381 (#.+char-attr-constituent-digit+ (go RIGHTDIGIT))
1382 (#.+char-attr-constituent-dot+ (go DOTS))
1383 (#.+char-attr-delimiter+ (simple-reader-error stream
1384 "dot context error"))
1385 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1386 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1387 (#.+char-attr-package-delimiter+ (go COLON))
1390 (ouch-read-buffer char buf)
1391 (getchar-or-else (go RETURN-SYMBOL))
1392 (setq possibly-float t)
1393 (case (char-class char attribute-array attribute-hash-table)
1394 (#.+char-attr-constituent-sign+ (go EXPTSIGN))
1395 (#.+char-attr-constituent-digit+ (go EXPTDIGIT))
1396 (#.+char-attr-delimiter+ (unread-char char stream) (go RETURN-SYMBOL))
1397 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1398 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1399 (#.+char-attr-package-delimiter+ (go COLON))
1401 EXPTSIGN ; got to EXPONENT, and saw a sign character
1402 (ouch-read-buffer char buf)
1403 (getchar-or-else (go RETURN-SYMBOL))
1404 (case (char-class char attribute-array attribute-hash-table)
1405 (#.+char-attr-constituent-digit+ (go EXPTDIGIT))
1406 (#.+char-attr-delimiter+ (unread-char char stream) (go RETURN-SYMBOL))
1407 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1408 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1409 (#.+char-attr-package-delimiter+ (go COLON))
1411 EXPTDIGIT ; got to EXPONENT, saw "[sign] {digit}+"
1412 (ouch-read-buffer char buf)
1413 (getchar-or-else (return (make-float stream)))
1414 (case (char-class char attribute-array attribute-hash-table)
1415 (#.+char-attr-constituent-digit+ (go EXPTDIGIT))
1416 (#.+char-attr-delimiter+
1417 (unread-char char stream)
1418 (return (make-float stream)))
1419 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1420 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1421 (#.+char-attr-package-delimiter+ (go COLON))
1423 RATIO ; saw "[sign] {digit}+ slash"
1424 (ouch-read-buffer char buf)
1425 (getchar-or-else (go RETURN-SYMBOL))
1426 (case (char-class2 char attribute-array attribute-hash-table base)
1427 (#.+char-attr-constituent-digit+ (go RATIODIGIT))
1428 (#.+char-attr-delimiter+ (unread-char char stream) (go RETURN-SYMBOL))
1429 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1430 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1431 (#.+char-attr-package-delimiter+ (go COLON))
1433 RATIODIGIT ; saw "[sign] {digit}+ slash {digit}+"
1434 (ouch-read-buffer char buf)
1435 (getchar-or-else (return (make-ratio stream)))
1436 (case (char-class2 char attribute-array attribute-hash-table base)
1437 (#.+char-attr-constituent-digit+ (go RATIODIGIT))
1438 (#.+char-attr-delimiter+
1439 (unread-char char stream)
1440 (return (make-ratio stream)))
1441 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1442 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1443 (#.+char-attr-package-delimiter+ (go COLON))
1445 DOTS ; saw "dot {dot}+"
1446 (ouch-read-buffer char buf)
1447 (getchar-or-else (simple-reader-error stream "too many dots"))
1448 (case (char-class char attribute-array attribute-hash-table)
1449 (#.+char-attr-constituent-dot+ (go DOTS))
1450 (#.+char-attr-delimiter+
1451 (unread-char char stream)
1452 (simple-reader-error stream "too many dots"))
1453 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1454 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1455 (#.+char-attr-package-delimiter+ (go COLON))
1457 SYMBOL ; not a dot, dots, or number
1458 (let ((stream (in-stream-from-designator stream)))
1460 ((scan (read-a-char &optional finish)
1463 (ouch-read-buffer char buf)
1464 (setq char ,read-a-char)
1465 (when (eq char +EOF+) (go RETURN-SYMBOL))
1466 (case (char-class char attribute-array attribute-hash-table)
1467 (#.+char-attr-single-escape+ ,finish (go SINGLE-ESCAPE))
1468 (#.+char-attr-delimiter+ ,finish
1469 (unread-char char stream)
1471 (#.+char-attr-multiple-escape+ ,finish (go MULT-ESCAPE))
1472 (#.+char-attr-package-delimiter+ ,finish (go COLON))
1473 (t (go SYMBOL-LOOP))))))
1474 (if (ansi-stream-p stream)
1475 (prepare-for-fast-read-char stream
1476 (scan (fast-read-char nil +EOF+) (done-with-fast-read-char)))
1478 (scan (read-char stream nil +EOF+)))))
1479 SINGLE-ESCAPE ; saw a single-escape
1480 ;; Don't put the escape character in the read buffer.
1481 ;; READ-NEXT CHAR, put in buffer (no case conversion).
1482 (let ((nextchar (read-char stream nil +EOF+)))
1483 (when (eq nextchar +EOF+)
1484 (reader-eof-error stream "after single-escape character"))
1485 (ouch-read-buffer-escaped nextchar buf))
1486 (getchar-or-else (go RETURN-SYMBOL))
1487 (case (char-class char attribute-array attribute-hash-table)
1488 (#.+char-attr-delimiter+ (unread-char char stream) (go RETURN-SYMBOL))
1489 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1490 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1491 (#.+char-attr-package-delimiter+ (go COLON))
1494 (setq seen-multiple-escapes t)
1495 ;; sometimes we pass eof-error=nil but check. here we just let it err.
1496 ;; should pick one style and stick with it.
1497 (do ((char (read-char stream t) (read-char stream t)))
1498 ((multiple-escape-p char rt))
1499 (if (single-escape-p char rt) (setq char (read-char stream t)))
1500 (ouch-read-buffer-escaped char buf))
1501 (getchar-or-else (go RETURN-SYMBOL))
1502 (case (char-class char attribute-array attribute-hash-table)
1503 (#.+char-attr-delimiter+ (unread-char char stream) (go RETURN-SYMBOL))
1504 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1505 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1506 (#.+char-attr-package-delimiter+ (go COLON))
1509 (unless (zerop colons)
1510 (simple-reader-error
1511 stream "too many colons in ~S" (copy-token-buf-string buf)))
1512 (setf buf (normalize-read-buffer buf))
1513 (casify-read-buffer buf)
1515 (setq package-designator
1516 (if (or (plusp (token-buf-fill-ptr buf)) seen-multiple-escapes)
1517 (prog1 (sized-token-buf-string buf)
1518 (let ((new (acquire-token-buf)))
1519 (setf (token-buf-next new) buf ; new points to old
1520 buf new *read-buffer* new)))
1522 (reset-read-buffer buf)
1523 (getchar-or-else (reader-eof-error stream "after reading a colon"))
1524 (case (char-class char attribute-array attribute-hash-table)
1525 (#.+char-attr-delimiter+
1526 (unread-char char stream)
1527 (simple-reader-error stream
1528 "illegal terminating character after a colon: ~S"
1530 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1531 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1532 (#.+char-attr-package-delimiter+ (go INTERN))
1536 (getchar-or-else (reader-eof-error stream "after reading a colon"))
1537 (case (char-class char attribute-array attribute-hash-table)
1538 (#.+char-attr-delimiter+
1539 (unread-char char stream)
1540 (if package-designator
1541 (let* ((*reader-package*
1542 (reader-find-package package-designator stream)))
1543 (return (read stream t nil t)))
1544 (simple-reader-error stream
1545 "illegal terminating character after a double-colon: ~S"
1547 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1548 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1549 (#.+char-attr-package-delimiter+
1550 (simple-reader-error stream
1551 "too many colons after ~S name"
1552 package-designator))
1555 (setf buf (normalize-read-buffer buf))
1556 (casify-read-buffer buf)
1557 (let* ((pkg (if package-designator
1558 (reader-find-package package-designator stream)
1559 (or *reader-package* (sane-package))))
1560 (intern-p (or (/= colons 1) (eq pkg *keyword-package*))))
1561 (unless intern-p ; Try %FIND-SYMBOL
1562 (multiple-value-bind (symbol accessibility)
1563 (%find-symbol (token-buf-string buf) (token-buf-fill-ptr buf) pkg)
1564 (when (eq accessibility :external) (return symbol))
1565 (with-simple-restart (continue "Use symbol anyway.")
1566 (error 'simple-reader-package-error
1570 (list (copy-token-buf-string buf) (package-name pkg))
1573 "The symbol ~S is not external in the ~A package."
1574 "Symbol ~S not found in the ~A package.")))))
1575 (return (%intern (token-buf-string buf)
1576 (token-buf-fill-ptr buf)
1578 (if (token-buf-only-base-chars buf)
1579 (%readtable-symbol-preference rt)
1582 ;;; For semi-external use: Return 3 values: the token-buf,
1583 ;;; a flag for whether there was an escape char, and the position of
1584 ;;; any package delimiter. The returned token-buf is not case-converted.
1585 (defun read-extended-token (stream)
1586 ;; recursive-p = T is basically irrelevant.
1587 (let ((first-char (read-char stream nil +EOF+ t)))
1588 (if (neq first-char +EOF+)
1589 (internal-read-extended-token stream first-char nil)
1590 (values (reset-read-buffer *read-buffer*) nil nil))))
1592 ;;; for semi-external use:
1594 ;;; Read an extended token with the first character escaped. Return
1595 ;;; the token-buf. The returned token-buf is not case-converted.
1596 (defun read-extended-token-escaped (stream)
1597 (let ((first-char (read-char stream nil +EOF+)))
1598 (if (neq first-char +EOF+)
1599 (values (internal-read-extended-token stream first-char t))
1600 (reader-eof-error stream "after escape"))))
1602 ;;;; number-reading functions
1604 ;; Mapping of read-base to the max input characters in a positive fixnum.
1605 (eval-when (:compile-toplevel :execute)
1606 (defun integer-reader-safe-digits ()
1607 (do ((a (make-array 35 :element-type '(unsigned-byte 8)))
1610 (do ((total (1- base) (+ (* total base) (1- base)))
1611 (n-digits 0 (1+ n-digits)))
1612 ((sb!xc:typep total 'bignum)
1613 (setf (aref a (- base 2)) n-digits))
1618 (do ((maxdigits (integer-reader-safe-digits))
1621 (let* ((n-digits (aref maxdigits (- base 2)))
1622 (d (char (write-to-string (1- base) :base base) 0))
1623 (string (make-string (1+ n-digits) :initial-element d))) ; 1 extra
1624 (assert (not (typep (parse-integer string :radix base)
1625 `(unsigned-byte ,sb!vm:n-positive-fixnum-bits))))
1626 (assert (typep (parse-integer string :end n-digits :radix base)
1627 `(unsigned-byte ,sb!vm:n-positive-fixnum-bits))))))
1629 (defmacro !setq-optional-leading-sign (sign-flag token-buf rewind)
1630 ;; guaranteed to have at least one character in buffer at the start
1631 ;; or immediately following [ESFDL] marker depending on 'rewind' flag.
1632 `(locally (declare (optimize (sb!c::insert-array-bounds-checks 0)))
1633 (,(if rewind 'setf 'incf)
1634 (token-buf-cursor ,token-buf)
1635 (case (elt (token-buf-string ,token-buf)
1636 ,(if rewind 0 `(token-buf-cursor ,token-buf)))
1637 (#\- (setq ,sign-flag t) 1)
1641 (defun make-integer (&optional (base *read-base*))
1642 "Minimizes bignum-fixnum multiplies by reading a 'safe' number of digits,
1643 then multiplying by a power of the base and adding."
1644 (declare ((integer 2 36) base)
1645 (inline token-buf-getchar)) ; makes for smaller code
1646 (let* ((fixnum-max-digits
1647 (macrolet ((maxdigits ()
1648 (!coerce-to-specialized (integer-reader-safe-digits)
1649 '(unsigned-byte 8))))
1650 (aref (maxdigits) (- base 2))))
1652 (macrolet ((base-powers ()
1653 (do ((maxdigits (integer-reader-safe-digits))
1657 (setf (aref a (- base 2))
1658 (expt base (aref maxdigits (- base 2)))))))
1659 (truly-the integer (aref (base-powers) (- base 2)))))
1662 (buf *read-buffer*))
1663 (!setq-optional-leading-sign negativep buf t)
1666 (declare (type (and fixnum unsigned-byte) acc))
1667 (dotimes (digit-count fixnum-max-digits)
1668 (let ((ch (token-buf-getchar buf)))
1669 (if (or (not ch) (eql ch #\.))
1670 (return-from make-integer
1672 (if (zerop result) acc
1673 (+ (* result (expt base digit-count)) acc))))
1674 (if negativep (- result) result)))
1675 (setq acc (truly-the fixnum
1676 (+ (digit-char-p ch base)
1677 (truly-the fixnum (* acc base))))))))
1678 (setq result (+ (* result base-power) acc))))))
1680 (defun truncate-exponent (exponent number divisor)
1681 "Truncate exponent if it's too large for a float"
1682 ;; Work with base-2 logarithms to avoid conversions to floats,
1683 ;; and convert to base-10 conservatively at the end.
1684 ;; Use the least positive float, because denormalized exponent
1685 ;; can be larger than normalized.
1686 (let* ((max-exponent
1688 (+ sb!vm:double-float-digits sb!vm:double-float-bias))
1689 (number-magnitude (integer-length number))
1690 (divisor-magnitude (1- (integer-length divisor)))
1691 (magnitude (- number-magnitude divisor-magnitude)))
1692 (if (minusp exponent)
1693 (max exponent (ceiling (- (+ max-exponent magnitude))
1694 #.(floor (log 10 2))))
1695 (min exponent (floor (- max-exponent magnitude)
1696 #.(floor (log 10 2)))))))
1698 (defun make-float (stream)
1699 ;; Assume that the contents of *read-buffer* are a legal float, with nothing
1701 (let ((buf *read-buffer*)
1702 (negative-fraction nil)
1705 (negative-exponent nil)
1709 (!setq-optional-leading-sign negative-fraction buf t)
1710 ;; Read digits before the dot.
1711 (macrolet ((accumulate (expr)
1713 (loop (if (and (setq char (token-buf-getchar buf))
1714 (setq digit (digit-char-p char)))
1717 (accumulate (setq number (+ (* number 10) digit)))
1718 ;; Deal with the dot, if it's there.
1719 (when (char= char #\.)
1720 ;; Read digits after the dot.
1721 (accumulate (setq divisor (* divisor 10)
1722 number (+ (* number 10) digit))))
1723 ;; Is there an exponent letter?
1726 ;; If not, we've read the whole number.
1727 (let ((num (make-float-aux number divisor
1728 *read-default-float-format*
1730 (return-from make-float (if negative-fraction (- num) num))))
1731 ((= (get-constituent-trait char) +char-attr-constituent-expt+)
1732 (setq float-char char)
1733 ;; Check leading sign.
1734 (!setq-optional-leading-sign negative-exponent buf nil)
1735 ;; Read digits for exponent.
1736 (accumulate (setq exponent (+ (* exponent 10) digit)))
1737 (setq exponent (if negative-exponent (- exponent) exponent))
1738 ;; Generate and return the float, depending on FLOAT-CHAR:
1739 (let* ((float-format (case (char-upcase float-char)
1740 (#\E *read-default-float-format*)
1745 (exponent (truncate-exponent exponent number divisor))
1746 (result (make-float-aux (* (expt 10 exponent) number)
1747 divisor float-format stream)))
1748 (return-from make-float
1749 (if negative-fraction (- result) result))))
1750 (t (bug "bad fallthrough in floating point reader"))))))
1752 (defun make-float-aux (number divisor float-format stream)
1754 (coerce (/ number divisor) float-format)
1756 (error 'reader-impossible-number-error
1757 :error c :stream stream
1758 :format-control "failed to build float from ~a"
1759 :format-arguments (list (copy-token-buf-string *read-buffer*))))))
1761 (defun make-ratio (stream)
1762 ;; Assume *READ-BUFFER* contains a legal ratio. Build the number from
1764 ;; This code is inferior to that of MAKE-INTEGER because it makes no
1765 ;; attempt to perform as few bignum multiplies as possible.
1767 (let ((numerator 0) (denominator 0) (negativep nil)
1768 (base *read-base*) (buf *read-buffer*))
1769 (!setq-optional-leading-sign negativep buf t)
1771 (loop (let ((dig (digit-char-p (token-buf-getchar buf) base)))
1773 (setq numerator (+ (* numerator base) dig))
1776 (do* ((ch (token-buf-getchar buf) (token-buf-getchar buf))
1778 ((or (null ch) (not (setq dig (digit-char-p ch base)))))
1779 (setq denominator (+ (* denominator base) dig)))
1780 (let ((num (handler-case
1781 (/ numerator denominator)
1782 (arithmetic-error (c)
1783 (error 'reader-impossible-number-error
1784 :error c :stream stream
1785 :format-control "failed to build ratio")))))
1786 (if negativep (- num) num))))
1788 ;;;; General reader for dispatch macros
1790 (defun dispatch-char-error (stream sub-char ignore)
1791 (declare (optimize allow-non-returning-tail-call))
1792 (declare (ignore ignore))
1795 (simple-reader-error stream
1796 "no dispatch function defined for ~S"
1799 (defun read-dispatch-char (stream dispatch-table)
1800 ;; Read some digits.
1805 (let ((ch (read-char stream nil +EOF+)))
1807 (reader-eof-error stream "inside dispatch character")
1808 ;; Take care of the extra char.
1809 (let ((dig (digit-char-p ch)))
1811 (setq numargp t numarg (+ (* numarg 10) dig))
1812 (return (setq sub-char (char-upcase ch))))))))
1813 ;; Look up the function and call it.
1814 (let ((fn (get-raw-cmt-dispatch-entry sub-char dispatch-table)))
1815 (funcall (!cmt-entry-to-function fn #'dispatch-char-error)
1816 stream sub-char (if numargp numarg nil)))))
1818 ;;;; READ-FROM-STRING
1820 (declaim (ftype (sfunction (string t t index (or null index) t) (values t index))
1822 (defun %read-from-string (string eof-error-p eof-value start end preserve-whitespace)
1823 (with-array-data ((string string :offset-var offset)
1826 :check-fill-pointer t)
1827 (let ((stream (make-string-input-stream string start end)))
1828 (values (if preserve-whitespace
1829 (%read-preserving-whitespace stream eof-error-p eof-value nil)
1830 (read stream eof-error-p eof-value))
1831 (- (string-input-stream-current stream) offset)))))
1834 (declare (muffle-conditions style-warning))
1835 (defun read-from-string (string &optional (eof-error-p t) eof-value
1836 &key (start 0) end preserve-whitespace)
1837 "The characters of string are successively given to the lisp reader
1838 and the lisp object built by the reader is returned. Macro chars
1840 (declare (string string))
1841 (maybe-note-read-from-string-signature-issue eof-error-p)
1842 (%read-from-string string eof-error-p eof-value start end preserve-whitespace)))
1846 (defun parse-integer (string &key (start 0) end (radix 10) junk-allowed)
1847 "Examine the substring of string delimited by start and end
1848 (default to the beginning and end of the string) It skips over
1849 whitespace characters and then tries to parse an integer. The
1850 radix parameter must be between 2 and 36."
1851 (flet ((parse-error (format-control)
1852 (declare (optimize allow-non-returning-tail-call))
1853 (error 'simple-parse-error
1854 :format-control format-control
1855 :format-arguments (list string))))
1856 (with-array-data ((string string :offset-var offset)
1859 :check-fill-pointer t)
1860 (let ((index (do ((i start (1+ i)))
1863 (return-from parse-integer (values nil end))
1864 (parse-error "no non-whitespace characters in string ~S.")))
1865 (declare (fixnum i))
1866 (unless (whitespace[1]p
(char string i
)) (return i
))))
1870 (declare (fixnum index
))
1871 (let ((char (char string index
)))
1872 (cond ((char= char
#\-
)
1878 (when (= index end
) (return nil
))
1879 (let* ((char (char string index
))
1880 (weight (digit-char-p char radix
)))
1882 (setq result
(+ weight
(* result radix
))
1884 (junk-allowed (return nil
))
1885 ((whitespace[1]p char)
1888 (when (= index end) (return))
1889 (unless (whitespace[1]p
(char string index
))
1890 (parse-error "junk in string ~S")))
1893 (parse-error "junk in string ~S"))))
1897 (if minusp
(- result
) result
)
1900 (parse-error "no digits in string ~S")))
1901 (- index offset
))))))
1903 ;;;; reader initialization code
1905 (defun !reader-cold-init
()
1906 (!cold-init-constituent-trait-table
)
1907 (!cold-init-standard-readtable
))
1909 (defmethod print-object ((readtable readtable
) stream
)
1910 (print-unreadable-object (readtable stream
:identity t
:type t
)))
1912 ;; Backward-compatibility adapter. The "named-readtables" system in
1913 ;; Quicklisp expects this interface, and it's a reasonable thing to support.
1914 ;; What is silly however is that DISPATCH-TABLES was an alist each of whose
1915 ;; values was a hashtable which got immediately coerced to an alist.
1916 ;; In anticipation of perhaps not doing an extra re-shaping, if HASH-TABLE-P
1917 ;; is NIL then return nested alists: ((#\# (#\R . #<FUNCTION SHARP-R>) ...))
1918 (defun dispatch-tables (readtable &optional
(hash-table-p t
))
1920 (flet ((process (char fn
&aux
(dtable (%dispatch-macro-char-table fn
)))
1922 (let ((output (awhen (car dtable
) (%hash-table-alist it
))))
1923 (loop for fn across
(the simple-vector
(cdr dtable
))
1925 when fn do
(push (cons (code-char ch
) fn
) output
))
1926 (dolist (cell output
) ; coerce values to function-designator
1927 (rplacd cell
(!cmt-entry-to-fun-designator
(cdr cell
))))
1928 (when hash-table-p
; caller wants hash-tables
1929 (setq output
(%stuff-hash-table
(make-hash-table) output
)))
1930 (push (cons char output
) alist
)))))
1931 (loop for fn across
(character-macro-array readtable
) and ch from
0
1932 do
(process (code-char ch
) fn
))
1933 (maphash #'process
(character-macro-hash-table readtable
)))
1936 ;; Stub - should never get called with anything but NIL
1937 ;; and only after all macros have been changed to constituents already.
1938 (defun (setf dispatch-tables
) (new-alist readtable
)
1939 (declare (ignore readtable
))
1940 (unless (null new-alist
)
1941 (error "Assignment to virtual DISPATCH-TABLES slot not allowed"))
1944 ;;; like LISTEN, but any whitespace in the input stream will be flushed
1945 (defun listen-skip-whitespace (&optional
(stream *standard-input
*))
1946 (do ((char (read-char-no-hang stream nil nil nil
)
1947 (read-char-no-hang stream nil nil nil
)))
1949 (cond ((not (whitespace[1]p char))
1950 (unread-char char stream)