-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclweb.lisp
3438 lines (3437 loc) · 155 KB
/
clweb.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
;;;; TANGLED WEB FROM "clweb.clw". DO NOT EDIT.
(EVAL-WHEN (:COMPILE-TOPLEVEL :LOAD-TOPLEVEL :EXECUTE)
#+:SBCL (require "SB-CLTL2"))
(DEFPACKAGE "CLWEB"
(:DOCUMENTATION "A literate programming system for Common Lisp.")
(:USE "COMMON-LISP")
(:EXPORT "TANGLE-FILE"
"LOAD-WEB"
"WEAVE"
"LOAD-SECTIONS-FROM-TEMP-FILE"
"INPUT-FILE-PATHNAME"
"LISP-FILE-PATHNAME"
"TEX-FILE-PATHNAME"
"INDEX-FILE-PATHNAME"
"SECTIONS-FILE-PATHNAME"
"FASL-FILE-PATHNAME"
"TESTS-FILE-PATHNAME"
"TANGLE-FILE-PATHNAMES"
"WEAVE-PATHNAMES"
"*WEB-PATHNAME-DEFAULTS*"
"*LISP-PATHNAME-DEFAULTS*"
"*TEX-PATHNAME-DEFAULTS*"
"*INDEX-PATHNAME-DEFAULTS*"
"*SECTIONS-PATHNAME-DEFAULTS*"
"*TESTS-FILE-PATHNAME-FUNCTION*"
"*COMPILE-TESTS-FILE*"
"*TANGLE-FILE-PATHNAME*"
"*TANGLE-FILE-TRUENAME*"
"*WEAVE-PRINT*"
"*WEAVE-VERBOSE*"
"*INDEX-LEXICAL-VARIABLES*"
"AMBIGUOUS-PREFIX-ERROR"
"SECTION-NAME-CONTEXT-ERROR"
"SECTION-NAME-USE-ERROR"
"SECTION-NAME-DEFINITION-ERROR"
"UNUSED-NAMED-SECTION-WARNING"
"CLWEB-FILE"
"WEAVE-OP")
#+(:OR :SBCL :CCL :ALLEGRO)
(:import-from #+sbcl "SB-CLTL2" #+ccl "CCL" #+allegro "SYS"
#-allegro "FUNCTION-INFORMATION"
#-allegro "VARIABLE-INFORMATION"
#-allegro "PARSE-MACRO"
"AUGMENT-ENVIRONMENT")
#+:SBCL
(:import-from "SB-INT" "NAMED-LAMBDA"))
(IN-PACKAGE "CLWEB")
(DEFVAR *PHYSICAL-PATHNAME-HOST* #+:SBCL "" #-:SBCL :unspecific)
(DEFUN MAKE-DEFAULT-PATHNAME (TYPE)
(MAKE-PATHNAME :TYPE TYPE :HOST *PHYSICAL-PATHNAME-HOST* :VERSION :NEWEST))
(DEFVAR *WEB-PATHNAME-DEFAULTS* (MAKE-DEFAULT-PATHNAME "clw"))
(DEFVAR *LISP-PATHNAME-DEFAULTS* (MAKE-DEFAULT-PATHNAME "lisp"))
(DEFVAR *TEX-PATHNAME-DEFAULTS* (MAKE-DEFAULT-PATHNAME "tex"))
(DEFVAR *INDEX-PATHNAME-DEFAULTS* (MAKE-DEFAULT-PATHNAME "idx"))
(DEFVAR *SECTIONS-PATHNAME-DEFAULTS* (MAKE-DEFAULT-PATHNAME "scn"))
(DEFVAR *TESTS-FILE-PATHNAME-FUNCTION*
(LAMBDA
(INPUT-FILE OUTPUT-FILE
&AUX (OUTPUT-FILE (PATHNAME OUTPUT-FILE))
(TESTS-FILE
(MAKE-PATHNAME :NAME
(CONCATENATE 'STRING (PATHNAME-NAME OUTPUT-FILE)
"-tests")
:VERSION :NEWEST :DEFAULTS OUTPUT-FILE)))
(OUTPUT-FILE-PATHNAME INPUT-FILE :OUTPUT-FILE TESTS-FILE :DEFAULTS
OUTPUT-FILE)))
(DEFVAR *SECTIONS* (MAKE-ARRAY 128 :ADJUSTABLE T :FILL-POINTER 0))
(DEFVAR *CURRENT-SECTION* NIL)
(DEFVAR *TEST-SECTIONS* (MAKE-ARRAY 128 :ADJUSTABLE T :FILL-POINTER 0))
(DEFVAR *NAMED-SECTIONS* NIL)
(DEFPARAMETER *WHITESPACE*
#.(COERCE
(REMOVE-DUPLICATES
(REMOVE NIL
(MAPCAR #'NAME-CHAR
'("Newline" "Space" "Tab" "Linefeed" "Return" "Page"))))
'STRING))
(EVAL-WHEN (:COMPILE-TOPLEVEL :LOAD-TOPLEVEL :EXECUTE)
(DEFPARAMETER *MODES* '(:LIMBO :TEX :LISP :INNER-LISP :RESTRICTED)))
(DEFTYPE MODE () `(MEMBER ,@*MODES*))
(DEFVAR *READTABLES*
(LOOP FOR MODE IN (CONS 'NIL *MODES*)
COLLECT (CONS MODE (COPY-READTABLE NIL))))
(DEFVAR *EOF* (MAKE-SYMBOL "EOF"))
(DEFPARAMETER *TAB-WIDTH* 8)
(DEFVAR *CHARPOS-STREAMS* (MAKE-HASH-TABLE :TEST #'EQ))
(DEFVAR *EVALUATING* NIL)
(DEFVAR *COMPILE-TESTS-FILE*
T
"The default value for the :COMPILE-TESTS-FILE argument to TANGLE-FILE.")
(DEFVAR *TANGLE-FILE-PATHNAME* NIL)
(DEFVAR *TANGLE-FILE-TRUENAME* NIL)
(DEFPARAMETER *TANGLE-PPRINT-DISPATCH* (COPY-PPRINT-DISPATCH NIL))
(DEFVAR *WEAVE-PATHNAME*)
(DEFVAR *WEAVE-VERBOSE* T "The default for the :VERBOSE argument to WEAVE.")
(DEFVAR *WEAVE-PRINT* T "The default for the :PRINT argument to WEAVE.")
(DEFVAR *WEAVE-SOURCE-LOCATIONS*
T
"If true, include src: specials in the woven TeX.")
(DEFPARAMETER *WEAVE-PPRINT-DISPATCH* (COPY-PPRINT-DISPATCH NIL))
(DEFVAR *PRINT-SYMBOL-SUFFIXES*
'(("/=" . "$\\neq$") ("<=" . "$\\leq$") (">=" . "$\\geq$") ("+-" . "$\\pm$")
("+" . "$+$") ("-" . "$-$") ("=" . "$=$") ("'" . "$'$")))
(DEFVAR *INDEX-PACKAGES*
NIL
"The list of packages whose symbols should be indexed.")
(DEFVAR *INDEX* NIL)
(DEFVAR *INDEX-LEXICAL-VARIABLES*
NIL
"If this flag is non-nil, the indexer will index lexical variables.")
(DEFVAR *REFERRING-CLASSES* 'NIL)
(DEFVAR *INDEXING* NIL)
(DEFINE-CONDITION UNDEFINED-NAMED-SECTION-ERROR
(SIMPLE-ERROR)
NIL)
(DEFINE-CONDITION AMBIGUOUS-PREFIX-ERROR
(ERROR)
((PREFIX :READER AMBIGUOUS-PREFIX :INITARG :PREFIX)
(FIRST-MATCH :READER AMBIGUOUS-PREFIX-FIRST-MATCH :INITARG :FIRST-MATCH)
(ALT-MATCH :READER AMBIGUOUS-PREFIX-ALT-MATCH :INITARG :ALT-MATCH))
(:REPORT
(LAMBDA (CONDITION STREAM)
(FORMAT STREAM "~@<Ambiguous prefix: <~A> matches both <~A> and <~A>~:@>"
(AMBIGUOUS-PREFIX CONDITION)
(AMBIGUOUS-PREFIX-FIRST-MATCH CONDITION)
(AMBIGUOUS-PREFIX-ALT-MATCH CONDITION)))))
(DEFINE-CONDITION SIMPLE-READER-ERROR
(READER-ERROR SIMPLE-CONDITION)
NIL
(:REPORT
(LAMBDA (CONDITION STREAM)
(FORMAT STREAM "~S on ~S:~%~?" CONDITION (STREAM-ERROR-STREAM CONDITION)
(SIMPLE-CONDITION-FORMAT-CONTROL CONDITION)
(SIMPLE-CONDITION-FORMAT-ARGUMENTS CONDITION)))))
(DEFINE-CONDITION SECTION-NAME-CONTEXT-ERROR
(ERROR)
((NAME :READER SECTION-NAME :INITARG :NAME)))
(DEFINE-CONDITION SECTION-NAME-DEFINITION-ERROR
(SECTION-NAME-CONTEXT-ERROR)
NIL
(:REPORT
(LAMBDA (CONDITION STREAM)
(FORMAT STREAM "Can't define a named section in Lisp mode: ~A"
(SECTION-NAME CONDITION)))))
(DEFINE-CONDITION SECTION-NAME-USE-ERROR
(SECTION-NAME-CONTEXT-ERROR)
NIL
(:REPORT
(LAMBDA (CONDITION STREAM)
(FORMAT STREAM "Can't use a section name in TeX mode: ~A"
(SECTION-NAME CONDITION)))))
(DEFINE-CONDITION SECTION-LACKS-COMMENTARY
(PARSE-ERROR)
((STREAM :INITARG :STREAM :READER SECTION-LACKS-COMMENTARY-STREAM))
(:REPORT
(LAMBDA (ERROR STREAM)
(LET* ((INPUT-STREAM
(DO ((STREAM (SECTION-LACKS-COMMENTARY-STREAM ERROR)))
(NIL)
(TYPECASE STREAM
(ECHO-STREAM (SETQ STREAM (ECHO-STREAM-INPUT-STREAM STREAM)))
(T (RETURN STREAM)))))
(POSITION (FILE-POSITION INPUT-STREAM))
(PATHNAME
(WHEN (TYPEP INPUT-STREAM 'FILE-STREAM) (PATHNAME INPUT-STREAM))))
(FORMAT STREAM "~@<Can't start a section with a code part ~
~:[~;~:*at position ~D in file ~A.~]~:@>"
POSITION (OR PATHNAME INPUT-STREAM))))))
(DEFINE-CONDITION UNUSED-NAMED-SECTION-WARNING
(SIMPLE-WARNING)
NIL)
(DEFUN ENSURE-LIST (OBJECT)
(IF (LISTP OBJECT)
OBJECT
(LIST OBJECT)))
(DEFUN MAPTREE (FN X)
(IF (ATOM X)
(FUNCALL FN X)
(LET ((A (FUNCALL FN (CAR X))) (D (MAPTREE FN (CDR X))))
(IF (AND (EQL A (CAR X)) (EQL D (CDR X)))
X
(CONS A D)))))
(DEFUN MAPAPPEND (FN &REST ARGS)
(IF (SOME #'NULL ARGS)
NIL
(APPEND (APPLY FN (MAPCAR #'CAR ARGS))
(APPLY #'MAPAPPEND FN (MAPCAR #'CDR ARGS)))))
(DEFMACRO MAYBE-PUSH (OBJ PLACE &AUX (TEMP (GENSYM)))
`(LET ((,TEMP ,OBJ))
(WHEN
(IF (STRINGP ,TEMP)
(PLUSP (LENGTH ,TEMP))
,TEMP)
(PUSH ,TEMP ,PLACE))))
(EVAL-WHEN (:COMPILE-TOPLEVEL :LOAD-TOPLEVEL :EXECUTE)
(DEFMACRO WITH-UNIQUE-NAMES (SYMBOLS &BODY BODY)
`(LET ,(LOOP FOR SYMBOL IN SYMBOLS
COLLECT `(,SYMBOL (COPY-SYMBOL ',SYMBOL)))
,@BODY)))
(DEFUN INPUT-FILE-PATHNAME (INPUT-FILE)
(LET ((DEFAULTS (MERGE-PATHNAMES *WEB-PATHNAME-DEFAULTS*)))
(MERGE-PATHNAMES INPUT-FILE DEFAULTS)))
(DEFUN OUTPUT-FILE-PATHNAME
(INPUT-FILE &KEY OUTPUT-FILE (DEFAULTS *DEFAULT-PATHNAME-DEFAULTS*))
(ASSERT
(OR (AND OUTPUT-FILE (PATHNAME-TYPE OUTPUT-FILE)) (PATHNAME-TYPE DEFAULTS))
(DEFAULTS) "Missing default output file type.")
(LET* ((INPUT-FILE (INPUT-FILE-PATHNAME INPUT-FILE))
(DEFAULTS
(MERGE-PATHNAMES
(MAKE-PATHNAME :TYPE NIL :VERSION :NEWEST :DEFAULTS INPUT-FILE)
DEFAULTS)))
(IF OUTPUT-FILE
(MERGE-PATHNAMES OUTPUT-FILE DEFAULTS)
DEFAULTS)))
(MACROLET ((DEFDEFAULTS (NAME (INPUT) DEFAULTS)
(WITH-UNIQUE-NAMES (ARGS)
`(DEFUN ,NAME (,INPUT &REST ,ARGS)
(APPLY #'OUTPUT-FILE-PATHNAME ,INPUT :DEFAULTS ,DEFAULTS
,ARGS)))))
(DEFDEFAULTS LISP-FILE-PATHNAME (INPUT-FILE) *LISP-PATHNAME-DEFAULTS*)
(DEFDEFAULTS TEX-FILE-PATHNAME (INPUT-FILE) *TEX-PATHNAME-DEFAULTS*)
(DEFDEFAULTS INDEX-FILE-PATHNAME (OUTPUT-FILE) *INDEX-PATHNAME-DEFAULTS*)
(DEFDEFAULTS SECTIONS-FILE-PATHNAME (INDEX-FILE)
*SECTIONS-PATHNAME-DEFAULTS*))
(DEFUN FASL-FILE-PATHNAME (INPUT-FILE &REST ARGS)
(APPLY #'COMPILE-FILE-PATHNAME (LISP-FILE-PATHNAME INPUT-FILE)
:ALLOW-OTHER-KEYS T ARGS))
(DEFUN TESTS-FILE-PATHNAME
(INPUT-FILE
&KEY OUTPUT-FILE (TESTS-FILE NIL TESTS-FILE-SUPPLIED)
&ALLOW-OTHER-KEYS)
(IF TESTS-FILE
(OUTPUT-FILE-PATHNAME INPUT-FILE :OUTPUT-FILE TESTS-FILE :DEFAULTS
OUTPUT-FILE)
(UNLESS TESTS-FILE-SUPPLIED
(FUNCALL *TESTS-FILE-PATHNAME-FUNCTION* INPUT-FILE OUTPUT-FILE))))
(DEFUN TANGLE-FILE-PATHNAMES
(INPUT-FILE &REST ARGS &KEY OUTPUT-FILE TESTS-FILE &ALLOW-OTHER-KEYS)
"Compute and return the defaulted names of the tangler output files."
(DECLARE (IGNORABLE OUTPUT-FILE TESTS-FILE))
(LET* ((INPUT-FILE (INPUT-FILE-PATHNAME INPUT-FILE))
(OUTPUT-FILE (APPLY #'FASL-FILE-PATHNAME INPUT-FILE ARGS))
(LISP-FILE (LISP-FILE-PATHNAME OUTPUT-FILE))
(TESTS-FILE
(APPLY #'TESTS-FILE-PATHNAME INPUT-FILE :OUTPUT-FILE LISP-FILE ARGS))
(TESTS-OUTPUT-FILE (AND TESTS-FILE (FASL-FILE-PATHNAME TESTS-FILE))))
(VALUES OUTPUT-FILE LISP-FILE TESTS-OUTPUT-FILE TESTS-FILE)))
(DEFUN WEAVE-PATHNAMES
(INPUT-FILE
&KEY OUTPUT-FILE (INDEX-FILE NIL INDEX-FILE-SUPPLIED)
&ALLOW-OTHER-KEYS)
"Compute and return the defaulted names of the weaver output files."
(LET* ((INPUT-FILE (INPUT-FILE-PATHNAME INPUT-FILE))
(OUTPUT-FILE (TEX-FILE-PATHNAME INPUT-FILE :OUTPUT-FILE OUTPUT-FILE))
(INDEX-FILE
(UNLESS (AND INDEX-FILE-SUPPLIED (NOT INDEX-FILE))
(INDEX-FILE-PATHNAME OUTPUT-FILE :OUTPUT-FILE INDEX-FILE)))
(SECTIONS-FILE (AND INDEX-FILE (SECTIONS-FILE-PATHNAME INDEX-FILE))))
(VALUES OUTPUT-FILE INDEX-FILE SECTIONS-FILE)))
(DEFCLASS SECTION NIL
((NAME :ACCESSOR SECTION-NAME :INITARG :NAME)
(NUMBER :ACCESSOR SECTION-NUMBER)
(COMMENTARY :ACCESSOR SECTION-COMMENTARY :INITARG :COMMENTARY)
(CODE :ACCESSOR SECTION-CODE :INITARG :CODE)
(SOURCE-LOCATION :READER SECTION-SOURCE-LOCATION :INITARG
:SOURCE-LOCATION))
(:DEFAULT-INITARGS :NAME NIL :COMMENTARY NIL :CODE NIL
:SOURCE-LOCATION NIL))
(DEFCLASS STARRED-SECTION (SECTION)
((DEPTH :READER SECTION-DEPTH :INITARG :DEPTH))
(:DEFAULT-INITARGS :DEPTH 0))
(DEFUN STARRED-SECTION-P (OBJECT) (TYPEP OBJECT 'STARRED-SECTION))
(DEFMETHOD SECTION-DEPTH ((SECTION SECTION)) NIL)
(DEFCLASS TEST-SECTION (SECTION)
((TEST-FOR :ACCESSOR TEST-FOR-SECTION :INITFORM NIL)))
(DEFCLASS STARRED-TEST-SECTION (TEST-SECTION STARRED-SECTION) NIL)
(DEFUN TEST-SECTION-P (OBJECT) (TYPEP OBJECT 'TEST-SECTION))
(DEFMETHOD INITIALIZE-INSTANCE :AFTER ((SECTION TEST-SECTION) &KEY)
(WHEN (> (FILL-POINTER *SECTIONS*) 0)
(SETF (TEST-FOR-SECTION SECTION)
(ELT *SECTIONS* (1- (FILL-POINTER *SECTIONS*))))))
(DEFCLASS LIMBO-SECTION (SECTION) NIL)
(DEFGENERIC PUSH-SECTION
(SECTION))
(DEFMETHOD PUSH-SECTION ((SECTION SECTION))
(SETF (SECTION-NUMBER SECTION) (VECTOR-PUSH-EXTEND SECTION *SECTIONS*))
SECTION)
(DEFMETHOD INITIALIZE-INSTANCE :AFTER ((SECTION SECTION) &KEY)
(SETQ *CURRENT-SECTION* (PUSH-SECTION SECTION)))
(DEFMETHOD PUSH-SECTION ((SECTION TEST-SECTION))
(LET ((*SECTIONS* *TEST-SECTIONS*))
(CALL-NEXT-METHOD)))
(DEFMETHOD PUSH-SECTION :AFTER ((SECTION LIMBO-SECTION))
(VECTOR-PUSH-EXTEND SECTION *TEST-SECTIONS*))
(DEFCLASS BINARY-SEARCH-TREE NIL
((KEY :ACCESSOR NODE-KEY :INITARG :KEY)
(LEFT-CHILD :ACCESSOR LEFT-CHILD :INITARG :LEFT)
(RIGHT-CHILD :ACCESSOR RIGHT-CHILD :INITARG :RIGHT))
(:DEFAULT-INITARGS :LEFT NIL :RIGHT NIL))
(DEFGENERIC FIND-OR-INSERT
(ITEM ROOT &KEY PREDICATE TEST INSERT-IF-NOT-FOUND))
(DEFMETHOD FIND-OR-INSERT
(ITEM (ROOT BINARY-SEARCH-TREE)
&KEY (PREDICATE #'<) (TEST #'EQL) (INSERT-IF-NOT-FOUND T))
(FLET ((LESSP (ITEM NODE)
(FUNCALL PREDICATE ITEM (NODE-KEY NODE)))
(SAMEP (ITEM NODE)
(FUNCALL TEST ITEM (NODE-KEY NODE))))
(DO ((PARENT NIL NODE)
(NODE ROOT
(IF (LESSP ITEM NODE)
(LEFT-CHILD NODE)
(RIGHT-CHILD NODE))))
((OR (NULL NODE) (SAMEP ITEM NODE))
(IF NODE
(VALUES NODE T)
(IF INSERT-IF-NOT-FOUND
(LET ((NODE (MAKE-INSTANCE (CLASS-OF ROOT) :KEY ITEM)))
(WHEN PARENT
(IF (LESSP ITEM PARENT)
(SETF (LEFT-CHILD PARENT) NODE)
(SETF (RIGHT-CHILD PARENT) NODE)))
(VALUES NODE NIL))
(VALUES NIL NIL)))))))
(DEFGENERIC MAP-BST
#'TREE)
(DEFMETHOD MAP-BST (FUNCTION (TREE NULL)) (DECLARE (IGNORE FUNCTION)))
(DEFMETHOD MAP-BST (FUNCTION (TREE BINARY-SEARCH-TREE))
(MAP-BST FUNCTION (LEFT-CHILD TREE))
(FUNCALL FUNCTION TREE)
(MAP-BST FUNCTION (RIGHT-CHILD TREE)))
(DEFCLASS NAMED-SECTION (BINARY-SEARCH-TREE)
((KEY :ACCESSOR SECTION-NAME :INITARG :NAME)
(SECTIONS :ACCESSOR NAMED-SECTION-SECTIONS :INITFORM 'NIL)
(USED-BY :ACCESSOR USED-BY :INITFORM 'NIL)
(CITED-BY :ACCESSOR CITED-BY :INITFORM 'NIL)))
(DEFMETHOD NAMED-SECTION-SECTIONS :AROUND ((SECTION NAMED-SECTION))
(SORT (COPY-LIST (CALL-NEXT-METHOD)) #'< :KEY #'SECTION-NUMBER))
(DEFMETHOD SECTION-CODE ((SECTION NAMED-SECTION))
(MAPAPPEND #'SECTION-CODE (NAMED-SECTION-SECTIONS SECTION)))
(DEFMETHOD SECTION-NUMBER ((SECTION NAMED-SECTION))
(LET ((SECTIONS (NAMED-SECTION-SECTIONS SECTION)))
(WHEN (NULL SECTIONS)
(ERROR 'UNDEFINED-NAMED-SECTION-ERROR :FORMAT-CONTROL
"Undefined named section <~A>." :FORMAT-ARGUMENTS
(LIST (SECTION-NAME SECTION))))
(SECTION-NUMBER (FIRST SECTIONS))))
(DEFUN SECTION-NAME-PREFIX-P (NAME)
(LET ((LEN (LENGTH NAME)))
(IF (STRING= NAME "..." :START1 (MAX (- LEN 3) 0) :END1 LEN)
(VALUES T (- LEN 3))
(VALUES NIL LEN))))
(DEFUN SECTION-NAME-LESSP (NAME1 NAME2)
(LET ((LEN1 (NTH-VALUE 1 (SECTION-NAME-PREFIX-P NAME1)))
(LEN2 (NTH-VALUE 1 (SECTION-NAME-PREFIX-P NAME2))))
(STRING-LESSP NAME1 NAME2 :END1 LEN1 :END2 LEN2)))
(DEFUN SECTION-NAME-EQUAL (NAME1 NAME2)
(MULTIPLE-VALUE-BIND (PREFIX-1-P LEN1)
(SECTION-NAME-PREFIX-P NAME1)
(MULTIPLE-VALUE-BIND (PREFIX-2-P LEN2)
(SECTION-NAME-PREFIX-P NAME2)
(LET ((END (MIN LEN1 LEN2)))
(IF (OR PREFIX-1-P PREFIX-2-P)
(STRING-EQUAL NAME1 NAME2 :END1 END :END2 END)
(STRING-EQUAL NAME1 NAME2))))))
(DEFMETHOD FIND-OR-INSERT
(ITEM (ROOT NAMED-SECTION)
&KEY (PREDICATE #'SECTION-NAME-LESSP) (TEST #'SECTION-NAME-EQUAL)
(INSERT-IF-NOT-FOUND T))
(MULTIPLE-VALUE-BIND (NODE PRESENT-P)
(CALL-NEXT-METHOD ITEM ROOT :PREDICATE PREDICATE :TEST TEST
:INSERT-IF-NOT-FOUND INSERT-IF-NOT-FOUND)
(IF PRESENT-P
(OR
(DOLIST (CHILD (LIST (LEFT-CHILD NODE) (RIGHT-CHILD NODE)))
(WHEN CHILD
(MULTIPLE-VALUE-BIND (ALT PRESENT-P)
(CALL-NEXT-METHOD ITEM CHILD :PREDICATE PREDICATE :TEST TEST
:INSERT-IF-NOT-FOUND NIL)
(WHEN PRESENT-P
(RESTART-CASE (ERROR 'AMBIGUOUS-PREFIX-ERROR :PREFIX ITEM
:FIRST-MATCH (NODE-KEY NODE) :ALT-MATCH
(NODE-KEY ALT))
(USE-FIRST-MATCH NIL :REPORT "Use the first match."
(RETURN (VALUES NODE T)))
(USE-ALT-MATCH NIL :REPORT "Use alternate match."
(RETURN (VALUES ALT T))))))))
(VALUES NODE T))
(VALUES NODE NIL))))
(DEFUN SQUEEZE (STRING)
(WITH-INPUT-FROM-STRING (*STANDARD-INPUT* STRING)
(WITH-OUTPUT-TO-STRING (*STANDARD-OUTPUT*)
(HANDLER-CASE
(FLET ((SNARF-WHITESPACE ()
(LOOP FOR CHAR = (READ-CHAR)
WHILE (WHITESPACEP CHAR)
FINALLY (UNREAD-CHAR CHAR))))
(LOOP INITIALLY (SNARF-WHITESPACE)
FOR CHAR = (READ-CHAR)
DO (WRITE-CHAR
(IF (WHITESPACEP CHAR)
(PROGN (SNARF-WHITESPACE) #\ )
CHAR))))
(END-OF-FILE NIL)))))
(DEFUN WHITESPACEP (CHAR) (FIND CHAR *WHITESPACE* :TEST #'CHAR=))
(DEFUN FIND-SECTION (NAME &AUX (NAME (SQUEEZE NAME)))
(IF (NULL *NAMED-SECTIONS*)
(SETQ *NAMED-SECTIONS* (MAKE-INSTANCE 'NAMED-SECTION :NAME NAME))
(MULTIPLE-VALUE-BIND (SECTION PRESENT-P)
(FIND-OR-INSERT NAME *NAMED-SECTIONS*)
(WHEN PRESENT-P
(MULTIPLE-VALUE-BIND (NEW-PREFIX-P NEW-LEN)
(SECTION-NAME-PREFIX-P NAME)
(MULTIPLE-VALUE-BIND (OLD-PREFIX-P OLD-LEN)
(SECTION-NAME-PREFIX-P (SECTION-NAME SECTION))
(WHEN
(OR (AND OLD-PREFIX-P (NOT NEW-PREFIX-P))
(AND OLD-PREFIX-P NEW-PREFIX-P (< NEW-LEN OLD-LEN)))
(SETF (SECTION-NAME SECTION) NAME)))))
SECTION)))
(DEFUN READTABLE-FOR-MODE (MODE)
(DECLARE (TYPE (OR MODE NULL) MODE))
(CDR (ASSOC MODE *READTABLES*)))
(DEFMACRO WITH-MODE (MODE &BODY BODY)
`(LET ((*READTABLE* (READTABLE-FOR-MODE ,MODE)))
,@BODY))
(DEFUN SIMPLE-READER-ERROR (STREAM CONTROL &REST ARGS)
(ERROR 'SIMPLE-READER-ERROR :STREAM STREAM :FORMAT-CONTROL CONTROL
:FORMAT-ARGUMENTS ARGS))
(EVAL-WHEN (:COMPILE-TOPLEVEL :LOAD-TOPLEVEL :EXECUTE)
(DEFINE-SYMBOL-MACRO EOF (LOAD-TIME-VALUE *EOF* T)))
(DEFUN EOF-P (OBJECT) (EQ OBJECT EOF))
(DEFTYPE EOF () '(SATISFIES EOF-P))
(DEFUN TOKEN-DELIMITER-P (CHAR)
(DECLARE (TYPE CHARACTER CHAR))
(OR (WHITESPACEP CHAR)
(MULTIPLE-VALUE-BIND #'NON-TERMINATING-P
(GET-MACRO-CHARACTER CHAR)
(AND FUNCTION (NOT NON-TERMINATING-P)))))
(DEFUN WRAP-READER-MACRO-FUNCTION (FUNCTION)
(LAMBDA (&REST ARGS)
(LET ((VALUES (MULTIPLE-VALUE-LIST (APPLY FUNCTION ARGS))))
(ASSERT (NULL (CDR VALUES)) (VALUES)
"Reader macro function ~S returned more than one value."
FUNCTION)
(IF VALUES
(VALUES-LIST VALUES)
(THROW 'READ-NOTHING (VALUES))))))
(DEFUN READ-MAYBE-NOTHING-INTERNAL
(READ STREAM EOF-ERROR-P EOF-VALUE RECURSIVE-P)
(MULTIPLE-VALUE-LIST
(CATCH 'READ-NOTHING
(FUNCALL READ STREAM EOF-ERROR-P EOF-VALUE RECURSIVE-P))))
(DEFUN READ-MAYBE-NOTHING
(STREAM &OPTIONAL (EOF-ERROR-P T) EOF-VALUE RECURSIVE-P)
(READ-MAYBE-NOTHING-INTERNAL #'READ STREAM EOF-ERROR-P EOF-VALUE RECURSIVE-P))
(DEFUN READ-MAYBE-NOTHING-PRESERVING-WHITESPACE
(STREAM &OPTIONAL (EOF-ERROR-P T) EOF-VALUE RECURSIVE-P)
(READ-MAYBE-NOTHING-INTERNAL #'READ-PRESERVING-WHITESPACE STREAM EOF-ERROR-P
EOF-VALUE RECURSIVE-P))
(DEFCLASS CHARPOS-STREAM NIL
((CHARPOS :INITARG :CHARPOS :READER CHARPOS :INITFORM 0)
(LINENO :INITARG :LINENO :READER LINENO :INITFORM 1)
(PROXY-STREAM :ACCESSOR CHARPOS-PROXY-STREAM :INITARG :PROXY)))
(DEFGENERIC GET-CHARPOS-STREAM-BUFFER
(STREAM))
(DEFMETHOD CHARPOS :BEFORE ((STREAM CHARPOS-STREAM))
(UPDATE-CHARPOS-STREAM-POSITIONS STREAM))
(DEFMETHOD LINENO ((STREAM STREAM)) NIL)
(DEFMETHOD LINENO :BEFORE ((STREAM CHARPOS-STREAM))
(UPDATE-CHARPOS-STREAM-POSITIONS STREAM))
(DEFUN UPDATE-CHARPOS-STREAM-POSITIONS (STREAM)
(WITH-SLOTS (CHARPOS LINENO)
STREAM
(LOOP FOR CHAR ACROSS (GET-CHARPOS-STREAM-BUFFER STREAM)
DO (CASE CHAR
(#\Tab (INCF CHARPOS (- *TAB-WIDTH* (REM CHARPOS *TAB-WIDTH*))))
(#\Newline (INCF LINENO) (SETF CHARPOS 0))
(T (INCF CHARPOS))))))
(DEFCLASS CHARPOS-INPUT-STREAM (CHARPOS-STREAM) NIL)
(DEFMETHOD SHARED-INITIALIZE
((INSTANCE CHARPOS-INPUT-STREAM) SLOT-NAMES
&REST INITARGS
&KEY STREAM
&AUX (ELEMENT-TYPE (STREAM-ELEMENT-TYPE STREAM)))
(APPLY #'CALL-NEXT-METHOD INSTANCE SLOT-NAMES :PROXY
(MAKE-ECHO-STREAM STREAM
(MAKE-STRING-OUTPUT-STREAM :ELEMENT-TYPE
ELEMENT-TYPE))
INITARGS))
(DEFMETHOD GET-CHARPOS-STREAM-BUFFER ((STREAM CHARPOS-INPUT-STREAM))
(GET-OUTPUT-STREAM-STRING
(ECHO-STREAM-OUTPUT-STREAM (CHARPOS-PROXY-STREAM STREAM))))
(DEFCLASS CHARPOS-OUTPUT-STREAM (CHARPOS-STREAM) NIL)
(DEFMETHOD SHARED-INITIALIZE
((INSTANCE CHARPOS-OUTPUT-STREAM) SLOT-NAMES
&REST INITARGS
&KEY STREAM
&AUX (ELEMENT-TYPE (STREAM-ELEMENT-TYPE STREAM)))
(APPLY #'CALL-NEXT-METHOD INSTANCE SLOT-NAMES :PROXY
(MAKE-BROADCAST-STREAM
(MAKE-STRING-OUTPUT-STREAM :ELEMENT-TYPE ELEMENT-TYPE) STREAM)
INITARGS))
(DEFMETHOD GET-CHARPOS-STREAM-BUFFER ((STREAM CHARPOS-OUTPUT-STREAM))
(GET-OUTPUT-STREAM-STRING
(FIRST (BROADCAST-STREAM-STREAMS (CHARPOS-PROXY-STREAM STREAM)))))
(DEFMETHOD INITIALIZE-INSTANCE :AFTER ((INSTANCE CHARPOS-STREAM) &KEY)
(SETF (GETHASH (CHARPOS-PROXY-STREAM INSTANCE) *CHARPOS-STREAMS*) INSTANCE))
(DEFUN STREAM-CHARPOS (STREAM)
(CHARPOS
(OR (GETHASH STREAM *CHARPOS-STREAMS*)
(ERROR "Not tracking charpos for ~S" STREAM))))
(DEFUN STREAM-LINENO (STREAM)
(LINENO
(OR (GETHASH STREAM *CHARPOS-STREAMS*)
(ERROR "Not tracking lineno for ~S" STREAM))))
(DEFUN RELEASE-CHARPOS-STREAM (STREAM)
(MULTIPLE-VALUE-BIND (CHARPOS-STREAM PRESENT-P)
(GETHASH STREAM *CHARPOS-STREAMS*)
(COND
(PRESENT-P (SETF (CHARPOS-PROXY-STREAM CHARPOS-STREAM) NIL)
(REMHASH STREAM *CHARPOS-STREAMS*))
(T (WARN "Not tracking charpos for ~S" STREAM)))))
(DEFUN MAKE-CHARPOS-INPUT-STREAM (INPUT-STREAM &KEY (CHARPOS 0))
(MAKE-INSTANCE 'CHARPOS-INPUT-STREAM :STREAM
(CASE INPUT-STREAM
((T) *TERMINAL-IO*)
((NIL) *STANDARD-INPUT*)
(OTHERWISE INPUT-STREAM))
:CHARPOS CHARPOS))
(DEFUN MAKE-CHARPOS-OUTPUT-STREAM (OUTPUT-STREAM &KEY (CHARPOS 0))
(MAKE-INSTANCE 'CHARPOS-OUTPUT-STREAM :STREAM
(CASE OUTPUT-STREAM
((T) *TERMINAL-IO*)
((NIL) *STANDARD-OUTPUT*)
(OTHERWISE OUTPUT-STREAM))
:CHARPOS CHARPOS))
(DEFMACRO WITH-CHARPOS-INPUT-STREAM ((VAR STREAM &KEY (CHARPOS 0)) &BODY BODY)
`(LET ((,VAR
(CHARPOS-PROXY-STREAM
(MAKE-CHARPOS-INPUT-STREAM ,STREAM :CHARPOS ,CHARPOS))))
(UNWIND-PROTECT (PROGN ,@BODY) (RELEASE-CHARPOS-STREAM ,VAR))))
(DEFMACRO WITH-CHARPOS-OUTPUT-STREAM ((VAR STREAM &KEY (CHARPOS 0)) &BODY BODY)
`(LET ((,VAR
(CHARPOS-PROXY-STREAM
(MAKE-CHARPOS-OUTPUT-STREAM ,STREAM :CHARPOS ,CHARPOS))))
(UNWIND-PROTECT (PROGN ,@BODY) (RELEASE-CHARPOS-STREAM ,VAR))))
(DEFMACRO WITH-REWIND-STREAM
((VAR STREAM &OPTIONAL (REWIND 'REWIND)) &BODY BODY)
(WITH-UNIQUE-NAMES (IN OUT CLOSE)
`(LET* ((,OUT (MAKE-STRING-OUTPUT-STREAM))
(,VAR (MAKE-ECHO-STREAM ,STREAM ,OUT))
(,CLOSE (LIST ,VAR ,OUT)))
(FLET ((,REWIND ()
(LET ((,IN
(MAKE-STRING-INPUT-STREAM
(GET-OUTPUT-STREAM-STRING ,OUT))))
(PROG1 (SETQ ,VAR (MAKE-CONCATENATED-STREAM ,IN ,VAR))
(PUSHNEW ,VAR ,CLOSE)
(PUSHNEW ,IN ,CLOSE)))))
(DECLARE (IGNORABLE (FUNCTION ,REWIND)))
(UNWIND-PROTECT (PROGN ,@BODY) (MAP NIL #'CLOSE ,CLOSE))))))
(DEFMACRO READ-WITH-ECHO ((STREAM OBJECT ECHOED) &BODY BODY)
(WITH-UNIQUE-NAMES (OUT ECHO RAW-OUTPUT LENGTH CHAR)
`(WITH-OPEN-STREAM (,OUT (MAKE-STRING-OUTPUT-STREAM))
(WITH-OPEN-STREAM (,ECHO (MAKE-ECHO-STREAM ,STREAM ,OUT))
(LET* ((,OBJECT (READ-PRESERVING-WHITESPACE ,ECHO))
(,RAW-OUTPUT (GET-OUTPUT-STREAM-STRING ,OUT))
(,LENGTH (LENGTH ,RAW-OUTPUT))
(,ECHOED
(SUBSEQ ,RAW-OUTPUT 0
(IF (LET ((,CHAR (READ-CHAR ,ECHO NIL EOF)))
(OR (EOF-P ,CHAR)
(PROGN
(UNREAD-CHAR ,CHAR ,ECHO)
(PLUSP
(LENGTH
(GET-OUTPUT-STREAM-STRING ,OUT))))))
,LENGTH
(1- ,LENGTH)))))
(DECLARE (IGNORABLE ,OBJECT ,ECHOED))
,@BODY)))))
(DEFCLASS MARKER NIL ((VALUE :READER MARKER-VALUE :INITARG :VALUE)))
(DEFUN MARKERP (OBJECT) (TYPEP OBJECT 'MARKER))
(DEFGENERIC MARKER-BOUNDP
(MARKER))
(DEFMETHOD MARKER-BOUNDP ((MARKER MARKER)) (SLOT-BOUNDP MARKER 'VALUE))
(DEFUN SET-TANGLE-DISPATCH (TYPE-SPECIFIER FUNCTION &OPTIONAL (PRIORITY 0))
(SET-PPRINT-DISPATCH TYPE-SPECIFIER FUNCTION PRIORITY
*TANGLE-PPRINT-DISPATCH*))
(DEFUN SET-WEAVE-DISPATCH (TYPE-SPECIFIER FUNCTION &OPTIONAL (PRIORITY 0))
(SET-PPRINT-DISPATCH TYPE-SPECIFIER FUNCTION PRIORITY
*WEAVE-PPRINT-DISPATCH*))
(SET-TANGLE-DISPATCH 'MARKER
(LAMBDA (STREAM MARKER)
(WHEN (MARKER-BOUNDP MARKER)
(WRITE (MARKER-VALUE MARKER) :STREAM STREAM))))
(DEFCLASS NEWLINE-MARKER (MARKER)
((INDENTATION :ACCESSOR INDENTATION :INITFORM NIL)))
(DEFCLASS PAR-MARKER (NEWLINE-MARKER) NIL)
(DEFUN NEWLINEP (OBJ) (TYPEP OBJ 'NEWLINE-MARKER))
(SET-MACRO-CHARACTER #\Newline
(LAMBDA (STREAM CHAR)
(DECLARE (IGNORE CHAR))
(CASE (PEEK-CHAR NIL STREAM NIL EOF T)
(#\Newline
(READ-CHAR STREAM T NIL T)
(MAKE-INSTANCE 'PAR-MARKER))
(OTHERWISE (MAKE-INSTANCE 'NEWLINE-MARKER))))
NIL (READTABLE-FOR-MODE :LISP))
(DEFCLASS EMPTY-LIST-MARKER (MARKER) NIL (:DEFAULT-INITARGS :VALUE 'NIL))
(DEFVAR *EMPTY-LIST* (MAKE-INSTANCE 'EMPTY-LIST-MARKER))
(DEFCLASS LIST-MARKER (MARKER)
((LENGTH :ACCESSOR LIST-MARKER-LENGTH :INITARG :LENGTH)
(LIST :ACCESSOR LIST-MARKER-LIST :INITARG :LIST)
(CHARPOS :ACCESSOR LIST-MARKER-CHARPOS :INITARG :CHARPOS)))
(DEFUN LIST-MARKER-P (OBJ) (TYPEP OBJ 'LIST-MARKER))
(DEFCLASS CONSING-DOT-MARKER (MARKER) NIL)
(DEFVAR *CONSING-DOT* (MAKE-INSTANCE 'CONSING-DOT-MARKER))
(DEFMETHOD MARKER-BOUNDP ((MARKER LIST-MARKER)) T)
(DEFMETHOD MARKER-VALUE ((MARKER LIST-MARKER))
(DO* ((LIST (LIST NIL))
(TAIL LIST)
(MARKER-LIST (LIST-MARKER-LIST MARKER) (CDR MARKER-LIST))
(X (CAR MARKER-LIST) (CAR MARKER-LIST)))
((ENDP MARKER-LIST) (CDR LIST))
(COND
((EQ X *CONSING-DOT*)
(RPLACD TAIL
(DOLIST (X MARKER-LIST (ERROR "Nothing after . in list"))
(WHEN
(OR (NOT (MARKERP X)) (AND (MARKERP X) (MARKER-BOUNDP X)))
(RETURN X))))
(RETURN (CDR LIST)))
((MARKERP X)
(WHEN (MARKER-BOUNDP X)
(LET ((OBJ (LIST X)))
(RPLACD TAIL OBJ)
(SETQ TAIL OBJ))))
(T
(LET ((OBJ (LIST X)))
(RPLACD TAIL OBJ)
(SETQ TAIL OBJ))))))
(DEFUN MAKE-LIST-READER (NEXT)
(LAMBDA (STREAM CHAR)
(IF (CHAR= (PEEK-CHAR T STREAM T NIL T) #\))
(PROGN (READ-CHAR STREAM T NIL T) *EMPTY-LIST*)
(FUNCALL NEXT STREAM CHAR))))
(SET-MACRO-CHARACTER #\( (MAKE-LIST-READER (GET-MACRO-CHARACTER #\( NIL)) NIL
(READTABLE-FOR-MODE :INNER-LISP))
(DEFUN LIST-READER (STREAM CHAR)
(DECLARE (IGNORE CHAR))
(LOOP WITH LIST = 'NIL
WITH CHARPOS-LIST = 'NIL
FOR N UPFROM 0
AND NEXT-CHAR = (PEEK-CHAR T STREAM T NIL
T) AS CHARPOS = (STREAM-CHARPOS STREAM)
UNTIL (CHAR= #\) NEXT-CHAR)
IF (CHAR= #\. NEXT-CHAR)
DO (WITH-REWIND-STREAM (STREAM STREAM)
(READ-CHAR STREAM T NIL T)
(LET ((FOLLOWING-CHAR (READ-CHAR STREAM T NIL T)))
(COND
((TOKEN-DELIMITER-P FOLLOWING-CHAR)
(UNLESS (OR LIST *READ-SUPPRESS*)
(SIMPLE-READER-ERROR STREAM
"Nothing appears before . in list."))
(UNREAD-CHAR FOLLOWING-CHAR STREAM) (PUSH *CONSING-DOT* LIST)
(PUSH CHARPOS CHARPOS-LIST))
(T (REWIND)
(LET ((VALUES (READ-MAYBE-NOTHING STREAM T NIL T)))
(WHEN VALUES
(PUSH (CAR VALUES) LIST)
(PUSH CHARPOS CHARPOS-LIST))))))) ELSE
DO (LET ((VALUES (READ-MAYBE-NOTHING STREAM T NIL T)))
(WHEN VALUES
(PUSH (CAR VALUES) LIST)
(PUSH CHARPOS CHARPOS-LIST)))
FINALLY (READ-CHAR STREAM T NIL T) (RETURN
(MAKE-INSTANCE 'LIST-MARKER :LENGTH
N :LIST
(NREVERSE LIST)
:CHARPOS
(NREVERSE
CHARPOS-LIST)))))
(SET-MACRO-CHARACTER #\( (MAKE-LIST-READER #'LIST-READER) NIL
(READTABLE-FOR-MODE :LISP))
(DEFCLASS QUOTE-MARKER (MARKER)
((QUOTE :READER QUOTE-MARKER-QUOTE :INITARG :QUOTE)
(FORM :READER QUOTED-FORM :INITARG :FORM)))
(DEFMETHOD MARKER-BOUNDP ((MARKER QUOTE-MARKER)) T)
(DEFMETHOD MARKER-VALUE ((MARKER QUOTE-MARKER))
(LIST (QUOTE-MARKER-QUOTE MARKER) (QUOTED-FORM MARKER)))
(DEFUN SINGLE-QUOTE-READER (STREAM CHAR)
(DECLARE (IGNORE CHAR))
(MAKE-INSTANCE 'QUOTE-MARKER :QUOTE 'QUOTE :FORM (READ STREAM T NIL T)))
(DOLIST (MODE '(:LISP :INNER-LISP))
(SET-MACRO-CHARACTER #\' #'SINGLE-QUOTE-READER NIL (READTABLE-FOR-MODE MODE)))
(DEFCLASS COMMENT-MARKER (MARKER) ((TEXT :READER COMMENT-TEXT :INITARG :TEXT)))
(DEFUN COMMENT-READER (STREAM CHAR)
(IF (CHAR= (PEEK-CHAR NIL STREAM NIL #\Newline T) #\Newline)
(PROGN (READ-CHAR STREAM T NIL T) (VALUES))
(MAKE-INSTANCE 'COMMENT-MARKER :TEXT
(WITH-OUTPUT-TO-STRING (S)
(WRITE-CHAR CHAR S)
(DO ()
((CHAR= (PEEK-CHAR NIL STREAM NIL #\Newline T)
#\Newline))
(WRITE-CHAR (READ-CHAR STREAM T NIL T) S))))))
(SET-MACRO-CHARACTER #\; (WRAP-READER-MACRO-FUNCTION #'COMMENT-READER) NIL
(READTABLE-FOR-MODE :LISP))
(DEFVAR *BACKQUOTE* (MAKE-SYMBOL "BACKQUOTE"))
(DEFUN BACKQUOTEP (OBJECT) (EQ OBJECT *BACKQUOTE*))
(DEFTYPE BACKQUOTE-FORM () '(CONS (SATISFIES BACKQUOTEP)))
(DEFCLASS COMMA NIL ((FORM :INITARG :FORM :INITFORM NIL)))
(DEFCLASS SPLICING-COMMA (COMMA)
((MODIFIER :READER COMMA-MODIFIER :INITARG :MODIFIER :TYPE
CHARACTER)))
(DEFMETHOD COMMA-MODIFIER ((COMMA COMMA)) NIL)
(DEFUN MAKE-COMMA (MODIFIER FORM)
(IF MODIFIER
(MAKE-INSTANCE 'SPLICING-COMMA :MODIFIER MODIFIER :FORM FORM)
(MAKE-INSTANCE 'COMMA :FORM FORM)))
(DEFUN COMMAP (OBJ) (TYPEP OBJ 'COMMA))
(DEFGENERIC COMMA-FORM
(COMMA &KEY TANGLE))
(DEFMETHOD COMMA-FORM ((COMMA COMMA) &KEY (TANGLE T))
(LET ((FORM (SLOT-VALUE COMMA 'FORM)))
(UNLESS TANGLE (RETURN-FROM COMMA-FORM FORM))
(LET ((TANGLED-FORM (TANGLE FORM)))
(TYPECASE FORM
(NAMED-SECTION
(WHEN (REST TANGLED-FORM)
(CERROR "Ignore the extra forms."
"Tried to unquote more than one form from section @<~A@>."
(SECTION-NAME FORM)))
(FIRST TANGLED-FORM))
(T TANGLED-FORM)))))
(DOLIST (MODE '(:LISP :INNER-LISP))
(SET-MACRO-CHARACTER #\`
(LAMBDA (STREAM CHAR)
(DECLARE (IGNORE CHAR))
(LIST *BACKQUOTE* (READ STREAM T NIL T)))
NIL (READTABLE-FOR-MODE MODE))
(SET-MACRO-CHARACTER #\,
(LAMBDA (STREAM CHAR)
(DECLARE (IGNORE CHAR))
(CASE (PEEK-CHAR NIL STREAM T NIL T)
((#\@ #\.)
(MAKE-COMMA (READ-CHAR STREAM T NIL T)
(READ STREAM T NIL T)))
(OTHERWISE (MAKE-COMMA NIL (READ STREAM T NIL T)))))
NIL (READTABLE-FOR-MODE MODE)))
(DEFMACRO BACKQUOTE (X) (BQ-PROCESS X))
(SETF (MACRO-FUNCTION *BACKQUOTE*) (MACRO-FUNCTION 'BACKQUOTE))
(DEFUN BQ-PROCESS (X &AUX (X (TANGLE X)))
(TYPECASE X
(SIMPLE-VECTOR `(APPLY #'VECTOR ,(BQ-PROCESS (COERCE X 'LIST))))
(SPLICING-COMMA (ERROR ",~C~S after `" (COMMA-MODIFIER X) (COMMA-FORM X)))
(COMMA (COMMA-FORM X))
(ATOM `',X)
(BACKQUOTE-FORM (BQ-PROCESS (BQ-PROCESS (CADR X))))
(T
(DO ((P X (CDR P))
(Q 'NIL (CONS (BRACKET (CAR P)) Q)))
((AND (ATOM P) (NOT (COMMAP P)))
(CONS 'APPEND (NRECONC Q (AND P (LIST (LIST 'QUOTE P))))))
(TYPECASE P
(SPLICING-COMMA
(ERROR "Dotted ,~C~S" (COMMA-MODIFIER P) (COMMA-FORM P)))
(COMMA (RETURN (CONS 'APPEND (NRECONC Q (LIST (COMMA-FORM P)))))))))))
(DEFUN BRACKET (X)
(TYPECASE X
(SPLICING-COMMA (COMMA-FORM X))
(COMMA `(LIST ,(COMMA-FORM X)))
(T `(LIST ,(BQ-PROCESS X)))))
(SET-TANGLE-DISPATCH 'BACKQUOTE-FORM
(LAMBDA (STREAM OBJ) (FORMAT STREAM "`~W" (CADR OBJ))))
(SET-TANGLE-DISPATCH 'COMMA
(LAMBDA (STREAM OBJ)
(FORMAT STREAM ",~@[~C~]~W" (COMMA-MODIFIER OBJ)
(COMMA-FORM OBJ))))
(DEFCLASS FUNCTION-MARKER (QUOTE-MARKER) NIL)
(DEFUN SHARPSIGN-QUOTE-READER (STREAM SUB-CHAR ARG)
(DECLARE (IGNORE SUB-CHAR ARG))
(MAKE-INSTANCE 'FUNCTION-MARKER :QUOTE 'FUNCTION :FORM (READ STREAM T NIL T)))
(DOLIST (MODE '(:LISP :INNER-LISP))
(SET-DISPATCH-MACRO-CHARACTER #\# #\' #'SHARPSIGN-QUOTE-READER
(READTABLE-FOR-MODE MODE)))
(DEFCLASS SIMPLE-VECTOR-MARKER (MARKER)
((LENGTH :INITARG :LENGTH)
(ELEMENTS :READER SIMPLE-VECTOR-MARKER-ELEMENTS :INITARG :ELEMENTS)
(ELEMENT-TYPE :READER SIMPLE-VECTOR-MARKER-ELEMENT-TYPE :INITARG
:ELEMENT-TYPE))
(:DEFAULT-INITARGS :ELEMENT-TYPE T))
(DEFMETHOD MARKER-BOUNDP ((MARKER SIMPLE-VECTOR-MARKER)) T)
(DEFMETHOD MARKER-VALUE ((MARKER SIMPLE-VECTOR-MARKER))
(LET ((ELEMENTS (TANGLE (SIMPLE-VECTOR-MARKER-ELEMENTS MARKER)))
(ELEMENT-TYPE (SIMPLE-VECTOR-MARKER-ELEMENT-TYPE MARKER)))
(IF (AND ELEMENTS (SLOT-BOUNDP MARKER 'LENGTH))
(WITH-SLOTS (LENGTH)
MARKER
(LET ((SUPPLIED-LENGTH (LENGTH ELEMENTS)))
(FILL
(REPLACE (MAKE-ARRAY LENGTH :ELEMENT-TYPE ELEMENT-TYPE) ELEMENTS)
(ELT ELEMENTS (1- SUPPLIED-LENGTH)) :START SUPPLIED-LENGTH)))
(COERCE ELEMENTS `(VECTOR ,ELEMENT-TYPE)))))
(DEFUN SIMPLE-VECTOR-READER (STREAM SUB-CHAR ARG)
(DECLARE (IGNORE SUB-CHAR))
(LET* ((LIST (READ-DELIMITED-LIST #\) STREAM T))
(LENGTH
(HANDLER-CASE (LENGTH LIST)
(TYPE-ERROR (ERROR) (DECLARE (IGNORE ERROR))
(SIMPLE-READER-ERROR STREAM "improper list in #(): ~S"
LIST))))
(ELEMENTS (OR LIST *EMPTY-LIST*)))
(UNLESS *READ-SUPPRESS*
(IF ARG
(IF (> LENGTH ARG)
(SIMPLE-READER-ERROR STREAM
"vector longer than specified length: #~S~S"
ARG LIST)
(MAKE-INSTANCE 'SIMPLE-VECTOR-MARKER :LENGTH ARG :ELEMENTS
ELEMENTS))
(MAKE-INSTANCE 'SIMPLE-VECTOR-MARKER :ELEMENTS ELEMENTS)))))
(DOLIST (MODE '(:LISP :INNER-LISP))
(SET-DISPATCH-MACRO-CHARACTER #\# #\( #'SIMPLE-VECTOR-READER
(READTABLE-FOR-MODE MODE)))
(DEFCLASS BIT-VECTOR-MARKER (SIMPLE-VECTOR-MARKER) NIL
(:DEFAULT-INITARGS :ELEMENT-TYPE 'BIT))
(DEFUN SIMPLE-BIT-VECTOR-READER (STREAM SUB-CHAR ARG)
(DECLARE (IGNORE SUB-CHAR))
(APPLY #'MAKE-INSTANCE 'BIT-VECTOR-MARKER :ELEMENTS
(COERCE
(LOOP FOR CHAR = (READ-CHAR STREAM NIL NIL T)
WHILE (AND CHAR (OR (CHAR= CHAR #\0) (CHAR= CHAR #\1)))
COLLECT (ECASE CHAR (#\0 0) (#\1 1))
FINALLY (WHEN CHAR (UNREAD-CHAR CHAR STREAM)))
'BIT-VECTOR)
(WHEN ARG `(:LENGTH ,ARG))))
(DOLIST (MODE '(:LISP :INNER-LISP))
(SET-DISPATCH-MACRO-CHARACTER #\# #\* #'SIMPLE-BIT-VECTOR-READER
(READTABLE-FOR-MODE MODE)))
(DEFCLASS READ-TIME-EVAL NIL
((FORM :READER READ-TIME-EVAL-FORM :INITARG :FORM)))
(SET-TANGLE-DISPATCH 'READ-TIME-EVAL
(LAMBDA (STREAM OBJ)
(FORMAT STREAM "#.~W" (READ-TIME-EVAL-FORM OBJ))))
(DEFCLASS READ-TIME-EVAL-MARKER (READ-TIME-EVAL MARKER) NIL)
(DEFMETHOD MARKER-BOUNDP ((MARKER READ-TIME-EVAL-MARKER)) T)
(DEFMETHOD MARKER-VALUE ((MARKER READ-TIME-EVAL-MARKER))
(IF *EVALUATING*
(CALL-NEXT-METHOD)
(MAKE-INSTANCE 'READ-TIME-EVAL :FORM (READ-TIME-EVAL-FORM MARKER))))
(DEFUN SHARPSIGN-DOT-READER (STREAM SUB-CHAR ARG)
(DECLARE (IGNORE SUB-CHAR ARG))
(LET ((FORM (READ STREAM T NIL T)))
(UNLESS *READ-SUPPRESS*
(UNLESS *READ-EVAL*
(SIMPLE-READER-ERROR STREAM "can't read #. while *READ-EVAL* is NIL"))
(MAKE-INSTANCE 'READ-TIME-EVAL-MARKER :FORM FORM :VALUE
(EVAL (TANGLE FORM))))))
(DOLIST (MODE '(:LISP :INNER-LISP))
(SET-DISPATCH-MACRO-CHARACTER #\# #\. #'SHARPSIGN-DOT-READER
(READTABLE-FOR-MODE MODE)))
(DEFCLASS RADIX-MARKER (MARKER)
((BASE :READER RADIX-MARKER-BASE :INITARG :BASE)))
(DEFPARAMETER *RADIX-PREFIX-ALIST* '((#\B . 2) (#\O . 8) (#\X . 16) (#\R)))
(DEFUN RADIX-READER (STREAM SUB-CHAR ARG)
(MAKE-INSTANCE 'RADIX-MARKER :BASE
(OR (CDR (ASSOC (CHAR-UPCASE SUB-CHAR) *RADIX-PREFIX-ALIST*))
ARG)
:VALUE
(FUNCALL
(GET-DISPATCH-MACRO-CHARACTER #\# SUB-CHAR
(READTABLE-FOR-MODE NIL))
STREAM SUB-CHAR ARG)))
(DOLIST (MODE '(:LISP :INNER-LISP))
(DOLIST (SUB-CHAR '(#\B #\b #\O #\o #\X #\x #\R #\r))
(SET-DISPATCH-MACRO-CHARACTER #\# SUB-CHAR #'RADIX-READER
(READTABLE-FOR-MODE MODE))))
(DEFCLASS COMPLEX-MARKER (MARKER)
((COMPONENTS :READER COMPLEX-COMPONENTS :INITARG :COMPONENTS)))
(DEFMETHOD MARKER-BOUNDP ((MARKER COMPLEX-MARKER)) T)
(DEFMETHOD MARKER-VALUE ((MARKER COMPLEX-MARKER))
(APPLY #'COMPLEX (TANGLE (COMPLEX-COMPONENTS MARKER))))
(DEFUN COMPLEX-READER (STREAM SUB-CHAR ARG)
(DECLARE (IGNORE SUB-CHAR ARG))
(MAKE-INSTANCE 'COMPLEX-MARKER :COMPONENTS (READ STREAM T NIL T)))
(DOLIST (MODE '(:LISP :INNER-LISP))
(SET-DISPATCH-MACRO-CHARACTER #\# #\C #'COMPLEX-READER
(READTABLE-FOR-MODE MODE)))
(DEFCLASS ARRAY-MARKER (MARKER)
((RANK :READER ARRAY-MARKER-RANK :INITARG :RANK)
(INITIAL-CONTENTS :READER ARRAY-MARKER-INITIAL-CONTENTS :INITARG
:INITIAL-CONTENTS)))
(DEFMETHOD MARKER-BOUNDP ((MARKER ARRAY-MARKER)) T)
(DEFMETHOD MARKER-VALUE ((MARKER ARRAY-MARKER))
(LOOP WITH CONTENTS = (TANGLE (ARRAY-MARKER-INITIAL-CONTENTS MARKER))
REPEAT (ARRAY-MARKER-RANK MARKER)
FOR SEQ = CONTENTS THEN (AND SEQ (ELT SEQ 0))
COLLECT (LENGTH SEQ) INTO DIMENSIONS
FINALLY (RETURN (MAKE-ARRAY DIMENSIONS :INITIAL-CONTENTS CONTENTS))))
(DEFUN ARRAY-READER (STREAM SUB-CHAR ARG)
(DECLARE (IGNORE SUB-CHAR))
(UNLESS ARG (SIMPLE-READER-ERROR STREAM "no rank supplied with #A"))
(MAKE-INSTANCE 'ARRAY-MARKER :RANK ARG :INITIAL-CONTENTS
(READ STREAM T NIL T)))
(DOLIST (MODE '(:LISP :INNER-LISP))
(SET-DISPATCH-MACRO-CHARACTER #\# #\A #'ARRAY-READER
(READTABLE-FOR-MODE MODE)))
(DEFCLASS STRUCTURE-MARKER (MARKER)
((FORM :READER STRUCTURE-MARKER-FORM :INITARG :FORM)))
(DEFMETHOD MARKER-BOUNDP ((MARKER STRUCTURE-MARKER)) T)
(DEFMETHOD MARKER-VALUE ((MARKER STRUCTURE-MARKER))
(LET ((*READTABLE* (READTABLE-FOR-MODE NIL)))
(VALUES
(READ-FROM-STRING
(WRITE-TO-STRING MARKER :PPRINT-DISPATCH *TANGLE-PPRINT-DISPATCH* :PRETTY
T :READABLY T)))))
(SET-TANGLE-DISPATCH 'STRUCTURE-MARKER
(LAMBDA (STREAM OBJ)
(FORMAT STREAM "#S~W" (STRUCTURE-MARKER-FORM OBJ)))
1)
(DEFUN STRUCTURE-READER (STREAM SUB-CHAR ARG)
(DECLARE (IGNORE SUB-CHAR ARG))
(MAKE-INSTANCE 'STRUCTURE-MARKER :FORM (READ STREAM T NIL T)))
(DOLIST (MODE '(:LISP :INNER-LISP))
(SET-DISPATCH-MACRO-CHARACTER #\# #\S #'STRUCTURE-READER
(READTABLE-FOR-MODE MODE)))
(DEFCLASS PATHNAME-MARKER (MARKER)
((NAMESTRING :READER PATHNAME-MARKER-NAMESTRING :INITARG
:NAMESTRING)))
(DEFMETHOD MARKER-BOUNDP ((MARKER PATHNAME-MARKER)) T)
(DEFMETHOD MARKER-VALUE ((MARKER PATHNAME-MARKER))
(PARSE-NAMESTRING (PATHNAME-MARKER-NAMESTRING MARKER)))
(SET-TANGLE-DISPATCH 'LOGICAL-PATHNAME
(LAMBDA (STREAM OBJ)
(FORMAT STREAM "#P~W"
(STRING-DOWNCASE
(WITH-STANDARD-IO-SYNTAX (NAMESTRING OBJ)))))
1)
(SET-TANGLE-DISPATCH 'PATHNAME-MARKER
(LAMBDA (STREAM OBJ)
(FORMAT STREAM "#P~W"
(ETYPECASE (MARKER-VALUE OBJ)
(LOGICAL-PATHNAME
(STRING-DOWNCASE
(PATHNAME-MARKER-NAMESTRING OBJ)))
(PATHNAME (PATHNAME-MARKER-NAMESTRING OBJ)))))
1)
(DEFUN PATHNAME-READER (STREAM SUB-CHAR ARG)
(DECLARE (IGNORE SUB-CHAR ARG))
(MAKE-INSTANCE 'PATHNAME-MARKER :NAMESTRING (READ STREAM T NIL T)))
(DOLIST (MODE '(:LISP :INNER-LISP))
(SET-DISPATCH-MACRO-CHARACTER #\# #\P #'PATHNAME-READER
(READTABLE-FOR-MODE MODE)))
(DEFUN FEATUREP (X)
(ETYPECASE X
(SYMBOL (NOT (NULL (MEMBER X *FEATURES* :TEST #'EQ))))
(CONS
(CASE (CAR X)
((:NOT NOT)
(COND
((CDDR X)
(ERROR "Too many subexpressions in feature expression: ~S." X))
((NULL (CDR X))
(ERROR "Too few subexpressions in feature expression: ~S." X))
(T (NOT (FEATUREP (CADR X))))))
((:AND AND) (EVERY #'FEATUREP (CDR X)))
((:OR OR) (SOME #'FEATUREP (CDR X)))
(T (ERROR "Unknown operator in feature expression: ~S." X))))))
(DEFCLASS READ-TIME-CONDITIONAL NIL
((PLUSP :READER READ-TIME-CONDITIONAL-PLUSP :INITARG :PLUSP)
(TEST :READER READ-TIME-CONDITIONAL-TEST :INITARG :TEST)
(FORM :READER READ-TIME-CONDITIONAL-FORM :INITARG :FORM)))
(SET-TANGLE-DISPATCH 'READ-TIME-CONDITIONAL
(LAMBDA (STREAM OBJ)
(FORMAT STREAM "#~:[-~;+~]~S~A"
(READ-TIME-CONDITIONAL-PLUSP OBJ)
(READ-TIME-CONDITIONAL-TEST OBJ)
(READ-TIME-CONDITIONAL-FORM OBJ))))
(DEFCLASS READ-TIME-CONDITIONAL-MARKER (READ-TIME-CONDITIONAL MARKER) NIL)
(DEFMETHOD MARKER-BOUNDP ((MARKER READ-TIME-CONDITIONAL-MARKER))
(IF *EVALUATING*
(CALL-NEXT-METHOD)
T))
(DEFMETHOD MARKER-VALUE ((MARKER READ-TIME-CONDITIONAL-MARKER))
(IF *EVALUATING*
(CALL-NEXT-METHOD)
(MAKE-INSTANCE 'READ-TIME-CONDITIONAL :PLUSP
(READ-TIME-CONDITIONAL-PLUSP MARKER) :TEST
(READ-TIME-CONDITIONAL-TEST MARKER) :FORM
(READ-TIME-CONDITIONAL-FORM MARKER))))
(DEFUN READ-TIME-CONDITIONAL-READER (STREAM SUB-CHAR ARG)
(DECLARE (IGNORE ARG))
(LET* ((PLUSP (ECASE SUB-CHAR (#\+ T) (#\- NIL)))
(*READTABLE* (READTABLE-FOR-MODE NIL))