author | aspinall |
Sun, 03 Dec 2006 16:25:33 +0100 | |
changeset 21628 | 7ab9ad8d6757 |
parent 19482 | 9f11af8f7ef9 |
child 21629 | c762bdc2b504 |
permissions | -rw-r--r-- |
12416 | 1 |
(* Title: Pure/General/xml.ML |
2 |
ID: $Id$ |
|
14728 | 3 |
Author: David Aspinall, Stefan Berghofer and Markus Wenzel |
12416 | 4 |
|
14969 | 5 |
Basic support for XML. |
12416 | 6 |
*) |
7 |
||
8 |
signature XML = |
|
9 |
sig |
|
21628
7ab9ad8d6757
Type abbreviations for element args and attributes
aspinall
parents:
19482
diff
changeset
|
10 |
type attributes = (string * string) list |
7ab9ad8d6757
Type abbreviations for element args and attributes
aspinall
parents:
19482
diff
changeset
|
11 |
datatype tree = |
7ab9ad8d6757
Type abbreviations for element args and attributes
aspinall
parents:
19482
diff
changeset
|
12 |
Elem of string * attributes * tree list |
7ab9ad8d6757
Type abbreviations for element args and attributes
aspinall
parents:
19482
diff
changeset
|
13 |
| Text of string |
7ab9ad8d6757
Type abbreviations for element args and attributes
aspinall
parents:
19482
diff
changeset
|
14 |
type element = string * attributes * tree list |
14969 | 15 |
val header: string |
16 |
val text: string -> string |
|
15211
5f54721547a7
Add text_charref to encode a string using character references
aspinall
parents:
15207
diff
changeset
|
17 |
val text_charref: string -> string |
14969 | 18 |
val cdata: string -> string |
21628
7ab9ad8d6757
Type abbreviations for element args and attributes
aspinall
parents:
19482
diff
changeset
|
19 |
val element: string -> attributes -> string list -> string |
13729
1a8dda49fd86
Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents:
12416
diff
changeset
|
20 |
val string_of_tree: tree -> string |
14185
9b3841638c06
Tried to make parser a bit more standard-conforming.
berghofe
parents:
13729
diff
changeset
|
21 |
val parse_content: string list -> tree list * string list |
9b3841638c06
Tried to make parser a bit more standard-conforming.
berghofe
parents:
13729
diff
changeset
|
22 |
val parse_elem: string list -> tree * string list |
9b3841638c06
Tried to make parser a bit more standard-conforming.
berghofe
parents:
13729
diff
changeset
|
23 |
val parse_document: string list -> (string option * tree) * string list |
15142
7b7109f22224
Add scan_comment_whspc to skip space and comments in PGIP stream
aspinall
parents:
15010
diff
changeset
|
24 |
val scan_comment_whspc : string list -> unit * string list |
14969 | 25 |
val tree_of_string: string -> tree |
12416 | 26 |
end; |
27 |
||
28 |
structure XML: XML = |
|
29 |
struct |
|
30 |
||
14969 | 31 |
(** string based representation (small scale) **) |
32 |
||
33 |
val header = "<?xml version=\"1.0\"?>\n"; |
|
34 |
||
35 |
||
36 |
(* text and character data *) |
|
37 |
||
38 |
fun decode "<" = "<" |
|
39 |
| decode ">" = ">" |
|
40 |
| decode "&" = "&" |
|
41 |
| decode "'" = "'" |
|
42 |
| decode """ = "\"" |
|
43 |
| decode c = c; |
|
12416 | 44 |
|
45 |
fun encode "<" = "<" |
|
46 |
| encode ">" = ">" |
|
47 |
| encode "&" = "&" |
|
48 |
| encode "'" = "'" |
|
49 |
| encode "\"" = """ |
|
50 |
| encode c = c; |
|
51 |
||
17756 | 52 |
fun encode_charref c = "&#" ^ Int.toString (ord c) ^ ";" |
15211
5f54721547a7
Add text_charref to encode a string using character references
aspinall
parents:
15207
diff
changeset
|
53 |
|
5f54721547a7
Add text_charref to encode a string using character references
aspinall
parents:
15207
diff
changeset
|
54 |
val text = Library.translate_string encode |
5f54721547a7
Add text_charref to encode a string using character references
aspinall
parents:
15207
diff
changeset
|
55 |
|
17756 | 56 |
val text_charref = translate_string encode_charref; |
12416 | 57 |
|
15207 | 58 |
val cdata = enclose "<![CDATA[" "]]>\n" |
12416 | 59 |
|
14728 | 60 |
|
12416 | 61 |
(* elements *) |
62 |
||
14728 | 63 |
fun attribute (a, x) = a ^ " = \"" ^ text x ^ "\""; |
12416 | 64 |
|
65 |
fun element name atts cs = |
|
66 |
let val elem = space_implode " " (name :: map attribute atts) in |
|
67 |
if null cs then enclose "<" "/>" elem |
|
68 |
else enclose "<" ">" elem ^ implode cs ^ enclose "</" ">" name |
|
69 |
end; |
|
70 |
||
71 |
||
13729
1a8dda49fd86
Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents:
12416
diff
changeset
|
72 |
|
15207 | 73 |
|
14969 | 74 |
(** explicit XML trees **) |
75 |
||
21628
7ab9ad8d6757
Type abbreviations for element args and attributes
aspinall
parents:
19482
diff
changeset
|
76 |
type attributes = (string * string) list |
7ab9ad8d6757
Type abbreviations for element args and attributes
aspinall
parents:
19482
diff
changeset
|
77 |
|
14969 | 78 |
datatype tree = |
21628
7ab9ad8d6757
Type abbreviations for element args and attributes
aspinall
parents:
19482
diff
changeset
|
79 |
Elem of string * attributes * tree list |
14969 | 80 |
| Text of string; |
81 |
||
21628
7ab9ad8d6757
Type abbreviations for element args and attributes
aspinall
parents:
19482
diff
changeset
|
82 |
type element = string * attributes * tree list |
7ab9ad8d6757
Type abbreviations for element args and attributes
aspinall
parents:
19482
diff
changeset
|
83 |
|
14969 | 84 |
fun string_of_tree tree = |
85 |
let |
|
86 |
fun string_of (Elem (name, atts, ts)) buf = |
|
87 |
let val buf' = |
|
88 |
buf |> Buffer.add "<" |
|
89 |
|> fold Buffer.add (separate " " (name :: map attribute atts)) |
|
90 |
in |
|
91 |
if null ts then |
|
92 |
buf' |> Buffer.add "/>" |
|
93 |
else |
|
94 |
buf' |> Buffer.add ">" |
|
95 |
|> fold string_of ts |
|
96 |
|> Buffer.add "</" |> Buffer.add name |> Buffer.add ">" |
|
97 |
end |
|
98 |
| string_of (Text s) buf = Buffer.add (text s) buf; |
|
99 |
in Buffer.content (string_of tree Buffer.empty) end; |
|
100 |
||
101 |
||
102 |
||
103 |
(** XML parsing **) |
|
13729
1a8dda49fd86
Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents:
12416
diff
changeset
|
104 |
|
14728 | 105 |
fun err s (xs, _) = |
106 |
"XML parsing error: " ^ s ^ "\nfound: " ^ quote (Symbol.beginning 100 xs); |
|
13729
1a8dda49fd86
Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents:
12416
diff
changeset
|
107 |
|
14728 | 108 |
val scan_whspc = Scan.any Symbol.is_blank; |
13729
1a8dda49fd86
Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents:
12416
diff
changeset
|
109 |
|
1a8dda49fd86
Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents:
12416
diff
changeset
|
110 |
val scan_special = $$ "&" ^^ Symbol.scan_id ^^ $$ ";" >> decode; |
1a8dda49fd86
Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents:
12416
diff
changeset
|
111 |
|
15216
2fac1f11b7f6
Remove white space skipping in element content; XML specification clearly requires whitespace to be passed to application.
aspinall
parents:
15211
diff
changeset
|
112 |
val parse_chars = Scan.repeat1 (Scan.unless ((* scan_whspc -- *)$$ "<") |
14185
9b3841638c06
Tried to make parser a bit more standard-conforming.
berghofe
parents:
13729
diff
changeset
|
113 |
(scan_special || Scan.one Symbol.not_eof)) >> implode; |
9b3841638c06
Tried to make parser a bit more standard-conforming.
berghofe
parents:
13729
diff
changeset
|
114 |
|
14910 | 115 |
val parse_cdata = Scan.this_string "<![CDATA[" |-- |
116 |
(Scan.repeat (Scan.unless (Scan.this_string "]]>") (Scan.one Symbol.not_eof)) >> |
|
117 |
implode) --| Scan.this_string "]]>"; |
|
14185
9b3841638c06
Tried to make parser a bit more standard-conforming.
berghofe
parents:
13729
diff
changeset
|
118 |
|
9b3841638c06
Tried to make parser a bit more standard-conforming.
berghofe
parents:
13729
diff
changeset
|
119 |
val parse_att = |
14865 | 120 |
Symbol.scan_id --| scan_whspc --| $$ "=" --| scan_whspc -- |
121 |
(($$ "\"" || $$ "'") :-- (fn s => (Scan.repeat (Scan.unless ($$ s) |
|
122 |
(scan_special || Scan.one Symbol.not_eof)) >> implode) --| $$ s) >> snd); |
|
14863 | 123 |
|
14910 | 124 |
val parse_comment = Scan.this_string "<!--" -- |
125 |
Scan.repeat (Scan.unless (Scan.this_string "-->") (Scan.one Symbol.not_eof)) -- |
|
126 |
Scan.this_string "-->"; |
|
13729
1a8dda49fd86
Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents:
12416
diff
changeset
|
127 |
|
15142
7b7109f22224
Add scan_comment_whspc to skip space and comments in PGIP stream
aspinall
parents:
15010
diff
changeset
|
128 |
val scan_comment_whspc = |
7b7109f22224
Add scan_comment_whspc to skip space and comments in PGIP stream
aspinall
parents:
15010
diff
changeset
|
129 |
(scan_whspc >> K()) --| (Scan.repeat (parse_comment |-- (scan_whspc >> K()))); |
7b7109f22224
Add scan_comment_whspc to skip space and comments in PGIP stream
aspinall
parents:
15010
diff
changeset
|
130 |
|
14910 | 131 |
val parse_pi = Scan.this_string "<?" |-- |
132 |
Scan.repeat (Scan.unless (Scan.this_string "?>") (Scan.one Symbol.not_eof)) --| |
|
133 |
Scan.this_string "?>"; |
|
13729
1a8dda49fd86
Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents:
12416
diff
changeset
|
134 |
|
14185
9b3841638c06
Tried to make parser a bit more standard-conforming.
berghofe
parents:
13729
diff
changeset
|
135 |
fun parse_content xs = |
15216
2fac1f11b7f6
Remove white space skipping in element content; XML specification clearly requires whitespace to be passed to application.
aspinall
parents:
15211
diff
changeset
|
136 |
((Scan.optional ((* scan_whspc |-- *) parse_chars >> (single o Text)) [] -- |
2fac1f11b7f6
Remove white space skipping in element content; XML specification clearly requires whitespace to be passed to application.
aspinall
parents:
15211
diff
changeset
|
137 |
(Scan.repeat ((* scan_whspc |-- *) |
14185
9b3841638c06
Tried to make parser a bit more standard-conforming.
berghofe
parents:
13729
diff
changeset
|
138 |
( parse_elem >> single |
9b3841638c06
Tried to make parser a bit more standard-conforming.
berghofe
parents:
13729
diff
changeset
|
139 |
|| parse_cdata >> (single o Text) |
9b3841638c06
Tried to make parser a bit more standard-conforming.
berghofe
parents:
13729
diff
changeset
|
140 |
|| parse_pi >> K [] |
9b3841638c06
Tried to make parser a bit more standard-conforming.
berghofe
parents:
13729
diff
changeset
|
141 |
|| parse_comment >> K []) -- |
15216
2fac1f11b7f6
Remove white space skipping in element content; XML specification clearly requires whitespace to be passed to application.
aspinall
parents:
15211
diff
changeset
|
142 |
Scan.optional ((* scan_whspc |-- *) parse_chars >> (single o Text)) [] |
19482
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
wenzelm
parents:
17756
diff
changeset
|
143 |
>> op @) >> flat) >> op @)(* --| scan_whspc*)) xs |
14185
9b3841638c06
Tried to make parser a bit more standard-conforming.
berghofe
parents:
13729
diff
changeset
|
144 |
|
9b3841638c06
Tried to make parser a bit more standard-conforming.
berghofe
parents:
13729
diff
changeset
|
145 |
and parse_elem xs = |
9b3841638c06
Tried to make parser a bit more standard-conforming.
berghofe
parents:
13729
diff
changeset
|
146 |
($$ "<" |-- Symbol.scan_id -- |
14865 | 147 |
Scan.repeat (scan_whspc |-- parse_att) --| scan_whspc :-- (fn (s, _) => |
14185
9b3841638c06
Tried to make parser a bit more standard-conforming.
berghofe
parents:
13729
diff
changeset
|
148 |
!! (err "Expected > or />") |
14910 | 149 |
(Scan.this_string "/>" >> K [] |
14185
9b3841638c06
Tried to make parser a bit more standard-conforming.
berghofe
parents:
13729
diff
changeset
|
150 |
|| $$ ">" |-- parse_content --| |
9b3841638c06
Tried to make parser a bit more standard-conforming.
berghofe
parents:
13729
diff
changeset
|
151 |
!! (err ("Expected </" ^ s ^ ">")) |
14910 | 152 |
(Scan.this_string ("</" ^ s) --| scan_whspc --| $$ ">"))) >> |
13729
1a8dda49fd86
Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents:
12416
diff
changeset
|
153 |
(fn ((s, atts), ts) => Elem (s, atts, ts))) xs; |
1a8dda49fd86
Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents:
12416
diff
changeset
|
154 |
|
14185
9b3841638c06
Tried to make parser a bit more standard-conforming.
berghofe
parents:
13729
diff
changeset
|
155 |
val parse_document = |
14910 | 156 |
Scan.option (Scan.this_string "<!DOCTYPE" -- scan_whspc |-- |
14185
9b3841638c06
Tried to make parser a bit more standard-conforming.
berghofe
parents:
13729
diff
changeset
|
157 |
(Scan.repeat (Scan.unless ($$ ">") |
9b3841638c06
Tried to make parser a bit more standard-conforming.
berghofe
parents:
13729
diff
changeset
|
158 |
(Scan.one Symbol.not_eof)) >> implode) --| $$ ">" --| scan_whspc) -- |
9b3841638c06
Tried to make parser a bit more standard-conforming.
berghofe
parents:
13729
diff
changeset
|
159 |
parse_elem; |
9b3841638c06
Tried to make parser a bit more standard-conforming.
berghofe
parents:
13729
diff
changeset
|
160 |
|
13729
1a8dda49fd86
Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents:
12416
diff
changeset
|
161 |
fun tree_of_string s = |
14185
9b3841638c06
Tried to make parser a bit more standard-conforming.
berghofe
parents:
13729
diff
changeset
|
162 |
(case Scan.finite Symbol.stopper (Scan.error (!! (err "Malformed element") |
9b3841638c06
Tried to make parser a bit more standard-conforming.
berghofe
parents:
13729
diff
changeset
|
163 |
(scan_whspc |-- parse_elem --| scan_whspc))) (Symbol.explode s) of |
14728 | 164 |
(x, []) => x |
165 |
| (_, ys) => error ("XML parsing error: Unprocessed input\n" ^ Symbol.beginning 100 ys)); |
|
13729
1a8dda49fd86
Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents:
12416
diff
changeset
|
166 |
|
12416 | 167 |
end; |