File: xmls.lisp

package info (click to toggle)
cl-xmls 3.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 720 kB
  • sloc: xml: 7,639; lisp: 1,084; perl: 101; makefile: 39
file content (946 lines) | stat: -rw-r--r-- 33,462 bytes parent folder | download
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
;;; $Id: xmls.lisp 2400 2017-12-18 00:17:36Z rpgoldman $
;;; xmls
;;; a simple xml parser for common lisp
;;; author: Miles Egan <miles@caddr.com>
;;; see COPYING file for license information

(defpackage xmls
  (:use :cl) ; :cl-user
  (:shadow #:read-char #:unread-char)
  (:export node-name node-ns node-attrs node-children make-node parse toxml write-xml
           node-p nodelist->node
           node->nodelist
           node ; needed to support use in typep

           ;; backwards compatibility
           #:parse-to-list

           ;; processing instruction objects
           proc-inst-p
           proc-inst-target
           proc-inst-contents

           write-prologue
           write-prolog
           ;; additional helpers from Robert P. Goldman
           make-xmlrep xmlrep-add-child!
           xmlrep-tag xmlrep-tagmatch
           xmlrep-attribs xmlrep-children
           xmlrep-string-child xmlrep-integer-child
           xmlrep-find-child-tags xmlrep-find-child-tag
           xmlrep-attrib-value
           xmlrep-boolean-attrib-value

           ;;debugging
           debug-on debug-off))


(in-package :xmls)

;;;-----------------------------------------------------------------------------
;;; GLOBAL SETTINGS
;;;-----------------------------------------------------------------------------
(defvar *strip-comments* t)
(defvar *compress-whitespace* t)
(defvar *test-verbose* nil)
(defvar *discard-processing-instructions*)
(defvar *entities*
  #(("lt;" #\<)
    ("gt;" #\>)
    ("amp;" #\&)
    ("apos;" #\')
    ("quot;" #\")))
(eval-when (:compile-toplevel :load-toplevel :execute)
  (defvar *whitespace* (remove-duplicates
                        '(#\Newline #\Space #\Tab #\Return #\Linefeed))))
(defvar *char-escapes*
  (let ((table (make-array 256 :element-type 'string :initial-element "")))
    (declare (type vector *entities*))
    (loop
     for code from 0 to 255
     for char = (code-char code)
     for entity = (first (find char *entities* :test #'char= :key #'second))
     do (setf (svref table code)
              (cond
                (entity
                 (concatenate 'string "&" entity))
                ((and (or (< code 32) (> code 126))
                      (not (= code 10))
                      (not (= code 9)))
                 (format nil "&#x~x;" code))
                (t
                 (format nil "~x" char))))
     finally (return table))
    table))

;;;---------------------------------------------------------------------------
;;; DYNAMIC VARIABLES
;;;---------------------------------------------------------------------------
(defvar *parser-stream* nil
  "The currently-being-parsed stream. Used so that we can appropriately track
the line number.")
(defvar *parser-line-number* nil)



;;;-----------------------------------------------------------------------------
;;; CONDITIONS
;;;-----------------------------------------------------------------------------
(define-condition xml-parse-error (error)
  ((line :initarg :line
         :initform nil
         :reader error-line))
  (:report (lambda (xpe stream)
             (format stream "XML-PARSE-ERROR~@[ at line ~d~]"
                     (error-line xpe)))))

(defmethod initialize-instance :after ((obj xml-parse-error) &key)
  (unless (slot-value obj 'line)
    (when *parser-line-number*
      (setf (slot-value obj 'line) *parser-line-number*))))

;;;-----------------------------------------------------------------------------
;;; NODE INTERFACE
;;;-----------------------------------------------------------------------------
(defstruct (node (:constructor %make-node))
  name
  ns
  attrs
  children)
  
(defun make-node (&key name ns attrs child children)
  "Convenience function for creating a new xml node."
  (when (and child children)
    (error "Cannot specify both :child and :children for MAKE-NODE."))
  (let ((children (if child
                      (list child)
                    children)))
    (%make-node :name name :ns ns
                :children children
                :attrs attrs)))

(defun nodelist->node (nodelist)
  "Take old-style list representation of XMLS nodes and translate it
into a NODE."
  (if (stringp nodelist)
      ;; child is a string -- a literal child
      ;; FIXME: is that the right XML jargon?
      nodelist
      (make-node :name (if (consp (car nodelist))
                           (caar nodelist)
                           (car nodelist))
                 :ns (if (consp (car nodelist))
                         (cdar nodelist)
                         nil)
                 :children (mapcar 'nodelist->node (cddr nodelist))
                 :attrs (second nodelist))))

(defun node->nodelist (node)
  "Backwards compatibility function.  Will take a NODE \(the output of
PARSE\), and translate it into a list structure that looks like the
output of PARSE from XMLS 1.x.  This should only be needed if there's
client code that didn't obey the API and instead directly accessed the
Lisp list structures that XMLS used to produce. Such code should be
fixed."
  (etypecase node
      (string node)
      (node
       (list* (if (node-ns node) (cons (node-name node) (node-ns node))
                  (node-name node))
              (node-attrs node)
              (mapcar 'node->nodelist (node-children node))))))
  

;;;-----------------------------------------------------------------------------

;;;---------------------------------------------------------------------------
;;; XML Processing Instruction
;;;---------------------------------------------------------------------------
(defstruct proc-inst
  (target "" :type string)
  (contents "" :type string)
  )


;;;-----------------------------------------------------------------------------
;;; UTILITY FUNCTIONS
;;;-----------------------------------------------------------------------------
(defun compress-whitespace (str)
  (if *compress-whitespace*
      (progn
        (setf str (string-trim *whitespace* str))
        (if (= 0 (length str))
            nil
            str))
      str))

;;;---------------------------------------------------------------------------
;;; Replicated the Norvig debug to avoid annoying dependency
;;;---------------------------------------------------------------------------
(defvar *dbg-ids* nil "Identifiers used by dbg")
(defmacro dbg (id format-string &rest args)
  "Print debugging info if (DEBUG ID) has been specified."
  `(when (member ',id *dbg-ids*)
     (fresh-line *debug-io*)
     (funcall #'format *debug-io* ,format-string ,@args)))
(defun debug-on (&rest ids)
  "Start dbg output on the given ids."
  (setf *dbg-ids* (union ids *dbg-ids*)))

(defun debug-off (&rest ids)
  "Stop dbg on the ids.  With no ids, stop dbg altogether."
  (setf *dbg-ids* (if (null ids) nil
                      (set-difference *dbg-ids* ids))))



;; (defun write-escaped (string stream)
;;   "Writes string to stream with all character entities escaped."
;;   #-allegro (coerce string 'simple-base-string)
;;   (when (eq stream t) (setf stream *standard-output*))
;;   (loop for char across string
;;         for esc = (svref *char-escapes* (char-code char))
;;         do (write-sequence esc stream)))

;;; Alternative definition, lifted from Edi Weitz's hunchentoot, per Norman
;;; Werner's suggestion. [2010/12/09:rpg]
(defun write-escaped (string stream)
  (write-string (escape-for-html string) stream))

(defun escape-for-html (string)
  "Escapes the characters #\\<, #\\>, #\\', #\\\", and #\\& for HTML output."
  (with-output-to-string (out)
    (with-input-from-string (in string)
      (loop for char = (read-char in nil nil)
            while char
            do (case char
                 ((#\<) (write-string "&lt;" out))
                 ((#\>) (write-string "&gt;" out))
                 ((#\") (write-string "&quot;" out))
                 ((#\') (write-string "&#039;" out))
                 ((#\&) (write-string "&amp;" out))
                 (otherwise (write-char char out)))))))

(defun make-extendable-string (&optional (size 10))
  "Creates an adjustable string with a fill pointer."
  (make-array size
              :element-type 'character
              :adjustable t
              :fill-pointer 0))

(defun push-string (c string)
  "Shorthand function for adding characters to an extendable string."
  (vector-push-extend c string))

(defun translate-raw-value (raw-value)
  "Helper function for xml generation."
  (etypecase raw-value
    (string raw-value)
    (symbol (symbol-name raw-value))
    (integer (format nil "~D" raw-value))
    (float (format nil "~G" raw-value))))

(defun generate-xml (e s indent)
  "Renders a lisp node tree to an xml string stream."
  (if (> indent 0) (incf indent))
  (etypecase e
    (node
     (progn
       (dotimes (i (* 2 (- indent 2)))
         (write-char #\Space s))
       (format s "<~A~@[ xmlns=\"~A\"~]" (node-name e) (node-ns e))
       (loop for a in (node-attrs e)
             do (progn
                  (write-char #\Space s)
                  (write-string (first a) s)
                  (write-char #\= s)
                  (write-char #\" s)
                  (write-escaped (translate-raw-value (second a)) s)
                  (write-char #\" s))))
     (if (null (node-children e))
         (progn
           (write-string "/>" s)
           (if (> indent 0) (write-char #\Newline s)))
         (progn
           (write-char #\> s)
           (if (> indent 0) (write-char #\Newline s))
           (mapc #'(lambda (c) (generate-xml c s indent)) (node-children e))
           (if (> indent 0)
               (progn
                 (dotimes (i (* 2 (- indent 2)))
                   (write-char #\Space s))))
           (format s "</~A>" (node-name e))
           (if (> indent 0) (write-char #\Newline s)))))
    (number
     (generate-xml (translate-raw-value e) s indent))
    (symbol
     (generate-xml (translate-raw-value e) s indent))
    (string
     (progn
       (if (> indent 0)
           (progn
             (dotimes (i (* 2 (- indent 2)))
               (write-char #\Space s))))
       (write-escaped e s)
       (if (> indent 0) (write-char #\Newline s))))))

;;;-----------------------------------------------------------------------------
;;; PARSER STATE & LOOKAHEAD
;;;-----------------------------------------------------------------------------
(defstruct state
  "Represents parser state.  Passed among rules to avoid threading issues."
  (got-doctype nil)
  (lines 1 :type integer)
  nsstack
  stream)

(defun resolve-entity (ent)
  "Resolves the xml entity ENT to a character.  Numeric entities are
converted using CODE-CHAR, which only works in implementations that
internally encode strings in US-ASCII, ISO-8859-1 or UCS."
  (declare (type simple-string ent))
  (declare (type vector *entities*))
  (or (and (>= (length ent) 2)
           (char= (char ent 0) #\#)
           (code-char
            (if (char= (char ent 1) #\x)
                (parse-integer ent :start 2 :end (- (length ent) 1) :radix 16)
                (parse-integer ent :start 1 :end (- (length ent) 1)))))
      (second (find ent *entities* :test #'string= :key #'first))
      (error "Unable to resolve entity ~S" ent)))

(declaim (inline peek-stream))
(defun peek-stream (stream)
  "Looks one character ahead in the input stream.  Serves as a potential hook for
character translation."
  (peek-char nil stream nil))

(defun read-stream (stream)
  "Reads a character from the stream, translating entities as it goes."
  (let ((c (read-char stream nil)))
    (if (and c (not (char= c #\&)))
        c
        (loop with ent = (make-extendable-string 5)
              for char = (read-char stream)
              do (push-string char ent)
              until (char= char #\;)
              finally (return (resolve-entity (coerce ent 'simple-string)))))))

;;;---------------------------------------------------------------------------
;;; Shadow READ-CHAR and UNREAD-CHAR so we can count lines while we parse...
;;;---------------------------------------------------------------------------
(defun read-char (&optional (stream *standard-input*) (eof-error-p t) eof-value recursive-p)
  (let ((eof-p nil))
    (let ((c
            (catch 'char-return
              (handler-bind
                  ((end-of-file
                     #'(lambda (e)
                         (declare (ignore e))
                         (unless eof-error-p
                           (setf eof-p t)
                           (throw 'char-return eof-value)))))
                (common-lisp:read-char stream t nil recursive-p)))))
    (when (and (eq stream *parser-stream*)
               (not eof-p)
               (char= c #\newline))
      (incf *parser-line-number*))
    c)))

(defun unread-char (char &optional (stream *standard-input*))
  (when (char= char #\newline)
    (decf *parser-line-number*))
  (common-lisp:unread-char char stream))
    
;;;END shadowing--------------------------------------------------------------

(define-symbol-macro next-char (peek-stream (state-stream s)))

(defmacro eat ()
  "Consumes one character from the input stream."
  `(read-char (state-stream s)))

(defmacro puke (char)
  "The opposite of EAT."
  `(unread-char ,char (state-stream s)))

(defmacro match (&rest matchers)
  "Attempts to match the next input character with one of the supplied matchers."
  `(let ((c (peek-stream (state-stream s))))
    (and
     (or ,@(loop for m in matchers
                 collect (etypecase m
                           (standard-char `(char= ,m c))
                           (symbol `(,m c)))))
     ;; cheat here a little bit - eat entire char entity instead
     ;; of peeked char
     (read-stream (state-stream s)))))

(defmacro match-seq (&rest sequence)
  "Tries to match the supplied matchers in sequence with characters in the input stream."
  `(and ,@(loop for s in sequence
                collect `(match ,s))))

(defmacro match* (&rest sequence)
  "Matches any occurances of any of the supplied matchers."
  `(loop with data = (make-extendable-string 10)
    for c = (match ,@sequence)
    while c
    do (push-string c data)
    finally (return data)))

(defmacro match+ (&rest sequence)
  "Matches one or more occurances of any of the supplied matchers."
  `(and (peek ,@sequence)
    (match* ,@sequence)))

(defmacro peek (&rest matchers)
  "Looks ahead for an occurance of any of the supplied matchers."
  `(let ((c (peek-stream (state-stream s))))
    (or ,@(loop for m in matchers
                collect (etypecase m
                          (standard-char `(char= ,m c))
                          (symbol `(,m c)))))))

(defmacro must (&rest body)
  "Throws a parse error if the supplied forms do not succeed."
  `(or (progn ,@body)
    (error 'xml-parse-error)))

;;;-----------------------------------------------------------------------------
;;; PARSER INTERNAL FUNCTIONS
;;;-----------------------------------------------------------------------------
(defstruct element
  "Common return type of all rule functions."
  (type nil :type symbol)
  (val nil))

(defun resolve-namespace (elem env)
  "Maps the ns prefix to its associated url via the supplied ns env."
  (let ((ns (node-ns elem)))
    (dolist (e env)
      (let ((nsurl (assoc ns e :test #'string=)))
        (and nsurl
             (setf (node-ns elem) (cadr nsurl))
             (return ns))))))

;;;-----------------------------------------------------------------------------
;;; MATCH AND RULE BUILDING UTILITIES
;;;-----------------------------------------------------------------------------
(defmacro defmatch (name &rest body)
  "Match definition macro that provides a common lexical environment for matchers."
  `(defun ,name (c)
    ,@body))

(defmacro defrule (name &rest body)
  "Rule definition macro that provides a common lexical environment for rules."
  `(defun ,name (s)
    ,@body))

(defmacro matchfn (name)
  "Convenience macro for creating an anonymous function wrapper around a matcher macro."
  `(lambda (s) (match ,name)))

(defun none-or-more (s func)
  "Collects any matches of the supplied rule with the input stream."
  (declare (type function func))
  (let ((val (funcall func s)))
    (if val
        (multiple-value-bind (res nextval)
            (none-or-more s func)
          (values res (cons val nextval)))
        (values t nil))))

(defun one-or-more (s func)
  "Collects one or more matches of the supplied rule with the input stream."
  (declare (type function func))
  (let ((val (funcall func s)))
    (if val
        (multiple-value-bind (res nextval)
            (none-or-more s func)
          (declare (ignore res))
          (cons val nextval))
        nil)))

;;;-----------------------------------------------------------------------------
;;; MATCHERS
;;;-----------------------------------------------------------------------------
(defmatch digit ()
  (and c (digit-char-p c)))

(defmatch letter ()
  (and c (alpha-char-p c)))

;; Modified because *whitespace* is not defined at compile
;; time. [2004/08/31:rpg]
(defmatch ws-char ()
  (member c *whitespace*))
;;;  (case c
;;;    (#.*whitespace* t)
;;;    (t nil)))

(defmatch namechar ()
  (or
   (and c (alpha-char-p c))
   (and c (digit-char-p c))
   (case c
     ((#\. #\- #\_ #\:) t))))

(defmatch ncname-char ()
  (or
   (and c (alpha-char-p c))
   (and c (digit-char-p c))
   (case c
     ((#\. #\- #\_) t))))

(defmatch attr-text-dq ()
  (and c (not (member c (list #\< #\")))))

(defmatch attr-text-sq ()
  (and c (not (member c (list #\< #\')))))

(defmatch chardata ()
  (and c (not (char= c #\<))))

(defmatch comment-char ()
  (and c (not (eql c #\-))))

;;;-----------------------------------------------------------------------------
;;; RULES
;;;-----------------------------------------------------------------------------
(defrule ncname ()
  (and (peek letter #\_)
       (match+ ncname-char)))

(defrule qname ()
  (let (name suffix)
    (and
     (setf name (ncname s))
     (or
      (and
       (match #\:)
       (setf suffix (ncname s)))
      t))
    (values name suffix)))

(defrule attr-or-nsdecl ()
  (let (suffix name val)
    (and
     (setf (values name suffix) (qname s))
     (or
      (and
       (progn
         (match* ws-char)
         (match #\=))
       (or
        (and
         (progn
           (match* ws-char)
           (match #\"))
         (setf val (match* attr-text-dq))
         (match #\"))
        (and
         (progn
           (match* ws-char)
           (match #\'))
         (setf val (match* attr-text-sq))
         (match #\'))))
      t)
     (if (string= "xmlns" name) (list 'nsdecl suffix val) (list
                                                           'attr (or suffix name) val)))))

(defrule ws ()
  (and (match+ ws-char)
       (make-element :type 'whitespace :val nil)))

(defrule name ()
  (and
   (peek namechar #\_ #\:)
   (match* namechar)))

(defrule ws-attr-or-nsdecl ()
  (and
   (ws s)
   (attr-or-nsdecl s)))

(defrule start-tag ()
  (let (name suffix attrs nsdecls)
    (and
     (peek namechar)
     (setf (values name suffix) (qname s))
     (multiple-value-bind (res a)
         (none-or-more s #'ws-attr-or-nsdecl)
       (mapcar (lambda (x) (if (eq (car x) 'attr)
                               (push (cdr x) attrs)
                               (push (cdr x) nsdecls)))
               a)
       res)
     (or (ws s) t)
     (values
      (make-node
       :name (or suffix name)
       :ns (and suffix name)
       :attrs attrs)
      nsdecls))))

(defrule end-tag ()
  (let (name suffix)
    (and
     (match #\/)
     (setf (values name suffix) (qname s))
     (or (ws s) t)
     (match #\>)
     (make-element :type 'end-tag :val (or suffix name)))))

(defrule comment ()
  (and
   (match-seq #\! #\- #\-)
   (progn
     (loop until (match-seq #\- #\- #\>)
           do (eat))
     t)
   (make-element :type 'comment)))

;;; For the CDATA matching of ]]> I by hand generated an NFA, and then
;;; determinized it (also by hand).  Then I did a simpler thing of just pushing
;;; ALL the data onto the data string, and truncating it when done.
(defrule comment-or-cdata ()
  (and
   (peek #\!)
   (must (or (comment s)
             (and
              (match-seq #\[ #\C #\D #\A #\T #\A #\[)
              (loop with data = (make-extendable-string 50)
                    with state = 0
                    for char = (eat)
                    do (push-string char data)
                    do (case state
                         (0
                          (case char
                            (#\]
                             (dbg :cdata "State 0 Match #\], go to state {0,1} = 4.")
                             (setf state 4))
                            (otherwise
                             (dbg :cdata "State 0 Non-], go to (remain in) state 0."))))
                         (4 ; {0, 1}
                          (case char
                            (#\]
                             (dbg :cdata "State 4 {0, 1}, match ], go to state {0,1,2} = 5")
                             (setf state 5))
                            (otherwise
                             (dbg :cdata "State 4 {0, 1}, Non-], go to state 0.")
                             (setf state 0))))
                         (5 ; {0, 1, 2}
                          (case char
                            (#\]
                             (dbg :cdata "State 5 {0, 1, 2}, match ], stay in state 5."))
                            (#\>
                             (dbg :cdata "State 5 {0, 1, 2}, match >, finish match and go to state 3.")
                             (setf state 3))
                            (otherwise
                             (dbg :cdata "State 5 {0, 1, 2}, find neither ] nor >; go to state 0.")
                             (setf state 0))))
                         )
                    until (eql state 3)
                    finally (return (make-element
                                     :type 'cdata
                                     :val (coerce
                                           ;; rip the ]]> off the end of the data and return it...
                                           (subseq data 0 (- (fill-pointer data) 3))
                                           'simple-string)))))))))


(declaim (ftype function element))     ; forward decl for content rule
(defrule content ()
  (if (match #\<)
      (must (or (comment-or-cdata s)
                (processing-instruction s)
                (element s)
                (end-tag s)))
      (or (let (content)
            (and (setf content (match+ chardata))
                 (make-element :type 'data :val (compress-whitespace content)))))))

(defrule element ()
  (let (elem children nsdecls end-name)
    (and
     ;; parse front end of tag
     (multiple-value-bind (e n)
         (start-tag s)
       (setf elem e)
       (setf nsdecls n)
       e)
     ;; resolve namespaces *before* parsing children
     (if nsdecls (push nsdecls (state-nsstack s)) t)
     (or (if (or nsdecls (state-nsstack s))
             (resolve-namespace elem (state-nsstack s)))
         t)
     ;; parse end-tag and children
     (or
      (match-seq #\/ #\>)
      (and
       (match #\>)
       (loop for c = (content s)
             while c
             do (etypecase c
                  (element (case (element-type c)
                             (end-tag
                              (return (setf end-name (element-val c))))
                             ;; processing instructions may be discarded
                             (pi
                              (unless *discard-processing-instructions*
                                (when (element-val c)
                                  (push (element-val c) children))))
                             (t (if (element-val c)
                                    (push (element-val c) children)))))))
       (string= (node-name elem) end-name)))
     ;; package up new node
     (progn
       (setf (node-children elem) (nreverse children))
       (make-element :type 'elem :val elem)))))

(defrule processing-instruction ()
  (let (name contents)
    (and
     (match #\?)
     (setf name (name s))
     (not (string= name "xml"))
     ;; contents of a processing instruction can be arbitrary stuff, as long
     ;; as it doesn't contain ?>...
     (setf contents (pi-contents s))
     ;; if we get here, we have eaten ?> off the input in the course of
     ;; processing PI-CONTENTS
     (make-element :type 'pi :val (make-proc-inst :target name :contents contents)))))

(defrule pi-contents ()
  (loop with data = (make-extendable-string 50)
         with state = 0
         for char = (eat)
         do (push-string char data)
         do (ecase state
              (0
               (case char
                 (#\?
                  (dbg :pi-contents "State 0 Match #\?, go to state 1.")
                  (setf state 1))
                 (otherwise
                  (dbg :pi-contents "State 0 ~c, go to (remain in) state 0." char))))
              (1
               (case char
                 (#\>
                  (dbg :pi-contents "State 1 Match #\>, done.")
                  (setf state 2))
                 (otherwise
                  (dbg :pi-contents "State 1, ~c, do not match #\>, return to 0." char)
                  (setf state 0)))))
         until (eql state 2)
         finally (return (coerce
                          ;; rip the ?> off the end of the data and return it...
                          (subseq data 0 (max 0 (- (fill-pointer data) 2)))
                          'simple-string))))

(defrule xmldecl ()
    (let (name contents)
    (and
     (match #\?)
     (setf name (name s))
     (string= name "xml")
     (setf contents (none-or-more s #'ws-attr-or-nsdecl))
     (match-seq #\? #\>)
     (make-element :type 'xmldecl :val contents))))

(defrule comment-or-doctype ()
  ;; skip dtd - bail out to comment if it's a comment
  ;; only match doctype once
  (and
   (peek #\!)
   (or (comment s)
       (and (not (state-got-doctype s))
            (must (match-seq #\D #\O #\C #\T #\Y #\P #\E))
            (loop with level = 1
                  do (case (eat)
                       (#\> (decf level))
                       (#\< (incf level)))
                  until (eq level 0)
                  finally (return t))
            (setf (state-got-doctype s) t)
            (make-element :type 'doctype)))))

(defrule misc ()
  (or
   (ws s)
   (and (match #\<) (must (or (processing-instruction s)
                              (comment-or-doctype s)
                              (element s))))))

(defrule document ()
  (let (elem)
    (if (match #\<)
        (must (or (xmldecl s)
                  (comment-or-doctype s)
                  (setf elem (element s)))))
    ;; NOTE: I don't understand this: it seems to parse arbitrary crap
    (unless elem
      (loop for c = (misc s)
            while c
            do (cond ((eql (element-type c) 'elem)
                      (return (setf elem c)))
                     ((and (eql (element-type c) 'pi)
                           (not *discard-processing-instructions*))
                      (return (setf elem c))))))
                       
    (and elem (element-val elem))))

;;;-----------------------------------------------------------------------------
;;; PUBLIC INTERFACE
;;;-----------------------------------------------------------------------------
(defun write-xml (e s &key (indent nil))
  "Renders a lisp node tree to an xml stream.  Indents if indent is non-nil."
  (if (null s)
      (toxml e :indent indent)
    (generate-xml e s (if indent 1 0))))

(defun write-prologue (xml-decl doctype s)
  "Render the leading <?xml ... ?> and <!DOCTYPE ... > tags to an xml stream."
  (format s "<?xml")
  (dolist (attrib xml-decl)
    (format s " ~A=\"~A\"" (car attrib) (cdr attrib)))
  (format s " ?>~%")
  (when doctype
    (format s "<!DOCTYPE ~A>~%" doctype)))

(defun write-prolog (xml-decl doctype s)
  (write-prologue xml-decl doctype s))

(defun toxml (e &key (indent nil))
  "Renders a lisp node tree to an xml string."
  (with-output-to-string (s)
    (write-xml e s :indent indent)))

(defun parse (s &key (compress-whitespace t) (quash-errors t))
  "Parses the supplied stream or string into a lisp node tree."
  (let* ((*compress-whitespace* compress-whitespace)
         (*discard-processing-instructions* t)
         (stream
           (etypecase s
             (string (make-string-input-stream s))
             (stream s)))
         (*parser-stream* stream)
         (*parser-line-number* 1))
    (if quash-errors
        (handler-case
            (document (make-state :stream stream))
          (end-of-file () nil)
          (xml-parse-error () nil))
        (document (make-state :stream stream)))))

(defun parse-to-list (&rest args)
  (node->nodelist (apply #'parse args)))

(defparameter *test-files*
  (mapcar #'(lambda (x) (asdf:system-relative-pathname "xmls" (format nil "tests/~a" x)))
          (list "ant/build.xml"
                "beep/greeting1.xml"
                "beep/msg1.xml"
                "cdata/cdata1.xml"
                "char-encoding/flux-test-utf-8.xml"
                "dav/propfind1.xml"
                "dav/propfind2r.xml"
                "dav/propfind3.xml"
                "large/two_gent.xml"
                "misc/entity.xml"
                "misc/example.xml"
                "misc/minimal.xml"
                "misc/minimal1.xml"
                "misc/minimal2.xml"
                "misc/minimal3.xml"
                "misc/whitespace.xml"
                "namespace/namespace1.xml"
                "namespace/namespace2.xml"
                "namespace/namespace3.xml"
                "nxml/genetics-article.xml"
                "nxml/no-processing-instructions.xml"
                "nxml/processing-instructions.xml"
                "rss.xml"
                "soap/soap1.xml"
                "soap/soap2.xml"
                "soap/soap3.xml"
                "soap/soap4.xml"
                "soap/soap5.xml"
                "soap/soap6.xml"
                "xml-rpc/array.xml"
                "xml-rpc/blogger1.xml"
                "xml-rpc/fault.xml"
                "xml-rpc/methodCall.xml"
                "xml-rpc/methodResponse.xml"
                "xml-rpc/struct.xml")))

#+(or sbcl cmu allegro abcl ccl clisp)
(defun test (&key interactive (test-files *test-files*))
  "Run the test suite. If it fails, either return NIL \(if INTERACTIVE\),
otherwise exit with an error exit status."
  ;;(sb-profile:profile "XMLS")
  #+cmu(extensions:gc-off) ;; too noisy
  #+clisp (pprint ext:*args*)
  (let ((exit-code 0))
    (dolist (test
                (or test-files
                    #-(or ccl clisp)
                    (cdr
                     #+sbcl  (member "--" sb-ext:*posix-argv* :test 'equal)
                     #+abcl extensions:*command-line-argument-list*
                     #+cmu  (member "--" extensions:*command-line-strings* :test 'equal)
                     #+allegro (sys:command-line-arguments)
                     #+clisp ext:*args*
                     #+ccl
                     ccl:*unprocessed-command-line-arguments*)))
      (catch 'test-failure
        (handler-bind ((error #'(lambda (c)
                                    (format t "FAILED with error:~%~a~%" c)
                                    (setf exit-code 1)
                                    (throw 'test-failure nil))))
          (with-open-file (str test :direction :input)
            (if *test-verbose*
                (let ((parsed (parse str :compress-whitespace t)))
                  (if parsed
                      (format t "~A~%" (toxml parsed :indent t))
                      (progn
                        (format t "~&Failed to parse ~A~%" test)
                        (setf exit-code 1))))
                (progn
                  (format t "~40A" (concatenate 'string (namestring test) "... "))
                  (force-output)
                  (cond ((parse str)
                         (format t "ok~%"))
                        (t
                         (setf exit-code 1)
                         (format t "FAILED!~%"))))))
          (with-open-file (str test :direction :input)
           (if *test-verbose*
               (let ((parsed (parse-to-list str :compress-whitespace t)))
                 (if parsed
                     (format t "~A~%" (toxml parsed :indent t))
                     (progn
                       (format t "~&Failed to parse ~A~%" test)
                       (setf exit-code 1))))
               (progn
                 (format t "~40A" (concatenate 'string (namestring test) "... "))
                 (force-output)
                 (cond ((parse-to-list str)
                        (format t "ok~%"))
                       (t
                        (setf exit-code 1)
                        (format t "FAILED!~%")))))))))
    (catch 'test-failure
      (handler-bind ((error #'(lambda (c)
                                (format t "FAILED with error:~%~S~%" c)
                                (setf exit-code 1)
                                (throw 'test-failure nil))))
        (format t "~40A" "Escaped writing...")
        (force-output)
        (with-output-to-string (str)
          (write-escaped "ÄΩ" str))
        (format t "ok~%")))
    (if interactive
        (zerop exit-code)
        (uiop:quit exit-code))))