08/02/03 10:32:25
>>240
PCDATAはXLfでなくて良いのか?
exception ParseException of string
let rec parse (xs : token list) (context : string list) (ks : ('a,'b) xml list list) : ('a,'b) xml =
let add_child ks x =
match ks with
| [] -> raise (ParseException "internal error: add_child")
| (k::ks1) -> (x::k)::ks1
in
match xs with
| [] ->
if context <> [] then
raise (ParseException ("some tags are not closed:" ^ (String.concat "," context)))
else
hd (hd ks)
| (PCDATA str)::xs1 ->
if context = [] then
raise (ParseException ("toplevel PCDATA:" ^ str))
else
parse xs1 context (add_child ks (XBr (str, [])))
| (Open tag)::xs1 ->
parse xs1 (tag::context) ([]::ks)
| (Close tag)::xs1 ->
if (hd context) <> tag then
raise (ParseException ("close tag mismatch:" ^ tag ^ " appeared where " ^ (hd context) ^ " expected"))
else
parse xs1 (tl context) (add_child (tl ks) (XBr (tag, (rev (hd ks)))))
let run_parse xs = parse xs [] [[]]