File: reader.impure.lisp

package info (click to toggle)
sbcl 1%3A0.9.16.0-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 19,960 kB
  • ctags: 16,537
  • sloc: lisp: 231,164; ansic: 19,558; asm: 2,539; sh: 1,925; makefile: 308
file content (112 lines) | stat: -rw-r--r-- 3,982 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
;;;; tests related to the Lisp reader

;;;; This file is impure because we want to modify the readtable and stuff.

;;;; This software is part of the SBCL system. See the README file for
;;;; more information.
;;;;
;;;; While most of SBCL is derived from the CMU CL system, the test
;;;; files (like this one) were written from scratch after the fork
;;;; from CMU CL.
;;;;
;;;; This software is in the public domain and is provided with
;;;; absolutely no warranty. See the COPYING and CREDITS files for
;;;; more information.

(load "assertoid.lisp")
(use-package "ASSERTOID")

;;; Bug 30, involving mistakes in binding the read table, made this
;;; code fail.
(defun read-vector (stream char)
  (declare (ignorable char))
  (coerce (read-delimited-list #\] stream t) 'vector))
(set-macro-character #\[ #'read-vector nil)
(set-macro-character #\] (get-macro-character #\)) nil)
(multiple-value-bind (res pos)
    (read-from-string "[1 2 3]") ; ==> #(1 2 3), 7
  (assert (equalp res #(1 2 3)))
  (assert (= pos 7)))
(multiple-value-bind (res pos)
    (read-from-string "#\\x") ; ==> #\x, 3
  (assert (equalp res #\x))
  (assert (= pos 3)))
(multiple-value-bind (res pos)
    (read-from-string "[#\\x]")
  (assert (equalp res #(#\x)))
  (assert (= pos 5)))

;;; Bug 51b. (try to throw READER-ERRORs when the reader encounters
;;; dubious input)
(assert (raises-error? (read-from-string "1e1000") reader-error))
(assert (raises-error? (read-from-string "1/0") reader-error))

;;; Bug reported by Antonio Martinez on comp.lang.lisp 2003-02-03 in
;;; message <b32da960.0302030640.7d6fc610@posting.google.com>: reading
;;; circular instances of CLOS classes didn't work:
(defclass box ()
  ((value :initarg :value :reader value)))
(defun read-box (stream char)
  (declare (ignore char))
  (let ((objects (read-delimited-list #\] stream t)))
    (unless (= 1 (length objects))
      (error "Unknown box reader syntax"))
    (make-instance 'box :value (first objects))))
(set-macro-character #\[ 'read-box)
(set-syntax-from-char #\] #\))
(multiple-value-bind (res pos)
    (read-from-string "#1=[#1#]")
  (assert (eq (value res) res))
  (assert (= pos 8)))

;;; CSR managed to break the #S reader macro in the process of merging
;;; SB-PCL:CLASS and CL:CLASS -- make sure it works
(defstruct readable-struct a)
(macrolet
    ((frob (string)
       `(assert (eq (readable-struct-a (read-from-string ,string)) t))))
  (frob "#S(READABLE-STRUCT :A T)")
  (frob "#S(READABLE-STRUCT A T)")
  (frob "#S(READABLE-STRUCT \"A\" T)")
  (frob "#S(READABLE-STRUCT #\\A T)")
  (frob "#S(READABLE-STRUCT #\\A T :A NIL)"))
(macrolet
    ((frob (string)
       `(assert (raises-error? (read-from-string ,string) reader-error))))
  (frob "#S(READABLE-STRUCT . :A)")
  (frob "#S(READABLE-STRUCT :A . T)")
  (frob "#S(READABLE-STRUCT :A T . :A)")
  (frob "#S(READABLE-STRUCT :A T :A . T)"))

;;; reported by Henrik Motakef
(defpackage "")
(assert (eq (symbol-package (read-from-string "||::FOO"))
            (find-package "")))

;;; test nested reads, test case by Helmut Eller for cmucl
(defclass my-in-stream (sb-gray:fundamental-character-input-stream)
  ((last-char :initarg :last-char)))

(let ((string " a ")
      (i 0))
  (defmethod sb-gray:stream-read-char ((s my-in-stream))
    (with-input-from-string (s "b") (read s))
    (with-slots (last-char) s
      (cond (last-char (prog1 last-char (setf last-char nil)))
             (t (prog1 (aref string i)
                  (setq i (mod (1+ i) (length string)))))))))

(defmethod sb-gray:stream-unread-char ((s my-in-stream) char)
  (setf (slot-value s 'last-char) char)
  nil)

(assert (eq 'a (read (make-instance 'my-in-stream :last-char nil))))

;;; NIL as the last argument to SET-SYNTAX-FROM-CHAR in compiled code,
;;; reported by Levente Mszros
(let ((fun (compile nil '(lambda ()
                          (set-syntax-from-char #\{ #\( *readtable* nil)))))
  (funcall fun)
  (assert (equal '(:ok) (read-from-string "{:ok)"))))

;;; success