Commit | Line | Data |
---|---|---|
7b041912 | 1 | ;;;; reader.test --- Reader test. -*- coding: iso-8859-1; mode: scheme -*- |
7337d56d | 2 | ;;;; |
684d664e | 3 | ;;;; Copyright (C) 1999, 2001, 2002, 2003, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc. |
7337d56d LC |
4 | ;;;; Jim Blandy <jimb@red-bean.com> |
5 | ;;;; | |
6 | ;;;; This library is free software; you can redistribute it and/or | |
7 | ;;;; modify it under the terms of the GNU Lesser General Public | |
8 | ;;;; License as published by the Free Software Foundation; either | |
53befeb7 | 9 | ;;;; version 3 of the License, or (at your option) any later version. |
2e85d145 | 10 | ;;;; |
7337d56d LC |
11 | ;;;; This library is distributed in the hope that it will be useful, |
12 | ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | ;;;; Lesser General Public License for more details. | |
2e85d145 | 15 | ;;;; |
7337d56d LC |
16 | ;;;; You should have received a copy of the GNU Lesser General Public |
17 | ;;;; License along with this library; if not, write to the Free Software | |
18 | ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
19 | ||
20 | (define-module (test-suite reader) | |
620c8965 | 21 | :use-module (srfi srfi-1) |
7337d56d LC |
22 | :use-module (test-suite lib)) |
23 | ||
0c76ebbd | 24 | |
ef9709da | 25 | (define exception:eof |
ba1b2226 | 26 | (cons 'read-error "end of file$")) |
ef9709da | 27 | (define exception:unexpected-rparen |
ba1b2226 | 28 | (cons 'read-error "unexpected \")\"$")) |
289c3a61 LC |
29 | (define exception:unexpected-rsqbracket |
30 | (cons 'read-error "unexpected \"]\"$")) | |
7337d56d | 31 | (define exception:unterminated-block-comment |
620c8965 | 32 | (cons 'read-error "unterminated `#. \\.\\.\\. .#' comment$")) |
7337d56d LC |
33 | (define exception:unknown-character-name |
34 | (cons 'read-error "unknown character name .*$")) | |
35 | (define exception:unknown-sharp-object | |
36 | (cons 'read-error "Unknown # object: .*$")) | |
37 | (define exception:eof-in-string | |
38 | (cons 'read-error "end of file in string constant$")) | |
d9527cfa AW |
39 | (define exception:eof-in-symbol |
40 | (cons 'read-error "end of file while reading symbol$")) | |
7337d56d LC |
41 | (define exception:illegal-escape |
42 | (cons 'read-error "illegal character in escape sequence: .*$")) | |
6ed0c41a AW |
43 | (define exception:missing-expression |
44 | (cons 'read-error "no expression after #;")) | |
5b69315e AW |
45 | (define exception:mismatched-paren |
46 | (cons 'read-error "mismatched close paren")) | |
7337d56d | 47 | |
ef9709da | 48 | |
6b4113af | 49 | (define (read-string s) |
7b041912 LC |
50 | (with-fluids ((%default-port-encoding #f)) |
51 | (with-input-from-string s (lambda () (read))))) | |
0c76ebbd | 52 | |
7337d56d LC |
53 | (define (with-read-options opts thunk) |
54 | (let ((saved-options (read-options))) | |
55 | (dynamic-wind | |
56 | (lambda () | |
57 | (read-options opts)) | |
58 | thunk | |
59 | (lambda () | |
60 | (read-options saved-options))))) | |
61 | ||
62 | \f | |
6b4113af DH |
63 | (with-test-prefix "reading" |
64 | (pass-if "0" | |
65 | (equal? (read-string "0") 0)) | |
66 | (pass-if "1++i" | |
67 | (equal? (read-string "1++i") '1++i)) | |
68 | (pass-if "1+i+i" | |
69 | (equal? (read-string "1+i+i") '1+i+i)) | |
70 | (pass-if "1+e10000i" | |
b7d22e03 | 71 | (equal? (read-string "1+e10000i") '1+e10000i)) |
23295dc3 MG |
72 | (pass-if "-nan.0-1i" |
73 | (not (equal? (imag-part (read-string "-nan.0-1i")) | |
74 | (imag-part (read-string "-nan.0+1i"))))) | |
b7d22e03 KR |
75 | |
76 | ;; At one time the arg list for "Unknown # object: ~S" didn't make it out | |
77 | ;; of read.c. Check that `format' can be applied to this error. | |
78 | (pass-if "error message on bad #" | |
79 | (catch #t | |
80 | (lambda () | |
81 | (read-string "#ZZZ") | |
82 | ;; oops, this # is supposed to be unrecognised | |
83 | #f) | |
84 | (lambda (key subr message args rest) | |
85 | (apply format #f message args) | |
86 | ;; message and args are ok | |
7337d56d LC |
87 | #t))) |
88 | ||
89 | (pass-if "block comment" | |
90 | (equal? '(+ 1 2 3) | |
91 | (read-string "(+ 1 #! this is a\ncomment !# 2 3)"))) | |
06974184 | 92 | |
454866e0 LC |
93 | (pass-if "block comment finishing s-exp" |
94 | (equal? '(+ 2) | |
95 | (read-string "(+ 2 #! a comment\n!#\n) "))) | |
96 | ||
911b03b2 JG |
97 | (pass-if "R6RS lexeme comment" |
98 | (equal? '(+ 1 2 3) | |
99 | (read-string "(+ 1 #!r6rs 2 3)"))) | |
100 | ||
101 | (pass-if "partial R6RS lexeme comment" | |
102 | (equal? '(+ 1 2 3) | |
103 | (read-string "(+ 1 #!r6r !# 2 3)"))) | |
104 | ||
620c8965 LC |
105 | (pass-if "R6RS/SRFI-30 block comment" |
106 | (equal? '(+ 1 2 3) | |
107 | (read-string "(+ 1 #| this is a\ncomment |# 2 3)"))) | |
108 | ||
109 | (pass-if "R6RS/SRFI-30 nested block comment" | |
110 | (equal? '(a b c) | |
111 | (read-string "(a b c #| d #| e |# f |#)"))) | |
112 | ||
6d5f8c32 AW |
113 | (pass-if "R6RS/SRFI-30 nested block comment (2)" |
114 | (equal? '(a b c) | |
115 | (read-string "(a b c #|||||||#)"))) | |
116 | ||
117 | (pass-if "R6RS/SRFI-30 nested block comment (3)" | |
118 | (equal? '(a b c) | |
119 | (read-string "(a b c #||||||||#)"))) | |
120 | ||
620c8965 LC |
121 | (pass-if "R6RS/SRFI-30 block comment syntax overridden" |
122 | ;; To be compatible with 1.8 and earlier, we should be able to override | |
123 | ;; this syntax. | |
d458073b AR |
124 | (with-fluids ((%read-hash-procedures (fluid-ref %read-hash-procedures))) |
125 | (read-hash-extend #\| (lambda args 'not)) | |
126 | (fold (lambda (x y result) | |
127 | (and result (eq? x y))) | |
128 | #t | |
129 | (read-string "(this is #| a comment)") | |
130 | `(this is not a comment)))) | |
131 | ||
7337d56d LC |
132 | (pass-if "unprintable symbol" |
133 | ;; The reader tolerates unprintable characters for symbols. | |
7b041912 LC |
134 | (equal? (string->symbol "\x01\x02\x03") |
135 | (read-string "\x01\x02\x03"))) | |
d41668fa LC |
136 | |
137 | (pass-if "CR recognized as a token delimiter" | |
138 | ;; In 1.8.3, character 0x0d was not recognized as a delimiter. | |
1ffa6923 LC |
139 | (equal? (read-string "one\x0dtwo") 'one)) |
140 | ||
141 | (pass-if "returned strings are mutable" | |
142 | ;; Per R5RS Section 3.4, "Storage Model", `read' is supposed to return | |
143 | ;; mutable objects. | |
144 | (let ((str (with-input-from-string "\"hello, world\"" read))) | |
145 | (string-set! str 0 #\H) | |
5b69315e AW |
146 | (string=? str "Hello, world"))) |
147 | ||
148 | (pass-if "square brackets are parens" | |
149 | (equal? '() (read-string "[]"))) | |
289c3a61 | 150 | |
5b69315e AW |
151 | (pass-if-exception "paren mismatch" exception:unexpected-rparen |
152 | (read-string "'[)")) | |
153 | ||
289c3a61 LC |
154 | (pass-if-exception "paren mismatch (2)" exception:unexpected-rsqbracket |
155 | (read-string "'(]")) | |
156 | ||
157 | (pass-if-exception "paren mismatch (3)" exception:mismatched-paren | |
158 | (read-string "'(foo bar]")) | |
159 | ||
160 | (pass-if-exception "paren mismatch (4)" exception:mismatched-paren | |
161 | (read-string "'[foo bar)"))) | |
5b69315e | 162 | |
7337d56d LC |
163 | |
164 | \f | |
6b4113af DH |
165 | (pass-if-exception "radix passed to number->string can't be zero" |
166 | exception:out-of-range | |
167 | (number->string 10 0)) | |
168 | (pass-if-exception "radix passed to number->string can't be one either" | |
169 | exception:out-of-range | |
170 | (number->string 10 1)) | |
ef9709da | 171 | |
7337d56d | 172 | \f |
ef9709da DH |
173 | (with-test-prefix "mismatching parentheses" |
174 | (pass-if-exception "opening parenthesis" | |
175 | exception:eof | |
176 | (read-string "(")) | |
177 | (pass-if-exception "closing parenthesis following mismatched opening" | |
178 | exception:unexpected-rparen | |
179 | (read-string ")")) | |
289c3a61 LC |
180 | (pass-if-exception "closing square bracket following mismatched opening" |
181 | exception:unexpected-rsqbracket | |
182 | (read-string "]")) | |
ef9709da DH |
183 | (pass-if-exception "opening vector parenthesis" |
184 | exception:eof | |
185 | (read-string "#(")) | |
186 | (pass-if-exception "closing parenthesis following mismatched vector opening" | |
187 | exception:unexpected-rparen | |
188 | (read-string ")"))) | |
7337d56d LC |
189 | |
190 | \f | |
191 | (with-test-prefix "exceptions" | |
192 | ||
193 | ;; Reader exceptions: although they are not documented, they may be relied | |
194 | ;; on by some programs, hence these tests. | |
195 | ||
196 | (pass-if-exception "unterminated block comment" | |
197 | exception:unterminated-block-comment | |
198 | (read-string "(+ 1 #! comment\n...")) | |
620c8965 LC |
199 | (pass-if-exception "R6RS/SRFI-30 unterminated nested block comment" |
200 | exception:unterminated-block-comment | |
201 | (read-string "(foo #| bar #| |#)")) | |
7337d56d LC |
202 | (pass-if-exception "unknown character name" |
203 | exception:unknown-character-name | |
204 | (read-string "#\\theunknowncharacter")) | |
205 | (pass-if-exception "unknown sharp object" | |
206 | exception:unknown-sharp-object | |
207 | (read-string "#?")) | |
208 | (pass-if-exception "eof in string" | |
209 | exception:eof-in-string | |
210 | (read-string "\"the string that never ends")) | |
211 | (pass-if-exception "illegal escape in string" | |
212 | exception:illegal-escape | |
213 | (read-string "\"some string \\???\""))) | |
214 | ||
215 | \f | |
216 | (with-test-prefix "read-options" | |
217 | (pass-if "case-sensitive" | |
218 | (not (eq? 'guile 'GuiLe))) | |
219 | (pass-if "case-insensitive" | |
220 | (eq? 'guile | |
221 | (with-read-options '(case-insensitive) | |
222 | (lambda () | |
223 | (read-string "GuiLe"))))) | |
224 | (pass-if "prefix keywords" | |
225 | (eq? #:keyword | |
226 | (with-read-options '(keywords prefix case-insensitive) | |
227 | (lambda () | |
228 | (read-string ":KeyWord"))))) | |
ef4cbc08 LC |
229 | (pass-if "prefix non-keywords" |
230 | (symbol? (with-read-options '(keywords prefix) | |
231 | (lambda () | |
232 | (read-string "srfi88-keyword:"))))) | |
233 | (pass-if "postfix keywords" | |
234 | (eq? #:keyword | |
235 | (with-read-options '(keywords postfix) | |
236 | (lambda () | |
237 | (read-string "keyword:"))))) | |
5d660052 MG |
238 | (pass-if "long postfix keywords" |
239 | (eq? #:keyword0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 | |
240 | (with-read-options '(keywords postfix) | |
241 | (lambda () | |
242 | (read-string "keyword0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789:"))))) | |
ef4cbc08 LC |
243 | (pass-if "`:' is not a postfix keyword (per SRFI-88)" |
244 | (eq? ': | |
245 | (with-read-options '(keywords postfix) | |
246 | (lambda () | |
247 | (read-string ":"))))) | |
7337d56d LC |
248 | (pass-if "no positions" |
249 | (let ((sexp (with-read-options '() | |
250 | (lambda () | |
251 | (read-string "(+ 1 2 3)"))))) | |
252 | (and (not (source-property sexp 'line)) | |
253 | (not (source-property sexp 'column))))) | |
254 | (pass-if "positions" | |
255 | (let ((sexp (with-read-options '(positions) | |
256 | (lambda () | |
257 | (read-string "(+ 1 2 3)"))))) | |
492faee1 LC |
258 | (and (equal? (source-property sexp 'line) 0) |
259 | (equal? (source-property sexp 'column) 0)))) | |
260 | (pass-if "positions on quote" | |
261 | (let ((sexp (with-read-options '(positions) | |
2e85d145 | 262 | (lambda () |
492faee1 | 263 | (read-string "'abcde"))))) |
7337d56d | 264 | (and (equal? (source-property sexp 'line) 0) |
2e85d145 | 265 | (equal? (source-property sexp 'column) 0)))) |
58b1db5f LC |
266 | (pass-if "position of SCSH block comment" |
267 | ;; In Guile 2.0.0 the reader would not update the port's position | |
268 | ;; when reading an SCSH block comment. | |
269 | (let ((sexp (with-read-options '(positions) | |
270 | (lambda () | |
271 | (read-string "#!foo\nbar\nbaz\n!#\n(hello world)\n"))))) | |
272 | (= 4 (source-property sexp 'line)))) | |
273 | ||
2e85d145 MG |
274 | (with-test-prefix "r6rs-hex-escapes" |
275 | (pass-if-exception "non-hex char in two-digit hex-escape" | |
276 | exception:illegal-escape | |
277 | (with-read-options '(r6rs-hex-escapes) | |
278 | (lambda () | |
279 | (with-input-from-string "\"\\x0g;\"" read)))) | |
280 | ||
281 | (pass-if-exception "non-hex char in four-digit hex-escape" | |
282 | exception:illegal-escape | |
283 | (with-read-options '(r6rs-hex-escapes) | |
284 | (lambda () | |
285 | (with-input-from-string "\"\\x000g;\"" read)))) | |
286 | ||
287 | (pass-if-exception "non-hex char in six-digit hex-escape" | |
288 | exception:illegal-escape | |
289 | (with-read-options '(r6rs-hex-escapes) | |
290 | (lambda () | |
291 | (with-input-from-string "\"\\x00000g;\"" read)))) | |
292 | ||
293 | (pass-if-exception "no semicolon at termination of one-digit hex-escape" | |
294 | exception:illegal-escape | |
295 | (with-read-options '(r6rs-hex-escapes) | |
296 | (lambda () | |
297 | (with-input-from-string "\"\\x0\"" read)))) | |
298 | ||
299 | (pass-if-exception "no semicolon at termination of three-digit hex-escape" | |
300 | exception:illegal-escape | |
301 | (with-read-options '(r6rs-hex-escapes) | |
302 | (lambda () | |
303 | (with-input-from-string "\"\\x000\"" read)))) | |
304 | ||
305 | (pass-if "two-digit hex escape" | |
306 | (eqv? | |
307 | (with-read-options '(r6rs-hex-escapes) | |
308 | (lambda () | |
309 | (string-ref (with-input-from-string "\"--\\xff;--\"" read) 2))) | |
310 | (integer->char #xff))) | |
311 | ||
312 | (pass-if "four-digit hex escape" | |
313 | (eqv? | |
314 | (with-read-options '(r6rs-hex-escapes) | |
315 | (lambda () | |
316 | (string-ref (with-input-from-string "\"--\\x0100;--\"" read) 2))) | |
317 | (integer->char #x0100))) | |
318 | ||
319 | (pass-if "six-digit hex escape" | |
320 | (eqv? | |
321 | (with-read-options '(r6rs-hex-escapes) | |
322 | (lambda () | |
323 | (string-ref (with-input-from-string "\"--\\x010300;--\"" read) 2))) | |
324 | (integer->char #x010300))) | |
325 | ||
326 | (pass-if "escaped characters match non-escaped ASCII characters" | |
327 | (string=? | |
328 | (with-read-options '(r6rs-hex-escapes) | |
329 | (lambda () | |
330 | (with-input-from-string "\"\\x41;\\x0042;\\x000043;\"" read))) | |
331 | "ABC")) | |
332 | ||
d31b9519 | 333 | (pass-if "write R6RS string escapes" |
2e85d145 MG |
334 | (let* ((s1 (apply string |
335 | (map integer->char '(#x8 ; backspace | |
c03ef352 | 336 | #x18 ; cancel |
2e85d145 MG |
337 | #x20 ; space |
338 | #x30 ; zero | |
339 | #x40 ; at sign | |
340 | )))) | |
341 | (s2 (with-read-options '(r6rs-hex-escapes) | |
342 | (lambda () | |
343 | (with-output-to-string | |
344 | (lambda () (write s1))))))) | |
345 | (lset= eqv? | |
346 | (string->list s2) | |
c03ef352 | 347 | (list #\" #\\ #\b #\\ #\x #\1 #\8 #\; #\space #\0 #\@ #\")))) |
d31b9519 MG |
348 | |
349 | (pass-if "display R6RS string escapes" | |
350 | (string=? | |
351 | (with-read-options '(r6rs-hex-escapes) | |
352 | (lambda () | |
353 | (let ((pt (open-output-string)) | |
354 | (s1 (apply string (map integer->char | |
355 | '(#xFF #x100 #xFFF #x1000 #xFFFF #x10000))))) | |
356 | (set-port-encoding! pt "ASCII") | |
357 | (set-port-conversion-strategy! pt 'escape) | |
358 | (display s1 pt) | |
359 | (get-output-string pt)))) | |
360 | "\\xff;\\x100;\\xfff;\\x1000;\\xffff;\\x10000;")) | |
361 | ||
2e85d145 | 362 | (pass-if "one-digit hex escape" |
0f3a70cf | 363 | (eqv? (with-input-from-string "#\\xA" read) |
2e85d145 MG |
364 | (integer->char #x0A))) |
365 | ||
366 | (pass-if "two-digit hex escape" | |
0f3a70cf | 367 | (eqv? (with-input-from-string "#\\xFF" read) |
2e85d145 MG |
368 | (integer->char #xFF))) |
369 | ||
370 | (pass-if "four-digit hex escape" | |
0f3a70cf | 371 | (eqv? (with-input-from-string "#\\x00FF" read) |
2e85d145 MG |
372 | (integer->char #xFF))) |
373 | ||
374 | (pass-if "eight-digit hex escape" | |
0f3a70cf | 375 | (eqv? (with-input-from-string "#\\x00006587" read) |
2e85d145 | 376 | (integer->char #x6587))) |
0f3a70cf | 377 | |
2e85d145 MG |
378 | (pass-if "write R6RS escapes" |
379 | (string=? | |
380 | (with-read-options '(r6rs-hex-escapes) | |
381 | (lambda () | |
382 | (with-output-to-string | |
383 | (lambda () | |
384 | (write (integer->char #x80)))))) | |
684d664e AW |
385 | "#\\x80"))) |
386 | ||
387 | (with-test-prefix "hungry escapes" | |
388 | (pass-if "default not hungry" | |
389 | ;; Assume default setting of not hungry. | |
390 | (equal? (with-input-from-string "\"foo\\\n bar\"" | |
391 | read) | |
392 | "foo bar")) | |
393 | (pass-if "hungry" | |
394 | (dynamic-wind | |
395 | (lambda () | |
396 | (read-enable 'hungry-eol-escapes)) | |
397 | (lambda () | |
398 | (equal? (with-input-from-string "\"foo\\\n bar\"" | |
399 | read) | |
400 | "foobar")) | |
401 | (lambda () | |
402 | (read-disable 'hungry-eol-escapes)))))) | |
2e85d145 | 403 | |
9331ffd8 MW |
404 | (with-test-prefix "per-port-read-options" |
405 | (pass-if "case-sensitive" | |
406 | (equal? '(guile GuiLe gUIle) | |
407 | (with-read-options '(case-insensitive) | |
408 | (lambda () | |
409 | (with-input-from-string "GUIle #!no-fold-case GuiLe gUIle" | |
410 | (lambda () | |
411 | (list (read) (read) (read)))))))) | |
412 | (pass-if "case-insensitive" | |
413 | (equal? '(GUIle guile guile) | |
414 | (with-input-from-string "GUIle #!fold-case GuiLe gUIle" | |
415 | (lambda () | |
416 | (list (read) (read) (read))))))) | |
7337d56d | 417 | |
6ed0c41a AW |
418 | (with-test-prefix "#;" |
419 | (for-each | |
420 | (lambda (pair) | |
421 | (pass-if (car pair) | |
422 | (equal? (with-input-from-string (car pair) read) (cdr pair)))) | |
423 | ||
424 | '(("#;foo 10". 10) | |
425 | ("#;(10 20 30) foo" . foo) | |
426 | ("#; (10 20 30) foo" . foo) | |
427 | ("#;\n10\n20" . 20))) | |
2e85d145 | 428 | |
6ed0c41a AW |
429 | (pass-if "#;foo" |
430 | (eof-object? (with-input-from-string "#;foo" read))) | |
2e85d145 | 431 | |
6ed0c41a AW |
432 | (pass-if-exception "#;" |
433 | exception:missing-expression | |
434 | (with-input-from-string "#;" read)) | |
435 | (pass-if-exception "#;(" | |
436 | exception:eof | |
437 | (with-input-from-string "#;(" read))) | |
438 | ||
e3c5df53 AW |
439 | (with-test-prefix "#'" |
440 | (for-each | |
441 | (lambda (pair) | |
442 | (pass-if (car pair) | |
443 | (equal? (with-input-from-string (car pair) read) (cdr pair)))) | |
444 | ||
445 | '(("#'foo". (syntax foo)) | |
446 | ("#`foo" . (quasisyntax foo)) | |
447 | ("#,foo" . (unsyntax foo)) | |
448 | ("#,@foo" . (unsyntax-splicing foo))))) | |
449 | ||
d9527cfa AW |
450 | (with-test-prefix "#{}#" |
451 | (pass-if (equal? (read-string "#{}#") '#{}#)) | |
1f7945a7 | 452 | (pass-if (not (equal? (read-string "(a #{.}# b)") '(a . b)))) |
d9527cfa AW |
453 | (pass-if (equal? (read-string "#{a}#") 'a)) |
454 | (pass-if (equal? (read-string "#{a b}#") '#{a b}#)) | |
d9527cfa AW |
455 | (pass-if-exception "#{" exception:eof-in-symbol |
456 | (read-string "#{")) | |
457 | (pass-if (equal? (read-string "#{a\\x20;b}#") '#{a b}#))) | |
458 | ||
62ef23cb AW |
459 | (begin-deprecated |
460 | (with-test-prefix "deprecated #{}# escapes" | |
461 | (pass-if (equal? (read-string "#{a\\ b}#") '#{a b}#)))) | |
e3c5df53 | 462 | |
58b1db5f LC |
463 | ;;; Local Variables: |
464 | ;;; eval: (put 'with-read-options 'scheme-indent-function 1) | |
465 | ;;; End: |