1 ;;;; (sxml fold) -- transformation of sxml via fold operations
3 ;;;; Copyright (C) 2009, 2010 Free Software Foundation, Inc.
4 ;;;; Written 2007 by Andy Wingo <wingo at pobox dot com>.
6 ;;;; This library is free software; you can redistribute it and/or
7 ;;;; modify it under the terms of the GNU Lesser General Public
8 ;;;; License as published by the Free Software Foundation; either
9 ;;;; version 3 of the License, or (at your option) any later version.
11 ;;;; This library is distributed in the hope that it will be useful,
12 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ;;;; Lesser General Public License for more details.
16 ;;;; You should have received a copy of the GNU Lesser General Public
17 ;;;; License along with this library; if not, write to the Free Software
18 ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 ;; @code{(sxml fold)} defines a number of variants of the @dfn{fold}
24 ;; algorithm for use in transforming SXML trees. Additionally it defines
25 ;; the layout operator, @code{fold-layout}, which might be described as
26 ;; a context-passing variant of SSAX's @code{pre-post-order}.
30 (define-module (sxml fold)
31 #:use-module (srfi srfi-1)
42 (define (foldt fup fhere tree)
43 "The standard multithreaded tree fold.
45 @var{fup} is of type [a] -> a. @var{fhere} is of type object -> a.
49 (fup (map (lambda (kid)
50 (foldt fup fhere kid))
53 (define (foldts fdown fup fhere seed tree)
54 "The single-threaded tree fold originally defined in SSAX.
55 @xref{sxml ssax,,(sxml ssax)}, for more information."
59 (fold (lambda (kid kseed)
60 (foldts fdown fup fhere kseed kid))
65 (define (foldts* fdown fup fhere seed tree)
66 "A variant of @ref{sxml fold foldts,,foldts} that allows pre-order
67 tree rewrites. Originally defined in Andy Wingo's 2007 paper,
68 @emph{Applications of fold to XML transformation}."
72 (lambda () (fdown seed tree))
75 (fold (lambda (kid kseed)
76 (foldts* fdown fup fhere
82 (define (fold-values proc list . seeds)
83 "A variant of @ref{SRFI-1 Fold and Map, fold} that allows multi-valued
84 seeds. Note that the order of the arguments differs from that of
89 (lambda () (apply proc (car list) seeds))
91 (apply fold-values proc (cdr list) seeds)))))
93 (define (foldts*-values fdown fup fhere tree . seeds)
94 "A variant of @ref{sxml fold foldts*,,foldts*} that allows
95 multi-valued seeds. Originally defined in Andy Wingo's 2007 paper,
96 @emph{Applications of fold to XML transformation}."
98 (apply fhere tree seeds)
100 (lambda () (apply fdown tree seeds))
101 (lambda (tree . kseeds)
105 (lambda (tree . seeds)
106 (apply foldts*-values
107 fdown fup fhere tree seeds))
110 (apply fup tree (append seeds kseeds))))))))
112 (define (assq-ref alist key default)
113 (cond ((assq key alist) => cdr)
116 (define (fold-layout tree bindings params layout stylesheet)
117 "A traversal combinator in the spirit of SSAX's @ref{sxml transform
118 pre-post-order,,pre-post-order}.
120 @code{fold-layout} was originally presented in Andy Wingo's 2007 paper,
121 @emph{Applications of fold to XML transformation}.
124 bindings := (<binding>...)
125 binding := (<tag> <bandler-pair>...)
126 | (*default* . <post-handler>)
127 | (*text* . <text-handler>)
129 handler-pair := (pre-layout . <pre-layout-handler>)
130 | (post . <post-handler>)
131 | (bindings . <bindings>)
132 | (pre . <pre-handler>)
133 | (macro . <macro-handler>)
137 @item pre-layout-handler
138 A function of three arguments:
142 the kids of the current node, before traversal
144 the params of the current node
146 the layout coming into this node
149 @var{pre-layout-handler} is expected to use this information to return a
150 layout to pass to the kids. The default implementation returns the
151 layout given in the arguments.
154 A function of five arguments:
157 the current tag being processed
159 the params of the current node
161 the layout coming into the current node, before any kids were processed
163 the layout after processing all of the children
165 the already-processed child nodes
168 @var{post-handler} should return two values, the layout to pass to the
169 next node and the final tree.
172 @var{text-handler} is a function of three arguments:
182 @var{text-handler} should return two values, the layout to pass to the
183 next node and the value to which the string should transform.
187 (error "no binding available" args))
188 (define (fdown tree bindings pcont params layout ret)
189 (define (fdown-helper new-bindings new-layout cont)
190 (let ((cont-with-tag (lambda args
191 (apply cont (car tree) args)))
192 (bindings (if new-bindings
193 (append new-bindings bindings)
195 (style-params (assq-ref stylesheet (car tree) '())))
199 '() bindings cont-with-tag (cons style-params params) new-layout '()))
200 ((and (pair? (cadr tree)) (eq? (caadr tree) '@))
201 (let ((params (cons (append (cdadr tree) style-params) params)))
203 (cddr tree) bindings cont-with-tag params new-layout '())))
206 (cdr tree) bindings cont-with-tag (cons style-params params) new-layout '())))))
207 (define (no-bindings)
208 (fdown-helper #f layout (assq-ref bindings '*default* err)))
209 (define (macro macro-handler)
210 (fdown (apply macro-handler tree)
211 bindings pcont params layout ret))
212 (define (pre pre-handler)
214 (lambda (params layout old-layout kids)
215 (values layout (reverse kids)))
216 params layout (apply pre-handler tree)))
217 (define (have-bindings tag-bindings)
219 (assq-ref tag-bindings 'bindings #f)
220 ((assq-ref tag-bindings 'pre-layout
221 (lambda (tag params layout)
224 (assq-ref tag-bindings 'post
225 (assq-ref bindings '*default* err))))
226 (let ((tag-bindings (assq-ref bindings (car tree) #f)))
228 ((not tag-bindings) (no-bindings))
229 ((assq-ref tag-bindings 'macro #f) => macro)
230 ((assq-ref tag-bindings 'pre #f) => pre)
231 (else (have-bindings tag-bindings)))))
232 (define (fup tree bindings cont params layout ret
233 kbindings kcont kparams klayout kret)
236 (kcont kparams layout klayout (reverse kret)))
237 (lambda (klayout kret)
238 (values bindings cont params klayout (cons kret ret)))))
239 (define (fhere tree bindings cont params layout ret)
242 ((assq-ref bindings '*text* err) tree params layout))
243 (lambda (tlayout tret)
244 (values bindings cont params tlayout (cons tret ret)))))
248 fdown fup fhere tree bindings #f (cons params '()) layout '()))
249 (lambda (bindings cont params layout ret)
250 (values (car ret) layout))))