Text output method
[xuriella.git] / parser.lisp
blob315506d6f0c0e12ce90873e0aea1f5a6db08222a
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 (defun map-namespace-declarations (fn element)
33 (let ((parent (stp:parent element)))
34 (maphash (lambda (prefix uri)
35 (unless (and (typep parent 'stp:element)
36 (equal (stp:find-namespace prefix parent) uri))
37 (funcall fn prefix uri)))
38 (cxml-stp-impl::collect-local-namespaces element))))
40 (defun maybe-wrap-namespaces (child exprs)
41 (if (typep child 'stp:element)
42 (let ((bindings '())
43 (excluded-uris '()))
44 (map-namespace-declarations (lambda (prefix uri)
45 (push (list prefix uri) bindings))
46 child)
47 (stp:with-attributes ((erp "exclude-result-prefixes" *xsl*))
48 child
49 (dolist (prefix (words (or erp "")))
50 (when (equal prefix "#default")
51 (setf prefix nil))
52 (push (or (stp:find-namespace prefix child)
53 (xslt-error "namespace not found: ~A" prefix))
54 excluded-uris)))
55 (if (or bindings excluded-uris)
56 `((xsl:with-namespaces ,bindings
57 (xsl:with-excluded-namespaces ,excluded-uris
58 ,@exprs)))
59 exprs))
60 exprs))
62 (defun parse-body (node &optional (start 0) (param-names '()))
63 (let ((n (stp:count-children-if #'identity node)))
64 (labels ((recurse (i)
65 (when (< i n)
66 (let ((child (stp:nth-child i node)))
67 (maybe-wrap-namespaces
68 child
69 (if (namep child "variable")
70 (stp:with-attributes (name select) child
71 (when (and select (stp:list-children child))
72 (xslt-error "variable with select and body"))
73 `((let ((,name ,(or select
74 `(progn ,@(parse-body child)))))
75 (xsl:with-duplicates-check (,name)
76 ,@(recurse (1+ i))))))
77 (cons (parse-instruction child)
78 (recurse (1+ i)))))))))
79 (let ((result (recurse start)))
80 (if param-names
81 `((xsl:with-duplicates-check (,@param-names)
82 ,@result))
83 result)))))
85 (defun parse-param (node)
86 ;; FIXME: empty body?
87 (stp:with-attributes (name select) node
88 (unless name
89 (xslt-error "name not specified for parameter"))
90 (when (and select (stp:list-children node))
91 (xslt-error "param with select and body"))
92 (list name
93 (or select
94 `(progn ,@(parse-body node))))))
96 (defun parse-instruction (node)
97 (typecase node
98 (stp:element
99 (let ((expr
100 (cond
101 ((equal (stp:namespace-uri node) *xsl*)
102 (parse-instruction/xsl-element
103 (or (find-symbol (stp:local-name node) :xuriella)
104 (xslt-error "undefined instruction: ~A"
105 (stp:local-name node)))
106 node))
107 ((find (stp:namespace-uri node)
108 *extension-namespaces*
109 :test #'equal)
110 (parse-fallback-children node))
112 (parse-instruction/literal-element node))))
113 (parent (stp:parent node)))
114 (if (and (equal (stp:base-uri node) (stp:base-uri parent))
115 (equal (stp:namespace-uri parent) *xsl*)
116 (find-symbol (stp:local-name parent) :xuriella))
117 expr
118 `(xsl:with-base-uri ,(stp:base-uri node)
119 ,expr))))
120 (stp:text
121 `(xsl:text ,(stp:data node)))))
123 (defun parse-instruction/literal-element (node)
124 `(xsl:literal-element
125 (,(stp:local-name node)
126 ,(stp:namespace-uri node)
127 ,(stp:namespace-prefix node))
128 (xsl:use-attribute-sets
129 ,(stp:attribute-value node "use-attribute-sets" *xsl*))
130 ,@(loop for a in (stp:list-attributes node)
131 unless (equal (stp:namespace-uri a) *xsl*)
132 collect `(xsl:literal-attribute
133 (,(stp:local-name a)
134 ,(stp:namespace-uri a)
135 ,(stp:namespace-prefix a))
136 ,(stp:value a)))
137 ,@(parse-body node)))
139 (defun parse-fallback-children (node)
140 `(progn
141 ,@(loop
142 for fallback in (stp:filter-children (of-name "fallback") node)
143 append (parse-body fallback))))
145 (defmacro define-instruction-parser (name (node-var) &body body)
146 `(progn
147 (setf (gethash ,(symbol-name name) *available-instructions*) t)
148 (defmethod parse-instruction/xsl-element
149 ((.name. (eql ',name)) ,node-var)
150 (declare (ignore .name.))
151 ,@body)))
153 (define-instruction-parser |fallback| (node)
154 '(progn))
156 (define-instruction-parser |apply-templates| (node)
157 (stp:with-attributes (select mode) node
158 (multiple-value-bind (decls rest)
159 (loop
160 for i from 0
161 for cons on (stp:list-children node)
162 for (child . nil) = cons
163 while (namep child "sort")
164 collect (parse-sort child) into decls
165 finally (return (values decls cons)))
166 `(xsl:apply-templates
167 (:select ,select :mode ,mode)
168 (declare ,@decls)
169 ,@(mapcar (lambda (clause)
170 (unless (namep clause "with-param")
171 (xslt-error "undefined instruction: ~A"
172 (stp:local-name clause)))
173 (parse-param clause))
174 rest)))))
176 (define-instruction-parser |apply-imports| (node)
177 `(xsl:apply-imports))
179 (define-instruction-parser |call-template| (node)
180 (stp:with-attributes (name) node
181 `(xsl:call-template
182 ,name ,@(stp:map-children 'list
183 (lambda (clause)
184 (if (namep clause "with-param")
185 (parse-param clause)
186 (xslt-error "undefined instruction: ~A"
187 (stp:local-name clause))))
188 node))))
190 (define-instruction-parser |if| (node)
191 (stp:with-attributes (test) node
192 `(when ,test
193 ,@(parse-body node))))
195 (define-instruction-parser |choose| (node)
196 `(cond
197 ,@(stp:map-children 'list
198 (lambda (clause)
199 (cond
200 ((namep clause "when")
201 (stp:with-attributes (test) clause
202 `(,test
203 ,@(parse-body clause))))
204 ((namep clause "otherwise")
205 `(t ,@(parse-body clause)))
207 (xslt-error "invalid <choose> clause: ~A"
208 (stp:local-name clause)))))
209 node)))
211 (define-instruction-parser |element| (node)
212 (stp:with-attributes (name namespace use-attribute-sets) node
213 `(xsl:element (,name :namespace ,namespace)
214 (xsl:use-attribute-sets ,use-attribute-sets)
215 ,@(parse-body node))))
217 (define-instruction-parser |attribute| (node)
218 (stp:with-attributes (name namespace) node
219 `(xsl:attribute (,name :namespace ,namespace)
220 ,@(parse-body node))))
222 (define-instruction-parser |text| (node)
223 `(xsl:text ,(stp:string-value node)))
225 (define-instruction-parser |comment| (node)
226 `(xsl:comment ,@(parse-body node)))
228 (define-instruction-parser |processing-instruction| (node)
229 (stp:with-attributes (name) node
230 `(xsl:processing-instruction ,name
231 ,@(parse-body node))))
233 (define-instruction-parser |value-of| (node)
234 (stp:with-attributes (select disable-output-escaping) node
235 (if disable-output-escaping
236 `(xsl:unescaped-value-of ,select)
237 `(xsl:value-of ,select))))
239 (define-instruction-parser |copy-of| (node)
240 (stp:with-attributes (select) node
241 `(xsl:copy-of ,select)))
243 (define-instruction-parser |copy| (node)
244 (stp:with-attributes (use-attribute-sets) node
245 `(xsl:copy
246 (xsl:use-attribute-sets ,use-attribute-sets)
247 ,@(parse-body node))))
249 (define-instruction-parser |variable| (node)
250 (xslt-error "unhandled xsl:variable"))
252 (define-instruction-parser |for-each| (node)
253 (stp:with-attributes (select) node
254 (multiple-value-bind (decls body-position)
255 (loop
256 for i from 0
257 for child in (stp:list-children node)
258 while (namep child "sort")
259 collect (parse-sort child) into decls
260 finally (return (values decls i)))
261 `(xsl:for-each ,select
262 (declare ,@decls)
263 ,@(parse-body node body-position)))))
265 (defun parse-sort (node)
266 (stp:with-attributes (select lang data-type order case-order) node
267 `(sort :select ,select
268 :lang ,lang
269 :data-type ,data-type
270 :order ,order
271 :case-order ,case-order)))
273 (define-instruction-parser |message| (node)
274 `(xsl:message ,@(parse-body node)))
276 (define-instruction-parser |terminate| (node)
277 `(xsl:terminate ,@(parse-body node)))
279 (define-instruction-parser |number| (node)
280 (stp:with-attributes (level count from value format lang letter-value
281 grouping-separator grouping-size)
282 node
283 `(xsl:number :level ,level
284 :count ,count
285 :from ,from
286 :value ,value
287 :format ,format
288 :lang ,lang
289 :letter-value ,letter-value
290 :grouping-separator ,grouping-separator
291 :grouping-size ,grouping-size)))
293 (define-instruction-parser |document| (node)
294 (stp:with-attributes (href method indent doctype-public doctype-system) node
295 `(xsl:document (,href :method ,method
296 :indent ,indent
297 :doctype-public ,doctype-public
298 :doctype-system ,doctype-system)
299 ,@(parse-body node))))