author | wenzelm |
Sat, 12 Jun 2004 22:46:51 +0200 | |
changeset 14928 | b8c1783c9101 |
parent 14910 | f145696d4bb5 |
child 14969 | 3d9126cbf0e6 |
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 |
License: GPL (GNU GENERAL PUBLIC LICENSE) |
5 |
||
13729
1a8dda49fd86
Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents:
12416
diff
changeset
|
6 |
Basic support for XML input and output. |
12416 | 7 |
*) |
8 |
||
9 |
signature XML = |
|
10 |
sig |
|
13729
1a8dda49fd86
Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents:
12416
diff
changeset
|
11 |
datatype tree = |
1a8dda49fd86
Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents:
12416
diff
changeset
|
12 |
Elem of string * (string * string) list * tree list |
1a8dda49fd86
Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents:
12416
diff
changeset
|
13 |
| Text of string |
12416 | 14 |
val element: string -> (string * string) list -> string list -> string |
15 |
val text: string -> string |
|
14713
6d203f6f0e8d
Add cdata output. Add tabs in whitespace. Write two strings instead of Library.quote.
aspinall
parents:
14596
diff
changeset
|
16 |
val cdata: string -> string |
12416 | 17 |
val header: string |
13729
1a8dda49fd86
Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents:
12416
diff
changeset
|
18 |
val string_of_tree: tree -> string |
1a8dda49fd86
Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents:
12416
diff
changeset
|
19 |
val tree_of_string: string -> tree |
14185
9b3841638c06
Tried to make parser a bit more standard-conforming.
berghofe
parents:
13729
diff
changeset
|
20 |
val parse_content: string list -> tree list * string list |
9b3841638c06
Tried to make parser a bit more standard-conforming.
berghofe
parents:
13729
diff
changeset
|
21 |
val parse_elem: string list -> tree * string list |
9b3841638c06
Tried to make parser a bit more standard-conforming.
berghofe
parents:
13729
diff
changeset
|
22 |
val parse_document: string list -> (string option * tree) * string list |
12416 | 23 |
end; |
24 |
||
25 |
structure XML: XML = |
|
26 |
struct |
|
27 |
||
28 |
(* character data *) |
|
29 |
||
30 |
fun encode "<" = "<" |
|
31 |
| encode ">" = ">" |
|
32 |
| encode "&" = "&" |
|
33 |
| encode "'" = "'" |
|
34 |
| encode "\"" = """ |
|
35 |
| encode c = c; |
|
36 |
||
13729
1a8dda49fd86
Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents:
12416
diff
changeset
|
37 |
fun decode "<" = "<" |
1a8dda49fd86
Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents:
12416
diff
changeset
|
38 |
| decode ">" = ">" |
1a8dda49fd86
Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents:
12416
diff
changeset
|
39 |
| decode "&" = "&" |
1a8dda49fd86
Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents:
12416
diff
changeset
|
40 |
| decode "'" = "'" |
1a8dda49fd86
Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents:
12416
diff
changeset
|
41 |
| decode """ = "\"" |
1a8dda49fd86
Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents:
12416
diff
changeset
|
42 |
| decode c = c; |
1a8dda49fd86
Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents:
12416
diff
changeset
|
43 |
|
14928 | 44 |
val text = Library.translate_string encode; |
12416 | 45 |
|
14713
6d203f6f0e8d
Add cdata output. Add tabs in whitespace. Write two strings instead of Library.quote.
aspinall
parents:
14596
diff
changeset
|
46 |
val cdata_open = "<![CDATA[" |
6d203f6f0e8d
Add cdata output. Add tabs in whitespace. Write two strings instead of Library.quote.
aspinall
parents:
14596
diff
changeset
|
47 |
val cdata_close = "]]>" |
6d203f6f0e8d
Add cdata output. Add tabs in whitespace. Write two strings instead of Library.quote.
aspinall
parents:
14596
diff
changeset
|
48 |
|
6d203f6f0e8d
Add cdata output. Add tabs in whitespace. Write two strings instead of Library.quote.
aspinall
parents:
14596
diff
changeset
|
49 |
fun cdata s = cdata_open ^ s ^ cdata_close; |
12416 | 50 |
|
14728 | 51 |
|
12416 | 52 |
(* elements *) |
53 |
||
13729
1a8dda49fd86
Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents:
12416
diff
changeset
|
54 |
datatype tree = |
1a8dda49fd86
Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents:
12416
diff
changeset
|
55 |
Elem of string * (string * string) list * tree list |
1a8dda49fd86
Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents:
12416
diff
changeset
|
56 |
| Text of string; |
1a8dda49fd86
Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents:
12416
diff
changeset
|
57 |
|
14728 | 58 |
fun attribute (a, x) = a ^ " = \"" ^ text x ^ "\""; |
12416 | 59 |
|
60 |
fun element name atts cs = |
|
61 |
let val elem = space_implode " " (name :: map attribute atts) in |
|
62 |
if null cs then enclose "<" "/>" elem |
|
63 |
else enclose "<" ">" elem ^ implode cs ^ enclose "</" ">" name |
|
64 |
end; |
|
65 |
||
13729
1a8dda49fd86
Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents:
12416
diff
changeset
|
66 |
fun string_of_tree (Elem (name, atts, ts)) = |
1a8dda49fd86
Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents:
12416
diff
changeset
|
67 |
element name atts (map string_of_tree ts) |
14728 | 68 |
| string_of_tree (Text s) = s; |
69 |
||
12416 | 70 |
val header = "<?xml version=\"1.0\"?>\n"; |
71 |
||
13729
1a8dda49fd86
Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents:
12416
diff
changeset
|
72 |
|
1a8dda49fd86
Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents:
12416
diff
changeset
|
73 |
(* parser *) |
1a8dda49fd86
Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents:
12416
diff
changeset
|
74 |
|
14728 | 75 |
fun err s (xs, _) = |
76 |
"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
|
77 |
|
14728 | 78 |
val scan_whspc = Scan.any Symbol.is_blank; |
13729
1a8dda49fd86
Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents:
12416
diff
changeset
|
79 |
|
1a8dda49fd86
Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents:
12416
diff
changeset
|
80 |
val scan_special = $$ "&" ^^ Symbol.scan_id ^^ $$ ";" >> decode; |
1a8dda49fd86
Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents:
12416
diff
changeset
|
81 |
|
14185
9b3841638c06
Tried to make parser a bit more standard-conforming.
berghofe
parents:
13729
diff
changeset
|
82 |
val parse_chars = Scan.repeat1 (Scan.unless (scan_whspc -- $$ "<") |
9b3841638c06
Tried to make parser a bit more standard-conforming.
berghofe
parents:
13729
diff
changeset
|
83 |
(scan_special || Scan.one Symbol.not_eof)) >> implode; |
9b3841638c06
Tried to make parser a bit more standard-conforming.
berghofe
parents:
13729
diff
changeset
|
84 |
|
14910 | 85 |
val parse_cdata = Scan.this_string "<![CDATA[" |-- |
86 |
(Scan.repeat (Scan.unless (Scan.this_string "]]>") (Scan.one Symbol.not_eof)) >> |
|
87 |
implode) --| Scan.this_string "]]>"; |
|
14185
9b3841638c06
Tried to make parser a bit more standard-conforming.
berghofe
parents:
13729
diff
changeset
|
88 |
|
9b3841638c06
Tried to make parser a bit more standard-conforming.
berghofe
parents:
13729
diff
changeset
|
89 |
val parse_att = |
14865 | 90 |
Symbol.scan_id --| scan_whspc --| $$ "=" --| scan_whspc -- |
91 |
(($$ "\"" || $$ "'") :-- (fn s => (Scan.repeat (Scan.unless ($$ s) |
|
92 |
(scan_special || Scan.one Symbol.not_eof)) >> implode) --| $$ s) >> snd); |
|
14863 | 93 |
|
14910 | 94 |
val parse_comment = Scan.this_string "<!--" -- |
95 |
Scan.repeat (Scan.unless (Scan.this_string "-->") (Scan.one Symbol.not_eof)) -- |
|
96 |
Scan.this_string "-->"; |
|
13729
1a8dda49fd86
Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents:
12416
diff
changeset
|
97 |
|
14910 | 98 |
val parse_pi = Scan.this_string "<?" |-- |
99 |
Scan.repeat (Scan.unless (Scan.this_string "?>") (Scan.one Symbol.not_eof)) --| |
|
100 |
Scan.this_string "?>"; |
|
13729
1a8dda49fd86
Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents:
12416
diff
changeset
|
101 |
|
14185
9b3841638c06
Tried to make parser a bit more standard-conforming.
berghofe
parents:
13729
diff
changeset
|
102 |
fun parse_content xs = |
9b3841638c06
Tried to make parser a bit more standard-conforming.
berghofe
parents:
13729
diff
changeset
|
103 |
((Scan.optional (scan_whspc |-- parse_chars >> (single o Text)) [] -- |
9b3841638c06
Tried to make parser a bit more standard-conforming.
berghofe
parents:
13729
diff
changeset
|
104 |
(Scan.repeat (scan_whspc |-- |
9b3841638c06
Tried to make parser a bit more standard-conforming.
berghofe
parents:
13729
diff
changeset
|
105 |
( parse_elem >> single |
9b3841638c06
Tried to make parser a bit more standard-conforming.
berghofe
parents:
13729
diff
changeset
|
106 |
|| parse_cdata >> (single o Text) |
9b3841638c06
Tried to make parser a bit more standard-conforming.
berghofe
parents:
13729
diff
changeset
|
107 |
|| parse_pi >> K [] |
9b3841638c06
Tried to make parser a bit more standard-conforming.
berghofe
parents:
13729
diff
changeset
|
108 |
|| parse_comment >> K []) -- |
9b3841638c06
Tried to make parser a bit more standard-conforming.
berghofe
parents:
13729
diff
changeset
|
109 |
Scan.optional (scan_whspc |-- parse_chars >> (single o Text)) [] |
9b3841638c06
Tried to make parser a bit more standard-conforming.
berghofe
parents:
13729
diff
changeset
|
110 |
>> op @) >> flat) >> op @) --| scan_whspc) xs |
9b3841638c06
Tried to make parser a bit more standard-conforming.
berghofe
parents:
13729
diff
changeset
|
111 |
|
9b3841638c06
Tried to make parser a bit more standard-conforming.
berghofe
parents:
13729
diff
changeset
|
112 |
and parse_elem xs = |
9b3841638c06
Tried to make parser a bit more standard-conforming.
berghofe
parents:
13729
diff
changeset
|
113 |
($$ "<" |-- Symbol.scan_id -- |
14865 | 114 |
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
|
115 |
!! (err "Expected > or />") |
14910 | 116 |
(Scan.this_string "/>" >> K [] |
14185
9b3841638c06
Tried to make parser a bit more standard-conforming.
berghofe
parents:
13729
diff
changeset
|
117 |
|| $$ ">" |-- parse_content --| |
9b3841638c06
Tried to make parser a bit more standard-conforming.
berghofe
parents:
13729
diff
changeset
|
118 |
!! (err ("Expected </" ^ s ^ ">")) |
14910 | 119 |
(Scan.this_string ("</" ^ s) --| scan_whspc --| $$ ">"))) >> |
13729
1a8dda49fd86
Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents:
12416
diff
changeset
|
120 |
(fn ((s, atts), ts) => Elem (s, atts, ts))) xs; |
1a8dda49fd86
Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents:
12416
diff
changeset
|
121 |
|
14185
9b3841638c06
Tried to make parser a bit more standard-conforming.
berghofe
parents:
13729
diff
changeset
|
122 |
val parse_document = |
14910 | 123 |
Scan.option (Scan.this_string "<!DOCTYPE" -- scan_whspc |-- |
14185
9b3841638c06
Tried to make parser a bit more standard-conforming.
berghofe
parents:
13729
diff
changeset
|
124 |
(Scan.repeat (Scan.unless ($$ ">") |
9b3841638c06
Tried to make parser a bit more standard-conforming.
berghofe
parents:
13729
diff
changeset
|
125 |
(Scan.one Symbol.not_eof)) >> implode) --| $$ ">" --| scan_whspc) -- |
9b3841638c06
Tried to make parser a bit more standard-conforming.
berghofe
parents:
13729
diff
changeset
|
126 |
parse_elem; |
9b3841638c06
Tried to make parser a bit more standard-conforming.
berghofe
parents:
13729
diff
changeset
|
127 |
|
13729
1a8dda49fd86
Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents:
12416
diff
changeset
|
128 |
fun tree_of_string s = |
14185
9b3841638c06
Tried to make parser a bit more standard-conforming.
berghofe
parents:
13729
diff
changeset
|
129 |
(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
|
130 |
(scan_whspc |-- parse_elem --| scan_whspc))) (Symbol.explode s) of |
14728 | 131 |
(x, []) => x |
132 |
| (_, 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
|
133 |
|
12416 | 134 |
end; |