set-syntax-from-char.lsp 13.6 KB
Newer Older
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
;-*- Mode:     Lisp -*-
;;;; Author:   Paul Dietz
;;;; Created:  Sat Jan 29 06:37:18 2005
;;;; Contains: Tests of SET-SYNTAX-FROM-CHAR

(in-package :cl-test)

(compile-and-load "reader-aux.lsp")

(defmacro def-set-syntax-from-char-test (name form &body expected-values)
  `(deftest ,name
     (with-standard-io-syntax
      (let ((*readtable* (copy-readtable nil)))
	(setf (readtable-case *readtable*) :preserve)
	,form))
     ,@expected-values))

;;; Test that constituent traits are not altered when a constituent character
;;; syntax type is set

(defmacro def-set-syntax-from-char-trait-test (c test-form expected-value)
  (setq c (typecase c
	    (character c)
	    ((or string symbol) (name-char (string c)))
	    (t nil)))
  (when c
27
    ;; (format t "~A ~A~%" c (char-name c))
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
    `(def-set-syntax-from-char-test
       ,(intern (concatenate 'string "SET-SYNTAX-FROM-CHAR-TRAIT-X-" (or (char-name c)
									 (string c)))
		:cl-test)
       (let ((c ,c))
	 (values
	  (set-syntax-from-char c #\X)
	  ,test-form))
       t ,expected-value)))

(defmacro def-set-syntax-from-char-alphabetic-trait-test (c)
  `(def-set-syntax-from-char-trait-test ,c
     (let* ((*package* (find-package "CL-TEST"))
	    (sym (read-from-string (string c))))
       (list (let ((sym2 (find-symbol (string c))))
	       (or (eqt sym sym2)
		   (list sym sym2)))
	     (or (equalt (symbol-name sym) (string c))
		 (list (symbol-name sym) (string c)))))
     (t t)))

49
(loop for c across "\\|!\"#$%&'()*,;<=>?@[]^_`~{}+-/abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
50
51
      do (eval `(def-set-syntax-from-char-alphabetic-trait-test ,c)))

52
53
54
;;; The invalid constituent character trait of invalid and whitespace characters
;;; is exposed when they are turned into constituent characters

55
56
(defmacro def-set-syntax-from-char-invalid-trait-test (c)
  `(def-set-syntax-from-char-trait-test ,c
57
58
59
60
61
62
     (handler-case
      (let* ((*package* (find-package "CL-TEST"))
	     (sym (read-from-string (concatenate 'string (string c) "Z"))))
	sym)
      (reader-error (c) (declare (ignore c)) :good))
     :good))
63
64
65
66

(loop for name in '("Backspace" "Tab" "Newline" "Linefeed" "Page" "Return" "Space" "Rubout")
      do (eval `(def-set-syntax-from-char-invalid-trait-test ,name)))

67
68
;;; Turning characters into single escape characters

69
(deftest set-syntax-from-char.single-escape.1
70
71
72
73
74
75
76
77
78
79
  (loop for c across +standard-chars+
	nconc
	(with-standard-io-syntax
	 (let ((*readtable* (copy-readtable nil))
	       (*package* (find-package "CL-TEST")))
	   (let ((results
		  (list
		   (set-syntax-from-char c #\\)
		   (read-from-string (concatenate 'string (list c #\Z))))))
	     (unless (equal results '(t |Z|))
pfdietz's avatar
pfdietz committed
80
	       (list (list c results)))))))
81
82
  nil)

83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
(deftest set-syntax-from-char.single-escape.2
  (loop for c across +standard-chars+
	unless (eql c #\")
	nconc
	(with-standard-io-syntax
	 (let ((*readtable* (copy-readtable nil))
	       (*package* (find-package "CL-TEST")))
	   (let ((results
		  (list
		   (set-syntax-from-char c #\\)
		   (read-from-string (concatenate 'string
						  (list #\" c #\" #\"))))))
	     (unless (equal results '(t "\""))
	       (list (list c results)))))))
  nil)


100
(deftest set-syntax-from-char.multiple-escape
101
102
103
104
105
106
107
108
109
110
111
112
  (loop for c across +standard-chars+
	nconc
	(with-standard-io-syntax
	 (let ((*readtable* (copy-readtable nil))
	       (*package* (find-package "CL-TEST")))
	   (let ((results
		  (list
		   (set-syntax-from-char c #\|)
		   (handler-case
		    (read-from-string (concatenate 'string (list c #\Z c)))
		    (error (c) c))
		   (handler-case
113
		    (read-from-string (concatenate 'string (list c #\z #\|)))
114
115
116
117
		    (error (c) c))
		   (handler-case
		    (read-from-string (concatenate 'string (list #\| #\Z c)))
		    (error (c) c)))))
118
	     (unless (or (eql c #\Z) (eql c #\z) (equal results '(t |Z| |z| |Z|)))
pfdietz's avatar
pfdietz committed
119
	       (list (list c results)))))))
120
121
  nil)

122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
(deftest set-syntax-from-char.semicolon
  (loop for c across +standard-chars+
	nconc
	(with-standard-io-syntax
	 (let ((*readtable* (copy-readtable nil))
	       (*package* (find-package "CL-TEST"))
	       (expected (if (eql c #\0) '1 '0))
	       (c2 (if (eql c #\0) #\1 #\0)))
	   (let ((results
		  (list
		   (set-syntax-from-char c #\;)
		   (handler-case
		    (read-from-string (concatenate 'string (list c2 c #\2)))
		    (error (c) c))
		   (handler-case
		    (read-from-string (concatenate 'string (list c2 c #\2 #\Newline #\3)))
		    (error (c) c))
		   (handler-case
		    (read-from-string (concatenate 'string (list c #\2 #\Newline c2)))
		    (error (c) c)))))
	     (unless (equal results (list t expected expected expected))
pfdietz's avatar
pfdietz committed
143
	       (list (list c results)))))))
144
  nil)
pfdietz's avatar
pfdietz committed
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473

(deftest set-syntax-from-char.left-paren
  (loop for c across +standard-chars+
	unless (find c ")")
	nconc
	(with-standard-io-syntax
	 (let ((*readtable* (copy-readtable nil))
	       (*package* (find-package "CL-TEST"))
	       (expected (if (eql c #\0) '(1) '(0)))
	       (c2 (if (eql c #\0) #\1 #\0)))
	   (let ((results
		  (list
		   (set-syntax-from-char c #\()
		   (handler-case
		    (read-from-string (concatenate 'string (list c) ")"))
		    (error (c) c))
		   (handler-case
		    (read-from-string (concatenate 'string (list c c2) ")2" (list #\Newline #\3)))
		    (error (c) c))
		   (handler-case
		    (read-from-string (concatenate 'string (list c c2) ")"))
		    (error (c) c)))))
	     (unless (equal results (list t nil expected expected))
	       (list (list c results)))))))
  nil)

(deftest set-syntax-from-char.right-paren
  (loop for c across +standard-chars+
	nconc
	(with-standard-io-syntax
	 (let ((*readtable* (copy-readtable nil))
	       (*package* (find-package "CL-TEST")))
	   (let ((results
		  (list
		   (set-syntax-from-char c #\))
		   (handler-case
		    (read-from-string (string c) nil nil)
		    (reader-error (c) :good)
		    (error (c) c)))))
	     (unless (equal results '(t :good))
	       (list (list c results)))))))
  nil)

(deftest set-syntax-from-char.single-quote
  (loop for c across +standard-chars+
	nconc
	(with-standard-io-syntax
	 (let ((*readtable* (copy-readtable nil))
	       (*package* (find-package "CL-TEST"))
	       (expected (if (eql c #\0) ''1 ''0))
	       (c2 (if (eql c #\0) #\1 #\0)))
	   (let ((results
		  (list
		   (set-syntax-from-char c #\')
		   (handler-case
		    (read-from-string (concatenate 'string (list c c2)))
		    (error (c) c))
		   (handler-case
		    (read-from-string (concatenate 'string (list c c2) " 2"))
		    (error (c) c))
		   (handler-case
		    (read-from-string (concatenate 'string (list c c2) ")"))
		    (error (c) c)))))
	     (unless (equal results (list t expected expected expected))
	       (list (list c results)))))))
  nil)

;;; I do not test that setting syntax from #\" allows the character to be
;;; used as the terminator of a double quoted string.  It is not clear that
;;; the standard implies this.

(deftest set-syntax-from-char.double-quote
  (loop for c across +standard-chars+
	nconc
	(with-standard-io-syntax
	 (let ((*readtable* (copy-readtable nil))
	       (*package* (find-package "CL-TEST"))
	       (expected (if (eql c #\0) "1" "0"))
	       (c2 (if (eql c #\0) #\1 #\0)))
	   (let ((results
		  (list
		   (set-syntax-from-char c #\")
		   (handler-case
		    (read-from-string
		     (concatenate 'string (list c c2 #\")))
		    (error (c) c))
		   (handler-case
		    (read-from-string
		     (concatenate 'string (list c c2 #\" #\2)))
		    (error (c) c))
		   (handler-case
		    (read-from-string (concatenate 'string (list c c2) "\")"))
		    (error (c) c)))))
	     (unless (equal results (list t expected expected expected))
	       (list (list c results)))))))
  nil)

(deftest set-syntax-from-char.backquote
  (loop for c across +standard-chars+
	unless (find c ",x")
	nconc
	(with-standard-io-syntax
	 (let* ((*readtable* (copy-readtable nil))
		(*package* (find-package "CL-TEST"))
		(c2 (if (eql c #\Space) #\Newline #\Space))
		(results
		 (list
		  (set-syntax-from-char c #\`)
		  (handler-case
		   (eval `(let ((x 0))
			    ,(read-from-string
			      (concatenate 'string (list c #\, #\x)))))
		   (error (c) c))
		  (handler-case
		   (eval `(let ((x 0))
			    ,(read-from-string
			      (concatenate 'string (list c #\, #\x c2)))))
		   (error (c) c))
		  (handler-case
		   (eval `(let ((x 0))
			    ,(read-from-string
			      (concatenate 'string (list c c2 #\, #\x c2)))))
		   (error (c) c)))))
	   (unless (equal results '(t 0 0 0))
	     (list (list c results))))))
  nil)

(deftest set-syntax-from-char.comma
  (loop for c across +standard-chars+
	unless (find c "`x")
	nconc
	(with-standard-io-syntax
	 (let* ((*readtable* (copy-readtable nil))
		(*package* (find-package "CL-TEST"))
		(c2 (if (eql c #\Space) #\Newline #\Space))
		(results
		 (list
		  (set-syntax-from-char c #\,)
		  (handler-case
		   (read-from-string (string c))
		   (reader-error (c) :good)
		   (error (c) c))
		  (handler-case
		   (eval `(let ((x 0))
			    ,(read-from-string
			      (concatenate 'string "`" (list c) "x"))))
		   (error (c) c)))))
	   (unless (equal results '(t :good 0))
	     (list (list c results))))))
  nil)
	       
;;; Tests of set-syntax-from-char on #\#

(deftest set-syntax-from-char.sharp.1
  (loop for c across +standard-chars+
	nconc
	(with-standard-io-syntax
	 (let* ((*readtable* (copy-readtable nil))
		(*package* (find-package "CL-TEST"))
		(results
		 (list
		  (set-syntax-from-char c #\#)
		  (if (not (eql c #\Space))
		      (handler-case
		       (read-from-string
			(concatenate 'string (list c #\Space)))
		       (reader-error () :good)
		       (error (c) c))
		    :good)
		  (if (not (find c "'X"))
		      (handler-case
		       (read-from-string
			(concatenate 'string (list c) "'X"))
		       (error (c) c))
		    '#'|X|)
		  (if (not (find c "(X)"))
		      (handler-case
		       (read-from-string
			(concatenate 'string (list c) "(X)"))
		       (error (c) c))
		    #(|X|))
		  (if (not (find c ")"))
		      (handler-case
		       (read-from-string
			(concatenate 'string (list c) ")"))
		       (reader-error (c) :good)
		       (error (c) c))
		    :good)
		  (if (not (find c "*"))
		      (handler-case
		       (read-from-string
			(concatenate 'string (list c #\*)))
		       (error (c) c))
		    #*)
		  (if (not (find c ":|"))
		      (handler-case
		       (let ((sym (read-from-string
				   (concatenate 'string (list c) ":||"))))
			 (and (symbolp sym)
			      (null (symbol-package sym))
			      (symbol-name sym)))
		       (error (c) c))
		    "")
		  (handler-case
		   (read-from-string
		    (concatenate 'string (list c #\<)))
		   (reader-error (c) :good)
		   (error (c) c))
		  (handler-case
		   (read-from-string
		    (concatenate 'string (list c #\\ #\X)))
		   (error (c) c))
		  (if (not (find c "1"))
		      (handler-case
		       (read-from-string
			(concatenate 'string (list c) "|1111|#1"))
		       (error (c) c))
		    1)
		  (if (not (find c "1"))
		      (handler-case
		       (read-from-string
			(concatenate 'string (list c) "|11#|111|#11|#1"))
		       (error (c) c))
		    1)
		  )))
	   (unless (equalp results '(t :good #'|X| #(|X|) :good #* "" :good #\X 1 1))
	     (list (list c results))))))
  nil)

(deftest set-syntax-from-char.sharp.2
  (loop for c across +standard-chars+
	nconc
	(with-standard-io-syntax
	 (let* ((*readtable* (copy-readtable nil))
		(*package* (find-package "CL-TEST"))
		(results
		 (list
		  (set-syntax-from-char c #\#)
		  (if (not (find c "+XC "))
		      (handler-case
		       (let ((*features* (cons ':X *features*)))
			 (read-from-string
			  (concatenate 'string (list c) "+X C")))
		       (error (c) c))
		    'c)
		  (if (not (find c "-(OR)"))
		      (handler-case
		       (read-from-string
			(concatenate 'string (list c) "-(OR)R"))
		       (error (c) c))
		    'r)
		  (if (not (find c ".1"))
		      (handler-case
		       (read-from-string
			(concatenate 'string (list c) ".1"))
		       (error (c) c))
		    1)
		  (if (not (find c "01aA"))
		      (handler-case
		       (list
			(read-from-string
			 (concatenate 'string (list c) "0a1"))
			(read-from-string
			 (concatenate 'string (list c) "0A1")))
		       (error (c) c))
		    '(#0a1 #0a1))
		  (if (not (find c "01bB"))
		      (handler-case
		       (list
			(read-from-string
			 (concatenate 'string (list c) "b101"))
			(read-from-string
			 (concatenate 'string (list c) "B011")))
		       (error (c) c))
		    '(5 3))
		  (if (not (find c "cC()12 "))
		      (handler-case
		       (list
			(read-from-string
			 (concatenate 'string (list c) "c(1 2)"))
			(read-from-string
			 (concatenate 'string (list c) "C(2 1)")))
		       (error (c) c))
		    '(#c(1 2) #c(2 1)))
		  (if (not (find c "oO0127"))
		      (handler-case
		       (list
			(read-from-string
			 (concatenate 'string (list c) "o172"))
			(read-from-string
			 (concatenate 'string (list c) "O7721")))
		       (error (c) c))
		    '(#o172 #o7721))
		  (if (not (find c "pP\""))
		      (handler-case
		       (list
			(read-from-string
			 (concatenate 'string (list c) "p\"\""))
			(read-from-string
			 (concatenate 'string (list c) "P\"\"")))
		       (error (c) c))
		    '(#p"" #p""))
		  (if (not (find c "rR0123"))
		      (handler-case
		       (list
			(read-from-string
			 (concatenate 'string (list c) "3r210"))
			(read-from-string
			 (concatenate 'string (list c) "3R1111")))
		       (error (c) c))
		    '(#3r210 #3r1111))
		  ;;; Add #s test here
		  (if (not (find c "xX04dF"))
		      (handler-case
		       (list
			(read-from-string
			 (concatenate 'string (list c) "x40Fd"))
			(read-from-string
			 (concatenate 'string (list c) "XFd04")))
		       (error (c) c))
		    '(#x40fd #xfd04))
		  )))
	   (unless (equalp results
			   '(t c r 1 (#0a1 #0a1) (5 3) (#c(1 2) #c(2 1))
			       (#o172 #o7721) (#p"" #p"") (#3r210 #3r1111)
			       (#x40fd #xfd04)))
	     (list (list c results)))
	   )))
  nil)