(* Title: Pure/PIDE/xml.ML Author: David Aspinall Author: Stefan Berghofer Author: Makarius
Untyped XML trees and representation of ML values.
*)
signature XML_DATA_OPS = sig type'a A type'a T type'a V type'a P val int_atom: int A val bool_atom: bool A val unit_atom: unit A val self: XML.body T val tree: XML.tree T val properties: Properties.T T valstring: string T val int: int T valbool: bool T val unit: unit T val pair: 'a T -> 'b T -> ('a * 'b) T val triple: 'a T -> 'b T -> 'c T -> ('a * 'b * 'c) T vallist: 'a T -> 'a list T valoption: 'a T -> 'a option T val variant: 'a V list -> 'a T end;
signature XML = sig
include XML val blob: stringlist -> body val is_empty: tree -> bool val is_empty_body: body -> bool valstring: string -> body val enclose: string -> string -> body -> body val trim_blanks: body -> body val filter_elements: {remove: string -> bool, expose: string -> bool} -> body -> body val header: string val text: string -> string val element: string -> attributes -> stringlist -> string val output_markup: Markup.T -> Markup.output val string_of: tree -> string val pretty: int -> tree -> Pretty.T val parse_comments: stringlist -> unit * stringlist val parse_string : string -> stringoption val parse_element: stringlist -> tree * stringlist val parse_document: stringlist -> tree * stringlist val parse: string -> tree
exception XML_ATOM ofstring
exception XML_BODY of body structure Encode: XML_DATA_OPS structure Decode: XML_DATA_OPS end;
fun enclose bg en body = string bg @ body @ string en;
(* trim blanks *)
fun trim_blanks trees =
trees |> maps
(fn Elem (markup, body) => [Elem (markup, trim_blanks body)]
| Text s => s |> raw_explode |> trim Symbol.is_blank |> implode |> string);
(* filter markup elements *)
fun filter_elements {remove, expose} = let funfilter ts =
ts |> maps (fn t =>
(case XML.unwrap_elem t of
SOME ((markup, body1), body2) => if remove (#1 markup) then [] elseif expose (#1 markup) thenfilter body2 else [XML.wrap_elem ((markup, body1), filter body2)]
| NONE =>
(case t of
XML.Elem (markup, body) => if remove (#1 markup) then [] elseif expose (#1 markup) thenfilter body else [XML.Elem (markup, filter body)]
| _ => [t]))); infilterend;
fun elem name atts =
implode_space (name :: map (fn (a, x) => a ^ "=\"" ^ text x ^ "\"") atts);
fun element name atts body = letval b = implode body in if b = ""then Library.enclose "<""/>" (elem name atts) else Library.enclose "<"">" (elem name atts) ^ b ^ Library.enclose """>" name end;
fun output_markup (markup as (name, atts)) = if Markup.is_empty markup then Markup.no_output else (Library.enclose "<"">" (elem name atts), Library.enclose """>" name);
(* output content *)
fun content_depth depth = let fun traverse _ (Elem ((name, atts), [])) =
Buffer.add "<" #> Buffer.add (elem name atts) #> Buffer.add "/>"
| traverse d (Elem ((name, atts), ts)) =
Buffer.add "<" #> Buffer.add (elem name atts) #> Buffer.add ">" #>
traverse_body d ts #>
Buffer.add "" #> Buffer.add name #> Buffer.add ">"
| traverse _ (Text s) = Buffer.add (text s) and traverse_body 0 _ = Buffer.add "..."
| traverse_body d ts = fold (traverse (d - 1)) ts; in Buffer.build_content o traverse depth end;
val string_of = content_depth ~1;
fun pretty depth tree = Pretty.str (content_depth (Int.max (0, depth)) tree);
val _ = ML_system_pp (fn depth => fn _ => Pretty.to_ML o pretty (FixedInt.toInt depth));
fun name_start_char c = Symbol.is_ascii_letter c orelse c = ":" orelse c = "_"; fun name_char c = name_start_char c orelse Symbol.is_ascii_digit c orelse c = "-" orelse c = "."; val parse_name = Scan.one name_start_char ::: Scan.many name_char;
val blanks = Scan.many Symbol.is_blank; val special = $$ "&" ^^ (parse_name >> implode) ^^ $$ ";" >> decode; val regular = Scan.one Symbol.not_eof; fun regular_except x = Scan.one (fn c => Symbol.not_eof c andalso c <> x);
val parse_chars = Scan.repeat1 (special || regular_except "<") >> implode;
fun variant fs [t] = let val (tag, (xs, ts)) = tagged t; val f = nth fs tag handle General.Subscript => raise XML_BODY [t]; in f (xs, ts) end
| variant _ ts = raise XML_BODY ts;
end;
end;
¤ Dauer der Verarbeitung: 0.17 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.