aboutsummaryrefslogtreecommitdiff
path: root/module/sxml/namespaced/xpath.scm
blob: 5b8cdb82d431206ebf289d26ce4af8e8d3fc7ce3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
(define-module (sxml namespaced xpath)
  :use-module (ice-9 curried-definitions)
  :use-module (srfi srfi-1)
  :export (nodeset?
           node-typeof?
           node-eq?
           node-equal?
           node-pos
           filter
           take-until
           take-after
           map-union
           node-reverse
           node-trace

           select-kids
           node-self
           node-join
           node-reduce
           node-or
           node-closure
           node-parent
           sxpath
           ))


;;; Basic Converters and Applicators

(define (node? x)
  (or (xml-element? x)
      (table? x)
      ;; table-entry? x
      (string? x)
      (pi-element? x)))

(define (nodeset? x)
  (every node? x))


(define (node-typeof? crit)
  (case crit
    ((@) table?)
    ((*) xml-element*?)
    ((*text*) string?)
    ((*PI*) pi-element?)
    ((*any*) (const #t))
    (else (and (xml-element*? crit)
               (xml-element-tagname crit)))))

(define ((node-eq? other) self)
  (eq? other self))

(define ((node-equal? other) self)
  (equal? other self))


(define ((node-pos n) nodeset)
  ;; 0 is neither
  (cond ((positive? n)
         (list-ref nodeset (1- n)))
        ((negative? n)
         (list-ref nodeset
                   (+ n (length nodeset))))))

(define (filter pred?)
  'TODO
  )


(define (take-until pred?)
  ;; TODO
  )

(define (take-after pred?)
  ;; TODO
  )


(define (map-union proc lst)
  ;; TODO this can be really optimized with better data structures
  (fold (lambda (x done)
          (let ((result (proc x)))
            (if (list? result)
                (append done result)
                (append done (list result)))))
        '()
        lst))


(define (node-reverse node-or-nodeset)
  (if (node? node-or-nodeset)
      (list node-or-nodeset)
      (reverse node-or-nodeset)))

(define ((node-trace title) node-or-nodeset)
  (format #t "~a~%" title)
  (if (node? node-or-nodeset)
      (list node-or-nodeset)
      node-or-nodeset))


;;; Converter Combinators

(define ((select-kids test-pred?) node-or-nodeset)
  (cond ((node? node-or-nodeset)
         ;; TODO Expand if node has children
         )
        (else                         ; Is a nodeset
         (map-union (select-kids test-pred?) node-or-nodeset))))

(define node-self filter)


;;; Takes a list of selectors, and returns a new one
;;; (node-join f) ⇒ f
;;; (node-join) ⇒ TODO
;;; (node-join f g) ⇒ TODO
(define (node-join . selectors)
  ;; TODO
  )

(define ((node-reduce . converters) nodeset)
  (foldl apply nodeset converters))

(define (node-or . converters)
  ;; for each converter
  ;;     get all matching in nodeset
  ;; then take the union of these
  ;; Return a new converter with these properties
  )


(define (node-closure test-pred?)
  (node-or
   (select-kids test-pred?)
   (node-reduce (select-kids (node-typeof? '*))
                (node-closure test-pred?))))

(define (node-parent rootnode)
  ;; TODO return converter
  )



(define (sxpath path)
  (match path
    ('() (node-join))
    ((x xs ...) (node-join (sxpath1 x) (sxpath xs)))))


(define (sxpath1 x)
  (match x
    ('//
     (node-or
      ;; The node itself
      (node-self (node-typeof? '*any*))
      ;; All ((...) grand) children
      (node-closure (node-typeof? '*any*))))
    (('equal? x)
     (select-kids (node-equal? x)))
    (('eq? x)
     (select-kids (node-eq? x)))
    ((? symbol? ?symbol)
     (select-kids (node-typeof? ?symbol)))
    ((? procedure? procedure)
     procedure)
    (((? symbol? ?symbol) rest ...)
     (sxpath1 `((?symbol) ,@rest)))
    ((path reducer ...)
     (apply node-reduce (sxpath path)
            (map sxpathr reducer)))))

(define (sxpathr x)
  (cond ((number? x) (node-pos x))
        (else (filter (sxpath x)))))