updated TEST for recent merges
[xuriella.git] / instructions.lisp
blobbcbef5615ce2c01efae43d4c8ed4bc5b8932cfe1
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 (with-element (local-name uri :suggested-prefix prefix)
132 (funcall body-thunk ctx)))))))
134 (define-instruction xsl:use-attribute-sets (args env)
135 (destructuring-bind (str) args
136 (let ((sets (mapcar (lambda (qname)
137 (multiple-value-list (decode-qname qname env nil)))
138 (words str))))
139 (lambda (ctx)
140 (loop for (local-name uri nil) in sets do
141 (dolist (thunk (find-attribute-set local-name uri))
142 (funcall thunk ctx)))))))
144 (define-instruction xsl:attribute (args env)
145 (destructuring-bind ((name &key namespace) &body body) args
146 (multiple-value-bind (name-thunk constant-name-p)
147 (compile-attribute-value-template name env)
148 (multiple-value-bind (ns-thunk constant-ns-p)
149 (if namespace
150 (compile-attribute-value-template namespace env)
151 (values nil t))
152 (let ((value-thunk (compile-instruction `(progn ,@body) env)))
153 (if (and constant-name-p constant-ns-p)
154 (compile-attribute/constant-name name namespace env value-thunk)
155 (compile-attribute/runtime name-thunk ns-thunk value-thunk)))))))
157 (defun compile-attribute/constant-name (qname namespace env value-thunk)
158 ;; the simple case: compile-time decoding of the QName
159 (multiple-value-bind (local-name uri prefix)
160 (decode-qname qname env nil)
161 (when namespace
162 (setf uri namespace))
163 (lambda (ctx)
164 (write-attribute local-name
166 (with-text-output-sink (s)
167 (with-xml-output s
168 (funcall value-thunk ctx)))
169 :suggested-prefix prefix))))
171 (defun compile-attribute/runtime (name-thunk ns-thunk value-thunk)
172 ;; run-time decoding of the QName, but using the same namespaces
173 ;; that would have been known at compilation time.
174 (let ((namespaces *namespaces*))
175 (lambda (ctx)
176 (let ((qname (funcall name-thunk ctx)))
177 (multiple-value-bind (local-name uri prefix)
178 (decode-qname/runtime qname namespaces nil)
179 (when ns-thunk
180 (setf uri (funcall ns-thunk ctx)))
181 (write-attribute local-name
182 (or uri "")
183 (with-text-output-sink (s)
184 (with-xml-output s
185 (funcall value-thunk ctx)))
186 :suggested-prefix prefix))))))
188 (defun remove-excluded-namespaces
189 (namespaces &optional (excluded-uris *excluded-namespaces*))
190 (let ((koerbchen '())
191 (kroepfchen '()))
192 (loop
193 for cons in namespaces
194 for (prefix . uri) = cons
196 (cond
197 ((find prefix kroepfchen :test #'equal))
198 ((find uri excluded-uris :test #'equal)
199 (push prefix kroepfchen))
201 (push cons koerbchen))))
202 koerbchen))
204 (define-instruction xsl:literal-element (args env)
205 (destructuring-bind
206 ((local-name &optional (uri "") suggested-prefix) &body body)
207 args
208 (let ((body-thunk (compile-instruction `(progn ,@body) env))
209 (namespaces (remove-excluded-namespaces *namespaces*)))
210 (lambda (ctx)
211 (with-element (local-name uri
212 :suggested-prefix suggested-prefix
213 :extra-namespaces namespaces)
214 (funcall body-thunk ctx))))))
216 (define-instruction xsl:literal-attribute (args env)
217 (destructuring-bind ((local-name &optional uri suggested-prefix) value) args
218 (let ((value-thunk (compile-attribute-value-template value env)))
219 (lambda (ctx)
220 (write-attribute local-name
222 (funcall value-thunk ctx)
223 :suggested-prefix suggested-prefix)))))
225 (define-instruction xsl:text (args env)
226 (destructuring-bind (str) args
227 (lambda (ctx)
228 (declare (ignore ctx))
229 (write-text str))))
231 (define-instruction xsl:processing-instruction (args env)
232 (destructuring-bind (name &rest body) args
233 (let ((name-thunk (compile-attribute-value-template name env))
234 (value-thunk (compile-instruction `(progn ,@body) env)))
235 (lambda (ctx)
236 (write-processing-instruction
237 (funcall name-thunk ctx)
238 (with-text-output-sink (s)
239 (with-xml-output s
240 (funcall value-thunk ctx))))))))
242 (define-instruction xsl:comment (args env)
243 (destructuring-bind (str) args
244 (lambda (ctx)
245 (declare (ignore ctx))
246 (write-comment str))))
248 (define-instruction xsl:value-of (args env)
249 (destructuring-bind (xpath) args
250 (let ((thunk (compile-xpath xpath env)))
251 (xslt-trace-thunk
252 (lambda (ctx)
253 (write-text (xpath:string-value (funcall thunk ctx))))
254 "value-of ~s = ~s" xpath :result))))
256 (define-instruction xsl:unescaped-value-of (args env)
257 (destructuring-bind (xpath) args
258 (let ((thunk (compile-xpath xpath env)))
259 (lambda (ctx)
260 (write-unescaped (xpath:string-value (funcall thunk ctx)))))))
262 (define-instruction xsl:copy-of (args env)
263 (destructuring-bind (xpath) args
264 (let ((thunk (compile-xpath xpath env))
265 ;; FIXME: what was this for? --david
266 #+(or) (v (intern-variable "varName" "")))
267 (lambda (ctx)
268 (let ((result (funcall thunk ctx)))
269 (typecase result
270 (xpath:node-set ;; FIXME: variables can contain node sets w/fragments inside. Maybe just fragments would do?
271 (xpath:map-node-set #'copy-into-result result))
272 (result-tree-fragment
273 (copy-into-result result))
275 (write-text (xpath:string-value result)))))))))
277 (defun copy-into-result (node)
278 (cond
279 ((result-tree-fragment-p node)
280 (stp:do-children (child (result-tree-fragment-node node))
281 (copy-into-result child)))
282 ((xpath-protocol:node-type-p node :element)
283 (with-element ((xpath-protocol:local-name node)
284 (xpath-protocol:namespace-uri node)
285 :suggested-prefix (xpath-protocol:namespace-prefix node)
286 ;; FIXME: is remove-excluded-namespaces correct here?
287 :extra-namespaces (remove-excluded-namespaces
288 (namespaces-as-alist node)))
289 (map-pipe-eagerly #'copy-into-result
290 (xpath-protocol:attribute-pipe node))
291 (map-pipe-eagerly #'copy-into-result
292 (xpath-protocol:child-pipe node))))
293 ((xpath-protocol:node-type-p node :document)
294 (map-pipe-eagerly #'copy-into-result
295 (xpath-protocol:child-pipe node)))
297 (copy-leaf-node node))))
299 (defun make-sorter (spec env)
300 (destructuring-bind (&key select lang data-type order case-order)
301 (cdr spec)
302 ;; FIXME: implement case-order
303 (declare (ignore lang case-order))
304 (let ((select-thunk (compile-xpath (or select ".") env))
305 (numberp (equal data-type "number"))
306 (f (if (equal order "descending") -1 1)))
307 (lambda (a b)
308 (let ((i (xpath:string-value
309 (funcall select-thunk (xpath:make-context a))))
310 (j (xpath:string-value
311 (funcall select-thunk (xpath:make-context b)))))
312 (* f
313 (if numberp
314 (signum (- (xpath:number-value i) (xpath:number-value j)))
315 (cond
316 ((string< i j) -1)
317 ((equal i j) 0)
318 (t 1)))))))))
320 (defun compose-sorters (sorters)
321 (if sorters
322 (let ((this (car sorters))
323 (next (compose-sorters (rest sorters))))
324 (lambda (a b)
325 (let ((d (funcall this a b)))
326 (if (zerop d)
327 (funcall next a b)
328 d))))
329 (constantly 0)))
331 (defun make-sort-predicate (decls env)
332 (let ((sorter
333 (compose-sorters
334 (mapcar (lambda (x) (make-sorter x env)) decls))))
335 (lambda (a b)
336 (minusp (funcall sorter a b)))))
338 (define-instruction xsl:for-each (args env)
339 (destructuring-bind (select &optional decls &rest body) args
340 (unless (and (consp decls)
341 (eq (car decls) 'declare))
342 (push decls body)
343 (setf decls nil))
344 (let ((select-thunk (compile-xpath select env))
345 (body-thunk (compile-instruction `(progn ,@body) env))
346 (sort-predicate
347 (when (cdr decls)
348 (make-sort-predicate (cdr decls) env))))
349 (lambda (ctx)
350 (let ((selected (funcall select-thunk ctx)))
351 (unless (xpath:node-set-p selected)
352 (xslt-error "for-each select expression should yield a node-set"))
353 (let ((nodes (xpath::force
354 (xpath::sorted-pipe-of selected))))
355 (when sort-predicate
356 (setf nodes (sort (copy-list nodes) sort-predicate)))
357 (loop
358 with n = (length nodes)
359 for node in nodes
360 for i from 1
362 (funcall body-thunk
363 (xpath:make-context node (lambda () n) i)))))))))
365 (define-instruction xsl:with-namespaces (args env)
366 (destructuring-bind ((&rest forms) &rest body) args
367 (let ((*namespaces* *namespaces*))
368 (dolist (form forms)
369 (destructuring-bind (prefix uri) form
370 (push (cons prefix uri) *namespaces*)))
371 (compile-instruction `(progn ,@body) env))))
373 (define-instruction xsl:with-excluded-namespaces (args env)
374 (destructuring-bind ((&rest uris) &rest body) args
375 (let ((*excluded-namespaces* (append uris *excluded-namespaces*)))
376 (compile-instruction `(progn ,@body) env))))
378 ;; XSLT disallows multiple definitions of the same variable within a
379 ;; template. Local variables can shadow global variables though.
380 ;; Since our LET syntax makes it natural to shadow local variables the
381 ;; Lisp way, we check for duplicate variables only where instructed to
382 ;; by the XML syntax parser using WITH-DUPLICATES-CHECK:
383 (defvar *template-variables* nil)
385 (define-instruction xsl:with-duplicates-check (args env)
386 (let ((*template-variables* *template-variables*))
387 (destructuring-bind ((&rest qnames) &rest body) args
388 (dolist (qname qnames)
389 (multiple-value-bind (local-name uri)
390 (decode-qname qname env nil)
391 (let ((key (cons local-name uri)))
392 (when (find key *template-variables* :test #'equal)
393 (xslt-error "duplicate variable: ~A, ~A" local-name uri))
394 (push key *template-variables*))))
395 (compile-instruction `(progn ,@body) env))))
397 (define-instruction xsl:with-base-uri (args env)
398 (destructuring-bind (uri &rest body) args
399 (let ((*instruction-base-uri* uri))
400 (compile-instruction `(progn ,@body) env))))
402 (defstruct (result-tree-fragment
403 (:constructor make-result-tree-fragment (node)))
404 node)
406 (defmethod xpath-protocol:node-p ((node result-tree-fragment))
409 (defmethod xpath-protocol:string-value ((node result-tree-fragment))
410 (xpath-protocol:string-value (result-tree-fragment-node node)))
412 (defun apply-to-result-tree-fragment (ctx thunk)
413 (let ((document
414 (with-xml-output (stp:make-builder)
415 (with-element ("fragment" "")
416 (funcall thunk ctx)))))
417 (make-result-tree-fragment (stp:document-element document))))
419 (define-instruction let (args env)
420 (destructuring-bind ((&rest forms) &rest body) args
421 (let* ((old-top (length *lexical-variable-declarations*))
422 (vars-and-names (compile-var-bindings/nointern forms env))
423 (vars-and-positions
424 (loop for ((local-name . uri) thunk) in vars-and-names
425 collect
426 (list (push-variable local-name
428 *lexical-variable-declarations*)
429 thunk))))
430 (let ((thunk (compile-instruction `(progn ,@body) env)))
431 (fill *lexical-variable-declarations* nil :start old-top)
432 (lambda (ctx)
433 (loop for (index var-thunk) in vars-and-positions
434 do (setf (lexical-variable-value index)
435 (funcall var-thunk ctx)))
436 (funcall thunk ctx))))))
438 (define-instruction let* (args env)
439 (destructuring-bind ((&rest forms) &rest body) args
440 (if forms
441 (compile-instruction `(let (,(car forms))
442 (let* (,@(cdr forms))
443 ,@body))
444 env)
445 (compile-instruction `(progn ,@body) env))))
447 (define-instruction xsl:message (args env)
448 (compile-message #'warn args env))
450 (define-instruction xsl:terminate (args env)
451 (compile-message #'error args env))
453 (defun namespaces-as-alist (element)
454 (let ((namespaces '()))
455 (do-pipe (ns (xpath-protocol:namespace-pipe element))
456 (push (cons (xpath-protocol:local-name ns)
457 (xpath-protocol:namespace-uri ns))
458 namespaces))
459 namespaces))
461 (define-instruction xsl:copy (args env)
462 (let ((body (compile-instruction `(progn ,@args) env)))
463 (lambda (ctx)
464 (let ((node (xpath:context-node ctx)))
465 (cond
466 ((xpath-protocol:node-type-p node :element)
467 (with-element
468 ((xpath-protocol:local-name node)
469 (xpath-protocol:namespace-uri node)
470 :suggested-prefix (xpath-protocol:namespace-prefix node)
471 :extra-namespaces (namespaces-as-alist node))
472 (funcall body ctx)))
473 ((xpath-protocol:node-type-p node :document)
474 (funcall body ctx))
476 (copy-leaf-node node)))))))
478 (defun copy-leaf-node (node)
479 (cond
480 ((xpath-protocol:node-type-p node :text)
481 (write-text (xpath-protocol:string-value node)))
482 ((xpath-protocol:node-type-p node :comment)
483 (write-comment (xpath-protocol:string-value node)))
484 ((xpath-protocol:node-type-p node :processing-instruction)
485 (write-processing-instruction
486 (xpath-protocol:processing-instruction-target node)
487 (xpath-protocol:string-value node)))
488 ((xpath-protocol:node-type-p node :attribute)
489 (write-attribute
490 (xpath-protocol:local-name node)
491 (xpath-protocol:namespace-uri node)
492 (xpath-protocol:string-value node)
493 :suggested-prefix (xpath-protocol:namespace-prefix node)))
495 (error "don't know how to copy node ~A" node))))
497 (defun compile-message (fn args env)
498 (let ((thunk (compile-instruction `(progn ,@args) env)))
499 (lambda (ctx)
500 (funcall fn
501 (with-xml-output (cxml:make-string-sink)
502 (funcall thunk ctx))))))
504 (define-instruction xsl:apply-templates (args env)
505 (destructuring-bind ((&key select mode) &rest param-binding-specs) args
506 (let* ((decls
507 (when (and (consp (car param-binding-specs))
508 (eq (caar param-binding-specs) 'declare))
509 (cdr (pop param-binding-specs))))
510 (select-thunk
511 (compile-xpath (or select "child::node()") env))
512 (param-bindings
513 (compile-var-bindings param-binding-specs env))
514 (sort-predicate
515 (when decls
516 (make-sort-predicate decls env))))
517 (multiple-value-bind (mode-local-name mode-uri)
518 (and mode (decode-qname mode env nil))
519 (lambda (ctx)
520 (let ((*mode* (if mode
521 (or (find-mode *stylesheet*
522 mode-local-name
523 mode-uri)
524 *empty-mode*)
525 *mode*)))
526 (apply-templates/list
527 (xpath::force
528 (xpath::sorted-pipe-of (funcall select-thunk ctx)))
529 (loop for (name nil value-thunk) in param-bindings
530 collect (list name (funcall value-thunk ctx)))
531 sort-predicate)))))))
533 (define-instruction xsl:apply-imports (args env)
534 (declare (ignore args env))
535 (lambda (ctx)
536 (declare (ignore ctx))
537 (funcall *apply-imports*)))
539 (define-instruction xsl:call-template (args env)
540 (destructuring-bind (name &rest param-binding-specs) args
541 (let ((param-bindings
542 (compile-var-bindings param-binding-specs env)))
543 (multiple-value-bind (local-name uri)
544 (decode-qname name env nil)
545 (setf name (cons local-name uri)))
546 (lambda (ctx)
547 (call-template ctx name
548 (loop for (name nil value-thunk) in param-bindings
549 collect (list name (funcall value-thunk ctx))))))))
551 (defun compile-instruction (form env)
552 (xslt-trace-thunk
553 (funcall (or (get (car form) 'xslt-instruction)
554 (error "undefined instruction: ~A" (car form)))
555 (cdr form)
556 env)
557 "instruction ~s" (car form)))
559 ;;: WTF: "A right curly brace inside a Literal in an expression is not
560 ;;; recognized as terminating the expression."
562 ;;; Da hilft nur tagbody.
563 (defun parse-attribute-value-template (template-string)
564 (with-input-from-string (input template-string)
565 (let ((ordinary (make-string-output-stream))
566 (xpath (make-string-output-stream))
567 (tokens '())
568 (c (read-char input nil :eof)))
569 (flet ((emit ()
570 (let ((o (get-output-stream-string ordinary)))
571 (when (plusp (length o))
572 (push (list :data o) tokens)))
573 (let ((x (get-output-stream-string xpath)))
574 (when (plusp (length x))
575 (push (list :xpath x) tokens))))
576 (collect-ordinary ()
577 (write-char c ordinary))
578 (collect-xpath ()
579 (write-char c xpath)))
580 (macrolet ((goto (target)
581 `(progn
582 (setf c (read-char input nil :eof))
583 (go ,target))))
584 (tagbody
585 ordinary
586 (case c
587 (#\{
588 (goto seen{))
589 (#\}
590 (goto seen-stray-}))
591 (:eof
592 (go done)))
593 (collect-ordinary)
594 (goto ordinary)
596 seen{
597 (case c
598 (#\{
599 (collect-ordinary)
600 (goto ordinary))
601 (#\'
602 (collect-xpath)
603 (goto in-single-quote))
604 (:eof
605 (xslt-error "unexpected end of avt")))
606 (emit)
607 (collect-xpath)
608 (goto xpath)
610 xpath
611 (case c
612 (#\'
613 (collect-xpath)
614 (goto in-single-quote))
615 (#\"
616 (collect-xpath)
617 (goto in-double-quote))
618 (#\}
619 (goto seen-closing-}))
620 (:eof
621 (xslt-error "unexpected end of avt")))
622 (collect-xpath)
623 (goto xpath)
625 in-single-quote
626 (case c
627 (#\'
628 (collect-xpath)
629 (goto xpath))
630 (:eof
631 (xslt-error "unexpected end of avt")))
632 (collect-xpath)
633 (goto in-single-quote)
635 in-double-quote
636 (case c
637 (#\"
638 (collect-xpath)
639 (goto xpath))
640 (:eof
641 (xslt-error "unexpected end of avt")))
642 (collect-xpath)
643 (goto in-double-quote)
645 seen-closing-}
646 (case c
647 (#\}
648 (collect-xpath)
649 (goto xpath))
650 (#\{
651 (emit)
652 (goto xpath))
653 (:eof
654 (goto done)))
655 (emit)
656 (collect-ordinary)
657 (goto ordinary)
659 seen-stray-}
660 (case c
661 (#\}
662 (collect-ordinary)
663 (goto ordinary)))
664 (xslt-error "unexpected closing brace in avt")
666 done
667 (emit))))
668 (nreverse tokens))))
670 (defun compile-attribute-value-template (template-string env)
671 (let* ((constantp t)
672 (fns
673 (mapcar (lambda (x)
674 (ecase (car x)
675 (:data
676 (constantly (second x)))
677 (:xpath
678 (setf constantp nil)
679 (xpath:compile-xpath (second x) env))))
680 (parse-attribute-value-template template-string))))
681 (values (lambda (ctx)
682 (with-output-to-string (s)
683 (dolist (fn fns)
684 (write-string (xpath:string-value (funcall fn ctx)) s))))
685 constantp)))
688 ;;;; Indentation for slime
690 (defmacro define-indentation (name (&rest args))
691 (labels ((collect-variables (list)
692 (loop
693 for sub in list
694 append
695 (etypecase sub
696 (list
697 (collect-variables sub))
698 (symbol
699 (if (eql (mismatch "&" (symbol-name sub)) 1)
701 (list sub)))))))
702 `(defmacro ,name (,@args)
703 (declare (ignorable ,@(collect-variables args)))
704 (error "XSL indentation helper ~A used literally in lisp code"
705 ',name))))
707 (define-indentation xsl:element
708 ((name &key namespace use-attribute-sets) &body body))
709 (define-indentation xsl:literal-element ((name &optional uri) &body body))
710 (define-indentation xsl:attribute ((name &key namespace) &body body))
711 (define-indentation xsl:literal-attribute ((name &optional uri) &body body))
712 (define-indentation xsl:text (str))
713 (define-indentation xsl:processing-instruction (name &body body))
714 (define-indentation xsl:comment (str))
715 (define-indentation xsl:value-of (xpath))
716 (define-indentation xsl:unescaped-value-of (xpath))
717 (define-indentation xsl:for-each (select &body decls-and-body))
718 (define-indentation xsl:message (&body body))
719 (define-indentation xsl:terminate (&body body))
720 (define-indentation xsl:apply-templates ((&key select mode) &body decls-and-body))
721 (define-indentation xsl:call-template (name &rest parameters))
722 (define-indentation xsl:copy-of (xpath))
724 ;;;;
726 (defun test-instruction (form document)
727 (let ((thunk (compile-instruction form (make-instance 'lexical-environment)))
728 (root (cxml:parse document (stp:make-builder))))
729 (with-xml-output (cxml:make-string-sink)
730 (funcall thunk (xpath:make-context root)))))