reader.test 18 KB

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