3 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
12 (in-package "SB!IMPL")
14 ;;;; miscellaneous global variables
16 ;;; ANSI: "the floating-point format that is to be used when reading a
17 ;;; floating-point number that has no exponent marker or that has e or
18 ;;; E for an exponent marker"
19 (defvar *read-default-float-format* 'single-float)
20 (declaim (type (member short-float single-float double-float long-float)
21 *read-default-float-format*))
24 (declaim (type readtable *readtable*))
26 (setf (fdocumentation '*readtable* 'variable)
27 "Variable bound to current readtable.")
29 ;;; a standard Lisp readtable. This is for recovery from broken
30 ;;; read-tables (and for WITH-STANDARD-IO-SYNTAX), and should not
31 ;;; normally be user-visible.
32 (defvar *standard-readtable*)
34 (defvar *old-package* nil
36 "the value of *PACKAGE* at the start of the last read, or NIL")
38 ;;; In case we get an error trying to parse a symbol, we want to rebind the
39 ;;; above stuff so it's cool.
41 ;;; FIXME: These forward declarations should be moved somewhere earlier,
43 (declaim (special *package* *keyword-package* *read-base*))
47 (defun reader-eof-error (stream context)
48 (error 'reader-eof-error
52 (defun %reader-error (stream control &rest args)
55 :format-control control
56 :format-arguments args))
58 ;;;; macros and functions for character tables
60 ;;; FIXME: could be SB!XC:DEFMACRO inside EVAL-WHEN (COMPILE EVAL)
61 (defmacro get-cat-entry (char rt)
62 ;; KLUDGE: Only give this side-effect-free args.
63 ;; FIXME: should probably become inline function
64 `(if (typep ,char 'base-char)
65 (elt (character-attribute-array ,rt) (char-code ,char))
66 (gethash ,char (character-attribute-hash-table ,rt)
67 +char-attr-constituent+)))
69 (defun set-cat-entry (char newvalue &optional (rt *readtable*))
70 (if (typep char 'base-char)
71 (setf (elt (character-attribute-array rt) (char-code char)) newvalue)
72 ;; FIXME: could REMHASH if we're setting to
73 ;; +CHAR-ATTR-CONSTITUENT+
74 (setf (gethash char (character-attribute-hash-table rt)) newvalue)))
76 ;;; the value actually stored in the character macro table. As per
77 ;;; ANSI #'GET-MACRO-CHARACTER and #'SET-MACRO-CHARACTER, this can
78 ;;; be either a function or NIL.
79 (eval-when (:compile-toplevel :execute)
80 (sb!xc:defmacro get-raw-cmt-entry (char readtable)
81 `(if (typep ,char 'base-char)
82 (svref (character-macro-array ,readtable) (char-code ,char))
83 ;; Note: DEFAULT here is NIL, not #'UNDEFINED-MACRO-CHAR, so
84 ;; that everything above the base-char range is a non-macro
85 ;; constituent by default.
86 (gethash ,char (character-macro-hash-table ,readtable) nil))))
88 ;;; the value represented by whatever is stored in the character macro
89 ;;; table. As per ANSI #'GET-MACRO-CHARACTER and #'SET-MACRO-CHARACTER,
90 ;;; a function value represents itself, and a NIL value represents the
92 (defun get-coerced-cmt-entry (char readtable)
94 (or (get-raw-cmt-entry char readtable)
97 (defun set-cmt-entry (char new-value-designator &optional (rt *readtable*))
98 (if (typep char 'base-char)
99 (setf (svref (character-macro-array rt) (char-code char))
100 (and new-value-designator
101 (%coerce-callable-to-fun new-value-designator)))
102 (setf (gethash char (character-macro-hash-table rt))
103 (and new-value-designator
104 (%coerce-callable-to-fun new-value-designator)))))
106 (defun undefined-macro-char (stream char)
107 (unless *read-suppress*
108 (%reader-error stream "undefined read-macro character ~S" char)))
110 ;;; The character attribute table is a CHAR-CODE-LIMIT vector of integers.
112 (defmacro test-attribute (char whichclass rt)
113 `(= (the fixnum (get-cat-entry ,char ,rt)) ,whichclass))
115 ;;; predicates for testing character attributes
117 ;;; the [1] and [2] here refer to ANSI glossary entries for
119 #!-sb-fluid (declaim (inline whitespace[1]p whitespace[2]p))
120 (defun whitespace[1]p (char)
121 (test-attribute char +char-attr-whitespace+ *standard-readtable*))
122 (defun whitespace[2]p (char &optional (rt *readtable*))
123 (test-attribute char +char-attr-whitespace+ rt))
125 (defmacro constituentp (char &optional (rt '*readtable*))
126 `(test-attribute ,char +char-attr-constituent+ ,rt))
128 (defmacro terminating-macrop (char &optional (rt '*readtable*))
129 `(test-attribute ,char +char-attr-terminating-macro+ ,rt))
131 (defmacro single-escape-p (char &optional (rt '*readtable*))
132 `(test-attribute ,char +char-attr-single-escape+ ,rt))
134 (defmacro multiple-escape-p (char &optional (rt '*readtable*))
135 `(test-attribute ,char +char-attr-multiple-escape+ ,rt))
137 (defmacro token-delimiterp (char &optional (rt '*readtable*))
138 ;; depends on actual attribute numbering above.
139 `(<= (get-cat-entry ,char ,rt) +char-attr-terminating-macro+))
141 ;;;; constituent traits (see ANSI 2.1.4.2)
143 ;;; There are a number of "secondary" attributes which are constant
144 ;;; properties of characters (as long as they are constituents).
146 (defvar *constituent-trait-table*)
147 (declaim (type attribute-table *constituent-trait-table*))
149 (defun !set-constituent-trait (char trait)
150 (aver (typep char 'base-char))
151 (setf (elt *constituent-trait-table* (char-code char))
154 (defun !cold-init-constituent-trait-table ()
155 (setq *constituent-trait-table*
156 (make-array base-char-code-limit :element-type '(unsigned-byte 8)
157 :initial-element +char-attr-constituent+))
158 (!set-constituent-trait #\: +char-attr-package-delimiter+)
159 (!set-constituent-trait #\. +char-attr-constituent-dot+)
160 (!set-constituent-trait #\+ +char-attr-constituent-sign+)
161 (!set-constituent-trait #\- +char-attr-constituent-sign+)
162 (!set-constituent-trait #\/ +char-attr-constituent-slash+)
163 (do ((i (char-code #\0) (1+ i)))
164 ((> i (char-code #\9)))
165 (!set-constituent-trait (code-char i) +char-attr-constituent-digit+))
166 (!set-constituent-trait #\E +char-attr-constituent-expt+)
167 (!set-constituent-trait #\F +char-attr-constituent-expt+)
168 (!set-constituent-trait #\D +char-attr-constituent-expt+)
169 (!set-constituent-trait #\S +char-attr-constituent-expt+)
170 (!set-constituent-trait #\L +char-attr-constituent-expt+)
171 (!set-constituent-trait #\e +char-attr-constituent-expt+)
172 (!set-constituent-trait #\f +char-attr-constituent-expt+)
173 (!set-constituent-trait #\d +char-attr-constituent-expt+)
174 (!set-constituent-trait #\s +char-attr-constituent-expt+)
175 (!set-constituent-trait #\l +char-attr-constituent-expt+)
176 (!set-constituent-trait #\Space +char-attr-invalid+)
177 (!set-constituent-trait #\Newline +char-attr-invalid+)
178 (dolist (c (list backspace-char-code tab-char-code form-feed-char-code
179 return-char-code rubout-char-code))
180 (!set-constituent-trait (code-char c) +char-attr-invalid+)))
182 (defmacro get-constituent-trait (char)
183 `(if (typep ,char 'base-char)
184 (elt *constituent-trait-table* (char-code ,char))
185 +char-attr-constituent+))
187 ;;;; readtable operations
189 (defun shallow-replace/eql-hash-table (to from)
190 (maphash (lambda (k v) (setf (gethash k to) v)) from))
192 (defun copy-readtable (&optional (from-readtable *readtable*)
194 (let ((really-from-readtable (or from-readtable *standard-readtable*))
195 (really-to-readtable (or to-readtable (make-readtable))))
196 (replace (character-attribute-array really-to-readtable)
197 (character-attribute-array really-from-readtable))
198 (shallow-replace/eql-hash-table
199 (character-attribute-hash-table really-to-readtable)
200 (character-attribute-hash-table really-from-readtable))
201 (replace (character-macro-array really-to-readtable)
202 (character-macro-array really-from-readtable))
203 (shallow-replace/eql-hash-table
204 (character-macro-hash-table really-to-readtable)
205 (character-macro-hash-table really-from-readtable))
206 (setf (dispatch-tables really-to-readtable)
207 (mapcar (lambda (pair)
209 (let ((table (make-hash-table)))
210 (shallow-replace/eql-hash-table table (cdr pair))
212 (dispatch-tables really-from-readtable)))
213 (setf (readtable-case really-to-readtable)
214 (readtable-case really-from-readtable))
215 really-to-readtable))
217 (defun set-syntax-from-char (to-char from-char &optional
218 (to-readtable *readtable*)
221 "Causes the syntax of TO-CHAR to be the same as FROM-CHAR in the
222 optional readtable (defaults to the current readtable). The
223 FROM-TABLE defaults to the standard Lisp readtable when NIL."
224 (let ((really-from-readtable (or from-readtable *standard-readtable*)))
225 (let ((att (get-cat-entry from-char really-from-readtable))
226 (mac (get-raw-cmt-entry from-char really-from-readtable))
227 (from-dpair (find from-char (dispatch-tables really-from-readtable)
228 :test #'char= :key #'car))
229 (to-dpair (find to-char (dispatch-tables to-readtable)
230 :test #'char= :key #'car)))
231 (set-cat-entry to-char att to-readtable)
232 (set-cmt-entry to-char mac to-readtable)
236 (let ((table (cdr to-dpair)))
238 (shallow-replace/eql-hash-table table (cdr from-dpair))))
240 (let ((pair (cons to-char (make-hash-table))))
241 (shallow-replace/eql-hash-table (cdr pair) (cdr from-dpair))
242 (setf (dispatch-tables to-readtable)
243 (push pair (dispatch-tables to-readtable)))))))))
246 (defun set-macro-character (char function &optional
247 (non-terminatingp nil)
248 (readtable *readtable*))
250 "Causes CHAR to be a macro character which invokes FUNCTION when seen
251 by the reader. The NON-TERMINATINGP flag can be used to make the macro
252 character non-terminating, i.e. embeddable in a symbol name."
253 (let ((designated-readtable (or readtable *standard-readtable*)))
254 (set-cat-entry char (if non-terminatingp
255 +char-attr-constituent+
256 +char-attr-terminating-macro+)
257 designated-readtable)
258 (set-cmt-entry char function designated-readtable)
259 t)) ; (ANSI-specified return value)
261 (defun get-macro-character (char &optional (readtable *readtable*))
263 "Return the function associated with the specified CHAR which is a macro
264 character, or NIL if there is no such function. As a second value, return
265 T if CHAR is a macro character which is non-terminating, i.e. which can
266 be embedded in a symbol name."
267 (let* ((designated-readtable (or readtable *standard-readtable*))
268 ;; the first return value: a FUNCTION if CHAR is a macro
269 ;; character, or NIL otherwise
270 (fun-value (get-raw-cmt-entry char designated-readtable)))
272 ;; NON-TERMINATING-P return value:
274 (or (constituentp char)
275 (not (terminating-macrop char)))
276 ;; ANSI's definition of GET-MACRO-CHARACTER says this
277 ;; value is NIL when CHAR is not a macro character.
278 ;; I.e. this value means not just "non-terminating
279 ;; character?" but "non-terminating macro character?".
282 ;;;; definitions to support internal programming conventions
284 (defmacro eofp (char)
285 `(eq ,char *eof-object*))
287 (defun flush-whitespace (stream)
288 ;; This flushes whitespace chars, returning the last char it read (a
289 ;; non-white one). It always gets an error on end-of-file.
290 (let ((stream (in-synonym-of stream)))
291 (if (ansi-stream-p stream)
292 (prepare-for-fast-read-char stream
293 (do ((attribute-array (character-attribute-array *readtable*))
294 (attribute-hash-table
295 (character-attribute-hash-table *readtable*))
296 (char (fast-read-char t) (fast-read-char t)))
298 (if (typep char 'base-char)
299 (aref attribute-array (char-code char))
300 (gethash char attribute-hash-table
301 +char-attr-constituent+)))
302 +char-attr-whitespace+)
303 (done-with-fast-read-char)
306 (do ((attribute-array (character-attribute-array *readtable*))
307 (attribute-hash-table
308 (character-attribute-hash-table *readtable*))
309 (char (read-char stream nil :eof) (read-char stream nil :eof)))
312 (if (typep char 'base-char)
313 (aref attribute-array (char-code char))
314 (gethash char attribute-hash-table
315 +char-attr-constituent+)))
316 +char-attr-whitespace+))
318 (error 'end-of-file :stream stream)
321 ;;;; temporary initialization hack
323 (defun !cold-init-standard-readtable ()
324 (setq *standard-readtable* (make-readtable))
325 ;; All characters get boring defaults in MAKE-READTABLE. Now we
326 ;; override the boring defaults on characters which need more
327 ;; interesting behavior.
328 (let ((*readtable* *standard-readtable*))
330 (flet ((whitespaceify (char)
331 (set-cmt-entry char nil)
332 (set-cat-entry char +char-attr-whitespace+)))
333 (whitespaceify (code-char tab-char-code))
334 (whitespaceify #\Newline)
335 (whitespaceify #\Space)
336 (whitespaceify (code-char form-feed-char-code))
337 (whitespaceify (code-char return-char-code)))
339 (set-cat-entry #\\ +char-attr-single-escape+)
340 (set-cmt-entry #\\ nil)
342 (set-cat-entry #\| +char-attr-multiple-escape+)
343 (set-cmt-entry #\| nil)
345 ;; Easy macro-character definitions are in this source file.
346 (set-macro-character #\" #'read-string)
347 (set-macro-character #\' #'read-quote)
348 (set-macro-character #\( #'read-list)
349 (set-macro-character #\) #'read-right-paren)
350 (set-macro-character #\; #'read-comment)
351 ;; (The hairier macro-character definitions, for #\# and #\`, are
352 ;; defined elsewhere, in their own source files.)
355 (do ((ichar 0 (1+ ichar))
357 ((= ichar base-char-code-limit))
358 (setq char (code-char ichar))
359 (when (constituentp char *standard-readtable*)
360 (set-cmt-entry char nil)))))
362 ;;;; implementation of the read buffer
364 (defvar *read-buffer*)
365 (defvar *read-buffer-length*)
366 ;;; FIXME: Is it really helpful to have *READ-BUFFER-LENGTH* be a
367 ;;; separate variable instead of just calculating it on the fly as
368 ;;; (LENGTH *READ-BUFFER*)?
373 (declaim (type index *read-buffer-length* *inch-ptr* *ouch-ptr*))
374 (declaim (type (simple-array character (*)) *read-buffer*))
376 (defmacro reset-read-buffer ()
377 ;; Turn *READ-BUFFER* into an empty read buffer.
379 ;; *OUCH-PTR* always points to next char to write.
381 ;; *INCH-PTR* always points to next char to read.
382 (setq *inch-ptr* 0)))
384 ;;; FIXME I removed "THE FIXNUM"'s from OUCH-READ-BUFFER and
385 ;;; OUCH-UNREAD-BUFFER, check to make sure that Python really is smart
386 ;;; enough to make good code without them. And while I'm at it,
387 ;;; converting them from macros to inline functions might be good,
390 (defmacro ouch-read-buffer (char)
392 ;; When buffer overflow
393 (when (>= *ouch-ptr* *read-buffer-length*)
394 ;; Size should be doubled.
396 (setf (elt (the simple-string *read-buffer*) *ouch-ptr*) ,char)
397 (setq *ouch-ptr* (1+ *ouch-ptr*))))
399 ;;; macro to move *ouch-ptr* back one.
400 (defmacro ouch-unread-buffer ()
401 '(when (> *ouch-ptr* *inch-ptr*)
402 (setq *ouch-ptr* (1- (the fixnum *ouch-ptr*)))))
404 (defun grow-read-buffer ()
405 (let ((rbl (length (the simple-string *read-buffer*))))
407 (concatenate 'simple-string
410 (setq *read-buffer-length* (* 2 rbl))))
412 (defun inchpeek-read-buffer ()
413 (if (>= (the fixnum *inch-ptr*) (the fixnum *ouch-ptr*))
415 (elt *read-buffer* *inch-ptr*)))
417 (defun inch-read-buffer ()
418 (if (>= *inch-ptr* *ouch-ptr*)
421 (elt *read-buffer* *inch-ptr*)
424 (defmacro unread-buffer ()
427 (defun read-unwind-read-buffer ()
428 ;; Keep contents, but make next (INCH..) return first character.
431 (defun read-buffer-to-string ()
432 (subseq *read-buffer* 0 *ouch-ptr*))
434 (defmacro with-reader ((&optional recursive-p) &body body)
436 "If RECURSIVE-P is NIL, bind *READER-BUFFER* and its subservient
437 variables to allow for nested and thread safe reading."
440 (let* ((*read-buffer* (make-string 128))
441 (*read-buffer-length* 128)
446 ;;;; READ-PRESERVING-WHITESPACE, READ-DELIMITED-LIST, and READ
448 ;;; an alist for #=, used to keep track of objects with labels assigned that
449 ;;; have been completely read. Each entry is (integer-tag gensym-tag value).
451 ;;; KLUDGE: Should this really be an alist? It seems as though users
452 ;;; could reasonably expect N log N performance for large datasets.
453 ;;; On the other hand, it's probably very very seldom a problem in practice.
454 ;;; On the third hand, it might be just as easy to use a hash table
455 ;;; as an alist, so maybe we should. -- WHN 19991202
456 (defvar *sharp-equal-alist* ())
458 (declaim (special *standard-input*))
460 ;;; READ-PRESERVING-WHITESPACE behaves just like READ, only it makes
461 ;;; sure to leave terminating whitespace in the stream. (This is a
462 ;;; COMMON-LISP exported symbol.)
463 (defun read-preserving-whitespace (&optional (stream *standard-input*)
468 "Read from STREAM and return the value read, preserving any whitespace
469 that followed the object."
471 ;; a loop for repeating when a macro returns nothing
473 (let ((char (read-char stream eof-error-p *eof-object*)))
474 (cond ((eofp char) (return eof-value))
475 ((whitespace[2]p char))
477 (let* ((macrofun (get-coerced-cmt-entry char *readtable*))
478 (result (multiple-value-list
479 (funcall macrofun stream char))))
480 ;; Repeat if macro returned nothing.
482 (return (unless *read-suppress* (car result)))))))))
484 (let ((*sharp-equal-alist* nil))
485 (read-preserving-whitespace stream eof-error-p eof-value t)))))
487 ;;; Return NIL or a list with one thing, depending.
489 ;;; for functions that want comments to return so that they can look
490 ;;; past them. We assume CHAR is not whitespace.
491 (defun read-maybe-nothing (stream char)
492 (let ((retval (multiple-value-list
493 (funcall (get-coerced-cmt-entry char *readtable*)
496 (if retval (rplacd retval nil))))
498 (defun read (&optional (stream *standard-input*)
503 "Read the next Lisp value from STREAM, and return it."
504 (let ((result (read-preserving-whitespace stream
508 ;; This function generally discards trailing whitespace. If you
509 ;; don't want to discard trailing whitespace, call
510 ;; CL:READ-PRESERVING-WHITESPACE instead.
511 (unless (or (eql result eof-value) recursivep)
512 (let ((next-char (read-char stream nil nil)))
513 (unless (or (null next-char)
514 (whitespace[2]p next-char))
515 (unread-char next-char stream))))
518 ;;; (This is a COMMON-LISP exported symbol.)
519 (defun read-delimited-list (endchar &optional
520 (input-stream *standard-input*)
523 "Read Lisp values from INPUT-STREAM until the next character after a
524 value's representation is ENDCHAR, and return the objects as a list."
525 (with-reader (recursive-p)
526 (do ((char (flush-whitespace input-stream)
527 (flush-whitespace input-stream))
529 ((char= char endchar) (unless *read-suppress* (nreverse retlist)))
530 (setq retlist (nconc (read-maybe-nothing input-stream char) retlist)))))
532 ;;;; basic readmacro definitions
534 ;;;; Some large, hairy subsets of readmacro definitions (backquotes
535 ;;;; and sharp macros) are not here, but in their own source files.
537 (defun read-quote (stream ignore)
538 (declare (ignore ignore))
539 (list 'quote (read stream t nil t)))
541 (defun read-comment (stream ignore)
542 (declare (ignore ignore))
544 ((character-decoding-error
545 #'(lambda (decoding-error)
546 (declare (ignorable decoding-error))
547 (style-warn "Character decoding error in a ;-comment at position ~A reading source file ~A, resyncing." (file-position stream) stream)
548 (invoke-restart 'attempt-resync))))
549 (let ((stream (in-synonym-of stream)))
550 (if (ansi-stream-p stream)
551 (prepare-for-fast-read-char stream
552 (do ((char (fast-read-char nil nil)
553 (fast-read-char nil nil)))
554 ((or (not char) (char= char #\newline))
555 (done-with-fast-read-char))))
557 (do ((char (read-char stream nil :eof) (read-char stream nil :eof)))
558 ((or (eq char :eof) (char= char #\newline)))))))
559 ;; Don't return anything.
562 (defun read-list (stream ignore)
563 (declare (ignore ignore))
564 (let* ((thelist (list nil))
566 (do ((firstchar (flush-whitespace stream) (flush-whitespace stream)))
567 ((char= firstchar #\) ) (cdr thelist))
568 (when (char= firstchar #\.)
569 (let ((nextchar (read-char stream t)))
570 (cond ((token-delimiterp nextchar)
571 (cond ((eq listtail thelist)
572 (unless *read-suppress*
575 "Nothing appears before . in list.")))
576 ((whitespace[2]p nextchar)
577 (setq nextchar (flush-whitespace stream))))
579 ;; Return list containing last thing.
580 (car (read-after-dot stream nextchar)))
581 (return (cdr thelist)))
582 ;; Put back NEXTCHAR so that we can read it normally.
583 (t (unread-char nextchar stream)))))
584 ;; Next thing is not an isolated dot.
585 (let ((listobj (read-maybe-nothing stream firstchar)))
586 ;; allows the possibility that a comment was read
588 (rplacd listtail listobj)
589 (setq listtail listobj))))))
591 (defun read-after-dot (stream firstchar)
592 ;; FIRSTCHAR is non-whitespace!
594 (do ((char firstchar (flush-whitespace stream)))
597 (return-from read-after-dot nil)
598 (%reader-error stream "Nothing appears after . in list.")))
599 ;; See whether there's something there.
600 (setq lastobj (read-maybe-nothing stream char))
601 (when lastobj (return t)))
602 ;; At least one thing appears after the dot.
603 ;; Check for more than one thing following dot.
604 (do ((lastchar (flush-whitespace stream)
605 (flush-whitespace stream)))
606 ((char= lastchar #\) ) lastobj) ;success!
607 ;; Try reading virtual whitespace.
608 (if (and (read-maybe-nothing stream lastchar)
609 (not *read-suppress*))
610 (%reader-error stream "More than one object follows . in list.")))))
612 (defun read-string (stream closech)
613 ;; This accumulates chars until it sees same char that invoked it.
614 ;; For a very long string, this could end up bloating the read buffer.
616 (let ((stream (in-synonym-of stream)))
617 (if (ansi-stream-p stream)
618 (prepare-for-fast-read-char stream
619 (do ((char (fast-read-char t) (fast-read-char t)))
620 ((char= char closech)
621 (done-with-fast-read-char))
622 (if (single-escape-p char) (setq char (fast-read-char t)))
623 (ouch-read-buffer char)))
625 (do ((char (read-char stream nil :eof) (read-char stream nil :eof)))
626 ((or (eq char :eof) (char= char closech))
628 (error 'end-of-file :stream stream)))
629 (when (single-escape-p char)
630 (setq char (read-char stream nil :eof))
632 (error 'end-of-file :stream stream)))
633 (ouch-read-buffer char))))
634 (read-buffer-to-string))
636 (defun read-right-paren (stream ignore)
637 (declare (ignore ignore))
638 (%reader-error stream "unmatched close parenthesis"))
640 ;;; Read from the stream up to the next delimiter. Leave the resulting
641 ;;; token in *READ-BUFFER*, and return two values:
642 ;;; -- a list of the escaped character positions, and
643 ;;; -- The position of the first package delimiter (or NIL).
644 (defun internal-read-extended-token (stream firstchar escape-firstchar)
647 (when escape-firstchar
648 (push *ouch-ptr* escapes)
649 (ouch-read-buffer firstchar)
650 (setq firstchar (read-char stream nil *eof-object*)))
651 (do ((char firstchar (read-char stream nil *eof-object*))
653 ((cond ((eofp char) t)
654 ((token-delimiterp char)
655 (unread-char char stream)
658 (values escapes colon))
659 (cond ((single-escape-p char)
660 ;; It can't be a number, even if it's 1\23.
661 ;; Read next char here, so it won't be casified.
662 (push *ouch-ptr* escapes)
663 (let ((nextchar (read-char stream nil *eof-object*)))
665 (reader-eof-error stream "after escape character")
666 (ouch-read-buffer nextchar))))
667 ((multiple-escape-p char)
668 ;; Read to next multiple-escape, escaping single chars
671 (let ((ch (read-char stream nil *eof-object*)))
674 (reader-eof-error stream "inside extended token"))
675 ((multiple-escape-p ch) (return))
676 ((single-escape-p ch)
677 (let ((nextchar (read-char stream nil *eof-object*)))
678 (cond ((eofp nextchar)
679 (reader-eof-error stream "after escape character"))
681 (push *ouch-ptr* escapes)
682 (ouch-read-buffer nextchar)))))
684 (push *ouch-ptr* escapes)
685 (ouch-read-buffer ch))))))
687 (when (and (constituentp char)
688 (eql (get-constituent-trait char)
689 +char-attr-package-delimiter+)
691 (setq colon *ouch-ptr*))
692 (ouch-read-buffer char))))))
694 ;;;; character classes
696 ;;; Return the character class for CHAR.
698 ;;; FIXME: why aren't these ATT-getting forms using GET-CAT-ENTRY?
699 ;;; Because we've cached the readtable tables?
700 (defmacro char-class (char attarray atthash)
701 `(let ((att (if (typep ,char 'base-char)
702 (aref ,attarray (char-code ,char))
703 (gethash ,char ,atthash +char-attr-constituent+))))
704 (declare (fixnum att))
706 ((<= att +char-attr-terminating-macro+) +char-attr-delimiter+)
707 ((< att +char-attr-constituent+) att)
708 (t (setf att (get-constituent-trait ,char))
709 (if (= att +char-attr-invalid+)
710 (%reader-error stream "invalid constituent")
713 ;;; Return the character class for CHAR, which might be part of a
715 (defmacro char-class2 (char attarray atthash)
716 `(let ((att (if (typep ,char 'base-char)
717 (aref ,attarray (char-code ,char))
718 (gethash ,char ,atthash +char-attr-constituent+))))
719 (declare (fixnum att))
721 ((<= att +char-attr-terminating-macro+) +char-attr-delimiter+)
722 ((< att +char-attr-constituent+) att)
723 (t (setf att (get-constituent-trait ,char))
725 ((digit-char-p ,char *read-base*) +char-attr-constituent-digit+)
726 ((= att +char-attr-constituent-digit+) +char-attr-constituent+)
727 ((= att +char-attr-invalid+)
728 (%reader-error stream "invalid constituent"))
731 ;;; Return the character class for a char which might be part of a
732 ;;; rational or floating number. (Assume that it is a digit if it
734 (defmacro char-class3 (char attarray atthash)
735 `(let ((att (if (typep ,char 'base-char)
736 (aref ,attarray (char-code ,char))
737 (gethash ,char ,atthash +char-attr-constituent+))))
738 (declare (fixnum att))
740 ((<= att +char-attr-terminating-macro+) +char-attr-delimiter+)
741 ((< att +char-attr-constituent+) att)
742 (t (setf att (get-constituent-trait ,char))
743 (when possibly-rational
744 (setq possibly-rational
745 (or (digit-char-p ,char *read-base*)
746 (= att +char-attr-constituent-slash+))))
749 (or (digit-char-p ,char 10)
750 (= att +char-attr-constituent-dot+))))
752 ((digit-char-p ,char (max *read-base* 10))
753 (if (digit-char-p ,char *read-base*)
754 (if (= att +char-attr-constituent-expt+)
755 +char-attr-constituent-digit-or-expt+
756 +char-attr-constituent-digit+)
757 +char-attr-constituent-decimal-digit+))
758 ((= att +char-attr-invalid+)
759 (%reader-error stream "invalid constituent"))
764 (defvar *read-suppress* nil
766 "Suppress most interpreting in the reader when T.")
768 (defvar *read-base* 10
770 "the radix that Lisp reads numbers in")
771 (declaim (type (integer 2 36) *read-base*))
773 ;;; Modify the read buffer according to READTABLE-CASE, ignoring
774 ;;; ESCAPES. ESCAPES is a list of the escaped indices, in reverse
776 (defun casify-read-buffer (escapes)
777 (let ((case (readtable-case *readtable*)))
779 ((and (null escapes) (eq case :upcase))
780 (dotimes (i *ouch-ptr*)
781 (setf (schar *read-buffer* i)
782 (char-upcase (schar *read-buffer* i)))))
783 ((eq case :preserve))
785 (macrolet ((skip-esc (&body body)
786 `(do ((i (1- *ouch-ptr*) (1- i))
790 (when (or (null escapes)
791 (let ((esc (first escapes)))
792 (declare (fixnum esc))
798 (let ((ch (schar *read-buffer* i)))
801 (skip-esc (setf (schar *read-buffer* i) (char-downcase ch))))
803 (skip-esc (setf (schar *read-buffer* i) (char-upcase ch)))))
806 (:downcase (lower-em))
811 (when (both-case-p ch)
812 (if (upper-case-p ch)
814 (setq all-upper nil))))
815 (cond (all-lower (raise-em))
816 (all-upper (lower-em))))))))))))
818 (defun read-token (stream firstchar)
820 "This function is just an fsm that recognizes numbers and symbols."
821 ;; Check explicitly whether FIRSTCHAR has an entry for
822 ;; NON-TERMINATING in CHARACTER-ATTRIBUTE-TABLE and
823 ;; READ-DOT-NUMBER-SYMBOL in CMT. Report an error if these are
824 ;; violated. (If we called this, we want something that is a
825 ;; legitimate token!) Read in the longest possible string satisfying
826 ;; the Backus-Naur form for "unqualified-token". Leave the result in
827 ;; the *READ-BUFFER*. Return next char after token (last char read).
828 (when *read-suppress*
829 (internal-read-extended-token stream firstchar nil)
830 (return-from read-token nil))
831 (let ((attribute-array (character-attribute-array *readtable*))
832 (attribute-hash-table (character-attribute-hash-table *readtable*))
833 (package-designator nil)
835 (possibly-rational t)
836 (seen-digit-or-expt nil)
838 (was-possibly-float nil)
840 (seen-multiple-escapes nil))
842 (prog ((char firstchar))
843 (case (char-class3 char attribute-array attribute-hash-table)
844 (#.+char-attr-constituent-sign+ (go SIGN))
845 (#.+char-attr-constituent-digit+ (go LEFTDIGIT))
846 (#.+char-attr-constituent-digit-or-expt+
847 (setq seen-digit-or-expt t)
849 (#.+char-attr-constituent-decimal-digit+ (go LEFTDECIMALDIGIT))
850 (#.+char-attr-constituent-dot+ (go FRONTDOT))
851 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
852 (#.+char-attr-package-delimiter+ (go COLON))
853 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
854 (#.+char-attr-invalid+ (%reader-error stream "invalid constituent"))
855 ;; can't have eof, whitespace, or terminating macro as first char!
858 (ouch-read-buffer char)
859 (setq char (read-char stream nil nil))
860 (unless char (go RETURN-SYMBOL))
861 (setq possibly-rational t
863 (case (char-class3 char attribute-array attribute-hash-table)
864 (#.+char-attr-constituent-digit+ (go LEFTDIGIT))
865 (#.+char-attr-constituent-digit-or-expt+
866 (setq seen-digit-or-expt t)
868 (#.+char-attr-constituent-decimal-digit+ (go LEFTDECIMALDIGIT))
869 (#.+char-attr-constituent-dot+ (go SIGNDOT))
870 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
871 (#.+char-attr-package-delimiter+ (go COLON))
872 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
873 (#.+char-attr-delimiter+ (unread-char char stream) (go RETURN-SYMBOL))
875 LEFTDIGIT ; saw "[sign] {digit}+"
876 (ouch-read-buffer char)
877 (setq char (read-char stream nil nil))
878 (unless char (return (make-integer)))
879 (setq was-possibly-float possibly-float)
880 (case (char-class3 char attribute-array attribute-hash-table)
881 (#.+char-attr-constituent-digit+ (go LEFTDIGIT))
882 (#.+char-attr-constituent-decimal-digit+ (if possibly-float
883 (go LEFTDECIMALDIGIT)
885 (#.+char-attr-constituent-dot+ (if possibly-float
888 (#.+char-attr-constituent-digit-or-expt+
889 (if (or seen-digit-or-expt (not was-possibly-float))
890 (progn (setq seen-digit-or-expt t) (go LEFTDIGIT))
891 (progn (setq seen-digit-or-expt t) (go LEFTDIGIT-OR-EXPT))))
892 (#.+char-attr-constituent-expt+
893 (if was-possibly-float
896 (#.+char-attr-constituent-slash+ (if possibly-rational
899 (#.+char-attr-delimiter+ (unread-char char stream)
900 (return (make-integer)))
901 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
902 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
903 (#.+char-attr-package-delimiter+ (go COLON))
906 (ouch-read-buffer char)
907 (setq char (read-char stream nil nil))
908 (unless char (return (make-integer)))
909 (case (char-class3 char attribute-array attribute-hash-table)
910 (#.+char-attr-constituent-digit+ (go LEFTDIGIT))
911 (#.+char-attr-constituent-decimal-digit+ (bug "impossible!"))
912 (#.+char-attr-constituent-dot+ (go SYMBOL))
913 (#.+char-attr-constituent-digit-or-expt+ (go LEFTDIGIT))
914 (#.+char-attr-constituent-expt+ (go SYMBOL))
915 (#.+char-attr-constituent-sign+ (go EXPTSIGN))
916 (#.+char-attr-constituent-slash+ (if possibly-rational
919 (#.+char-attr-delimiter+ (unread-char char stream)
920 (return (make-integer)))
921 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
922 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
923 (#.+char-attr-package-delimiter+ (go COLON))
925 LEFTDECIMALDIGIT ; saw "[sign] {decimal-digit}+"
926 (aver possibly-float)
927 (ouch-read-buffer char)
928 (setq char (read-char stream nil nil))
929 (unless char (go RETURN-SYMBOL))
930 (case (char-class char attribute-array attribute-hash-table)
931 (#.+char-attr-constituent-digit+ (go LEFTDECIMALDIGIT))
932 (#.+char-attr-constituent-dot+ (go MIDDLEDOT))
933 (#.+char-attr-constituent-expt+ (go EXPONENT))
934 (#.+char-attr-constituent-slash+ (aver (not possibly-rational))
936 (#.+char-attr-delimiter+ (unread-char char stream)
938 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
939 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
940 (#.+char-attr-package-delimiter+ (go COLON))
942 MIDDLEDOT ; saw "[sign] {digit}+ dot"
943 (ouch-read-buffer char)
944 (setq char (read-char stream nil nil))
945 (unless char (return (let ((*read-base* 10))
947 (case (char-class char attribute-array attribute-hash-table)
948 (#.+char-attr-constituent-digit+ (go RIGHTDIGIT))
949 (#.+char-attr-constituent-expt+ (go EXPONENT))
950 (#.+char-attr-delimiter+
951 (unread-char char stream)
952 (return (let ((*read-base* 10))
954 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
955 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
956 (#.+char-attr-package-delimiter+ (go COLON))
958 RIGHTDIGIT ; saw "[sign] {decimal-digit}* dot {digit}+"
959 (ouch-read-buffer char)
960 (setq char (read-char stream nil nil))
961 (unless char (return (make-float stream)))
962 (case (char-class char attribute-array attribute-hash-table)
963 (#.+char-attr-constituent-digit+ (go RIGHTDIGIT))
964 (#.+char-attr-constituent-expt+ (go EXPONENT))
965 (#.+char-attr-delimiter+
966 (unread-char char stream)
967 (return (make-float stream)))
968 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
969 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
970 (#.+char-attr-package-delimiter+ (go COLON))
972 SIGNDOT ; saw "[sign] dot"
973 (ouch-read-buffer char)
974 (setq char (read-char stream nil nil))
975 (unless char (go RETURN-SYMBOL))
976 (case (char-class char attribute-array attribute-hash-table)
977 (#.+char-attr-constituent-digit+ (go RIGHTDIGIT))
978 (#.+char-attr-delimiter+ (unread-char char stream) (go RETURN-SYMBOL))
979 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
980 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
983 (ouch-read-buffer char)
984 (setq char (read-char stream nil nil))
985 (unless char (%reader-error stream "dot context error"))
986 (case (char-class char attribute-array attribute-hash-table)
987 (#.+char-attr-constituent-digit+ (go RIGHTDIGIT))
988 (#.+char-attr-constituent-dot+ (go DOTS))
989 (#.+char-attr-delimiter+ (%reader-error stream "dot context error"))
990 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
991 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
992 (#.+char-attr-package-delimiter+ (go COLON))
995 (ouch-read-buffer char)
996 (setq char (read-char stream nil nil))
997 (unless char (go RETURN-SYMBOL))
998 (setq possibly-float t)
999 (case (char-class char attribute-array attribute-hash-table)
1000 (#.+char-attr-constituent-sign+ (go EXPTSIGN))
1001 (#.+char-attr-constituent-digit+ (go EXPTDIGIT))
1002 (#.+char-attr-delimiter+ (unread-char char stream) (go RETURN-SYMBOL))
1003 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1004 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1005 (#.+char-attr-package-delimiter+ (go COLON))
1007 EXPTSIGN ; got to EXPONENT, and saw a sign character
1008 (ouch-read-buffer char)
1009 (setq char (read-char stream nil nil))
1010 (unless char (go RETURN-SYMBOL))
1011 (case (char-class char attribute-array attribute-hash-table)
1012 (#.+char-attr-constituent-digit+ (go EXPTDIGIT))
1013 (#.+char-attr-delimiter+ (unread-char char stream) (go RETURN-SYMBOL))
1014 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1015 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1016 (#.+char-attr-package-delimiter+ (go COLON))
1018 EXPTDIGIT ; got to EXPONENT, saw "[sign] {digit}+"
1019 (ouch-read-buffer char)
1020 (setq char (read-char stream nil nil))
1021 (unless char (return (make-float stream)))
1022 (case (char-class char attribute-array attribute-hash-table)
1023 (#.+char-attr-constituent-digit+ (go EXPTDIGIT))
1024 (#.+char-attr-delimiter+
1025 (unread-char char stream)
1026 (return (make-float stream)))
1027 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1028 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1029 (#.+char-attr-package-delimiter+ (go COLON))
1031 RATIO ; saw "[sign] {digit}+ slash"
1032 (ouch-read-buffer char)
1033 (setq char (read-char stream nil nil))
1034 (unless char (go RETURN-SYMBOL))
1035 (case (char-class2 char attribute-array attribute-hash-table)
1036 (#.+char-attr-constituent-digit+ (go RATIODIGIT))
1037 (#.+char-attr-delimiter+ (unread-char char stream) (go RETURN-SYMBOL))
1038 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1039 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1040 (#.+char-attr-package-delimiter+ (go COLON))
1042 RATIODIGIT ; saw "[sign] {digit}+ slash {digit}+"
1043 (ouch-read-buffer char)
1044 (setq char (read-char stream nil nil))
1045 (unless char (return (make-ratio stream)))
1046 (case (char-class2 char attribute-array attribute-hash-table)
1047 (#.+char-attr-constituent-digit+ (go RATIODIGIT))
1048 (#.+char-attr-delimiter+
1049 (unread-char char stream)
1050 (return (make-ratio stream)))
1051 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1052 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1053 (#.+char-attr-package-delimiter+ (go COLON))
1055 DOTS ; saw "dot {dot}+"
1056 (ouch-read-buffer char)
1057 (setq char (read-char stream nil nil))
1058 (unless char (%reader-error stream "too many dots"))
1059 (case (char-class char attribute-array attribute-hash-table)
1060 (#.+char-attr-constituent-dot+ (go DOTS))
1061 (#.+char-attr-delimiter+
1062 (unread-char char stream)
1063 (%reader-error stream "too many dots"))
1064 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1065 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1066 (#.+char-attr-package-delimiter+ (go COLON))
1068 SYMBOL ; not a dot, dots, or number
1069 (let ((stream (in-synonym-of stream)))
1070 (if (ansi-stream-p stream)
1071 (prepare-for-fast-read-char stream
1074 (ouch-read-buffer char)
1075 (setq char (fast-read-char nil nil))
1076 (unless char (go RETURN-SYMBOL))
1077 (case (char-class char attribute-array attribute-hash-table)
1078 (#.+char-attr-single-escape+ (done-with-fast-read-char)
1080 (#.+char-attr-delimiter+ (done-with-fast-read-char)
1081 (unread-char char stream)
1083 (#.+char-attr-multiple-escape+ (done-with-fast-read-char)
1085 (#.+char-attr-package-delimiter+ (done-with-fast-read-char)
1087 (t (go SYMBOL-LOOP)))))
1091 (ouch-read-buffer char)
1092 (setq char (read-char stream nil :eof))
1093 (when (eq char :eof) (go RETURN-SYMBOL))
1094 (case (char-class char attribute-array attribute-hash-table)
1095 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1096 (#.+char-attr-delimiter+ (unread-char char stream)
1098 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1099 (#.+char-attr-package-delimiter+ (go COLON))
1100 (t (go SYMBOL-LOOP))))))
1101 SINGLE-ESCAPE ; saw a single-escape
1102 ;; Don't put the escape character in the read buffer.
1103 ;; READ-NEXT CHAR, put in buffer (no case conversion).
1104 (let ((nextchar (read-char stream nil nil)))
1106 (reader-eof-error stream "after single-escape character"))
1107 (push *ouch-ptr* escapes)
1108 (ouch-read-buffer nextchar))
1109 (setq char (read-char stream nil nil))
1110 (unless char (go RETURN-SYMBOL))
1111 (case (char-class char attribute-array attribute-hash-table)
1112 (#.+char-attr-delimiter+ (unread-char char stream) (go RETURN-SYMBOL))
1113 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1114 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1115 (#.+char-attr-package-delimiter+ (go COLON))
1118 (setq seen-multiple-escapes t)
1119 (do ((char (read-char stream t) (read-char stream t)))
1120 ((multiple-escape-p char))
1121 (if (single-escape-p char) (setq char (read-char stream t)))
1122 (push *ouch-ptr* escapes)
1123 (ouch-read-buffer char))
1124 (setq char (read-char stream nil nil))
1125 (unless char (go RETURN-SYMBOL))
1126 (case (char-class char attribute-array attribute-hash-table)
1127 (#.+char-attr-delimiter+ (unread-char char stream) (go RETURN-SYMBOL))
1128 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1129 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1130 (#.+char-attr-package-delimiter+ (go COLON))
1133 (casify-read-buffer escapes)
1134 (unless (zerop colons)
1135 (%reader-error stream "too many colons in ~S"
1136 (read-buffer-to-string)))
1138 (setq package-designator
1139 (if (plusp *ouch-ptr*)
1140 ;; FIXME: It seems inefficient to cons up a package
1141 ;; designator string every time we read a symbol with an
1142 ;; explicit package prefix. Perhaps we could implement
1143 ;; a FIND-PACKAGE* function analogous to INTERN*
1145 (read-buffer-to-string)
1146 (if seen-multiple-escapes
1147 (read-buffer-to-string)
1148 *keyword-package*)))
1151 (setq char (read-char stream nil nil))
1152 (unless char (reader-eof-error stream "after reading a colon"))
1153 (case (char-class char attribute-array attribute-hash-table)
1154 (#.+char-attr-delimiter+
1155 (unread-char char stream)
1156 (%reader-error stream
1157 "illegal terminating character after a colon: ~S"
1159 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1160 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1161 (#.+char-attr-package-delimiter+ (go INTERN))
1165 (setq char (read-char stream nil nil))
1167 (reader-eof-error stream "after reading a colon"))
1168 (case (char-class char attribute-array attribute-hash-table)
1169 (#.+char-attr-delimiter+
1170 (unread-char char stream)
1171 (%reader-error stream
1172 "illegal terminating character after a colon: ~S"
1174 (#.+char-attr-single-escape+ (go SINGLE-ESCAPE))
1175 (#.+char-attr-multiple-escape+ (go MULT-ESCAPE))
1176 (#.+char-attr-package-delimiter+
1177 (%reader-error stream
1178 "too many colons after ~S name"
1179 package-designator))
1182 (casify-read-buffer escapes)
1183 (let ((found (if package-designator
1184 (find-package package-designator)
1187 (error 'reader-package-error :stream stream
1188 :format-arguments (list package-designator)
1189 :format-control "package ~S not found"))
1191 (if (or (zerop colons) (= colons 2) (eq found *keyword-package*))
1192 (return (intern* *read-buffer* *ouch-ptr* found))
1193 (multiple-value-bind (symbol test)
1194 (find-symbol* *read-buffer* *ouch-ptr* found)
1195 (when (eq test :external) (return symbol))
1196 (let ((name (read-buffer-to-string)))
1197 (with-simple-restart (continue "Use symbol anyway.")
1198 (error 'reader-package-error :stream stream
1199 :format-arguments (list name (package-name found))
1202 "The symbol ~S is not external in the ~A package."
1203 "Symbol ~S not found in the ~A package.")))
1204 (return (intern name found)))))))))
1206 ;;; for semi-external use:
1208 ;;; For semi-external use: Return 3 values: the string for the token,
1209 ;;; a flag for whether there was an escape char, and the position of
1210 ;;; any package delimiter.
1211 (defun read-extended-token (stream &optional (*readtable* *readtable*))
1212 (let ((first-char (read-char stream nil nil t)))
1214 (multiple-value-bind (escapes colon)
1215 (internal-read-extended-token stream first-char nil)
1216 (casify-read-buffer escapes)
1217 (values (read-buffer-to-string) (not (null escapes)) colon)))
1219 (values "" nil nil)))))
1221 ;;; for semi-external use:
1223 ;;; Read an extended token with the first character escaped. Return
1224 ;;; the string for the token.
1225 (defun read-extended-token-escaped (stream &optional (*readtable* *readtable*))
1226 (let ((first-char (read-char stream nil nil)))
1228 (let ((escapes (internal-read-extended-token stream first-char t)))
1229 (casify-read-buffer escapes)
1230 (read-buffer-to-string)))
1232 (reader-eof-error stream "after escape")))))
1234 ;;;; number-reading functions
1236 (defmacro digit* nil
1237 `(do ((ch char (inch-read-buffer)))
1238 ((or (eofp ch) (not (digit-char-p ch))) (setq char ch))
1239 ;; Report if at least one digit is seen.
1240 (setq one-digit t)))
1242 (defmacro exponent-letterp (letter)
1243 `(memq ,letter '(#\E #\S #\F #\L #\D #\e #\s #\f #\l #\d)))
1245 ;;; FIXME: It would be cleaner to have these generated automatically
1246 ;;; by compile-time code instead of having them hand-created like
1247 ;;; this. The !COLD-INIT-INTEGER-READER code below should be resurrected
1249 (defvar *integer-reader-safe-digits*
1251 26 17 13 11 10 9 8 8 8 7 7 7 7 6 6 6 6 6 6 6 6 5 5 5 5 5 5 5 5 5 5 5 5 5 5)
1253 "the mapping of base to 'safe' number of digits to read for a fixnum")
1254 (defvar *integer-reader-base-power*
1256 67108864 129140163 67108864 48828125 60466176 40353607
1257 16777216 43046721 100000000 19487171 35831808 62748517 105413504 11390625
1258 16777216 24137569 34012224 47045881 64000000 85766121 113379904 6436343
1259 7962624 9765625 11881376 14348907 17210368 20511149 24300000 28629151
1260 33554432 39135393 45435424 52521875 60466176)
1262 "the largest fixnum power of the base for MAKE-INTEGER")
1263 (declaim (simple-vector *integer-reader-safe-digits*
1264 *integer-reader-base-power*))
1266 (defun !cold-init-integer-reader ()
1267 (do ((base 2 (1+ base)))
1270 (do ((fix (truncate most-positive-fixnum base)
1271 (truncate fix base))
1272 (digits 0 (1+ digits)))
1273 ((zerop fix) digits))))
1274 (setf (aref *integer-reader-safe-digits* base)
1276 (aref *integer-reader-base-power* base)
1277 (expt base digits)))))
1280 (defun make-integer ()
1282 "Minimizes bignum-fixnum multiplies by reading a 'safe' number of digits,
1283 then multiplying by a power of the base and adding."
1284 (let* ((base *read-base*)
1285 (digits-per (aref *integer-reader-safe-digits* base))
1286 (base-power (aref *integer-reader-base-power* base))
1289 (declare (type index digits-per base-power))
1290 (read-unwind-read-buffer)
1291 (let ((char (inch-read-buffer)))
1292 (cond ((char= char #\-)
1295 (t (unread-buffer))))
1298 (declare (type index num))
1299 (dotimes (digit digits-per)
1300 (let* ((ch (inch-read-buffer)))
1301 (cond ((or (eofp ch) (char= ch #\.))
1302 (return-from make-integer
1304 (if (zerop number) num
1306 (expt base digit))))))
1307 (if negativep (- res) res))))
1308 (t (setq num (+ (digit-char-p ch base)
1309 (the index (* num base))))))))
1310 (setq number (+ num (* number base-power)))))))
1312 (defun make-float (stream)
1313 ;; Assume that the contents of *read-buffer* are a legal float, with nothing
1315 (read-unwind-read-buffer)
1316 (let ((negative-fraction nil)
1319 (negative-exponent nil)
1322 (char (inch-read-buffer)))
1323 (if (cond ((char= char #\+) t)
1324 ((char= char #\-) (setq negative-fraction t)))
1326 (setq char (inch-read-buffer)))
1327 ;; Read digits before the dot.
1328 (do* ((ch char (inch-read-buffer))
1329 (dig (digit-char-p ch) (digit-char-p ch)))
1330 ((not dig) (setq char ch))
1331 (setq number (+ (* number 10) dig)))
1332 ;; Deal with the dot, if it's there.
1333 (when (char= char #\.)
1334 (setq char (inch-read-buffer))
1335 ;; Read digits after the dot.
1336 (do* ((ch char (inch-read-buffer))
1337 (dig (and (not (eofp ch)) (digit-char-p ch))
1338 (and (not (eofp ch)) (digit-char-p ch))))
1339 ((not dig) (setq char ch))
1340 (setq divisor (* divisor 10))
1341 (setq number (+ (* number 10) dig))))
1342 ;; Is there an exponent letter?
1344 ;; If not, we've read the whole number.
1345 (let ((num (make-float-aux number divisor
1346 *read-default-float-format*
1348 (return-from make-float (if negative-fraction (- num) num))))
1349 ((exponent-letterp char)
1350 (setq float-char char)
1352 (setq char (inch-read-buffer))
1353 ;; Check leading sign.
1354 (if (cond ((char= char #\+) t)
1355 ((char= char #\-) (setq negative-exponent t)))
1357 (setq char (inch-read-buffer)))
1358 ;; Read digits for exponent.
1359 (do* ((ch char (inch-read-buffer))
1360 (dig (and (not (eofp ch)) (digit-char-p ch))
1361 (and (not (eofp ch)) (digit-char-p ch))))
1363 (setq exponent (if negative-exponent (- exponent) exponent)))
1364 (setq exponent (+ (* exponent 10) dig)))
1365 ;; Generate and return the float, depending on FLOAT-CHAR:
1366 (let* ((float-format (case (char-upcase float-char)
1367 (#\E *read-default-float-format*)
1372 (result (make-float-aux (* (expt 10 exponent) number)
1373 divisor float-format stream)))
1374 (return-from make-float
1375 (if negative-fraction (- result) result))))
1376 (t (bug "bad fallthrough in floating point reader")))))
1378 (defun make-float-aux (number divisor float-format stream)
1380 (coerce (/ number divisor) float-format)
1382 (error 'reader-impossible-number-error
1383 :error c :stream stream
1384 :format-control "failed to build float"))))
1386 (defun make-ratio (stream)
1387 ;; Assume *READ-BUFFER* contains a legal ratio. Build the number from
1390 ;; Look for optional "+" or "-".
1391 (let ((numerator 0) (denominator 0) (char ()) (negative-number nil))
1392 (read-unwind-read-buffer)
1393 (setq char (inch-read-buffer))
1394 (cond ((char= char #\+)
1395 (setq char (inch-read-buffer)))
1397 (setq char (inch-read-buffer))
1398 (setq negative-number t)))
1400 (do* ((ch char (inch-read-buffer))
1401 (dig (digit-char-p ch *read-base*)
1402 (digit-char-p ch *read-base*)))
1404 (setq numerator (+ (* numerator *read-base*) dig)))
1406 (do* ((ch (inch-read-buffer) (inch-read-buffer))
1408 ((or (eofp ch) (not (setq dig (digit-char-p ch *read-base*)))))
1409 (setq denominator (+ (* denominator *read-base*) dig)))
1410 (let ((num (handler-case
1411 (/ numerator denominator)
1412 (arithmetic-error (c)
1413 (error 'reader-impossible-number-error
1414 :error c :stream stream
1415 :format-control "failed to build ratio")))))
1416 (if negative-number (- num) num))))
1418 ;;;; cruft for dispatch macros
1420 (defun make-char-dispatch-table ()
1423 (defun dispatch-char-error (stream sub-char ignore)
1424 (declare (ignore ignore))
1427 (%reader-error stream "no dispatch function defined for ~S" sub-char)))
1429 (defun make-dispatch-macro-character (char &optional
1430 (non-terminating-p nil)
1433 "Cause CHAR to become a dispatching macro character in readtable (which
1434 defaults to the current readtable). If NON-TERMINATING-P, the char will
1435 be non-terminating."
1436 (set-macro-character char #'read-dispatch-char non-terminating-p rt)
1437 (let* ((dalist (dispatch-tables rt))
1438 (dtable (cdr (find char dalist :test #'char= :key #'car))))
1440 (error "The dispatch character ~S already exists." char))
1442 (setf (dispatch-tables rt)
1443 (push (cons char (make-char-dispatch-table)) dalist)))))
1446 (defun set-dispatch-macro-character (disp-char sub-char function
1447 &optional (rt *readtable*))
1449 "Cause FUNCTION to be called whenever the reader reads DISP-CHAR
1450 followed by SUB-CHAR."
1451 ;; Get the dispatch char for macro (error if not there), diddle
1452 ;; entry for sub-char.
1453 (when (digit-char-p sub-char)
1454 (error "SUB-CHAR must not be a decimal digit: ~S" sub-char))
1455 (let* ((sub-char (char-upcase sub-char))
1456 (rt (or rt *standard-readtable*))
1457 (dpair (find disp-char (dispatch-tables rt)
1458 :test #'char= :key #'car)))
1460 (setf (gethash sub-char (cdr dpair)) (coerce function 'function))
1461 (error "~S is not a dispatch char." disp-char))))
1463 (defun get-dispatch-macro-character (disp-char sub-char
1464 &optional (rt *readtable*))
1466 "Return the macro character function for SUB-CHAR under DISP-CHAR
1467 or NIL if there is no associated function."
1468 (let* ((sub-char (char-upcase sub-char))
1469 (rt (or rt *standard-readtable*))
1470 (dpair (find disp-char (dispatch-tables rt)
1471 :test #'char= :key #'car)))
1473 (values (gethash sub-char (cdr dpair)))
1474 (error "~S is not a dispatch char." disp-char))))
1476 (defun read-dispatch-char (stream char)
1477 ;; Read some digits.
1481 (do* ((ch (read-char stream nil *eof-object*)
1482 (read-char stream nil *eof-object*))
1485 (not (setq dig (digit-char-p ch))))
1486 ;; Take care of the extra char.
1488 (reader-eof-error stream "inside dispatch character")
1489 (setq sub-char (char-upcase ch))))
1491 (setq numarg (+ (* numarg 10) dig)))
1492 ;; Look up the function and call it.
1493 (let ((dpair (find char (dispatch-tables *readtable*)
1494 :test #'char= :key #'car)))
1496 (funcall (the function
1497 (gethash sub-char (cdr dpair) #'dispatch-char-error))
1498 stream sub-char (if numargp numarg nil))
1499 (%reader-error stream "no dispatch table for dispatch char")))))
1501 ;;;; READ-FROM-STRING
1503 (defun read-from-string (string &optional (eof-error-p t) eof-value
1505 preserve-whitespace)
1507 "The characters of string are successively given to the lisp reader
1508 and the lisp object built by the reader is returned. Macro chars
1510 (declare (string string))
1511 (with-array-data ((string string :offset-var offset)
1513 (end (%check-vector-sequence-bounds string start end)))
1514 (let ((stream (make-string-input-stream string start end)))
1515 (values (if preserve-whitespace
1516 (read-preserving-whitespace stream eof-error-p eof-value)
1517 (read stream eof-error-p eof-value))
1518 (- (string-input-stream-current stream) offset)))))
1522 (defun parse-integer (string &key (start 0) end (radix 10) junk-allowed)
1524 "Examine the substring of string delimited by start and end
1525 (default to the beginning and end of the string) It skips over
1526 whitespace characters and then tries to parse an integer. The
1527 radix parameter must be between 2 and 36."
1528 (macrolet ((parse-error (format-control)
1529 `(error 'simple-parse-error
1530 :format-control ,format-control
1531 :format-arguments (list string))))
1532 (with-array-data ((string string :offset-var offset)
1534 (end (%check-vector-sequence-bounds string start end)))
1535 (let ((index (do ((i start (1+ i)))
1538 (return-from parse-integer (values nil end))
1539 (parse-error "no non-whitespace characters in string ~S.")))
1540 (declare (fixnum i))
1541 (unless (whitespace[1]p (char string i)) (return i))))
1545 (declare (fixnum index))
1546 (let ((char (char string index)))
1547 (cond ((char= char #\-)
1553 (when (= index end) (return nil))
1554 (let* ((char (char string index))
1555 (weight (digit-char-p char radix)))
1557 (setq result (+ weight (* result radix))
1559 (junk-allowed (return nil))
1560 ((whitespace[1]p char)
1563 (when (= index end) (return))
1564 (unless (whitespace[1]p (char string index))
1565 (parse-error "junk in string ~S")))
1568 (parse-error "junk in string ~S"))))
1572 (if minusp (- result) result)
1575 (parse-error "no digits in string ~S")))
1576 (- index offset))))))
1578 ;;;; reader initialization code
1580 (defun !reader-cold-init ()
1581 (!cold-init-constituent-trait-table)
1582 (!cold-init-standard-readtable)
1583 ;; FIXME: This was commented out, but should probably be restored.
1584 #+nil (!cold-init-integer-reader))
1586 (def!method print-object ((readtable readtable) stream)
1587 (print-unreadable-object (readtable stream :identity t :type t)))