Merge remote-tracking branch 'origin/stable-2.0'
[bpt/guile.git] / test-suite / tests / reader.test
1 ;;;; reader.test --- Reader test. -*- coding: iso-8859-1; mode: scheme -*-
2 ;;;;
3 ;;;; Copyright (C) 1999, 2001, 2002, 2003, 2007, 2008, 2009, 2010, 2011,
4 ;;;; 2013 Free Software Foundation, Inc.
5 ;;;; Jim Blandy <jimb@red-bean.com>
6 ;;;;
7 ;;;; This library is free software; you can redistribute it and/or
8 ;;;; modify it under the terms of the GNU Lesser General Public
9 ;;;; License as published by the Free Software Foundation; either
10 ;;;; version 3 of the License, or (at your option) any later version.
11 ;;;;
12 ;;;; This library is distributed in the hope that it will be useful,
13 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 ;;;; Lesser General Public License for more details.
16 ;;;;
17 ;;;; You should have received a copy of the GNU Lesser General Public
18 ;;;; License along with this library; if not, write to the Free Software
19 ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
21 (define-module (test-suite reader)
22 :use-module (srfi srfi-1)
23 :use-module (test-suite lib))
24
25
26 (define exception:eof
27 (cons 'read-error "end of file$"))
28 (define exception:unexpected-rparen
29 (cons 'read-error "unexpected \")\"$"))
30 (define exception:unexpected-rsqbracket
31 (cons 'read-error "unexpected \"]\"$"))
32 (define exception:unterminated-block-comment
33 (cons 'read-error "unterminated `#. \\.\\.\\. .#' comment$"))
34 (define exception:unknown-character-name
35 (cons 'read-error "unknown character name .*$"))
36 (define exception:unknown-sharp-object
37 (cons 'read-error "Unknown # object: .*$"))
38 (define exception:eof-in-string
39 (cons 'read-error "end of file in string constant$"))
40 (define exception:eof-in-symbol
41 (cons 'read-error "end of file while reading symbol$"))
42 (define exception:illegal-escape
43 (cons 'read-error "illegal character in escape sequence: .*$"))
44 (define exception:missing-expression
45 (cons 'read-error "no expression after #;"))
46 (define exception:mismatched-paren
47 (cons 'read-error "mismatched close paren"))
48
49
50 (define (read-string s)
51 (with-input-from-string s (lambda () (read))))
52
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
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"
71 (equal? (read-string "1+e10000i") '1+e10000i))
72 (pass-if "-nan.0-1i"
73 (not (equal? (imag-part (read-string "-nan.0-1i"))
74 (imag-part (read-string "-nan.0+1i")))))
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
87 #t)))
88
89 (pass-if "block comment"
90 (equal? '(+ 1 2 3)
91 (read-string "(+ 1 #! this is a\ncomment !# 2 3)")))
92
93 (pass-if "block comment finishing s-exp"
94 (equal? '(+ 2)
95 (read-string "(+ 2 #! a comment\n!#\n) ")))
96
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
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
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
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.
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
132 (pass-if "unprintable symbol"
133 ;; The reader tolerates unprintable characters for symbols.
134 (equal? (string->symbol "\x01\x02\x03")
135 (read-string "\x01\x02\x03")))
136
137 (pass-if "CR recognized as a token delimiter"
138 ;; In 1.8.3, character 0x0d was not recognized as a delimiter.
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)
146 (string=? str "Hello, world")))
147
148 (pass-if "square brackets are parens"
149 (equal? '() (read-string "[]")))
150
151 (pass-if-exception "paren mismatch" exception:unexpected-rparen
152 (read-string "'[)"))
153
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)")))
162
163
164 \f
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))
171
172 \f
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 ")"))
180 (pass-if-exception "closing square bracket following mismatched opening"
181 exception:unexpected-rsqbracket
182 (read-string "]"))
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 ")")))
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..."))
199 (pass-if-exception "R6RS/SRFI-30 unterminated nested block comment"
200 exception:unterminated-block-comment
201 (read-string "(foo #| bar #| |#)"))
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")))))
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:")))))
238 (pass-if "long postfix keywords"
239 (eq? #:keyword0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
240 (with-read-options '(keywords postfix)
241 (lambda ()
242 (read-string "keyword0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789:")))))
243 (pass-if "`:' is not a postfix keyword (per SRFI-88)"
244 (eq? ':
245 (with-read-options '(keywords postfix)
246 (lambda ()
247 (read-string ":")))))
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)")))))
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)
262 (lambda ()
263 (read-string "'abcde")))))
264 (and (equal? (source-property sexp 'line) 0)
265 (equal? (source-property sexp 'column) 0))))
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
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
333 (pass-if "write R6RS string escapes"
334 (let* ((s1 (apply string
335 (map integer->char '(#x8 ; backspace
336 #x18 ; cancel
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)
347 (list #\" #\\ #\b #\\ #\x #\1 #\8 #\; #\space #\0 #\@ #\"))))
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
362 (pass-if "one-digit hex escape"
363 (eqv? (with-input-from-string "#\\xA" read)
364 (integer->char #x0A)))
365
366 (pass-if "two-digit hex escape"
367 (eqv? (with-input-from-string "#\\xFF" read)
368 (integer->char #xFF)))
369
370 (pass-if "four-digit hex escape"
371 (eqv? (with-input-from-string "#\\x00FF" read)
372 (integer->char #xFF)))
373
374 (pass-if "eight-digit hex escape"
375 (eqv? (with-input-from-string "#\\x00006587" read)
376 (integer->char #x6587)))
377
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))))))
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))))))
403
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)))))))
417
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)))
428
429 (pass-if "#;foo"
430 (eof-object? (with-input-from-string "#;foo" read)))
431
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
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
450 (with-test-prefix "#{}#"
451 (pass-if (equal? (read-string "#{}#") '#{}#))
452 (pass-if (not (equal? (read-string "(a #{.}# b)") '(a . b))))
453 (pass-if (equal? (read-string "#{a}#") 'a))
454 (pass-if (equal? (read-string "#{a b}#") '#{a b}#))
455 (pass-if-exception "#{" exception:eof-in-symbol
456 (read-string "#{"))
457 (pass-if (equal? (read-string "#{a\\x20;b}#") '#{a b}#)))
458
459 (begin-deprecated
460 (with-test-prefix "deprecated #{}# escapes"
461 (pass-if (equal? (read-string "#{a\\ b}#") '#{a b}#))))
462
463 ;;; Local Variables:
464 ;;; eval: (put 'with-read-options 'scheme-indent-function 1)
465 ;;; End: