| 1 |
;; SXPath processor for string representation of W3C XPath/XPointer expressions |
| 2 |
; $Id: txpath.scm,v 1.1 2003-07-22 11:22:11 shirok Exp $: |
| 3 |
; |
| 4 |
; This software is in Public Domain. |
| 5 |
; IT IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND. |
| 6 |
; |
| 7 |
; Please send bug reports and comments to: |
| 8 |
; [email protected] Kirill Lisovsky |
| 9 |
; [email protected] Dmitry Lizorkin |
| 10 |
; |
| 11 |
; XPointer's points and ranges are NOT implemented |
| 12 |
; |
| 13 |
; Full XPath Core Function Library is now supported. That is: |
| 14 |
; 4.1 Node Set Functions |
| 15 |
; number last() |
| 16 |
; number position() |
| 17 |
; number count(node-set) |
| 18 |
; node-set id(object) |
| 19 |
; string local-name(node-set?) |
| 20 |
; string namespace-uri(node-set?) |
| 21 |
; string name(node-set?) |
| 22 |
; 4.2 String Functions |
| 23 |
; string string(object?) |
| 24 |
; string concat(string, string, string*) |
| 25 |
; boolean starts-with(string, string) |
| 26 |
; boolean contains(string, string) |
| 27 |
; string substring-before(string, string) |
| 28 |
; string substring-after(string, string) |
| 29 |
; string substring(string, number, number?) |
| 30 |
; number string-length(string?) |
| 31 |
; string normalize-space(string?) |
| 32 |
; string translate(string, string, string) |
| 33 |
; 4.3 Boolean Functions |
| 34 |
; boolean boolean(object) |
| 35 |
; boolean not(boolean) |
| 36 |
; boolean true() |
| 37 |
; boolean false() |
| 38 |
; boolean lang(string) |
| 39 |
; 4.4 Number Functions |
| 40 |
; number number(object?) |
| 41 |
; number sum(node-set) |
| 42 |
; number floor(number) |
| 43 |
; number ceiling(number) |
| 44 |
; number round(number) |
| 45 |
|
| 46 |
;========================================================================= |
| 47 |
; Errors handling |
| 48 |
|
| 49 |
(define (sxml:xpointer-parse-error . text) |
| 50 |
(apply cerr (append (list nl "XPointer parser error: ") text (list nl))) |
| 51 |
#f) |
| 52 |
|
| 53 |
; A warning message for grammar features which are not supported by this |
| 54 |
; implementation |
| 55 |
(define (sxml:xpointer-parse-warning . text) |
| 56 |
(apply cerr (append (list nl "XPointer parser warning: ") text (list nl)))) |
| 57 |
|
| 58 |
; Runtime errors handler (unbound variable, bad argument, etc). |
| 59 |
; It may be re-defined (say, like a warning) without 'exit', and evaluation will |
| 60 |
; be continued. |
| 61 |
; In this case, a default value (usually empty nodeset or 0) is returned by |
| 62 |
; a sub-expression which caused an XPointer runtime error. |
| 63 |
(define (sxml:xpointer-runtime-error . text) |
| 64 |
(apply cerr (append (list nl "XPointer runtime error: ") text (list nl))) |
| 65 |
(exit -1)) |
| 66 |
|
| 67 |
;========================================================================= |
| 68 |
; Low level parsing functions |
| 69 |
; The XPoiner path is represented as a list of chars |
| 70 |
|
| 71 |
; A list of whitespace characters |
| 72 |
(define sxml:whitespace '(#\space #\return #\newline #\tab)) |
| 73 |
|
| 74 |
; A sxml:whitespace or () <> [] : / + * , = | ! " ' @ $ |
| 75 |
(define sxml:delimiter (append sxml:whitespace |
| 76 |
'(#\( #\) #\< #\> #\[ #\] #\: #\/ #\+ |
| 77 |
#\* #\, #\= #\| #\! #\" #\' #\@ #\$))) |
| 78 |
|
| 79 |
; A list of characters a NCName cannot start with |
| 80 |
(define (sxml:non-first? ch) |
| 81 |
(or (char-numeric? ch) |
| 82 |
(memv ch sxml:delimiter) |
| 83 |
(memv ch '(#\. #\-)))) |
| 84 |
|
| 85 |
; The function reads a whitespace , production [3] (S) in XML Rec. |
| 86 |
; path - xpointer path string as a list of chars |
| 87 |
; It returns a new path |
| 88 |
(define (sxml:skip-ws path) |
| 89 |
(if (or (null? path) |
| 90 |
(not (memv (car path) sxml:whitespace))) |
| 91 |
path |
| 92 |
(sxml:skip-ws (cdr path)))) |
| 93 |
|
| 94 |
;------------------------------------------------ |
| 95 |
; These two functions read expected information from the path |
| 96 |
|
| 97 |
; Whether the path begins with a 'str' (starting whitespaces are ignored) |
| 98 |
; str - a string to match |
| 99 |
; path - an xpointer path represented as a list of chars |
| 100 |
; char-list - an optional argument. If this argument is supplied, a 'str' |
| 101 |
; pattern must be followed by a character from a 'char-list' |
| 102 |
; If 'str' is really in the beginning of path, a new path is returned |
| 103 |
; Otherwise, function returns #f (path remains unchanged) |
| 104 |
(define (sxml:parse-check str path . char-list) |
| 105 |
(let loop ((lst (string->list str)) |
| 106 |
(p (sxml:skip-ws path))) |
| 107 |
(cond |
| 108 |
((null? lst) |
| 109 |
(if |
| 110 |
(or (null? p) (null? char-list) (memv (car p) (car char-list))) |
| 111 |
p |
| 112 |
#f)) |
| 113 |
((null? p) #f) |
| 114 |
((char=? (car lst) (car p)) |
| 115 |
(loop (cdr lst) (cdr p))) |
| 116 |
(else #f)))) |
| 117 |
|
| 118 |
; Checks whether the PATH starts with a sequence of strings (possibly |
| 119 |
; separated by a whitespace) from STR-SEQ |
| 120 |
; Returns a new PATH (match successful) or #f (otherwise) |
| 121 |
(define (sxml:parse-check-sequence str-seq path . char-list) |
| 122 |
(let ((char-list (if (null? char-list) #f (car char-list)))) |
| 123 |
(let loop ((str-seq str-seq) |
| 124 |
(path path)) |
| 125 |
(cond |
| 126 |
((null? str-seq) path) ; successful match |
| 127 |
((if char-list |
| 128 |
(sxml:parse-check (car str-seq) path char-list) |
| 129 |
(sxml:parse-check (car str-seq) path)) |
| 130 |
=> (lambda (new-path) |
| 131 |
(loop (cdr str-seq) new-path))) |
| 132 |
(else #f))))) ; unsuccessful match |
| 133 |
|
| 134 |
; Similar to the 'parse-check' function. But this function also has a side |
| 135 |
; effect. It displays an error message if the 'str' doesn't match the beginning |
| 136 |
; of 'path'. |
| 137 |
(define (sxml:parse-assert str path) |
| 138 |
(let loop ((lst (string->list str)) |
| 139 |
(p (sxml:skip-ws path))) |
| 140 |
(cond |
| 141 |
((null? lst) p) |
| 142 |
((null? p) |
| 143 |
(sxml:xpointer-parse-error |
| 144 |
"unexpected end of XPointer path. " |
| 145 |
"Expected - \"" str "\", given - \"" (list->string path) "\"")) |
| 146 |
((char=? (car lst) (car p)) (loop (cdr lst) (cdr p))) |
| 147 |
(else |
| 148 |
(sxml:xpointer-parse-error |
| 149 |
"expected - \"" str "\", given - \"" (list->string path) "\""))))) |
| 150 |
|
| 151 |
|
| 152 |
;------------------------------------------------ |
| 153 |
; NCName readers |
| 154 |
|
| 155 |
; Reads a NCName, taking into account that whitespaces and characters: |
| 156 |
; ( ) < > [ ] : / + * , = | ! " ' @ $ |
| 157 |
; may not be used in it. |
| 158 |
; Moreover, its first character can't be: . - or a digit |
| 159 |
; The result: (list ncname new-path) |
| 160 |
; or #f |
| 161 |
; ncname - NCName represented as a string |
| 162 |
; If there is no NCName in the current position of the path, then an error |
| 163 |
; message is displayed and #f is returned |
| 164 |
(define (sxml:parse-ncname path) |
| 165 |
(let((path (sxml:skip-ws path))) |
| 166 |
(cond |
| 167 |
((null? path) |
| 168 |
(sxml:xpointer-parse-error |
| 169 |
"unexpected end of XPointer path. Expected - NCName")) |
| 170 |
((sxml:non-first? (car path)) |
| 171 |
(sxml:xpointer-parse-error |
| 172 |
"expected - NCName instead of " (car path))) |
| 173 |
(else |
| 174 |
(let loop ((ncname (list (car path))) |
| 175 |
(path (cdr path))) |
| 176 |
(cond |
| 177 |
((null? path) (list (list->string (reverse ncname)) path)) |
| 178 |
((memv (car path) sxml:delimiter) |
| 179 |
(list (list->string (reverse ncname)) path)) |
| 180 |
(else (loop (cons (car path) ncname) (cdr path))))))))) |
| 181 |
|
| 182 |
; Reads a Name production. It is similar to a 'parse-ncname' function. |
| 183 |
; The only difference is that #\: is allowed within a Name |
| 184 |
(define (sxml:parse-name path) |
| 185 |
(let ((path (sxml:skip-ws path))) |
| 186 |
(cond |
| 187 |
((null? path) |
| 188 |
(sxml:xpointer-parse-error |
| 189 |
"unexpected end of XPointer path. Expected - Name")) |
| 190 |
((and (sxml:non-first? (car path)) |
| 191 |
(not (char=? (car path) #\:))) |
| 192 |
(sxml:xpointer-parse-error "expected - Name instead of " (car path))) |
| 193 |
(else (let loop ((ncname (list (car path))) |
| 194 |
(path (cdr path))) |
| 195 |
(cond |
| 196 |
((null? path) |
| 197 |
(list (list->string (reverse ncname)) path)) |
| 198 |
((and (memv (car path) sxml:delimiter) |
| 199 |
(not (char=? (car path) #\:))) |
| 200 |
(list (list->string (reverse ncname)) path)) |
| 201 |
(else (loop (cons (car path) ncname) (cdr path))))))))) |
| 202 |
|
| 203 |
; The function reads a qualified name (QName) |
| 204 |
; Returns: ( (prefix . local-part) new-path ) |
| 205 |
; or ( local-part new-path ) if there is no prefix |
| 206 |
; if there is not QName in the beginning of the 'path' it calls |
| 207 |
; sxml:xpointer-parse-error |
| 208 |
; prefix, local-part - strings |
| 209 |
; new-path - a list of characters |
| 210 |
(define (sxml:parse-qname path) |
| 211 |
(and-let* ((r1 (sxml:parse-ncname path))) |
| 212 |
(let ((first (car r1)) |
| 213 |
(path2 (cadr r1))) |
| 214 |
(cond |
| 215 |
((null? path2) (list first path2)) |
| 216 |
((not (char=? (car path2) #\:)) (list first path2)) |
| 217 |
((null? (cdr path2)) |
| 218 |
(sxml:xpointer-parse-error "no local part of a qualified name")) |
| 219 |
((char=? (cadr path2) #\:) (list first path2)) |
| 220 |
(else (and-let* ((r2 (sxml:parse-ncname (cdr path2)))) |
| 221 |
(list (cons first (car r2)) (cadr r2))) |
| 222 |
))))) |
| 223 |
|
| 224 |
;------------------------------------------------ |
| 225 |
; Parsers for data of basic types |
| 226 |
|
| 227 |
; Reads a natural number: |
| 228 |
; [1-9] [0-9]* |
| 229 |
; The result: (list number new-path) or #f |
| 230 |
(define (sxml:parse-natural path) |
| 231 |
(let ((path (sxml:skip-ws path))) |
| 232 |
(cond |
| 233 |
((null? path) |
| 234 |
(sxml:xpointer-parse-error |
| 235 |
"unexpected end of XPointer path. Expected - number")) |
| 236 |
((or (char<? (car path) #\1) (char>? (car path) #\9)) |
| 237 |
(sxml:xpointer-parse-error "expected - number instead of " (car path))) |
| 238 |
(else (let loop ((res (- (char->integer (car path)) |
| 239 |
48)) ; (char->integer #\0) |
| 240 |
(path (cdr path))) |
| 241 |
(cond |
| 242 |
((null? path) (list res path)) |
| 243 |
((char-numeric? (car path)) |
| 244 |
(loop (+ (* res 10) (- (char->integer (car path)) |
| 245 |
48)) ; (char->integer #\0) |
| 246 |
(cdr path))) |
| 247 |
(else (list res path)))))))) |
| 248 |
|
| 249 |
; Reads a Literal ([29] in XPath specification) |
| 250 |
; [29] Literal ::= '"' [^"]* '"' |
| 251 |
; | "'" [^']* "'" |
| 252 |
; The result: (string new-path) or #f |
| 253 |
(define (sxml:parse-literal path) |
| 254 |
(let ((ch (if (sxml:parse-check "\"" path) #\" #\'))) |
| 255 |
(let loop ((res '()) |
| 256 |
(path (sxml:parse-assert (if (char=? ch #\") "\"" "'") |
| 257 |
path))) |
| 258 |
(cond |
| 259 |
((not path) #f) |
| 260 |
((null? path) |
| 261 |
(sxml:parse-assert (if (char=? ch #\") "\"" "'") |
| 262 |
path) |
| 263 |
#f) |
| 264 |
((char=? (car path) ch) |
| 265 |
(list (list->string (reverse res)) |
| 266 |
(cdr path))) |
| 267 |
(else (loop (cons (car path) res) (cdr path))))))) |
| 268 |
|
| 269 |
; Reads a Number ([30]-[31] in XPath specification) |
| 270 |
; [30] Number ::= Digits ('.' Digits?)? |
| 271 |
; | '.' Digits |
| 272 |
; [31] Digits ::= [0-9]+ |
| 273 |
; The result: (number new-path) or #f |
| 274 |
(define (sxml:parse-number path) |
| 275 |
(define (digits path) |
| 276 |
(let loop ((n-lst '()) |
| 277 |
(path path)) |
| 278 |
(cond |
| 279 |
((and (null? path) (null? n-lst)) |
| 280 |
(sxml:xpointer-parse-error |
| 281 |
"unexpected end of XPointer path. Expected - number")) |
| 282 |
((null? path) (list n-lst path)) |
| 283 |
((and (or (char<? (car path) #\0) (char>? (car path) #\9)) |
| 284 |
(null? n-lst)) |
| 285 |
(sxml:xpointer-parse-error "expected - number instead of " (car path))) |
| 286 |
((or (char<? (car path) #\0) (char>? (car path) #\9)) |
| 287 |
(list n-lst path)) |
| 288 |
(else |
| 289 |
(loop (cons (- (char->integer (car path)) (char->integer #\0)) n-lst) |
| 290 |
(cdr path)))))) |
| 291 |
|
| 292 |
(let ((path (sxml:skip-ws path))) |
| 293 |
(cond |
| 294 |
((null? path) |
| 295 |
(sxml:xpointer-parse-error |
| 296 |
"unexpected end of XPointer path. Expected - number")) |
| 297 |
((char=? (car path) #\.) |
| 298 |
(and-let* ((lst (digits (cdr path)))) |
| 299 |
(let rpt ((res 0) |
| 300 |
(n-lst (car lst)) |
| 301 |
(path (cadr lst))) |
| 302 |
(if(null? n-lst) |
| 303 |
(list (/ res 10) path) |
| 304 |
(rpt (+ (/ res 10) (car n-lst)) |
| 305 |
(cdr n-lst) |
| 306 |
path))))) |
| 307 |
(else (and-let* ((lst (digits path))) |
| 308 |
(let loop ((num1 0) |
| 309 |
(n-lst (reverse (car lst))) |
| 310 |
(path (cadr lst))) |
| 311 |
(if (null? n-lst) |
| 312 |
(cond |
| 313 |
((null? path) (list num1 path)) |
| 314 |
((not (char=? (car path) #\.)) (list num1 path)) |
| 315 |
(else |
| 316 |
(and-let* ((lst2 (digits (cdr path)))) |
| 317 |
(let rpt ((num2 0) |
| 318 |
(n-lst (car lst2)) |
| 319 |
(path (cadr lst2))) |
| 320 |
(if (null? n-lst) |
| 321 |
(list (+ num1 (/ num2 10)) path) |
| 322 |
(rpt (+ (/ num2 10) (car n-lst)) |
| 323 |
(cdr n-lst) |
| 324 |
path)))))) |
| 325 |
(loop (+ (* num1 10) (car n-lst)) |
| 326 |
(cdr n-lst) |
| 327 |
path)))))))) |
| 328 |
|
| 329 |
;========================================================================= |
| 330 |
; Grammar parsing functions |
| 331 |
; All these functions have similar arguments: |
| 332 |
; path - an xpointer path represented as a list of chars |
| 333 |
; ns-binding - declared namespace prefixes (not for all functions) |
| 334 |
; ns-binding = (list (prefix . uri) (prefix . uri) ... ) |
| 335 |
; prefix, uri - strings |
| 336 |
|
| 337 |
;------------------------------------------------ |
| 338 |
; Functions which parse XPath grammar |
| 339 |
|
| 340 |
; Parses an AxisSpecifier production ([5],[6],[13] in XPath specification) |
| 341 |
; [5] AxisSpecifier ::= AxisName '::' |
| 342 |
; | AbbreviatedAxisSpecifier |
| 343 |
; [6] AxisName ::= 'ancestor' |
| 344 |
; | 'ancestor-or-self' |
| 345 |
; | 'attribute' |
| 346 |
; | 'child' |
| 347 |
; | 'descendant' |
| 348 |
; | 'descendant-or-self' |
| 349 |
; | 'following' |
| 350 |
; | 'following-sibling' |
| 351 |
; | 'namespace' |
| 352 |
; | 'parent' |
| 353 |
; | 'preceding' |
| 354 |
; | 'preceding-sibling' |
| 355 |
; | 'self' |
| 356 |
; [13] AbbreviatedAxisSpecifier ::= '@'? |
| 357 |
; |
| 358 |
; The result is represented as a list |
| 359 |
; (list (lambda ...) new-path root-node-required) |
| 360 |
; or #f in case of parse error |
| 361 |
; new-path - a list of chars. It represents the remainder of the source string |
| 362 |
; root-node-required - a boolean value |
| 363 |
; (lambda ...) one of the axis functions |
| 364 |
; If root-node-required = #t, lambda's signature is |
| 365 |
; (lambda (test-pred?) |
| 366 |
; (lambda (root-node) |
| 367 |
; (lambda (nodeset) ... ))) |
| 368 |
; otherwise |
| 369 |
; (lambda (test-pred?) |
| 370 |
; (lambda (nodeset) ... )) |
| 371 |
(define (sxml:parse-axis-specifier path) |
| 372 |
(let loop |
| 373 |
((triples |
| 374 |
`((("ancestor" "::") ,sxml:ancestor #t) |
| 375 |
(("ancestor-or-self" "::") ,sxml:ancestor-or-self #t) |
| 376 |
(("attribute" "::") ,sxml:attribute #f) |
| 377 |
(("child" "::") ,sxml:child #f) |
| 378 |
(("descendant" "::") ,sxml:descendant #f) |
| 379 |
(("descendant-or-self" "::") ,sxml:descendant-or-self #f) |
| 380 |
(("following" "::") ,sxml:following #t) |
| 381 |
(("following-sibling" "::") ,sxml:following-sibling #t) |
| 382 |
(("namespace" "::") ,sxml:namespace #f) |
| 383 |
(("parent" "::") ,sxml:parent #t) |
| 384 |
(("preceding" "::") ,sxml:preceding #t) |
| 385 |
(("preceding-sibling" "::") ,sxml:preceding-sibling #t) |
| 386 |
(("self" "::") ,sxml:filter #f) |
| 387 |
(("@") ,sxml:attribute #f)))) |
| 388 |
(if |
| 389 |
(null? triples) ; a default (child) axis |
| 390 |
(list sxml:child path #f) |
| 391 |
(let rpt ((pattern (caar triples)) |
| 392 |
(p path)) |
| 393 |
(cond |
| 394 |
((null? pattern) ; match is successful |
| 395 |
(list (cadar triples) p (caddar triples))) |
| 396 |
((sxml:parse-check (car pattern) p) |
| 397 |
=> (lambda (new-path) |
| 398 |
(rpt (cdr pattern) new-path))) |
| 399 |
(else ; match unsuccessful |
| 400 |
(loop (cdr triples)))))))) |
| 401 |
|
| 402 |
|
| 403 |
; Parses a NodeTest production |
| 404 |
; ([7],[37] in XPath specification, [11] in XPointer specification) |
| 405 |
; [7] NodeTest ::= NameTest |
| 406 |
; | NodeType '(' ')' |
| 407 |
; | 'processing-instruction' '(' Literal ')' |
| 408 |
; [37] NameTest ::= '*' |
| 409 |
; | NCName ':' '*' |
| 410 |
; | QName |
| 411 |
; [11] NodeType ::= 'comment' |
| 412 |
; | 'text' |
| 413 |
; | 'processing-instruction' |
| 414 |
; | 'node' |
| 415 |
; | 'point' |
| 416 |
; | 'range' |
| 417 |
; The result is: ( (lambda (node) ...) new-path ) or #f |
| 418 |
; #f signals of an error |
| 419 |
; (lambda (node) ...) - a node test function |
| 420 |
(define (sxml:parse-node-test path ns-binding) |
| 421 |
|
| 422 |
(define (brackets path) |
| 423 |
(and-let* ((path (sxml:parse-assert "(" path))) |
| 424 |
(sxml:parse-assert ")" path))) |
| 425 |
(cond |
| 426 |
((sxml:parse-check-sequence '("comment" "(") path) |
| 427 |
=> (lambda (path) |
| 428 |
(and-let* |
| 429 |
((path (sxml:parse-assert ")" path))) |
| 430 |
(list (ntype?? '*COMMENT*) path)))) |
| 431 |
((sxml:parse-check-sequence '("text" "(") path) |
| 432 |
=> (lambda (path) |
| 433 |
(and-let* |
| 434 |
((path (sxml:parse-assert ")" path))) |
| 435 |
(list (ntype?? '*text*) path)))) |
| 436 |
((sxml:parse-check-sequence '("node" "(") path) |
| 437 |
=> (lambda (path) |
| 438 |
(and-let* |
| 439 |
((path (sxml:parse-assert ")" path))) |
| 440 |
(list sxml:node? path)))) |
| 441 |
((sxml:parse-check-sequence '("processing-instruction" "(") path) |
| 442 |
=> (lambda (path) |
| 443 |
(cond |
| 444 |
((sxml:parse-check ")" path) |
| 445 |
(list (lambda (node) |
| 446 |
(and (pair? node) (eq? (car node) '*PI*))) |
| 447 |
(sxml:parse-assert ")" path))) |
| 448 |
(else |
| 449 |
(and-let* |
| 450 |
((lst (sxml:parse-literal path)) |
| 451 |
(name (string->symbol (car lst))) |
| 452 |
(path (sxml:parse-assert ")" (cadr lst)))) |
| 453 |
(list |
| 454 |
(lambda (node) |
| 455 |
(and (pair? node) |
| 456 |
(eq? (car node) '*PI*) |
| 457 |
(equal? (cadr node) name))) |
| 458 |
path)))))) |
| 459 |
((sxml:parse-check-sequence '("point" "(") path) |
| 460 |
=> (lambda (path) |
| 461 |
(and-let* |
| 462 |
((path (sxml:parse-assert ")" path))) |
| 463 |
(sxml:xpointer-parse-warning |
| 464 |
"'point()' NodeTest is not supported by this implementation. " |
| 465 |
"It is defaulted to an predicate which is always false") |
| 466 |
(list (lambda (node) #f) path)))) |
| 467 |
((sxml:parse-check-sequence '("range" "(") path) |
| 468 |
=> (lambda (path) |
| 469 |
(and-let* |
| 470 |
((path (sxml:parse-assert ")" path))) |
| 471 |
(sxml:xpointer-parse-warning |
| 472 |
"'range()' NodeTest is not supported by this implementation. " |
| 473 |
"It is defaulted to an predicate which is always false") |
| 474 |
(list (lambda (node) #f) path)))) |
| 475 |
((sxml:parse-check "*" path) |
| 476 |
(list (ntype?? '*) (sxml:parse-assert "*" path))) |
| 477 |
(else ; NCName ':' '*' | QName |
| 478 |
(and-let* |
| 479 |
((lst (sxml:parse-ncname path))) |
| 480 |
(let ((name (string->symbol (car lst))) |
| 481 |
(path (cadr lst))) |
| 482 |
(if |
| 483 |
(and (not (null? path)) (char=? (car path) #\:)) |
| 484 |
(let ((path (sxml:parse-assert ":" path)) |
| 485 |
(pair (assq name ns-binding))) |
| 486 |
(cond |
| 487 |
((not pair) |
| 488 |
(sxml:xpointer-parse-error "unknown namespace prefix - " name)) |
| 489 |
((and (not (null? path)) (char=? (car path) #\*)) |
| 490 |
(list |
| 491 |
(ntype-namespace-id?? (cdr pair)) |
| 492 |
(sxml:parse-assert "*" path))) |
| 493 |
(else |
| 494 |
(and-let* |
| 495 |
((lst (sxml:parse-ncname path))) |
| 496 |
(list |
| 497 |
(ntype?? (string->symbol |
| 498 |
(string-append (cdr pair) ":" (car lst)))) |
| 499 |
(cadr lst)))))) |
| 500 |
(list (ntype?? name) path))))))) ; just a local name |
| 501 |
|
| 502 |
;------------------------------------------------ |
| 503 |
; Functions which parse XPath grammar, part 2 |
| 504 |
; |
| 505 |
; All these functions return a value which has the same signature: |
| 506 |
; (list (lambda (nodeset root-node context var-binding) ...) |
| 507 |
; path |
| 508 |
; index-required ) |
| 509 |
; or #f |
| 510 |
; #f signals of a parse error (error message is printed as a side effect |
| 511 |
; during parsing) |
| 512 |
; |
| 513 |
; (lambda (nodeset root-node context var-binding) - an SXPath-like |
| 514 |
; function (it transforms a nodeset into a new nodeset) |
| 515 |
; nodeset - a current set of nodes |
| 516 |
; root-node - the root of a document (a singleton nodeset) |
| 517 |
; context - the context of the node; list of two elements - (position size) |
| 518 |
; position - context position (a number) |
| 519 |
; size - context size (a number) |
| 520 |
; path - an XPointer path represented as the list of chars |
| 521 |
; index-required - boolean value: whether an id-index was required "deeper" |
| 522 |
; within the grammar |
| 523 |
|
| 524 |
; Filter nodeset using preds-list as described in XPath rec. 2.4 |
| 525 |
; A helper for sxml:parse-step and sxml:parse-filter-expr |
| 526 |
(define (sxml:xpath-nodeset-filter preds-list nodeset root-node |
| 527 |
var-binding) |
| 528 |
(let rpt ((nodeset nodeset) |
| 529 |
(ps (reverse preds-list))) |
| 530 |
(if (null? ps) |
| 531 |
nodeset |
| 532 |
(let ((size (length nodeset))) |
| 533 |
(let lab ((nset nodeset) |
| 534 |
(res '()) |
| 535 |
(pos 1)) |
| 536 |
(if (null? nset) |
| 537 |
(rpt (reverse res) (cdr ps)) |
| 538 |
(let ((val ((car ps) |
| 539 |
(list (car nset)) |
| 540 |
root-node |
| 541 |
(cons pos size) |
| 542 |
var-binding))) |
| 543 |
(lab (cdr nset) |
| 544 |
(if (if (number? val) |
| 545 |
(= val pos) |
| 546 |
(sxml:boolean val)) |
| 547 |
(cons (car nset) res) |
| 548 |
res) |
| 549 |
(+ pos 1)) |
| 550 |
))))))) |
| 551 |
|
| 552 |
; Parses a Step production |
| 553 |
; ([4xptr] in XPointer specification, [12] in XPath specification) |
| 554 |
; [4xptr] Step ::= AxisSpecifier NodeTest Predicate* |
| 555 |
; | AbbreviatedStep |
| 556 |
; | 'range-to' '(' Expr ')' Predicate* |
| 557 |
; [12] AbbreviatedStep ::= '.' |
| 558 |
; | '..' |
| 559 |
; ATTENTION: 'range-to' FUNCTION IS NOT IMPLEMENTED. INFORMATION REFERRING |
| 560 |
; TO IT IS PARSED AND IGNORED. THIS FUNCTION CALL RESULTS TO AN EMPTY NODESET |
| 561 |
(define (sxml:parse-step path ns-binding) |
| 562 |
(cond |
| 563 |
((sxml:parse-check ".." path) |
| 564 |
(list |
| 565 |
(lambda (nodeset root-node context var-binding) |
| 566 |
(((sxml:parent sxml:node?) root-node) nodeset)) |
| 567 |
(sxml:parse-assert ".." path) |
| 568 |
#f)) |
| 569 |
((sxml:parse-check "." path) |
| 570 |
(list |
| 571 |
(lambda (nodeset root-node context var-binding) |
| 572 |
((sxml:filter sxml:node?) nodeset)) |
| 573 |
(sxml:parse-assert "." path) |
| 574 |
#f)) |
| 575 |
((sxml:parse-check "range-to" path) |
| 576 |
(and-let* |
| 577 |
((path0 (sxml:parse-assert "(" (sxml:parse-assert "range-to" path))) |
| 578 |
(lst (sxml:parse-expr path0 ns-binding)) |
| 579 |
(path (sxml:parse-assert ")" (cadr lst)))) ; check again? |
| 580 |
(let loop ((path path)) |
| 581 |
(cond |
| 582 |
((sxml:parse-check "[" path) |
| 583 |
(and-let* ((lst (sxml:parse-predicate path ns-binding))) |
| 584 |
(loop (cadr lst)))) |
| 585 |
(else ; Predicates are over |
| 586 |
(sxml:xpointer-parse-warning |
| 587 |
"range-to function not implemented. " |
| 588 |
"Defaulting to an empty nodeset") |
| 589 |
(list |
| 590 |
(lambda (nodeset root-node context var-binding) |
| 591 |
'()) |
| 592 |
path |
| 593 |
#f)))))) |
| 594 |
(else (and-let* ((lst (sxml:parse-axis-specifier path))) |
| 595 |
(let ((axis (car lst)) |
| 596 |
(root-node-required (caddr lst))) |
| 597 |
(and-let* ((lst (sxml:parse-node-test (cadr lst) ns-binding))) |
| 598 |
(let ((test (car lst))) |
| 599 |
(let loop ((preds '()) |
| 600 |
(path (cadr lst)) |
| 601 |
(index-required #f)) |
| 602 |
(cond |
| 603 |
((sxml:parse-check "[" path) |
| 604 |
(and-let* ((lst (sxml:parse-predicate path ns-binding))) |
| 605 |
(loop (cons (car lst) preds) |
| 606 |
(cadr lst) |
| 607 |
(or index-required (caddr lst))))) |
| 608 |
; No more predicates |
| 609 |
((null? preds) |
| 610 |
(list |
| 611 |
(lambda (nodeset root-node context var-binding) |
| 612 |
(if root-node-required |
| 613 |
(((axis test) root-node) nodeset) |
| 614 |
((axis test) nodeset))) |
| 615 |
path |
| 616 |
index-required)) |
| 617 |
(else ; More predicates |
| 618 |
(list |
| 619 |
(lambda (nodeset root-node context var-binding) |
| 620 |
(sxml:xpath-nodeset-filter |
| 621 |
preds |
| 622 |
((if root-node-required |
| 623 |
((axis test) root-node) |
| 624 |
(axis test)) |
| 625 |
nodeset) |
| 626 |
root-node var-binding)) |
| 627 |
path |
| 628 |
index-required)) |
| 629 |
))))))))) |
| 630 |
|
| 631 |
; Parses a RelativeLocationPath production ([3],[11] in XPath specification) |
| 632 |
; [3] RelativeLocationPath ::= Step |
| 633 |
; | RelativeLocationPath '/' Step |
| 634 |
; | AbbreviatedRelativeLocationPath |
| 635 |
; [11] AbbreviatedRelativeLocationPath ::= RelativeLocationPath '//' Step |
| 636 |
(define (sxml:parse-relative-location-path path ns-binding) |
| 637 |
(let loop ((funcs '()) |
| 638 |
(path path) |
| 639 |
(index-required #f)) |
| 640 |
(and-let* ((lst (sxml:parse-step path ns-binding))) |
| 641 |
(let ((func (car lst)) |
| 642 |
(path (cadr lst))) |
| 643 |
(cond |
| 644 |
((sxml:parse-check "//" path) |
| 645 |
(loop |
| 646 |
(cons |
| 647 |
(lambda (nodeset root-node context var-binding) |
| 648 |
((sxml:descendant-or-self sxml:node?) nodeset)) |
| 649 |
(cons func funcs)) |
| 650 |
(sxml:parse-assert "//" path) |
| 651 |
(or index-required (caddr lst)))) |
| 652 |
((sxml:parse-check "/" path) |
| 653 |
(loop (cons func funcs) |
| 654 |
(sxml:parse-assert "/" path) |
| 655 |
(or index-required (caddr lst)))) |
| 656 |
; no more steps |
| 657 |
((null? funcs) lst) ; a current step is the only step |
| 658 |
(else ; several steps |
| 659 |
(list |
| 660 |
(lambda (nodeset root-node context var-binding) |
| 661 |
(let rpt ((nset nodeset) |
| 662 |
(fs (reverse (cons func funcs)))) |
| 663 |
(if (null? fs) |
| 664 |
nset |
| 665 |
(rpt ((car fs) |
| 666 |
nset root-node context var-binding) |
| 667 |
(cdr fs))))) |
| 668 |
path |
| 669 |
(or index-required (caddr lst))))))))) |
| 670 |
|
| 671 |
; Parses XPath grammar |
| 672 |
(define (sxml:parse-xpath path ns-binding) |
| 673 |
(and-let* |
| 674 |
((res (sxml:parse-location-path path ns-binding)) |
| 675 |
(path (sxml:skip-ws (cadr res)))) |
| 676 |
(if |
| 677 |
(not (null? path)) |
| 678 |
(sxml:xpointer-parse-error "unexpected - \"" (list->string path) "\"") |
| 679 |
res))) |
| 680 |
|
| 681 |
; Parses a LocationPath production ([1],[2],[10] in XPath specification) |
| 682 |
; [1] LocationPath ::= RelativeLocationPath |
| 683 |
; | AbsoluteLocationPath |
| 684 |
; [2] AbsoluteLocationPath ::= '/' RelativeLocationPath? |
| 685 |
; | AbbreviatedAbsoluteLocationPath |
| 686 |
; [10] AbbreviatedAbsoluteLocationPath ::= '//' RelativeLocationPath |
| 687 |
(define (sxml:parse-location-path path ns-binding) |
| 688 |
(define (nothing? path) |
| 689 |
(let ((path (sxml:skip-ws path))) |
| 690 |
(cond |
| 691 |
((null? path) #t) |
| 692 |
((memv (car path) '(#\| #\+ #\- #\< #\> #\= #\) #\] #\,)) #t) |
| 693 |
((or (sxml:parse-check "mod" path sxml:delimiter) |
| 694 |
(sxml:parse-check "div" path sxml:delimiter) |
| 695 |
(sxml:parse-check "!=" path) |
| 696 |
(sxml:parse-check "and" path sxml:delimiter) |
| 697 |
(sxml:parse-check "or" path sxml:delimiter)) #t) |
| 698 |
(else #f)))) |
| 699 |
(cond |
| 700 |
((sxml:parse-check "//" path) |
| 701 |
(and-let* ((lst (sxml:parse-relative-location-path |
| 702 |
(sxml:parse-assert "//" path) |
| 703 |
ns-binding))) |
| 704 |
(let ((func (car lst)) |
| 705 |
(path (cadr lst)) |
| 706 |
(index-required (caddr lst))) |
| 707 |
(list (lambda (nodeset root-node context var-binding) |
| 708 |
(func |
| 709 |
((sxml:descendant-or-self sxml:node?) root-node) |
| 710 |
root-node context var-binding)) |
| 711 |
path |
| 712 |
index-required)))) |
| 713 |
((sxml:parse-check "/" path) |
| 714 |
(let ((path (sxml:parse-assert "/" path))) |
| 715 |
(if (nothing? path) |
| 716 |
(list (lambda (nodeset root-node context var-binding) |
| 717 |
root-node) |
| 718 |
path |
| 719 |
#f) |
| 720 |
(let ((lst (sxml:parse-relative-location-path path ns-binding))) |
| 721 |
(if (null? lst) |
| 722 |
#f |
| 723 |
(let((func (car lst)) |
| 724 |
(path (cadr lst)) |
| 725 |
(index-required (caddr lst))) |
| 726 |
(list (lambda (nodeset root-node context var-binding) |
| 727 |
(func root-node root-node context var-binding)) |
| 728 |
path |
| 729 |
index-required))))))) |
| 730 |
(else (sxml:parse-relative-location-path path ns-binding)))) |
| 731 |
|
| 732 |
; Parses a Predicate production ([8]-[9] in XPath specification) |
| 733 |
; [8] Predicate ::= '[' PredicateExpr ']' |
| 734 |
; [9] PredicateExpr ::= Expr |
| 735 |
; Note that (according to specification) a Predicate must return a number or |
| 736 |
; a boolean value. However, the return value type is not checked in this |
| 737 |
; function. This is performed in functions that use 'parse-predicate' |
| 738 |
(define (sxml:parse-predicate path ns-binding) |
| 739 |
(and-let* ((path0 (sxml:parse-assert "[" path)) |
| 740 |
(lst (sxml:parse-expr path0 ns-binding)) |
| 741 |
(path (sxml:parse-assert "]" (cadr lst)))) |
| 742 |
(list (car lst) ; func |
| 743 |
path |
| 744 |
(caddr lst) ; index required |
| 745 |
))) |
| 746 |
|
| 747 |
; Parses a VariableReference production ([36] in XPath specification) |
| 748 |
; [36] VariableReference ::= '$' QName |
| 749 |
(define (sxml:parse-variable-reference path ns-binding) |
| 750 |
(and-let* |
| 751 |
((path (sxml:parse-assert "$" path)) |
| 752 |
(lst (sxml:parse-qname path))) |
| 753 |
(let ((name |
| 754 |
(string->symbol |
| 755 |
(if (pair? (car lst)) ; contains a prefix-part |
| 756 |
(string-append (caar lst) ":" (cdar lst)) |
| 757 |
(car lst))))) |
| 758 |
(list (lambda (nodeset root-node context var-binding) |
| 759 |
(cond |
| 760 |
((assoc name var-binding) |
| 761 |
=> cdr) |
| 762 |
(else (sxml:xpointer-runtime-error |
| 763 |
"Unbound variable - " name |
| 764 |
". Defaulting to an empty nodeset") |
| 765 |
'()))) |
| 766 |
(cadr lst) |
| 767 |
#f)))) |
| 768 |
|
| 769 |
; Parses a FunctionCall production ([16],[17],[35] in XPath specification) |
| 770 |
; [16] FunctionCall ::= FunctionName |
| 771 |
; '(' ( Argument ( ',' Argument )* )? ')' |
| 772 |
; [17] Argument ::= Expr |
| 773 |
; [35] FunctionName ::= QName - NodeType |
| 774 |
; ATTENTION: SOME CORE FUNCTIONS ARE NOT SUPPORTED. |
| 775 |
; SUCH A FUNCTION CALL IS DEFAULTED TO AN EMPTY NODESET |
| 776 |
(define (sxml:parse-function-call path ns-binding) |
| 777 |
|
| 778 |
; Parses a list of arguments |
| 779 |
; min - minimal number of arguments |
| 780 |
; max - maximal number of arguments. If max<0, then the number of |
| 781 |
; arguments is not limited from above |
| 782 |
; Result: |
| 783 |
; (funcs new-path index-required) |
| 784 |
; funcs = (func func ...) - functions which evaluate arguments |
| 785 |
(define (sxml:parse-arguments min max path ns-binding) |
| 786 |
(let ((path (sxml:parse-assert "(" path))) |
| 787 |
(cond |
| 788 |
((not path) #f) |
| 789 |
((= max 0) |
| 790 |
(and-let* ((path (sxml:parse-assert ")" path))) |
| 791 |
(list '() path #f))) |
| 792 |
((and (= min 0) (sxml:parse-check ")" path)) |
| 793 |
(list '() (sxml:parse-assert ")" path) #f)) |
| 794 |
(else (let back ((n 1) |
| 795 |
(funcs '()) |
| 796 |
(path path) |
| 797 |
(index-required #f)) |
| 798 |
(and-let* ((lst (sxml:parse-expr path ns-binding))) |
| 799 |
(let ((func (car lst)) |
| 800 |
(path (cadr lst)) |
| 801 |
(bool (or index-required (caddr lst)))) |
| 802 |
(cond |
| 803 |
((= n max) |
| 804 |
(and-let* ((path (sxml:parse-assert ")" path))) |
| 805 |
(list (reverse (cons func funcs)) |
| 806 |
path |
| 807 |
bool))) |
| 808 |
((and (>= n min) (sxml:parse-check ")" path)) |
| 809 |
(list (reverse (cons func funcs)) |
| 810 |
(sxml:parse-assert ")" path) |
| 811 |
bool)) |
| 812 |
(else (and-let* ((path (sxml:parse-assert "," path))) |
| 813 |
(back (+ n 1) |
| 814 |
(cons func funcs) |
| 815 |
path |
| 816 |
bool))))))))))) |
| 817 |
|
| 818 |
(and-let* |
| 819 |
((lst (sxml:parse-qname path))) |
| 820 |
(let ((name (car lst)) ; can be a pair |
| 821 |
(path (cadr lst))) |
| 822 |
(cond |
| 823 |
((equal? name "last") ; it is a 'last' function |
| 824 |
(and-let* |
| 825 |
((lst2 (sxml:parse-arguments 0 0 path ns-binding))) |
| 826 |
(list (lambda (nodeset root-node context var-binding) |
| 827 |
(cdr context)) |
| 828 |
(cadr lst2) |
| 829 |
#f))) |
| 830 |
((equal? name "position") ; it is a 'position' function |
| 831 |
(and-let* |
| 832 |
((lst2 (sxml:parse-arguments 0 0 path ns-binding))) |
| 833 |
(list (lambda (nodeset root-node context var-binding) |
| 834 |
(car context)) |
| 835 |
(cadr lst2) |
| 836 |
#f))) |
| 837 |
((equal? name "count") ; it is a 'count' function |
| 838 |
(and-let* |
| 839 |
((lst2 (sxml:parse-arguments 1 1 path ns-binding))) |
| 840 |
(let ((func (caar lst2)) |
| 841 |
(path (cadr lst2)) |
| 842 |
(index-required (caddr lst2))) |
| 843 |
(list (lambda (nodeset root-node context var-binding) |
| 844 |
(let ((res (func nodeset root-node context var-binding))) |
| 845 |
(cond |
| 846 |
((nodeset? res) (length res)) |
| 847 |
(else |
| 848 |
(sxml:xpointer-runtime-error |
| 849 |
"'count' function - an argument is not a nodeset. " |
| 850 |
"Returning zero") |
| 851 |
0)))) |
| 852 |
path |
| 853 |
index-required)))) |
| 854 |
((equal? name "id") ; it is an 'id' function |
| 855 |
(and-let* |
| 856 |
((lst2 (sxml:parse-arguments 1 1 path ns-binding))) |
| 857 |
(let ((func (caar lst2)) |
| 858 |
(path (cadr lst2))) |
| 859 |
(list |
| 860 |
(lambda (nodeset root-node context var-binding) |
| 861 |
(let* ((id-nset ((sxml:child (ntype?? 'id-index)) |
| 862 |
((sxml:child (ntype?? '@@)) root-node)))) |
| 863 |
(if |
| 864 |
(null? id-nset) ; no id-index |
| 865 |
'() ; ID function returns an empty nodeset |
| 866 |
((sxml:id (cdar id-nset)) |
| 867 |
(func nodeset root-node context var-binding))))) |
| 868 |
path |
| 869 |
#t)))) |
| 870 |
((equal? name "local-name") ; it is a LOCAL-NAME function |
| 871 |
(and-let* |
| 872 |
((lst2 (sxml:parse-arguments 0 1 path ns-binding))) |
| 873 |
(cons |
| 874 |
(if |
| 875 |
(null? (car lst2)) ; no argument |
| 876 |
(lambda (nodeset root-node context var-binding) |
| 877 |
(cond |
| 878 |
((null? nodeset) "") |
| 879 |
((not (pair? (car nodeset))) "") ; no name |
| 880 |
(else |
| 881 |
(let ((name (symbol->string (caar nodeset)))) |
| 882 |
(cond |
| 883 |
((string-rindex name #\:) |
| 884 |
=> (lambda (pos) |
| 885 |
(substring name (+ pos 1) (string-length name)))) |
| 886 |
(else ; a NCName |
| 887 |
name)))))) |
| 888 |
(let ((func (caar lst2))) |
| 889 |
(lambda (nodeset root-node context var-binding) |
| 890 |
(let ((obj (func nodeset root-node context var-binding))) |
| 891 |
(cond |
| 892 |
((null? obj) "") ; an empty nodeset |
| 893 |
((not (nodeset? obj)) |
| 894 |
(sxml:xpointer-runtime-error |
| 895 |
"NAME function - an argument is not a nodeset. " |
| 896 |
"Returning an empty string") |
| 897 |
"") |
| 898 |
((not (pair? (car obj))) "") ; no name |
| 899 |
(else |
| 900 |
(let ((name (symbol->string (caar obj)))) |
| 901 |
(cond |
| 902 |
((string-rindex name #\:) |
| 903 |
=> (lambda (pos) |
| 904 |
(substring |
| 905 |
name (+ pos 1) (string-length name)))) |
| 906 |
(else ; a NCName |
| 907 |
name))))))))) |
| 908 |
(cdr lst2)))) |
| 909 |
((equal? name "namespace-uri") ; it is a NAMESPACE-URI function |
| 910 |
(and-let* |
| 911 |
((lst2 (sxml:parse-arguments 0 1 path ns-binding))) |
| 912 |
(cons |
| 913 |
(if |
| 914 |
(null? (car lst2)) ; no argument |
| 915 |
(lambda (nodeset root-node context var-binding) |
| 916 |
(cond |
| 917 |
((null? nodeset) "") |
| 918 |
((not (pair? (car nodeset))) "") ; no name |
| 919 |
(else |
| 920 |
(let ((name (symbol->string (caar nodeset)))) |
| 921 |
(cond |
| 922 |
((string-rindex name #\:) |
| 923 |
=> (lambda (pos) |
| 924 |
(substring name 0 pos))) |
| 925 |
(else "")))))) ; a NCName |
| 926 |
(let ((func (caar lst2))) |
| 927 |
(lambda (nodeset root-node context var-binding) |
| 928 |
(let ((obj (func nodeset root-node context var-binding))) |
| 929 |
(cond |
| 930 |
((null? obj) "") ; an empty nodeset |
| 931 |
((not (nodeset? obj)) |
| 932 |
(sxml:xpointer-runtime-error |
| 933 |
"NAME function - an argument is not a nodeset. " |
| 934 |
"Returning an empty string") |
| 935 |
"") |
| 936 |
((not (pair? (car obj))) "") ; no name |
| 937 |
(else |
| 938 |
(let ((name (symbol->string (caar obj)))) |
| 939 |
(cond |
| 940 |
((string-rindex name #\:) |
| 941 |
=> (lambda (pos) |
| 942 |
(substring name 0 pos))) |
| 943 |
(else ""))))))))) ; a NCName |
| 944 |
(cdr lst2)))) |
| 945 |
((equal? name "name") ; it is a NAME function |
| 946 |
(and-let* |
| 947 |
((lst2 (sxml:parse-arguments 0 1 path ns-binding))) |
| 948 |
(cons |
| 949 |
(if |
| 950 |
(null? (car lst2)) ; no argument |
| 951 |
(lambda (nodeset root-node context var-binding) |
| 952 |
(cond |
| 953 |
((null? nodeset) "") |
| 954 |
((not (pair? (car nodeset))) "") ; no name |
| 955 |
(else |
| 956 |
(symbol->string (caar nodeset))))) |
| 957 |
(let ((func (caar lst2))) |
| 958 |
(lambda (nodeset root-node context var-binding) |
| 959 |
(let ((obj (func nodeset root-node context var-binding))) |
| 960 |
(cond |
| 961 |
((null? obj) "") ; an empty nodeset |
| 962 |
((not (nodeset? obj)) |
| 963 |
(sxml:xpointer-runtime-error |
| 964 |
"NAME function - an argument is not a nodeset. " |
| 965 |
"Returning an empty string") |
| 966 |
"") |
| 967 |
((not (pair? (car obj))) "") ; no name |
| 968 |
(else |
| 969 |
(symbol->string (caar obj)))))))) |
| 970 |
(cdr lst2)))) |
| 971 |
((equal? name "string") ; it is a 'string' function |
| 972 |
(and-let* |
| 973 |
((lst2 (sxml:parse-arguments 0 1 path ns-binding))) |
| 974 |
(let((funcs (car lst2)) |
| 975 |
(path (cadr lst2)) |
| 976 |
(index-required (caddr lst2))) |
| 977 |
(if (null? funcs) |
| 978 |
(list (lambda (nodeset root-node context var-binding) |
| 979 |
(sxml:string nodeset)) |
| 980 |
path #f) |
| 981 |
(list (lambda (nodeset root-node context var-binding) |
| 982 |
(sxml:string |
| 983 |
((car funcs) |
| 984 |
nodeset root-node context var-binding))) |
| 985 |
path |
| 986 |
index-required))))) |
| 987 |
((equal? name "concat") ; it is a CONCAT function |
| 988 |
(and-let* |
| 989 |
((lst2 (sxml:parse-arguments 2 -1 path ns-binding))) |
| 990 |
(let ((funcs (car lst2))) |
| 991 |
(cons |
| 992 |
(lambda (nodeset root-node context var-binding) |
| 993 |
(apply |
| 994 |
string-append |
| 995 |
(map |
| 996 |
(lambda (f) |
| 997 |
(sxml:string (f nodeset root-node context var-binding))) |
| 998 |
funcs))) |
| 999 |
(cdr lst2))))) |
| 1000 |
((equal? name "starts-with") ; it is a STARTS-WITH function |
| 1001 |
(and-let* |
| 1002 |
((lst2 (sxml:parse-arguments 2 2 path ns-binding))) |
| 1003 |
(let ((func1 (caar lst2)) |
| 1004 |
(func2 (cadar lst2))) |
| 1005 |
(cons |
| 1006 |
(lambda (nodeset root-node context var-binding) |
| 1007 |
(let ((str1 (sxml:string |
| 1008 |
(func1 nodeset root-node context var-binding))) |
| 1009 |
(str2 (sxml:string |
| 1010 |
(func2 nodeset root-node context var-binding)))) |
| 1011 |
(string-prefix? str2 str1))) |
| 1012 |
(cdr lst2))))) |
| 1013 |
((equal? name "contains") ; it is a 'contains' function |
| 1014 |
(and-let* |
| 1015 |
((lst2 (sxml:parse-arguments 2 2 path ns-binding))) |
| 1016 |
(let ((func1 (caar lst2)) |
| 1017 |
(func2 (cadar lst2)) |
| 1018 |
(path (cadr lst2)) |
| 1019 |
(index-required (caddr lst2))) |
| 1020 |
(list |
| 1021 |
(lambda (nodeset root-node context var-binding) |
| 1022 |
(let ((str1 (sxml:string |
| 1023 |
(func1 nodeset root-node context var-binding))) |
| 1024 |
(str2 (sxml:string |
| 1025 |
(func2 nodeset root-node context var-binding)))) |
| 1026 |
(if (substring? str2 str1) #t #f))) ; must return a boolean |
| 1027 |
path |
| 1028 |
index-required)))) |
| 1029 |
((equal? name "substring-before") ; it is a SUBSTRING-BEFORE function |
| 1030 |
(and-let* |
| 1031 |
((lst2 (sxml:parse-arguments 2 2 path ns-binding))) |
| 1032 |
(let ((func1 (caar lst2)) |
| 1033 |
(func2 (cadar lst2))) |
| 1034 |
(cons |
| 1035 |
(lambda (nodeset root-node context var-binding) |
| 1036 |
(let* ((str1 (sxml:string |
| 1037 |
(func1 nodeset root-node context var-binding))) |
| 1038 |
(str2 (sxml:string |
| 1039 |
(func2 nodeset root-node context var-binding))) |
| 1040 |
(pos (substring? str2 str1))) |
| 1041 |
(if (not pos) ; STR1 doesn't contain STR2 |
| 1042 |
"" |
| 1043 |
(substring str1 0 pos)))) |
| 1044 |
(cdr lst2))))) |
| 1045 |
((equal? name "substring-after") ; it is a SUBSTRING-AFTER function |
| 1046 |
(and-let* |
| 1047 |
((lst2 (sxml:parse-arguments 2 2 path ns-binding))) |
| 1048 |
(let ((func1 (caar lst2)) |
| 1049 |
(func2 (cadar lst2))) |
| 1050 |
(cons |
| 1051 |
(lambda (nodeset root-node context var-binding) |
| 1052 |
(let* ((str1 (sxml:string |
| 1053 |
(func1 nodeset root-node context var-binding))) |
| 1054 |
(str2 (sxml:string |
| 1055 |
(func2 nodeset root-node context var-binding))) |
| 1056 |
(pos (substring? str2 str1))) |
| 1057 |
(if |
| 1058 |
(not pos) ; STR1 doesn't contain STR2 |
| 1059 |
"" |
| 1060 |
(substring |
| 1061 |
str1 (+ pos (string-length str2)) (string-length str1))))) |
| 1062 |
(cdr lst2))))) |
| 1063 |
((equal? name "substring") ; it is a SUBSTRING function |
| 1064 |
(and-let* |
| 1065 |
((lst2 (sxml:parse-arguments 2 3 path ns-binding))) |
| 1066 |
(let ((func1 (caar lst2)) |
| 1067 |
(func2 (cadar lst2))) |
| 1068 |
(if |
| 1069 |
(= (length (car lst2)) 2) ; no third argument |
| 1070 |
(cons |
| 1071 |
(lambda (nodeset root-node context var-binding) |
| 1072 |
(let ((str (sxml:string |
| 1073 |
(func1 nodeset root-node context var-binding))) |
| 1074 |
(num1 (sxml:number |
| 1075 |
(func2 nodeset root-node context var-binding)))) |
| 1076 |
(let ((len (string-length str)) |
| 1077 |
(start (- (inexact->exact (round num1)) 1))) |
| 1078 |
(if |
| 1079 |
(> start len) |
| 1080 |
"" |
| 1081 |
(substring |
| 1082 |
str |
| 1083 |
(if (< start 0) 0 start) |
| 1084 |
len))))) |
| 1085 |
(cdr lst2)) |
| 1086 |
(let ((func3 (caddar lst2))) |
| 1087 |
(cons |
| 1088 |
(lambda (nodeset root-node context var-binding) |
| 1089 |
(let ((str (sxml:string |
| 1090 |
(func1 nodeset root-node context var-binding))) |
| 1091 |
(num1 (sxml:number |
| 1092 |
(func2 nodeset root-node context var-binding))) |
| 1093 |
(num2 (sxml:number |
| 1094 |
(func3 nodeset root-node context var-binding)))) |
| 1095 |
(let* ((len (string-length str)) |
| 1096 |
(start (- (inexact->exact (round num1)) 1)) |
| 1097 |
(fin (+ start (inexact->exact (round num2))))) |
| 1098 |
(if |
| 1099 |
(or (> start len) (< fin 0) (< fin start)) |
| 1100 |
"" |
| 1101 |
(substring |
| 1102 |
str |
| 1103 |
(if (< start 0) 0 start) |
| 1104 |
(if (> fin len) len fin)))))) |
| 1105 |
(cdr lst2))))))) |
| 1106 |
((equal? name "string-length") ; it is a STRING-LENGTH function |
| 1107 |
(and-let* |
| 1108 |
((lst2 (sxml:parse-arguments 0 1 path ns-binding))) |
| 1109 |
(cons |
| 1110 |
(if |
| 1111 |
(null? (car lst2)) ; no argument |
| 1112 |
(lambda (nodeset root-node context var-binding) |
| 1113 |
(string-length (sxml:string nodeset))) |
| 1114 |
(let ((func (caar lst2))) |
| 1115 |
(lambda (nodeset root-node context var-binding) |
| 1116 |
(string-length |
| 1117 |
(sxml:string |
| 1118 |
(func nodeset root-node context var-binding)))))) |
| 1119 |
(cdr lst2)))) |
| 1120 |
((equal? name "normalize-space") ; it is a NORMALIZE-SPACE function |
| 1121 |
(and-let* |
| 1122 |
((lst2 (sxml:parse-arguments 0 1 path ns-binding))) |
| 1123 |
(cons |
| 1124 |
(if |
| 1125 |
(null? (car lst2)) ; no argument |
| 1126 |
(lambda (nodeset root-node context var-binding) |
| 1127 |
(let rpt ((src (string-split |
| 1128 |
(sxml:string nodeset) sxml:whitespace)) |
| 1129 |
(res '())) |
| 1130 |
(cond |
| 1131 |
((null? src) |
| 1132 |
(apply string-append (reverse res))) |
| 1133 |
((= (string-length (car src)) 0) ; empty string |
| 1134 |
(rpt (cdr src) res)) |
| 1135 |
((null? res) |
| 1136 |
(rpt (cdr src) (cons (car src) res))) |
| 1137 |
(else |
| 1138 |
(rpt (cdr src) (cons (car src) (cons " " res))))))) |
| 1139 |
(let ((func (caar lst2))) |
| 1140 |
(lambda (nodeset root-node context var-binding) |
| 1141 |
(let rpt ((src (string-split |
| 1142 |
(sxml:string |
| 1143 |
(func nodeset root-node context var-binding)) |
| 1144 |
sxml:whitespace)) |
| 1145 |
(res '())) |
| 1146 |
(cond |
| 1147 |
((null? src) |
| 1148 |
(apply string-append (reverse res))) |
| 1149 |
((= (string-length (car src)) 0) ; empty string |
| 1150 |
(rpt (cdr src) res)) |
| 1151 |
((null? res) |
| 1152 |
(rpt (cdr src) (cons (car src) res))) |
| 1153 |
(else |
| 1154 |
(rpt (cdr src) (cons (car src) (cons " " res))))))))) |
| 1155 |
(cdr lst2)))) |
| 1156 |
((equal? name "translate") ; it is a TRANSLATE function |
| 1157 |
(and-let* |
| 1158 |
((lst2 (sxml:parse-arguments 3 3 path ns-binding))) |
| 1159 |
(let ((func1 (caar lst2)) |
| 1160 |
(func2 (cadar lst2)) |
| 1161 |
(func3 (caddar lst2))) |
| 1162 |
(cons |
| 1163 |
(lambda (nodeset root-node context var-binding) |
| 1164 |
(let ((str1 (sxml:string |
| 1165 |
(func1 nodeset root-node context var-binding))) |
| 1166 |
(str2 (sxml:string |
| 1167 |
(func2 nodeset root-node context var-binding))) |
| 1168 |
(str3 (sxml:string |
| 1169 |
(func3 nodeset root-node context var-binding)))) |
| 1170 |
(let ((alist |
| 1171 |
(let while ((lst2 (string->list str2)) |
| 1172 |
(lst3 (string->list str3)) |
| 1173 |
(alist '())) |
| 1174 |
(cond |
| 1175 |
((null? lst2) (reverse alist)) |
| 1176 |
((null? lst3) |
| 1177 |
(append |
| 1178 |
(reverse alist) |
| 1179 |
(map |
| 1180 |
(lambda (ch) (cons ch #f)) |
| 1181 |
lst2))) |
| 1182 |
(else |
| 1183 |
(while |
| 1184 |
(cdr lst2) |
| 1185 |
(cdr lst3) |
| 1186 |
(cons (cons (car lst2) (car lst3)) alist))))))) |
| 1187 |
(let rpt ((lst1 (string->list str1)) |
| 1188 |
(res '())) |
| 1189 |
(cond |
| 1190 |
((null? lst1) (list->string (reverse res))) |
| 1191 |
((assoc (car lst1) alist) |
| 1192 |
=> (lambda (pair) |
| 1193 |
(if (cdr pair) |
| 1194 |
(rpt (cdr lst1) (cons (cdr pair) res)) |
| 1195 |
(rpt (cdr lst1) res)))) |
| 1196 |
(else |
| 1197 |
(rpt (cdr lst1) (cons (car lst1) res)))))))) |
| 1198 |
(cdr lst2))))) |
| 1199 |
((equal? name "boolean") ; it is a 'boolean' function |
| 1200 |
(and-let* |
| 1201 |
((lst2 (sxml:parse-arguments 1 1 path ns-binding))) |
| 1202 |
(let ((func (caar lst2)) |
| 1203 |
(path (cadr lst2)) |
| 1204 |
(index-required (caddr lst2))) |
| 1205 |
(list (lambda (nodeset root-node context var-binding) |
| 1206 |
(sxml:boolean |
| 1207 |
(func nodeset root-node context var-binding))) |
| 1208 |
path |
| 1209 |
index-required)))) |
| 1210 |
((equal? name "not") ; it is a 'not' function |
| 1211 |
(and-let* |
| 1212 |
((lst2 (sxml:parse-arguments 1 1 path ns-binding))) |
| 1213 |
(let ((func (caar lst2)) |
| 1214 |
(path (cadr lst2)) |
| 1215 |
(index-required (caddr lst2))) |
| 1216 |
(list (lambda (nodeset root-node context var-binding) |
| 1217 |
(not (sxml:boolean |
| 1218 |
(func nodeset root-node context var-binding)))) |
| 1219 |
path |
| 1220 |
index-required)))) |
| 1221 |
((equal? name "true") ; it is a 'true' function |
| 1222 |
(and-let* |
| 1223 |
((lst2 (sxml:parse-arguments 0 0 path ns-binding))) |
| 1224 |
(list (lambda (nodeset root-node context var-binding) #t) |
| 1225 |
(cadr lst2) |
| 1226 |
#f))) |
| 1227 |
((equal? name "false") ; it is a 'false' function |
| 1228 |
(and-let* |
| 1229 |
((lst2 (sxml:parse-arguments 0 0 path ns-binding))) |
| 1230 |
(list (lambda (nodeset root-node context var-binding) #f) |
| 1231 |
(cadr lst2) |
| 1232 |
#f))) |
| 1233 |
((equal? name "lang") ; it is a LANG function |
| 1234 |
(and-let* |
| 1235 |
((lst2 (sxml:parse-arguments 1 1 path ns-binding))) |
| 1236 |
(let ((func (caar lst2))) |
| 1237 |
(cons |
| 1238 |
(lambda (nodeset root-node context var-binding) |
| 1239 |
(if |
| 1240 |
(null? nodeset) |
| 1241 |
#f |
| 1242 |
(let ((arg (sxml:string |
| 1243 |
(func nodeset root-node context var-binding))) |
| 1244 |
(context-node (car nodeset))) |
| 1245 |
(let rpt ((pairs |
| 1246 |
(map |
| 1247 |
(lambda (node) (cons node #f)) |
| 1248 |
root-node))) |
| 1249 |
(if |
| 1250 |
(null? pairs) ; context node not found |
| 1251 |
#f |
| 1252 |
(let* ((lng |
| 1253 |
((sxml:child (ntype?? '*text*)) |
| 1254 |
((sxml:attribute (ntype?? 'xml:lang)) |
| 1255 |
(caar pairs)))) |
| 1256 |
(lng (if (null? lng) (cdar pairs) (car lng)))) |
| 1257 |
(if |
| 1258 |
(eq? context-node (caar pairs)) ; context node found |
| 1259 |
(and |
| 1260 |
lng |
| 1261 |
(or (string-ci=? arg lng) |
| 1262 |
(string-prefix-ci? (string-append arg "-") lng))) |
| 1263 |
(rpt |
| 1264 |
(append |
| 1265 |
(map |
| 1266 |
(lambda (node) (cons node lng)) |
| 1267 |
((sxml:attribute (ntype?? '*)) (caar pairs))) |
| 1268 |
(map |
| 1269 |
(lambda (node) (cons node lng)) |
| 1270 |
((sxml:child sxml:node?) (caar pairs))) |
| 1271 |
(cdr pairs)))))))))) |
| 1272 |
(cdr lst2))))) |
| 1273 |
((equal? name "number") ; it is a 'number' function |
| 1274 |
(and-let* |
| 1275 |
((lst2 (sxml:parse-arguments 0 1 path ns-binding))) |
| 1276 |
(let ((funcs (car lst2)) |
| 1277 |
(path (cadr lst2)) |
| 1278 |
(index-required (caddr lst2))) |
| 1279 |
(if (null? funcs) |
| 1280 |
(list |
| 1281 |
(lambda (nodeset root-node context var-binding) |
| 1282 |
(sxml:number nodeset)) |
| 1283 |
path |
| 1284 |
#f) |
| 1285 |
(list |
| 1286 |
(lambda (nodeset root-node context var-binding) |
| 1287 |
(sxml:number |
| 1288 |
((car funcs) nodeset root-node context |
| 1289 |
var-binding))) |
| 1290 |
path |
| 1291 |
index-required))))) |
| 1292 |
((equal? name "sum") ; it is a SUM function |
| 1293 |
(and-let* |
| 1294 |
((lst2 (sxml:parse-arguments 1 1 path ns-binding))) |
| 1295 |
(let ((func (caar lst2))) |
| 1296 |
(cons |
| 1297 |
(lambda (nodeset root-node context var-binding) |
| 1298 |
(let ((res (func nodeset root-node context var-binding))) |
| 1299 |
(cond |
| 1300 |
((nodeset? res) |
| 1301 |
(apply |
| 1302 |
+ |
| 1303 |
(map |
| 1304 |
(lambda (node) |
| 1305 |
(sxml:number (sxml:string-value node))) |
| 1306 |
res))) |
| 1307 |
(else |
| 1308 |
(sxml:xpointer-runtime-error |
| 1309 |
"SUM function - an argument is not a nodeset. " |
| 1310 |
"Returning zero") |
| 1311 |
0)))) |
| 1312 |
(cdr lst2))))) |
| 1313 |
((equal? name "floor") ; it is a 'floor' function |
| 1314 |
(and-let* |
| 1315 |
((lst2 (sxml:parse-arguments 1 1 path ns-binding))) |
| 1316 |
(let ((func (caar lst2)) |
| 1317 |
(path (cadr lst2)) |
| 1318 |
(index-required (caddr lst2))) |
| 1319 |
(list (lambda (nodeset root-node context var-binding) |
| 1320 |
(inexact->exact |
| 1321 |
(floor (sxml:number |
| 1322 |
(func nodeset root-node context var-binding))))) |
| 1323 |
path |
| 1324 |
index-required)))) |
| 1325 |
((equal? name "ceiling") ; it is a 'ceiling' function |
| 1326 |
(and-let* |
| 1327 |
((lst2 (sxml:parse-arguments 1 1 path ns-binding))) |
| 1328 |
(let ((func (caar lst2)) |
| 1329 |
(path (cadr lst2)) |
| 1330 |
(index-required (caddr lst2))) |
| 1331 |
(list (lambda (nodeset root-node context var-binding) |
| 1332 |
(inexact->exact |
| 1333 |
(ceiling (sxml:number |
| 1334 |
(func nodeset root-node context var-binding))))) |
| 1335 |
path |
| 1336 |
index-required)))) |
| 1337 |
((equal? name "round") ; it is a 'round' function |
| 1338 |
(and-let* |
| 1339 |
((lst2 (sxml:parse-arguments 1 1 path ns-binding))) |
| 1340 |
(let ((func (caar lst2)) |
| 1341 |
(path (cadr lst2)) |
| 1342 |
(index-required (caddr lst2))) |
| 1343 |
(list (lambda (nodeset root-node context var-binding) |
| 1344 |
(inexact->exact |
| 1345 |
(round (sxml:number |
| 1346 |
(func nodeset root-node context var-binding))))) |
| 1347 |
path |
| 1348 |
index-required)))) |
| 1349 |
(else ; unknown function |
| 1350 |
(and-let* |
| 1351 |
((lst (sxml:parse-arguments 0 -1 path ns-binding))) |
| 1352 |
(let ((path (cadr lst))) |
| 1353 |
(sxml:xpointer-parse-warning |
| 1354 |
"function not implemented - " |
| 1355 |
(if (pair? name) |
| 1356 |
(string-append (car name) ":" (cdr name)) |
| 1357 |
name)) |
| 1358 |
(list (lambda (nodeset root-node context var-binding) '()) |
| 1359 |
path |
| 1360 |
#f)))))))) |
| 1361 |
|
| 1362 |
; Parses a PrimaryExpr production ([15] in XPath specification) |
| 1363 |
; [15] PrimaryExpr ::= VariableReference |
| 1364 |
; | '(' Expr ')' |
| 1365 |
; | Literal |
| 1366 |
; | Number |
| 1367 |
; | FunctionCall |
| 1368 |
; [29] Literal ::= '"' [^"]* '"' |
| 1369 |
; | "'" [^']* "'" |
| 1370 |
; [30] Number ::= Digits ('.' Digits?)? |
| 1371 |
; | '.' Digits |
| 1372 |
; [31] Digits ::= [0-9]+ |
| 1373 |
(define (sxml:parse-primary-expr path ns-binding) |
| 1374 |
(cond |
| 1375 |
((sxml:parse-check "$" path) ; a VariableReference |
| 1376 |
(sxml:parse-variable-reference path ns-binding)) |
| 1377 |
((sxml:parse-check "(" path) ; an '(' Expr ')' |
| 1378 |
(and-let* ((lst (sxml:parse-expr (sxml:parse-assert "(" path) ns-binding)) |
| 1379 |
(path (sxml:parse-assert ")" (cadr lst)))) |
| 1380 |
(let ((func (car lst)) |
| 1381 |
(index-required (caddr lst))) |
| 1382 |
(list func path index-required)))) |
| 1383 |
((or (sxml:parse-check "\"" path) (sxml:parse-check "'" path)) ; a Literal |
| 1384 |
(and-let* ((lst (sxml:parse-literal path))) |
| 1385 |
(list |
| 1386 |
(lambda (nodeset root-node context var-binding) (car lst)) |
| 1387 |
(cadr lst) |
| 1388 |
#f))) |
| 1389 |
((let ((p (sxml:skip-ws path))) |
| 1390 |
(cond ((null? p) #f) |
| 1391 |
((char=? (car p) #\.) #t) |
| 1392 |
((and (char>=? (car p) #\0) (char<=? (car p) #\9)) #t) |
| 1393 |
(else #f))) ; a Number |
| 1394 |
(and-let* ((lst (sxml:parse-number path))) |
| 1395 |
(list |
| 1396 |
(lambda (nodeset root-node context var-binding) (car lst)) |
| 1397 |
(cadr lst) |
| 1398 |
#f))) |
| 1399 |
(else ; a Function call |
| 1400 |
(sxml:parse-function-call path ns-binding)))) |
| 1401 |
|
| 1402 |
|
| 1403 |
; Parses a FilterExpr production ([20] in XPath specification) |
| 1404 |
; [20] FilterExpr ::= PrimaryExpr |
| 1405 |
; | FilterExpr Predicate |
| 1406 |
(define (sxml:parse-filter-expr path ns-binding) |
| 1407 |
(and-let* ((lst (sxml:parse-primary-expr path ns-binding))) |
| 1408 |
(let ((prim (car lst))) |
| 1409 |
(let loop ((preds '()) |
| 1410 |
(path (cadr lst)) |
| 1411 |
(index-required (caddr lst))) |
| 1412 |
(if (sxml:parse-check "[" path) |
| 1413 |
(and-let* ((lst (sxml:parse-predicate path ns-binding))) |
| 1414 |
(loop (cons (car lst) preds) |
| 1415 |
(cadr lst) |
| 1416 |
(or index-required (caddr lst)))) |
| 1417 |
(if (null? preds) ; No more predicates |
| 1418 |
(list prim path index-required) |
| 1419 |
(list |
| 1420 |
(lambda (nodeset root-node context var-binding) |
| 1421 |
(let ((nodeset |
| 1422 |
(prim nodeset root-node context var-binding))) |
| 1423 |
(sxml:xpath-nodeset-filter |
| 1424 |
preds |
| 1425 |
(cond |
| 1426 |
((nodeset? nodeset) nodeset) |
| 1427 |
(else |
| 1428 |
(sxml:xpointer-runtime-error |
| 1429 |
"expected - nodeset instead of " nodeset |
| 1430 |
". Converting to an empty nodeset") |
| 1431 |
'())) |
| 1432 |
root-node var-binding))) |
| 1433 |
path |
| 1434 |
index-required))))))) |
| 1435 |
|
| 1436 |
; Parses a PathExpr production ([19] in XPath specification) |
| 1437 |
; [19] PathExpr ::= LocationPath |
| 1438 |
; | FilterExpr |
| 1439 |
; | FilterExpr '/' RelativeLocationPath |
| 1440 |
; | FilterExpr '//' RelativeLocationPath |
| 1441 |
(define (sxml:parse-path-expr path ns-binding) |
| 1442 |
|
| 1443 |
(define (filter-expr? path) |
| 1444 |
(let ((path (sxml:skip-ws path))) |
| 1445 |
(cond |
| 1446 |
((null? path) #f) |
| 1447 |
((member |
| 1448 |
(car path) |
| 1449 |
'(#\$ #\( #\" #\' #\0 #\1 #\2 #\3 #\4 #\5 #\6 #\7 #\8 #\9)) #t) |
| 1450 |
((char=? (car path) #\.) |
| 1451 |
(cond((null? (cdr path)) #f) |
| 1452 |
((member (cadr path) |
| 1453 |
'(#\0 #\1 #\2 #\3 #\4 #\5 #\6 #\7 #\8 #\9)) #t) |
| 1454 |
(else #f))) |
| 1455 |
((member |
| 1456 |
(car path) |
| 1457 |
'(#\) #\< #\> #\[ #\] #\/ #\+ #\* #\, #\= #\| #\! #\@ #\-)) #f) |
| 1458 |
(else (let ((lst (sxml:parse-ncname path))) |
| 1459 |
(cond |
| 1460 |
((not lst) #f) |
| 1461 |
((sxml:parse-check "::" (cadr lst)) #f) |
| 1462 |
(else (and-let* ((lst (sxml:parse-name path))) |
| 1463 |
(let ((name (car lst)) |
| 1464 |
(new-path (sxml:skip-ws (cadr lst)))) |
| 1465 |
(cond |
| 1466 |
((string=? name "range-to") #f) |
| 1467 |
((string=? name "comment") #f) |
| 1468 |
((string=? name "text") #f) |
| 1469 |
((string=? name "processing-instruction") #f) |
| 1470 |
((string=? name "node") #f) |
| 1471 |
((string=? name "point") #f) |
| 1472 |
((string=? name "range") #f) |
| 1473 |
((null? new-path) #f) |
| 1474 |
((char=? (car new-path) #\() #t) |
| 1475 |
(else #f))))))))))) |
| 1476 |
(if (not (filter-expr? path)) |
| 1477 |
(sxml:parse-location-path path ns-binding) |
| 1478 |
(and-let* ((lst (sxml:parse-filter-expr path ns-binding))) |
| 1479 |
(let ((f-ex (car lst)) |
| 1480 |
(path (cadr lst)) |
| 1481 |
(index-required (caddr lst))) |
| 1482 |
(cond |
| 1483 |
((sxml:parse-check "//" path) |
| 1484 |
(and-let* ((lst2 (sxml:parse-relative-location-path |
| 1485 |
(sxml:parse-assert "//" path) ns-binding))) |
| 1486 |
(let ((f-rel (car lst2)) |
| 1487 |
(path (cadr lst2)) |
| 1488 |
(bool (or index-required (caddr lst2)))) |
| 1489 |
(list |
| 1490 |
(lambda (nodeset root-node context var-binding) |
| 1491 |
(let ((nset (f-ex nodeset root-node context |
| 1492 |
var-binding))) |
| 1493 |
(let ((nset |
| 1494 |
(cond |
| 1495 |
((nodeset? nset) nset) |
| 1496 |
(else |
| 1497 |
(sxml:xpointer-runtime-error |
| 1498 |
"expected - nodeset instead of " nset |
| 1499 |
". Converting to an empty nodeset") |
| 1500 |
'())))) |
| 1501 |
(let ((nset ((sxml:descendant-or-self sxml:node?) |
| 1502 |
nset))) |
| 1503 |
(f-rel nset root-node context |
| 1504 |
var-binding))))) |
| 1505 |
path |
| 1506 |
bool)))) |
| 1507 |
((sxml:parse-check "/" path) |
| 1508 |
(and-let* ((lst2 (sxml:parse-relative-location-path |
| 1509 |
(sxml:parse-assert "/" path) ns-binding))) |
| 1510 |
(let ((f-rel (car lst2)) |
| 1511 |
(path (cadr lst2)) |
| 1512 |
(bool (or index-required (caddr lst2)))) |
| 1513 |
(list |
| 1514 |
(lambda (nodeset root-node context var-binding) |
| 1515 |
(let ((nset (f-ex nodeset root-node context |
| 1516 |
var-binding))) |
| 1517 |
(let ((nset |
| 1518 |
(cond |
| 1519 |
((nodeset? nset) nset) |
| 1520 |
(else |
| 1521 |
(sxml:xpointer-runtime-error |
| 1522 |
"expected - nodeset instead of " |
| 1523 |
nset |
| 1524 |
". Converting to an empty nodeset") |
| 1525 |
'())))) |
| 1526 |
(f-rel |
| 1527 |
nset root-node context var-binding)))) |
| 1528 |
path |
| 1529 |
bool)))) |
| 1530 |
(else lst)))))) |
| 1531 |
|
| 1532 |
; Parses a UnionExpr production ([18] in XPath specification) |
| 1533 |
; [18] UnionExpr ::= PathExpr |
| 1534 |
; | UnionExpr '|' PathExpr |
| 1535 |
(define (sxml:parse-union-expr path ns-binding) |
| 1536 |
(let loop ((funcs '()) |
| 1537 |
(path path) |
| 1538 |
(index-required #f)) |
| 1539 |
(and-let* ((lst (sxml:parse-path-expr path ns-binding))) |
| 1540 |
(let ((func (car lst)) |
| 1541 |
(path (cadr lst)) |
| 1542 |
(id-ind? (or index-required (caddr lst)))) |
| 1543 |
(let ((new-path (sxml:parse-check "|" path))) |
| 1544 |
(if (not new-path) ; no more PathExprs |
| 1545 |
(if (null? funcs) ; only one PathExpr |
| 1546 |
(list func path id-ind?) |
| 1547 |
(list |
| 1548 |
(lambda (nodeset root-node context var-binding) |
| 1549 |
(let rpt ((res '()) |
| 1550 |
(fs (reverse (cons func funcs)))) |
| 1551 |
(if (null? fs) |
| 1552 |
res |
| 1553 |
(let ((nset ((car fs) nodeset root-node context |
| 1554 |
var-binding))) |
| 1555 |
(rpt |
| 1556 |
(append |
| 1557 |
res |
| 1558 |
(cond |
| 1559 |
((not (nodeset? nset)) |
| 1560 |
(sxml:xpointer-runtime-error |
| 1561 |
"expected - nodeset instead of " |
| 1562 |
nset ". Ignoring") |
| 1563 |
'()) |
| 1564 |
(else nset))) |
| 1565 |
(cdr fs)))))) |
| 1566 |
path |
| 1567 |
id-ind?)) |
| 1568 |
(loop (cons func funcs) new-path id-ind?))))))) |
| 1569 |
|
| 1570 |
; Parses a UnaryExpr production ([27] in XPath specification) |
| 1571 |
; [27] UnaryExpr ::= UnionExpr |
| 1572 |
; | '-' UnaryExpr |
| 1573 |
; Note that the grammar allows multiple unary minuses |
| 1574 |
(define (sxml:parse-unary-expr path ns-binding) |
| 1575 |
(if (not (sxml:parse-check "-" path)) |
| 1576 |
(sxml:parse-union-expr path ns-binding) |
| 1577 |
(let loop ((minu #f) (path path)) |
| 1578 |
(let ((new-path (sxml:parse-check "-" path))) |
| 1579 |
(if (not new-path) ; minuses are over |
| 1580 |
(and-let* ((lst (sxml:parse-union-expr path ns-binding))) |
| 1581 |
(let ((func (car lst)) |
| 1582 |
(path2 (cadr lst)) |
| 1583 |
(index-required (caddr lst))) |
| 1584 |
(list |
| 1585 |
(lambda (nodeset root-node context var-binding) |
| 1586 |
(if minu |
| 1587 |
(- 0 (sxml:number |
| 1588 |
(func nodeset root-node context |
| 1589 |
var-binding))) |
| 1590 |
(sxml:number |
| 1591 |
(func nodeset root-node context |
| 1592 |
var-binding)))) |
| 1593 |
path2 |
| 1594 |
index-required))) |
| 1595 |
(loop (not minu) new-path)))))) |
| 1596 |
|
| 1597 |
; A helper for arithmetic parsers |
| 1598 |
; sxml:parse-additive-expr and sxml:parse-multiplicative-expr |
| 1599 |
(define (sxml:no-more-exprs funcs func path id-ind? opers) |
| 1600 |
(if (null? funcs) ; only one Expr |
| 1601 |
(list func path id-ind?) |
| 1602 |
(let ((flst (reverse (cons func funcs)))) |
| 1603 |
(list (lambda (nodeset root-node context var-binding) |
| 1604 |
(let rpt ((res (sxml:number |
| 1605 |
((car flst) nodeset root-node context |
| 1606 |
var-binding))) |
| 1607 |
(fs (cdr flst)) |
| 1608 |
(ops (reverse opers))) |
| 1609 |
(if (null? fs) |
| 1610 |
res |
| 1611 |
(rpt ((car ops) |
| 1612 |
res |
| 1613 |
(sxml:number |
| 1614 |
((car fs) nodeset root-node context |
| 1615 |
var-binding))) |
| 1616 |
(cdr fs) |
| 1617 |
(cdr ops))))) |
| 1618 |
path |
| 1619 |
id-ind?)))) |
| 1620 |
|
| 1621 |
; Parses a MultiplicativeExpr production ([26],[34] in XPath specification) |
| 1622 |
; [26] MultiplicativeExpr ::= UnaryExpr |
| 1623 |
; | MultiplicativeExpr MultiplyOperator UnaryExpr |
| 1624 |
; | MultiplicativeExpr 'div' UnaryExpr |
| 1625 |
; | MultiplicativeExpr 'mod' UnaryExpr |
| 1626 |
; [34] MultiplyOperator ::= '*' |
| 1627 |
(define (sxml:parse-multiplicative-expr path ns-binding) |
| 1628 |
(let loop ((funcs '()) |
| 1629 |
(opers '()) |
| 1630 |
(path path) |
| 1631 |
(index-required #f)) |
| 1632 |
(and-let* ((lst (sxml:parse-unary-expr path ns-binding))) |
| 1633 |
(let ((func (car lst)) |
| 1634 |
(path (cadr lst)) |
| 1635 |
(id-ind? (or index-required (caddr lst)))) |
| 1636 |
(cond |
| 1637 |
((sxml:parse-check "*" path) |
| 1638 |
(loop (cons func funcs) (cons * opers) |
| 1639 |
(sxml:parse-assert "*" path) id-ind?)) |
| 1640 |
((sxml:parse-check "div" path sxml:delimiter) |
| 1641 |
(loop (cons func funcs) (cons / opers) |
| 1642 |
(sxml:parse-assert "div" path) id-ind?)) |
| 1643 |
((sxml:parse-check "mod" path sxml:delimiter) |
| 1644 |
(loop (cons func funcs) (cons remainder opers) |
| 1645 |
(sxml:parse-assert "mod" path) id-ind?)) |
| 1646 |
(else ; no more UnaryExprs |
| 1647 |
(sxml:no-more-exprs funcs func path id-ind? opers) |
| 1648 |
)))))) |
| 1649 |
|
| 1650 |
; Parses a AdditiveExpr production ([25] in XPath specification) |
| 1651 |
; [25] AdditiveExpr ::= MultiplicativeExpr |
| 1652 |
; | AdditiveExpr '+' MultiplicativeExpr |
| 1653 |
; | AdditiveExpr '-' MultiplicativeExpr |
| 1654 |
(define (sxml:parse-additive-expr path ns-binding) |
| 1655 |
(let loop ((funcs '()) |
| 1656 |
(opers '()) |
| 1657 |
(path path) |
| 1658 |
(index-required #f)) |
| 1659 |
(and-let* ((lst (sxml:parse-multiplicative-expr path ns-binding))) |
| 1660 |
(let ((func (car lst)) |
| 1661 |
(path (cadr lst)) |
| 1662 |
(id-ind? (or index-required (caddr lst)))) |
| 1663 |
(cond |
| 1664 |
((sxml:parse-check "+" path) |
| 1665 |
(loop (cons func funcs) (cons + opers) |
| 1666 |
(sxml:parse-assert "+" path) id-ind?)) |
| 1667 |
((sxml:parse-check "-" path) |
| 1668 |
(loop (cons func funcs) (cons - opers) |
| 1669 |
(sxml:parse-assert "-" path) id-ind?)) |
| 1670 |
(else ; no more MultiplicativeExprs |
| 1671 |
(sxml:no-more-exprs funcs func path id-ind? opers) |
| 1672 |
)))))) |
| 1673 |
|
| 1674 |
; Parses a RelationalExpr production ([24] in XPath specification) |
| 1675 |
; [24] RelationalExpr ::= AdditiveExpr |
| 1676 |
; | RelationalExpr '<' AdditiveExpr |
| 1677 |
; | RelationalExpr '>' AdditiveExpr |
| 1678 |
; | RelationalExpr '<=' AdditiveExpr |
| 1679 |
; | RelationalExpr '>=' AdditiveExpr |
| 1680 |
(define (sxml:parse-relational-expr path ns-binding) |
| 1681 |
(let loop ((funcs '()) |
| 1682 |
(opers '()) |
| 1683 |
(path path) |
| 1684 |
(index-required #f)) |
| 1685 |
(let ((lst (sxml:parse-additive-expr path ns-binding))) |
| 1686 |
(if (not lst) |
| 1687 |
#f |
| 1688 |
(let ((func (car lst)) |
| 1689 |
(path (cadr lst)) |
| 1690 |
(bool (or index-required (caddr lst)))) |
| 1691 |
(cond |
| 1692 |
((sxml:parse-check "<=" path) |
| 1693 |
(loop (cons func funcs) (cons (sxml:relational-cmp <=) opers) |
| 1694 |
(sxml:parse-assert "<=" path) bool)) |
| 1695 |
((sxml:parse-check ">=" path) |
| 1696 |
(loop (cons func funcs) (cons (sxml:relational-cmp >=) opers) |
| 1697 |
(sxml:parse-assert ">=" path) bool)) |
| 1698 |
((sxml:parse-check "<" path) |
| 1699 |
(loop (cons func funcs) (cons (sxml:relational-cmp <) opers) |
| 1700 |
(sxml:parse-assert "<" path) bool)) |
| 1701 |
((sxml:parse-check ">" path) |
| 1702 |
(loop (cons func funcs) (cons (sxml:relational-cmp >) opers) |
| 1703 |
(sxml:parse-assert ">" path) bool)) |
| 1704 |
(else ; no more AdditiveExprs |
| 1705 |
(if (null? funcs) ; only one AdditiveExpr ; sxml:no-more-helper ? |
| 1706 |
(list func path bool) |
| 1707 |
(let ((flst (reverse (cons func funcs)))) |
| 1708 |
(list |
| 1709 |
(lambda (nodeset root-node context var-binding) |
| 1710 |
(let rpt ((res ((car flst) nodeset root-node context |
| 1711 |
var-binding)) |
| 1712 |
(fs (cdr flst)) |
| 1713 |
(ops (reverse opers))) |
| 1714 |
(if (null? fs) |
| 1715 |
res |
| 1716 |
(rpt ((car ops) |
| 1717 |
res |
| 1718 |
((car fs) nodeset root-node context |
| 1719 |
var-binding)) |
| 1720 |
(cdr fs) |
| 1721 |
(cdr ops))))) |
| 1722 |
path |
| 1723 |
bool))) |
| 1724 |
))))))) |
| 1725 |
|
| 1726 |
|
| 1727 |
; Parses an EqualityExpr production ([23] in XPath specification) |
| 1728 |
; [23] EqualityExpr ::= RelationalExpr |
| 1729 |
; | EqualityExpr '=' RelationalExpr |
| 1730 |
; | EqualityExpr '!=' RelationalExpr |
| 1731 |
(define (sxml:parse-equality-expr path ns-binding) |
| 1732 |
(let loop ((funcs '()) |
| 1733 |
(opers '()) |
| 1734 |
(path path) |
| 1735 |
(index-required #f)) |
| 1736 |
(and-let* ((lst (sxml:parse-relational-expr path ns-binding))) |
| 1737 |
(let ((func (car lst)) |
| 1738 |
(path (cadr lst)) |
| 1739 |
(id-ind? (or index-required (caddr lst)))) |
| 1740 |
(cond |
| 1741 |
((sxml:parse-check "=" path) |
| 1742 |
(loop (cons func funcs) (cons sxml:equal? opers) |
| 1743 |
(sxml:parse-assert "=" path) id-ind?)) |
| 1744 |
((sxml:parse-check "!=" path) |
| 1745 |
(loop (cons func funcs) (cons sxml:not-equal? opers) |
| 1746 |
(sxml:parse-assert "!=" path) id-ind?)) |
| 1747 |
(else ; no more RelationalExprs |
| 1748 |
(if (null? funcs) ; only one RelationalExpr |
| 1749 |
(list func path id-ind?) |
| 1750 |
(let ((flst (reverse (cons func funcs)))) |
| 1751 |
(list |
| 1752 |
(lambda (nodeset root-node context var-binding) |
| 1753 |
(let rpt ((res ((car flst) nodeset root-node context |
| 1754 |
var-binding)) |
| 1755 |
(fs (cdr flst)) |
| 1756 |
(ops (reverse opers))) |
| 1757 |
(if (null? fs) |
| 1758 |
res |
| 1759 |
(rpt ((car ops) |
| 1760 |
res |
| 1761 |
((car fs) nodeset root-node context |
| 1762 |
var-binding)) |
| 1763 |
(cdr fs) |
| 1764 |
(cdr ops))))) |
| 1765 |
path |
| 1766 |
id-ind?))) |
| 1767 |
)))))) |
| 1768 |
|
| 1769 |
; Parses an AndExpr production ([22] in XPath specification) |
| 1770 |
; [22] AndExpr ::= EqualityExpr |
| 1771 |
; | AndExpr 'and' EqualityExpr |
| 1772 |
; Note that according to 3.4 in XPath specification, the right operand is not |
| 1773 |
; evaluated if the left operand evaluates to false |
| 1774 |
(define (sxml:parse-and-expr path ns-binding) |
| 1775 |
(let loop ((funcs '()) |
| 1776 |
(path path) |
| 1777 |
(index-required #f)) |
| 1778 |
(and-let* ((lst (sxml:parse-equality-expr path ns-binding))) |
| 1779 |
(let ((func (car lst)) |
| 1780 |
(path (cadr lst)) |
| 1781 |
(id-ind? (or index-required (caddr lst)))) |
| 1782 |
(let ((new-path (sxml:parse-check "and" path sxml:delimiter))) |
| 1783 |
(if (not new-path) ; no more EqualityExprs |
| 1784 |
(if (null? funcs) ; only one EqualityExpr |
| 1785 |
(list func path id-ind?) |
| 1786 |
(list |
| 1787 |
(lambda (nodeset root-node context var-binding) |
| 1788 |
(let rpt ((fs (reverse (cons func funcs)))) |
| 1789 |
(cond |
| 1790 |
((null? fs) #t) |
| 1791 |
((not (sxml:boolean |
| 1792 |
((car fs) nodeset root-node context |
| 1793 |
var-binding))) #f) |
| 1794 |
(else (rpt (cdr fs)))))) |
| 1795 |
path |
| 1796 |
id-ind?)) |
| 1797 |
(loop (cons func funcs) new-path id-ind?))))))) |
| 1798 |
|
| 1799 |
|
| 1800 |
; Parses an Expr production ([14],[21] in XPath specification) |
| 1801 |
; [14] Expr ::= OrExpr |
| 1802 |
; [21] OrExpr ::= AndExpr |
| 1803 |
; | OrExpr 'or' AndExpr |
| 1804 |
; Note that according to 3.4 in XPath specification, the right operand is not |
| 1805 |
; evaluated if the left operand evaluates to true |
| 1806 |
(define (sxml:parse-expr path ns-binding) |
| 1807 |
(let loop ((funcs '()) |
| 1808 |
(path path) |
| 1809 |
(index-required #f)) |
| 1810 |
(and-let* ((lst (sxml:parse-and-expr path ns-binding))) |
| 1811 |
(let ((func (car lst)) |
| 1812 |
(path (cadr lst)) |
| 1813 |
(id-ind? (or index-required (caddr lst)))) |
| 1814 |
(let ((new-path (sxml:parse-check "or" path sxml:delimiter))) |
| 1815 |
(if (not new-path) ; no more AndExprs |
| 1816 |
(if (null? funcs) ; only one AndExpr |
| 1817 |
(list func path id-ind?) |
| 1818 |
(list |
| 1819 |
(lambda (nodeset root-node context var-binding) |
| 1820 |
(let rpt ((fs (reverse (cons func funcs)))) |
| 1821 |
(cond |
| 1822 |
((null? fs) #f) |
| 1823 |
((sxml:boolean |
| 1824 |
((car fs) nodeset root-node context |
| 1825 |
var-binding)) #t) |
| 1826 |
(else (rpt (cdr fs)))))) |
| 1827 |
path |
| 1828 |
id-ind?)) |
| 1829 |
(loop (cons func funcs) new-path id-ind?))))))) |
| 1830 |
|
| 1831 |
;------------------------------------------------ |
| 1832 |
; Functions which parse XPointer grammar |
| 1833 |
; |
| 1834 |
; The return value has the same signature for all these functions (and it |
| 1835 |
; coincides with the signature from the previous section) |
| 1836 |
; (list (lambda (nodeset root-node context var-binding) ...) |
| 1837 |
; path |
| 1838 |
; index-required ) |
| 1839 |
; or #f |
| 1840 |
; #f signals of a parse error (error message is printed as a side effect |
| 1841 |
; during parsing) |
| 1842 |
; |
| 1843 |
; (lambda (nodeset root-node context var-binding) - an SXPath-like |
| 1844 |
; function (it transforms a nodeset into a new nodeset) |
| 1845 |
; nodeset - a current set of nodes |
| 1846 |
; root-node - the root of a document (a singleton nodeset) |
| 1847 |
; context - the context of the node: context = (cons position size) |
| 1848 |
; position - context position (a number) |
| 1849 |
; size - context size (a number) |
| 1850 |
; path - an XPointer path represented as the list of chars |
| 1851 |
; index-required - boolean value: whether an id-index was required "deeper" |
| 1852 |
; within the grammar |
| 1853 |
|
| 1854 |
|
| 1855 |
; Parses an FullXPtr production ([3]-[10] in XPointer specification) |
| 1856 |
; [3] FullXPtr ::= XPtrPart (S? XPtrPart)* |
| 1857 |
; [4] XPtrPart ::= 'xpointer' '(' XPtrExpr ')' |
| 1858 |
; | 'xmlns' '(' XPtrNsDecl? ')' |
| 1859 |
; | Scheme '(' SchemeSpecificExpr ')' |
| 1860 |
; [5] Scheme ::= NCName |
| 1861 |
; [6] SchemeSpecificExpr ::= StringWithBalancedParens |
| 1862 |
; [7] StringWithBalancedParens ::= |
| 1863 |
; [^()]* ('(' StringWithBalancedParens ')' [^()]*)* |
| 1864 |
; [8] XPtrExpr ::= Expr |
| 1865 |
; [9] XPtrNsDecl ::= NCName S? '=' S? XPtrNsURI |
| 1866 |
; [10] XPtrNsURI ::= Char* |
| 1867 |
(define (sxml:parse-full-xptr path ns-binding) |
| 1868 |
(let loop ((funcs '()) |
| 1869 |
(ns-binding ns-binding) |
| 1870 |
(path path) |
| 1871 |
(index-required #f)) |
| 1872 |
(if (null? (sxml:skip-ws path)) ; the string is over |
| 1873 |
(if (= (length funcs) 1) |
| 1874 |
(list (car funcs) path index-required) |
| 1875 |
(list (lambda (nodeset root-node context var-binding) |
| 1876 |
(let rpt ((fs (reverse funcs))) |
| 1877 |
(if (null? fs) |
| 1878 |
'() |
| 1879 |
(let ((nset ((car fs) nodeset root-node context |
| 1880 |
var-binding))) |
| 1881 |
(if (null? nset) |
| 1882 |
(rpt (cdr fs)) |
| 1883 |
nset))))) |
| 1884 |
path |
| 1885 |
index-required)) |
| 1886 |
(and-let* ((lst (sxml:parse-name path)) |
| 1887 |
(name (car lst)) |
| 1888 |
(path (cadr lst))) |
| 1889 |
(cond |
| 1890 |
((string=? name "xpointer") |
| 1891 |
(and-let* ((path (sxml:parse-assert "(" path)) |
| 1892 |
(lst2 (sxml:parse-expr path ns-binding))) |
| 1893 |
(let ((func (car lst2)) |
| 1894 |
(path (cadr lst2)) |
| 1895 |
(bool (caddr lst2))) |
| 1896 |
(and-let* ((path (sxml:parse-assert ")" path))) |
| 1897 |
(loop (cons func funcs) ns-binding |
| 1898 |
path (or index-required bool)))))) |
| 1899 |
((string=? name "xmlns") |
| 1900 |
(and-let* ((path0 (sxml:parse-assert "(" path)) |
| 1901 |
(lst2 (sxml:parse-ncname path0)) |
| 1902 |
(prefix (string->symbol (car lst2))) |
| 1903 |
(path (sxml:parse-assert "=" (cadr lst2)))) |
| 1904 |
(let rpt2 ((path (sxml:skip-ws path)) (uri '())) |
| 1905 |
(cond |
| 1906 |
((null? path) |
| 1907 |
(sxml:parse-assert ")" path) |
| 1908 |
#f) |
| 1909 |
((and (char=? (car path) #\)) (null? uri)) |
| 1910 |
(sxml:xpointer-parse-error |
| 1911 |
"namespace URI cannot be empty")) |
| 1912 |
((char=? (car path) #\)) |
| 1913 |
(loop funcs |
| 1914 |
(cons |
| 1915 |
(cons prefix (list->string (reverse uri))) |
| 1916 |
ns-binding) |
| 1917 |
(cdr path) |
| 1918 |
index-required)) |
| 1919 |
(else (rpt2 (cdr path) (cons (car path) uri))))))) |
| 1920 |
(else (and-let* ((path (sxml:parse-assert "(" path))) |
| 1921 |
(let rpt3 ((n 1) (path path)) |
| 1922 |
(cond |
| 1923 |
((= n 0) |
| 1924 |
(sxml:xpointer-parse-warning |
| 1925 |
"unknown xpointer schema - " name ". Ignoring") |
| 1926 |
(loop funcs ns-binding path index-required)) |
| 1927 |
((null? path) |
| 1928 |
(sxml:parse-assert ")" path) |
| 1929 |
#f) |
| 1930 |
((char=? (car path) #\() (rpt3 (+ n 1) (cdr path))) |
| 1931 |
((char=? (car path) #\)) (rpt3 (- n 1) (cdr path))) |
| 1932 |
(else (rpt3 n (cdr path)))))))))))) |
| 1933 |
|
| 1934 |
; Parses an ChildSeq production ([2] in XPointer specification) |
| 1935 |
; [2] ChildSeq ::= Name? ('/' [1-9] [0-9]* )+ |
| 1936 |
(define (sxml:parse-child-seq path) |
| 1937 |
|
| 1938 |
(define (helper path) |
| 1939 |
(let loop ((funcs '()) (path path)) |
| 1940 |
(let ((path2 (sxml:parse-check "/" path))) |
| 1941 |
(if (not path2) ; no more #\/ |
| 1942 |
(if (null? (sxml:skip-ws path)) ; end of path |
| 1943 |
(reverse funcs) |
| 1944 |
(sxml:parse-assert "/" path)) ; this will cause an error message |
| 1945 |
(and-let* ((lst (sxml:parse-natural path2))) |
| 1946 |
(loop (cons |
| 1947 |
(node-pos (car lst)) |
| 1948 |
(cons (sxml:child (ntype?? '*)) funcs)) |
| 1949 |
(cadr lst))))))) |
| 1950 |
|
| 1951 |
(let ((path2 (sxml:parse-check "/" path))) |
| 1952 |
(if |
| 1953 |
(not path2) |
| 1954 |
(and-let* |
| 1955 |
((lst (sxml:parse-name path)) |
| 1956 |
(name (car lst)) |
| 1957 |
(funcs (helper (cadr lst)))) |
| 1958 |
(list |
| 1959 |
(lambda (nodeset root-node context var-binding) |
| 1960 |
(let* ((id-nset ((sxml:child (ntype?? 'id-index)) |
| 1961 |
((sxml:child (ntype?? '@@)) root-node)))) |
| 1962 |
(if |
| 1963 |
(null? id-nset) ; no id-index |
| 1964 |
'() |
| 1965 |
(let ((nd (sxml:lookup name (cdar id-nset)))) |
| 1966 |
(if (not nd) |
| 1967 |
'() |
| 1968 |
(let rpt ((nset (list nd)) |
| 1969 |
(fs funcs)) |
| 1970 |
(if (null? fs) |
| 1971 |
nset |
| 1972 |
(rpt ((car fs) nset) (cdr fs))))))))) |
| 1973 |
'() |
| 1974 |
#t)) |
| 1975 |
(and-let* |
| 1976 |
((funcs (helper path))) |
| 1977 |
(list |
| 1978 |
(lambda (nodeset root-node context var-binding) |
| 1979 |
(if (nodeset? nodeset) |
| 1980 |
(let rpt ((nodeset nodeset) (res '())) |
| 1981 |
(if (null? nodeset) |
| 1982 |
res |
| 1983 |
(let rpt2 ((nset (list (car nodeset))) |
| 1984 |
(fs funcs)) |
| 1985 |
(if (null? fs) |
| 1986 |
(rpt (cdr nodeset) (append res nset)) |
| 1987 |
(rpt2 ((car fs) nset) (cdr fs)))))) |
| 1988 |
(let rpt ((nodeset nodeset) |
| 1989 |
(fs funcs)) |
| 1990 |
(if (null? fs) |
| 1991 |
nodeset |
| 1992 |
(rpt ((car fs) nodeset) (cdr fs)))))) |
| 1993 |
'() |
| 1994 |
#f))))) |
| 1995 |
|
| 1996 |
; Parses an XPointer production ([1] in XPointer specification) |
| 1997 |
; [1] XPointer ::= Name | ChildSeq | FullXPtr |
| 1998 |
(define (sxml:parse-xpointer path ns-binding) |
| 1999 |
(if (sxml:parse-check "/" path) ; #\/ is the first symbol => ChildSeq |
| 2000 |
(sxml:parse-child-seq path) |
| 2001 |
(and-let* ((lst (sxml:parse-name path)) |
| 2002 |
(new-path (cadr lst))) |
| 2003 |
(if (sxml:parse-check "(" new-path) ; FullXPtr production |
| 2004 |
(sxml:parse-full-xptr path ns-binding) |
| 2005 |
(sxml:parse-child-seq path))))) |
| 2006 |
|
| 2007 |
;========================================================================= |
| 2008 |
; Highest level API functions |
| 2009 |
|
| 2010 |
;------------------------------------------------ |
| 2011 |
; 'sxml:xpath' and 'sxml:xpointer' functions |
| 2012 |
; |
| 2013 |
; xpath-string - an XPath query (a string) |
| 2014 |
; ns-binding - declared namespace prefixes (an optional argument) |
| 2015 |
; ns-binding = (list (prefix . uri) |
| 2016 |
; (prefix . uri) |
| 2017 |
; ...) |
| 2018 |
; prefix - a symbol |
| 2019 |
; uri - a string |
| 2020 |
; |
| 2021 |
; The returned result: (lambda (node) ...) |
| 2022 |
; or #f |
| 2023 |
; #f - signals of a parse error (error message is printed as a side effect |
| 2024 |
; during parsing) |
| 2025 |
; (lambda (node) ...) - an SXPath function |
| 2026 |
; node - a root node of the SXML document |
| 2027 |
|
| 2028 |
(define (sxml:api-helper parse-proc) |
| 2029 |
(lambda (xpath-string . ns-binding) |
| 2030 |
(and-let* |
| 2031 |
((res (parse-proc |
| 2032 |
(string->list xpath-string) |
| 2033 |
(if (null? ns-binding) ns-binding (car ns-binding))))) |
| 2034 |
(let ((func (car res))) |
| 2035 |
(lambda (node) |
| 2036 |
(let ((node (if (nodeset? node) node (list node)))) |
| 2037 |
(func node node (cons 1 1) '()))))))) |
| 2038 |
|
| 2039 |
(define sxml:xpath (sxml:api-helper sxml:parse-xpath)) |
| 2040 |
|
| 2041 |
(define sxml:xpointer (sxml:api-helper sxml:parse-xpointer)) |
| 2042 |
|
| 2043 |
|
| 2044 |
;------------------------------------------------ |
| 2045 |
; 'sxml:xpath+index' and 'sxml:xpointer+index' functions |
| 2046 |
; |
| 2047 |
; xpath-string - an XPath query (a string) |
| 2048 |
; ns-binding - declared namespace prefixes (an optional argument) |
| 2049 |
; ns-binding = (list (prefix . uri) |
| 2050 |
; (prefix . uri) |
| 2051 |
; ...) |
| 2052 |
; prefix - a symbol |
| 2053 |
; uri - a string |
| 2054 |
; |
| 2055 |
; The returned result: (cons (lambda (node . id-index) ...) |
| 2056 |
; index-required ) |
| 2057 |
; or #f |
| 2058 |
; #f - signals of a parse error (error message is printed as a side effect |
| 2059 |
; during parsing) |
| 2060 |
; (lambda (node) ...) - an SXPath function |
| 2061 |
; node - a root node of the SXML document |
| 2062 |
; index-required - a boolean value: whether an id-index is required |
| 2063 |
|
| 2064 |
(define (sxml:api-index-helper parse-proc) |
| 2065 |
(lambda (xpath-string . ns-binding) |
| 2066 |
(and-let* |
| 2067 |
((res (parse-proc |
| 2068 |
(string->list xpath-string) |
| 2069 |
(if (null? ns-binding) ns-binding (car ns-binding))))) |
| 2070 |
(let ((func (car res))) |
| 2071 |
(cons |
| 2072 |
(lambda (node) |
| 2073 |
(let ((node (if (nodeset? node) node (list node)))) |
| 2074 |
(func node node (cons 1 1) '()))) |
| 2075 |
(caddr res)))))) |
| 2076 |
|
| 2077 |
(define sxml:xpath+index (sxml:api-index-helper sxml:parse-xpath)) |
| 2078 |
|
| 2079 |
(define sxml:xpointer+index (sxml:api-index-helper sxml:parse-xpointer)) |
| 2080 |
|
| 2081 |
|
| 2082 |
;------------------------------------------------ |
| 2083 |
; 'sxml:xpath+root+vars' and 'sxml:xpointer+root+vars' functions |
| 2084 |
; |
| 2085 |
; xpath-string - an XPath query (a string) |
| 2086 |
; ns-binding - declared namespace prefixes (an optional argument) |
| 2087 |
; ns-binding = (list (prefix . uri) |
| 2088 |
; (prefix . uri) |
| 2089 |
; ...) |
| 2090 |
; prefix - a symbol |
| 2091 |
; uri - a string |
| 2092 |
; |
| 2093 |
; The returned result: (lambda (node root-node . var-binding) ...) |
| 2094 |
; or #f |
| 2095 |
; #f - signals of a parse error (error message is printed as a side effect |
| 2096 |
; during parsing) |
| 2097 |
; (lambda (node root-node . var-binding) ...) - an SXPath function |
| 2098 |
; node - a node (or a node-set) of the SXML document |
| 2099 |
; root-node - the root of the document |
| 2100 |
; var-binding = (list (var-name . value) |
| 2101 |
; (var-name . value) |
| 2102 |
; ...) |
| 2103 |
; var-name - (a symbol) a name of a variable |
| 2104 |
; value - its value. The value can have the following type: boolean, number, |
| 2105 |
; string, nodeset. NOTE: a node must be represented as a singleton nodeset |
| 2106 |
|
| 2107 |
(define (sxml:api-root-vars-helper parse-proc) |
| 2108 |
(lambda (xpath-string . ns-binding) |
| 2109 |
(and-let* |
| 2110 |
((res (parse-proc |
| 2111 |
(string->list xpath-string) |
| 2112 |
(if (null? ns-binding) ns-binding (car ns-binding))))) |
| 2113 |
(let ((func (car res))) |
| 2114 |
(lambda (node . root-var-binding) |
| 2115 |
(let ((root-node |
| 2116 |
(if (null? root-var-binding) node (car root-var-binding)))) |
| 2117 |
(func |
| 2118 |
(if (nodeset? node) node (list node)) |
| 2119 |
(if (nodeset? root-node) root-node (list root-node)) |
| 2120 |
(cons 1 1) |
| 2121 |
(if (or (null? root-var-binding) (null? (cdr root-var-binding))) |
| 2122 |
'() (cadr root-var-binding))))))))) |
| 2123 |
|
| 2124 |
(define sxml:xpath+root+vars (sxml:api-root-vars-helper sxml:parse-xpath)) |
| 2125 |
|
| 2126 |
(define sxml:xpointer+root+vars (sxml:api-root-vars-helper sxml:parse-xpointer)) |
| 2127 |
|
| 2128 |
; A stub |
| 2129 |
(define sxml:xpath+root sxml:xpath+root+vars) |
| 2130 |
|
| 2131 |
(define txpath sxml:xpath+root+vars) |