![]() |
![]() |
|
home—info—lectures—exams—archive
Remainder of semester:
Today (as a review/preview of interpreting), we'll revisit and exand code which processes (xml) trees:
1 #lang scheme
2
3 ;;;; This file handles xml in scheme.
4 ;;;; It differs from xnode.ss in three important ways:
5 ;;;; - our data-definition allows attributes in xml tags;
6 ;;;; - we don't use structs -- just lists (of lists of ...)
7 ;;;; - we write some functions which process xml trees.
8
9
10 ; An xnode is:
11 ; - an string, or
12 ; - a xtag
13
14 ; An xtag is:
15 ; (list symbol (list-of attr+val) xnode...)
16 ;
17 ; Note that the '...' is shorthand for 0-or-more; we really mean:
18 ; (cons [symbol] (cons (list-of attr+val) (list-of xnode)))
19
20 ; An attr+val is:
21 ; (list string string)
22
23
24
25 ;; Examples of the data:
26 ;; xnode:
27 (define x1 "hello there")
28 (define x2 '(em {} "wowy zowy"))
29 (define x3 '(p {} "hello" (em {} "wowy zowy")))
30 (define x3 '(p {["align" "center"] ["style" "footnote"]}
31 "hello"
32 (em {} "wowy zowy")))
33
34 ;; Here is the result of parsing a string:
35 "<p align="center" style="footnote"><em>hi</em> to all the <b>very <b>very <b><b>very</b></b></b></b></p>"
36
37 ;;; TODO IN CLASS
38
39
40 (define xtag-tag first)
41 (define xtag-attrs second)
42 (define (xtag-contents x) (rest (rest x)))
43 (define (xtag? x) (and (symbol? (xtag-tag x))
44 (attrs? (xtag-attrs x))
45 (xlist? (xtag-contents x))))
46 (define (attr+val? a+v) (and (list? a+v)
47 (= 2 (length a+v))
48 (string? (first a+v))
49 (string? (second a+v))))
50 (define (attrs? a+vs) ... )
51 (define (xlist? xx) ... )
52
53
54 ;; We won't actually call 'make-xtag' (since
55 ;; we'll just make the lists manually),
56 ;; but there's no reason why we couldn't:
57 ;;
58 (define (make-xtag tag attrs contents)
59 (cons tag (cons attrs contents)))
60
61
62 (define (xnode->string x)
63 (cond [(string? x) x]
64 [(xtag? x) (string-append "<" (symbol->string (xtag-tag x)) ">"
65 (foldl (lambda (f rr) (string-append (xnode->string f) rr)) "" (xtag-contents x))
66 "</" (symbol->string (xtag-tag x)) ">")]))
67
68
69
70 ; Example handlers
71
72 ;; (-> xtag xtag)
73 ;; Transform an "em" tag into one
74 ;; which is "i" *and* font w/ color "blue"
75 (define (handle-em x)
76 ...)
77
78
79 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
80 ;;; Code to *process* an xml tree.
81 ;;; We'll pass in a association-list [java.util.ListMap]
82 ;;; of (tag-name + (-> xnode xnode)).
83
84 (define (process xnode processors)
85 (cond [(string? xnode) xnode]
86 [(xtag? xnode) ...]))
87 |
Then, we'll implement a more general tree-processor,
which passes in its own environments, in a way which
could also be used to implement dynamic scope.
(Though since we are processing only xml -- recursive data --
in a structural way, it still 'feels' similar to static scope.)
home—info—lectures—exams—archive
| ©2008, Ian Barland, Radford University Last modified 2008.Oct.31 (Fri) |
Please mail any suggestions (incl. typos, broken links) to ibarland |
![]() |