src/Pure/PIDE/xml.ML
author wenzelm
Wed Dec 12 23:36:07 2012 +0100 (2012-12-12 ago)
changeset 50499 f496b2b7bafb
parent 49650 9fad6480300d
child 56059 2390391584c2
permissions -rw-r--r--
rendering of selected dialog_result as active_result_color, depending on dynamic command status in output panel, but not static popups etc.;
     1 (*  Title:      Pure/PIDE/xml.ML
     2     Author:     David Aspinall
     3     Author:     Stefan Berghofer
     4     Author:     Makarius
     5 
     6 Untyped XML trees and representation of ML values.
     7 *)
     8 
     9 signature XML_DATA_OPS =
    10 sig
    11   type 'a A
    12   type 'a T
    13   type 'a V
    14   val int_atom: int A
    15   val bool_atom: bool A
    16   val unit_atom: unit A
    17   val properties: Properties.T T
    18   val string: string T
    19   val int: int T
    20   val bool: bool T
    21   val unit: unit T
    22   val pair: 'a T -> 'b T -> ('a * 'b) T
    23   val triple: 'a T -> 'b T -> 'c T -> ('a * 'b * 'c) T
    24   val list: 'a T -> 'a list T
    25   val option: 'a T -> 'a option T
    26   val variant: 'a V list -> 'a T
    27 end;
    28 
    29 signature XML =
    30 sig
    31   type attributes = (string * string) list
    32   datatype tree =
    33       Elem of (string * attributes) * tree list
    34     | Text of string
    35   type body = tree list
    36   val wrap_elem: ((string * attributes) * tree list) * tree list -> tree
    37   val unwrap_elem: tree -> (((string * attributes) * tree list) * tree list) option
    38   val add_content: tree -> Buffer.T -> Buffer.T
    39   val content_of: body -> string
    40   val header: string
    41   val text: string -> string
    42   val element: string -> attributes -> string list -> string
    43   val output_markup: Markup.T -> Output.output * Output.output
    44   val string_of: tree -> string
    45   val pretty: int -> tree -> Pretty.T
    46   val output: tree -> TextIO.outstream -> unit
    47   val parse_comments: string list -> unit * string list
    48   val parse_string : string -> string option
    49   val parse_element: string list -> tree * string list
    50   val parse_document: string list -> tree * string list
    51   val parse: string -> tree
    52   exception XML_ATOM of string
    53   exception XML_BODY of body
    54   structure Encode: XML_DATA_OPS
    55   structure Decode: XML_DATA_OPS
    56 end;
    57 
    58 structure XML: XML =
    59 struct
    60 
    61 (** XML trees **)
    62 
    63 type attributes = (string * string) list;
    64 
    65 datatype tree =
    66     Elem of (string * attributes) * tree list
    67   | Text of string;
    68 
    69 type body = tree list;
    70 
    71 
    72 (* wrapped elements *)
    73 
    74 val xml_elemN = "xml_elem";
    75 val xml_nameN = "xml_name";
    76 val xml_bodyN = "xml_body";
    77 
    78 fun wrap_elem (((a, atts), body1), body2) =
    79   Elem ((xml_elemN, (xml_nameN, a) :: atts), Elem ((xml_bodyN, []), body1) :: body2);
    80 
    81 fun unwrap_elem (Elem ((name, (n, a) :: atts), Elem ((name', atts'), body1) :: body2)) =
    82       if name = xml_elemN andalso n = xml_nameN andalso name' = xml_bodyN andalso null atts'
    83       then SOME (((a, atts), body1), body2) else NONE
    84   | unwrap_elem _ = NONE;
    85 
    86 
    87 (* text context *)
    88 
    89 fun add_content tree =
    90   (case unwrap_elem tree of
    91     SOME (_, ts) => fold add_content ts
    92   | NONE =>
    93       (case tree of
    94         Elem (_, ts) => fold add_content ts
    95       | Text s => Buffer.add s));
    96 
    97 fun content_of body = Buffer.empty |> fold add_content body |> Buffer.content;
    98 
    99 
   100 
   101 (** string representation **)
   102 
   103 val header = "<?xml version=\"1.0\"?>\n";
   104 
   105 
   106 (* escaped text *)
   107 
   108 fun decode "&lt;" = "<"
   109   | decode "&gt;" = ">"
   110   | decode "&amp;" = "&"
   111   | decode "&apos;" = "'"
   112   | decode "&quot;" = "\""
   113   | decode c = c;
   114 
   115 fun encode "<" = "&lt;"
   116   | encode ">" = "&gt;"
   117   | encode "&" = "&amp;"
   118   | encode "'" = "&apos;"
   119   | encode "\"" = "&quot;"
   120   | encode c = c;
   121 
   122 val text = translate_string encode;
   123 
   124 
   125 (* elements *)
   126 
   127 fun elem name atts =
   128   space_implode " " (name :: map (fn (a, x) => a ^ "=\"" ^ text x ^ "\"") atts);
   129 
   130 fun element name atts body =
   131   let val b = implode body in
   132     if b = "" then enclose "<" "/>" (elem name atts)
   133     else enclose "<" ">" (elem name atts) ^ b ^ enclose "</" ">" name
   134   end;
   135 
   136 fun output_markup (markup as (name, atts)) =
   137   if Markup.is_empty markup then Markup.no_output
   138   else (enclose "<" ">" (elem name atts), enclose "</" ">" name);
   139 
   140 
   141 (* output *)
   142 
   143 fun buffer_of depth tree =
   144   let
   145     fun traverse _ (Elem ((name, atts), [])) =
   146           Buffer.add "<" #> Buffer.add (elem name atts) #> Buffer.add "/>"
   147       | traverse d (Elem ((name, atts), ts)) =
   148           Buffer.add "<" #> Buffer.add (elem name atts) #> Buffer.add ">" #>
   149           traverse_body d ts #>
   150           Buffer.add "</" #> Buffer.add name #> Buffer.add ">"
   151       | traverse _ (Text s) = Buffer.add (text s)
   152     and traverse_body 0 _ = Buffer.add "..."
   153       | traverse_body d ts = fold (traverse (d - 1)) ts;
   154   in Buffer.empty |> traverse depth tree end;
   155 
   156 val string_of = Buffer.content o buffer_of ~1;
   157 val output = Buffer.output o buffer_of ~1;
   158 
   159 fun pretty depth tree =
   160   Pretty.str (Buffer.content (buffer_of (Int.max (0, depth)) tree));
   161 
   162 
   163 
   164 (** XML parsing **)
   165 
   166 local
   167 
   168 fun err msg (xs, _) =
   169   fn () => "XML parsing error: " ^ msg () ^ "\nfound: " ^ quote (Symbol.beginning 100 xs);
   170 
   171 fun ignored _ = [];
   172 
   173 fun name_start_char c = Symbol.is_ascii_letter c orelse c = ":" orelse c = "_";
   174 fun name_char c = name_start_char c orelse Symbol.is_ascii_digit c orelse c = "-" orelse c = ".";
   175 val parse_name = Scan.one name_start_char ::: Scan.many name_char;
   176 
   177 val blanks = Scan.many Symbol.is_blank;
   178 val special = $$ "&" ^^ (parse_name >> implode) ^^ $$ ";" >> decode;
   179 val regular = Scan.one Symbol.is_regular;
   180 fun regular_except x = Scan.one (fn c => Symbol.is_regular c andalso c <> x);
   181 
   182 val parse_chars = Scan.repeat1 (special || regular_except "<") >> implode;
   183 
   184 val parse_cdata =
   185   Scan.this_string "<![CDATA[" |--
   186   (Scan.repeat (Scan.unless (Scan.this_string "]]>") regular) >> implode) --|
   187   Scan.this_string "]]>";
   188 
   189 val parse_att =
   190   ((parse_name >> implode) --| (blanks -- $$ "=" -- blanks)) --
   191   (($$ "\"" || $$ "'") :|-- (fn s =>
   192     (Scan.repeat (special || regular_except s) >> implode) --| $$ s));
   193 
   194 val parse_comment =
   195   Scan.this_string "<!--" --
   196   Scan.repeat (Scan.unless (Scan.this_string "-->") regular) --
   197   Scan.this_string "-->" >> ignored;
   198 
   199 val parse_processing_instruction =
   200   Scan.this_string "<?" --
   201   Scan.repeat (Scan.unless (Scan.this_string "?>") regular) --
   202   Scan.this_string "?>" >> ignored;
   203 
   204 val parse_doctype =
   205   Scan.this_string "<!DOCTYPE" --
   206   Scan.repeat (Scan.unless ($$ ">") regular) --
   207   $$ ">" >> ignored;
   208 
   209 val parse_misc =
   210   Scan.one Symbol.is_blank >> ignored ||
   211   parse_processing_instruction ||
   212   parse_comment;
   213 
   214 val parse_optional_text =
   215   Scan.optional (parse_chars >> (single o Text)) [];
   216 
   217 in
   218 
   219 val parse_comments =
   220   blanks -- Scan.repeat (parse_comment -- blanks >> K ()) >> K ();
   221 
   222 val parse_string = Scan.read Symbol.stopper parse_chars o raw_explode;
   223 
   224 fun parse_content xs =
   225   (parse_optional_text @@@
   226     (Scan.repeat
   227       ((parse_element >> single ||
   228         parse_cdata >> (single o Text) ||
   229         parse_processing_instruction ||
   230         parse_comment)
   231       @@@ parse_optional_text) >> flat)) xs
   232 
   233 and parse_element xs =
   234   ($$ "<" |-- parse_name -- Scan.repeat (blanks |-- parse_att) --| blanks :--
   235     (fn (name, _) =>
   236       !! (err (fn () => "Expected > or />"))
   237        ($$ "/" -- $$ ">" >> ignored ||
   238         $$ ">" |-- parse_content --|
   239           !! (err (fn () => "Expected </" ^ implode name ^ ">"))
   240               ($$ "<" -- $$ "/" -- Scan.this name -- blanks -- $$ ">")))
   241     >> (fn ((name, atts), body) => Elem ((implode name, atts), body))) xs;
   242 
   243 val parse_document =
   244   (Scan.repeat parse_misc -- Scan.option parse_doctype -- Scan.repeat parse_misc)
   245   |-- parse_element;
   246 
   247 fun parse s =
   248   (case Scan.finite Symbol.stopper (Scan.error (!! (err (fn () => "Malformed element"))
   249       (blanks |-- parse_document --| blanks))) (raw_explode s) of
   250     (x, []) => x
   251   | (_, ys) => error ("XML parsing error: unprocessed input\n" ^ Symbol.beginning 100 ys));
   252 
   253 end;
   254 
   255 
   256 
   257 (** XML as data representation language **)
   258 
   259 exception XML_ATOM of string;
   260 exception XML_BODY of tree list;
   261 
   262 
   263 structure Encode =
   264 struct
   265 
   266 type 'a A = 'a -> string;
   267 type 'a T = 'a -> body;
   268 type 'a V = 'a -> string list * body;
   269 
   270 
   271 (* atomic values *)
   272 
   273 fun int_atom i = signed_string_of_int i;
   274 
   275 fun bool_atom false = "0"
   276   | bool_atom true = "1";
   277 
   278 fun unit_atom () = "";
   279 
   280 
   281 (* structural nodes *)
   282 
   283 fun node ts = Elem ((":", []), ts);
   284 
   285 fun vector xs = map_index (fn (i, x) => (int_atom i, x)) xs;
   286 
   287 fun tagged (tag, (xs, ts)) = Elem ((int_atom tag, vector xs), ts);
   288 
   289 
   290 (* representation of standard types *)
   291 
   292 fun properties props = [Elem ((":", props), [])];
   293 
   294 fun string "" = []
   295   | string s = [Text s];
   296 
   297 val int = string o int_atom;
   298 
   299 val bool = string o bool_atom;
   300 
   301 val unit = string o unit_atom;
   302 
   303 fun pair f g (x, y) = [node (f x), node (g y)];
   304 
   305 fun triple f g h (x, y, z) = [node (f x), node (g y), node (h z)];
   306 
   307 fun list f xs = map (node o f) xs;
   308 
   309 fun option _ NONE = []
   310   | option f (SOME x) = [node (f x)];
   311 
   312 fun variant fs x =
   313   [tagged (the (get_index (fn f => SOME (f x) handle General.Match => NONE) fs))];
   314 
   315 end;
   316 
   317 
   318 structure Decode =
   319 struct
   320 
   321 type 'a A = string -> 'a;
   322 type 'a T = body -> 'a;
   323 type 'a V = string list * body -> 'a;
   324 
   325 
   326 (* atomic values *)
   327 
   328 fun int_atom s =
   329   Markup.parse_int s
   330     handle Fail _ => raise XML_ATOM s;
   331 
   332 fun bool_atom "0" = false
   333   | bool_atom "1" = true
   334   | bool_atom s = raise XML_ATOM s;
   335 
   336 fun unit_atom "" = ()
   337   | unit_atom s = raise XML_ATOM s;
   338 
   339 
   340 (* structural nodes *)
   341 
   342 fun node (Elem ((":", []), ts)) = ts
   343   | node t = raise XML_BODY [t];
   344 
   345 fun vector atts =
   346   map_index (fn (i, (a, x)) => if int_atom a = i then x else raise XML_ATOM a) atts;
   347 
   348 fun tagged (Elem ((name, atts), ts)) = (int_atom name, (vector atts, ts))
   349   | tagged t = raise XML_BODY [t];
   350 
   351 
   352 (* representation of standard types *)
   353 
   354 fun properties [Elem ((":", props), [])] = props
   355   | properties ts = raise XML_BODY ts;
   356 
   357 fun string [] = ""
   358   | string [Text s] = s
   359   | string ts = raise XML_BODY ts;
   360 
   361 val int = int_atom o string;
   362 
   363 val bool = bool_atom o string;
   364 
   365 val unit = unit_atom o string;
   366 
   367 fun pair f g [t1, t2] = (f (node t1), g (node t2))
   368   | pair _ _ ts = raise XML_BODY ts;
   369 
   370 fun triple f g h [t1, t2, t3] = (f (node t1), g (node t2), h (node t3))
   371   | triple _ _ _ ts = raise XML_BODY ts;
   372 
   373 fun list f ts = map (f o node) ts;
   374 
   375 fun option _ [] = NONE
   376   | option f [t] = SOME (f (node t))
   377   | option _ ts = raise XML_BODY ts;
   378 
   379 fun variant fs [t] =
   380       let
   381         val (tag, (xs, ts)) = tagged t;
   382         val f = nth fs tag handle General.Subscript => raise XML_BODY [t];
   383       in f (xs, ts) end
   384   | variant _ ts = raise XML_BODY ts;
   385 
   386 end;
   387 
   388 end;