src/Pure/General/xml.ML
author wenzelm
Thu, 03 Apr 2008 18:42:37 +0200
changeset 26539 a0754be538ab
parent 26525 14a56f013469
child 26546 ba4cdf92c7c4
permissions -rw-r--r--
added output_markup (from Tools/isabelle_process.ML); major cleanup of signature;
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
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    10
  (*string functions*)
26525
14a56f013469 added detect;
wenzelm
parents: 25838
diff changeset
    11
  val detect: string -> bool
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    12
  val header: string
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    13
  val text: string -> string
26539
a0754be538ab added output_markup (from Tools/isabelle_process.ML);
wenzelm
parents: 26525
diff changeset
    14
  type attributes = Markup.property list
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    15
  val element: string -> attributes -> string list -> string
26539
a0754be538ab added output_markup (from Tools/isabelle_process.ML);
wenzelm
parents: 26525
diff changeset
    16
  val output_markup: Markup.T -> output * output
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    17
  (*tree functions*)
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    18
  datatype tree =
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    19
      Elem of string * attributes * tree list
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    20
    | Text of string
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    21
    | Output of output
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    22
  type content = tree list
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    23
  type element = string * attributes * content
26539
a0754be538ab added output_markup (from Tools/isabelle_process.ML);
wenzelm
parents: 26525
diff changeset
    24
  val string_of: tree -> string
25838
00b2a1b2c4e9 removed unused text_charref, cdata;
wenzelm
parents: 24584
diff changeset
    25
  val plain_content: tree -> string
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    26
  val parse_string : string -> string option
26539
a0754be538ab added output_markup (from Tools/isabelle_process.ML);
wenzelm
parents: 26525
diff changeset
    27
  val parse_comment_whspc: string list -> unit * string list
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    28
  val parse_content: string list -> tree list * string list
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    29
  val parse_elem: string list -> tree * string list
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    30
  val parse_document: string list -> (string option * tree) * string list
26539
a0754be538ab added output_markup (from Tools/isabelle_process.ML);
wenzelm
parents: 26525
diff changeset
    31
  val parse: string -> tree
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    32
end;
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    33
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    34
structure XML: XML =
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    35
struct
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    36
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    37
26525
14a56f013469 added detect;
wenzelm
parents: 25838
diff changeset
    38
(** string representation **)
14a56f013469 added detect;
wenzelm
parents: 25838
diff changeset
    39
14a56f013469 added detect;
wenzelm
parents: 25838
diff changeset
    40
val detect = String.isPrefix "<?xml";
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    41
val header = "<?xml version=\"1.0\"?>\n";
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    42
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    43
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    44
(* text and character data *)
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    45
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    46
fun decode "&lt;" = "<"
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    47
  | decode "&gt;" = ">"
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    48
  | decode "&amp;" = "&"
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    49
  | decode "&apos;" = "'"
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    50
  | decode "&quot;" = "\""
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    51
  | decode c = c;
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    52
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    53
fun encode "<" = "&lt;"
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    54
  | encode ">" = "&gt;"
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    55
  | encode "&" = "&amp;"
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    56
  | encode "'" = "&apos;"
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    57
  | encode "\"" = "&quot;"
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    58
  | encode c = c;
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    59
25838
00b2a1b2c4e9 removed unused text_charref, cdata;
wenzelm
parents: 24584
diff changeset
    60
val text = translate_string encode;
24264
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
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    63
(* elements *)
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    64
26539
a0754be538ab added output_markup (from Tools/isabelle_process.ML);
wenzelm
parents: 26525
diff changeset
    65
type attributes = Markup.property list;
a0754be538ab added output_markup (from Tools/isabelle_process.ML);
wenzelm
parents: 26525
diff changeset
    66
a0754be538ab added output_markup (from Tools/isabelle_process.ML);
wenzelm
parents: 26525
diff changeset
    67
fun elem name atts =
a0754be538ab added output_markup (from Tools/isabelle_process.ML);
wenzelm
parents: 26525
diff changeset
    68
  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
    69
26525
14a56f013469 added detect;
wenzelm
parents: 25838
diff changeset
    70
fun element name atts body =
26539
a0754be538ab added output_markup (from Tools/isabelle_process.ML);
wenzelm
parents: 26525
diff changeset
    71
  let val b = implode body in
a0754be538ab added output_markup (from Tools/isabelle_process.ML);
wenzelm
parents: 26525
diff changeset
    72
    if b = "" then enclose "<" "/>" (elem name atts)
a0754be538ab added output_markup (from Tools/isabelle_process.ML);
wenzelm
parents: 26525
diff changeset
    73
    else enclose "<" ">" (elem name atts) ^ b ^ enclose "</" ">" name
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    74
  end;
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    75
26539
a0754be538ab added output_markup (from Tools/isabelle_process.ML);
wenzelm
parents: 26525
diff changeset
    76
fun output_markup (name, atts) =
a0754be538ab added output_markup (from Tools/isabelle_process.ML);
wenzelm
parents: 26525
diff changeset
    77
 (enclose "<" ">" (elem name atts),
a0754be538ab added output_markup (from Tools/isabelle_process.ML);
wenzelm
parents: 26525
diff changeset
    78
  enclose "</" ">" name);
a0754be538ab added output_markup (from Tools/isabelle_process.ML);
wenzelm
parents: 26525
diff changeset
    79
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    80
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
(** explicit XML trees **)
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    83
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    84
datatype tree =
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    85
    Elem of string * attributes * tree list
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    86
  | Text of string
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    87
  | Output of output;
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    88
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    89
type content = tree list;
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    90
type element = string * attributes * content;
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    91
26539
a0754be538ab added output_markup (from Tools/isabelle_process.ML);
wenzelm
parents: 26525
diff changeset
    92
fun string_of t =
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    93
  let
26525
14a56f013469 added detect;
wenzelm
parents: 25838
diff changeset
    94
    fun tree (Elem (name, atts, [])) =
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 "/>"
26525
14a56f013469 added detect;
wenzelm
parents: 25838
diff changeset
    96
      | tree (Elem (name, atts, ts)) =
26539
a0754be538ab added output_markup (from Tools/isabelle_process.ML);
wenzelm
parents: 26525
diff changeset
    97
          Buffer.add "<" #> Buffer.add (elem name atts) #> Buffer.add ">" #>
26525
14a56f013469 added detect;
wenzelm
parents: 25838
diff changeset
    98
          fold tree ts #>
14a56f013469 added detect;
wenzelm
parents: 25838
diff changeset
    99
          Buffer.add "</" #> Buffer.add name #> Buffer.add ">"
14a56f013469 added detect;
wenzelm
parents: 25838
diff changeset
   100
      | tree (Text s) = Buffer.add (text s)
14a56f013469 added detect;
wenzelm
parents: 25838
diff changeset
   101
      | tree (Output s) = Buffer.add s;
14a56f013469 added detect;
wenzelm
parents: 25838
diff changeset
   102
  in Buffer.empty |> tree t |> Buffer.content end;
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   103
25838
00b2a1b2c4e9 removed unused text_charref, cdata;
wenzelm
parents: 24584
diff changeset
   104
fun plain_content tree =
00b2a1b2c4e9 removed unused text_charref, cdata;
wenzelm
parents: 24584
diff changeset
   105
  let
00b2a1b2c4e9 removed unused text_charref, cdata;
wenzelm
parents: 24584
diff changeset
   106
    fun content (Elem (_, _, ts)) = fold content ts
00b2a1b2c4e9 removed unused text_charref, cdata;
wenzelm
parents: 24584
diff changeset
   107
      | content (Text s) = Buffer.add s
00b2a1b2c4e9 removed unused text_charref, cdata;
wenzelm
parents: 24584
diff changeset
   108
      | content (Output _) = I;    (* FIXME !? *)
00b2a1b2c4e9 removed unused text_charref, cdata;
wenzelm
parents: 24584
diff changeset
   109
  in Buffer.empty |> content tree |> Buffer.content end;
00b2a1b2c4e9 removed unused text_charref, cdata;
wenzelm
parents: 24584
diff changeset
   110
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   111
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   112
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   113
(** XML parsing **)
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   114
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   115
fun err s (xs, _) =
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   116
  "XML parsing error: " ^ s ^ "\nfound: " ^ quote (Symbol.beginning 100 xs);
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   117
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   118
val scan_whspc = Scan.many Symbol.is_blank;
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   119
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   120
val scan_special = $$ "&" ^^ Symbol.scan_id ^^ $$ ";" >> decode;
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   121
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   122
val parse_chars = Scan.repeat1 (Scan.unless ((* scan_whspc -- *)$$ "<")
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   123
  (scan_special || Scan.one Symbol.is_regular)) >> implode;
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
val parse_string = Scan.read Symbol.stopper parse_chars o explode;
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   126
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   127
val parse_cdata = Scan.this_string "<![CDATA[" |--
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   128
  (Scan.repeat (Scan.unless (Scan.this_string "]]>") (Scan.one Symbol.is_regular)) >>
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   129
    implode) --| Scan.this_string "]]>";
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   130
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   131
val parse_att =
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   132
  Symbol.scan_id --| scan_whspc --| $$ "=" --| scan_whspc --
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   133
  (($$ "\"" || $$ "'") :|-- (fn s => (Scan.repeat (Scan.unless ($$ s)
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   134
    (scan_special || Scan.one Symbol.is_regular)) >> implode) --| $$ s));
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   135
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   136
val parse_comment = Scan.this_string "<!--" --
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   137
  Scan.repeat (Scan.unless (Scan.this_string "-->") (Scan.one Symbol.is_regular)) --
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   138
  Scan.this_string "-->";
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   139
26539
a0754be538ab added output_markup (from Tools/isabelle_process.ML);
wenzelm
parents: 26525
diff changeset
   140
val parse_comment_whspc =
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   141
  (scan_whspc >> K()) --| (Scan.repeat (parse_comment |-- (scan_whspc >> K())));
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   142
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   143
val parse_pi = Scan.this_string "<?" |--
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   144
  Scan.repeat (Scan.unless (Scan.this_string "?>") (Scan.one Symbol.is_regular)) --|
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   145
  Scan.this_string "?>";
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
fun parse_content xs =
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   148
  ((Scan.optional ((* scan_whspc |-- *) parse_chars >> (single o Text)) [] --
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   149
    (Scan.repeat ((* scan_whspc |-- *)
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   150
       (   parse_elem >> single
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   151
        || parse_cdata >> (single o Text)
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   152
        || parse_pi >> K []
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   153
        || parse_comment >> K []) --
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   154
       Scan.optional ((* scan_whspc |-- *) parse_chars >> (single o Text)) []
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   155
         >> op @) >> flat) >> op @)(* --| scan_whspc*)) xs
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   156
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   157
and parse_elem xs =
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   158
  ($$ "<" |-- Symbol.scan_id --
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   159
    Scan.repeat (scan_whspc |-- parse_att) --| scan_whspc :-- (fn (s, _) =>
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 ^ ">"))
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   164
              (Scan.this_string ("</" ^ s) --| scan_whspc --| $$ ">"))) >>
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
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   167
val parse_document =
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   168
  Scan.option (Scan.this_string "<!DOCTYPE" -- scan_whspc |--
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   169
    (Scan.repeat (Scan.unless ($$ ">")
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   170
      (Scan.one Symbol.is_regular)) >> implode) --| $$ ">" --| scan_whspc) --
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   171
  parse_elem;
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   172
26539
a0754be538ab added output_markup (from Tools/isabelle_process.ML);
wenzelm
parents: 26525
diff changeset
   173
fun parse s =
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   174
  (case Scan.finite Symbol.stopper (Scan.error (!! (err "Malformed element")
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   175
      (scan_whspc |-- parse_elem --| scan_whspc))) (Symbol.explode s) of
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   176
    (x, []) => x
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   177
  | (_, 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
   178
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   179
end;