関数型言語ML(SML, OCaml, etc.), Part 5at TECH
関数型言語ML(SML, OCaml, etc.), Part 5 - 暇つぶし2ch242:デフォルトの名無しさん
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 [] [[]]


次ページ
続きを表示
1を表示
最新レス表示
レスジャンプ
類似スレ一覧
スレッドの検索
話題のニュース
おまかせリスト
オプション
しおりを挟む
スレッドに書込
スレッドの一覧
暇つぶし2ch