about summary refs log tree commit diff
path: root/maintainers/scripts/gnu/sxml-match.scm
blob: 48d8c91b4b1f0b12d4644900d020a0a003d487fb (plain) (blame)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
;; Library: sxml-match
;; Author: Jim Bender
;; Version: 1.1, version for PLT Scheme
;;
;; Copyright 2005-9, Jim Bender
;; sxml-match is released under the MIT License
;;

(define-module (sxml-match)
  #:export (sxml-match
            sxml-match-let
            sxml-match-let*)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-11))


;;;
;;; PLT compatibility layer.
;;;

(define-syntax syntax-object->datum
  (syntax-rules ()
    ((_ stx)
     (syntax->datum stx))))

(define-syntax void
  (syntax-rules ()
    ((_) *unspecified*)))

(define-syntax call/ec
  ;; aka. `call-with-escape-continuation'
  (syntax-rules ()
    ((_ proc)
     (let ((prompt (gensym)))
       (call-with-prompt prompt
                         (lambda ()
                           (proc (lambda args
                                   (apply abort-to-prompt
                                          prompt args))))
                         (lambda (k . args)
                           (apply values args)))))))

(define-syntax let/ec
  (syntax-rules ()
    ((_ cont body ...)
     (call/ec (lambda (cont) body ...)))))

(define (raise-syntax-error x msg obj sub)
  (throw 'sxml-match-error x msg obj sub))


;;;
;;; Body, unmodified from
;;; http://planet.plt-scheme.org/package-source/jim/sxml-match.plt/1/1/sxml-match.ss
;;; except for:
;;;
;;;   1. The PLT-specific `module' form.
;;;
;;;   2. In `sxml-match1', ESCAPE is called with `call-with-values' instead
;;;      of being called "normally", such that the example below returns the
;;;      values `x' and `y' instead of just `x':
;;;
;;;      (sxml-match '(foo) ((bar) (values 'p 'q)) ((foo) (values 'x 'y)))
;;;

(define (nodeset? x)
  (or (and (pair? x) (not (symbol? (car x)))) (null? x)))

(define (xml-element-tag s)
  (if (and (pair? s) (symbol? (car s)))
      (car s)
      (error 'xml-element-tag "expected an xml-element, given" s)))

(define (xml-element-attributes s)
  (if (and (pair? s) (symbol? (car s)))
      (fold-right (lambda (a b)
                    (if (and (pair? a) (eq? '@ (car a)))
                        (if (null? b)
                            (filter (lambda (i) (not (and (pair? i) (eq? '@ (car i))))) (cdr a))
                            (fold-right (lambda (c d)
                                          (if (and (pair? c) (eq? '@ (car c)))
                                              d
                                              (cons c d)))
                                        b (cdr a)))
                        b))
                  '()
                  (cdr s))
      (error 'xml-element-attributes "expected an xml-element, given" s)))

(define (xml-element-contents s)
  (if (and (pair? s) (symbol? (car s)))
      (filter (lambda (i)
                (not (and (pair? i) (eq? '@ (car i)))))
              (cdr s))
      (error 'xml-element-contents "expected an xml-element, given" s)))

(define (match-xml-attribute key l)
  (if (not (pair? l))
      #f
      (if (eq? (car (car l)) key)
          (car l)
          (match-xml-attribute key (cdr l)))))

(define (filter-attributes keys lst)
  (if (null? lst)
      '()
      (if (member (caar lst) keys)
          (filter-attributes keys (cdr lst))
          (cons (car lst) (filter-attributes keys (cdr lst))))))

(define-syntax compile-clause
  (lambda (stx)
    (letrec
        ([sxml-match-syntax-error
          (lambda (msg exp sub)
            (raise-syntax-error #f msg (with-syntax ([s exp]) (syntax (sxml-match s))) sub))]
         [ellipsis?
          (lambda (stx)
            (and (identifier? stx) (eq? '... (syntax->datum stx))))]
         [literal?
          (lambda (stx)
            (let ([x (syntax->datum stx)])
              (or (string? x)
                  (char? x)
                  (number? x)
                  (boolean? x))))]
         [keyword?
          (lambda (stx)
            (and (identifier? stx)
                 (let ([str (symbol->string (syntax->datum stx))])
                   (char=? #\: (string-ref str (- (string-length str) 1))))))]
         [extract-cata-fun
          (lambda (cf)
            (syntax-case cf ()
              [#f #f]
              [other cf]))]
         [add-pat-var
          (lambda (pvar pvar-lst)
            (define (check-pvar lst)
              (if (null? lst)
                  (void)
                  (if (bound-identifier=? (car lst) pvar)
                      (sxml-match-syntax-error "duplicate pattern variable not allowed"
                                               stx
                                               pvar)
                      (check-pvar (cdr lst)))))
            (check-pvar pvar-lst)
            (cons pvar pvar-lst))]
         [add-cata-def
          (lambda (depth cvars cfun ctemp cdefs)
            (cons (list depth cvars cfun ctemp) cdefs))]
         [process-cata-exp
          (lambda (depth cfun ctemp)
            (if (= depth 0)
                (with-syntax ([cf cfun]
                              [ct ctemp])
                  (syntax (cf ct)))
                (let ([new-ctemp (car (generate-temporaries (list ctemp)))])
                  (with-syntax ([ct ctemp]
                                [nct new-ctemp]
                                [body (process-cata-exp (- depth 1) cfun new-ctemp)])
                    (syntax (map (lambda (nct) body) ct))))))]
         [process-cata-defs
          (lambda (cata-defs body)
            (if (null? cata-defs)
                body
                (with-syntax ([(cata-binding ...)
                               (map (lambda (def)
                                      (with-syntax ([bvar (cadr def)]
                                                    [bval (process-cata-exp (car def)
                                                                            (caddr def)
                                                                            (cadddr def))])
                                        (syntax (bvar bval))))
                                    cata-defs)]
                              [body-stx body])
                  (syntax (let-values (cata-binding ...)
                            body-stx)))))]
         [cata-defs->pvar-lst
          (lambda (lst)
            (if (null? lst)
                '()
                (let iter ([items (cadr (car lst))])
                  (syntax-case items ()
                    [() (cata-defs->pvar-lst (cdr lst))]
                    [(fst . rst) (cons (syntax fst) (iter (syntax rst)))]))))]
         [process-output-action
          (lambda (action dotted-vars)
            (define (finite-lst? lst)
              (syntax-case lst ()
                (item
                 (identifier? (syntax item))
                 #f)
                (()
                 #t)
                ((fst dots . rst)
                 (ellipsis? (syntax dots))
                 #f)
                ((fst . rst)
                 (finite-lst? (syntax rst)))))
            (define (expand-lst lst)
              (syntax-case lst ()
                [() (syntax '())]
                [item
                 (identifier? (syntax item))
                 (syntax item)]
                [(fst dots . rst)
                 (ellipsis? (syntax dots))
                 (with-syntax ([exp-lft (expand-dotted-item
                                         (process-output-action (syntax fst)
                                                                dotted-vars))]
                               [exp-rgt (expand-lst (syntax rst))])
                   (syntax (append exp-lft exp-rgt)))]
                [(fst . rst)
                 (with-syntax ([exp-lft (process-output-action (syntax fst)
                                                               dotted-vars)]
                               [exp-rgt (expand-lst (syntax rst))])
                   (syntax (cons exp-lft exp-rgt)))]))
            (define (member-var? var lst)
              (let iter ([lst lst])
                (if (null? lst)
                    #f
                    (if (or (bound-identifier=? var (car lst))
                            (free-identifier=? var (car lst)))
                        #t
                        (iter (cdr lst))))))
            (define (dotted-var? var)
              (member-var? var dotted-vars))
            (define (merge-pvars lst1 lst2)
              (if (null? lst1)
                  lst2
                  (if (member-var? (car lst1) lst2)
                      (merge-pvars (cdr lst1) lst2)
                      (cons (car lst1) (merge-pvars (cdr lst1) lst2)))))
            (define (select-dotted-vars x)
              (define (walk-quasi-body y)
                (syntax-case y (unquote unquote-splicing)
                  [((unquote a) . rst)
                   (merge-pvars (select-dotted-vars (syntax a))
                                (walk-quasi-body (syntax rst)))]
                  [((unquote-splicing a) . rst)
                   (merge-pvars (select-dotted-vars (syntax a))
                                (walk-quasi-body (syntax rst)))]
                  [(fst . rst)
                   (merge-pvars (walk-quasi-body (syntax fst))
                                (walk-quasi-body (syntax rst)))]
                  [other
                   '()]))
              (syntax-case x (quote quasiquote)
                [(quote . rst) '()]
                [(quasiquote . rst) (walk-quasi-body (syntax rst))]
                [(fst . rst)
                 (merge-pvars (select-dotted-vars (syntax fst))
                              (select-dotted-vars (syntax rst)))]
                [item
                 (and (identifier? (syntax item))
                      (dotted-var? (syntax item)))
                 (list (syntax item))]
                [item '()]))
            (define (expand-dotted-item item)
              (let ([dvars (select-dotted-vars item)])
                (syntax-case item ()
                  [x
                   (identifier? (syntax x))
                   (syntax x)]
                  [x (with-syntax ([(dv ...) dvars])
                       (syntax (map (lambda (dv ...) x) dv ...)))])))
            (define (expand-quasiquote-body x)
              (syntax-case x (unquote unquote-splicing quasiquote)
                [(quasiquote . rst) (process-quasiquote x)]
                [(unquote item)
                 (with-syntax ([expanded-item (process-output-action (syntax item)
                                                                     dotted-vars)])
                   (syntax (unquote expanded-item)))]
                [(unquote-splicing item)
                 (with-syntax ([expanded-item (process-output-action (syntax item)
                                                                     dotted-vars)])
                   (syntax (unquote-splicing expanded-item)))]
                [((unquote item) dots . rst)
                 (ellipsis? (syntax dots))
                 (with-syntax ([expanded-item (expand-dotted-item
                                               (process-output-action (syntax item)
                                                                      dotted-vars))]
                               [expanded-rst (expand-quasiquote-body (syntax rst))])
                   (syntax ((unquote-splicing expanded-item) . expanded-rst)))]
                [(item dots . rst)
                 (ellipsis? (syntax dots))
                 (with-syntax ([expanded-item (expand-dotted-item
                                               (process-output-action (syntax (quasiquote item))
                                                                      dotted-vars))]
                               [expanded-rst (expand-quasiquote-body (syntax rst))])
                   (syntax ((unquote-splicing expanded-item) . expanded-rst)))]
                [(fst . rst)
                 (with-syntax ([expanded-fst (expand-quasiquote-body (syntax fst))]
                               [expanded-rst (expand-quasiquote-body (syntax rst))])
                   (syntax (expanded-fst . expanded-rst)))]
                [other x]))
            (define (process-quasiquote x)
              (syntax-case x ()
                [(quasiquote term) (with-syntax ([expanded-body (expand-quasiquote-body (syntax term))])
                                     (syntax (quasiquote expanded-body)))]
                [else (sxml-match-syntax-error "bad quasiquote-form"
                                               stx
                                               x)]))
            (syntax-case action (quote quasiquote)
              [(quote . rst) action]
              [(quasiquote . rst) (process-quasiquote action)]
              [(fst . rst) (if (finite-lst? action)
                               (with-syntax ([exp-lft (process-output-action (syntax fst) dotted-vars)]
                                             [exp-rgt (process-output-action (syntax rst) dotted-vars)])
                                 (syntax (exp-lft . exp-rgt)))
                               (with-syntax ([exp-lft (process-output-action (syntax fst)
                                                                             dotted-vars)]
                                             [exp-rgt (expand-lst (syntax rst))])
                                 (syntax (apply exp-lft exp-rgt))))]
              [item action]))]
         [compile-element-pat
          (lambda (ele exp nextp fail-k pvar-lst depth cata-fun cata-defs dotted-vars)
            (syntax-case ele (@)
              [(tag (@ . attr-items) . items)
               (identifier? (syntax tag))
               (let ([attr-exp (car (generate-temporaries (list exp)))]
                     [body-exp (car (generate-temporaries (list exp)))])
                 (let-values ([(tests new-pvar-lst new-cata-defs new-dotted-vars)
                               (compile-attr-list (syntax attr-items)
                                                  (syntax items)
                                                  attr-exp
                                                  body-exp
                                                  '()
                                                  nextp
                                                  fail-k
                                                  pvar-lst
                                                  depth
                                                  cata-fun
                                                  cata-defs
                                                  dotted-vars)])
                   (values (with-syntax ([x exp]
                                         [ax attr-exp]
                                         [bx body-exp]
                                         [body tests]
                                         [fail-to fail-k])
                             (syntax (if (and (pair? x) (eq? 'tag (xml-element-tag x)))
                                         (let ([ax (xml-element-attributes x)]
                                               [bx (xml-element-contents x)])
                                           body)
                                         (fail-to))))
                           new-pvar-lst
                           new-cata-defs
                           new-dotted-vars)))]
              [(tag . items)
               (identifier? (syntax tag))
               (let ([body-exp (car (generate-temporaries (list exp)))])
                 (let-values ([(tests new-pvar-lst new-cata-defs new-dotted-vars)
                               (compile-item-list (syntax items)
                                                  body-exp
                                                  nextp
                                                  fail-k
                                                  #t
                                                  pvar-lst
                                                  depth
                                                  cata-fun
                                                  cata-defs
                                                  dotted-vars)])
                   (values (with-syntax ([x exp]
                                         [bx body-exp]
                                         [body tests]
                                         [fail-to fail-k])
                             (syntax (if (and (pair? x) (eq? 'tag (xml-element-tag x)))
                                         (let ([bx (xml-element-contents x)])
                                           body)
                                         (fail-to))))
                           new-pvar-lst
                           new-cata-defs
                           new-dotted-vars)))]))]
         [compile-end-element
          (lambda (exp nextp fail-k pvar-lst cata-defs dotted-vars)
            (let-values ([(next-tests new-pvar-lst new-cata-defs new-dotted-vars)
                          (nextp pvar-lst cata-defs dotted-vars)])
              (values (with-syntax ([x exp]
                                    [body next-tests]
                                    [fail-to fail-k])
                        (syntax (if (null? x) body (fail-to))))
                      new-pvar-lst
                      new-cata-defs
                      new-dotted-vars)))]
         [compile-attr-list
          (lambda (attr-lst body-lst attr-exp body-exp attr-key-lst nextp fail-k pvar-lst depth cata-fun cata-defs dotted-vars)
            (syntax-case attr-lst (unquote ->)
              [(unquote var)
               (identifier? (syntax var))
               (let-values ([(tests new-pvar-lst new-cata-defs new-dotted-vars)
                             (compile-item-list body-lst
                                                body-exp
                                                nextp
                                                fail-k
                                                #t
                                                (add-pat-var (syntax var) pvar-lst)
                                                depth
                                                cata-fun
                                                cata-defs
                                                dotted-vars)])
                 (values (with-syntax ([ax attr-exp]
                                       [matched-attrs attr-key-lst]
                                       [body tests])
                           (syntax (let ([var (filter-attributes 'matched-attrs ax)])
                                     body)))
                         new-pvar-lst
                         new-cata-defs
                         new-dotted-vars))]
              [((atag [(unquote [cata -> cvar ...]) default]) . rst)
               (identifier? (syntax atag))
               (let ([ctemp (car (generate-temporaries (syntax ([cvar ...]))))])
                 (let-values ([(tests new-pvar-lst new-cata-defs new-dotted-vars)
                               (compile-attr-list (syntax rst)
                                                  body-lst
                                                  attr-exp
                                                  body-exp
                                                  (cons (syntax atag) attr-key-lst)
                                                  nextp
                                                  fail-k
                                                  (add-pat-var ctemp pvar-lst)
                                                  depth
                                                  cata-fun
                                                  (add-cata-def depth
                                                                (syntax [cvar ...])
                                                                (syntax cata)
                                                                ctemp
                                                                cata-defs)
                                                  dotted-vars)])
                   (values (with-syntax ([ax attr-exp]
                                         [ct ctemp]
                                         [body tests])
                             (syntax (let ([binding (match-xml-attribute 'atag ax)])
                                       (let ([ct (if binding
                                                     (cadr binding)
                                                     default)])
                                         body))))
                           new-pvar-lst
                           new-cata-defs
                           new-dotted-vars)))]
              [((atag [(unquote [cvar ...]) default]) . rst)
               (identifier? (syntax atag))
               (let ([ctemp (car (generate-temporaries (syntax ([cvar ...]))))])
                 (if (not cata-fun)
                     (sxml-match-syntax-error "sxml-match pattern: catamorphism not allowed in this context"
                                              stx
                                              (syntax [cvar ...])))
                 (let-values ([(tests new-pvar-lst new-cata-defs new-dotted-vars)
                               (compile-attr-list (syntax rst)
                                                  body-lst
                                                  attr-exp
                                                  body-exp
                                                  (cons (syntax atag) attr-key-lst)
                                                  nextp
                                                  fail-k
                                                  (add-pat-var ctemp pvar-lst)
                                                  depth
                                                  cata-fun
                                                  (add-cata-def depth
                                                                (syntax [cvar ...])
                                                                cata-fun
                                                                ctemp
                                                                cata-defs)
                                                  dotted-vars)])
                   (values (with-syntax ([ax attr-exp]
                                         [ct ctemp]
                                         [body tests])
                             (syntax (let ([binding (match-xml-attribute 'atag ax)])
                                       (let ([ct (if binding
                                                     (cadr binding)
                                                     default)])
                                         body))))
                           new-pvar-lst
                           new-cata-defs
                           new-dotted-vars)))]
              [((atag [(unquote var) default]) . rst)
               (and (identifier? (syntax atag)) (identifier? (syntax var)))
               (let-values ([(tests new-pvar-lst new-cata-defs new-dotted-vars)
                             (compile-attr-list (syntax rst)
                                                body-lst
                                                attr-exp
                                                body-exp
                                                (cons (syntax atag) attr-key-lst)
                                                nextp
                                                fail-k
                                                (add-pat-var (syntax var) pvar-lst)
                                                depth
                                                cata-fun
                                                cata-defs
                                                dotted-vars)])
                 (values (with-syntax ([ax attr-exp]
                                       [body tests])
                           (syntax (let ([binding (match-xml-attribute 'atag ax)])
                                     (let ([var (if binding
                                                    (cadr binding)
                                                    default)])
                                       body))))
                         new-pvar-lst
                         new-cata-defs
                         new-dotted-vars))]
              [((atag (unquote [cata -> cvar ...])) . rst)
               (identifier? (syntax atag))
               (let ([ctemp (car (generate-temporaries (syntax ([cvar ...]))))])
                 (let-values ([(tests new-pvar-lst new-cata-defs new-dotted-vars)
                               (compile-attr-list (syntax rst)
                                                  body-lst
                                                  attr-exp
                                                  body-exp
                                                  (cons (syntax atag) attr-key-lst)
                                                  nextp
                                                  fail-k
                                                  (add-pat-var ctemp pvar-lst)
                                                  depth
                                                  cata-fun
                                                  (add-cata-def depth
                                                                (syntax [cvar ...])
                                                                (syntax cata)
                                                                ctemp
                                                                cata-defs)
                                                  dotted-vars)])
                   (values (with-syntax ([ax attr-exp]
                                         [ct ctemp]
                                         [body tests]
                                         [fail-to fail-k])
                             (syntax (let ([binding (match-xml-attribute 'atag ax)])
                                       (if binding
                                           (let ([ct (cadr binding)])
                                             body)
                                           (fail-to)))))
                           new-pvar-lst
                           new-cata-defs
                           new-dotted-vars)))]
              [((atag (unquote [cvar ...])) . rst)
               (identifier? (syntax atag))
               (let ([ctemp (car (generate-temporaries (syntax ([cvar ...]))))])
                 (if (not cata-fun)
                     (sxml-match-syntax-error "sxml-match pattern: catamorphism not allowed in this context"
                                              stx
                                              (syntax [cvar ...])))
                 (let-values ([(tests new-pvar-lst new-cata-defs new-dotted-vars)
                               (compile-attr-list (syntax rst)
                                                  body-lst
                                                  attr-exp
                                                  body-exp
                                                  (cons (syntax atag) attr-key-lst)
                                                  nextp
                                                  fail-k
                                                  (add-pat-var ctemp pvar-lst)
                                                  depth
                                                  cata-fun
                                                  (add-cata-def depth
                                                                (syntax [cvar ...])
                                                                cata-fun
                                                                ctemp
                                                                cata-defs)
                                                  dotted-vars)])
                   (values (with-syntax ([ax attr-exp]
                                         [ct ctemp]
                                         [body tests]
                                         [fail-to fail-k])
                             (syntax (let ([binding (match-xml-attribute 'atag ax)])
                                       (if binding
                                           (let ([ct (cadr binding)])
                                             body)
                                           (fail-to)))))
                           new-pvar-lst
                           new-cata-defs
                           new-dotted-vars)))]
              [((atag (unquote var)) . rst)
               (and (identifier? (syntax atag)) (identifier? (syntax var)))
               (let-values ([(tests new-pvar-lst new-cata-defs new-dotted-vars)
                             (compile-attr-list (syntax rst)
                                                body-lst
                                                attr-exp
                                                body-exp
                                                (cons (syntax atag) attr-key-lst)
                                                nextp
                                                fail-k
                                                (add-pat-var (syntax var) pvar-lst)
                                                depth
                                                cata-fun
                                                cata-defs
                                                dotted-vars)])
                 (values (with-syntax ([ax attr-exp]
                                       [body tests]
                                       [fail-to fail-k])
                           (syntax (let ([binding (match-xml-attribute 'atag ax)])
                                     (if binding
                                         (let ([var (cadr binding)])
                                           body)
                                         (fail-to)))))
                         new-pvar-lst
                         new-cata-defs
                         new-dotted-vars))]
              [((atag (i ...)) . rst)
               (identifier? (syntax atag))
               (sxml-match-syntax-error "bad attribute pattern"
                                        stx
                                        (syntax (kwd (i ...))))]
              [((atag i) . rst)
               (and (identifier? (syntax atag)) (identifier? (syntax i)))
               (sxml-match-syntax-error "bad attribute pattern"
                                        stx
                                        (syntax (kwd i)))]
              [((atag literal) . rst)
               (and (identifier? (syntax atag)) (literal? (syntax literal)))
               (let-values ([(tests new-pvar-lst new-cata-defs new-dotted-vars)
                             (compile-attr-list (syntax rst)
                                                body-lst
                                                attr-exp
                                                body-exp
                                                (cons (syntax atag) attr-key-lst)
                                                nextp
                                                fail-k
                                                pvar-lst
                                                depth
                                                cata-fun
                                                cata-defs
                                                dotted-vars)])
                 (values (with-syntax ([ax attr-exp]
                                       [body tests]
                                       [fail-to fail-k])
                           (syntax (let ([binding (match-xml-attribute 'atag ax)])
                                     (if binding
                                         (if (equal? (cadr binding) literal)
                                             body
                                             (fail-to))
                                         (fail-to)))))
                         new-pvar-lst
                         new-cata-defs
                         new-dotted-vars))]
              [()
               (compile-item-list body-lst
                                  body-exp
                                  nextp
                                  fail-k
                                  #t
                                  pvar-lst
                                  depth
                                  cata-fun
                                  cata-defs
                                  dotted-vars)]))]
         [compile-item-list
          (lambda (lst exp nextp fail-k ellipsis-allowed? pvar-lst depth cata-fun cata-defs dotted-vars)
            (syntax-case lst (unquote ->)
              [() (compile-end-element exp nextp fail-k pvar-lst cata-defs dotted-vars)]
              [(unquote var)
               (identifier? (syntax var))
               (if (not ellipsis-allowed?)
                   (sxml-match-syntax-error "improper list pattern not allowed in this context"
                                            stx
                                            (syntax dots))
                   (let-values ([(next-tests new-pvar-lst new-cata-defs new-dotted-vars)
                                 (nextp (add-pat-var (syntax var) pvar-lst) cata-defs dotted-vars)])
                     (values (with-syntax ([x exp]
                                           [body next-tests])
                               (syntax (let ([var x]) body)))
                             new-pvar-lst
                             new-cata-defs
                             new-dotted-vars)))]
              [(unquote [cata -> cvar ...])
               (if (not ellipsis-allowed?)
                   (sxml-match-syntax-error "improper list pattern not allowed in this context"
                                            stx
                                            (syntax dots))
                   (let ([ctemp (car (generate-temporaries (syntax ([cvar ...]))))])
                     (let-values ([(next-tests new-pvar-lst new-cata-defs new-dotted-vars)
                                   (nextp (add-pat-var ctemp pvar-lst)
                                          (add-cata-def depth
                                                        (syntax [cvar ...])
                                                        (syntax cata)
                                                        ctemp
                                                        cata-defs)
                                          dotted-vars)])
                       (values (with-syntax ([ct ctemp]
                                             [x exp]
                                             [body next-tests])
                                 (syntax (let ([ct x]) body)))
                               new-pvar-lst
                               new-cata-defs
                               new-dotted-vars))))]
              [(unquote [cvar ...])
               (let ([ctemp (car (generate-temporaries (syntax ([cvar ...]))))])
                 (if (not cata-fun)
                     (sxml-match-syntax-error "sxml-match pattern: catamorphism not allowed in this context"
                                              stx
                                              (syntax [cvar ...])))
                 (let-values ([(next-tests new-pvar-lst new-cata-defs new-dotted-vars)
                               (nextp (add-pat-var ctemp pvar-lst)
                                      (add-cata-def depth
                                                    (syntax [cvar ...])
                                                    cata-fun
                                                    ctemp
                                                    cata-defs)
                                      dotted-vars)])
                   (values (with-syntax ([ct ctemp]
                                         [x exp]
                                         [body next-tests])
                             (syntax (let ([ct x]) body)))
                           new-pvar-lst
                           new-cata-defs
                           new-dotted-vars)))]
              [(item dots . rst)
               (ellipsis? (syntax dots))
               (if (not ellipsis-allowed?)
                   (sxml-match-syntax-error "ellipses not allowed in this context"
                                            stx
                                            (syntax dots))
                   (compile-dotted-pattern-list (syntax item)
                                                (syntax rst)
                                                exp
                                                nextp
                                                fail-k
                                                pvar-lst
                                                depth
                                                cata-fun
                                                cata-defs
                                                dotted-vars))]
              [(item . rst)
               (compile-item (syntax item)
                             exp
                             (lambda (new-exp new-pvar-lst new-cata-defs new-dotted-vars)
                               (compile-item-list (syntax rst)
                                                  new-exp
                                                  nextp
                                                  fail-k
                                                  ellipsis-allowed?
                                                  new-pvar-lst
                                                  depth
                                                  cata-fun
                                                  new-cata-defs
                                                  new-dotted-vars))
                             fail-k
                             pvar-lst
                             depth
                             cata-fun
                             cata-defs
                             dotted-vars)]))]
         [compile-dotted-pattern-list
          (lambda (item
                   tail
                   exp
                   nextp
                   fail-k
                   pvar-lst
                   depth
                   cata-fun
                   cata-defs
                   dotted-vars)
            (let-values ([(tail-tests tail-pvar-lst tail-cata-defs tail-dotted-vars)
                          (compile-item-list tail
                                             (syntax lst)
                                             (lambda (new-pvar-lst new-cata-defs new-dotted-vars)
                                               (values (with-syntax ([(npv ...) new-pvar-lst])
                                                         (syntax (values #t npv ...)))
                                                       new-pvar-lst
                                                       new-cata-defs
                                                       new-dotted-vars))
                                             (syntax fail)
                                             #f
                                             '()
                                             depth
                                             '()
                                             '()
                                             dotted-vars)]
                         [(item-tests item-pvar-lst item-cata-defs item-dotted-vars)
                          (compile-item item
                                        (syntax lst)
                                        (lambda (new-exp new-pvar-lst new-cata-defs new-dotted-vars)
                                          (values (with-syntax ([(npv ...) new-pvar-lst])
                                                    (syntax (values #t (cdr lst) npv ...)))
                                                  new-pvar-lst
                                                  new-cata-defs
                                                  new-dotted-vars))
                                        (syntax fail)
                                        '()
                                        (+ 1 depth)
                                        cata-fun
                                        '()
                                        dotted-vars)])
              ; more here: check for duplicate pat-vars, cata-defs
              (let-values ([(final-tests final-pvar-lst final-cata-defs final-dotted-vars)
                            (nextp (append tail-pvar-lst item-pvar-lst pvar-lst)
                                   (append tail-cata-defs item-cata-defs cata-defs)
                                   (append item-pvar-lst
                                           (cata-defs->pvar-lst item-cata-defs)
                                           tail-dotted-vars
                                           dotted-vars))])
                (let ([temp-item-pvar-lst (generate-temporaries item-pvar-lst)])
                  (values
                   (with-syntax
                       ([x exp]
                        [fail-to fail-k]
                        [tail-body tail-tests]
                        [item-body item-tests]
                        [final-body final-tests]
                        [(ipv ...) item-pvar-lst]
                        [(gpv ...) temp-item-pvar-lst]
                        [(tpv ...) tail-pvar-lst]
                        [(item-void ...) (map (lambda (i) (syntax (void))) item-pvar-lst)]
                        [(tail-void ...) (map (lambda (i) (syntax (void))) tail-pvar-lst)]
                        [(item-null ...) (map (lambda (i) (syntax '())) item-pvar-lst)]
                        [(item-cons ...) (map (lambda (a b)
                                                (with-syntax ([xa a]
                                                              [xb b])
                                                  (syntax (cons xa xb))))
                                              item-pvar-lst
                                              temp-item-pvar-lst)])
                     (syntax (letrec ([match-tail
                                       (lambda (lst fail)
                                         tail-body)]
                                      [match-item
                                       (lambda (lst)
                                         (let ([fail (lambda ()
                                                       (values #f
                                                               lst
                                                               item-void ...))])
                                           item-body))]
                                      [match-dotted
                                       (lambda (x)
                                         (let-values ([(tail-res tpv ...)
                                                       (match-tail x
                                                                   (lambda ()
                                                                     (values #f
                                                                             tail-void ...)))])
                                           (if tail-res
                                               (values item-null ...
                                                       tpv ...)
                                               (let-values ([(res new-x ipv ...) (match-item x)])
                                                 (if res
                                                     (let-values ([(gpv ... tpv ...)
                                                                   (match-dotted new-x)])
                                                       (values item-cons ... tpv ...))
                                                     (let-values ([(last-tail-res tpv ...)
                                                                   (match-tail x fail-to)])
                                                       (values item-null ... tpv ...)))))))])
                               (let-values ([(ipv ... tpv ...)
                                             (match-dotted x)])
                                 final-body))))
                   final-pvar-lst
                   final-cata-defs
                   final-dotted-vars)))))]
         [compile-item
          (lambda (item exp nextp fail-k pvar-lst depth cata-fun cata-defs dotted-vars)
            (syntax-case item (unquote ->)
              ; normal pattern var
              [(unquote var)
               (identifier? (syntax var))
               (let ([new-exp (car (generate-temporaries (list exp)))])
                 (let-values ([(next-tests new-pvar-lst new-cata-defs new-dotted-vars)
                               (nextp new-exp (add-pat-var (syntax var) pvar-lst) cata-defs dotted-vars)])
                   (values (with-syntax ([x exp]
                                         [nx new-exp]
                                         [body next-tests]
                                         [fail-to fail-k])
                             (syntax (if (pair? x)
                                         (let ([nx (cdr x)]
                                               [var (car x)])
                                           body)
                                         (fail-to))))
                           new-pvar-lst
                           new-cata-defs
                           new-dotted-vars)))]
              ; named catamorphism
              [(unquote [cata -> cvar ...])
               (let ([new-exp (car (generate-temporaries (list exp)))]
                     [ctemp (car (generate-temporaries (syntax ([cvar ...]))))])
                 (let-values ([(next-tests new-pvar-lst new-cata-defs new-dotted-vars)
                               (nextp new-exp
                                      (add-pat-var ctemp pvar-lst)
                                      (add-cata-def depth
                                                    (syntax [cvar ...])
                                                    (syntax cata)
                                                    ctemp
                                                    cata-defs)
                                      dotted-vars)])
                   (values (with-syntax ([x exp]
                                         [nx new-exp]
                                         [ct ctemp]
                                         [body next-tests]
                                         [fail-to fail-k])
                             (syntax (if (pair? x)
                                         (let ([nx (cdr x)]
                                               [ct (car x)])
                                           body)
                                         (fail-to))))
                           new-pvar-lst
                           new-cata-defs
                           new-dotted-vars)))]
              ; basic catamorphism
              [(unquote [cvar ...])
               (let ([new-exp (car (generate-temporaries (list exp)))]
                     [ctemp (car (generate-temporaries (syntax ([cvar ...]))))])
                 (if (not cata-fun)
                     (sxml-match-syntax-error "sxml-match pattern: catamorphism not allowed in this context"
                                              stx
                                              (syntax [cvar ...])))
                 (let-values ([(next-tests new-pvar-lst new-cata-defs new-dotted-vars)
                               (nextp new-exp
                                      (add-pat-var ctemp pvar-lst)
                                      (add-cata-def depth
                                                    (syntax [cvar ...])
                                                    cata-fun
                                                    ctemp
                                                    cata-defs)
                                      dotted-vars)])
                   (values (with-syntax ([x exp]
                                         [nx new-exp]
                                         [ct ctemp]
                                         [body next-tests]
                                         [fail-to fail-k])
                             (syntax (if (pair? x)
                                         (let ([nx (cdr x)]
                                               [ct (car x)])
                                           body)
                                         (fail-to))))
                           new-pvar-lst
                           new-cata-defs
                           new-dotted-vars)))]
              [(tag item ...)
               (identifier? (syntax tag))
               (let ([new-exp (car (generate-temporaries (list exp)))])
                 (let-values ([(after-tests after-pvar-lst after-cata-defs after-dotted-vars)
                               (compile-element-pat (syntax (tag item ...))
                                                    (with-syntax ([x exp])
                                                      (syntax (car x)))
                                                    (lambda (more-pvar-lst more-cata-defs more-dotted-vars)
                                                      (let-values ([(next-tests new-pvar-lst
                                                                                new-cata-defs
                                                                                new-dotted-vars)
                                                                    (nextp new-exp
                                                                           more-pvar-lst
                                                                           more-cata-defs
                                                                           more-dotted-vars)])
                                                        (values (with-syntax ([x exp]
                                                                              [nx new-exp]
                                                                              [body next-tests])
                                                                  (syntax (let ([nx (cdr x)])
                                                                            body)))
                                                                new-pvar-lst
                                                                new-cata-defs
                                                                new-dotted-vars)))
                                                    fail-k
                                                    pvar-lst
                                                    depth
                                                    cata-fun
                                                    cata-defs
                                                    dotted-vars)])
                   ; test that we are not at the end of an item-list, BEFORE
                   ; entering tests for the element pattern (against the 'car' of the item-list)
                   (values (with-syntax ([x exp]
                                         [body after-tests]
                                         [fail-to fail-k])
                             (syntax (if (pair? x)
                                         body
                                         (fail-to))))
                           after-pvar-lst
                           after-cata-defs
                           after-dotted-vars)))]
              [(i ...)
               (sxml-match-syntax-error "bad pattern syntax (not an element pattern)"
                                        stx
                                        (syntax (i ...)))]
              [i
               (identifier? (syntax i))
               (sxml-match-syntax-error "bad pattern syntax (symbol not allowed in this context)"
                                        stx
                                        (syntax i))]
              [literal
               (literal? (syntax literal))
               (let ([new-exp (car (generate-temporaries (list exp)))])
                 (let-values ([(next-tests new-pvar-lst new-cata-defs new-dotted-vars)
                               (nextp new-exp pvar-lst cata-defs dotted-vars)])
                   (values (with-syntax ([x exp]
                                         [nx new-exp]
                                         [body next-tests]
                                         [fail-to fail-k])
                             (syntax (if (and (pair? x) (equal? literal (car x)))
                                         (let ([nx (cdr x)])
                                           body)
                                         (fail-to))))
                           new-pvar-lst
                           new-cata-defs
                           new-dotted-vars)))]))])
      (let ([fail-k (syntax failure)])
        (syntax-case stx (unquote guard ->)
          [(compile-clause ((unquote var) (guard gexp ...) action0 action ...)
                           exp
                           cata-fun
                           fail-exp)
           (identifier? (syntax var))
           (syntax (let ([var exp])
                     (if (and gexp ...)
                         (begin action0 action ...)
                         (fail-exp))))]
          [(compile-clause ((unquote [cata -> cvar ...]) (guard gexp ...) action0 action ...)
                           exp
                           cata-fun
                           fail-exp)
           (syntax (if (and gexp ...)
                       (let-values ([(cvar ...) (cata exp)])
                         (begin action0 action ...))
                       (fail-exp)))]
          [(compile-clause ((unquote [cvar ...]) (guard gexp ...) action0 action ...)
                           exp
                           cata-fun
                           fail-exp)
           (if (not (extract-cata-fun (syntax cata-fun)))
               (sxml-match-syntax-error "sxml-match pattern: catamorphism not allowed in this context"
                                        stx
                                        (syntax [cvar ...]))
               (syntax (if (and gexp ...)
                           (let-values ([(cvar ...) (cata-fun exp)])
                             (begin action0 action ...))
                           (fail-exp))))]
          [(compile-clause ((unquote var) action0 action ...) exp cata-fun fail-exp)
           (identifier? (syntax var))
           (syntax (let ([var exp])
                     action0 action ...))]
          [(compile-clause ((unquote [cata -> cvar ...]) action0 action ...) exp cata-fun fail-exp)
           (syntax (let-values ([(cvar ...) (cata exp)])
                     action0 action ...))]
          [(compile-clause ((unquote [cvar ...]) action0 action ...) exp cata-fun fail-exp)
           (if (not (extract-cata-fun (syntax cata-fun)))
               (sxml-match-syntax-error "sxml-match pattern: catamorphism not allowed in this context"
                                        stx
                                        (syntax [cvar ...]))
               (syntax (let-values ([(cvar ...) (cata-fun exp)])
                         action0 action ...)))]
          [(compile-clause ((lst . rst) (guard gexp ...) action0 action ...) exp cata-fun fail-exp)
           (and (identifier? (syntax lst)) (eq? 'list (syntax->datum (syntax lst))))
           (let-values ([(result pvar-lst cata-defs dotted-vars)
                         (compile-item-list (syntax rst)
                                            (syntax exp)
                                            (lambda (new-pvar-lst new-cata-defs new-dotted-vars)
                                              (values
                                               (with-syntax
                                                   ([exp-body (process-cata-defs new-cata-defs
                                                                                 (process-output-action
                                                                                  (syntax (begin action0
                                                                                                 action ...))
                                                                                  new-dotted-vars))]
                                                    [fail-to fail-k])
                                                 (syntax (if (and gexp ...) exp-body (fail-to))))
                                               new-pvar-lst
                                               new-cata-defs
                                               new-dotted-vars))
                                            fail-k
                                            #t
                                            '()
                                            0
                                            (extract-cata-fun (syntax cata-fun))
                                            '()
                                            '())])
             (with-syntax ([fail-to fail-k]
                           [body result])
               (syntax (let ([fail-to fail-exp])
                         (if (nodeset? exp)
                             body
                             (fail-to))))))]
          [(compile-clause ((lst . rst) action0 action ...) exp cata-fun fail-exp)
           (and (identifier? (syntax lst)) (eq? 'list (syntax-object->datum (syntax lst))))
           (let-values ([(result pvar-lst cata-defs dotted-vars)
                         (compile-item-list (syntax rst)
                                            (syntax exp)
                                            (lambda (new-pvar-lst new-cata-defs new-dotted-vars)
                                              (values (process-cata-defs new-cata-defs
                                                                         (process-output-action
                                                                          (syntax (begin action0
                                                                                         action ...))
                                                                          new-dotted-vars))
                                                      new-pvar-lst
                                                      new-cata-defs
                                                      new-dotted-vars))
                                            fail-k
                                            #t
                                            '()
                                            0
                                            (extract-cata-fun (syntax cata-fun))
                                            '()
                                            '())])
             (with-syntax ([body result]
                           [fail-to fail-k])
               (syntax (let ([fail-to fail-exp])
                         (if (nodeset? exp)
                             body
                             (fail-to))))))]
          [(compile-clause ((fst . rst) (guard gexp ...) action0 action ...) exp cata-fun fail-exp)
           (identifier? (syntax fst))
           (let-values ([(result pvar-lst cata-defs dotted-vars)
                         (compile-element-pat (syntax (fst . rst))
                                              (syntax exp)
                                              (lambda (new-pvar-lst new-cata-defs new-dotted-vars)
                                                (values
                                                 (with-syntax
                                                     ([body (process-cata-defs new-cata-defs
                                                                               (process-output-action
                                                                                (syntax (begin action0
                                                                                               action ...))
                                                                                new-dotted-vars))]
                                                      [fail-to fail-k])
                                                   (syntax (if (and gexp ...) body (fail-to))))
                                                 new-pvar-lst
                                                 new-cata-defs
                                                 new-dotted-vars))
                                              fail-k
                                              '()
                                              0
                                              (extract-cata-fun (syntax cata-fun))
                                              '()
                                              '())])
             (with-syntax ([fail-to fail-k]
                           [body result])
               (syntax (let ([fail-to fail-exp])
                         body))))]
          [(compile-clause ((fst . rst) action0 action ...) exp cata-fun fail-exp)
           (identifier? (syntax fst))
           (let-values ([(result pvar-lst cata-defs dotted-vars)
                         (compile-element-pat (syntax (fst . rst))
                                              (syntax exp)
                                              (lambda (new-pvar-lst new-cata-defs new-dotted-vars)
                                                (values (process-cata-defs new-cata-defs
                                                                           (process-output-action
                                                                            (syntax (begin action0
                                                                                           action ...))
                                                                            new-dotted-vars))
                                                        new-pvar-lst
                                                        new-cata-defs
                                                        new-dotted-vars))
                                              fail-k
                                              '()
                                              0
                                              (extract-cata-fun (syntax cata-fun))
                                              '()
                                              '())])
             (with-syntax ([fail-to fail-k]
                           [body result])
               (syntax (let ([fail-to fail-exp])
                         body))))]
          [(compile-clause ((i ...) (guard gexp ...) action0 action ...) exp cata-fun fail-exp)
           (sxml-match-syntax-error "bad pattern syntax (not an element pattern)"
                                    stx
                                    (syntax (i ...)))]
          [(compile-clause ((i ...) action0 action ...) exp cata-fun fail-exp)
           (sxml-match-syntax-error "bad pattern syntax (not an element pattern)"
                                    stx
                                    (syntax (i ...)))]
          [(compile-clause (pat (guard gexp ...) action0 action ...) exp cata-fun fail-exp)
           (identifier? (syntax pat))
           (sxml-match-syntax-error "bad pattern syntax (symbol not allowed in this context)"
                                    stx
                                    (syntax pat))]
          [(compile-clause (pat action0 action ...) exp cata-fun fail-exp)
           (identifier? (syntax pat))
           (sxml-match-syntax-error "bad pattern syntax (symbol not allowed in this context)"
                                    stx
                                    (syntax pat))]
          [(compile-clause (literal (guard gexp ...) action0 action ...) exp cata-fun fail-exp)
           (literal? (syntax literal))
           (syntax (if (and (equal? literal exp) (and gexp ...))
                       (begin action0 action ...)
                       (fail-exp)))]
          [(compile-clause (literal action0 action ...) exp cata-fun fail-exp)
           (literal? (syntax literal))
           (syntax (if (equal? literal exp)
                       (begin action0 action ...)
                       (fail-exp)))])))))

(define-syntax sxml-match1
  (syntax-rules ()
    [(sxml-match1 exp cata-fun clause)
     (compile-clause clause exp cata-fun
                     (lambda () (error 'sxml-match "no matching clause found")))]
    [(sxml-match1 exp cata-fun clause0 clause ...)
     (let/ec escape
       (compile-clause clause0 exp cata-fun
                       (lambda () (call-with-values
                                      (lambda () (sxml-match1 exp cata-fun
                                                              clause ...))
                                    escape))))]))

(define-syntax sxml-match
  (syntax-rules ()
    ((sxml-match val clause0 clause ...)
     (letrec ([cfun (lambda (exp)
                      (sxml-match1 exp cfun clause0 clause ...))])
       (cfun val)))))

(define-syntax sxml-match-let1
  (syntax-rules ()
    [(sxml-match-let1 syntag synform () body0 body ...)
     (let () body0 body ...)]
    [(sxml-match-let1 syntag synform ([pat exp]) body0 body ...)
     (compile-clause (pat (let () body0 body ...))
                     exp
                     #f
                     (lambda () (error 'syntag "could not match pattern ~s" 'pat)))]
    [(sxml-match-let1 syntag synform ([pat0 exp0] [pat exp] ...) body0 body ...)
     (compile-clause (pat0 (sxml-match-let1 syntag synform ([pat exp] ...) body0 body ...))
                     exp0
                     #f
                     (lambda () (error 'syntag "could not match pattern ~s" 'pat0)))]))

(define-syntax sxml-match-let-help
  (lambda (stx)
    (syntax-case stx ()
      [(sxml-match-let-help syntag synform ([pat exp] ...) body0 body ...)
       (with-syntax ([(temp-name ...) (generate-temporaries (syntax (exp ...)))])
         (syntax (let ([temp-name exp] ...)
                   (sxml-match-let1 syntag synform ([pat temp-name] ...) body0 body ...))))])))

(define-syntax sxml-match-let
  (lambda (stx)
    (syntax-case stx ()
      [(sxml-match-let ([pat exp] ...) body0 body ...)
       (with-syntax ([synform stx])
         (syntax (sxml-match-let-help sxml-match-let synform ([pat exp] ...) body0 body ...)))])))

(define-syntax sxml-match-let*
  (lambda (stx)
    (syntax-case stx ()
      [(sxml-match-let* () body0 body ...)
       (syntax (let () body0 body ...))]
      [(sxml-match-let* ([pat0 exp0] [pat exp] ...) body0 body ...)
       (with-syntax ([synform stx])
         (syntax (sxml-match-let-help sxml-match-let* synform ([pat0 exp0])
                                      (sxml-match-let* ([pat exp] ...)
                                                       body0 body ...))))])))