xsl:number fixes: use default separator when repeating only format token
[xuriella.git] / instructions.lisp
blobf3ba04574cdcfe5a17a92a004cb5bb23e7d30e9e
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 (defparameter *available-instructions* (make-hash-table :test 'equal))
40 (defmacro define-instruction (name (args-var env-var) &body body)
41 `(setf (get ',name 'xslt-instruction)
42 (lambda (,args-var ,env-var)
43 (declare (ignorable ,env-var))
44 ,@body)))
46 (define-instruction if (args env)
47 (destructuring-bind (test then &optional else) args
48 (let ((test-thunk (compile-xpath test env))
49 (then-thunk (compile-instruction then env))
50 (else-thunk (when else (compile-instruction else env))))
51 (lambda (ctx)
52 (cond
53 ((xpath:boolean-value (funcall test-thunk ctx))
54 (funcall then-thunk ctx))
55 (else-thunk
56 (funcall else-thunk ctx)))))))
58 (define-instruction when (args env)
59 (destructuring-bind (test &rest body) args
60 (compile-instruction `(if ,test (progn ,@body)) env)))
62 (define-instruction unless (args env)
63 (destructuring-bind (test &rest body) args
64 (compile-instruction `(if (:not ,test) (progn ,@body)) env)))
66 (define-instruction cond (args env)
67 (if args
68 (destructuring-bind ((test &body body) &rest clauses) args
69 (compile-instruction (if (eq test t)
70 `(progn ,@body)
71 `(if ,test
72 (progn ,@body)
73 (cond ,@clauses)))
74 env))
75 (constantly nil)))
77 (define-instruction progn (args env)
78 (if args
79 (let ((first-thunk (compile-instruction (first args) env))
80 (rest-thunk (compile-instruction `(progn ,@(rest args)) env)))
81 (lambda (ctx)
82 (funcall first-thunk ctx)
83 (funcall rest-thunk ctx)))
84 (constantly nil)))
86 (defun decode-qname/runtime (qname namespaces attributep)
87 (handler-case
88 (multiple-value-bind (prefix local-name)
89 (split-qname qname)
90 (values local-name
91 (if (or prefix (not attributep))
92 (cdr (assoc prefix namespaces :test 'equal))
93 "")
94 prefix))
95 (cxml:well-formedness-violation ()
96 (xslt-error "not a qname: ~A" qname))))
98 (define-instruction xsl:element (args env)
99 (destructuring-bind ((name &key namespace use-attribute-sets)
100 &body body)
101 args
102 (declare (ignore use-attribute-sets)) ;fixme
103 (multiple-value-bind (name-thunk constant-name-p)
104 (compile-avt name env)
105 (multiple-value-bind (ns-thunk constant-ns-p)
106 (if namespace
107 (compile-avt namespace env)
108 (values nil t))
109 (let ((body-thunk (compile-instruction `(progn ,@body) env)))
110 (if (and constant-name-p constant-ns-p)
111 (compile-element/constant-name name namespace env body-thunk)
112 (compile-element/runtime name-thunk ns-thunk body-thunk)))))))
114 (defun compile-element/constant-name (qname namespace env body-thunk)
115 ;; the simple case: compile-time decoding of the QName
116 (multiple-value-bind (local-name uri prefix)
117 (decode-qname qname env nil)
118 (when namespace
119 (setf uri namespace))
120 (lambda (ctx)
121 (cond
122 (uri
123 (with-element (local-name uri :suggested-prefix prefix)
124 (funcall body-thunk ctx)))
126 ;; ERROR rather than CERROR because saxon doesn't do the recovery,
127 ;; and the official output illustrates recovery but is useless as
128 ;; always.
129 (xslt-error "namespace not found: ~A" prefix)
130 #+(or)
131 (let ((*start-tag-written-p* t))
132 (declare (special *start-tag-written-p*))
133 (funcall body-thunk ctx)))))))
135 (defun compile-element/runtime (name-thunk ns-thunk body-thunk)
136 ;; run-time decoding of the QName, but using the same namespaces
137 ;; that would have been known at compilation time.
138 (let ((namespaces *namespaces*))
139 (lambda (ctx)
140 (let ((qname (funcall name-thunk ctx)))
141 (multiple-value-bind (local-name uri prefix)
142 (decode-qname/runtime qname namespaces nil)
143 (when ns-thunk
144 (setf uri (funcall ns-thunk ctx)))
145 (unless uri
146 (setf uri ""))
147 (with-element (local-name uri :suggested-prefix prefix)
148 (funcall body-thunk ctx)))))))
150 (define-instruction xsl:use-attribute-sets (args env)
151 (destructuring-bind (str) args
152 (let ((sets (mapcar (lambda (qname)
153 (multiple-value-list (decode-qname qname env nil)))
154 (words str))))
155 (lambda (ctx)
156 (loop for (local-name uri nil) in sets do
157 (dolist (thunk (find-attribute-set local-name uri))
158 (funcall thunk ctx)))))))
160 (define-instruction xsl:attribute (args env)
161 (destructuring-bind ((name &key namespace) &body body) args
162 (when (null name)
163 (xslt-error "xsl:attribute: name not specified"))
164 (multiple-value-bind (name-thunk constant-name-p)
165 (compile-avt name env)
166 (multiple-value-bind (ns-thunk constant-ns-p)
167 (if namespace
168 (compile-avt namespace env)
169 (values nil t))
170 (let ((value-thunk (compile-instruction `(progn ,@body) env)))
171 (if (and constant-name-p constant-ns-p)
172 (compile-attribute/constant-name name namespace env value-thunk)
173 (compile-attribute/runtime name-thunk ns-thunk value-thunk)))))))
175 (defun compile-attribute/constant-name (qname namespace env value-thunk)
176 ;; the simple case: compile-time decoding of the QName
177 (multiple-value-bind (local-name uri prefix)
178 (decode-qname qname env t)
179 (when namespace
180 (setf uri namespace))
181 (lambda (ctx)
182 (write-attribute local-name
183 (or uri "")
184 (with-toplevel-text-output-sink (s)
185 (with-xml-output s
186 (funcall value-thunk ctx)))
187 :suggested-prefix prefix))))
189 (defun compile-attribute/runtime (name-thunk ns-thunk value-thunk)
190 ;; run-time decoding of the QName, but using the same namespaces
191 ;; that would have been known at compilation time.
192 (let ((namespaces *namespaces*))
193 (lambda (ctx)
194 (let ((qname (funcall name-thunk ctx)))
195 (multiple-value-bind (local-name uri prefix)
196 (decode-qname/runtime qname namespaces t)
197 (when ns-thunk
198 (setf uri (funcall ns-thunk ctx)))
199 (write-attribute local-name
200 (or uri "")
201 (with-toplevel-text-output-sink (s)
202 (with-xml-output s
203 (funcall value-thunk ctx)))
204 :suggested-prefix prefix))))))
206 ;; zzz Also elides (later) namespaces hidden by (earlier) ones.
207 ;; zzz Reverses order.
208 (defun remove-excluded-namespaces
209 (namespaces &optional (excluded-uris *excluded-namespaces*))
210 (let ((koerbchen '())
211 (kroepfchen '()))
212 (loop
213 for cons in namespaces
214 for (prefix* . uri) = cons
215 for prefix = (or prefix* "")
217 (cond
218 ((find prefix kroepfchen :test #'equal))
219 ((find prefix koerbchen :test #'equal :key #'car))
220 ((find uri excluded-uris :test #'equal)
221 (push prefix kroepfchen))
223 (push cons koerbchen))))
224 koerbchen))
226 (define-instruction xsl:literal-element (args env)
227 (destructuring-bind
228 ((local-name &optional (uri "") suggested-prefix) &body body)
229 args
230 (let ((body-thunk (compile-instruction `(progn ,@body) env))
231 (namespaces (remove-excluded-namespaces *namespaces*)))
232 (lambda (ctx)
233 (with-element (local-name (or uri "")
234 :suggested-prefix suggested-prefix
235 :extra-namespaces namespaces
236 :process-aliases t)
237 (funcall body-thunk ctx))))))
239 (define-instruction xsl:literal-attribute (args env)
240 (destructuring-bind ((local-name &optional uri suggested-prefix) value) args
241 (let ((value-thunk (compile-avt value env)))
242 (lambda (ctx)
243 (write-attribute local-name
245 (funcall value-thunk ctx)
246 :process-aliases t
247 :suggested-prefix suggested-prefix)))))
249 (define-instruction xsl:text (args env)
250 (destructuring-bind (str) args
251 (lambda (ctx)
252 (declare (ignore ctx))
253 (write-text str))))
255 (define-instruction xsl:processing-instruction (args env)
256 (destructuring-bind (name &rest body) args
257 (let ((name-thunk (compile-avt name env))
258 (value-thunk (compile-instruction `(progn ,@body) env)))
259 (lambda (ctx)
260 (write-processing-instruction
261 (funcall name-thunk ctx)
262 (with-toplevel-text-output-sink (s)
263 (with-xml-output s
264 (funcall value-thunk ctx))))))))
266 (define-instruction xsl:comment (args env)
267 (let ((value-thunk (compile-instruction `(progn ,@args) env)))
268 (lambda (ctx)
269 (write-comment (with-toplevel-text-output-sink (s)
270 (with-xml-output s
271 (funcall value-thunk ctx)))))))
273 (define-instruction xsl:value-of (args env)
274 (destructuring-bind (xpath) args
275 (let ((thunk (compile-xpath xpath env)))
276 (xslt-trace-thunk
277 (lambda (ctx)
278 (write-text (xpath:string-value (funcall thunk ctx))))
279 "value-of ~s = ~s" xpath :result))))
281 (define-instruction xsl:unescaped-value-of (args env)
282 (destructuring-bind (xpath) args
283 (let ((thunk (compile-xpath xpath env)))
284 (lambda (ctx)
285 (write-unescaped (xpath:string-value (funcall thunk ctx)))))))
287 (define-instruction xsl:copy-of (args env)
288 (destructuring-bind (xpath) args
289 (let ((thunk (compile-xpath xpath env))
290 ;; FIXME: what was this for? --david
291 #+(or) (v (intern-variable "varName" "")))
292 (xslt-trace-thunk
293 (lambda (ctx)
294 (let ((result (funcall thunk ctx)))
295 (typecase result
296 (xpath:node-set ;; FIXME: variables can contain node sets w/fragments inside. Maybe just fragments would do?
297 (xpath:map-node-set #'copy-into-result (xpath:sort-node-set result)))
298 (result-tree-fragment
299 (copy-into-result result))
301 (write-text (xpath:string-value result))))))
302 "copy-of ~s" xpath))))
304 (defun copy-into-result (node)
305 (cond
306 ((result-tree-fragment-p node)
307 (stp:do-children (child (result-tree-fragment-node node))
308 (copy-into-result child)))
309 ((xpath-protocol:node-type-p node :element)
310 (with-element ((xpath-protocol:local-name node)
311 (xpath-protocol:namespace-uri node)
312 :suggested-prefix (xpath-protocol:namespace-prefix node)
313 :extra-namespaces (namespaces-as-alist node))
314 (map-pipe-eagerly #'copy-into-result
315 (xpath-protocol:attribute-pipe node))
316 (map-pipe-eagerly #'copy-into-result
317 (xpath-protocol:child-pipe node))))
318 ((xpath-protocol:node-type-p node :document)
319 (map-pipe-eagerly #'copy-into-result
320 (xpath-protocol:child-pipe node)))
322 (copy-leaf-node node))))
324 (defun make-sorter (spec env)
325 (destructuring-bind (&key select lang data-type order case-order)
326 (cdr spec)
327 ;; FIXME: implement case-order
328 (declare (ignore lang case-order))
329 (let ((select-thunk (compile-xpath (or select ".") env))
330 (numberp (equal data-type "number"))
331 (f (if (equal order "descending") -1 1)))
332 (lambda (a b)
333 (let ((i (xpath:string-value
334 (funcall select-thunk (xpath:make-context a))))
335 (j (xpath:string-value
336 (funcall select-thunk (xpath:make-context b)))))
337 (* f
338 (if numberp
339 (let ((n-a (xpath:number-value i))
340 (n-b (xpath:number-value j)))
341 (cond ((and (xpath::nan-p a)
342 (not (xpath::nan-p b)))
344 ((and (not (xpath::nan-p a))
345 (xpath::nan-p b))
347 ((xpath::compare-numbers '< n-a n-b) -1)
348 ((xpath::compare-numbers '> n-a n-b) 1)
349 (t 0)))
350 (cond
351 ((string< i j) -1)
352 ((equal i j) 0)
353 (t 1)))))))))
355 (defun compose-sorters (sorters)
356 (if sorters
357 (let ((this (car sorters))
358 (next (compose-sorters (rest sorters))))
359 (lambda (a b)
360 (let ((d (funcall this a b)))
361 (if (zerop d)
362 (funcall next a b)
363 d))))
364 (constantly 0)))
366 (defun make-sort-predicate (decls env)
367 (let ((sorter
368 (compose-sorters
369 (mapcar (lambda (x) (make-sorter x env)) decls))))
370 (lambda (a b)
371 (minusp (funcall sorter a b)))))
373 (define-instruction xsl:for-each (args env)
374 (destructuring-bind (select &optional decls &rest body) args
375 (unless (and (consp decls)
376 (eq (car decls) 'declare))
377 (push decls body)
378 (setf decls nil))
379 (let ((select-thunk (compile-xpath select env))
380 (body-thunk (compile-instruction `(progn ,@body) env))
381 (sort-predicate
382 (when (cdr decls)
383 (make-sort-predicate (cdr decls) env))))
384 (lambda (ctx)
385 (let ((selected (funcall select-thunk ctx)))
386 (unless (xpath:node-set-p selected)
387 (xslt-error "for-each select expression should yield a node-set"))
388 (let ((nodes (xpath::force
389 (xpath::sorted-pipe-of selected))))
390 (when sort-predicate
391 (setf nodes (sort (copy-list nodes) sort-predicate)))
392 (loop
393 with n = (length nodes)
394 for node in nodes
395 for i from 1
397 (funcall body-thunk
398 (xpath:make-context node (lambda () n) i)))))))))
400 (define-instruction xsl:with-namespaces (args env)
401 (destructuring-bind ((&rest forms) &rest body) args
402 (let ((*namespaces* *namespaces*))
403 (dolist (form forms)
404 (destructuring-bind (prefix uri) form
405 (push (cons prefix uri) *namespaces*)))
406 (compile-instruction `(progn ,@body) env))))
408 (define-instruction xsl:with-excluded-namespaces (args env)
409 (destructuring-bind ((&rest uris) &rest body) args
410 (let ((*excluded-namespaces* (append uris *excluded-namespaces*)))
411 (compile-instruction `(progn ,@body) env))))
413 (define-instruction xsl:with-extension-namespaces (args env)
414 (destructuring-bind ((&rest uris) &rest body) args
415 (let ((*extension-namespaces* (append uris *extension-namespaces*)))
416 (compile-instruction `(progn ,@body) env))))
418 ;; XSLT disallows multiple definitions of the same variable within a
419 ;; template. Local variables can shadow global variables though.
420 ;; Since our LET syntax makes it natural to shadow local variables the
421 ;; Lisp way, we check for duplicate variables only where instructed to
422 ;; by the XML syntax parser using WITH-DUPLICATES-CHECK:
423 (defvar *template-variables* nil)
425 (define-instruction xsl:with-duplicates-check (args env)
426 (let ((*template-variables* *template-variables*))
427 (destructuring-bind ((&rest qnames) &rest body) args
428 (dolist (qname qnames)
429 (multiple-value-bind (local-name uri)
430 (decode-qname qname env nil)
431 (let ((key (cons local-name uri)))
432 (when (find key *template-variables* :test #'equal)
433 (xslt-error "duplicate variable: ~A, ~A" local-name uri))
434 (push key *template-variables*))))
435 (compile-instruction `(progn ,@body) env))))
437 (define-instruction xsl:with-base-uri (args env)
438 (destructuring-bind (uri &rest body) args
439 (let ((*instruction-base-uri* uri))
440 (compile-instruction `(progn ,@body) env))))
442 (defstruct (result-tree-fragment
443 (:constructor make-result-tree-fragment (node)))
444 node)
446 (define-default-method xpath-protocol:node-p
447 ((node result-tree-fragment))
450 (define-default-method xpath-protocol:node-text
451 ((node result-tree-fragment))
452 (xpath-protocol:node-text (result-tree-fragment-node node)))
454 (defun apply-to-result-tree-fragment (ctx thunk)
455 (let ((document
456 (with-xml-output (stp:make-builder)
457 (with-element ("fragment" "")
458 (funcall thunk ctx)))))
459 (make-result-tree-fragment (stp:document-element document))))
461 (define-instruction let (args env)
462 (destructuring-bind ((&rest forms) &rest body) args
463 (let* ((old-top (length *lexical-variable-declarations*))
464 (vars-and-names (compile-var-bindings/nointern forms env))
465 (vars-and-positions
466 (loop for ((local-name . uri) thunk) in vars-and-names
467 collect
468 (list (push-variable local-name
470 *lexical-variable-declarations*)
471 thunk))))
472 (let ((thunk (compile-instruction `(progn ,@body) env)))
473 (fill *lexical-variable-declarations* nil :start old-top)
474 (lambda (ctx)
475 (loop for (index var-thunk) in vars-and-positions
476 do (setf (lexical-variable-value index)
477 (funcall var-thunk ctx)))
478 (funcall thunk ctx))))))
480 (define-instruction let* (args env)
481 (destructuring-bind ((&rest forms) &rest body) args
482 (if forms
483 (compile-instruction `(let (,(car forms))
484 (let* (,@(cdr forms))
485 ,@body))
486 env)
487 (compile-instruction `(progn ,@body) env))))
489 (define-instruction xsl:message (args env)
490 (compile-message #'warn args env))
492 (define-instruction xsl:terminate (args env)
493 (compile-message #'error args env))
495 (defun namespaces-as-alist (element)
496 (let ((namespaces '()))
497 (do-pipe (ns (xpath-protocol:namespace-pipe element))
498 (push (cons (xpath-protocol:local-name ns)
499 (xpath-protocol:node-text ns))
500 namespaces))
501 namespaces))
503 (define-instruction xsl:copy (args env)
504 (let ((body (compile-instruction `(progn ,@args) env)))
505 (lambda (ctx)
506 (let ((node (xpath:context-node ctx)))
507 (cond
508 ((xpath-protocol:node-type-p node :element)
509 (with-element
510 ((xpath-protocol:local-name node)
511 (xpath-protocol:namespace-uri node)
512 :suggested-prefix (xpath-protocol:namespace-prefix node)
513 :extra-namespaces (namespaces-as-alist node))
514 (funcall body ctx)))
515 ((xpath-protocol:node-type-p node :document)
516 (funcall body ctx))
518 (copy-leaf-node node)))))))
520 (defun copy-leaf-node (node)
521 (cond
522 ((xpath-protocol:node-type-p node :text)
523 (write-text (xpath-protocol:node-text node)))
524 ((xpath-protocol:node-type-p node :comment)
525 (write-comment (xpath-protocol:node-text node)))
526 ((xpath-protocol:node-type-p node :processing-instruction)
527 (write-processing-instruction
528 (xpath-protocol:processing-instruction-target node)
529 (xpath-protocol:node-text node)))
530 ((xpath-protocol:node-type-p node :attribute)
531 (write-attribute
532 (xpath-protocol:local-name node)
533 (xpath-protocol:namespace-uri node)
534 (xpath-protocol:node-text node)
535 :suggested-prefix (xpath-protocol:namespace-prefix node)))
536 ((xpath-protocol:node-type-p node :namespace)
537 (write-extra-namespace
538 (xpath-protocol:local-name node)
539 (xpath-protocol:node-text node)
540 nil))
542 (error "don't know how to copy node ~A" node))))
544 (defun compile-message (fn args env)
545 (let ((thunk (compile-instruction `(progn ,@args) env)))
546 (lambda (ctx)
547 (funcall fn
548 (with-xml-output (cxml:make-string-sink)
549 (funcall thunk ctx))))))
551 (define-instruction xsl:apply-templates (args env)
552 (destructuring-bind ((&key select mode) &rest param-binding-specs) args
553 (let* ((decls
554 (when (and (consp (car param-binding-specs))
555 (eq (caar param-binding-specs) 'declare))
556 (cdr (pop param-binding-specs))))
557 (select-thunk
558 (compile-xpath (or select "child::node()") env))
559 (param-bindings
560 (compile-var-bindings param-binding-specs env))
561 (sort-predicate
562 (when decls
563 (make-sort-predicate decls env))))
564 (multiple-value-bind (mode-local-name mode-uri)
565 (and mode (decode-qname mode env nil))
566 (lambda (ctx)
567 (apply-templates/list
568 (xpath::force
569 (xpath::sorted-pipe-of (funcall select-thunk ctx)))
570 :param-bindings
571 (loop for (name nil value-thunk) in param-bindings
572 collect (list name (funcall value-thunk ctx)))
573 :sort-predicate sort-predicate
574 :mode (when mode
575 (or (find-mode *stylesheet*
576 mode-local-name
577 mode-uri)
578 *empty-mode*))))))))
580 (define-instruction xsl:apply-imports (args env)
581 (declare (ignore args env))
582 (lambda (ctx)
583 (declare (ignore ctx))
584 (funcall *apply-imports*)))
586 (define-instruction xsl:call-template (args env)
587 (destructuring-bind (name &rest param-binding-specs) args
588 (let ((param-bindings
589 (compile-var-bindings param-binding-specs env)))
590 (multiple-value-bind (local-name uri)
591 (decode-qname name env nil)
592 (setf name (cons local-name uri)))
593 (lambda (ctx)
594 (call-template ctx name
595 (loop for (name nil value-thunk) in param-bindings
596 collect (list name (funcall value-thunk ctx))))))))
598 ;; fixme: incompatible with XSLT 2.0
599 (define-instruction xsl:document (args env)
600 (destructuring-bind ((href &key method indent doctype-public doctype-system)
601 &body body)
602 args
603 (declare (ignore doctype-public doctype-system)) ;fixme
604 (let ((thunk (compile-instruction `(progn ,@body) env))
605 (href-thunk (compile-avt href env)))
606 (lambda (ctx)
607 (let ((pathname
608 (uri-to-pathname
609 (puri:merge-uris (funcall href-thunk ctx)
610 (xpath-protocol:base-uri
611 (xpath:context-node ctx))))))
612 (ensure-directories-exist pathname) ;really?
613 (invoke-with-output-sink
614 (lambda ()
615 (funcall thunk ctx))
616 (make-output-specification :method (or method "XML") :indent indent)
617 pathname))))))
619 (defun compile-instruction (form env)
620 (xslt-trace-thunk
621 (funcall (or (get (car form) 'xslt-instruction)
622 (error "undefined instruction: ~A" (car form)))
623 (cdr form)
624 env)
625 "instruction ~s" (car form)))
627 ;;: WTF: "A right curly brace inside a Literal in an expression is not
628 ;;; recognized as terminating the expression."
630 ;;; Da hilft nur tagbody.
631 (defun parse-attribute-value-template (template-string)
632 (with-input-from-string (input template-string)
633 (let ((ordinary (make-string-output-stream))
634 (xpath (make-string-output-stream))
635 (tokens '())
636 (c (read-char input nil :eof)))
637 (flet ((emit ()
638 (let ((o (get-output-stream-string ordinary)))
639 (when (plusp (length o))
640 (push (list :data o) tokens)))
641 (let ((x (get-output-stream-string xpath)))
642 (when (plusp (length x))
643 (push (list :xpath x) tokens))))
644 (collect-ordinary ()
645 (write-char c ordinary))
646 (collect-xpath ()
647 (write-char c xpath)))
648 (macrolet ((goto (target)
649 `(progn
650 (setf c (read-char input nil :eof))
651 (go ,target))))
652 (tagbody
653 ordinary
654 (case c
655 (#\{
656 (goto seen{))
657 (#\}
658 (goto seen-stray-}))
659 (:eof
660 (go done)))
661 (collect-ordinary)
662 (goto ordinary)
664 seen{
665 (case c
666 (#\{
667 (collect-ordinary)
668 (goto ordinary))
669 (#\'
670 (collect-xpath)
671 (goto in-single-quote))
672 (:eof
673 (xslt-error "unexpected end of avt")))
674 (emit)
675 (collect-xpath)
676 (goto xpath)
678 xpath
679 (case c
680 (#\'
681 (collect-xpath)
682 (goto in-single-quote))
683 (#\"
684 (collect-xpath)
685 (goto in-double-quote))
686 (#\}
687 (goto seen-closing-}))
688 (:eof
689 (xslt-error "unexpected end of avt")))
690 (collect-xpath)
691 (goto xpath)
693 in-single-quote
694 (case c
695 (#\'
696 (collect-xpath)
697 (goto xpath))
698 (:eof
699 (xslt-error "unexpected end of avt")))
700 (collect-xpath)
701 (goto in-single-quote)
703 in-double-quote
704 (case c
705 (#\"
706 (collect-xpath)
707 (goto xpath))
708 (:eof
709 (xslt-error "unexpected end of avt")))
710 (collect-xpath)
711 (goto in-double-quote)
713 seen-closing-}
714 (case c
715 (#\}
716 (collect-xpath)
717 (goto xpath))
718 (#\{
719 (emit)
720 (goto xpath))
721 (:eof
722 (goto done)))
723 (emit)
724 (collect-ordinary)
725 (goto ordinary)
727 seen-stray-}
728 (case c
729 (#\}
730 (collect-ordinary)
731 (goto ordinary)))
732 (xslt-error "unexpected closing brace in avt")
734 done
735 (emit))))
736 (nreverse tokens))))
738 (defun compile-avt (template-string env)
739 (let* ((constantp t)
740 (fns
741 (mapcar (lambda (x)
742 (ecase (car x)
743 (:data
744 (constantly (second x)))
745 (:xpath
746 (setf constantp nil)
747 (compile-xpath (second x) env))))
748 (parse-attribute-value-template template-string))))
749 (values (lambda (ctx)
750 (with-output-to-string (s)
751 (dolist (fn fns)
752 (write-string (xpath:string-value (funcall fn ctx)) s))))
753 constantp)))
756 ;;;; Indentation for slime
758 (defmacro define-indentation (name (&rest args))
759 (labels ((collect-variables (list)
760 (loop
761 for sub in list
762 append
763 (etypecase sub
764 (list
765 (collect-variables sub))
766 (symbol
767 (if (eql (mismatch "&" (symbol-name sub)) 1)
769 (list sub)))))))
770 `(defmacro ,name (,@args)
771 (declare (ignorable ,@(collect-variables args)))
772 (error "XSL indentation helper ~A used literally in lisp code"
773 ',name))))
775 (define-indentation xsl:element
776 ((name &key namespace use-attribute-sets) &body body))
777 (define-indentation xsl:literal-element ((name &optional uri) &body body))
778 (define-indentation xsl:attribute ((name &key namespace) &body body))
779 (define-indentation xsl:literal-attribute ((name &optional uri) &body body))
780 (define-indentation xsl:text (str))
781 (define-indentation xsl:processing-instruction (name &body body))
782 (define-indentation xsl:comment (&body body))
783 (define-indentation xsl:value-of (xpath))
784 (define-indentation xsl:unescaped-value-of (xpath))
785 (define-indentation xsl:for-each (select &body decls-and-body))
786 (define-indentation xsl:message (&body body))
787 (define-indentation xsl:terminate (&body body))
788 (define-indentation xsl:apply-templates ((&key select mode) &body decls-and-body))
789 (define-indentation xsl:call-template (name &rest parameters))
790 (define-indentation xsl:copy-of (xpath))
792 ;;;;
794 (defun test-instruction (form document)
795 (let ((thunk (compile-instruction form (make-instance 'lexical-environment)))
796 (root (cxml:parse document (stp:make-builder))))
797 (with-xml-output (cxml:make-string-sink)
798 (funcall thunk (xpath:make-context root)))))