Docstrings
[xuriella.git] / parser.lisp
blob7014e24cf319f29f56d17fa171f582875827be42
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 &optional include-redeclared)
33 (let ((parent (stp:parent element)))
34 (maphash (lambda (prefix uri)
35 (unless (and (not include-redeclared)
36 (typep parent 'stp:element)
37 (equal (stp:find-namespace prefix parent) uri))
38 (funcall fn prefix uri)))
39 (cxml-stp-impl::collect-local-namespaces element))))
41 (defun maybe-wrap-namespaces (child exprs)
42 (if (typep child 'stp:element)
43 (let ((bindings '())
44 (excluded-uris '()))
45 (map-namespace-declarations (lambda (prefix uri)
46 (push (list prefix uri) bindings))
47 child)
48 (stp:with-attributes ((erp "exclude-result-prefixes" *xsl*))
49 child
50 (dolist (prefix (words (or erp "")))
51 (when (equal prefix "#default")
52 (setf prefix nil))
53 (push (or (stp:find-namespace prefix child)
54 (xslt-error "namespace not found: ~A" prefix))
55 excluded-uris)))
56 (if (or bindings excluded-uris)
57 `((xsl:with-namespaces ,bindings
58 (xsl:with-excluded-namespaces ,excluded-uris
59 ,@exprs)))
60 exprs))
61 exprs))
63 (defun parse-body (node &optional (start 0) (param-names '()))
64 "@arg[node]{A node representing part of a stylesheet.}
65 @arg[start]{An optional integer, defaulting to 0.}
66 @arg[param-names]{Undocumented.}
67 @return{An list of XSLT instructions in sexp syntax}
69 @short{Parses the children of an XSLT instruction.}
71 This function is for use in XSLT extensions. When defining an
72 extension using @fun{define-extension-parser}, it can be used
73 to parse the children of the extension node using regular XSLT syntax
74 recursively.
76 Specify @code{start} to skip the first @code{start} child nodes."
77 (let ((n (stp:count-children-if #'identity node)))
78 (labels ((recurse (i)
79 (when (< i n)
80 (let ((child (stp:nth-child i node)))
81 (if (namep child "variable")
82 (maybe-wrap-namespaces
83 child
84 (only-with-attributes (name select) child
85 (when (and select (stp:list-children child))
86 (xslt-error "variable with select and body"))
87 `((let ((,name ,(or select
88 `(progn ,@(parse-body child)))))
89 (xsl:with-duplicates-check (,name)
90 ,@(recurse (1+ i)))))))
91 (append (maybe-wrap-namespaces
92 child
93 (list (parse-instruction child)))
94 (recurse (1+ i))))))))
95 (let ((result (recurse start)))
96 (if param-names
97 `((xsl:with-duplicates-check (,@param-names)
98 ,@result))
99 result)))))
101 (defun parse-param (node)
102 ;; FIXME: empty body?
103 (only-with-attributes (name select) node
104 (unless name
105 (xslt-error "name not specified for parameter"))
106 (when (and select (stp:list-children node))
107 (xslt-error "param with select and body"))
108 (list name
109 (or select
110 `(progn ,@(parse-body node))))))
112 (defun parse-instruction (node)
113 (typecase node
114 (stp:element
115 (let ((expr
116 (cond
117 ((equal (stp:namespace-uri node) *xsl*)
118 (let ((sym (find-symbol (stp:local-name node) :xuriella)))
119 (cond
120 (sym
121 (parse-instruction/xsl-element sym node))
122 (*forwards-compatible-p*
123 (parse-fallback-children node))
125 (xslt-error "undefined instruction: ~A"
126 (stp:local-name node))))))
127 ((find (stp:namespace-uri node)
128 *extension-namespaces*
129 :test #'equal)
130 (let ((extension (find-extension-element
131 (stp:local-name node)
132 (stp:namespace-uri node))))
133 (if extension
134 (funcall (extension-element-parser extension) node)
135 (parse-fallback-children node))))
137 (parse-instruction/literal-element node))))
138 (parent (stp:parent node)))
139 (if (and (equal (stp:base-uri node) (stp:base-uri parent))
140 (equal (stp:namespace-uri parent) *xsl*)
141 (find-symbol (stp:local-name parent) :xuriella))
142 expr
143 `(xsl:with-base-uri ,(stp:base-uri node)
144 ,expr))))
145 (stp:text
146 `(xsl:text ,(stp:data node)))))
148 (defun parse-instruction/literal-element (node)
149 (let ((extensions '()))
150 (stp:with-attributes ((eep "extension-element-prefixes" *xsl*))
151 node
152 (dolist (prefix (words (or eep "")))
153 (when (equal prefix "#default")
154 (setf prefix nil))
155 (push (or (stp:find-namespace prefix node)
156 (xslt-error "namespace not found: ~A" prefix))
157 extensions)))
158 (if (find (stp:namespace-uri node) extensions :test #'equal)
159 ;; oops, this isn't a literal result element after all
160 (parse-fallback-children node)
161 (let ((le
162 `(xsl:literal-element
163 (,(stp:local-name node)
164 ,(stp:namespace-uri node)
165 ,(stp:namespace-prefix node))
166 (xsl:use-attribute-sets
167 ,(stp:attribute-value node "use-attribute-sets" *xsl*))
168 ,@(loop
169 for a in (stp:list-attributes node)
170 for xslp = (equal (stp:namespace-uri a) *xsl*)
171 when xslp
172 do (unless (find (stp:local-name a)
173 '("version"
174 "extension-element-prefixes"
175 "exclude-result-prefixes"
176 "use-attribute-sets")
177 :test #'equal)
178 (xslt-error
179 "unknown attribute on literal result element: ~A"
180 (stp:local-name a)))
181 else
182 collect `(xsl:literal-attribute
183 (,(stp:local-name a)
184 ,(stp:namespace-uri a)
185 ,(stp:namespace-prefix a))
186 ,(stp:value a)))
187 ,@ (let ((*extension-namespaces*
188 (append extensions *extension-namespaces*)))
189 (parse-body node))))
190 (version (stp:attribute-value node "version" *xsl*)))
191 (when extensions
192 (setf le
193 `(xsl:with-extension-namespaces ,extensions
194 (xsl:with-excluded-namespaces ,extensions
195 ,le))))
196 (when version
197 (setf le
198 `(xsl:with-version ,version
199 ,le)))
200 le))))
202 (defun parse-fallback-children (node)
203 (let ((fallbacks
204 (loop
205 for fallback in (stp:filter-children (of-name "fallback") node)
206 do (only-with-attributes () fallback)
207 append (parse-body fallback))))
208 (if fallbacks
209 `(progn ,@fallbacks)
210 `(xsl:terminate
211 (xsl:text
212 "no fallback children in unknown element using forwards compatible processing")))))
214 (defmacro define-instruction-parser (name (node-var) &body body)
215 `(progn
216 (setf (gethash ,(symbol-name name) *builtin-instructions*) t)
217 (defmethod parse-instruction/xsl-element
218 ((.name. (eql ',name)) ,node-var)
219 (declare (ignore .name.))
220 ,@body)))
222 (define-instruction-parser |fallback| (node)
223 (only-with-attributes () node
224 '(progn)))
226 (define-instruction-parser |apply-templates| (node)
227 (only-with-attributes (select mode) node
228 (multiple-value-bind (decls rest)
229 (loop
230 for i from 0
231 for cons on (stp:filter-children
232 (lambda (node)
233 (or (typep node 'stp:element)
234 (xslt-error "non-element in apply-templates")))
235 node)
236 for (child . nil) = cons
237 while (namep child "sort")
238 collect (parse-sort child) into decls
239 finally (return (values decls cons)))
240 `(xsl:apply-templates
241 (:select ,select :mode ,mode)
242 (declare ,@decls)
243 ,@(mapcar (lambda (clause)
244 (unless (namep clause "with-param")
245 (xslt-error "undefined instruction: ~A"
246 (stp:local-name clause)))
247 (parse-param clause))
248 rest)))))
250 (define-instruction-parser |apply-imports| (node)
251 (only-with-attributes () node)
252 (assert-no-body node)
253 `(xsl:apply-imports))
255 (define-instruction-parser |call-template| (node)
256 (only-with-attributes (name) node
257 `(xsl:call-template
258 ,name ,@(stp:map-children 'list
259 (lambda (clause)
260 (if (namep clause "with-param")
261 (parse-param clause)
262 (xslt-error "undefined instruction: ~A"
263 (stp:local-name clause))))
264 node))))
266 (define-instruction-parser |if| (node)
267 (only-with-attributes (test) node
268 `(when ,test
269 ,@(parse-body node))))
271 (define-instruction-parser |choose| (node)
272 (let ((whenp nil))
273 (prog1
274 (only-with-attributes () node
275 `(cond
276 ,@(stp:map-children 'list
277 (lambda (clause)
278 (cond
279 ((namep clause "when")
280 (setf whenp t)
281 (only-with-attributes (test) clause
282 `(,test
283 ,@(parse-body clause))))
284 ((namep clause "otherwise")
285 `(t ,@(parse-body clause)))
287 (xslt-error "invalid <choose> clause: ~A"
288 (stp:local-name clause)))))
289 node)))
290 (unless whenp
291 (xslt-error "<choose> without <when>")))))
293 (define-instruction-parser |element| (node)
294 (only-with-attributes (name namespace use-attribute-sets) node
295 `(xsl:element (,name :namespace ,namespace)
296 (xsl:use-attribute-sets ,use-attribute-sets)
297 ,@(parse-body node))))
299 (define-instruction-parser |attribute| (node)
300 (only-with-attributes (name namespace) node
301 `(xsl:attribute (,name :namespace ,namespace)
302 ,@(parse-body node))))
304 (defun boolean-or-error (str)
305 (cond
306 ((equal str "yes")
308 ((or (null str) (equal str "no"))
309 nil)
311 (xslt-error "not a boolean: ~A" str))))
313 (define-instruction-parser |text| (node)
314 (only-with-attributes (select disable-output-escaping) node
315 (when (xpath:evaluate "boolean(*)" node)
316 (xslt-error "non-text found in xsl:text"))
317 (if (boolean-or-error disable-output-escaping)
318 `(xsl:unescaped-text ,(stp:string-value node))
319 `(xsl:text ,(stp:string-value node)))))
321 (define-instruction-parser |comment| (node)
322 (only-with-attributes () node
323 `(xsl:comment ,@(parse-body node))))
325 (define-instruction-parser |processing-instruction| (node)
326 (only-with-attributes (name) node
327 `(xsl:processing-instruction ,name
328 ,@(parse-body node))))
330 (defun assert-no-body (node)
331 (when (stp:list-children node)
332 (xslt-error "no child nodes expected in ~A" (stp:local-name node))))
334 (define-instruction-parser |value-of| (node)
335 (only-with-attributes (select disable-output-escaping) node
336 (assert-no-body node)
337 (if (boolean-or-error disable-output-escaping)
338 `(xsl:unescaped-value-of ,select)
339 `(xsl:value-of ,select))))
341 (define-instruction-parser |copy-of| (node)
342 (only-with-attributes (select) node
343 (assert-no-body node)
344 `(xsl:copy-of ,select)))
346 (define-instruction-parser |copy| (node)
347 (only-with-attributes (use-attribute-sets) node
348 `(xsl:copy
349 (xsl:use-attribute-sets ,use-attribute-sets)
350 ,@(parse-body node))))
352 (define-instruction-parser |variable| (node)
353 (xslt-error "unhandled xsl:variable"))
355 (define-instruction-parser |for-each| (node)
356 (only-with-attributes (select) node
357 (multiple-value-bind (decls body-position)
358 (loop
359 for i from 0
360 for child in (stp:list-children node)
361 while (namep child "sort")
362 collect (parse-sort child) into decls
363 finally (return (values decls i)))
364 `(xsl:for-each ,select
365 (declare ,@decls)
366 ,@(parse-body node body-position)))))
368 (defun parse-sort (node)
369 (only-with-attributes (select lang data-type order case-order) node
370 (assert-no-body node)
371 `(sort :select ,select
372 :lang ,lang
373 :data-type ,data-type
374 :order ,order
375 :case-order ,case-order)))
377 (define-instruction-parser |message| (node)
378 (only-with-attributes (terminate) node
379 (if (boolean-or-error terminate)
380 `(xsl:terminate ,@(parse-body node))
381 `(xsl:message ,@(parse-body node)))))
383 (define-instruction-parser |number| (node)
384 (only-with-attributes (level count from value format lang letter-value
385 grouping-separator grouping-size)
386 node
387 (assert-no-body node)
388 `(xsl:number :level ,level
389 :count ,count
390 :from ,from
391 :value ,value
392 :format ,format
393 :lang ,lang
394 :letter-value ,letter-value
395 :grouping-separator ,grouping-separator
396 :grouping-size ,grouping-size)))