-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathSSAX-code.sch
335 lines (334 loc) · 13.5 KB
/
SSAX-code.sch
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
; $Id: SSAX.scm,v 1.8 2003/08/02 02:14:18 oleg Exp $
; DL: let*-values rewritten to call-with-values
(define-macro run-test
(lambda body
(define (re-write body)
(cond
((vector? body) (list->vector (re-write (vector->list body))))
((not (pair? body)) body)
((and (eq? 'quote (car body)) (pair? (cdr body)) (string? (cadr body)))
(string->symbol (cadr body)))
(else (cons (re-write (car body)) (re-write (cdr body))))))
(cons 'begin (re-write body))))
(define-macro xml-token-kind (lambda (token) `(car ,token)))
(define-macro xml-token-head (lambda (token) `(cdr ,token)))
(define-macro ssax:make-pi-parser
(lambda (my-pi-handlers)
`(lambda (port target seed)
(case target
(unquote-splicing
(let loop ((pi-handlers my-pi-handlers) (default #f))
(cond
((null? pi-handlers)
(if default
`((else (,default port target seed)))
'((else
(ssax:warn port "Skipping PI: " target nl)
(ssax:skip-pi port)
seed))))
((eq? '*DEFAULT* (caar pi-handlers))
(loop (cdr pi-handlers) (cdar pi-handlers)))
(else
(cons
`((,(caar pi-handlers)) (,(cdar pi-handlers) port target seed))
(loop (cdr pi-handlers) default))))))))))
(define-macro ssax:make-elem-parser
(lambda (my-new-level-seed
my-finish-element
my-char-data-handler
my-pi-handlers)
`(lambda (start-tag-head port elems entities namespaces preserve-ws? seed)
(define xml-space-gi (cons ssax:Prefix-XML (string->symbol "space")))
(let handle-start-tag ((start-tag-head start-tag-head)
(port port)
(entities entities)
(namespaces namespaces)
(preserve-ws? preserve-ws?)
(parent-seed seed))
(call-with-values
(lambda () (ssax:complete-start-tag
start-tag-head
port
elems
entities
namespaces))
(lambda (elem-gi attributes namespaces expected-content)
(let ((seed (,my-new-level-seed
elem-gi
attributes
namespaces
expected-content
parent-seed)))
(case expected-content
((EMPTY-TAG)
(,my-finish-element
elem-gi
attributes
namespaces
parent-seed
seed))
((EMPTY)
(ssax:assert-token
(and (eqv? #\< (ssax:skip-S port))
(ssax:read-markup-token port))
'END
start-tag-head
(lambda (token exp-kind exp-head)
(parser-error
port
"[elementvalid] broken for "
token
" while expecting "
exp-kind
exp-head)))
(,my-finish-element
elem-gi
attributes
namespaces
parent-seed
seed))
(else
(let ((preserve-ws?
(cond
((assoc xml-space-gi attributes)
=>
(lambda (name-value)
(equal? "preserve" (cdr name-value))))
(else preserve-ws?))))
(let loop ((port port)
(entities entities)
(expect-eof? #f)
(seed seed))
(call-with-values
(lambda () (ssax:read-char-data
port
expect-eof?
,my-char-data-handler
seed))
(lambda (seed term-token)
(if (eof-object? term-token)
seed
(case (xml-token-kind term-token)
((END)
(ssax:assert-token
term-token
'END
start-tag-head
(lambda (token exp-kind exp-head)
(parser-error
port
"[GIMatch] broken for "
term-token
" while expecting "
exp-kind
exp-head)))
(,my-finish-element
elem-gi
attributes
namespaces
parent-seed
seed))
((PI)
(let ((seed
((ssax:make-pi-parser ,my-pi-handlers)
port
(xml-token-head term-token)
seed)))
(loop port entities expect-eof? seed)))
((ENTITY-REF)
(let ((seed
(ssax:handle-parsed-entity
port
(xml-token-head term-token)
entities
(lambda (port entities seed)
(loop port entities #t seed))
,my-char-data-handler
seed)))
(loop port entities expect-eof? seed)))
((START)
(if (eq? expected-content 'PCDATA)
(parser-error
port
"[elementvalid] broken for "
elem-gi
" with char content only; unexpected token "
term-token))
(let ((seed
(handle-start-tag
(xml-token-head term-token)
port
entities
namespaces
preserve-ws?
seed)))
(loop port entities expect-eof? seed)))
(else
(parser-error
port
"XML [43] broken for "
term-token)))))))))))))))))
(define-macro ssax:make-parser
(lambda user-handlers
(define all-handlers
'((DOCTYPE
lambda
(port docname systemid internal-subset? seed)
(when internal-subset?
(ssax:warn port "Internal DTD subset is not currently handled ")
(ssax:skip-internal-dtd port))
(ssax:warn
port
"DOCTYPE DECL "
docname
" "
systemid
" found and skipped")
(values #f '() '() seed))
(UNDECL-ROOT lambda (elem-gi seed) (values #f '() '() seed))
(DECL-ROOT lambda (elem-gi seed) seed)
(NEW-LEVEL-SEED . REQD)
(FINISH-ELEMENT . REQD)
(CHAR-DATA-HANDLER . REQD)
(PI)))
(define (delete-assoc alist tag cont)
(let loop ((alist alist) (scanned '()))
(cond
((null? alist) (error "Unknown user-handler-tag: " tag))
((eq? tag (caar alist))
(cont tag (cdar alist) (append scanned (cdr alist))))
(else (loop (cdr alist) (cons (car alist) scanned))))))
(define (merge-handlers declared-handlers given-handlers)
(cond
((null? given-handlers)
(cond
((null? declared-handlers) '())
((not (eq? 'REQD (cdar declared-handlers)))
(cons
(car declared-handlers)
(merge-handlers (cdr declared-handlers) given-handlers)))
(else
(error
"The handler for the tag "
(caar declared-handlers)
" must be specified"))))
((null? (cdr given-handlers))
(error "Odd number of arguments to ssax:make-parser"))
(else
(delete-assoc
declared-handlers
(car given-handlers)
(lambda (tag value alist)
(cons
(cons tag (cadr given-handlers))
(merge-handlers alist (cddr given-handlers))))))))
(let ((user-handlers (merge-handlers all-handlers user-handlers)))
(define (get-handler tag)
(cond
((assq tag user-handlers) => cdr)
(else (error "unknown tag: " tag))))
`(lambda (port seed)
(define (handle-decl port token-head seed)
(or (eq? (string->symbol "DOCTYPE") token-head)
(parser-error
port
"XML [22], expected DOCTYPE declaration, found "
token-head))
(assert-curr-char ssax:S-chars "XML [28], space after DOCTYPE" port)
(ssax:skip-S port)
(let* ((docname (ssax:read-QName port))
(systemid
(and (ssax:ncname-starting-char? (ssax:skip-S port))
(ssax:read-external-id port)))
(internal-subset?
(begin
(ssax:skip-S port)
(eqv?
#\[
(assert-curr-char
'(#\> #\[)
"XML [28], end-of-DOCTYPE"
port)))))
(call-with-values
(lambda () (,(get-handler 'DOCTYPE)
port
docname
systemid
internal-subset?
seed))
(lambda (elems entities namespaces seed)
(scan-for-significant-prolog-token-2
port
elems
entities
namespaces
seed)))))
(define (scan-for-significant-prolog-token-1 port seed)
(let ((token (ssax:scan-Misc port)))
(if (eof-object? token)
(parser-error port "XML [22], unexpected EOF")
(case (xml-token-kind token)
((PI)
(let ((seed
((ssax:make-pi-parser ,(get-handler 'PI))
port
(xml-token-head token)
seed)))
(scan-for-significant-prolog-token-1 port seed)))
((DECL) (handle-decl port (xml-token-head token) seed))
((START)
(call-with-values
(lambda () (,(get-handler 'UNDECL-ROOT)
(xml-token-head token)
seed))
(lambda (elems entities namespaces seed)
(element-parser
(xml-token-head token)
port
elems
entities
namespaces
#f
seed))))
(else
(parser-error port "XML [22], unexpected markup " token))))))
(define (scan-for-significant-prolog-token-2
port
elems
entities
namespaces
seed)
(let ((token (ssax:scan-Misc port)))
(if (eof-object? token)
(parser-error port "XML [22], unexpected EOF")
(case (xml-token-kind token)
((PI)
(let ((seed
((ssax:make-pi-parser ,(get-handler 'PI))
port
(xml-token-head token)
seed)))
(scan-for-significant-prolog-token-2
port
elems
entities
namespaces
seed)))
((START)
(element-parser
(xml-token-head token)
port
elems
entities
namespaces
#f
(,(get-handler 'DECL-ROOT) (xml-token-head token) seed)))
(else
(parser-error port "XML [22], unexpected markup " token))))))
(define element-parser
(ssax:make-elem-parser
,(get-handler 'NEW-LEVEL-SEED)
,(get-handler 'FINISH-ELEMENT)
,(get-handler 'CHAR-DATA-HANDLER)
,(get-handler 'PI)))
(scan-for-significant-prolog-token-1 port seed)))))