/
trivial-formatter.lisp
1372 lines (1074 loc) · 43.6 KB
/
trivial-formatter.lisp
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(defpackage :trivial-formatter.spec
(:import-from :trivial-formatter #:pprint-extended-loop #:split-keywords
#:pprint-fun-call #:pprint-define-condition #:pprint-restart-case
#:pprint-with-open-file #:split-to-lines #:pprint-cond #:pprint-flet
#:pprint-defgeneric #:pprint-defstruct #:pprint-defclass #:pprint-handler-case
#:pprint-method-lambda-list #:pprint-restart-bind)
(:use :cl :jingoh :trivial-formatter))
(in-package :trivial-formatter.spec)
(setup :trivial-formatter)
(requirements-about impl-specific-guards :doc-type nil)
#+abcl
#?(pprint-logical-block (*standard-output* nil)
(prin1 (streamp *standard-output*)))
:outputs "NIL"
,:comment "When this test is failed, fix stream-designator and output-stream."
(requirements-about FMT :doc-type function)
;;;; Description:
; Format every source code of asdf:system.
#+syntax
(FMT system &optional (if-exists nil supplied-p)) ; => result
;;;; Arguments and Values:
; system := (or symbol (vector character) (vector nil) base-string asdf/system:system)
; otherwise, the condition depending on implementation is signaled.
#?(fmt '(invalid type)) :signals condition
; if-exists := (member nil :append :supersede :rename :error :new-version :rename-and-delete :overwrite)
; otherwise, the condition depending on implementation is signaled.
#?(fmt :dummy :not-member) :signals condition
; If specified, depending on its value source file is modified.
; If not specified, formatted source code is printed to `*STANDARD-OUTPUT*`.
; Member is completely same with `CL:OPEN` keyword argument `IF-EXISTS`.
; result := null
;;;; Affected By:
; `*readtable*` `*print-pprint-dispatch*`
; `*strict-loop-keyword-p*`, when t, loop macro keywords are forced to be keyword symbols.
;;;; Side-Effects:
; Load asdf:system to lisp environment.
; Depends on IF-EXISTS, some side effect occurs.
; The default behavior is to print to `*STANDARD-OUTPUT*`.
;;;; Notes:
;;;; Exceptional-Situations:
; When the specified system is not found, an error of asdf:missing-component is signaled.
#?(fmt :no-such-system) :signals asdf:missing-component
(requirements-about READ-AS-CODE :doc-type function)
;;;; Description:
; Almost the same with `CL:READ` but
; * Can handle the symbols which have the unknown package prefix.
; * Can handle the comment.
; * Treat dot list as a proper list.
#+syntax
(READ-AS-CODE &optional stream (eof-error-p t) (eof-value nil) (recursive-p nil)) ; => result
;;;; Arguments and Values:
; stream := (or null stream), otherwise, the condition depending on implementation is signaled.
#?(read-as-code :not-stream) :signals condition
; When NIL is specified, it means `*STANDARD-INPUT*`.
#?(with-input-from-string(*standard-input* "nil")
(read-as-code nil))
=> NIL
; eof-error-p := boolean, otherwise the condition depending on implementation is signaled.
#?(read-as-code nil :not-boolean) :signals condition
; When NIL is specified and gets an end-of-file, never signals end-of-file.
#?(with-input-from-string(*standard-input* "")
(read-as-code nil nil :returned))
:invokes-debugger not
; When T is specified (the default) and get end-of-file, an error is signaled.
#?(with-input-from-string(*standard-input* "")
(read-as-code nil t))
:signals end-of-file
; eof-value := t, when `EOF-ERROR-P` is `NIL`, and get end-of-file, this value is returned.
#?(with-input-from-string(*standard-input* "")
(read-as-code nil nil :returned))
=> :returned
; recursive-p := boolean, internal use.
; result := t intermediate expression or eof-value is returned.
#?(with-input-from-string(s "symbol")
(read-as-code s))
=> SYMBOL
#?(with-input-from-string(s "#:uninterned")
(read-as-code s))
:satisfies (lambda(result)
(& (symbolp result)
(null(symbol-package result))
(equal "UNINTERNED" (symbol-name result))))
#?(with-input-from-string(s ":keyword")
(read-as-code s))
=> :KEYWORD
; Double colon keyword symbol will be canonicalized to a single colon keyword symbol.
#?(with-input-from-string(s "::double-colloned")
(read-as-code s))
=> :DOUBLE-COLLONED
; Without package prefix, the symbol is interned to the current package.
#?(with-input-from-string(s "without-prefix")
(read-as-code s))
:satisfies (lambda(result)
(&(symbolp result)
(eq *package* (symbol-package result))
(equal "WITHOUT-PREFIX" (symbol-name result))))
; When prefixed,
; * If the package does not exist in the current lisp image, uninterned symbol will be made as an intermediate expression.
#?(with-input-from-string(s "no-such-package:symbol")
(read-as-code s))
:satisfies (lambda(result)
(& (symbolp result)
(null(symbol-package result))
(equal "no-such-package:symbol" (get result 'trivial-formatter::notation))))
; ECL specific guard.
#+ecl
#?(subtypep
(type-of
(nth-value 1 (ignore-errors (read-from-string "no-such-package:symbol"))))
'package-error)
=> NIL
; * If the specified package name is different from the actual package name, such a symbol will have a mark.
; This prevents e.g. closer-mop symbols become underlying implementation-dependent symbols.
#?(with-input-from-string(s "ppcre:regex-replace")
(read-as-code s))
:satisfies (lambda(result)
(&(eq result 'ppcre:regex-replace)
(equal "ppcre:regex-replace" (get result 'trivial-formatter::notation))))
; * If explicitly specify it is an internal symbol (i.e. p::s), such symbol is marked
; because there may a reason.
; E.g. It is external after compile but internal in compile time.
#?(with-input-from-string (s "asdf::defsystem")
(read-as-code s))
:satisfies (lambda (result)
(& (eq result 'asdf:defsystem)
(equal "asdf::defsystem" (get result 'trivial-formatter::notation))))
#?(with-input-from-string(s "; line comment.")
(read-as-code s))
:be-the trivial-formatter::line-comment
#?(with-input-from-string(s "#|block comment|#")
(read-as-code s))
:be-the trivial-formatter::block-comment
#?(with-input-from-string(s "(dot . list)")
(read-as-code s))
:satisfies (lambda(result)
(& (listp result)
(= 3 (length result))
(typep (second result) 'trivial-formatter::dot)
(string= #.(format nil "(dot . list)")
(with-output-to-string(s)
(print-as-code result s)))))
#?(with-input-from-string(s "hoge")
(read-as-code s nil #\h))
=> HOGE
#?(with-input-from-string (s ".3")
(read-as-code s))
=> 0.3
#?(with-input-from-string (s ".symbol")
(read-as-code s))
=> .SYMBOL
;;;; Affected By:
; `*readtable*`
;;;; Side-Effects:
; Consume stream contents.
;;;; Notes:
;;; Known bug.
; * Not support comment in #S, #A, reader macros.
; Comment will be discarded.
; Comment in the vector is supported.
#?(with-input-from-string (s "#(;comment
)")
(print-as-code (read-as-code s)))
:outputs "#( ; comment
)"
#?(with-input-from-string (s (format nil "#(a;~%b;~%c;~%)"))
(print-as-code (read-as-code s)))
:outputs "#(a ;
b ;
c ;
)"
;;;; Exceptional-Situations:
(requirements-about PRINT-AS-CODE :doc-type function)
;;;; Description:
; Print intermediate expression as lisp source code.
#+syntax
(PRINT-AS-CODE exp &optional stream) ; => result
;;;; Arguments and Values:
; exp := t
; stream := (or null stream) otherwise the condition depending on implementation is signaled.
#?(print-as-code :dummy :not-stream) :signals condition
; result := null
;;;; Affected By:
; `*print-pprint-dispatch*`
; `*strict-loop-keyword-p*`, when t, loop macro keywords are forced to be keyword symbols.
;;;; Side-Effects:
; Output to `STREAM`.
;;;; Notes:
; These printings are heavily dependent on the implementation pretty-printing feature.
; It means different outputs in a different implementation.
;;;; Exceptional-Situations:
;;;; Tests.
; 1. After line comment, a line break is required.
; 2. Space may be put if needed.
#?(with-input-from-string(s #.(format nil "(dummy; comment~%)"))
(print-as-code (read-as-code s)))
:outputs
"(dummy ; comment
)"
; Handle read time refer.
#?(with-input-from-string(s "(#0=#:hoge #0#)")
(print-as-code (read-as-code s)))
:outputs
"(#0=#:hoge #0#)"
; Circular list.
#?(with-input-from-string(s "#0=(dummy . #0#)")
(print-as-code (read-as-code s)))
:outputs
"#0=(dummy . #0#)"
;;;; Guards.
#?(let((*print-pretty* t))
(format nil "~<~:@_~:>"nil))
=> "
"
,:test equal
; Corner case with symbol.
; The result is different depending on implementation.
; e.g. "|\"|" or "\\\\"".
#?(print-as-code '|"|)
:outputs #.(prin1-to-string '|"|)
; Corner case of declaration with comma. Especially for SBCL.
#?(with-input-from-string (s "`(declare ,'nil)")
(print-as-code (read-as-code s)))
:outputs "`(declare ,'nil)"
;; Corner case of dots, especially when *read-suppress* t.
#?(with-input-from-string (s "(a ...)")
(print-as-code (read-as-code s)))
:outputs "(a ...)"
;; Corner case of block-comment, especially reader-macro syntax in the comment.
#?(with-input-from-string (s "#| '#0=(1 2 3 . #0#) |#")
(print-as-code (read-as-code s)))
:outputs "#| '#0=(1 2 3 . #0#) |#"
(requirements-about PPRINT-EXTENDED-LOOP :doc-type function
:around(let((*print-pretty* t))
(call-body)))
;;;; Description:
#+syntax
(PPRINT-EXTENDED-LOOP stream list) ; => result
;;;; Arguments and Values:
; stream :=
; list :=
; result :=
;;;; Affected By:
; `*strict-loop-keyword-p*` when t, loop macro keywords are forced to be keyword symbols.
;;;; Side-Effects:
;;;; Notes:
;;;; Exceptional-Situations:
;;;; TESTS
#?(pprint-extended-loop nil '(loop))
:outputs "(LOOP)"
; CLHS 6.1.1.1.1 Simple loop.
#?(pprint-extended-loop nil '(loop (print :hoge *standard-output*)
(print :hoge *standard-output*)
(print :hoge *standard-output*)))
:outputs "(LOOP (PRINT :HOGE *STANDARD-OUTPUT*)
(PRINT :HOGE *STANDARD-OUTPUT*)
(PRINT :HOGE *STANDARD-OUTPUT*))"
; CLHS 6.1.1.3
#?(pprint-extended-loop nil '(loop for i from 1 to (compute-top-value) ; first clause
while (not (unacceptable i)) ; second clause
collect (square i) ; third clause
do (format t "Working on ~D now" i) ; fourth clause
when (evenp i) ; fifth clause
do (format t "~D is a non-odd number" i)
finally (format t "About to exit!"))) ; sixth clause
:outputs "(LOOP FOR I FROM 1 TO (COMPUTE-TOP-VALUE)
WHILE (NOT (UNACCEPTABLE I))
COLLECT (SQUARE I)
DO (FORMAT T \"Working on ~D now\" I)
WHEN (EVENP I)
DO (FORMAT T \"~D is a non-odd number\" I)
FINALLY (FORMAT T \"About to exit!\"))"
; CLHS 6.1.1.7
#?(pprint-extended-loop nil '(loop for (x y) of-type (vector fixnum)
in (list :super :long :list :like :this :more)
do))
:outputs "(LOOP FOR (X Y) OF-TYPE (VECTOR FIXNUM)
IN (LIST :SUPER :LONG :LIST :LIKE :THIS :MORE)
DO)"
; CLHS 6.1.1.7
#?(pprint-extended-loop nil '(loop for numlist in '((1 2 4.0) (5 6 8.3) (8 9 10.4))
for a of-type integer = (first numlist)
and b of-type integer = (second numlist)
and c of-type float = (third numlist)
collect (list c b a)))
:outputs "(LOOP FOR NUMLIST IN '((1 2 4.0) (5 6 8.3) (8 9 10.4))
FOR A OF-TYPE INTEGER = (FIRST NUMLIST)
AND B OF-TYPE INTEGER = (SECOND NUMLIST)
AND C OF-TYPE FLOAT = (THIRD NUMLIST)
COLLECT (LIST C B A))"
; CLHS 6.1.1.7
#?(pprint-extended-loop nil '(loop with (a b) of-type float = '(1.0 2.0)
and (c d) of-type integer = '(3 4)
and (e f)
return (list a b c d e f)))
:outputs "(LOOP WITH (A B) OF-TYPE FLOAT = '(1.0 2.0)
AND (C D) OF-TYPE INTEGER = '(3 4)
AND (E F)
RETURN (LIST A B C D E F))"
; CLHS 6.1.3.2
#?(pprint-extended-loop nil '(loop for i upfrom 0
as x in '(a b (c))
nconc (if (evenp i) (list x) nil)))
:outputs #.(format nil "(LOOP FOR I UPFROM 0
AS X IN '(A B (C))
NCONC ~A)" '(if (evenp i)(list x)nil))
#?(pprint-extended-loop nil '(loop for i from 1 to 3
do (print i *standard-output*)
(print i *standard-output*)
(print (* i i) *standard-output*)))
:outputs "(LOOP FOR I FROM 1 TO 3
DO (PRINT I *STANDARD-OUTPUT*)
(PRINT I *STANDARD-OUTPUT*)
(PRINT (* I I) *STANDARD-OUTPUT*))"
; CLHS 6.1.8.1
#?(pprint-extended-loop nil '(loop for i in '(1 324 2345 323 2 4 235 252)
when (oddp i)
do (print i *standard-output*)
and collect i into odd-numbers
and do (terpri)
else ; I is even.
collect i into even-numbers
finally
(return (values odd-numbers even-numbers))))
:outputs "(LOOP FOR I IN '(1 324 2345 323 2 4 235 252)
WHEN (ODDP I)
DO (PRINT I *STANDARD-OUTPUT*)
AND COLLECT I INTO ODD-NUMBERS
AND DO (TERPRI)
ELSE
COLLECT I INTO EVEN-NUMBERS
FINALLY (RETURN (VALUES ODD-NUMBERS EVEN-NUMBERS)))"
#?(trivial-formatter::make-loop-clauses (cdr '(loop for i in '(1 324 2345 323 2 4 235 252)
when (oddp i)
do (print i *standard-output*)
and collect i into odd-numbers
and do (terpri)
else ; I is even.
collect i into even-numbers
finally
(return (values odd-numbers even-numbers)))))
:satisfies
(lambda (list)
(& (equalp list
(list (trivial-formatter::make-clause :keyword 'for :forms '(i in '(1 324 2345 323 2 4 235 252)))
(trivial-formatter::make-nestable :keyword 'when :pred '(oddp i))
(trivial-formatter::make-clause :keyword 'do :forms '((print i *standard-output*)))
(trivial-formatter::make-clause :keyword 'and)
(trivial-formatter::make-clause :keyword 'collect :forms '(i into odd-numbers))
(trivial-formatter::make-clause :keyword 'and)
(trivial-formatter::make-clause :keyword 'do :forms '((terpri)))
(trivial-formatter::make-clause :keyword 'else)
(trivial-formatter::make-clause :keyword 'collect :forms '(i into even-numbers))
(trivial-formatter::make-clause :keyword 'finally :forms '((return (values odd-numbers even-numbers))))))))
; CLHS 6.1.8.1
#?(pprint-extended-loop nil '(loop for i in list
when (numberp i)
when (floatp i)
collect i into float-numbers
else ; Not (floatp i)
collect i into other-numbers
else ; Not (numberp i)
when (symbolp i)
collect i into symbol-list
else ; Not (symbolp i)
do (error "found a funny value in list ~S, value ~S~%" list i)
finally (return (values float-numbers other-numbers symbol-list))))
:outputs "(LOOP FOR I IN LIST
WHEN (NUMBERP I)
WHEN (FLOATP I)
COLLECT I INTO FLOAT-NUMBERS
ELSE
COLLECT I INTO OTHER-NUMBERS
ELSE WHEN (SYMBOLP I)
COLLECT I INTO SYMBOL-LIST
ELSE
DO (ERROR \"found a funny value in list ~S, value ~S~%\" LIST I)
FINALLY (RETURN (VALUES FLOAT-NUMBERS OTHER-NUMBERS SYMBOL-LIST)))"
#?(trivial-formatter::make-loop-clauses (cdr
'(loop for i in list
when (numberp i)
when (floatp i)
collect i into float-numbers
else ; Not (floatp i)
collect i into other-numbers
else ; Not (numberp i)
when (symbolp i)
collect i into symbol-list
else ; Not (symbolp i)
do (error "found a funny value in list ~S, value ~S~%" list i)
finally (return (values float-numbers other-numbers symbol-list)))))
:satisfies
(lambda (list)
(& (equalp list
(list (trivial-formatter::make-clause :keyword 'for :forms '(i in list))
(trivial-formatter::make-nestable :keyword 'when :pred '(numberp i))
(trivial-formatter::make-nestable :keyword 'when :pred '(floatp i))
(trivial-formatter::make-clause :keyword 'collect :forms '(i into float-numbers))
(trivial-formatter::make-clause :keyword 'else)
(trivial-formatter::make-clause :keyword 'collect :forms '(i into other-numbers))
(trivial-formatter::make-clause :keyword 'else)
(trivial-formatter::make-nestable :keyword 'when :pred '(symbolp i))
(trivial-formatter::make-clause :keyword 'collect :forms '(i into symbol-list))
(trivial-formatter::make-clause :keyword 'else)
(trivial-formatter::make-clause :keyword 'do :forms '((error "found a funny value in list ~S, value ~S~%" list i)))
(trivial-formatter::make-clause :keyword 'finally :forms '((return (values float-numbers other-numbers symbol-list))))))))
; CLHS 6.1.8.1
#?(pprint-extended-loop nil '(loop for x from 0 to 3
do (print x)
if (zerop (mod x 2))
do (princ " a")
and if (zerop (floor x 2))
do (princ " b")
end
and do (princ " c")))
:outputs "(LOOP FOR X FROM 0 TO 3
DO (PRINT X)
IF (ZEROP (MOD X 2))
DO (PRINC \" a\")
AND IF (ZEROP (FLOOR X 2))
DO (PRINC \" b\")
END
AND DO (PRINC \" c\"))"
#?(trivial-formatter::make-loop-clauses (cdr '(loop for x from 0 to 3
do (print x)
if (zerop (mod x 2))
do (princ " a")
and if (zerop (floor x 2))
do (princ " b")
end
and do (princ " c"))))
:satisfies (lambda (list)
(& (equalp list (list (trivial-formatter::make-clause :keyword 'for :forms '(x from 0 to 3))
(trivial-formatter::make-clause :keyword 'do :forms '((print x)))
(trivial-formatter::make-nestable :keyword 'if :pred '(zerop (mod x 2)))
(trivial-formatter::make-clause :keyword 'do :forms '((princ " a")))
(trivial-formatter::make-clause :keyword 'and)
(trivial-formatter::make-nestable :keyword 'if :pred '(zerop (floor x 2)))
(trivial-formatter::make-clause :keyword 'do :forms '((princ " b")))
(trivial-formatter::make-clause :keyword 'end)
(trivial-formatter::make-clause :keyword 'and)
(trivial-formatter::make-clause :keyword 'do :forms '((princ " c")))))))
;; Case symbol confliction with loop macro keywords.
#?(pprint-extended-loop nil '(loop for count in counts collect count))
:outputs "(LOOP FOR COUNT IN COUNTS
COLLECT COUNT)"
#?(trivial-formatter::make-loop-clauses '(for count in counts collect count))
:satisfies
(lambda (list)
(& (equalp list
(list (trivial-formatter::make-clause :keyword 'for :forms '(count in counts))
(trivial-formatter::make-clause :keyword 'collect :forms '(count))))))
;; Corner case. If after nil.
#?(pprint-extended-loop nil '(loop :with flag = nil :if))
:outputs "(LOOP :WITH FLAG = NIL
:IF)"
#?(pprint-extended-loop nil '(loop :with flag = nil :if nil))
:outputs "(LOOP :WITH FLAG = NIL
:IF NIL)"
#?(trivial-formatter::make-loop-clauses '(with flag = nil if))
:satisfies
(lambda (list)
(& (equalp list
(list (trivial-formatter::make-clause :keyword 'with :forms '(flag = nil))
(trivial-formatter::make-clause :keyword 'if)))))
#?(PPRINT-EXTENDED-LOOP NIL
'(LOOP :NAMED NAME
:FOR I :BELOW 10))
:outputs "(LOOP :NAMED NAME
:FOR I :BELOW 10)"
;; Corner case. Nested.
#?(PRINT-AS-CODE
'(LOOP :WHEN (ODDP X)
:DO (LOOP :WHEN (ODDP Y)
:DO (PRINT (LIST X Y)))))
:outputs "(loop :when (oddp x)
:do (loop :when (oddp y)
:do (print (list x y))))"
;; Corner case. Variable that is conflict with loop macro keyword.
#?(PPRINT-EXTENDED-LOOP NIL
'(LOOP :FOR I :UPFROM START :BELOW
END
:COLLECT I))
:outputs "(LOOP :FOR I :UPFROM START :BELOW END
:COLLECT I)"
;; Comment in the loop.
;; Line comment's indent should belong to the next line's indent.
#?(let* ((source (format nil "(loop if pred ;; comment~%do (something))"))
(code (with-input-from-string (in source)
(read-as-code in))))
(print-as-code code))
:outputs "(loop if pred
;; comment
do (something))"
;; Corner case. :ELSE clause with :AND clause.
#?(pprint-extended-loop nil
'(loop :for i :below 10
:if (evenp i)
:collect i
:else
:collect i
:and :do (print i)
:and :collect (1+ i)))
:outputs
"(LOOP :FOR I :BELOW 10
:IF (EVENP I)
:COLLECT I
:ELSE
:COLLECT I
:AND :DO (PRINT I)
:AND :COLLECT (1+ I))"
;; Corner case. VAR conflicts loop macro keyword.
#?(pprint-extended-loop nil '(loop :with last-end =
end))
:outputs
"(LOOP :WITH LAST-END = END)"
(requirements-about SPLIT-KEYWORDS :doc-type function)
;;;; Description:
#+syntax
(SPLIT-KEYWORDS exp) ; => result
;;;; Arguments and Values:
; exp :=
; result :=
;;;; Affected By:
;;;; Side-Effects:
;;;; Notes:
;;;; Exceptional-Situations:
;;;; Tests:
#?(split-keywords '(call)) :values ((call)nil)
#?(split-keywords '(call :key)) :values ((call :key)nil)
#?(split-keywords '(call :key :value)) :values ((call)(:key :value))
#?(split-keywords '(call :k1 :v1 :k2 :v2)) :values ((call)(:k1 :v1 :k2 :v2))
#?(split-keywords '(call :k1 :k2 :k3)) :values ((call :k1)(:k2 :k3))
#?(split-keywords '(call :k1 :k2 :k3 :k4 :k5)) :values ((call :k1)(:k2 :k3 :k4 :k5))
#?(split-keywords '(call :k1 v1 rest)) :values ((call :k1 v1 rest) nil)
#?(split-keywords '(call not-key :k1 v1)) :values ((call not-key)(:k1 v1))
(requirements-about PPRINT-FUN-CALL :doc-type function
:around(let((*print-pretty* t))
(call-body)))
;;;; Description:
#+syntax
(PPRINT-FUN-CALL stream exp) ; => result
;;;; Arguments and Values:
; stream :=
; exp :=
; result :=
;;;; Affected By:
;;;; Side-Effects:
;;;; Notes:
;;;; Exceptional-Situations:
;;;; Tests:
#?(pprint-fun-call nil '(call)) :outputs "(CALL)"
#?(pprint-fun-call nil '(call arg)) :outputs "(CALL ARG)"
#?(pprint-fun-call nil '(asdf:component-pathname component
:direction :output
:if-does-not-exist :create
:if-exists if-exists))
:outputs
"(ASDF/COMPONENT:COMPONENT-PATHNAME COMPONENT
:DIRECTION :OUTPUT
:IF-DOES-NOT-EXIST :CREATE
:IF-EXISTS IF-EXISTS)"
#?(pprint-fun-call nil '(asdf:component-pathname :direction :output
:if-does-not-exist :create
:if-exists if-exists))
:outputs
"(ASDF/COMPONENT:COMPONENT-PATHNAME :DIRECTION :OUTPUT
:IF-DOES-NOT-EXIST :CREATE
:IF-EXISTS IF-EXISTS)"
#?(pprint-fun-call nil '(call :k v)) :outputs "(CALL :K V)"
#?(pprint-fun-call nil '(member nil :append :supersede :rename :error :new-version))
:outputs
"(MEMBER NIL :APPEND :SUPERSEDE :RENAME :ERROR :NEW-VERSION)"
#?(PPRINT-FUN-CALL NIL NIL)
:outputs "()"
(requirements-about PPRINT-DEFINE-CONDITION :doc-type function
:around (let((*print-pretty* t))
(call-body)))
;;;; Description:
#+syntax
(PPRINT-DEFINE-CONDITION stream exp &rest noise) ; => result
;;;; Arguments and Values:
; stream :=
; exp :=
; noise :=
; result :=
;;;; Affected By:
;;;; Side-Effects:
;;;; Notes:
;;;; Exceptional-Situations:
;;;; Tests:
#?(pprint-define-condition
nil
'(define-condition syntax-error(error)
((whole :initform nil :initarg :whole :reader whole-form<=syntax-error)
(definitions :initform nil :initarg :definitions :reader bnf-definitions))
(:report (lambda(condition stream)
(princ condition stream)))
(:default-initargs :format-control "")))
:outputs "(DEFINE-CONDITION SYNTAX-ERROR (ERROR)
((WHOLE :INITFORM NIL :INITARG :WHOLE :READER WHOLE-FORM<=SYNTAX-ERROR)
(DEFINITIONS :INITFORM NIL :INITARG :DEFINITIONS :READER BNF-DEFINITIONS))
(:REPORT (LAMBDA (CONDITION STREAM) (PRINC CONDITION STREAM)))
(:DEFAULT-INITARGS :FORMAT-CONTROL \"\"))"
#?(PPRINT-DEFINE-CONDITION NIL '(DEFINE-CONDITION))
:outputs "(DEFINE-CONDITION)"
#?(PPRINT-DEFINE-CONDITION NIL '(DEFINE-CONDITION NAME))
:outputs "(DEFINE-CONDITION NAME)"
#?(PPRINT-DEFINE-CONDITION NIL
'(DEFINE-CONDITION NAME
SUPERCLASSES))
:outputs "(DEFINE-CONDITION NAME SUPERCLASSES)"
#?(PPRINT-DEFINE-CONDITION NIL
'(DEFINE-CONDITION NAME
SUPERCLASSES
SLOTS))
:outputs "(DEFINE-CONDITION NAME SUPERCLASSES SLOTS)"
#?(PPRINT-DEFINE-CONDITION NIL
'(DEFINE-CONDITION NAME
SUPERCLASSES
SLOTS
ADDITIONAL
OPTIONS))
:outputs "(DEFINE-CONDITION NAME SUPERCLASSES SLOTS ADDITIONAL OPTIONS)"
(requirements-about PPRINT-RESTART-CASE :doc-type function
:around (let ((*print-pretty* t))
(call-body)))
;;;; Description:
#+syntax
(PPRINT-RESTART-CASE stream exp) ; => result
;;;; Arguments and Values:
; stream :=
; exp :=
; result :=
;;;; Affected By:
;;;; Side-Effects:
;;;; Notes:
;;;; Exceptional-Situations:
;;;; Tests:
#?(pprint-restart-case nil '(restart-case))
:outputs "(RESTART-CASE)"
#?(pprint-restart-case nil '(restart-case form))
:outputs "(RESTART-CASE FORM)"
#?(pprint-restart-case nil '(restart-case form nil))
:outputs "(RESTART-CASE FORM NIL)"
#?(pprint-restart-case nil '(restart-case form (restart)))
:outputs "(RESTART-CASE FORM (RESTART))"
#?(pprint-restart-case nil '(restart-case form (restart nil)))
:outputs "(RESTART-CASE FORM (RESTART ()))"
#?(pprint-restart-case nil '(restart-case form (restart (var))))
:outputs "(RESTART-CASE FORM (RESTART (VAR)))"
#?(pprint-restart-case nil '(restart-case form (restart (var) var)))
:outputs "(RESTART-CASE FORM (RESTART (VAR) VAR))"
#?(pprint-restart-case nil '(restart-case (funcall *macroexpand-hook*)
(dribble()
:report "Return to dribble.")))
:outputs
"(RESTART-CASE (FUNCALL *MACROEXPAND-HOOK*)
(DRIBBLE () :REPORT \"Return to dribble.\"))"
#?(pprint-restart-case nil '(restart-case(error 'unexpected-behavior)
(use-value(expected)
:report "Specify expected output"
:interactive #'read
expected)))
:outputs
"(RESTART-CASE (ERROR 'UNEXPECTED-BEHAVIOR)
(USE-VALUE (EXPECTED)
:REPORT \"Specify expected output\"
:INTERACTIVE #'READ
EXPECTED))"
#?(PPRINT-RESTART-CASE NIL
'(RESTART-CASE NIL
(TEST NIL :REPORT "this is report" (RETURN))))
:outputs "(RESTART-CASE NIL
(TEST ()
:REPORT \"this is report\"
(RETURN)))"
#?(pprint-restart-case nil '(restart-case ()
(use-value (&rest args) ; <--- Some elt in the lambda-list.
:report "report"
:interactive (lambda () (list (read *query-io*)))
args)))
:outputs "(RESTART-CASE NIL
(USE-VALUE (&REST ARGS)
:REPORT \"report\"
:INTERACTIVE (LAMBDA () (LIST (READ *QUERY-IO*)))
ARGS))"
(requirements-about PPRINT-WITH-OPEN-FILE :doc-type function
:around (let((*print-pretty* t))
(call-body)))
;;;; Description:
#+syntax
(PPRINT-WITH-OPEN-FILE stream exp) ; => result
;;;; Arguments and Values:
; stream :=
; exp :=
; result :=
;;;; Affected By:
;;;; Side-Effects:
;;;; Notes:
;;;; Exceptional-Situations:
;;;; Tests:
#?(pprint-with-open-file nil '(with-open-file))
:outputs "(WITH-OPEN-FILE)"
#?(pprint-with-open-file nil '(with-open-file :not-list))
:outputs "(WITH-OPEN-FILE :NOT-LIST)"
#?(pprint-with-open-file nil '(with-open-file nil))
:outputs "(WITH-OPEN-FILE ())"
#?(pprint-with-open-file nil '(with-open-file
(*standard-output* #P"hoge" :direction
:output :if-does-not-exist :create :if-exists if-exists)
(write-string string)))
:outputs
"(WITH-OPEN-FILE (*STANDARD-OUTPUT* #P\"hoge\" :DIRECTION :OUTPUT
:IF-DOES-NOT-EXIST :CREATE
:IF-EXISTS IF-EXISTS)
(WRITE-STRING STRING))"
#?(pprint-with-open-file nil '(with-open-file(*spec-output* *default-pathname-defaults*
:direction :output
:if-exists :append)
(funcall appender)))
:outputs
"(WITH-OPEN-FILE (*SPEC-OUTPUT* *DEFAULT-PATHNAME-DEFAULTS* :DIRECTION :OUTPUT
:IF-EXISTS :APPEND)
(FUNCALL APPENDER))"
(requirements-about SPLIT-TO-LINES :doc-type function)
;;;; Description:
#+syntax
(SPLIT-TO-LINES string) ; => result
;;;; Arguments and Values:
; string :=
; result :=
;;;; Affected By:
;;;; Side-Effects:
;;;; Notes:
;;;; Exceptional-Situations:
;;;; Tests:
#?(split-to-lines "; |")
=> ("; |")
,:test equalp
(requirements-about PPRINT-COND :doc-type function
:around (let ((*print-pretty* t))
(call-body)))
;;;; Description:
#+syntax
(PPRINT-COND stream exp) ; => result
;;;; Arguments and Values:
; stream :=
; exp :=
; result :=
;;;; Affected By:
;;;; Side-Effects:
;;;; Notes:
;;;; Exceptional-Situations:
#?(PPRINT-COND *STANDARD-OUTPUT* '(COND NIL NIL)) :outputs "(COND () ())"
#?(PPRINT-COND NIL '(COND))
:outputs "(COND)"
#?(PPRINT-COND NIL '(COND CLAUSE))
:outputs "(COND CLAUSE)"
#?(PPRINT-COND NIL '(COND CLAUSE CLAUSE2))
:outputs "(COND CLAUSE CLAUSE2)"
#?(PPRINT-COND NIL '(COND CLAUSE (T)))
:outputs "(COND CLAUSE (T))"
#?(PPRINT-COND NIL
'(COND ((EVENP I) (DO-IT-FOR-EVEN I))
((ODDP I) (DO-IT-FOR-ODD I)) (T (DO-IT) (FOR-OTHERS))))
:outputs "(COND ((EVENP I) (DO-IT-FOR-EVEN I))
((ODDP I) (DO-IT-FOR-ODD I))
(T
(DO-IT)
(FOR-OTHERS)))"
(requirements-about PPRINT-FLET :doc-type function
:around (let ((*print-pretty* t))
(call-body)))
;;;; Description:
#+syntax
(PPRINT-FLET stream exp) ; => result
;;;; Arguments and Values:
; stream :=
; exp :=
; result :=
;;;; Affected By:
;;;; Side-Effects:
;;;; Notes:
;;;; Exceptional-Situations:
#?(pprint-flet nil '(flet)) :outputs "(FLET)"
#?(PPRINT-FLET NIL '(FLET T))
:outputs #.(let ((*print-pretty* t))
(prin1-to-string '(flet t)))
#+ecl ; as guard.
#?(prin1-to-string '(flet t))
=> "(FLET T
)"
,:test equal
#?(pprint-flet NIL
'(flet ((a nil) (b nil))
(+ (a) (b))))
:outputs
"(FLET ((A ())
(B ()))
(+ (A) (B)))"
#?(pprint-flet nil
'(macrolet ((with-check ((var form) &body body)
todo))
(labels ((check-first ()
(with-check (first (read-char input nil nil))