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
(xpath:make-pattern-matcher
* count env
)))
37 (from (and from
(xpath:make-pattern-matcher
* 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 pattern-thunk-matches-p (pattern-thunk node
)
76 (xpath:matching-value pattern-thunk node
))
78 (defun ancestors-using-count-and-from (node count from
)
81 (funcall (xpath::axis-function
:ancestor-or-self
) node
))))
82 (remove-if-not (lambda (ancestor)
83 (pattern-thunk-matches-p count ancestor
))
87 when
(pattern-thunk-matches-p from a
)
90 finally
(return ancestors
))
93 (defun node-position-among-siblings (node count
)
95 (count-if (lambda (sibling)
96 (pattern-thunk-matches-p count sibling
))
98 (funcall (xpath::axis-function
:preceding-sibling
) node
)))))
100 (defun node-type (node)
101 (dolist (type '(:element
106 :processing-instruction
108 (when (xpath-protocol:node-type-p node type
)
111 (defun compute-number-list (level node count from
)
114 (let ((uri (xpath-protocol:namespace-uri node
))
115 (lname (xpath-protocol:local-name node
))
116 (node-type (node-type node
)))
117 (lambda (pattern-node)
118 (if (if (eq node-type
:element
)
119 (and (xpath-protocol:node-type-p pattern-node
:element
)
120 (equal (xpath-protocol:namespace-uri pattern-node
)
122 (equal (xpath-protocol:local-name pattern-node
)
124 (xpath-protocol:node-type-p pattern-node node-type
))
128 ((equal level
"single")
129 (let ((ancestor (car (ancestors-using-count-and-from node count from
))))
131 (list (node-position-among-siblings ancestor count
))
133 ((equal level
"multiple")
134 (mapcar (lambda (ancestor)
135 (node-position-among-siblings ancestor count
))
137 (ancestors-using-count-and-from node count from
))))
139 (destructuring-bind (root)
140 (xpath::force
(funcall (xpath::axis-function
:root
) node
))
141 (let ((nodes (xpath::force
143 (xpath::subpipe-before
145 (funcall (xpath::axis-function
:descendant-or-self
) root
))
149 for
(current . rest
) on nodes
150 when
(pattern-thunk-matches-p from current
)
156 count
(pattern-thunk-matches-p count n
))))))
158 (xslt-error "invalid number level: ~A" level
))))
160 (xpath::deflexer
(format-lexer :ignore-whitespace nil
)
161 ;; zzz just enough unicode "support" here to pass the tests
162 (#.
(format nil
"([a-zA-Z0-9~A]+)" (code-char 945)) (x) (values :format x
))
163 (#.
(format nil
"([^a-zA-Z0-9~A]+)" (code-char 945)) (x) (values :text x
)))
165 (defun format-number-token (str n
)
169 ;; zzz just enough unicode "support" here to pass the tests
170 (equal str
#.
(string (code-char 945))))
171 (let ((start (char-code (elt str
0)))
172 (greekp (equal str
#.
(string (code-char 945)))))
174 (xslt-error "cannot format zero"))
176 (with-output-to-string (r)
179 for
(rest digit
) = (multiple-value-list
185 (write-char (code-char (+ start digit
)) r
))
187 (write-char (code-char (+ start digit
)) r
)
192 (format nil
"~(~@R~)" n
)))
196 (format nil
"~@R" n
)))
198 (unless (cl-ppcre:all-matches
"^0*1$" str
)
199 ;; unsupported format
201 (format nil
"~v,'0D" (length str
) n
))))
203 (defun group-numbers (str separator size stream
)
206 for i from
(1- (length str
)) downto
0
208 (write-char c stream
)
209 (when (and (zerop (mod i size
)) (plusp i
))
210 (write-string separator stream
))))
212 ;;; fixme: unicode support
213 (defun format-number-list
214 (list format lang letter-value grouping-separator grouping-size
)
215 (declare (ignore lang letter-value
))
216 (if (some #'xpath
::nan-p list
)
218 (multiple-value-bind (prefix pairs suffix
)
219 (parse-number-format format
)
220 (with-output-to-string (s)
221 (write-string prefix s
)
223 for
(separator . subformat
) in pairs
225 for formatted
= (format-number-token subformat n
)
228 (write-string separator s
))
229 (if (and grouping-separator
231 (group-numbers formatted
235 (write-string formatted s
)))
236 (write-string suffix s
)))))
238 (defun parse-number-format (format)
239 (let ((lexer (format-lexer format
))
245 (multiple-value-bind (type str
) (funcall lexer
)
251 (setf current-text str
)
254 (push (cons (if conses
255 (or current-text
".")
259 (setf current-text nil
)))))
261 (setf suffix current-text
))
263 (setf conses
(list (cons nil
"1"))))
264 (let* ((tail-cons (car conses
))
265 (tail (if (car tail-cons
)
267 (push (cons "." (cdr tail-cons
)) conses
))))
268 (setf conses
(nreverse conses
))
269 (setf (cdr tail
) tail
))
270 (values prefix conses suffix
)))