Updated TEST for the recent merges. Strip pathnames from TEST.
[xuriella.git] / instructions.lisp
blob987bd9fbdd315912853e5b74a86b0d5c1f1f792d
1 ;;; -*- show-trailing-whitespace: t; indent-tabs-mode: nil -*-
3 ;;; Copyright (c) 2007,2008 David Lichteblau, Ivan Shvedunov.
4 ;;; All rights reserved.
6 ;;; Redistribution and use in source and binary forms, with or without
7 ;;; modification, are permitted provided that the following conditions
8 ;;; are met:
9 ;;;
10 ;;; * Redistributions of source code must retain the above copyright
11 ;;; notice, this list of conditions and the following disclaimer.
12 ;;;
13 ;;; * Redistributions in binary form must reproduce the above
14 ;;; copyright notice, this list of conditions and the following
15 ;;; disclaimer in the documentation and/or other materials
16 ;;; provided with the distribution.
17 ;;;
18 ;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED
19 ;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 ;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 ;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22 ;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 ;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
24 ;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 ;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 ;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 (in-package :xuriella)
32 #+sbcl
33 (declaim (optimize (debug 2)))
36 ;;;; Instructions
38 (defmacro define-instruction (name (args-var env-var) &body body)
39 `(setf (get ',name 'xslt-instruction)
40 (lambda (,args-var ,env-var)
41 (declare (ignorable ,env-var))
42 ,@body)))
44 (define-instruction if (args env)
45 (destructuring-bind (test then &optional else) args
46 (let ((test-thunk (compile-xpath test env))
47 (then-thunk (compile-instruction then env))
48 (else-thunk (when else (compile-instruction else env))))
49 (lambda (ctx)
50 (cond
51 ((xpath:boolean-value (funcall test-thunk ctx))
52 (funcall then-thunk ctx))
53 (else-thunk
54 (funcall else-thunk ctx)))))))
56 (define-instruction when (args env)
57 (destructuring-bind (test &rest body) args
58 (compile-instruction `(if ,test (progn ,@body)) env)))
60 (define-instruction unless (args env)
61 (destructuring-bind (test &rest body) args
62 (compile-instruction `(if (:not ,test) (progn ,@body)) env)))
64 (define-instruction cond (args env)
65 (if args
66 (destructuring-bind ((test &body body) &rest clauses) args
67 (compile-instruction (if (eq test t)
68 `(progn ,@body)
69 `(if ,test
70 (progn ,@body)
71 (cond ,@clauses)))
72 env))
73 (constantly nil)))
75 (define-instruction progn (args env)
76 (if args
77 (let ((first-thunk (compile-instruction (first args) env))
78 (rest-thunk (compile-instruction `(progn ,@(rest args)) env)))
79 (lambda (ctx)
80 (funcall first-thunk ctx)
81 (funcall rest-thunk ctx)))
82 (constantly nil)))
84 (defun decode-qname/runtime (qname namespaces attributep)
85 (handler-case
86 (multiple-value-bind (prefix local-name)
87 (split-qname qname)
88 (values local-name
89 (if (or prefix (not attributep))
90 (cdr (assoc prefix namespaces :test 'equal))
91 "")
92 prefix))
93 (cxml:well-formedness-violation ()
94 (xslt-error "not a qname: ~A" qname))))
96 (define-instruction xsl:element (args env)
97 (destructuring-bind ((name &key namespace use-attribute-sets)
98 &body body)
99 args
100 (declare (ignore use-attribute-sets)) ;fixme
101 (multiple-value-bind (name-thunk constant-name-p)
102 (compile-avt name env)
103 (multiple-value-bind (ns-thunk constant-ns-p)
104 (if namespace
105 (compile-avt namespace env)
106 (values nil t))
107 (let ((body-thunk (compile-instruction `(progn ,@body) env)))
108 (if (and constant-name-p constant-ns-p)
109 (compile-element/constant-name name namespace env body-thunk)
110 (compile-element/runtime name-thunk ns-thunk body-thunk)))))))
112 (defun compile-element/constant-name (qname namespace env body-thunk)
113 ;; the simple case: compile-time decoding of the QName
114 (multiple-value-bind (local-name uri prefix)
115 (decode-qname qname env nil)
116 (when namespace
117 (setf uri namespace))
118 (lambda (ctx)
119 (with-element (local-name uri :suggested-prefix prefix)
120 (funcall body-thunk ctx)))))
122 (defun compile-element/runtime (name-thunk ns-thunk body-thunk)
123 ;; run-time decoding of the QName, but using the same namespaces
124 ;; that would have been known at compilation time.
125 (let ((namespaces *namespaces*))
126 (lambda (ctx)
127 (let ((qname (funcall name-thunk ctx)))
128 (multiple-value-bind (local-name uri prefix)
129 (decode-qname/runtime qname namespaces nil)
130 (when ns-thunk
131 (setf uri (funcall ns-thunk ctx)))
132 (unless uri
133 (setf uri ""))
134 (with-element (local-name uri :suggested-prefix prefix)
135 (funcall body-thunk ctx)))))))
137 (define-instruction xsl:use-attribute-sets (args env)
138 (destructuring-bind (str) args
139 (let ((sets (mapcar (lambda (qname)
140 (multiple-value-list (decode-qname qname env nil)))
141 (words str))))
142 (lambda (ctx)
143 (loop for (local-name uri nil) in sets do
144 (dolist (thunk (find-attribute-set local-name uri))
145 (funcall thunk ctx)))))))
147 (define-instruction xsl:attribute (args env)
148 (destructuring-bind ((name &key namespace) &body body) args
149 (when (null name)
150 (xslt-error "xsl:attribute: name not specified"))
151 (multiple-value-bind (name-thunk constant-name-p)
152 (compile-avt name env)
153 (multiple-value-bind (ns-thunk constant-ns-p)
154 (if namespace
155 (compile-avt namespace env)
156 (values nil t))
157 (let ((value-thunk (compile-instruction `(progn ,@body) env)))
158 (if (and constant-name-p constant-ns-p)
159 (compile-attribute/constant-name name namespace env value-thunk)
160 (compile-attribute/runtime name-thunk ns-thunk value-thunk)))))))
162 (defun compile-attribute/constant-name (qname namespace env value-thunk)
163 ;; the simple case: compile-time decoding of the QName
164 (multiple-value-bind (local-name uri prefix)
165 (decode-qname qname env nil)
166 (when namespace
167 (setf uri namespace))
168 (lambda (ctx)
169 (write-attribute local-name
171 (with-text-output-sink (s)
172 (with-xml-output s
173 (funcall value-thunk ctx)))
174 :suggested-prefix prefix))))
176 (defun compile-attribute/runtime (name-thunk ns-thunk value-thunk)
177 ;; run-time decoding of the QName, but using the same namespaces
178 ;; that would have been known at compilation time.
179 (let ((namespaces *namespaces*))
180 (lambda (ctx)
181 (let ((qname (funcall name-thunk ctx)))
182 (multiple-value-bind (local-name uri prefix)
183 (decode-qname/runtime qname namespaces nil)
184 (when ns-thunk
185 (setf uri (funcall ns-thunk ctx)))
186 (write-attribute local-name
187 (or uri "")
188 (with-text-output-sink (s)
189 (with-xml-output s
190 (funcall value-thunk ctx)))
191 :suggested-prefix prefix))))))
193 (defun remove-excluded-namespaces
194 (namespaces &optional (excluded-uris *excluded-namespaces*))
195 (let ((koerbchen '())
196 (kroepfchen '()))
197 (loop
198 for cons in namespaces
199 for (prefix . uri) = cons
201 (cond
202 ((find prefix kroepfchen :test #'equal))
203 ((find uri excluded-uris :test #'equal)
204 (push prefix kroepfchen))
206 (push cons koerbchen))))
207 koerbchen))
209 (define-instruction xsl:literal-element (args env)
210 (destructuring-bind
211 ((local-name &optional (uri "") suggested-prefix) &body body)
212 args
213 (let ((body-thunk (compile-instruction `(progn ,@body) env))
214 (namespaces (remove-excluded-namespaces *namespaces*)))
215 (lambda (ctx)
216 (with-element (local-name uri
217 :suggested-prefix suggested-prefix
218 :extra-namespaces namespaces)
219 (funcall body-thunk ctx))))))
221 (define-instruction xsl:literal-attribute (args env)
222 (destructuring-bind ((local-name &optional uri suggested-prefix) value) args
223 (let ((value-thunk (compile-avt value env)))
224 (lambda (ctx)
225 (write-attribute local-name
227 (funcall value-thunk ctx)
228 :suggested-prefix suggested-prefix)))))
230 (define-instruction xsl:text (args env)
231 (destructuring-bind (str) args
232 (lambda (ctx)
233 (declare (ignore ctx))
234 (write-text str))))
236 (define-instruction xsl:processing-instruction (args env)
237 (destructuring-bind (name &rest body) args
238 (let ((name-thunk (compile-avt name env))
239 (value-thunk (compile-instruction `(progn ,@body) env)))
240 (lambda (ctx)
241 (write-processing-instruction
242 (funcall name-thunk ctx)
243 (with-text-output-sink (s)
244 (with-xml-output s
245 (funcall value-thunk ctx))))))))
247 (define-instruction xsl:comment (args env)
248 (let ((value-thunk (compile-instruction `(progn ,@args) env)))
249 (lambda (ctx)
250 (write-comment (with-text-output-sink (s)
251 (with-xml-output s
252 (funcall value-thunk ctx)))))))
254 (define-instruction xsl:value-of (args env)
255 (destructuring-bind (xpath) args
256 (let ((thunk (compile-xpath xpath env)))
257 (xslt-trace-thunk
258 (lambda (ctx)
259 (write-text (xpath:string-value (funcall thunk ctx))))
260 "value-of ~s = ~s" xpath :result))))
262 (define-instruction xsl:unescaped-value-of (args env)
263 (destructuring-bind (xpath) args
264 (let ((thunk (compile-xpath xpath env)))
265 (lambda (ctx)
266 (write-unescaped (xpath:string-value (funcall thunk ctx)))))))
268 (define-instruction xsl:copy-of (args env)
269 (destructuring-bind (xpath) args
270 (let ((thunk (compile-xpath xpath env))
271 ;; FIXME: what was this for? --david
272 #+(or) (v (intern-variable "varName" "")))
273 (xslt-trace-thunk
274 (lambda (ctx)
275 (let ((result (funcall thunk ctx)))
276 (typecase result
277 (xpath:node-set ;; FIXME: variables can contain node sets w/fragments inside. Maybe just fragments would do?
278 (xpath:map-node-set #'copy-into-result (xpath:sort-node-set result)))
279 (result-tree-fragment
280 (copy-into-result result))
282 (write-text (xpath:string-value result))))))
283 "copy-of ~s" xpath))))
285 (defun copy-into-result (node)
286 (cond
287 ((result-tree-fragment-p node)
288 (stp:do-children (child (result-tree-fragment-node node))
289 (copy-into-result child)))
290 ((xpath-protocol:node-type-p node :element)
291 (with-element ((xpath-protocol:local-name node)
292 (xpath-protocol:namespace-uri node)
293 :suggested-prefix (xpath-protocol:namespace-prefix node)
294 ;; FIXME: is remove-excluded-namespaces correct here?
295 :extra-namespaces (remove-excluded-namespaces
296 (namespaces-as-alist node)))
297 (map-pipe-eagerly #'copy-into-result
298 (xpath-protocol:attribute-pipe node))
299 (map-pipe-eagerly #'copy-into-result
300 (xpath-protocol:child-pipe node))))
301 ((xpath-protocol:node-type-p node :document)
302 (map-pipe-eagerly #'copy-into-result
303 (xpath-protocol:child-pipe node)))
305 (copy-leaf-node node))))
307 (defun make-sorter (spec env)
308 (destructuring-bind (&key select lang data-type order case-order)
309 (cdr spec)
310 ;; FIXME: implement case-order
311 (declare (ignore lang case-order))
312 (let ((select-thunk (compile-xpath (or select ".") env))
313 (numberp (equal data-type "number"))
314 (f (if (equal order "descending") -1 1)))
315 (lambda (a b)
316 (let ((i (xpath:string-value
317 (funcall select-thunk (xpath:make-context a))))
318 (j (xpath:string-value
319 (funcall select-thunk (xpath:make-context b)))))
320 (* f
321 (if numberp
322 (let ((n-a (xpath:number-value i))
323 (n-b (xpath:number-value j)))
324 (cond ((and (xpath::nan-p a)
325 (not (xpath::nan-p b)))
327 ((and (not (xpath::nan-p a))
328 (xpath::nan-p b))
330 ((xpath::compare-numbers '< n-a n-b) -1)
331 ((xpath::compare-numbers '> n-a n-b) 1)
332 (t 0)))
333 (cond
334 ((string< i j) -1)
335 ((equal i j) 0)
336 (t 1)))))))))
338 (defun compose-sorters (sorters)
339 (if sorters
340 (let ((this (car sorters))
341 (next (compose-sorters (rest sorters))))
342 (lambda (a b)
343 (let ((d (funcall this a b)))
344 (if (zerop d)
345 (funcall next a b)
346 d))))
347 (constantly 0)))
349 (defun make-sort-predicate (decls env)
350 (let ((sorter
351 (compose-sorters
352 (mapcar (lambda (x) (make-sorter x env)) decls))))
353 (lambda (a b)
354 (minusp (funcall sorter a b)))))
356 (define-instruction xsl:for-each (args env)
357 (destructuring-bind (select &optional decls &rest body) args
358 (unless (and (consp decls)
359 (eq (car decls) 'declare))
360 (push decls body)
361 (setf decls nil))
362 (let ((select-thunk (compile-xpath select env))
363 (body-thunk (compile-instruction `(progn ,@body) env))
364 (sort-predicate
365 (when (cdr decls)
366 (make-sort-predicate (cdr decls) env))))
367 (lambda (ctx)
368 (let ((selected (funcall select-thunk ctx)))
369 (unless (xpath:node-set-p selected)
370 (xslt-error "for-each select expression should yield a node-set"))
371 (let ((nodes (xpath::force
372 (xpath::sorted-pipe-of selected))))
373 (when sort-predicate
374 (setf nodes (sort (copy-list nodes) sort-predicate)))
375 (loop
376 with n = (length nodes)
377 for node in nodes
378 for i from 1
380 (funcall body-thunk
381 (xpath:make-context node (lambda () n) i)))))))))
383 (define-instruction xsl:with-namespaces (args env)
384 (destructuring-bind ((&rest forms) &rest body) args
385 (let ((*namespaces* *namespaces*))
386 (dolist (form forms)
387 (destructuring-bind (prefix uri) form
388 (push (cons prefix uri) *namespaces*)))
389 (compile-instruction `(progn ,@body) env))))
391 (define-instruction xsl:with-excluded-namespaces (args env)
392 (destructuring-bind ((&rest uris) &rest body) args
393 (let ((*excluded-namespaces* (append uris *excluded-namespaces*)))
394 (compile-instruction `(progn ,@body) env))))
396 ;; XSLT disallows multiple definitions of the same variable within a
397 ;; template. Local variables can shadow global variables though.
398 ;; Since our LET syntax makes it natural to shadow local variables the
399 ;; Lisp way, we check for duplicate variables only where instructed to
400 ;; by the XML syntax parser using WITH-DUPLICATES-CHECK:
401 (defvar *template-variables* nil)
403 (define-instruction xsl:with-duplicates-check (args env)
404 (let ((*template-variables* *template-variables*))
405 (destructuring-bind ((&rest qnames) &rest body) args
406 (dolist (qname qnames)
407 (multiple-value-bind (local-name uri)
408 (decode-qname qname env nil)
409 (let ((key (cons local-name uri)))
410 (when (find key *template-variables* :test #'equal)
411 (xslt-error "duplicate variable: ~A, ~A" local-name uri))
412 (push key *template-variables*))))
413 (compile-instruction `(progn ,@body) env))))
415 (define-instruction xsl:with-base-uri (args env)
416 (destructuring-bind (uri &rest body) args
417 (let ((*instruction-base-uri* uri))
418 (compile-instruction `(progn ,@body) env))))
420 (defstruct (result-tree-fragment
421 (:constructor make-result-tree-fragment (node)))
422 node)
424 (define-default-method xpath-protocol:node-p
425 ((node result-tree-fragment))
428 (define-default-method xpath-protocol:node-text
429 ((node result-tree-fragment))
430 (xpath-protocol:node-text (result-tree-fragment-node node)))
432 (defun apply-to-result-tree-fragment (ctx thunk)
433 (let ((document
434 (with-xml-output (stp:make-builder)
435 (with-element ("fragment" "")
436 (funcall thunk ctx)))))
437 (make-result-tree-fragment (stp:document-element document))))
439 (define-instruction let (args env)
440 (destructuring-bind ((&rest forms) &rest body) args
441 (let* ((old-top (length *lexical-variable-declarations*))
442 (vars-and-names (compile-var-bindings/nointern forms env))
443 (vars-and-positions
444 (loop for ((local-name . uri) thunk) in vars-and-names
445 collect
446 (list (push-variable local-name
448 *lexical-variable-declarations*)
449 thunk))))
450 (let ((thunk (compile-instruction `(progn ,@body) env)))
451 (fill *lexical-variable-declarations* nil :start old-top)
452 (lambda (ctx)
453 (loop for (index var-thunk) in vars-and-positions
454 do (setf (lexical-variable-value index)
455 (funcall var-thunk ctx)))
456 (funcall thunk ctx))))))
458 (define-instruction let* (args env)
459 (destructuring-bind ((&rest forms) &rest body) args
460 (if forms
461 (compile-instruction `(let (,(car forms))
462 (let* (,@(cdr forms))
463 ,@body))
464 env)
465 (compile-instruction `(progn ,@body) env))))
467 (define-instruction xsl:message (args env)
468 (compile-message #'warn args env))
470 (define-instruction xsl:terminate (args env)
471 (compile-message #'error args env))
473 (defun namespaces-as-alist (element)
474 (let ((namespaces '()))
475 (do-pipe (ns (xpath-protocol:namespace-pipe element))
476 (push (cons (xpath-protocol:local-name ns)
477 (xpath-protocol:namespace-uri ns))
478 namespaces))
479 namespaces))
481 (define-instruction xsl:copy (args env)
482 (let ((body (compile-instruction `(progn ,@args) env)))
483 (lambda (ctx)
484 (let ((node (xpath:context-node ctx)))
485 (cond
486 ((xpath-protocol:node-type-p node :element)
487 (with-element
488 ((xpath-protocol:local-name node)
489 (xpath-protocol:namespace-uri node)
490 :suggested-prefix (xpath-protocol:namespace-prefix node)
491 :extra-namespaces (namespaces-as-alist node))
492 (funcall body ctx)))
493 ((xpath-protocol:node-type-p node :document)
494 (funcall body ctx))
496 (copy-leaf-node node)))))))
498 (defun copy-leaf-node (node)
499 (cond
500 ((xpath-protocol:node-type-p node :text)
501 (write-text (xpath-protocol:node-text node)))
502 ((xpath-protocol:node-type-p node :comment)
503 (write-comment (xpath-protocol:node-text node)))
504 ((xpath-protocol:node-type-p node :processing-instruction)
505 (write-processing-instruction
506 (xpath-protocol:processing-instruction-target node)
507 (xpath-protocol:node-text node)))
508 ((xpath-protocol:node-type-p node :attribute)
509 (write-attribute
510 (xpath-protocol:local-name node)
511 (xpath-protocol:namespace-uri node)
512 (xpath-protocol:node-text node)
513 :suggested-prefix (xpath-protocol:namespace-prefix node)))
515 (error "don't know how to copy node ~A" node))))
517 (defun compile-message (fn args env)
518 (let ((thunk (compile-instruction `(progn ,@args) env)))
519 (lambda (ctx)
520 (funcall fn
521 (with-xml-output (cxml:make-string-sink)
522 (funcall thunk ctx))))))
524 (define-instruction xsl:apply-templates (args env)
525 (destructuring-bind ((&key select mode) &rest param-binding-specs) args
526 (let* ((decls
527 (when (and (consp (car param-binding-specs))
528 (eq (caar param-binding-specs) 'declare))
529 (cdr (pop param-binding-specs))))
530 (select-thunk
531 (compile-xpath (or select "child::node()") env))
532 (param-bindings
533 (compile-var-bindings param-binding-specs env))
534 (sort-predicate
535 (when decls
536 (make-sort-predicate decls env))))
537 (multiple-value-bind (mode-local-name mode-uri)
538 (and mode (decode-qname mode env nil))
539 (lambda (ctx)
540 (let ((*mode* (if mode
541 (or (find-mode *stylesheet*
542 mode-local-name
543 mode-uri)
544 *empty-mode*)
545 *mode*)))
546 (apply-templates/list
547 (xpath::force
548 (xpath::sorted-pipe-of (funcall select-thunk ctx)))
549 (loop for (name nil value-thunk) in param-bindings
550 collect (list name (funcall value-thunk ctx)))
551 sort-predicate)))))))
553 (define-instruction xsl:apply-imports (args env)
554 (declare (ignore args env))
555 (lambda (ctx)
556 (declare (ignore ctx))
557 (funcall *apply-imports*)))
559 (define-instruction xsl:call-template (args env)
560 (destructuring-bind (name &rest param-binding-specs) args
561 (let ((param-bindings
562 (compile-var-bindings param-binding-specs env)))
563 (multiple-value-bind (local-name uri)
564 (decode-qname name env nil)
565 (setf name (cons local-name uri)))
566 (lambda (ctx)
567 (call-template ctx name
568 (loop for (name nil value-thunk) in param-bindings
569 collect (list name (funcall value-thunk ctx))))))))
571 ;; fixme: incompatible with XSLT 2.0
572 (define-instruction xsl:document (args env)
573 (destructuring-bind ((href &key method indent doctype-public doctype-system)
574 &body body)
575 args
576 (declare (ignore doctype-public doctype-system)) ;fixme
577 (let ((thunk (compile-instruction `(progn ,@body) env))
578 (href-thunk (compile-avt href env)))
579 (lambda (ctx)
580 (let ((pathname
581 (uri-to-pathname
582 (puri:merge-uris (funcall href-thunk ctx)
583 (xpath-protocol:base-uri
584 (xpath:context-node ctx))))))
585 (ensure-directories-exist pathname) ;really?
586 (invoke-with-output-sink
587 (lambda ()
588 (funcall thunk ctx))
589 (make-output-specification :method (or method "XML") :indent indent)
590 pathname))))))
592 (defun compile-instruction (form env)
593 (xslt-trace-thunk
594 (funcall (or (get (car form) 'xslt-instruction)
595 (error "undefined instruction: ~A" (car form)))
596 (cdr form)
597 env)
598 "instruction ~s" (car form)))
600 ;;: WTF: "A right curly brace inside a Literal in an expression is not
601 ;;; recognized as terminating the expression."
603 ;;; Da hilft nur tagbody.
604 (defun parse-attribute-value-template (template-string)
605 (with-input-from-string (input template-string)
606 (let ((ordinary (make-string-output-stream))
607 (xpath (make-string-output-stream))
608 (tokens '())
609 (c (read-char input nil :eof)))
610 (flet ((emit ()
611 (let ((o (get-output-stream-string ordinary)))
612 (when (plusp (length o))
613 (push (list :data o) tokens)))
614 (let ((x (get-output-stream-string xpath)))
615 (when (plusp (length x))
616 (push (list :xpath x) tokens))))
617 (collect-ordinary ()
618 (write-char c ordinary))
619 (collect-xpath ()
620 (write-char c xpath)))
621 (macrolet ((goto (target)
622 `(progn
623 (setf c (read-char input nil :eof))
624 (go ,target))))
625 (tagbody
626 ordinary
627 (case c
628 (#\{
629 (goto seen{))
630 (#\}
631 (goto seen-stray-}))
632 (:eof
633 (go done)))
634 (collect-ordinary)
635 (goto ordinary)
637 seen{
638 (case c
639 (#\{
640 (collect-ordinary)
641 (goto ordinary))
642 (#\'
643 (collect-xpath)
644 (goto in-single-quote))
645 (:eof
646 (xslt-error "unexpected end of avt")))
647 (emit)
648 (collect-xpath)
649 (goto xpath)
651 xpath
652 (case c
653 (#\'
654 (collect-xpath)
655 (goto in-single-quote))
656 (#\"
657 (collect-xpath)
658 (goto in-double-quote))
659 (#\}
660 (goto seen-closing-}))
661 (:eof
662 (xslt-error "unexpected end of avt")))
663 (collect-xpath)
664 (goto xpath)
666 in-single-quote
667 (case c
668 (#\'
669 (collect-xpath)
670 (goto xpath))
671 (:eof
672 (xslt-error "unexpected end of avt")))
673 (collect-xpath)
674 (goto in-single-quote)
676 in-double-quote
677 (case c
678 (#\"
679 (collect-xpath)
680 (goto xpath))
681 (:eof
682 (xslt-error "unexpected end of avt")))
683 (collect-xpath)
684 (goto in-double-quote)
686 seen-closing-}
687 (case c
688 (#\}
689 (collect-xpath)
690 (goto xpath))
691 (#\{
692 (emit)
693 (goto xpath))
694 (:eof
695 (goto done)))
696 (emit)
697 (collect-ordinary)
698 (goto ordinary)
700 seen-stray-}
701 (case c
702 (#\}
703 (collect-ordinary)
704 (goto ordinary)))
705 (xslt-error "unexpected closing brace in avt")
707 done
708 (emit))))
709 (nreverse tokens))))
711 (defun compile-avt (template-string env)
712 (let* ((constantp t)
713 (fns
714 (mapcar (lambda (x)
715 (ecase (car x)
716 (:data
717 (constantly (second x)))
718 (:xpath
719 (setf constantp nil)
720 (compile-xpath (second x) env))))
721 (parse-attribute-value-template template-string))))
722 (values (lambda (ctx)
723 (with-output-to-string (s)
724 (dolist (fn fns)
725 (write-string (xpath:string-value (funcall fn ctx)) s))))
726 constantp)))
729 ;;;; Indentation for slime
731 (defmacro define-indentation (name (&rest args))
732 (labels ((collect-variables (list)
733 (loop
734 for sub in list
735 append
736 (etypecase sub
737 (list
738 (collect-variables sub))
739 (symbol
740 (if (eql (mismatch "&" (symbol-name sub)) 1)
742 (list sub)))))))
743 `(defmacro ,name (,@args)
744 (declare (ignorable ,@(collect-variables args)))
745 (error "XSL indentation helper ~A used literally in lisp code"
746 ',name))))
748 (define-indentation xsl:element
749 ((name &key namespace use-attribute-sets) &body body))
750 (define-indentation xsl:literal-element ((name &optional uri) &body body))
751 (define-indentation xsl:attribute ((name &key namespace) &body body))
752 (define-indentation xsl:literal-attribute ((name &optional uri) &body body))
753 (define-indentation xsl:text (str))
754 (define-indentation xsl:processing-instruction (name &body body))
755 (define-indentation xsl:comment (&body body))
756 (define-indentation xsl:value-of (xpath))
757 (define-indentation xsl:unescaped-value-of (xpath))
758 (define-indentation xsl:for-each (select &body decls-and-body))
759 (define-indentation xsl:message (&body body))
760 (define-indentation xsl:terminate (&body body))
761 (define-indentation xsl:apply-templates ((&key select mode) &body decls-and-body))
762 (define-indentation xsl:call-template (name &rest parameters))
763 (define-indentation xsl:copy-of (xpath))
765 ;;;;
767 (defun test-instruction (form document)
768 (let ((thunk (compile-instruction form (make-instance 'lexical-environment)))
769 (root (cxml:parse document (stp:make-builder))))
770 (with-xml-output (cxml:make-string-sink)
771 (funcall thunk (xpath:make-context root)))))