src/Pure/PIDE/xml.ML
author wenzelm
Sat, 10 Apr 2021 21:50:59 +0200
changeset 73556 192bcee4f8b8
parent 70997 325247f32da9
child 73570 e21aef453cd4
permissions -rw-r--r--
more robust treatment of empty markup: it allows to produce formal chunks;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
44698
0385292321a0 moved XML/YXML to src/Pure/PIDE;
wenzelm
parents: 43949
diff changeset
     1
(*  Title:      Pure/PIDE/xml.ML
0385292321a0 moved XML/YXML to src/Pure/PIDE;
wenzelm
parents: 43949
diff changeset
     2
    Author:     David Aspinall
0385292321a0 moved XML/YXML to src/Pure/PIDE;
wenzelm
parents: 43949
diff changeset
     3
    Author:     Stefan Berghofer
0385292321a0 moved XML/YXML to src/Pure/PIDE;
wenzelm
parents: 43949
diff changeset
     4
    Author:     Makarius
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
     5
46840
42e32c777581 tuned comment;
wenzelm
parents: 46839
diff changeset
     6
Untyped XML trees and representation of ML values.
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
     7
*)
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
     8
43767
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
     9
signature XML_DATA_OPS =
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
    10
sig
43778
ce9189450447 more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents: 43768
diff changeset
    11
  type 'a A
43767
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
    12
  type 'a T
43778
ce9189450447 more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents: 43768
diff changeset
    13
  type 'a V
70828
cb70d84a9f5e more compact XML representation;
wenzelm
parents: 69806
diff changeset
    14
  type 'a P
43778
ce9189450447 more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents: 43768
diff changeset
    15
  val int_atom: int A
ce9189450447 more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents: 43768
diff changeset
    16
  val bool_atom: bool A
ce9189450447 more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents: 43768
diff changeset
    17
  val unit_atom: unit A
43767
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
    18
  val properties: Properties.T T
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
    19
  val string: string T
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
    20
  val int: int T
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
    21
  val bool: bool T
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
    22
  val unit: unit T
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
    23
  val pair: 'a T -> 'b T -> ('a * 'b) T
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
    24
  val triple: 'a T -> 'b T -> 'c T -> ('a * 'b * 'c) T
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
    25
  val list: 'a T -> 'a list T
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
    26
  val option: 'a T -> 'a option T
43778
ce9189450447 more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents: 43768
diff changeset
    27
  val variant: 'a V list -> 'a T
43767
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
    28
end;
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
    29
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    30
signature XML =
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    31
sig
46837
5bdd68f380b3 clarified XML signature (again) -- coincide with basic Markup without explicit dependency;
wenzelm
parents: 45155
diff changeset
    32
  type attributes = (string * string) list
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    33
  datatype tree =
46837
5bdd68f380b3 clarified XML signature (again) -- coincide with basic Markup without explicit dependency;
wenzelm
parents: 45155
diff changeset
    34
      Elem of (string * attributes) * tree list
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    35
    | Text of string
38266
492d377ecfe2 type XML.body as basic data representation language;
wenzelm
parents: 38228
diff changeset
    36
  type body = tree list
70991
f9f7c34b7dd4 more scalable protocol_message: use XML.body directly (Output.output hook is not required);
wenzelm
parents: 70828
diff changeset
    37
  val blob: string list -> body
73556
192bcee4f8b8 more robust treatment of empty markup: it allows to produce formal chunks;
wenzelm
parents: 70997
diff changeset
    38
  val chunk: body -> tree
70994
ff11af12dfdd clarified signature;
wenzelm
parents: 70991
diff changeset
    39
  val is_empty: tree -> bool
ff11af12dfdd clarified signature;
wenzelm
parents: 70991
diff changeset
    40
  val is_empty_body: body -> bool
69234
2dec32c7313f more Haskell operations;
wenzelm
parents: 69224
diff changeset
    41
  val xml_elemN: string
2dec32c7313f more Haskell operations;
wenzelm
parents: 69224
diff changeset
    42
  val xml_nameN: string
2dec32c7313f more Haskell operations;
wenzelm
parents: 69224
diff changeset
    43
  val xml_bodyN: string
49650
9fad6480300d support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents: 49599
diff changeset
    44
  val wrap_elem: ((string * attributes) * tree list) * tree list -> tree
9fad6480300d support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents: 49599
diff changeset
    45
  val unwrap_elem: tree -> (((string * attributes) * tree list) * tree list) option
26546
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
    46
  val add_content: tree -> Buffer.T -> Buffer.T
39555
ccb223a4d49c added XML.content_of convenience -- cover XML.body, which is the general situation;
wenzelm
parents: 38474
diff changeset
    47
  val content_of: body -> string
56059
2390391584c2 some document antiquotations for Isabelle/jEdit elements;
wenzelm
parents: 49650
diff changeset
    48
  val trim_blanks: body -> body
26546
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
    49
  val header: string
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
    50
  val text: string -> string
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
    51
  val element: string -> attributes -> string list -> string
69345
6bd63c94cf62 tuned signature (see also src/Tools/Haskell/Markup.hs);
wenzelm
parents: 69234
diff changeset
    52
  val output_markup: Markup.T -> Markup.output
26539
a0754be538ab added output_markup (from Tools/isabelle_process.ML);
wenzelm
parents: 26525
diff changeset
    53
  val string_of: tree -> string
43791
5e9a1d71f94d XML.pretty with depth limit;
wenzelm
parents: 43783
diff changeset
    54
  val pretty: int -> tree -> Pretty.T
26984
d0e098e206f3 added parse_document (optional unchecked header material);
wenzelm
parents: 26554
diff changeset
    55
  val parse_comments: string list -> unit * string list
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    56
  val parse_string : string -> string option
26546
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
    57
  val parse_element: string list -> tree * string list
26984
d0e098e206f3 added parse_document (optional unchecked header material);
wenzelm
parents: 26554
diff changeset
    58
  val parse_document: string list -> tree * string list
26539
a0754be538ab added output_markup (from Tools/isabelle_process.ML);
wenzelm
parents: 26525
diff changeset
    59
  val parse: string -> tree
43767
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
    60
  exception XML_ATOM of string
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
    61
  exception XML_BODY of body
65333
289561ca4fa3 more operations;
wenzelm
parents: 63806
diff changeset
    62
  structure Encode:
289561ca4fa3 more operations;
wenzelm
parents: 63806
diff changeset
    63
  sig
289561ca4fa3 more operations;
wenzelm
parents: 63806
diff changeset
    64
    include XML_DATA_OPS
289561ca4fa3 more operations;
wenzelm
parents: 63806
diff changeset
    65
    val tree: tree T
289561ca4fa3 more operations;
wenzelm
parents: 63806
diff changeset
    66
  end
289561ca4fa3 more operations;
wenzelm
parents: 63806
diff changeset
    67
  structure Decode:
289561ca4fa3 more operations;
wenzelm
parents: 63806
diff changeset
    68
  sig
289561ca4fa3 more operations;
wenzelm
parents: 63806
diff changeset
    69
    include XML_DATA_OPS
289561ca4fa3 more operations;
wenzelm
parents: 63806
diff changeset
    70
    val tree: tree T
289561ca4fa3 more operations;
wenzelm
parents: 63806
diff changeset
    71
  end
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    72
end;
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    73
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    74
structure XML: XML =
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    75
struct
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    76
26546
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
    77
(** XML trees **)
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
    78
70991
f9f7c34b7dd4 more scalable protocol_message: use XML.body directly (Output.output hook is not required);
wenzelm
parents: 70828
diff changeset
    79
open Output_Primitives.XML;
26546
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
    80
70991
f9f7c34b7dd4 more scalable protocol_message: use XML.body directly (Output.output hook is not required);
wenzelm
parents: 70828
diff changeset
    81
val blob = map Text;
38266
492d377ecfe2 type XML.body as basic data representation language;
wenzelm
parents: 38228
diff changeset
    82
73556
192bcee4f8b8 more robust treatment of empty markup: it allows to produce formal chunks;
wenzelm
parents: 70997
diff changeset
    83
fun chunk body = Elem (Markup.empty, body);
192bcee4f8b8 more robust treatment of empty markup: it allows to produce formal chunks;
wenzelm
parents: 70997
diff changeset
    84
70994
ff11af12dfdd clarified signature;
wenzelm
parents: 70991
diff changeset
    85
fun is_empty (Text "") = true
ff11af12dfdd clarified signature;
wenzelm
parents: 70991
diff changeset
    86
  | is_empty _ = false;
ff11af12dfdd clarified signature;
wenzelm
parents: 70991
diff changeset
    87
ff11af12dfdd clarified signature;
wenzelm
parents: 70991
diff changeset
    88
val is_empty_body = forall is_empty;
ff11af12dfdd clarified signature;
wenzelm
parents: 70991
diff changeset
    89
49650
9fad6480300d support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents: 49599
diff changeset
    90
9fad6480300d support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents: 49599
diff changeset
    91
(* wrapped elements *)
9fad6480300d support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents: 49599
diff changeset
    92
9fad6480300d support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents: 49599
diff changeset
    93
val xml_elemN = "xml_elem";
9fad6480300d support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents: 49599
diff changeset
    94
val xml_nameN = "xml_name";
9fad6480300d support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents: 49599
diff changeset
    95
val xml_bodyN = "xml_body";
9fad6480300d support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents: 49599
diff changeset
    96
9fad6480300d support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents: 49599
diff changeset
    97
fun wrap_elem (((a, atts), body1), body2) =
9fad6480300d support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents: 49599
diff changeset
    98
  Elem ((xml_elemN, (xml_nameN, a) :: atts), Elem ((xml_bodyN, []), body1) :: body2);
9fad6480300d support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents: 49599
diff changeset
    99
9fad6480300d support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents: 49599
diff changeset
   100
fun unwrap_elem (Elem ((name, (n, a) :: atts), Elem ((name', atts'), body1) :: body2)) =
9fad6480300d support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents: 49599
diff changeset
   101
      if name = xml_elemN andalso n = xml_nameN andalso name' = xml_bodyN andalso null atts'
9fad6480300d support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents: 49599
diff changeset
   102
      then SOME (((a, atts), body1), body2) else NONE
9fad6480300d support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents: 49599
diff changeset
   103
  | unwrap_elem _ = NONE;
9fad6480300d support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents: 49599
diff changeset
   104
9fad6480300d support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents: 49599
diff changeset
   105
69224
fe9d746b273e tuned comments;
wenzelm
parents: 65333
diff changeset
   106
(* text content *)
49650
9fad6480300d support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents: 49599
diff changeset
   107
9fad6480300d support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents: 49599
diff changeset
   108
fun add_content tree =
9fad6480300d support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents: 49599
diff changeset
   109
  (case unwrap_elem tree of
9fad6480300d support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents: 49599
diff changeset
   110
    SOME (_, ts) => fold add_content ts
9fad6480300d support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents: 49599
diff changeset
   111
  | NONE =>
9fad6480300d support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents: 49599
diff changeset
   112
      (case tree of
9fad6480300d support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents: 49599
diff changeset
   113
        Elem (_, ts) => fold add_content ts
9fad6480300d support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents: 49599
diff changeset
   114
      | Text s => Buffer.add s));
26546
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
   115
39555
ccb223a4d49c added XML.content_of convenience -- cover XML.body, which is the general situation;
wenzelm
parents: 38474
diff changeset
   116
fun content_of body = Buffer.empty |> fold add_content body |> Buffer.content;
ccb223a4d49c added XML.content_of convenience -- cover XML.body, which is the general situation;
wenzelm
parents: 38474
diff changeset
   117
26546
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
   118
56059
2390391584c2 some document antiquotations for Isabelle/jEdit elements;
wenzelm
parents: 49650
diff changeset
   119
(* trim blanks *)
2390391584c2 some document antiquotations for Isabelle/jEdit elements;
wenzelm
parents: 49650
diff changeset
   120
2390391584c2 some document antiquotations for Isabelle/jEdit elements;
wenzelm
parents: 49650
diff changeset
   121
fun trim_blanks trees =
2390391584c2 some document antiquotations for Isabelle/jEdit elements;
wenzelm
parents: 49650
diff changeset
   122
  trees |> maps
2390391584c2 some document antiquotations for Isabelle/jEdit elements;
wenzelm
parents: 49650
diff changeset
   123
    (fn Elem (markup, body) => [Elem (markup, trim_blanks body)]
2390391584c2 some document antiquotations for Isabelle/jEdit elements;
wenzelm
parents: 49650
diff changeset
   124
      | Text s =>
61707
wenzelm
parents: 61476
diff changeset
   125
          let val s' = s |> raw_explode |> trim Symbol.is_blank |> implode;
56059
2390391584c2 some document antiquotations for Isabelle/jEdit elements;
wenzelm
parents: 49650
diff changeset
   126
          in if s' = "" then [] else [Text s'] end);
2390391584c2 some document antiquotations for Isabelle/jEdit elements;
wenzelm
parents: 49650
diff changeset
   127
2390391584c2 some document antiquotations for Isabelle/jEdit elements;
wenzelm
parents: 49650
diff changeset
   128
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   129
26525
14a56f013469 added detect;
wenzelm
parents: 25838
diff changeset
   130
(** string representation **)
14a56f013469 added detect;
wenzelm
parents: 25838
diff changeset
   131
69806
2156053c4ce9 tuned according to Scala version;
wenzelm
parents: 69804
diff changeset
   132
val header = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   133
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   134
26546
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
   135
(* escaped text *)
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   136
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   137
fun decode "&lt;" = "<"
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   138
  | decode "&gt;" = ">"
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   139
  | decode "&amp;" = "&"
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   140
  | decode "&apos;" = "'"
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   141
  | decode "&quot;" = "\""
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   142
  | decode c = c;
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   143
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   144
fun encode "<" = "&lt;"
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   145
  | encode ">" = "&gt;"
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   146
  | encode "&" = "&amp;"
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   147
  | encode "'" = "&apos;"
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   148
  | encode "\"" = "&quot;"
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   149
  | encode c = c;
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   150
25838
00b2a1b2c4e9 removed unused text_charref, cdata;
wenzelm
parents: 24584
diff changeset
   151
val text = translate_string encode;
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   152
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   153
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   154
(* elements *)
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   155
26539
a0754be538ab added output_markup (from Tools/isabelle_process.ML);
wenzelm
parents: 26525
diff changeset
   156
fun elem name atts =
26551
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   157
  space_implode " " (name :: map (fn (a, x) => a ^ "=\"" ^ text x ^ "\"") atts);
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   158
26525
14a56f013469 added detect;
wenzelm
parents: 25838
diff changeset
   159
fun element name atts body =
26539
a0754be538ab added output_markup (from Tools/isabelle_process.ML);
wenzelm
parents: 26525
diff changeset
   160
  let val b = implode body in
a0754be538ab added output_markup (from Tools/isabelle_process.ML);
wenzelm
parents: 26525
diff changeset
   161
    if b = "" then enclose "<" "/>" (elem name atts)
a0754be538ab added output_markup (from Tools/isabelle_process.ML);
wenzelm
parents: 26525
diff changeset
   162
    else enclose "<" ">" (elem name atts) ^ b ^ enclose "</" ">" name
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   163
  end;
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   164
27884
10c927e4abf5 output_markup: check Markup.is_none;
wenzelm
parents: 26984
diff changeset
   165
fun output_markup (markup as (name, atts)) =
38474
e498dc2eb576 uniform Markup.empty/Markup.Empty in ML and Scala;
wenzelm
parents: 38266
diff changeset
   166
  if Markup.is_empty markup then Markup.no_output
27884
10c927e4abf5 output_markup: check Markup.is_none;
wenzelm
parents: 26984
diff changeset
   167
  else (enclose "<" ">" (elem name atts), enclose "</" ">" name);
26539
a0754be538ab added output_markup (from Tools/isabelle_process.ML);
wenzelm
parents: 26525
diff changeset
   168
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   169
26546
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
   170
(* output *)
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   171
43791
5e9a1d71f94d XML.pretty with depth limit;
wenzelm
parents: 43783
diff changeset
   172
fun buffer_of depth tree =
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   173
  let
43791
5e9a1d71f94d XML.pretty with depth limit;
wenzelm
parents: 43783
diff changeset
   174
    fun traverse _ (Elem ((name, atts), [])) =
26539
a0754be538ab added output_markup (from Tools/isabelle_process.ML);
wenzelm
parents: 26525
diff changeset
   175
          Buffer.add "<" #> Buffer.add (elem name atts) #> Buffer.add "/>"
43791
5e9a1d71f94d XML.pretty with depth limit;
wenzelm
parents: 43783
diff changeset
   176
      | traverse d (Elem ((name, atts), ts)) =
26539
a0754be538ab added output_markup (from Tools/isabelle_process.ML);
wenzelm
parents: 26525
diff changeset
   177
          Buffer.add "<" #> Buffer.add (elem name atts) #> Buffer.add ">" #>
43791
5e9a1d71f94d XML.pretty with depth limit;
wenzelm
parents: 43783
diff changeset
   178
          traverse_body d ts #>
26525
14a56f013469 added detect;
wenzelm
parents: 25838
diff changeset
   179
          Buffer.add "</" #> Buffer.add name #> Buffer.add ">"
43791
5e9a1d71f94d XML.pretty with depth limit;
wenzelm
parents: 43783
diff changeset
   180
      | traverse _ (Text s) = Buffer.add (text s)
5e9a1d71f94d XML.pretty with depth limit;
wenzelm
parents: 43783
diff changeset
   181
    and traverse_body 0 _ = Buffer.add "..."
5e9a1d71f94d XML.pretty with depth limit;
wenzelm
parents: 43783
diff changeset
   182
      | traverse_body d ts = fold (traverse (d - 1)) ts;
5e9a1d71f94d XML.pretty with depth limit;
wenzelm
parents: 43783
diff changeset
   183
  in Buffer.empty |> traverse depth tree end;
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   184
43791
5e9a1d71f94d XML.pretty with depth limit;
wenzelm
parents: 43783
diff changeset
   185
val string_of = Buffer.content o buffer_of ~1;
5e9a1d71f94d XML.pretty with depth limit;
wenzelm
parents: 43783
diff changeset
   186
5e9a1d71f94d XML.pretty with depth limit;
wenzelm
parents: 43783
diff changeset
   187
fun pretty depth tree =
5e9a1d71f94d XML.pretty with depth limit;
wenzelm
parents: 43783
diff changeset
   188
  Pretty.str (Buffer.content (buffer_of (Int.max (0, depth)) tree));
25838
00b2a1b2c4e9 removed unused text_charref, cdata;
wenzelm
parents: 24584
diff changeset
   189
62819
d3ff367a16a0 careful export of type-dependent functions, without losing their special status;
wenzelm
parents: 62663
diff changeset
   190
val _ = ML_system_pp (fn depth => fn _ => Pretty.to_polyml o pretty (FixedInt.toInt depth));
62663
bea354f6ff21 clarified modules;
wenzelm
parents: 61707
diff changeset
   191
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   192
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   193
44698
0385292321a0 moved XML/YXML to src/Pure/PIDE;
wenzelm
parents: 43949
diff changeset
   194
(** XML parsing **)
26546
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
   195
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
   196
local
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   197
43947
9b00f09f7721 defer evaluation of Scan.message, for improved performance in the frequent situation where failure is handled later (e.g. via ||);
wenzelm
parents: 43844
diff changeset
   198
fun err msg (xs, _) =
9b00f09f7721 defer evaluation of Scan.message, for improved performance in the frequent situation where failure is handled later (e.g. via ||);
wenzelm
parents: 43844
diff changeset
   199
  fn () => "XML parsing error: " ^ msg () ^ "\nfound: " ^ quote (Symbol.beginning 100 xs);
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   200
26984
d0e098e206f3 added parse_document (optional unchecked header material);
wenzelm
parents: 26554
diff changeset
   201
fun ignored _ = [];
d0e098e206f3 added parse_document (optional unchecked header material);
wenzelm
parents: 26554
diff changeset
   202
45155
3216d65d8f34 slightly more standard-conformant XML parsing (see also 94033767ef9b);
wenzelm
parents: 44809
diff changeset
   203
fun name_start_char c = Symbol.is_ascii_letter c orelse c = ":" orelse c = "_";
3216d65d8f34 slightly more standard-conformant XML parsing (see also 94033767ef9b);
wenzelm
parents: 44809
diff changeset
   204
fun name_char c = name_start_char c orelse Symbol.is_ascii_digit c orelse c = "-" orelse c = ".";
3216d65d8f34 slightly more standard-conformant XML parsing (see also 94033767ef9b);
wenzelm
parents: 44809
diff changeset
   205
val parse_name = Scan.one name_start_char ::: Scan.many name_char;
3216d65d8f34 slightly more standard-conformant XML parsing (see also 94033767ef9b);
wenzelm
parents: 44809
diff changeset
   206
26551
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   207
val blanks = Scan.many Symbol.is_blank;
45155
3216d65d8f34 slightly more standard-conformant XML parsing (see also 94033767ef9b);
wenzelm
parents: 44809
diff changeset
   208
val special = $$ "&" ^^ (parse_name >> implode) ^^ $$ ";" >> decode;
58854
b979c781c2db discontinued obsolete \<^sync> marker;
wenzelm
parents: 56059
diff changeset
   209
val regular = Scan.one Symbol.not_eof;
b979c781c2db discontinued obsolete \<^sync> marker;
wenzelm
parents: 56059
diff changeset
   210
fun regular_except x = Scan.one (fn c => Symbol.not_eof c andalso c <> x);
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   211
26551
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   212
val parse_chars = Scan.repeat1 (special || regular_except "<") >> implode;
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   213
26551
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   214
val parse_cdata =
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   215
  Scan.this_string "<![CDATA[" |--
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   216
  (Scan.repeat (Scan.unless (Scan.this_string "]]>") regular) >> implode) --|
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   217
  Scan.this_string "]]>";
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   218
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   219
val parse_att =
45155
3216d65d8f34 slightly more standard-conformant XML parsing (see also 94033767ef9b);
wenzelm
parents: 44809
diff changeset
   220
  ((parse_name >> implode) --| (blanks -- $$ "=" -- blanks)) --
26551
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   221
  (($$ "\"" || $$ "'") :|-- (fn s =>
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   222
    (Scan.repeat (special || regular_except s) >> implode) --| $$ s));
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   223
26551
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   224
val parse_comment =
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   225
  Scan.this_string "<!--" --
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   226
  Scan.repeat (Scan.unless (Scan.this_string "-->") regular) --
26984
d0e098e206f3 added parse_document (optional unchecked header material);
wenzelm
parents: 26554
diff changeset
   227
  Scan.this_string "-->" >> ignored;
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   228
26551
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   229
val parse_processing_instruction =
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   230
  Scan.this_string "<?" --
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   231
  Scan.repeat (Scan.unless (Scan.this_string "?>") regular) --
26984
d0e098e206f3 added parse_document (optional unchecked header material);
wenzelm
parents: 26554
diff changeset
   232
  Scan.this_string "?>" >> ignored;
d0e098e206f3 added parse_document (optional unchecked header material);
wenzelm
parents: 26554
diff changeset
   233
d0e098e206f3 added parse_document (optional unchecked header material);
wenzelm
parents: 26554
diff changeset
   234
val parse_doctype =
d0e098e206f3 added parse_document (optional unchecked header material);
wenzelm
parents: 26554
diff changeset
   235
  Scan.this_string "<!DOCTYPE" --
d0e098e206f3 added parse_document (optional unchecked header material);
wenzelm
parents: 26554
diff changeset
   236
  Scan.repeat (Scan.unless ($$ ">") regular) --
d0e098e206f3 added parse_document (optional unchecked header material);
wenzelm
parents: 26554
diff changeset
   237
  $$ ">" >> ignored;
d0e098e206f3 added parse_document (optional unchecked header material);
wenzelm
parents: 26554
diff changeset
   238
d0e098e206f3 added parse_document (optional unchecked header material);
wenzelm
parents: 26554
diff changeset
   239
val parse_misc =
d0e098e206f3 added parse_document (optional unchecked header material);
wenzelm
parents: 26554
diff changeset
   240
  Scan.one Symbol.is_blank >> ignored ||
d0e098e206f3 added parse_document (optional unchecked header material);
wenzelm
parents: 26554
diff changeset
   241
  parse_processing_instruction ||
d0e098e206f3 added parse_document (optional unchecked header material);
wenzelm
parents: 26554
diff changeset
   242
  parse_comment;
26551
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   243
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   244
val parse_optional_text =
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   245
  Scan.optional (parse_chars >> (single o Text)) [];
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   246
26546
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
   247
in
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
   248
26984
d0e098e206f3 added parse_document (optional unchecked header material);
wenzelm
parents: 26554
diff changeset
   249
val parse_comments =
d0e098e206f3 added parse_document (optional unchecked header material);
wenzelm
parents: 26554
diff changeset
   250
  blanks -- Scan.repeat (parse_comment -- blanks >> K ()) >> K ();
d0e098e206f3 added parse_document (optional unchecked header material);
wenzelm
parents: 26554
diff changeset
   251
40627
becf5d5187cc renamed raw "explode" function to "raw_explode" to emphasize its meaning;
wenzelm
parents: 40131
diff changeset
   252
val parse_string = Scan.read Symbol.stopper parse_chars o raw_explode;
26546
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
   253
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   254
fun parse_content xs =
26551
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   255
  (parse_optional_text @@@
61476
1884c40f1539 tuned signature;
wenzelm
parents: 60982
diff changeset
   256
    Scan.repeats
26551
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   257
      ((parse_element >> single ||
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   258
        parse_cdata >> (single o Text) ||
26984
d0e098e206f3 added parse_document (optional unchecked header material);
wenzelm
parents: 26554
diff changeset
   259
        parse_processing_instruction ||
d0e098e206f3 added parse_document (optional unchecked header material);
wenzelm
parents: 26554
diff changeset
   260
        parse_comment)
61476
1884c40f1539 tuned signature;
wenzelm
parents: 60982
diff changeset
   261
      @@@ parse_optional_text)) xs
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   262
26546
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
   263
and parse_element xs =
43949
94033767ef9b more precise parse_name according to XML standard;
wenzelm
parents: 43947
diff changeset
   264
  ($$ "<" |-- parse_name -- Scan.repeat (blanks |-- parse_att) --| blanks :--
94033767ef9b more precise parse_name according to XML standard;
wenzelm
parents: 43947
diff changeset
   265
    (fn (name, _) =>
43947
9b00f09f7721 defer evaluation of Scan.message, for improved performance in the frequent situation where failure is handled later (e.g. via ||);
wenzelm
parents: 43844
diff changeset
   266
      !! (err (fn () => "Expected > or />"))
43949
94033767ef9b more precise parse_name according to XML standard;
wenzelm
parents: 43947
diff changeset
   267
       ($$ "/" -- $$ ">" >> ignored ||
94033767ef9b more precise parse_name according to XML standard;
wenzelm
parents: 43947
diff changeset
   268
        $$ ">" |-- parse_content --|
94033767ef9b more precise parse_name according to XML standard;
wenzelm
parents: 43947
diff changeset
   269
          !! (err (fn () => "Expected </" ^ implode name ^ ">"))
94033767ef9b more precise parse_name according to XML standard;
wenzelm
parents: 43947
diff changeset
   270
              ($$ "<" -- $$ "/" -- Scan.this name -- blanks -- $$ ">")))
94033767ef9b more precise parse_name according to XML standard;
wenzelm
parents: 43947
diff changeset
   271
    >> (fn ((name, atts), body) => Elem ((implode name, atts), body))) xs;
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   272
26984
d0e098e206f3 added parse_document (optional unchecked header material);
wenzelm
parents: 26554
diff changeset
   273
val parse_document =
d0e098e206f3 added parse_document (optional unchecked header material);
wenzelm
parents: 26554
diff changeset
   274
  (Scan.repeat parse_misc -- Scan.option parse_doctype -- Scan.repeat parse_misc)
d0e098e206f3 added parse_document (optional unchecked header material);
wenzelm
parents: 26554
diff changeset
   275
  |-- parse_element;
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   276
26539
a0754be538ab added output_markup (from Tools/isabelle_process.ML);
wenzelm
parents: 26525
diff changeset
   277
fun parse s =
43947
9b00f09f7721 defer evaluation of Scan.message, for improved performance in the frequent situation where failure is handled later (e.g. via ||);
wenzelm
parents: 43844
diff changeset
   278
  (case Scan.finite Symbol.stopper (Scan.error (!! (err (fn () => "Malformed element"))
40627
becf5d5187cc renamed raw "explode" function to "raw_explode" to emphasize its meaning;
wenzelm
parents: 40131
diff changeset
   279
      (blanks |-- parse_document --| blanks))) (raw_explode s) of
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   280
    (x, []) => x
48769
e3b7087bb923 tuned message;
wenzelm
parents: 47199
diff changeset
   281
  | (_, ys) => error ("XML parsing error: unprocessed input\n" ^ Symbol.beginning 100 ys));
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   282
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   283
end;
26546
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
   284
43767
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   285
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   286
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   287
(** XML as data representation language **)
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   288
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   289
exception XML_ATOM of string;
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   290
exception XML_BODY of tree list;
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   291
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   292
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   293
structure Encode =
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   294
struct
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   295
43778
ce9189450447 more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents: 43768
diff changeset
   296
type 'a A = 'a -> string;
43767
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   297
type 'a T = 'a -> body;
43778
ce9189450447 more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents: 43768
diff changeset
   298
type 'a V = 'a -> string list * body;
70828
cb70d84a9f5e more compact XML representation;
wenzelm
parents: 69806
diff changeset
   299
type 'a P = 'a -> string list;
43767
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   300
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   301
43778
ce9189450447 more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents: 43768
diff changeset
   302
(* atomic values *)
43767
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   303
69574
wenzelm
parents: 69345
diff changeset
   304
fun int_atom i = Value.print_int i;
43767
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   305
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   306
fun bool_atom false = "0"
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   307
  | bool_atom true = "1";
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   308
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   309
fun unit_atom () = "";
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   310
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   311
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   312
(* structural nodes *)
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   313
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   314
fun node ts = Elem ((":", []), ts);
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   315
43778
ce9189450447 more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents: 43768
diff changeset
   316
fun vector xs = map_index (fn (i, x) => (int_atom i, x)) xs;
ce9189450447 more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents: 43768
diff changeset
   317
ce9189450447 more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents: 43768
diff changeset
   318
fun tagged (tag, (xs, ts)) = Elem ((int_atom tag, vector xs), ts);
43767
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   319
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   320
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   321
(* representation of standard types *)
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   322
65333
289561ca4fa3 more operations;
wenzelm
parents: 63806
diff changeset
   323
fun tree (t: tree) = [t];
289561ca4fa3 more operations;
wenzelm
parents: 63806
diff changeset
   324
43767
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   325
fun properties props = [Elem ((":", props), [])];
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   326
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   327
fun string "" = []
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   328
  | string s = [Text s];
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   329
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   330
val int = string o int_atom;
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   331
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   332
val bool = string o bool_atom;
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   333
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   334
val unit = string o unit_atom;
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   335
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   336
fun pair f g (x, y) = [node (f x), node (g y)];
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   337
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   338
fun triple f g h (x, y, z) = [node (f x), node (g y), node (h z)];
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   339
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   340
fun list f xs = map (node o f) xs;
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   341
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   342
fun option _ NONE = []
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   343
  | option f (SOME x) = [node (f x)];
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   344
47199
15ede9f1da3f more specific notion of partiality (cf. Scala version);
wenzelm
parents: 46840
diff changeset
   345
fun variant fs x =
15ede9f1da3f more specific notion of partiality (cf. Scala version);
wenzelm
parents: 46840
diff changeset
   346
  [tagged (the (get_index (fn f => SOME (f x) handle General.Match => NONE) fs))];
43767
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   347
26546
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
   348
end;
43767
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   349
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   350
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   351
structure Decode =
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   352
struct
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   353
43778
ce9189450447 more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents: 43768
diff changeset
   354
type 'a A = string -> 'a;
43767
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   355
type 'a T = body -> 'a;
43778
ce9189450447 more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents: 43768
diff changeset
   356
type 'a V = string list * body -> 'a;
70828
cb70d84a9f5e more compact XML representation;
wenzelm
parents: 69806
diff changeset
   357
type 'a P = string list -> 'a;
43767
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   358
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   359
43778
ce9189450447 more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents: 43768
diff changeset
   360
(* atomic values *)
43767
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   361
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   362
fun int_atom s =
63806
c54a53ef1873 clarified modules;
wenzelm
parents: 62819
diff changeset
   363
  Value.parse_int s
43797
fad7758421bf more precise integer Markup.properties/XML.attributes: disallow ML-style ~ minus;
wenzelm
parents: 43791
diff changeset
   364
    handle Fail _ => raise XML_ATOM s;
43767
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   365
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   366
fun bool_atom "0" = false
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   367
  | bool_atom "1" = true
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   368
  | bool_atom s = raise XML_ATOM s;
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   369
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   370
fun unit_atom "" = ()
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   371
  | unit_atom s = raise XML_ATOM s;
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   372
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   373
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   374
(* structural nodes *)
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   375
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   376
fun node (Elem ((":", []), ts)) = ts
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   377
  | node t = raise XML_BODY [t];
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   378
43783
ef45eaf2775f made SML/NJ happy;
wenzelm
parents: 43781
diff changeset
   379
fun vector atts =
46839
f7232c078fa5 simplified -- plain map_index is sufficient (pointed out by Enrico Tassi);
wenzelm
parents: 46837
diff changeset
   380
  map_index (fn (i, (a, x)) => if int_atom a = i then x else raise XML_ATOM a) atts;
43778
ce9189450447 more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents: 43768
diff changeset
   381
43844
wenzelm
parents: 43797
diff changeset
   382
fun tagged (Elem ((name, atts), ts)) = (int_atom name, (vector atts, ts))
43767
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   383
  | tagged t = raise XML_BODY [t];
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   384
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   385
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   386
(* representation of standard types *)
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   387
65333
289561ca4fa3 more operations;
wenzelm
parents: 63806
diff changeset
   388
fun tree [t] = t
289561ca4fa3 more operations;
wenzelm
parents: 63806
diff changeset
   389
  | tree ts = raise XML_BODY ts;
289561ca4fa3 more operations;
wenzelm
parents: 63806
diff changeset
   390
43767
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   391
fun properties [Elem ((":", props), [])] = props
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   392
  | properties ts = raise XML_BODY ts;
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   393
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   394
fun string [] = ""
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   395
  | string [Text s] = s
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   396
  | string ts = raise XML_BODY ts;
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   397
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   398
val int = int_atom o string;
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   399
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   400
val bool = bool_atom o string;
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   401
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   402
val unit = unit_atom o string;
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   403
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   404
fun pair f g [t1, t2] = (f (node t1), g (node t2))
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   405
  | pair _ _ ts = raise XML_BODY ts;
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   406
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   407
fun triple f g h [t1, t2, t3] = (f (node t1), g (node t2), h (node t3))
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   408
  | triple _ _ _ ts = raise XML_BODY ts;
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   409
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   410
fun list f ts = map (f o node) ts;
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   411
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   412
fun option _ [] = NONE
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   413
  | option f [t] = SOME (f (node t))
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   414
  | option _ ts = raise XML_BODY ts;
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   415
43768
d52ab827d62b more precise exceptions;
wenzelm
parents: 43767
diff changeset
   416
fun variant fs [t] =
d52ab827d62b more precise exceptions;
wenzelm
parents: 43767
diff changeset
   417
      let
43778
ce9189450447 more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents: 43768
diff changeset
   418
        val (tag, (xs, ts)) = tagged t;
43768
d52ab827d62b more precise exceptions;
wenzelm
parents: 43767
diff changeset
   419
        val f = nth fs tag handle General.Subscript => raise XML_BODY [t];
43778
ce9189450447 more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents: 43768
diff changeset
   420
      in f (xs, ts) end
43767
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   421
  | variant _ ts = raise XML_BODY ts;
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   422
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   423
end;
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   424
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   425
end;