src/Pure/General/xml.ML
author wenzelm
Tue, 12 Jul 2011 10:44:30 +0200
changeset 43767 e0219ef7f84c
parent 40627 becf5d5187cc
child 43768 d52ab827d62b
permissions -rw-r--r--
tuned XML modules;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24584
01e83ffa6c54 fixed title
haftmann
parents: 24264
diff changeset
     1
(*  Title:      Pure/General/xml.ML
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
     2
    Author:     David Aspinall, Stefan Berghofer and Markus Wenzel
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
     3
38228
ada3ab6b9085 simplified type XML.tree: embed Markup.T directly, avoid slightly odd triple;
wenzelm
parents: 31469
diff changeset
     4
Simple XML tree values.
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
     5
*)
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
     6
43767
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
     7
signature XML_DATA_OPS =
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
     8
sig
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
     9
  type 'a T
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
    10
  val properties: Properties.T T
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
    11
  val string: string T
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
    12
  val int: int T
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
    13
  val bool: bool T
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
    14
  val unit: unit T
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
    15
  val pair: 'a T -> 'b T -> ('a * 'b) T
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
    16
  val triple: 'a T -> 'b T -> 'c T -> ('a * 'b * 'c) T
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
    17
  val list: 'a T -> 'a list T
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
    18
  val option: 'a T -> 'a option T
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
    19
  val variant: 'a T list -> 'a T
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
    20
end;
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
    21
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    22
signature XML =
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    23
sig
28017
4919bd124a58 type Properties.T;
wenzelm
parents: 27884
diff changeset
    24
  type attributes = Properties.T
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    25
  datatype tree =
38228
ada3ab6b9085 simplified type XML.tree: embed Markup.T directly, avoid slightly odd triple;
wenzelm
parents: 31469
diff changeset
    26
      Elem of Markup.T * tree list
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    27
    | Text of string
38266
492d377ecfe2 type XML.body as basic data representation language;
wenzelm
parents: 38228
diff changeset
    28
  type body = tree list
26546
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
    29
  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
    30
  val content_of: body -> string
26546
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
    31
  val header: string
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
    32
  val text: string -> string
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
    33
  val element: string -> attributes -> string list -> string
40131
7cbebd636e79 explicitly qualify type Output.output, which is a slightly odd internal feature;
wenzelm
parents: 39555
diff changeset
    34
  val output_markup: Markup.T -> Output.output * Output.output
26539
a0754be538ab added output_markup (from Tools/isabelle_process.ML);
wenzelm
parents: 26525
diff changeset
    35
  val string_of: tree -> string
26546
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
    36
  val output: tree -> TextIO.outstream -> unit
26984
d0e098e206f3 added parse_document (optional unchecked header material);
wenzelm
parents: 26554
diff changeset
    37
  val parse_comments: string list -> unit * string list
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    38
  val parse_string : string -> string option
26546
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
    39
  val parse_element: string list -> tree * string list
26984
d0e098e206f3 added parse_document (optional unchecked header material);
wenzelm
parents: 26554
diff changeset
    40
  val parse_document: string list -> tree * string list
26539
a0754be538ab added output_markup (from Tools/isabelle_process.ML);
wenzelm
parents: 26525
diff changeset
    41
  val parse: string -> tree
43767
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
    42
  exception XML_ATOM of string
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
    43
  exception XML_BODY of body
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
    44
  structure Encode: XML_DATA_OPS where type 'a T = 'a -> body
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
    45
  structure Decode: XML_DATA_OPS where type 'a T = body -> 'a
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    46
end;
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    47
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    48
structure XML: XML =
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    49
struct
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    50
26546
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
    51
(** XML trees **)
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
    52
28017
4919bd124a58 type Properties.T;
wenzelm
parents: 27884
diff changeset
    53
type attributes = Properties.T;
26546
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
    54
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
    55
datatype tree =
38228
ada3ab6b9085 simplified type XML.tree: embed Markup.T directly, avoid slightly odd triple;
wenzelm
parents: 31469
diff changeset
    56
    Elem of Markup.T * tree list
28033
f03b5856f286 removed obsolete XML.Output workaround;
wenzelm
parents: 28017
diff changeset
    57
  | Text of string;
26546
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
    58
38266
492d377ecfe2 type XML.body as basic data representation language;
wenzelm
parents: 38228
diff changeset
    59
type body = tree list;
492d377ecfe2 type XML.body as basic data representation language;
wenzelm
parents: 38228
diff changeset
    60
38228
ada3ab6b9085 simplified type XML.tree: embed Markup.T directly, avoid slightly odd triple;
wenzelm
parents: 31469
diff changeset
    61
fun add_content (Elem (_, ts)) = fold add_content ts
28033
f03b5856f286 removed obsolete XML.Output workaround;
wenzelm
parents: 28017
diff changeset
    62
  | add_content (Text s) = Buffer.add s;
26546
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
    63
39555
ccb223a4d49c added XML.content_of convenience -- cover XML.body, which is the general situation;
wenzelm
parents: 38474
diff changeset
    64
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
    65
26546
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
    66
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    67
26525
14a56f013469 added detect;
wenzelm
parents: 25838
diff changeset
    68
(** string representation **)
14a56f013469 added detect;
wenzelm
parents: 25838
diff changeset
    69
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    70
val header = "<?xml version=\"1.0\"?>\n";
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    71
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    72
26546
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
    73
(* escaped text *)
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    74
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    75
fun decode "&lt;" = "<"
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    76
  | decode "&gt;" = ">"
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    77
  | decode "&amp;" = "&"
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    78
  | decode "&apos;" = "'"
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    79
  | decode "&quot;" = "\""
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    80
  | decode c = c;
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    81
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    82
fun encode "<" = "&lt;"
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    83
  | encode ">" = "&gt;"
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    84
  | encode "&" = "&amp;"
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    85
  | encode "'" = "&apos;"
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    86
  | encode "\"" = "&quot;"
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    87
  | encode c = c;
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    88
25838
00b2a1b2c4e9 removed unused text_charref, cdata;
wenzelm
parents: 24584
diff changeset
    89
val text = translate_string encode;
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    90
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    91
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    92
(* elements *)
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    93
26539
a0754be538ab added output_markup (from Tools/isabelle_process.ML);
wenzelm
parents: 26525
diff changeset
    94
fun elem name atts =
26551
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
    95
  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
    96
26525
14a56f013469 added detect;
wenzelm
parents: 25838
diff changeset
    97
fun element name atts body =
26539
a0754be538ab added output_markup (from Tools/isabelle_process.ML);
wenzelm
parents: 26525
diff changeset
    98
  let val b = implode body in
a0754be538ab added output_markup (from Tools/isabelle_process.ML);
wenzelm
parents: 26525
diff changeset
    99
    if b = "" then enclose "<" "/>" (elem name atts)
a0754be538ab added output_markup (from Tools/isabelle_process.ML);
wenzelm
parents: 26525
diff changeset
   100
    else enclose "<" ">" (elem name atts) ^ b ^ enclose "</" ">" name
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   101
  end;
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   102
27884
10c927e4abf5 output_markup: check Markup.is_none;
wenzelm
parents: 26984
diff changeset
   103
fun output_markup (markup as (name, atts)) =
38474
e498dc2eb576 uniform Markup.empty/Markup.Empty in ML and Scala;
wenzelm
parents: 38266
diff changeset
   104
  if Markup.is_empty markup then Markup.no_output
27884
10c927e4abf5 output_markup: check Markup.is_none;
wenzelm
parents: 26984
diff changeset
   105
  else (enclose "<" ">" (elem name atts), enclose "</" ">" name);
26539
a0754be538ab added output_markup (from Tools/isabelle_process.ML);
wenzelm
parents: 26525
diff changeset
   106
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   107
26546
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
   108
(* output *)
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   109
26546
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
   110
fun buffer_of tree =
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   111
  let
38228
ada3ab6b9085 simplified type XML.tree: embed Markup.T directly, avoid slightly odd triple;
wenzelm
parents: 31469
diff changeset
   112
    fun traverse (Elem ((name, atts), [])) =
26539
a0754be538ab added output_markup (from Tools/isabelle_process.ML);
wenzelm
parents: 26525
diff changeset
   113
          Buffer.add "<" #> Buffer.add (elem name atts) #> Buffer.add "/>"
38228
ada3ab6b9085 simplified type XML.tree: embed Markup.T directly, avoid slightly odd triple;
wenzelm
parents: 31469
diff changeset
   114
      | traverse (Elem ((name, atts), ts)) =
26539
a0754be538ab added output_markup (from Tools/isabelle_process.ML);
wenzelm
parents: 26525
diff changeset
   115
          Buffer.add "<" #> Buffer.add (elem name atts) #> Buffer.add ">" #>
26546
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
   116
          fold traverse ts #>
26525
14a56f013469 added detect;
wenzelm
parents: 25838
diff changeset
   117
          Buffer.add "</" #> Buffer.add name #> Buffer.add ">"
28033
f03b5856f286 removed obsolete XML.Output workaround;
wenzelm
parents: 28017
diff changeset
   118
      | traverse (Text s) = Buffer.add (text s);
26546
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
   119
  in Buffer.empty |> traverse tree end;
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   120
26546
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
   121
val string_of = Buffer.content o buffer_of;
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
   122
val output = Buffer.output o buffer_of;
25838
00b2a1b2c4e9 removed unused text_charref, cdata;
wenzelm
parents: 24584
diff changeset
   123
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   124
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   125
26546
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
   126
(** XML parsing (slow) **)
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
   127
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
   128
local
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   129
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   130
fun err s (xs, _) =
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   131
  "XML parsing error: " ^ s ^ "\nfound: " ^ quote (Symbol.beginning 100 xs);
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   132
26984
d0e098e206f3 added parse_document (optional unchecked header material);
wenzelm
parents: 26554
diff changeset
   133
fun ignored _ = [];
d0e098e206f3 added parse_document (optional unchecked header material);
wenzelm
parents: 26554
diff changeset
   134
26551
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   135
val blanks = Scan.many Symbol.is_blank;
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   136
val special = $$ "&" ^^ Symbol.scan_id ^^ $$ ";" >> decode;
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   137
val regular = Scan.one Symbol.is_regular;
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   138
fun regular_except x = Scan.one (fn c => Symbol.is_regular c andalso c <> x);
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   139
26551
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   140
val parse_chars = Scan.repeat1 (special || regular_except "<") >> implode;
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   141
26551
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   142
val parse_cdata =
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   143
  Scan.this_string "<![CDATA[" |--
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   144
  (Scan.repeat (Scan.unless (Scan.this_string "]]>") regular) >> implode) --|
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   145
  Scan.this_string "]]>";
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   146
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   147
val parse_att =
26551
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   148
  (Symbol.scan_id --| (blanks -- $$ "=" -- blanks)) --
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   149
  (($$ "\"" || $$ "'") :|-- (fn s =>
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   150
    (Scan.repeat (special || regular_except s) >> implode) --| $$ s));
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   151
26551
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   152
val parse_comment =
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   153
  Scan.this_string "<!--" --
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   154
  Scan.repeat (Scan.unless (Scan.this_string "-->") regular) --
26984
d0e098e206f3 added parse_document (optional unchecked header material);
wenzelm
parents: 26554
diff changeset
   155
  Scan.this_string "-->" >> ignored;
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   156
26551
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   157
val parse_processing_instruction =
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   158
  Scan.this_string "<?" --
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   159
  Scan.repeat (Scan.unless (Scan.this_string "?>") regular) --
26984
d0e098e206f3 added parse_document (optional unchecked header material);
wenzelm
parents: 26554
diff changeset
   160
  Scan.this_string "?>" >> ignored;
d0e098e206f3 added parse_document (optional unchecked header material);
wenzelm
parents: 26554
diff changeset
   161
d0e098e206f3 added parse_document (optional unchecked header material);
wenzelm
parents: 26554
diff changeset
   162
val parse_doctype =
d0e098e206f3 added parse_document (optional unchecked header material);
wenzelm
parents: 26554
diff changeset
   163
  Scan.this_string "<!DOCTYPE" --
d0e098e206f3 added parse_document (optional unchecked header material);
wenzelm
parents: 26554
diff changeset
   164
  Scan.repeat (Scan.unless ($$ ">") regular) --
d0e098e206f3 added parse_document (optional unchecked header material);
wenzelm
parents: 26554
diff changeset
   165
  $$ ">" >> ignored;
d0e098e206f3 added parse_document (optional unchecked header material);
wenzelm
parents: 26554
diff changeset
   166
d0e098e206f3 added parse_document (optional unchecked header material);
wenzelm
parents: 26554
diff changeset
   167
val parse_misc =
d0e098e206f3 added parse_document (optional unchecked header material);
wenzelm
parents: 26554
diff changeset
   168
  Scan.one Symbol.is_blank >> ignored ||
d0e098e206f3 added parse_document (optional unchecked header material);
wenzelm
parents: 26554
diff changeset
   169
  parse_processing_instruction ||
d0e098e206f3 added parse_document (optional unchecked header material);
wenzelm
parents: 26554
diff changeset
   170
  parse_comment;
26551
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   171
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   172
val parse_optional_text =
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   173
  Scan.optional (parse_chars >> (single o Text)) [];
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   174
26546
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
   175
in
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
   176
26984
d0e098e206f3 added parse_document (optional unchecked header material);
wenzelm
parents: 26554
diff changeset
   177
val parse_comments =
d0e098e206f3 added parse_document (optional unchecked header material);
wenzelm
parents: 26554
diff changeset
   178
  blanks -- Scan.repeat (parse_comment -- blanks >> K ()) >> K ();
d0e098e206f3 added parse_document (optional unchecked header material);
wenzelm
parents: 26554
diff changeset
   179
40627
becf5d5187cc renamed raw "explode" function to "raw_explode" to emphasize its meaning;
wenzelm
parents: 40131
diff changeset
   180
val parse_string = Scan.read Symbol.stopper parse_chars o raw_explode;
26546
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
   181
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   182
fun parse_content xs =
26551
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   183
  (parse_optional_text @@@
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   184
    (Scan.repeat
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   185
      ((parse_element >> single ||
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   186
        parse_cdata >> (single o Text) ||
26984
d0e098e206f3 added parse_document (optional unchecked header material);
wenzelm
parents: 26554
diff changeset
   187
        parse_processing_instruction ||
d0e098e206f3 added parse_document (optional unchecked header material);
wenzelm
parents: 26554
diff changeset
   188
        parse_comment)
26551
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   189
      @@@ parse_optional_text) >> flat)) xs
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   190
26546
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
   191
and parse_element xs =
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   192
  ($$ "<" |-- Symbol.scan_id --
26551
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   193
    Scan.repeat (blanks |-- parse_att) --| blanks :-- (fn (s, _) =>
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   194
      !! (err "Expected > or />")
26984
d0e098e206f3 added parse_document (optional unchecked header material);
wenzelm
parents: 26554
diff changeset
   195
        (Scan.this_string "/>" >> ignored
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   196
         || $$ ">" |-- parse_content --|
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   197
            !! (err ("Expected </" ^ s ^ ">"))
38228
ada3ab6b9085 simplified type XML.tree: embed Markup.T directly, avoid slightly odd triple;
wenzelm
parents: 31469
diff changeset
   198
              (Scan.this_string ("</" ^ s) --| blanks --| $$ ">"))) >> Elem) xs;
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   199
26984
d0e098e206f3 added parse_document (optional unchecked header material);
wenzelm
parents: 26554
diff changeset
   200
val parse_document =
d0e098e206f3 added parse_document (optional unchecked header material);
wenzelm
parents: 26554
diff changeset
   201
  (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
   202
  |-- parse_element;
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   203
26539
a0754be538ab added output_markup (from Tools/isabelle_process.ML);
wenzelm
parents: 26525
diff changeset
   204
fun parse s =
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   205
  (case Scan.finite Symbol.stopper (Scan.error (!! (err "Malformed element")
40627
becf5d5187cc renamed raw "explode" function to "raw_explode" to emphasize its meaning;
wenzelm
parents: 40131
diff changeset
   206
      (blanks |-- parse_document --| blanks))) (raw_explode s) of
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   207
    (x, []) => x
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   208
  | (_, ys) => error ("XML parsing error: Unprocessed input\n" ^ Symbol.beginning 100 ys));
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   209
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   210
end;
26546
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
   211
43767
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   212
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   213
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   214
(** XML as data representation language **)
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   215
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   216
exception XML_ATOM of string;
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   217
exception XML_BODY of tree list;
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   218
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   219
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   220
structure Encode =
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   221
struct
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   222
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   223
type 'a T = 'a -> body;
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   224
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   225
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   226
(* basic values *)
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   227
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   228
fun int_atom i = signed_string_of_int i;
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   229
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   230
fun bool_atom false = "0"
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   231
  | bool_atom true = "1";
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   232
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   233
fun unit_atom () = "";
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   234
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   235
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   236
(* structural nodes *)
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   237
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   238
fun node ts = Elem ((":", []), ts);
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   239
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   240
fun tagged (tag, ts) = Elem ((int_atom tag, []), ts);
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   241
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   242
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   243
(* representation of standard types *)
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   244
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   245
fun properties props = [Elem ((":", props), [])];
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   246
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   247
fun string "" = []
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   248
  | string s = [Text s];
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   249
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   250
val int = string o int_atom;
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   251
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   252
val bool = string o bool_atom;
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   253
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   254
val unit = string o unit_atom;
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   255
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   256
fun pair f g (x, y) = [node (f x), node (g y)];
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   257
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   258
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
   259
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   260
fun list f xs = map (node o f) xs;
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   261
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   262
fun option _ NONE = []
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   263
  | option f (SOME x) = [node (f x)];
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   264
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   265
fun variant fs x = [tagged (the (get_index (fn f => try f x) fs))];
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   266
26546
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
   267
end;
43767
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   268
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   269
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   270
structure Decode =
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   271
struct
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   272
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   273
type 'a T = body -> 'a;
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   274
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   275
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   276
(* basic values *)
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   277
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   278
fun int_atom s =
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   279
  (case Int.fromString s of
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   280
    SOME i => i
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   281
  | NONE => raise XML_ATOM s);
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   282
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   283
fun bool_atom "0" = false
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   284
  | bool_atom "1" = true
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   285
  | bool_atom s = raise XML_ATOM s;
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   286
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   287
fun unit_atom "" = ()
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   288
  | unit_atom s = raise XML_ATOM s;
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   289
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   290
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   291
(* structural nodes *)
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   292
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   293
fun node (Elem ((":", []), ts)) = ts
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   294
  | node t = raise XML_BODY [t];
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   295
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   296
fun tagged (Elem ((s, []), ts)) = (int_atom s, ts)
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   297
  | tagged t = raise XML_BODY [t];
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   298
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   299
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   300
(* representation of standard types *)
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   301
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   302
fun properties [Elem ((":", props), [])] = props
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   303
  | properties ts = raise XML_BODY ts;
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   304
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   305
fun string [] = ""
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   306
  | string [Text s] = s
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   307
  | string ts = raise XML_BODY ts;
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   308
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   309
val int = int_atom o string;
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   310
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   311
val bool = bool_atom o string;
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   312
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   313
val unit = unit_atom o string;
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   314
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   315
fun pair f g [t1, t2] = (f (node t1), g (node t2))
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   316
  | pair _ _ ts = raise XML_BODY ts;
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   317
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   318
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
   319
  | triple _ _ _ ts = raise XML_BODY ts;
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   320
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   321
fun list f ts = map (f o node) ts;
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   322
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   323
fun option _ [] = NONE
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   324
  | option f [t] = SOME (f (node t))
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   325
  | option _ ts = raise XML_BODY ts;
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   326
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   327
fun variant fs [t] = uncurry (nth fs) (tagged t)
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   328
  | variant _ ts = raise XML_BODY ts;
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   329
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   330
end;
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   331
e0219ef7f84c tuned XML modules;
wenzelm
parents: 40627
diff changeset
   332
end;