src/Pure/General/xml.ML
author wenzelm
Fri, 23 May 2008 21:18:47 +0200
changeset 26977 e736139b553d
parent 26554 5ee45391576e
child 26984 d0e098e206f3
permissions -rw-r--r--
added theory_nameN;
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
    ID:         $Id$
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
     3
    Author:     David Aspinall, Stefan Berghofer and Markus Wenzel
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
     4
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
     5
Basic support for XML.
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
     6
*)
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
signature XML =
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
     9
sig
26539
a0754be538ab added output_markup (from Tools/isabelle_process.ML);
wenzelm
parents: 26525
diff changeset
    10
  type attributes = Markup.property list
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    11
  datatype tree =
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    12
      Elem of string * attributes * tree list
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    13
    | Text of string
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    14
    | Output of output
26546
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
    15
  val add_content: tree -> Buffer.T -> Buffer.T
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
    16
  val detect: string -> bool
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
    17
  val header: string
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
    18
  val text: string -> string
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
    19
  val element: string -> attributes -> string list -> string
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
    20
  val output_markup: Markup.T -> output * output
26539
a0754be538ab added output_markup (from Tools/isabelle_process.ML);
wenzelm
parents: 26525
diff changeset
    21
  val string_of: tree -> string
26546
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
    22
  val output: tree -> TextIO.outstream -> unit
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    23
  val parse_string : string -> string option
26551
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
    24
  val parse_comments: string list -> unit * string list
26546
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
    25
  val parse_element: string list -> tree * string list
26539
a0754be538ab added output_markup (from Tools/isabelle_process.ML);
wenzelm
parents: 26525
diff changeset
    26
  val parse: string -> tree
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    27
end;
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    28
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    29
structure XML: XML =
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    30
struct
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    31
26546
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
    32
(** XML trees **)
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
    33
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
    34
type attributes = Markup.property list;
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
    35
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
    36
datatype tree =
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
    37
    Elem of string * attributes * tree list
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
    38
  | Text of string
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
    39
  | Output of output;   (* FIXME !? *)
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
    40
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
    41
fun add_content (Elem (_, _, ts)) = fold add_content ts
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
    42
  | add_content (Text s) = Buffer.add s
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
    43
  | add_content (Output _) = I;    (* FIXME !? *)
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
    44
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
    45
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    46
26525
14a56f013469 added detect;
wenzelm
parents: 25838
diff changeset
    47
(** string representation **)
14a56f013469 added detect;
wenzelm
parents: 25838
diff changeset
    48
14a56f013469 added detect;
wenzelm
parents: 25838
diff changeset
    49
val detect = String.isPrefix "<?xml";
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    50
val header = "<?xml version=\"1.0\"?>\n";
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    51
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    52
26546
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
    53
(* escaped text *)
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    54
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    55
fun decode "&lt;" = "<"
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    56
  | decode "&gt;" = ">"
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    57
  | decode "&amp;" = "&"
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    58
  | decode "&apos;" = "'"
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    59
  | decode "&quot;" = "\""
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    60
  | decode c = c;
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    61
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    62
fun encode "<" = "&lt;"
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    63
  | encode ">" = "&gt;"
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    64
  | encode "&" = "&amp;"
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    65
  | encode "'" = "&apos;"
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    66
  | encode "\"" = "&quot;"
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    67
  | encode c = c;
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    68
25838
00b2a1b2c4e9 removed unused text_charref, cdata;
wenzelm
parents: 24584
diff changeset
    69
val text = translate_string encode;
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    70
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
(* elements *)
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    73
26539
a0754be538ab added output_markup (from Tools/isabelle_process.ML);
wenzelm
parents: 26525
diff changeset
    74
fun elem name atts =
26551
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
    75
  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
    76
26525
14a56f013469 added detect;
wenzelm
parents: 25838
diff changeset
    77
fun element name atts body =
26539
a0754be538ab added output_markup (from Tools/isabelle_process.ML);
wenzelm
parents: 26525
diff changeset
    78
  let val b = implode body in
a0754be538ab added output_markup (from Tools/isabelle_process.ML);
wenzelm
parents: 26525
diff changeset
    79
    if b = "" then enclose "<" "/>" (elem name atts)
a0754be538ab added output_markup (from Tools/isabelle_process.ML);
wenzelm
parents: 26525
diff changeset
    80
    else enclose "<" ">" (elem name atts) ^ b ^ enclose "</" ">" name
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    81
  end;
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    82
26539
a0754be538ab added output_markup (from Tools/isabelle_process.ML);
wenzelm
parents: 26525
diff changeset
    83
fun output_markup (name, atts) =
a0754be538ab added output_markup (from Tools/isabelle_process.ML);
wenzelm
parents: 26525
diff changeset
    84
 (enclose "<" ">" (elem name atts),
a0754be538ab added output_markup (from Tools/isabelle_process.ML);
wenzelm
parents: 26525
diff changeset
    85
  enclose "</" ">" name);
a0754be538ab added output_markup (from Tools/isabelle_process.ML);
wenzelm
parents: 26525
diff changeset
    86
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    87
26546
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
    88
(* output *)
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    89
26546
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
    90
fun buffer_of tree =
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    91
  let
26546
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
    92
    fun traverse (Elem (name, atts, [])) =
26539
a0754be538ab added output_markup (from Tools/isabelle_process.ML);
wenzelm
parents: 26525
diff changeset
    93
          Buffer.add "<" #> Buffer.add (elem name atts) #> Buffer.add "/>"
26546
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
    94
      | traverse (Elem (name, atts, ts)) =
26539
a0754be538ab added output_markup (from Tools/isabelle_process.ML);
wenzelm
parents: 26525
diff changeset
    95
          Buffer.add "<" #> Buffer.add (elem name atts) #> Buffer.add ">" #>
26546
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
    96
          fold traverse ts #>
26525
14a56f013469 added detect;
wenzelm
parents: 25838
diff changeset
    97
          Buffer.add "</" #> Buffer.add name #> Buffer.add ">"
26546
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
    98
      | traverse (Text s) = Buffer.add (text s)
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
    99
      | traverse (Output s) = Buffer.add s;
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
   100
  in Buffer.empty |> traverse tree end;
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   101
26546
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
   102
val string_of = Buffer.content o buffer_of;
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
   103
val output = Buffer.output o buffer_of;
25838
00b2a1b2c4e9 removed unused text_charref, cdata;
wenzelm
parents: 24584
diff changeset
   104
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   105
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   106
26546
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
   107
(** XML parsing (slow) **)
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
   108
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
   109
local
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   110
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   111
fun err s (xs, _) =
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   112
  "XML parsing error: " ^ s ^ "\nfound: " ^ quote (Symbol.beginning 100 xs);
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   113
26551
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   114
val blanks = Scan.many Symbol.is_blank;
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   115
val special = $$ "&" ^^ Symbol.scan_id ^^ $$ ";" >> decode;
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   116
val regular = Scan.one Symbol.is_regular;
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   117
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
   118
26551
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   119
val parse_chars = Scan.repeat1 (special || regular_except "<") >> implode;
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   120
26551
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   121
val parse_cdata =
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   122
  Scan.this_string "<![CDATA[" |--
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   123
  (Scan.repeat (Scan.unless (Scan.this_string "]]>") regular) >> implode) --|
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   124
  Scan.this_string "]]>";
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   125
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   126
val parse_att =
26551
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   127
  (Symbol.scan_id --| (blanks -- $$ "=" -- blanks)) --
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   128
  (($$ "\"" || $$ "'") :|-- (fn s =>
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   129
    (Scan.repeat (special || regular_except s) >> implode) --| $$ s));
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   130
26551
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   131
val parse_comment =
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   132
  Scan.this_string "<!--" --
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   133
  Scan.repeat (Scan.unless (Scan.this_string "-->") regular) --
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   134
  Scan.this_string "-->" >> K [];
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   135
26551
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   136
val parse_processing_instruction =
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   137
  Scan.this_string "<?" --
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   138
  Scan.repeat (Scan.unless (Scan.this_string "?>") regular) --
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   139
  Scan.this_string "?>" >> K [];
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   140
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   141
val parse_optional_text =
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   142
  Scan.optional (parse_chars >> (single o Text)) [];
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   143
26546
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
   144
in
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
   145
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
   146
val parse_string = Scan.read Symbol.stopper parse_chars o explode;
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
   147
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   148
fun parse_content xs =
26551
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   149
  (parse_optional_text @@@
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   150
    (Scan.repeat
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   151
      ((parse_element >> single ||
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   152
        parse_cdata >> (single o Text) ||
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   153
        parse_processing_instruction >> K [] ||
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   154
        parse_comment >> K [])
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   155
      @@@ parse_optional_text) >> flat)) xs
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   156
26546
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
   157
and parse_element xs =
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   158
  ($$ "<" |-- Symbol.scan_id --
26551
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   159
    Scan.repeat (blanks |-- parse_att) --| blanks :-- (fn (s, _) =>
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   160
      !! (err "Expected > or />")
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   161
        (Scan.this_string "/>" >> K []
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   162
         || $$ ">" |-- parse_content --|
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   163
            !! (err ("Expected </" ^ s ^ ">"))
26551
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   164
              (Scan.this_string ("</" ^ s) --| blanks --| $$ ">"))) >>
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   165
    (fn ((s, atts), ts) => Elem (s, atts, ts))) xs;
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   166
26551
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   167
val parse_comments =
da1cd11d8a25 renamed parse_comment_whspc to parse_comments;
wenzelm
parents: 26546
diff changeset
   168
  blanks -- Scan.repeat (parse_comment -- blanks >> K ()) >> K ();
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   169
26539
a0754be538ab added output_markup (from Tools/isabelle_process.ML);
wenzelm
parents: 26525
diff changeset
   170
fun parse s =
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   171
  (case Scan.finite Symbol.stopper (Scan.error (!! (err "Malformed element")
26554
5ee45391576e parser: use plain explode, not Symbol.explode!
wenzelm
parents: 26551
diff changeset
   172
      (blanks |-- parse_element --| blanks))) (explode s) of
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   173
    (x, []) => x
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   174
  | (_, 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
   175
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   176
end;
26546
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
   177
ba4cdf92c7c4 further cleanup of XML signature;
wenzelm
parents: 26539
diff changeset
   178
end;