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
10 ;;; * Redistributions of source code must retain the above copyright
11 ;;; notice, this list of conditions and the following disclaimer.
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.
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 (define-instruction xsl
:number
(args env
)
33 (destructuring-bind (&key level count from value format lang letter-value
34 grouping-separator grouping-size
)
36 (let ((count (and count
(compile-pattern count env
)))
37 (from (and from
(compile-pattern from env
)))
38 (value (and value
(compile-xpath value env
)))
39 (format (compile-avt (or format
"1") env
))
40 (lang (compile-avt (or lang
"") env
))
41 (letter-value (compile-avt (or letter-value
"foo") env
))
43 (and grouping-separator
(compile-avt grouping-separator env
)))
44 (grouping-size (and grouping-size
(compile-avt grouping-size env
))))
46 (let ((value (when value
49 (funcall value ctx
))))
52 (round (xpath::xnum-round x
))))))
53 (format (funcall format ctx
))
54 (lang (funcall lang ctx
))
55 (letter-value (funcall letter-value ctx
))
56 (grouping-separator (when grouping-separator
57 (funcall grouping-separator ctx
)))
58 (grouping-size (when grouping-size
60 (funcall grouping-size ctx
)))))
65 (compute-number-list (or level
"single")
66 (xpath::context-node ctx
)
75 (defun compile-pattern (str env
)
79 ,@(mapcar #'naive-pattern-expression
(parse-pattern str
))))
82 (defun pattern-thunk-matches-p (pattern-thunk node
)
84 (xpath:all-nodes
(funcall pattern-thunk
(xpath:make-context node
)))))
86 (defun ancestors-using-count-and-from (node count from
)
89 (funcall (xpath::axis-function
:ancestor-or-self
) node
))))
90 (remove-if-not (lambda (ancestor)
91 (pattern-thunk-matches-p count ancestor
))
95 when
(pattern-thunk-matches-p from a
)
98 finally
(return ancestors
))
101 (defun node-position-among-siblings (node count
)
103 (count-if (lambda (sibling)
104 (pattern-thunk-matches-p count sibling
))
106 (funcall (xpath::axis-function
:preceding-sibling
) node
)))))
108 (defun node-type (node)
109 (dolist (type '(:element
114 :processing-instruction
116 (when (xpath-protocol:node-type-p node type
)
119 (defun compute-number-list (level node count from
)
122 (let ((uri (xpath-protocol:namespace-uri node
))
123 (lname (xpath-protocol:local-name node
))
124 (node-type (node-type node
)))
126 (let ((ctx-node (xpath:context-node ctx
)))
127 (xpath-sys:make-node-set
128 (if (if (eq node-type
:element
)
129 (and (xpath-protocol:node-type-p ctx-node
:element
)
130 (equal (xpath-protocol:namespace-uri ctx-node
)
132 (equal (xpath-protocol:local-name ctx-node
)
134 (xpath-protocol:node-type-p ctx-node node-type
))
138 ((equal level
"single")
139 (let ((ancestor (car (ancestors-using-count-and-from node count from
))))
141 (list (node-position-among-siblings ancestor count
))
143 ((equal level
"multiple")
144 (mapcar (lambda (ancestor)
145 (node-position-among-siblings ancestor count
))
147 (ancestors-using-count-and-from node count from
))))
149 (destructuring-bind (root)
150 (xpath::force
(funcall (xpath::axis-function
:root
) node
))
151 (let ((nodes (xpath::force
153 (xpath::subpipe-before
155 (funcall (xpath::axis-function
:descendant-or-self
) root
))
159 for
(current . rest
) on nodes
160 when
(pattern-thunk-matches-p from current
)
166 count
(pattern-thunk-matches-p count n
))))))
168 (xslt-error "invalid number level: ~A" level
))))
170 (xpath::deflexer
(format-lexer :ignore-whitespace nil
)
171 ;; zzz just enough unicode "support" here to pass the tests
172 (#.
(format nil
"([a-zA-Z0-9~A]+)" (code-char 945)) (x) (values :format x
))
173 (#.
(format nil
"([^a-zA-Z0-9~A]+)" (code-char 945)) (x) (values :text x
)))
175 (defun format-number-token (str n
)
179 ;; zzz just enough unicode "support" here to pass the tests
180 (equal str
#.
(string (code-char 945))))
181 (let ((start (char-code (elt str
0)))
182 (greekp (equal str
#.
(string (code-char 945)))))
184 (xslt-error "cannot format zero"))
186 (with-output-to-string (r)
189 for
(rest digit
) = (multiple-value-list
195 (write-char (code-char (+ start digit
)) r
))
197 (write-char (code-char (+ start digit
)) r
)
202 (format nil
"~(~@R~)" n
)))
206 (format nil
"~@R" n
)))
208 (unless (cl-ppcre:all-matches
"^0*1$" str
)
209 ;; unsupported format
211 (format nil
"~v,'0D" (length str
) n
))))
213 (defun group-numbers (str separator size stream
)
216 for i from
(1- (length str
)) downto
0
218 (write-char c stream
)
219 (when (and (zerop (mod i size
)) (plusp i
))
220 (write-string separator stream
))))
222 ;;; fixme: unicode support
223 (defun format-number-list
224 (list format lang letter-value grouping-separator grouping-size
)
225 (declare (ignore lang letter-value
))
226 (if (some #'xpath
::nan-p list
)
228 (multiple-value-bind (prefix pairs suffix
)
229 (parse-number-format format
)
230 (with-output-to-string (s)
231 (write-string prefix s
)
233 for
(separator . subformat
) in pairs
235 for formatted
= (format-number-token subformat n
)
238 (write-string separator s
))
239 (if (and grouping-separator
241 (group-numbers formatted
245 (write-string formatted s
)))
246 (write-string suffix s
)))))
248 (defun parse-number-format (format)
249 (let ((lexer (format-lexer format
))
255 (multiple-value-bind (type str
) (funcall lexer
)
261 (setf current-text str
)
264 (push (cons (if conses
265 (or current-text
".")
269 (setf current-text nil
)))))
271 (setf suffix current-text
))
273 (setf conses
(list (cons nil
"1"))))
274 (let* ((tail-cons (car conses
))
275 (tail (if (car tail-cons
)
277 (push (cons "." (cdr tail-cons
)) conses
))))
278 (setf conses
(nreverse conses
))
279 (setf (cdr tail
) tail
))
280 (values prefix conses suffix
)))