136 |
136 |
137 (** XML parsing (slow) **) |
137 (** XML parsing (slow) **) |
138 |
138 |
139 local |
139 local |
140 |
140 |
141 fun err s (xs, _) = |
141 fun err msg (xs, _) = |
142 "XML parsing error: " ^ s ^ "\nfound: " ^ quote (Symbol.beginning 100 xs); |
142 fn () => "XML parsing error: " ^ msg () ^ "\nfound: " ^ quote (Symbol.beginning 100 xs); |
143 |
143 |
144 fun ignored _ = []; |
144 fun ignored _ = []; |
145 |
145 |
146 val blanks = Scan.many Symbol.is_blank; |
146 val blanks = Scan.many Symbol.is_blank; |
147 val special = $$ "&" ^^ Symbol.scan_id ^^ $$ ";" >> decode; |
147 val special = $$ "&" ^^ Symbol.scan_id ^^ $$ ";" >> decode; |
200 @@@ parse_optional_text) >> flat)) xs |
200 @@@ parse_optional_text) >> flat)) xs |
201 |
201 |
202 and parse_element xs = |
202 and parse_element xs = |
203 ($$ "<" |-- Symbol.scan_id -- |
203 ($$ "<" |-- Symbol.scan_id -- |
204 Scan.repeat (blanks |-- parse_att) --| blanks :-- (fn (s, _) => |
204 Scan.repeat (blanks |-- parse_att) --| blanks :-- (fn (s, _) => |
205 !! (err "Expected > or />") |
205 !! (err (fn () => "Expected > or />")) |
206 (Scan.this_string "/>" >> ignored |
206 (Scan.this_string "/>" >> ignored |
207 || $$ ">" |-- parse_content --| |
207 || $$ ">" |-- parse_content --| |
208 !! (err ("Expected </" ^ s ^ ">")) |
208 !! (err (fn () => "Expected </" ^ s ^ ">")) |
209 (Scan.this_string ("</" ^ s) --| blanks --| $$ ">"))) >> Elem) xs; |
209 (Scan.this_string ("</" ^ s) --| blanks --| $$ ">"))) >> Elem) xs; |
210 |
210 |
211 val parse_document = |
211 val parse_document = |
212 (Scan.repeat parse_misc -- Scan.option parse_doctype -- Scan.repeat parse_misc) |
212 (Scan.repeat parse_misc -- Scan.option parse_doctype -- Scan.repeat parse_misc) |
213 |-- parse_element; |
213 |-- parse_element; |
214 |
214 |
215 fun parse s = |
215 fun parse s = |
216 (case Scan.finite Symbol.stopper (Scan.error (!! (err "Malformed element") |
216 (case Scan.finite Symbol.stopper (Scan.error (!! (err (fn () => "Malformed element")) |
217 (blanks |-- parse_document --| blanks))) (raw_explode s) of |
217 (blanks |-- parse_document --| blanks))) (raw_explode s) of |
218 (x, []) => x |
218 (x, []) => x |
219 | (_, ys) => error ("XML parsing error: Unprocessed input\n" ^ Symbol.beginning 100 ys)); |
219 | (_, ys) => error ("XML parsing error: Unprocessed input\n" ^ Symbol.beginning 100 ys)); |
220 |
220 |
221 end; |
221 end; |