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