Don't ad-hoc reimplement DEFCONSTANT-EQX for LAMBDA-LIST-KEYWORDS.
[sbcl.git] / src / code / reader.lisp
blob2cea6d3e8425657a7e85b0231e990552d5efaff3
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 ;;; an alist for #=, used to keep track of objects with labels assigned that
662 ;;; have been completely read. Each entry is (integer-tag gensym-tag value).
664 ;;; KLUDGE: Should this really be an alist? 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 ;;; as an alist, so maybe we should. -- WHN 19991202
669 (defvar *sharp-equal-alist* ())
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-alist* 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 ;; For a very long string, this could end up bloating the read buffer.
897 (declare (character closech))
898 (let ((stream (in-synonym-of stream))
899 (buf *read-buffer*)
900 (rt *readtable*)
901 ;; *read-suppress* => "... macros will not construct any new objects"
902 (suppress *read-suppress*))
903 (reset-read-buffer buf)
904 (macrolet ((scan (read-a-char eofp &optional finish)
905 `(loop (let ((char ,read-a-char))
906 (cond (,eofp (error 'end-of-file :stream stream))
907 ((eql char closech)
908 (return ,finish))
909 ((single-escape-p char rt)
910 (setq char ,read-a-char)
911 (when ,eofp
912 (error 'end-of-file :stream stream))))
913 (unless suppress
914 (ouch-read-buffer (truly-the character char)
915 buf))))))
916 (if (ansi-stream-p stream)
917 (prepare-for-fast-read-char stream
918 (scan (fast-read-char t) nil (done-with-fast-read-char)))
919 ;; CLOS stream
920 (scan (read-char stream nil +EOF+) (eq char +EOF+))))
921 (if suppress "" (copy-token-buf-string buf))))
923 (defun read-right-paren (stream ignore)
924 (declare (ignore ignore))
925 (simple-reader-error stream "unmatched close parenthesis"))
927 ;;; Read from the stream up to the next delimiter. Leave the resulting
928 ;;; token in *READ-BUFFER*, and return three values:
929 ;;; -- a TOKEN-BUF
930 ;;; -- whether any escape character was seen (even if no character is escaped)
931 ;;; -- whether a package delimiter character was seen
932 ;;; Normalizes the input to NFKC before returning
933 (defun internal-read-extended-token (stream firstchar escape-firstchar
934 &aux (read-buffer *read-buffer*))
935 (reset-read-buffer read-buffer)
936 (when escape-firstchar
937 (ouch-read-buffer-escaped firstchar read-buffer)
938 (setq firstchar (read-char stream nil +EOF+)))
939 (do ((char firstchar (read-char stream nil +EOF+))
940 (seen-multiple-escapes nil)
941 (rt *readtable*)
942 (colon nil))
943 ((cond ((eq char +EOF+) t)
944 ((token-delimiterp char rt)
945 (unread-char char stream)
947 (t nil))
948 (progn
949 (multiple-value-setq (read-buffer colon)
950 (normalize-read-buffer read-buffer colon))
951 (values read-buffer
952 (or (plusp (fill-pointer (token-buf-escapes read-buffer)))
953 seen-multiple-escapes)
954 colon)))
955 (cond ((single-escape-p char rt)
956 ;; It can't be a number, even if it's 1\23.
957 ;; Read next char here, so it won't be casified.
958 (let ((nextchar (read-char stream nil +EOF+)))
959 (if (eq nextchar +EOF+)
960 (reader-eof-error stream "after escape character")
961 (ouch-read-buffer-escaped nextchar read-buffer))))
962 ((multiple-escape-p char rt)
963 (setq seen-multiple-escapes t)
964 ;; Read to next multiple-escape, escaping single chars
965 ;; along the way.
966 (loop
967 (let ((ch (read-char stream nil +EOF+)))
968 (cond
969 ((eq ch +EOF+)
970 (reader-eof-error stream "inside extended token"))
971 ((multiple-escape-p ch rt) (return))
972 ((single-escape-p ch rt)
973 (let ((nextchar (read-char stream nil +EOF+)))
974 (if (eq nextchar +EOF+)
975 (reader-eof-error stream "after escape character")
976 (ouch-read-buffer-escaped nextchar read-buffer))))
978 (ouch-read-buffer-escaped ch read-buffer))))))
980 (when (and (not colon) ; easiest test first
981 (constituentp char rt)
982 (eql (get-constituent-trait char)
983 +char-attr-package-delimiter+))
984 (setq colon t))
985 (ouch-read-buffer char read-buffer)))))
987 ;;;; character classes
989 ;;; Return the character class for CHAR.
991 ;;; FIXME: why aren't these ATT-getting forms using GET-CAT-ENTRY?
992 ;;; Because we've cached the readtable tables?
993 (defmacro char-class (char attarray atthash)
994 `(let ((att (if (typep (truly-the character ,char) 'base-char)
995 (aref ,attarray (char-code ,char))
996 (gethash ,char ,atthash +char-attr-constituent+))))
997 (declare (fixnum att))
998 (cond
999 ((<= att +char-attr-terminating-macro+) +char-attr-delimiter+)
1000 ((< att +char-attr-constituent+) att)
1001 (t (setf att (get-constituent-trait ,char))
1002 (if (= att +char-attr-invalid+)
1003 (simple-reader-error stream "invalid constituent")
1004 att)))))
1006 ;;; Return the character class for CHAR, which might be part of a
1007 ;;; rational number.
1008 (defmacro char-class2 (char attarray atthash)
1009 `(let ((att (if (typep (truly-the character ,char) 'base-char)
1010 (aref ,attarray (char-code ,char))
1011 (gethash ,char ,atthash +char-attr-constituent+))))
1012 (declare (fixnum att))
1013 (cond
1014 ((<= att +char-attr-terminating-macro+) +char-attr-delimiter+)
1015 ((< att +char-attr-constituent+) att)
1016 (t (setf att (get-constituent-trait ,char))
1017 (cond
1018 ((digit-char-p ,char *read-base*) +char-attr-constituent-digit+)
1019 ((= att +char-attr-constituent-digit+) +char-attr-constituent+)
1020 ((= att +char-attr-invalid+)
1021 (simple-reader-error stream "invalid constituent"))
1022 (t att))))))
1024 ;;; Return the character class for a char which might be part of a
1025 ;;; rational or floating number. (Assume that it is a digit if it
1026 ;;; could be.)
1027 (defmacro char-class3 (char attarray atthash)
1028 `(let ((att (if (typep (truly-the character ,char) 'base-char)
1029 (aref ,attarray (char-code ,char))
1030 (gethash ,char ,atthash +char-attr-constituent+))))
1031 (declare (fixnum att))
1032 (cond
1033 ((<= att +char-attr-terminating-macro+) +char-attr-delimiter+)
1034 ((< att +char-attr-constituent+) att)
1035 (t (setf att (get-constituent-trait ,char))
1036 (when possibly-rational
1037 (setq possibly-rational
1038 (or (digit-char-p ,char *read-base*)
1039 (= att +char-attr-constituent-slash+))))
1040 (when possibly-float
1041 (setq possibly-float
1042 (or (digit-char-p ,char 10)
1043 (= att +char-attr-constituent-dot+))))
1044 (cond
1045 ((digit-char-p ,char (max *read-base* 10))
1046 (if (digit-char-p ,char *read-base*)
1047 (if (= att +char-attr-constituent-expt+)
1048 +char-attr-constituent-digit-or-expt+
1049 +char-attr-constituent-digit+)
1050 +char-attr-constituent-decimal-digit+))
1051 ((= att +char-attr-invalid+)
1052 (simple-reader-error stream "invalid constituent"))
1053 (t att))))))
1055 ;;;; token fetching
1057 (defvar *read-suppress* nil
1058 #!+sb-doc
1059 "Suppress most interpreting in the reader when T.")
1061 (defvar *read-base* 10
1062 #!+sb-doc
1063 "the radix that Lisp reads numbers in")
1064 (declaim (type (integer 2 36) *read-base*))
1066 ;;; Normalize TOKEN-BUF to NFKC, returning a new TOKEN-BUF and the
1067 ;;; COLON value
1068 (defun normalize-read-buffer (token-buf &optional colon)
1069 (unless (readtable-normalization *readtable*)
1070 (return-from normalize-read-buffer (values token-buf colon)))
1071 (when (token-buf-only-base-chars token-buf)
1072 (return-from normalize-read-buffer (values token-buf colon)))
1073 (let ((current-buffer (copy-token-buf-string token-buf))
1074 (old-escapes (copy-seq (token-buf-escapes token-buf)))
1075 (str-to-normalize (make-string (token-buf-fill-ptr token-buf)))
1076 (normalize-ptr 0) (escapes-ptr 0))
1077 (reset-read-buffer token-buf)
1078 (macrolet ((clear-str-to-normalize ()
1079 `(progn
1080 (loop for char across (sb!unicode:normalize-string
1081 (subseq str-to-normalize 0 normalize-ptr)
1082 :nfkc) do
1083 (ouch-read-buffer char token-buf))
1084 (setf normalize-ptr 0)))
1085 (push-to-normalize (ch)
1086 (let ((ch-gen (gensym)))
1087 `(let ((,ch-gen ,ch))
1088 (setf (char str-to-normalize normalize-ptr) ,ch-gen)
1089 (incf normalize-ptr)))))
1090 (loop for c across current-buffer
1091 for i from 0
1093 (if (and (< escapes-ptr (length old-escapes))
1094 (eql i (aref old-escapes escapes-ptr)))
1095 (progn
1096 (clear-str-to-normalize)
1097 (ouch-read-buffer-escaped c token-buf)
1098 (incf escapes-ptr))
1099 (push-to-normalize c)))
1100 (clear-str-to-normalize)
1101 (values token-buf colon))))
1103 ;;; Modify the read buffer according to READTABLE-CASE, ignoring
1104 ;;; ESCAPES. ESCAPES is a vector of the escaped indices.
1105 (defun casify-read-buffer (token-buf)
1106 (let ((case (readtable-case *readtable*))
1107 (escapes (token-buf-escapes token-buf)))
1108 (cond
1109 ((and (zerop (length escapes)) (eq case :upcase))
1110 (let ((buffer (token-buf-string token-buf)))
1111 (dotimes (i (token-buf-fill-ptr token-buf))
1112 (declare (optimize (sb!c::insert-array-bounds-checks 0)))
1113 (setf (schar buffer i) (char-upcase (schar buffer i))))))
1114 ((eq case :preserve))
1116 (macrolet ((skip-esc (&body body)
1117 `(do ((i (1- (token-buf-fill-ptr token-buf)) (1- i))
1118 (buffer (token-buf-string token-buf))
1119 (esc (if (zerop (fill-pointer escapes))
1120 -1 (vector-pop escapes))))
1121 ((minusp i))
1122 (declare (fixnum i)
1123 (optimize (sb!c::insert-array-bounds-checks 0)))
1124 (if (< esc i)
1125 (let ((ch (schar buffer i)))
1126 ,@body)
1127 (progn
1128 (aver (= esc i))
1129 (setq esc (if (zerop (fill-pointer escapes))
1130 -1 (vector-pop escapes))))))))
1131 (flet ((lower-em ()
1132 (skip-esc (setf (schar buffer i) (char-downcase ch))))
1133 (raise-em ()
1134 (skip-esc (setf (schar buffer i) (char-upcase ch)))))
1135 (ecase case
1136 (:upcase (raise-em))
1137 (:downcase (lower-em))
1138 (:invert
1139 (let ((all-upper t)
1140 (all-lower t)
1141 (fillptr (fill-pointer escapes)))
1142 (skip-esc
1143 (when (both-case-p ch)
1144 (if (upper-case-p ch)
1145 (setq all-lower nil)
1146 (setq all-upper nil))))
1147 (setf (fill-pointer escapes) fillptr)
1148 (cond (all-lower (raise-em))
1149 (all-upper (lower-em))))))))))))
1151 (eval-when (:compile-toplevel :load-toplevel :execute)
1152 (defvar *reader-package* nil))
1153 (declaim (type (or null package) *reader-package*)
1154 (always-bound *reader-package*))
1156 (defun reader-find-package (package-designator stream)
1157 (if (%instancep package-designator)
1158 package-designator
1159 (let ((package (find-package package-designator)))
1160 (cond (package
1161 ;; Release the token-buf that was used for the designator
1162 (release-token-buf (shiftf (token-buf-next *read-buffer*) nil))
1163 package)
1165 (error 'simple-reader-package-error
1166 :package package-designator
1167 :stream stream
1168 :format-control "Package ~A does not exist."
1169 :format-arguments (list package-designator)))))))
1171 (defun read-token (stream firstchar)
1172 #!+sb-doc
1173 "Default readmacro function. Handles numbers, symbols, and SBCL's
1174 extended <package-name>::<form-in-package> syntax."
1175 ;; Check explicitly whether FIRSTCHAR has an entry for
1176 ;; NON-TERMINATING in CHARACTER-ATTRIBUTE-TABLE and
1177 ;; READ-DOT-NUMBER-SYMBOL in CMT. Report an error if these are
1178 ;; violated. (If we called this, we want something that is a
1179 ;; legitimate token!) Read in the longest possible string satisfying
1180 ;; the Backus-Naur form for "unqualified-token". Leave the result in
1181 ;; the *READ-BUFFER*. Return next char after token (last char read).
1182 (when *read-suppress*
1183 (internal-read-extended-token stream firstchar nil)
1184 (return-from read-token nil))
1185 (let* ((rt *readtable*)
1186 (attribute-array (character-attribute-array rt))
1187 (attribute-hash-table (character-attribute-hash-table rt))
1188 (buf *read-buffer*)
1189 (package-designator nil)
1190 (colons 0)
1191 (possibly-rational t)
1192 (seen-digit-or-expt nil)
1193 (possibly-float t)
1194 (was-possibly-float nil)
1195 (seen-multiple-escapes nil))
1196 (declare (token-buf buf))
1197 (reset-read-buffer buf)
1198 (macrolet ((getchar-or-else (what)
1199 `(when (eq (setq char (read-char stream nil +EOF+)) +EOF+)
1200 ,what)))
1201 (prog ((char firstchar))
1202 (case (char-class3 char attribute-array attribute-hash-table)
1203 (#.+char-attr-constituent-sign+ (go SIGN))
1204 (#.+char-attr-constituent-digit+ (go LEFTDIGIT))
1205 (#.+char-attr-constituent-digit-or-expt+
1206 (setq seen-digit-or-expt t)
1207 (go LEFTDIGIT))
1208 (#.+char-attr-constituent-decimal-digit+ (go LEFTDECIMALDIGIT))
1209 (#.+char-attr-constituent-dot+ (go FRONTDOT))
1210 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1211 (#.+char-attr-package-delimiter+ (go COLON))
1212 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1213 (#.+char-attr-invalid+ (simple-reader-error stream
1214 "invalid constituent"))
1215 ;; can't have eof, whitespace, or terminating macro as first char!
1216 (t (go SYMBOL)))
1217 SIGN ; saw "sign"
1218 (ouch-read-buffer char buf)
1219 (getchar-or-else (go RETURN-SYMBOL))
1220 (setq possibly-rational t
1221 possibly-float t)
1222 (case (char-class3 char attribute-array attribute-hash-table)
1223 (#.+char-attr-constituent-digit+ (go LEFTDIGIT))
1224 (#.+char-attr-constituent-digit-or-expt+
1225 (setq seen-digit-or-expt t)
1226 (go LEFTDIGIT))
1227 (#.+char-attr-constituent-decimal-digit+ (go LEFTDECIMALDIGIT))
1228 (#.+char-attr-constituent-dot+ (go SIGNDOT))
1229 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1230 (#.+char-attr-package-delimiter+ (go COLON))
1231 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1232 (#.+char-attr-delimiter+ (unread-char char stream) (go RETURN-SYMBOL))
1233 (t (go SYMBOL)))
1234 LEFTDIGIT ; saw "[sign] {digit}+"
1235 (ouch-read-buffer char buf)
1236 (getchar-or-else (return (make-integer)))
1237 (setq was-possibly-float possibly-float)
1238 (case (char-class3 char attribute-array attribute-hash-table)
1239 (#.+char-attr-constituent-digit+ (go LEFTDIGIT))
1240 (#.+char-attr-constituent-decimal-digit+ (if possibly-float
1241 (go LEFTDECIMALDIGIT)
1242 (go SYMBOL)))
1243 (#.+char-attr-constituent-dot+ (if possibly-float
1244 (go MIDDLEDOT)
1245 (go SYMBOL)))
1246 (#.+char-attr-constituent-digit-or-expt+
1247 (if (or seen-digit-or-expt (not was-possibly-float))
1248 (progn (setq seen-digit-or-expt t) (go LEFTDIGIT))
1249 (progn (setq seen-digit-or-expt t) (go LEFTDIGIT-OR-EXPT))))
1250 (#.+char-attr-constituent-expt+
1251 (if was-possibly-float
1252 (go EXPONENT)
1253 (go SYMBOL)))
1254 (#.+char-attr-constituent-slash+ (if possibly-rational
1255 (go RATIO)
1256 (go SYMBOL)))
1257 (#.+char-attr-delimiter+ (unread-char char stream)
1258 (return (make-integer)))
1259 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1260 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1261 (#.+char-attr-package-delimiter+ (go COLON))
1262 (t (go SYMBOL)))
1263 LEFTDIGIT-OR-EXPT
1264 (ouch-read-buffer char buf)
1265 (getchar-or-else (return (make-integer)))
1266 (case (char-class3 char attribute-array attribute-hash-table)
1267 (#.+char-attr-constituent-digit+ (go LEFTDIGIT))
1268 (#.+char-attr-constituent-decimal-digit+ (bug "impossible!"))
1269 (#.+char-attr-constituent-dot+ (go SYMBOL))
1270 (#.+char-attr-constituent-digit-or-expt+ (go LEFTDIGIT))
1271 (#.+char-attr-constituent-expt+ (go SYMBOL))
1272 (#.+char-attr-constituent-sign+ (go EXPTSIGN))
1273 (#.+char-attr-constituent-slash+ (if possibly-rational
1274 (go RATIO)
1275 (go SYMBOL)))
1276 (#.+char-attr-delimiter+ (unread-char char stream)
1277 (return (make-integer)))
1278 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1279 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1280 (#.+char-attr-package-delimiter+ (go COLON))
1281 (t (go SYMBOL)))
1282 LEFTDECIMALDIGIT ; saw "[sign] {decimal-digit}+"
1283 (aver possibly-float)
1284 (ouch-read-buffer char buf)
1285 (getchar-or-else (go RETURN-SYMBOL))
1286 (case (char-class char attribute-array attribute-hash-table)
1287 (#.+char-attr-constituent-digit+ (go LEFTDECIMALDIGIT))
1288 (#.+char-attr-constituent-dot+ (go MIDDLEDOT))
1289 (#.+char-attr-constituent-expt+ (go EXPONENT))
1290 (#.+char-attr-constituent-slash+ (aver (not possibly-rational))
1291 (go SYMBOL))
1292 (#.+char-attr-delimiter+ (unread-char char stream)
1293 (go RETURN-SYMBOL))
1294 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1295 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1296 (#.+char-attr-package-delimiter+ (go COLON))
1297 (t (go SYMBOL)))
1298 MIDDLEDOT ; saw "[sign] {digit}+ dot"
1299 (ouch-read-buffer char buf)
1300 (getchar-or-else (return (make-integer 10)))
1301 (case (char-class char attribute-array attribute-hash-table)
1302 (#.+char-attr-constituent-digit+ (go RIGHTDIGIT))
1303 (#.+char-attr-constituent-expt+ (go EXPONENT))
1304 (#.+char-attr-delimiter+
1305 (unread-char char stream)
1306 (return (make-integer 10)))
1307 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1308 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1309 (#.+char-attr-package-delimiter+ (go COLON))
1310 (t (go SYMBOL)))
1311 RIGHTDIGIT ; saw "[sign] {decimal-digit}* dot {digit}+"
1312 (ouch-read-buffer char buf)
1313 (getchar-or-else (return (make-float stream)))
1314 (case (char-class char attribute-array attribute-hash-table)
1315 (#.+char-attr-constituent-digit+ (go RIGHTDIGIT))
1316 (#.+char-attr-constituent-expt+ (go EXPONENT))
1317 (#.+char-attr-delimiter+
1318 (unread-char char stream)
1319 (return (make-float stream)))
1320 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1321 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1322 (#.+char-attr-package-delimiter+ (go COLON))
1323 (t (go SYMBOL)))
1324 SIGNDOT ; saw "[sign] dot"
1325 (ouch-read-buffer char buf)
1326 (getchar-or-else (go RETURN-SYMBOL))
1327 (case (char-class char attribute-array attribute-hash-table)
1328 (#.+char-attr-constituent-digit+ (go RIGHTDIGIT))
1329 (#.+char-attr-delimiter+ (unread-char char stream) (go RETURN-SYMBOL))
1330 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1331 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1332 (t (go SYMBOL)))
1333 FRONTDOT ; saw "dot"
1334 (ouch-read-buffer char buf)
1335 (getchar-or-else (simple-reader-error stream "dot context error"))
1336 (case (char-class char attribute-array attribute-hash-table)
1337 (#.+char-attr-constituent-digit+ (go RIGHTDIGIT))
1338 (#.+char-attr-constituent-dot+ (go DOTS))
1339 (#.+char-attr-delimiter+ (simple-reader-error stream
1340 "dot context error"))
1341 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1342 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1343 (#.+char-attr-package-delimiter+ (go COLON))
1344 (t (go SYMBOL)))
1345 EXPONENT
1346 (ouch-read-buffer char buf)
1347 (getchar-or-else (go RETURN-SYMBOL))
1348 (setq possibly-float t)
1349 (case (char-class char attribute-array attribute-hash-table)
1350 (#.+char-attr-constituent-sign+ (go EXPTSIGN))
1351 (#.+char-attr-constituent-digit+ (go EXPTDIGIT))
1352 (#.+char-attr-delimiter+ (unread-char char stream) (go RETURN-SYMBOL))
1353 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1354 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1355 (#.+char-attr-package-delimiter+ (go COLON))
1356 (t (go SYMBOL)))
1357 EXPTSIGN ; got to EXPONENT, and saw a sign character
1358 (ouch-read-buffer char buf)
1359 (getchar-or-else (go RETURN-SYMBOL))
1360 (case (char-class char attribute-array attribute-hash-table)
1361 (#.+char-attr-constituent-digit+ (go EXPTDIGIT))
1362 (#.+char-attr-delimiter+ (unread-char char stream) (go RETURN-SYMBOL))
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 EXPTDIGIT ; got to EXPONENT, saw "[sign] {digit}+"
1368 (ouch-read-buffer char buf)
1369 (getchar-or-else (return (make-float stream)))
1370 (case (char-class char attribute-array attribute-hash-table)
1371 (#.+char-attr-constituent-digit+ (go EXPTDIGIT))
1372 (#.+char-attr-delimiter+
1373 (unread-char char stream)
1374 (return (make-float stream)))
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 RATIO ; saw "[sign] {digit}+ slash"
1380 (ouch-read-buffer char buf)
1381 (getchar-or-else (go RETURN-SYMBOL))
1382 (case (char-class2 char attribute-array attribute-hash-table)
1383 (#.+char-attr-constituent-digit+ (go RATIODIGIT))
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 RATIODIGIT ; saw "[sign] {digit}+ slash {digit}+"
1390 (ouch-read-buffer char buf)
1391 (getchar-or-else (return (make-ratio stream)))
1392 (case (char-class2 char attribute-array attribute-hash-table)
1393 (#.+char-attr-constituent-digit+ (go RATIODIGIT))
1394 (#.+char-attr-delimiter+
1395 (unread-char char stream)
1396 (return (make-ratio 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 DOTS ; saw "dot {dot}+"
1402 (ouch-read-buffer char buf)
1403 (getchar-or-else (simple-reader-error stream "too many dots"))
1404 (case (char-class char attribute-array attribute-hash-table)
1405 (#.+char-attr-constituent-dot+ (go DOTS))
1406 (#.+char-attr-delimiter+
1407 (unread-char char stream)
1408 (simple-reader-error stream "too many dots"))
1409 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1410 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1411 (#.+char-attr-package-delimiter+ (go COLON))
1412 (t (go SYMBOL)))
1413 SYMBOL ; not a dot, dots, or number
1414 (let ((stream (in-synonym-of stream)))
1415 (macrolet
1416 ((scan (read-a-char &optional finish)
1417 `(prog ()
1418 SYMBOL-LOOP
1419 (ouch-read-buffer char buf)
1420 (setq char ,read-a-char)
1421 (when (eq char +EOF+) (go RETURN-SYMBOL))
1422 (case (char-class char attribute-array attribute-hash-table)
1423 (#.+char-attr-single-escape+ ,finish (go SINGLE-ESCAPE))
1424 (#.+char-attr-delimiter+ ,finish
1425 (unread-char char stream)
1426 (go RETURN-SYMBOL))
1427 (#.+char-attr-multiple-escape+ ,finish (go MULT-ESCAPE))
1428 (#.+char-attr-package-delimiter+ ,finish (go COLON))
1429 (t (go SYMBOL-LOOP))))))
1430 (if (ansi-stream-p stream)
1431 (prepare-for-fast-read-char stream
1432 (scan (fast-read-char nil +EOF+) (done-with-fast-read-char)))
1433 ;; CLOS stream
1434 (scan (read-char stream nil +EOF+)))))
1435 SINGLE-ESCAPE ; saw a single-escape
1436 ;; Don't put the escape character in the read buffer.
1437 ;; READ-NEXT CHAR, put in buffer (no case conversion).
1438 (let ((nextchar (read-char stream nil +EOF+)))
1439 (when (eq nextchar +EOF+)
1440 (reader-eof-error stream "after single-escape character"))
1441 (ouch-read-buffer-escaped nextchar buf))
1442 (getchar-or-else (go RETURN-SYMBOL))
1443 (case (char-class char attribute-array attribute-hash-table)
1444 (#.+char-attr-delimiter+ (unread-char char stream) (go RETURN-SYMBOL))
1445 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1446 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1447 (#.+char-attr-package-delimiter+ (go COLON))
1448 (t (go SYMBOL)))
1449 MULT-ESCAPE
1450 (setq seen-multiple-escapes t)
1451 ;; sometimes we pass eof-error=nil but check. here we just let it err.
1452 ;; should pick one style and stick with it.
1453 (do ((char (read-char stream t) (read-char stream t)))
1454 ((multiple-escape-p char rt))
1455 (if (single-escape-p char rt) (setq char (read-char stream t)))
1456 (ouch-read-buffer-escaped char buf))
1457 (getchar-or-else (go RETURN-SYMBOL))
1458 (case (char-class char attribute-array attribute-hash-table)
1459 (#.+char-attr-delimiter+ (unread-char char stream) (go RETURN-SYMBOL))
1460 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1461 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1462 (#.+char-attr-package-delimiter+ (go COLON))
1463 (t (go SYMBOL)))
1464 COLON
1465 (unless (zerop colons)
1466 (simple-reader-error
1467 stream "too many colons in ~S" (copy-token-buf-string buf)))
1468 (setf buf (normalize-read-buffer buf))
1469 (casify-read-buffer buf)
1470 (setq colons 1)
1471 (setq package-designator
1472 (if (or (plusp (token-buf-fill-ptr *read-buffer*))
1473 seen-multiple-escapes)
1474 (prog1 (sized-token-buf-string buf)
1475 (let ((new (acquire-token-buf)))
1476 (setf (token-buf-next new) buf ; new points to old
1477 buf new *read-buffer* new)))
1478 *keyword-package*))
1479 (reset-read-buffer buf)
1480 (getchar-or-else (reader-eof-error stream "after reading a colon"))
1481 (case (char-class char attribute-array attribute-hash-table)
1482 (#.+char-attr-delimiter+
1483 (unread-char char stream)
1484 (simple-reader-error stream
1485 "illegal terminating character after a colon: ~S"
1486 char))
1487 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1488 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1489 (#.+char-attr-package-delimiter+ (go INTERN))
1490 (t (go SYMBOL)))
1491 INTERN
1492 (setq colons 2)
1493 (getchar-or-else (reader-eof-error stream "after reading a colon"))
1494 (case (char-class char attribute-array attribute-hash-table)
1495 (#.+char-attr-delimiter+
1496 (unread-char char stream)
1497 (if package-designator
1498 (let* ((*reader-package*
1499 (reader-find-package package-designator stream)))
1500 (return (read stream t nil t)))
1501 (simple-reader-error stream
1502 "illegal terminating character after a double-colon: ~S"
1503 char)))
1504 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1505 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1506 (#.+char-attr-package-delimiter+
1507 (simple-reader-error stream
1508 "too many colons after ~S name"
1509 package-designator))
1510 (t (go SYMBOL)))
1511 RETURN-SYMBOL
1512 (setf buf (normalize-read-buffer buf))
1513 (casify-read-buffer buf)
1514 (let ((pkg (if package-designator
1515 (reader-find-package package-designator stream)
1516 (or *reader-package* (sane-package)))))
1517 (if (or (zerop colons) (= colons 2) (eq pkg *keyword-package*))
1518 (return (%intern (token-buf-string buf) (token-buf-fill-ptr buf)
1519 pkg t))
1520 (multiple-value-bind (symbol accessibility)
1521 (%find-symbol (token-buf-string buf) (token-buf-fill-ptr buf)
1522 pkg)
1523 (when (eq accessibility :external) (return symbol))
1524 (let ((name (copy-token-buf-string buf)))
1525 (with-simple-restart (continue "Use symbol anyway.")
1526 (error 'simple-reader-package-error
1527 :package pkg
1528 :stream stream
1529 :format-arguments (list name (package-name pkg))
1530 :format-control
1531 (if accessibility
1532 "The symbol ~S is not external in the ~A package."
1533 "Symbol ~S not found in the ~A package.")))
1534 (return (intern name pkg))))))))))
1536 ;;; For semi-external use: Return 3 values: the token-buf,
1537 ;;; a flag for whether there was an escape char, and the position of
1538 ;;; any package delimiter. The returned token-buf is not case-converted.
1539 (defun read-extended-token (stream)
1540 ;; recursive-p = T is basically irrelevant.
1541 (let ((first-char (read-char stream nil +EOF+ t)))
1542 (if (neq first-char +EOF+)
1543 (internal-read-extended-token stream first-char nil)
1544 (values (reset-read-buffer *read-buffer*) nil nil))))
1546 ;;; for semi-external use:
1548 ;;; Read an extended token with the first character escaped. Return
1549 ;;; the token-buf. The returned token-buf is not case-converted.
1550 (defun read-extended-token-escaped (stream)
1551 (let ((first-char (read-char stream nil +EOF+)))
1552 (if (neq first-char +EOF+)
1553 (values (internal-read-extended-token stream first-char t))
1554 (reader-eof-error stream "after escape"))))
1556 ;;;; number-reading functions
1558 ;; Mapping of read-base to the max input characters in a positive fixnum.
1559 (eval-when (:compile-toplevel :execute)
1560 (defun integer-reader-safe-digits ()
1561 (do ((a (make-array 35 :element-type '(unsigned-byte 8)))
1562 (base 2 (1+ base)))
1563 ((> base 36) a)
1564 (do ((total (1- base) (+ (* total base) (1- base)))
1565 (n-digits 0 (1+ n-digits)))
1566 ((sb!xc:typep total 'bignum)
1567 (setf (aref a (- base 2)) n-digits))
1568 ;; empty DO body
1571 ;; self-test
1572 (do ((maxdigits (integer-reader-safe-digits))
1573 (base 2 (1+ base)))
1574 ((> base 36))
1575 (let* ((n-digits (aref maxdigits (- base 2)))
1576 (d (char (write-to-string (1- base) :base base) 0))
1577 (string (make-string (1+ n-digits) :initial-element d))) ; 1 extra
1578 (assert (not (typep (parse-integer string :radix base)
1579 `(unsigned-byte ,sb!vm:n-positive-fixnum-bits))))
1580 (assert (typep (parse-integer string :end n-digits :radix base)
1581 `(unsigned-byte ,sb!vm:n-positive-fixnum-bits))))))
1583 (defmacro !setq-optional-leading-sign (sign-flag token-buf rewind)
1584 ;; guaranteed to have at least one character in buffer at the start
1585 ;; or immediately following [ESFDL] marker depending on 'rewind' flag.
1586 `(locally (declare (optimize (sb!c::insert-array-bounds-checks 0)))
1587 (,(if rewind 'setf 'incf)
1588 (token-buf-cursor ,token-buf)
1589 (case (elt (token-buf-string ,token-buf)
1590 ,(if rewind 0 `(token-buf-cursor ,token-buf)))
1591 (#\- (setq ,sign-flag t) 1)
1592 (#\+ 1)
1593 (t 0)))))
1595 (defun make-integer (&optional (base *read-base*))
1596 #!+sb-doc
1597 "Minimizes bignum-fixnum multiplies by reading a 'safe' number of digits,
1598 then multiplying by a power of the base and adding."
1599 (declare ((integer 2 36) base)
1600 (inline token-buf-getchar)) ; makes for smaller code
1601 (let* ((fixnum-max-digits
1602 (macrolet ((maxdigits ()
1603 (!coerce-to-specialized (integer-reader-safe-digits)
1604 '(unsigned-byte 8))))
1605 (aref (maxdigits) (- base 2))))
1606 (base-power
1607 (macrolet ((base-powers ()
1608 (do ((maxdigits (integer-reader-safe-digits))
1609 (a (make-array 35))
1610 (base 2 (1+ base)))
1611 ((> base 36) a)
1612 (setf (aref a (- base 2))
1613 (expt base (aref maxdigits (- base 2)))))))
1614 (truly-the integer (aref (base-powers) (- base 2)))))
1615 (negativep nil)
1616 (result 0)
1617 (buf *read-buffer*))
1618 (!setq-optional-leading-sign negativep buf t)
1619 (loop
1620 (let ((acc 0))
1621 (declare (type (and fixnum unsigned-byte) acc))
1622 (dotimes (digit-count fixnum-max-digits)
1623 (let ((ch (token-buf-getchar buf)))
1624 (if (or (not ch) (eql ch #\.))
1625 (return-from make-integer
1626 (let ((result
1627 (if (zerop result) acc
1628 (+ (* result (expt base digit-count)) acc))))
1629 (if negativep (- result) result)))
1630 (setq acc (truly-the fixnum
1631 (+ (digit-char-p ch base)
1632 (truly-the fixnum (* acc base))))))))
1633 (setq result (+ (* result base-power) acc))))))
1635 (defun truncate-exponent (exponent number divisor)
1636 #!+sb-doc
1637 "Truncate exponent if it's too large for a float"
1638 ;; Work with base-2 logarithms to avoid conversions to floats,
1639 ;; and convert to base-10 conservatively at the end.
1640 ;; Use the least positive float, because denormalized exponent
1641 ;; can be larger than normalized.
1642 (let* ((max-exponent
1643 #!-long-float
1644 (+ sb!vm:double-float-digits sb!vm:double-float-bias))
1645 (number-magnitude (integer-length number))
1646 (divisor-magnitude (1- (integer-length divisor)))
1647 (magnitude (- number-magnitude divisor-magnitude)))
1648 (if (minusp exponent)
1649 (max exponent (ceiling (- (+ max-exponent magnitude))
1650 #.(floor (log 10 2))))
1651 (min exponent (floor (- max-exponent magnitude)
1652 #.(floor (log 10 2)))))))
1654 (defun make-float (stream)
1655 ;; Assume that the contents of *read-buffer* are a legal float, with nothing
1656 ;; else after it.
1657 (let ((buf *read-buffer*)
1658 (negative-fraction nil)
1659 (number 0)
1660 (divisor 1)
1661 (negative-exponent nil)
1662 (exponent 0)
1663 (float-char ())
1664 char)
1665 (!setq-optional-leading-sign negative-fraction buf t)
1666 ;; Read digits before the dot.
1667 (macrolet ((accumulate (expr)
1668 `(let (digit)
1669 (loop (if (and (setq char (token-buf-getchar buf))
1670 (setq digit (digit-char-p char)))
1671 ,expr
1672 (return))))))
1673 (accumulate (setq number (+ (* number 10) digit)))
1674 ;; Deal with the dot, if it's there.
1675 (when (char= char #\.)
1676 ;; Read digits after the dot.
1677 (accumulate (setq divisor (* divisor 10)
1678 number (+ (* number 10) digit))))
1679 ;; Is there an exponent letter?
1680 (cond
1681 ((null char)
1682 ;; If not, we've read the whole number.
1683 (let ((num (make-float-aux number divisor
1684 *read-default-float-format*
1685 stream)))
1686 (return-from make-float (if negative-fraction (- num) num))))
1687 ((= (get-constituent-trait char) +char-attr-constituent-expt+)
1688 (setq float-char char)
1689 ;; Check leading sign.
1690 (!setq-optional-leading-sign negative-exponent buf nil)
1691 ;; Read digits for exponent.
1692 (accumulate (setq exponent (+ (* exponent 10) digit)))
1693 (setq exponent (if negative-exponent (- exponent) exponent))
1694 ;; Generate and return the float, depending on FLOAT-CHAR:
1695 (let* ((float-format (case (char-upcase float-char)
1696 (#\E *read-default-float-format*)
1697 (#\S 'short-float)
1698 (#\F 'single-float)
1699 (#\D 'double-float)
1700 (#\L 'long-float)))
1701 (exponent (truncate-exponent exponent number divisor))
1702 (result (make-float-aux (* (expt 10 exponent) number)
1703 divisor float-format stream)))
1704 (return-from make-float
1705 (if negative-fraction (- result) result))))
1706 (t (bug "bad fallthrough in floating point reader"))))))
1708 (defun make-float-aux (number divisor float-format stream)
1709 (handler-case
1710 (coerce (/ number divisor) float-format)
1711 (type-error (c)
1712 (error 'reader-impossible-number-error
1713 :error c :stream stream
1714 :format-control "failed to build float from ~a"
1715 :format-arguments (list (copy-token-buf-string *read-buffer*))))))
1717 (defun make-ratio (stream)
1718 ;; Assume *READ-BUFFER* contains a legal ratio. Build the number from
1719 ;; the string.
1720 ;; This code is inferior to that of MAKE-INTEGER because it makes no
1721 ;; attempt to perform as few bignum multiplies as possible.
1723 (let ((numerator 0) (denominator 0) (negativep nil)
1724 (base *read-base*) (buf *read-buffer*))
1725 (!setq-optional-leading-sign negativep buf t)
1726 ;; Get numerator.
1727 (loop (let ((dig (digit-char-p (token-buf-getchar buf) base)))
1728 (if dig
1729 (setq numerator (+ (* numerator base) dig))
1730 (return))))
1731 ;; Get denominator.
1732 (do* ((ch (token-buf-getchar buf) (token-buf-getchar buf))
1733 (dig ()))
1734 ((or (null ch) (not (setq dig (digit-char-p ch base)))))
1735 (setq denominator (+ (* denominator base) dig)))
1736 (let ((num (handler-case
1737 (/ numerator denominator)
1738 (arithmetic-error (c)
1739 (error 'reader-impossible-number-error
1740 :error c :stream stream
1741 :format-control "failed to build ratio")))))
1742 (if negativep (- num) num))))
1744 ;;;; General reader for dispatch macros
1746 (defun dispatch-char-error (stream sub-char ignore)
1747 (declare (ignore ignore))
1748 (if *read-suppress*
1749 (values)
1750 (simple-reader-error stream
1751 "no dispatch function defined for ~S"
1752 sub-char)))
1754 (defun read-dispatch-char (stream dispatch-table)
1755 ;; Read some digits.
1756 (let ((numargp nil)
1757 (numarg 0)
1758 (sub-char ()))
1759 (loop
1760 (let ((ch (read-char stream nil +EOF+)))
1761 (if (eq ch +EOF+)
1762 (reader-eof-error stream "inside dispatch character")
1763 ;; Take care of the extra char.
1764 (let ((dig (digit-char-p ch)))
1765 (if dig
1766 (setq numargp t numarg (+ (* numarg 10) dig))
1767 (return (setq sub-char (char-upcase ch))))))))
1768 ;; Look up the function and call it.
1769 (let ((fn (get-raw-cmt-dispatch-entry sub-char dispatch-table)))
1770 (funcall (!cmt-entry-to-function fn #'dispatch-char-error)
1771 stream sub-char (if numargp numarg nil)))))
1773 ;;;; READ-FROM-STRING
1775 (declaim (ftype (sfunction (string t t index (or null index) t) (values t index))
1776 %read-from-string))
1777 (defun %read-from-string (string eof-error-p eof-value start end preserve-whitespace)
1778 (with-array-data ((string string :offset-var offset)
1779 (start start)
1780 (end end)
1781 :check-fill-pointer t)
1782 (let ((stream (make-string-input-stream string start end)))
1783 (values (if preserve-whitespace
1784 (%read-preserving-whitespace stream eof-error-p eof-value nil)
1785 (read stream eof-error-p eof-value))
1786 (- (string-input-stream-current stream) offset)))))
1788 (locally
1789 (declare (muffle-conditions style-warning))
1790 (defun read-from-string (string &optional (eof-error-p t) eof-value
1791 &key (start 0) end preserve-whitespace)
1792 #!+sb-doc
1793 "The characters of string are successively given to the lisp reader
1794 and the lisp object built by the reader is returned. Macro chars
1795 will take effect."
1796 (declare (string string))
1797 (maybe-note-read-from-string-signature-issue eof-error-p)
1798 (%read-from-string string eof-error-p eof-value start end preserve-whitespace)))
1800 ;;;; PARSE-INTEGER
1802 (defun parse-integer (string &key (start 0) end (radix 10) junk-allowed)
1803 #!+sb-doc
1804 "Examine the substring of string delimited by start and end
1805 (default to the beginning and end of the string) It skips over
1806 whitespace characters and then tries to parse an integer. The
1807 radix parameter must be between 2 and 36."
1808 (macrolet ((parse-error (format-control)
1809 `(error 'simple-parse-error
1810 :format-control ,format-control
1811 :format-arguments (list string))))
1812 (with-array-data ((string string :offset-var offset)
1813 (start start)
1814 (end end)
1815 :check-fill-pointer t)
1816 (let ((index (do ((i start (1+ i)))
1817 ((= i end)
1818 (if junk-allowed
1819 (return-from parse-integer (values nil end))
1820 (parse-error "no non-whitespace characters in string ~S.")))
1821 (declare (fixnum i))
1822 (unless (whitespace[1]p (char string i)) (return i))))
1823 (minusp nil)
1824 (found-digit nil)
1825 (result 0))
1826 (declare (fixnum index))
1827 (let ((char (char string index)))
1828 (cond ((char= char #\-)
1829 (setq minusp t)
1830 (incf index))
1831 ((char= char #\+)
1832 (incf index))))
1833 (loop
1834 (when (= index end) (return nil))
1835 (let* ((char (char string index))
1836 (weight (digit-char-p char radix)))
1837 (cond (weight
1838 (setq result (+ weight (* result radix))
1839 found-digit t))
1840 (junk-allowed (return nil))
1841 ((whitespace[1]p char)
1842 (loop
1843 (incf index)
1844 (when (= index end) (return))
1845 (unless (whitespace[1]p (char string index))
1846 (parse-error "junk in string ~S")))
1847 (return nil))
1849 (parse-error "junk in string ~S"))))
1850 (incf index))
1851 (values
1852 (if found-digit
1853 (if minusp (- result) result)
1854 (if junk-allowed
1856 (parse-error "no digits in string ~S")))
1857 (- index offset))))))
1859 ;;;; reader initialization code
1861 (defun !reader-cold-init ()
1862 (!cold-init-constituent-trait-table)
1863 (!cold-init-standard-readtable))
1865 (def!method print-object ((readtable readtable) stream)
1866 (print-unreadable-object (readtable stream :identity t :type t)))
1868 ;; Backward-compatibility adapter. The "named-readtables" system in
1869 ;; Quicklisp expects this interface, and it's a reasonable thing to support.
1870 ;; What is silly however is that DISPATCH-TABLES was an alist each of whose
1871 ;; values was a hashtable which got immediately coerced to an alist.
1872 ;; In anticipation of perhaps not doing an extra re-shaping, if HASH-TABLE-P
1873 ;; is NIL then return nested alists: ((#\# (#\R . #<FUNCTION SHARP-R>) ...))
1874 (defun dispatch-tables (readtable &optional (hash-table-p t))
1875 (let (alist)
1876 (flet ((process (char fn &aux (dtable (%dispatch-macro-char-table fn)))
1877 (when dtable
1878 (let ((output (awhen (car dtable) (%hash-table-alist it))))
1879 (loop for fn across (the simple-vector (cdr dtable))
1880 and ch from 0
1881 when fn do (push (cons (code-char ch) fn) output))
1882 (dolist (cell output) ; coerce values to function-designator
1883 (rplacd cell (!cmt-entry-to-fun-designator (cdr cell))))
1884 (when hash-table-p ; caller wants hash-tables
1885 (setq output (%stuff-hash-table (make-hash-table) output)))
1886 (push (cons char output) alist)))))
1887 (loop for fn across (character-macro-array readtable) and ch from 0
1888 do (process (code-char ch) fn))
1889 (maphash #'process (character-macro-hash-table readtable)))
1890 alist))
1892 ;; Stub - should never get called with anything but NIL
1893 ;; and only after all macros have been changed to constituents already.
1894 (defun (setf dispatch-tables) (new-alist readtable)
1895 (declare (ignore readtable))
1896 (unless (null new-alist)
1897 (error "Assignment to virtual DISPATCH-TABLES slot not allowed"))
1898 new-alist)
1900 ;;; like LISTEN, but any whitespace in the input stream will be flushed
1901 (defun listen-skip-whitespace (&optional (stream *standard-input*))
1902 (do ((char (read-char-no-hang stream nil nil nil)
1903 (read-char-no-hang stream nil nil nil)))
1904 ((null char) nil)
1905 (cond ((not (whitespace[1]p char))
1906 (unread-char char stream)
1907 (return t)))))