src/Pure/General/xml.ML
author wenzelm
Tue, 14 Aug 2007 13:20:47 +0200
changeset 24264 d6935e7dac8b
child 24584 01e83ffa6c54
permissions -rw-r--r--
moved Tools/xml.ML to General/xml.ML (again);
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24264
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/Tools/xml.ML
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*)
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    11
  val header: string
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    12
  val text: string -> string
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    13
  val text_charref: string -> string
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    14
  val cdata: string -> string
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    15
  type attributes = (string * string) list
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    16
  val attribute: string * string -> string
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    17
  val element: string -> attributes -> string list -> string
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    18
  (*tree functions*)
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    19
  datatype tree =
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    20
      Elem of string * attributes * tree list
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    21
    | Text of string
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    22
    | Output of output
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    23
  type content = tree list
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    24
  type element = string * attributes * content
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    25
  val string_of_tree: tree -> string
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    26
  val buffer_of_tree: tree -> Buffer.T
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    27
  val parse_string : string -> string option
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
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    31
  val tree_of_string: string -> tree
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    32
  val scan_comment_whspc : string list -> unit * string list
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    33
end;
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    34
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    35
structure XML: XML =
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    36
struct
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    37
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    38
(** string based representation (small scale) **)
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    39
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    40
val header = "<?xml version=\"1.0\"?>\n";
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    41
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
(* text and character data *)
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    44
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    45
fun decode "&lt;" = "<"
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    46
  | decode "&gt;" = ">"
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    47
  | decode "&amp;" = "&"
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    48
  | decode "&apos;" = "'"
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    49
  | decode "&quot;" = "\""
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    50
  | decode c = c;
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
fun encode "<" = "&lt;"
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    53
  | encode ">" = "&gt;"
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    54
  | encode "&" = "&amp;"
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    55
  | encode "'" = "&apos;"
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    56
  | encode "\"" = "&quot;"
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    57
  | encode c = c;
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    58
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    59
fun encode_charref c = "&#" ^ Int.toString (ord c) ^ ";"
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    60
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    61
val text = Library.translate_string encode;
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
val text_charref = translate_string encode_charref;
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    64
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    65
val cdata = enclose "<![CDATA[" "]]>\n";
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    66
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    67
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    68
(* elements *)
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    69
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    70
fun attribute (a, x) = a ^ " = \"" ^ text x ^ "\"";
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
fun element name atts cs =
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    73
  let val elem = space_implode " " (name :: map attribute atts) in
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    74
    if null cs then enclose "<" "/>" elem
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    75
    else enclose "<" ">" elem ^ implode cs ^ enclose "</" ">" name
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    76
  end;
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    77
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    78
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    79
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    80
(** explicit XML trees **)
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
type attributes = (string * string) list;
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
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    91
type element = string * attributes * content;
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    92
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    93
fun buffer_of_tree tree =
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    94
  let
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    95
    fun string_of (Elem (name, atts, ts)) buf =
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    96
        let val buf' =
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    97
          buf |> Buffer.add "<"
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    98
          |> fold Buffer.add (separate " " (name :: map attribute atts))
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
    99
        in
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   100
          if null ts then
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   101
            buf' |> Buffer.add "/>"
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   102
          else
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   103
            buf' |> Buffer.add ">"
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   104
            |> fold string_of ts
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   105
            |> Buffer.add "</" |> Buffer.add name |> Buffer.add ">"
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   106
        end
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   107
      | string_of (Text s) buf = Buffer.add (text s) buf
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   108
      | string_of (Output s) buf = Buffer.add s buf;
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   109
  in string_of tree Buffer.empty end;
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
val string_of_tree = Buffer.content o buffer_of_tree;
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
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
(** XML parsing **)
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   116
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   117
fun err s (xs, _) =
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   118
  "XML parsing error: " ^ s ^ "\nfound: " ^ quote (Symbol.beginning 100 xs);
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_whspc = Scan.many Symbol.is_blank;
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 scan_special = $$ "&" ^^ Symbol.scan_id ^^ $$ ";" >> decode;
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   123
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   124
val parse_chars = Scan.repeat1 (Scan.unless ((* scan_whspc -- *)$$ "<")
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   125
  (scan_special || Scan.one Symbol.is_regular)) >> implode;
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_string = Scan.read Symbol.stopper parse_chars o explode;
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   128
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   129
val parse_cdata = Scan.this_string "<![CDATA[" |--
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   130
  (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
   131
    implode) --| Scan.this_string "]]>";
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   132
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   133
val parse_att =
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   134
  Symbol.scan_id --| scan_whspc --| $$ "=" --| scan_whspc --
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   135
  (($$ "\"" || $$ "'") :|-- (fn s => (Scan.repeat (Scan.unless ($$ s)
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   136
    (scan_special || Scan.one Symbol.is_regular)) >> implode) --| $$ s));
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   137
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   138
val parse_comment = Scan.this_string "<!--" --
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   139
  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
   140
  Scan.this_string "-->";
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   141
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   142
val scan_comment_whspc =
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   143
  (scan_whspc >> K()) --| (Scan.repeat (parse_comment |-- (scan_whspc >> K())));
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   144
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   145
val parse_pi = Scan.this_string "<?" |--
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   146
  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
   147
  Scan.this_string "?>";
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   148
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   149
fun parse_content xs =
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   150
  ((Scan.optional ((* scan_whspc |-- *) parse_chars >> (single o Text)) [] --
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   151
    (Scan.repeat ((* scan_whspc |-- *)
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   152
       (   parse_elem >> single
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   153
        || parse_cdata >> (single o Text)
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   154
        || parse_pi >> K []
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   155
        || parse_comment >> K []) --
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   156
       Scan.optional ((* scan_whspc |-- *) parse_chars >> (single o Text)) []
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   157
         >> op @) >> flat) >> op @)(* --| scan_whspc*)) xs
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   158
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   159
and parse_elem xs =
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   160
  ($$ "<" |-- Symbol.scan_id --
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   161
    Scan.repeat (scan_whspc |-- parse_att) --| scan_whspc :-- (fn (s, _) =>
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   162
      !! (err "Expected > or />")
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   163
        (Scan.this_string "/>" >> K []
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   164
         || $$ ">" |-- parse_content --|
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   165
            !! (err ("Expected </" ^ s ^ ">"))
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   166
              (Scan.this_string ("</" ^ s) --| scan_whspc --| $$ ">"))) >>
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   167
    (fn ((s, atts), ts) => Elem (s, atts, ts))) xs;
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   168
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   169
val parse_document =
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   170
  Scan.option (Scan.this_string "<!DOCTYPE" -- scan_whspc |--
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   171
    (Scan.repeat (Scan.unless ($$ ">")
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   172
      (Scan.one Symbol.is_regular)) >> implode) --| $$ ">" --| scan_whspc) --
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   173
  parse_elem;
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   174
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   175
fun tree_of_string s =
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   176
  (case Scan.finite Symbol.stopper (Scan.error (!! (err "Malformed element")
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   177
      (scan_whspc |-- parse_elem --| scan_whspc))) (Symbol.explode s) of
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   178
    (x, []) => x
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   179
  | (_, 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
   180
d6935e7dac8b moved Tools/xml.ML to General/xml.ML (again);
wenzelm
parents:
diff changeset
   181
end;