updated TEST for current Plexippus
[xuriella.git] / instructions.lisp
blob008fe6e66c87d3b3f18ab10ece87973d871e843d
1 ;;; -*- show-trailing-whitespace: t; indent-tabs: 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 (declaim (optimize (debug 3) (safety 3) (space 0) (speed 0)))
33 ;;;; Instructions
35 (defmacro define-instruction (name (args-var env-var) &body body)
36 `(setf (get ',name 'xslt-instruction)
37 (lambda (,args-var ,env-var)
38 (declare (ignorable ,env-var))
39 ,@body)))
41 (define-instruction if (args env)
42 (destructuring-bind (test then &optional else) args
43 (let ((test-thunk (compile-xpath test env))
44 (then-thunk (compile-instruction then env))
45 (else-thunk (when else (compile-instruction else env))))
46 (lambda (ctx)
47 (cond
48 ((xpath:boolean-value (funcall test-thunk ctx))
49 (funcall then-thunk ctx))
50 (else-thunk
51 (funcall else-thunk ctx)))))))
53 (define-instruction when (args env)
54 (destructuring-bind (test &rest body) args
55 (compile-instruction `(if ,test (progn ,@body)) env)))
57 (define-instruction unless (args env)
58 (destructuring-bind (test &rest body) args
59 (compile-instruction `(if (:not ,test) (progn ,@body)) env)))
61 (define-instruction cond (args env)
62 (if args
63 (destructuring-bind ((test &body body) &rest clauses) args
64 (compile-instruction (if (eq test t)
65 `(progn ,@body)
66 `(if ,test
67 (progn ,@body)
68 (cond ,@clauses)))
69 env))
70 (constantly nil)))
72 (define-instruction progn (args env)
73 (if args
74 (let ((first-thunk (compile-instruction (first args) env))
75 (rest-thunk (compile-instruction `(progn ,@(rest args)) env)))
76 (lambda (ctx)
77 (funcall first-thunk ctx)
78 (funcall rest-thunk ctx)))
79 (constantly nil)))
81 (defun decode-qname/runtime (qname namespaces attributep)
82 (handler-case
83 (multiple-value-bind (prefix local-name)
84 (split-qname qname)
85 (values local-name
86 (if (or prefix (not attributep))
87 (cdr (assoc prefix namespaces :test 'equal))
88 "")
89 prefix))
90 (cxml:well-formedness-violation ()
91 (xslt-error "not a qname: ~A" qname))))
93 (define-instruction xsl:element (args env)
94 (destructuring-bind ((name &key namespace use-attribute-sets)
95 &body body)
96 args
97 (declare (ignore use-attribute-sets)) ;fixme
98 (multiple-value-bind (name-thunk constant-name-p)
99 (compile-attribute-value-template name env)
100 (multiple-value-bind (ns-thunk constant-ns-p)
101 (if namespace
102 (compile-attribute-value-template namespace env)
103 (values nil t))
104 (let ((body-thunk (compile-instruction `(progn ,@body) env)))
105 (if (and constant-name-p constant-ns-p)
106 (compile-element/constant-name name namespace env body-thunk)
107 (compile-element/runtime name-thunk ns-thunk body-thunk)))))))
109 (defun compile-element/constant-name (qname namespace env body-thunk)
110 ;; the simple case: compile-time decoding of the QName
111 (multiple-value-bind (local-name uri prefix)
112 (decode-qname qname env nil)
113 (when namespace
114 (setf uri namespace))
115 (lambda (ctx)
116 (with-element (local-name uri :suggested-prefix prefix)
117 (funcall body-thunk ctx)))))
119 (defun compile-element/runtime (name-thunk ns-thunk body-thunk)
120 ;; run-time decoding of the QName, but using the same namespaces
121 ;; that would have been known at compilation time.
122 (let ((namespaces *namespaces*))
123 (lambda (ctx)
124 (let ((qname (funcall name-thunk ctx)))
125 (multiple-value-bind (local-name uri prefix)
126 (decode-qname/runtime qname namespaces nil)
127 (when ns-thunk
128 (setf uri (funcall ns-thunk ctx)))
129 (unless uri
130 (setf uri ""))
131 (lambda (ctx)
132 (with-element (local-name uri :suggested-prefix prefix)
133 (funcall body-thunk ctx))))))))
135 (define-instruction xsl:use-attribute-sets (args env)
136 (destructuring-bind (str) args
137 (let ((sets (mapcar (lambda (qname)
138 (multiple-value-list (decode-qname qname env nil)))
139 (words str))))
140 (lambda (ctx)
141 (loop for (local-name uri nil) in sets do
142 (dolist (thunk (find-attribute-set local-name uri))
143 (funcall thunk ctx)))))))
145 (define-instruction xsl:attribute (args env)
146 (destructuring-bind ((name &key namespace) &body body) args
147 (multiple-value-bind (name-thunk constant-name-p)
148 (compile-attribute-value-template name env)
149 (multiple-value-bind (ns-thunk constant-ns-p)
150 (if namespace
151 (compile-attribute-value-template namespace env)
152 (values nil t))
153 (let ((value-thunk (compile-instruction `(progn ,@body) env)))
154 (if (and constant-name-p constant-ns-p)
155 (compile-attribute/constant-name name namespace env value-thunk)
156 (compile-attribute/runtime name-thunk ns-thunk value-thunk)))))))
158 (defun compile-attribute/constant-name (qname namespace env value-thunk)
159 ;; the simple case: compile-time decoding of the QName
160 (multiple-value-bind (local-name uri prefix)
161 (decode-qname qname env nil)
162 (when namespace
163 (setf uri namespace))
164 (lambda (ctx)
165 (write-attribute local-name
167 (with-text-output-sink (s)
168 (with-xml-output s
169 (funcall value-thunk ctx)))
170 :suggested-prefix prefix))))
172 (defun compile-attribute/runtime (name-thunk ns-thunk value-thunk)
173 ;; run-time decoding of the QName, but using the same namespaces
174 ;; that would have been known at compilation time.
175 (let ((namespaces *namespaces*))
176 (lambda (ctx)
177 (let ((qname (funcall name-thunk ctx)))
178 (multiple-value-bind (local-name uri prefix)
179 (decode-qname/runtime qname namespaces nil)
180 (when ns-thunk
181 (setf uri (funcall ns-thunk ctx)))
182 (write-attribute local-name
183 (or uri "")
184 (with-text-output-sink (s)
185 (with-xml-output s
186 (funcall value-thunk ctx)))
187 :suggested-prefix prefix))))))
189 (defun remove-excluded-namespaces
190 (namespaces &optional (excluded-uris *excluded-namespaces*))
191 (let ((koerbchen '())
192 (kroepfchen '()))
193 (loop
194 for cons in namespaces
195 for (prefix . uri) = cons
197 (cond
198 ((find prefix kroepfchen :test #'equal))
199 ((find uri excluded-uris :test #'equal)
200 (push prefix kroepfchen))
202 (push cons koerbchen))))
203 koerbchen))
205 (define-instruction xsl:literal-element (args env)
206 (destructuring-bind
207 ((local-name &optional (uri "") suggested-prefix) &body body)
208 args
209 (let ((body-thunk (compile-instruction `(progn ,@body) env))
210 (namespaces (remove-excluded-namespaces *namespaces*)))
211 (lambda (ctx)
212 (with-element (local-name uri
213 :suggested-prefix suggested-prefix
214 :extra-namespaces namespaces)
215 (funcall body-thunk ctx))))))
217 (define-instruction xsl:literal-attribute (args env)
218 (destructuring-bind ((local-name &optional uri suggested-prefix) value) args
219 (let ((value-thunk (compile-attribute-value-template value env)))
220 (lambda (ctx)
221 (write-attribute local-name
223 (funcall value-thunk ctx)
224 :suggested-prefix suggested-prefix)))))
226 (define-instruction xsl:text (args env)
227 (destructuring-bind (str) args
228 (lambda (ctx)
229 (declare (ignore ctx))
230 (write-text str))))
232 (define-instruction xsl:processing-instruction (args env)
233 (destructuring-bind (name &rest body) args
234 (let ((name-thunk (compile-attribute-value-template name env))
235 (value-thunk (compile-instruction `(progn ,@body) env)))
236 (lambda (ctx)
237 (write-processing-instruction
238 (funcall name-thunk ctx)
239 (with-text-output-sink (s)
240 (with-xml-output s
241 (funcall value-thunk ctx))))))))
243 (define-instruction xsl:comment (args env)
244 (destructuring-bind (str) args
245 (lambda (ctx)
246 (declare (ignore ctx))
247 (write-comment str))))
249 (define-instruction xsl:value-of (args env)
250 (destructuring-bind (xpath) args
251 (let ((thunk (compile-xpath xpath env)))
252 (lambda (ctx)
253 (write-text (xpath:string-value (funcall thunk ctx)))))))
255 (define-instruction xsl:unescaped-value-of (args env)
256 (destructuring-bind (xpath) args
257 (let ((thunk (compile-xpath xpath env)))
258 (lambda (ctx)
259 (write-unescaped (xpath:string-value (funcall thunk ctx)))))))
261 (define-instruction xsl:copy-of (args env)
262 (destructuring-bind (xpath) args
263 (let ((thunk (compile-xpath xpath env))
264 ;; FIXME: what was this for? --david
265 #+(or) (v (intern-variable "varName" "")))
266 (lambda (ctx)
267 (let ((result (funcall thunk ctx)))
268 (typecase result
269 (xpath:node-set ;; FIXME: variables can contain node sets w/fragments inside. Maybe just fragments would do?
270 (xpath:map-node-set #'copy-into-result result))
271 (result-tree-fragment
272 (copy-into-result result))
274 (write-text (xpath:string-value result)))))))))
276 (defun copy-into-result (node)
277 (cond
278 ((result-tree-fragment-p node)
279 (stp:do-children (child (result-tree-fragment-node node))
280 (copy-into-result child)))
281 ((xpath-protocol:node-type-p node :element)
282 (with-element ((xpath-protocol:local-name node)
283 (xpath-protocol:namespace-uri node)
284 :suggested-prefix (xpath-protocol:namespace-prefix node)
285 ;; FIXME: is remove-excluded-namespaces correct here?
286 :extra-namespaces (remove-excluded-namespaces
287 (namespaces-as-alist node)))
288 (map-pipe-eagerly #'copy-into-result
289 (xpath-protocol:attribute-pipe node))
290 (map-pipe-eagerly #'copy-into-result
291 (xpath-protocol:child-pipe node))))
292 ((xpath-protocol:node-type-p node :document)
293 (map-pipe-eagerly #'copy-into-result
294 (xpath-protocol:child-pipe node)))
296 (copy-leaf-node node))))
298 (defun make-sorter (spec env)
299 (destructuring-bind (&key select lang data-type order case-order)
300 (cdr spec)
301 ;; FIXME: implement case-order
302 (declare (ignore lang case-order))
303 (let ((select-thunk (compile-xpath (or select ".") env))
304 (numberp (equal data-type "number"))
305 (f (if (equal order "descending") -1 1)))
306 (lambda (a b)
307 (let ((i (xpath:string-value
308 (funcall select-thunk (xpath:make-context a))))
309 (j (xpath:string-value
310 (funcall select-thunk (xpath:make-context b)))))
311 (* f
312 (if numberp
313 (signum (- (xpath:number-value i) (xpath:number-value j)))
314 (cond
315 ((string< i j) -1)
316 ((equal i j) 0)
317 (t 1)))))))))
319 (defun compose-sorters (sorters)
320 (if sorters
321 (let ((this (car sorters))
322 (next (compose-sorters (rest sorters))))
323 (lambda (a b)
324 (let ((d (funcall this a b)))
325 (if (zerop d)
326 (funcall next a b)
327 d))))
328 (constantly 0)))
330 (defun make-sort-predicate (decls env)
331 (let ((sorter
332 (compose-sorters
333 (mapcar (lambda (x) (make-sorter x env)) decls))))
334 (lambda (a b)
335 (minusp (funcall sorter a b)))))
337 (define-instruction xsl:for-each (args env)
338 (destructuring-bind (select &optional decls &rest body) args
339 (unless (and (consp decls)
340 (eq (car decls) 'declare))
341 (push decls body)
342 (setf decls nil))
343 (let ((select-thunk (compile-xpath select env))
344 (body-thunk (compile-instruction `(progn ,@body) env))
345 (sort-predicate
346 (when (cdr decls)
347 (make-sort-predicate (cdr decls) env))))
348 (lambda (ctx)
349 (let* ((nodes (xpath:all-nodes (funcall select-thunk ctx)))
350 (n (length nodes)))
351 (if sort-predicate
352 (setf nodes (sort nodes sort-predicate))
353 (setf nodes (sort nodes #'xpath::node<)))
354 (loop
355 for node in nodes
356 for i from 1
358 (funcall body-thunk
359 (xpath:make-context node (lambda () n) i))))))))
361 (define-instruction xsl:with-namespaces (args env)
362 (destructuring-bind ((&rest forms) &rest body) args
363 (let ((*namespaces* *namespaces*))
364 (dolist (form forms)
365 (destructuring-bind (prefix uri) form
366 (push (cons prefix uri) *namespaces*)))
367 (compile-instruction `(progn ,@body) env))))
369 (define-instruction xsl:with-excluded-namespaces (args env)
370 (destructuring-bind ((&rest uris) &rest body) args
371 (let ((*excluded-namespaces* (append uris *excluded-namespaces*)))
372 (compile-instruction `(progn ,@body) env))))
374 ;; XSLT disallows multiple definitions of the same variable within a
375 ;; template. Local variables can shadow global variables though.
376 ;; Since our LET syntax makes it natural to shadow local variables the
377 ;; Lisp way, we check for duplicate variables only where instructed to
378 ;; by the XML syntax parser using WITH-DUPLICATES-CHECK:
379 (defvar *template-variables* nil)
381 (define-instruction xsl:with-duplicates-check (args env)
382 (let ((*template-variables* *template-variables*))
383 (destructuring-bind ((&rest qnames) &rest body) args
384 (dolist (qname qnames)
385 (multiple-value-bind (local-name uri)
386 (decode-qname qname env nil)
387 (let ((key (cons local-name uri)))
388 (when (find key *template-variables* :test #'equal)
389 (xslt-error "duplicate variable: ~A, ~A" local-name uri))
390 (push key *template-variables*))))
391 (compile-instruction `(progn ,@body) env))))
393 (define-instruction xsl:with-base-uri (args env)
394 (destructuring-bind (uri &rest body) args
395 (let ((*instruction-base-uri* uri))
396 (compile-instruction `(progn ,@body) env))))
398 (defstruct (result-tree-fragment
399 (:constructor make-result-tree-fragment (node)))
400 node)
402 (defmethod xpath-protocol:node-p ((node result-tree-fragment))
405 (defmethod xpath-protocol:string-value ((node result-tree-fragment))
406 (xpath-protocol:string-value (result-tree-fragment-node node)))
408 (defun apply-to-result-tree-fragment (ctx thunk)
409 (let ((document
410 (with-xml-output (stp:make-builder)
411 (with-element ("fragment" "")
412 (funcall thunk ctx)))))
413 (make-result-tree-fragment (stp:document-element document))))
415 (define-instruction let (args env)
416 (destructuring-bind ((&rest forms) &rest body) args
417 (let* ((old-top (length *lexical-variable-declarations*))
418 (vars-and-names (compile-var-bindings/nointern forms env))
419 (vars-and-positions
420 (loop for ((local-name . uri) thunk) in vars-and-names
421 collect
422 (list (push-variable local-name
424 *lexical-variable-declarations*)
425 thunk))))
426 (let ((thunk (compile-instruction `(progn ,@body) env)))
427 (fill *lexical-variable-declarations* nil :start old-top)
428 (lambda (ctx)
429 (loop for (index var-thunk) in vars-and-positions
430 do (setf (lexical-variable-value index)
431 (funcall var-thunk ctx)))
432 (funcall thunk ctx))))))
434 (define-instruction let* (args env)
435 (destructuring-bind ((&rest forms) &rest body) args
436 (if forms
437 (compile-instruction `(let (,(car forms))
438 (let* (,@(cdr forms))
439 ,@body))
440 env)
441 (compile-instruction `(progn ,@body) env))))
443 (define-instruction xsl:message (args env)
444 (compile-message #'warn args env))
446 (define-instruction xsl:terminate (args env)
447 (compile-message #'error args env))
449 (defun namespaces-as-alist (element)
450 (let ((namespaces '()))
451 (do-pipe (ns (xpath-protocol:namespace-pipe element))
452 (push (cons (xpath-protocol:local-name ns)
453 (xpath-protocol:namespace-uri ns))
454 namespaces))
455 namespaces))
457 (define-instruction xsl:copy (args env)
458 (let ((body (compile-instruction `(progn ,@args) env)))
459 (lambda (ctx)
460 (let ((node (xpath:context-node ctx)))
461 (cond
462 ((xpath-protocol:node-type-p node :element)
463 (with-element
464 ((xpath-protocol:local-name node)
465 (xpath-protocol:namespace-uri node)
466 :suggested-prefix (xpath-protocol:namespace-prefix node)
467 :extra-namespaces (namespaces-as-alist node))
468 (funcall body ctx)))
469 ((xpath-protocol:node-type-p node :document)
470 (funcall body ctx))
472 (copy-leaf-node node)))))))
474 (defun copy-leaf-node (node)
475 (cond
476 ((xpath-protocol:node-type-p node :text)
477 (write-text (xpath-protocol:string-value node)))
478 ((xpath-protocol:node-type-p node :comment)
479 (write-comment (xpath-protocol:string-value node)))
480 ((xpath-protocol:node-type-p node :processing-instruction)
481 (write-processing-instruction
482 (xpath-protocol:processing-instruction-target node)
483 (xpath-protocol:string-value node)))
484 ((xpath-protocol:node-type-p node :attribute)
485 (write-attribute
486 (xpath-protocol:local-name node)
487 (xpath-protocol:namespace-uri node)
488 (xpath-protocol:string-value node)
489 :suggested-prefix (xpath-protocol:namespace-prefix node)))
491 (error "don't know how to copy node ~A" node))))
493 (defun compile-message (fn args env)
494 (let ((thunk (compile-instruction `(progn ,@args) env)))
495 (lambda (ctx)
496 (funcall fn
497 (with-xml-output (cxml:make-string-sink)
498 (funcall thunk ctx))))))
500 (define-instruction xsl:apply-templates (args env)
501 (destructuring-bind ((&key select mode) &rest param-binding-specs) args
502 (let* ((decls
503 (when (and (consp (car param-binding-specs))
504 (eq (caar param-binding-specs) 'declare))
505 (cdr (pop param-binding-specs))))
506 (select-thunk
507 (compile-xpath (or select "child::node()") env))
508 (param-bindings
509 (compile-var-bindings param-binding-specs env))
510 (sort-predicate
511 (when decls
512 (make-sort-predicate decls env))))
513 (multiple-value-bind (mode-local-name mode-uri)
514 (and mode (decode-qname mode env nil))
515 (lambda (ctx)
516 (let ((*mode* (if mode
517 (or (find-mode *stylesheet*
518 mode-local-name
519 mode-uri)
520 *empty-mode*)
521 *mode*))
522 (nodes (xpath:all-nodes (funcall select-thunk ctx))))
523 (setf nodes (sort nodes #'xpath::node<))
524 (apply-templates/list
525 nodes
526 (loop for (name nil value-thunk) in param-bindings
527 collect (list name (funcall value-thunk ctx)))
528 sort-predicate)))))))
530 (define-instruction xsl:apply-imports (args env)
531 (lambda (ctx)
532 (declare (ignore ctx))
533 (funcall *apply-imports*)))
535 (define-instruction xsl:call-template (args env)
536 (destructuring-bind (name &rest param-binding-specs) args
537 (let ((param-bindings
538 (compile-var-bindings param-binding-specs env)))
539 (multiple-value-bind (local-name uri)
540 (decode-qname name env nil)
541 (setf name (cons local-name uri)))
542 (lambda (ctx)
543 (call-template ctx name
544 (loop for (name nil value-thunk) in param-bindings
545 collect (list name (funcall value-thunk ctx))))))))
547 (defun compile-instruction (form env)
548 (funcall (or (get (car form) 'xslt-instruction)
549 (error "undefined instruction: ~A" (car form)))
550 (cdr form)
551 env))
553 ;;: WTF: "A right curly brace inside a Literal in an expression is not
554 ;;; recognized as terminating the expression."
556 ;;; Da hilft nur tagbody.
557 (defun parse-attribute-value-template (template-string)
558 (with-input-from-string (input template-string)
559 (let ((ordinary (make-string-output-stream))
560 (xpath (make-string-output-stream))
561 (tokens '())
562 (c (read-char input nil :eof)))
563 (flet ((emit ()
564 (let ((o (get-output-stream-string ordinary)))
565 (when (plusp (length o))
566 (push (list :data o) tokens)))
567 (let ((x (get-output-stream-string xpath)))
568 (when (plusp (length x))
569 (push (list :xpath x) tokens))))
570 (collect-ordinary ()
571 (write-char c ordinary))
572 (collect-xpath ()
573 (write-char c xpath)))
574 (macrolet ((goto (target)
575 `(progn
576 (setf c (read-char input nil :eof))
577 (go ,target))))
578 (tagbody
579 ordinary
580 (case c
581 (#\{
582 (goto seen{))
583 (#\}
584 (goto seen-stray-}))
585 (:eof
586 (go done)))
587 (collect-ordinary)
588 (goto ordinary)
590 seen{
591 (case c
592 (#\{
593 (collect-ordinary)
594 (goto ordinary))
595 (#\'
596 (collect-xpath)
597 (goto in-single-quote))
598 (:eof
599 (xslt-error "unexpected end of avt")))
600 (emit)
601 (collect-xpath)
602 (goto xpath)
604 xpath
605 (case c
606 (#\'
607 (collect-xpath)
608 (goto in-single-quote))
609 (#\"
610 (collect-xpath)
611 (goto in-double-quote))
612 (#\}
613 (goto seen-closing-}))
614 (:eof
615 (xslt-error "unexpected end of avt")))
616 (collect-xpath)
617 (goto xpath)
619 in-single-quote
620 (case c
621 (#\'
622 (collect-xpath)
623 (goto xpath))
624 (:eof
625 (xslt-error "unexpected end of avt")))
626 (collect-xpath)
627 (goto in-single-quote)
629 in-double-quote
630 (case c
631 (#\"
632 (collect-xpath)
633 (goto xpath))
634 (:eof
635 (xslt-error "unexpected end of avt")))
636 (collect-xpath)
637 (goto in-double-quote)
639 seen-closing-}
640 (case c
641 (#\}
642 (collect-xpath)
643 (goto xpath))
644 (#\{
645 (emit)
646 (goto xpath))
647 (:eof
648 (goto done)))
649 (emit)
650 (collect-ordinary)
651 (goto ordinary)
653 seen-stray-}
654 (case c
655 (#\}
656 (collect-ordinary)
657 (goto ordinary)))
658 (xslt-error "unexpected closing brace in avt")
660 done
661 (emit))))
662 (nreverse tokens))))
664 (defun compile-attribute-value-template (template-string env)
665 (let* ((constantp t)
666 (fns
667 (mapcar (lambda (x)
668 (ecase (car x)
669 (:data
670 (constantly (second x)))
671 (:xpath
672 (setf constantp nil)
673 (xpath:compile-xpath (second x) env))))
674 (parse-attribute-value-template template-string))))
675 (values (lambda (ctx)
676 (with-output-to-string (s)
677 (dolist (fn fns)
678 (write-string (xpath:string-value (funcall fn ctx)) s))))
679 constantp)))
682 ;;;; Indentation for slime
684 (defmacro define-indentation (name (&rest args))
685 (labels ((collect-variables (list)
686 (loop
687 for sub in list
688 append
689 (etypecase sub
690 (list
691 (collect-variables sub))
692 (symbol
693 (if (eql (mismatch "&" (symbol-name sub)) 1)
695 (list sub)))))))
696 `(defmacro ,name (,@args)
697 (declare (ignorable ,@(collect-variables args)))
698 (error "XSL indentation helper ~A used literally in lisp code"
699 ',name))))
701 (define-indentation xsl:element
702 ((name &key namespace use-attribute-sets) &body body))
703 (define-indentation xsl:literal-element ((name &optional uri) &body body))
704 (define-indentation xsl:attribute ((name &key namespace) &body body))
705 (define-indentation xsl:literal-attribute ((name &optional uri) &body body))
706 (define-indentation xsl:text (str))
707 (define-indentation xsl:processing-instruction (name &body body))
708 (define-indentation xsl:comment (str))
709 (define-indentation xsl:value-of (xpath))
710 (define-indentation xsl:unescaped-value-of (xpath))
711 (define-indentation xsl:for-each (select &body decls-and-body))
712 (define-indentation xsl:message (&body body))
713 (define-indentation xsl:terminate (&body body))
714 (define-indentation xsl:apply-templates ((&key select mode) &body decls-and-body))
715 (define-indentation xsl:call-template (name &rest parameters))
716 (define-indentation xsl:copy-of (xpath))
718 ;;;;
720 (defun test-instruction (form document)
721 (let ((thunk (compile-instruction form (make-instance 'lexical-environment)))
722 (root (cxml:parse document (stp:make-builder))))
723 (with-xml-output (cxml:make-string-sink)
724 (funcall thunk (xpath:make-context root)))))