Forwards compatible processing: Unknown instruction elements
[xuriella.git] / parser.lisp
blob2854480df171e23bf51f69dc5fe4908006f22ce7
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 (let ((sym (find-symbol (stp:local-name node) :xuriella)))
103 (cond
104 (sym
105 (parse-instruction/xsl-element sym node))
106 (*forwards-compatible-p*
107 (parse-fallback-children node))
109 (xslt-error "undefined instruction: ~A"
110 (stp:local-name node))))))
111 ((find (stp:namespace-uri node)
112 *extension-namespaces*
113 :test #'equal)
114 (parse-fallback-children node))
116 (parse-instruction/literal-element node))))
117 (parent (stp:parent node)))
118 (if (and (equal (stp:base-uri node) (stp:base-uri parent))
119 (equal (stp:namespace-uri parent) *xsl*)
120 (find-symbol (stp:local-name parent) :xuriella))
121 expr
122 `(xsl:with-base-uri ,(stp:base-uri node)
123 ,expr))))
124 (stp:text
125 `(xsl:text ,(stp:data node)))))
127 (defun parse-instruction/literal-element (node)
128 (let ((le
129 `(xsl:literal-element
130 (,(stp:local-name node)
131 ,(stp:namespace-uri node)
132 ,(stp:namespace-prefix node))
133 (xsl:use-attribute-sets
134 ,(stp:attribute-value node "use-attribute-sets" *xsl*))
135 ,@(loop for a in (stp:list-attributes node)
136 unless (equal (stp:namespace-uri a) *xsl*)
137 collect `(xsl:literal-attribute
138 (,(stp:local-name a)
139 ,(stp:namespace-uri a)
140 ,(stp:namespace-prefix a))
141 ,(stp:value a)))
142 ,@(parse-body node)))
143 (version (stp:attribute-value node "version" *xsl*))
144 (extensions '()))
145 (stp:with-attributes ((eep "extension-element-prefixes" *xsl*))
146 node
147 (dolist (prefix (words (or eep "")))
148 (when (equal prefix "#default")
149 (setf prefix nil))
150 (push (or (stp:find-namespace prefix node)
151 (xslt-error "namespace not found: ~A" prefix))
152 extensions)))
153 (when extensions
154 (setf le
155 `(xsl:with-extension-namespaces ,extensions
156 (xsl:with-excluded-namespaces ,extensions
157 ,le))))
158 (when version
159 (setf le
160 `(xsl:with-version ,version
161 ,le)))
162 le))
164 (defun parse-fallback-children (node)
165 (let ((fallbacks
166 (loop
167 for fallback in (stp:filter-children (of-name "fallback") node)
168 append (parse-body fallback))))
169 (if fallbacks
170 `(progn ,@fallbacks)
171 `(xsl:terminate
172 (xsl:text
173 "no fallback children in unknown element using forwards compatible processing")))))
175 (defmacro define-instruction-parser (name (node-var) &body body)
176 `(progn
177 (setf (gethash ,(symbol-name name) *available-instructions*) t)
178 (defmethod parse-instruction/xsl-element
179 ((.name. (eql ',name)) ,node-var)
180 (declare (ignore .name.))
181 ,@body)))
183 (define-instruction-parser |fallback| (node)
184 '(progn))
186 (define-instruction-parser |apply-templates| (node)
187 (stp:with-attributes (select mode) node
188 (multiple-value-bind (decls rest)
189 (loop
190 for i from 0
191 for cons on (stp:list-children node)
192 for (child . nil) = cons
193 while (namep child "sort")
194 collect (parse-sort child) into decls
195 finally (return (values decls cons)))
196 `(xsl:apply-templates
197 (:select ,select :mode ,mode)
198 (declare ,@decls)
199 ,@(mapcar (lambda (clause)
200 (unless (namep clause "with-param")
201 (xslt-error "undefined instruction: ~A"
202 (stp:local-name clause)))
203 (parse-param clause))
204 rest)))))
206 (define-instruction-parser |apply-imports| (node)
207 `(xsl:apply-imports))
209 (define-instruction-parser |call-template| (node)
210 (stp:with-attributes (name) node
211 `(xsl:call-template
212 ,name ,@(stp:map-children 'list
213 (lambda (clause)
214 (if (namep clause "with-param")
215 (parse-param clause)
216 (xslt-error "undefined instruction: ~A"
217 (stp:local-name clause))))
218 node))))
220 (define-instruction-parser |if| (node)
221 (stp:with-attributes (test) node
222 `(when ,test
223 ,@(parse-body node))))
225 (define-instruction-parser |choose| (node)
226 `(cond
227 ,@(stp:map-children 'list
228 (lambda (clause)
229 (cond
230 ((namep clause "when")
231 (stp:with-attributes (test) clause
232 `(,test
233 ,@(parse-body clause))))
234 ((namep clause "otherwise")
235 `(t ,@(parse-body clause)))
237 (xslt-error "invalid <choose> clause: ~A"
238 (stp:local-name clause)))))
239 node)))
241 (define-instruction-parser |element| (node)
242 (stp:with-attributes (name namespace use-attribute-sets) node
243 `(xsl:element (,name :namespace ,namespace)
244 (xsl:use-attribute-sets ,use-attribute-sets)
245 ,@(parse-body node))))
247 (define-instruction-parser |attribute| (node)
248 (stp:with-attributes (name namespace) node
249 `(xsl:attribute (,name :namespace ,namespace)
250 ,@(parse-body node))))
252 (define-instruction-parser |text| (node)
253 (stp:with-attributes (select disable-output-escaping) node
254 (if (equal disable-output-escaping "yes")
255 `(xsl:unescaped-text ,(stp:string-value node))
256 `(xsl:text ,(stp:string-value node)))))
258 (define-instruction-parser |comment| (node)
259 `(xsl:comment ,@(parse-body node)))
261 (define-instruction-parser |processing-instruction| (node)
262 (stp:with-attributes (name) node
263 `(xsl:processing-instruction ,name
264 ,@(parse-body node))))
266 (define-instruction-parser |value-of| (node)
267 (stp:with-attributes (select disable-output-escaping) node
268 (if (equal disable-output-escaping "yes")
269 `(xsl:unescaped-value-of ,select)
270 `(xsl:value-of ,select))))
272 (define-instruction-parser |copy-of| (node)
273 (stp:with-attributes (select) node
274 `(xsl:copy-of ,select)))
276 (define-instruction-parser |copy| (node)
277 (stp:with-attributes (use-attribute-sets) node
278 `(xsl:copy
279 (xsl:use-attribute-sets ,use-attribute-sets)
280 ,@(parse-body node))))
282 (define-instruction-parser |variable| (node)
283 (xslt-error "unhandled xsl:variable"))
285 (define-instruction-parser |for-each| (node)
286 (stp:with-attributes (select) node
287 (multiple-value-bind (decls body-position)
288 (loop
289 for i from 0
290 for child in (stp:list-children node)
291 while (namep child "sort")
292 collect (parse-sort child) into decls
293 finally (return (values decls i)))
294 `(xsl:for-each ,select
295 (declare ,@decls)
296 ,@(parse-body node body-position)))))
298 (defun parse-sort (node)
299 (stp:with-attributes (select lang data-type order case-order) node
300 `(sort :select ,select
301 :lang ,lang
302 :data-type ,data-type
303 :order ,order
304 :case-order ,case-order)))
306 (define-instruction-parser |message| (node)
307 `(xsl:message ,@(parse-body node)))
309 (define-instruction-parser |terminate| (node)
310 `(xsl:terminate ,@(parse-body node)))
312 (define-instruction-parser |number| (node)
313 (stp:with-attributes (level count from value format lang letter-value
314 grouping-separator grouping-size)
315 node
316 `(xsl:number :level ,level
317 :count ,count
318 :from ,from
319 :value ,value
320 :format ,format
321 :lang ,lang
322 :letter-value ,letter-value
323 :grouping-separator ,grouping-separator
324 :grouping-size ,grouping-size)))
326 (define-instruction-parser |document| (node)
327 (stp:with-attributes (href method indent doctype-public doctype-system) node
328 `(xsl:document (,href :method ,method
329 :indent ,indent
330 :doctype-public ,doctype-public
331 :doctype-system ,doctype-system)
332 ,@(parse-body node))))