src/Pure/General/xml.ML
author wenzelm
Wed, 09 Jun 2004 18:56:38 +0200
changeset 14910 f145696d4bb5
parent 14865 8b9a372b3e90
child 14928 b8c1783c9101
permissions -rw-r--r--
Scan.this_string;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12416
9b3e7a35da30 Basic support for XML output.
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/General/xml.ML
9b3e7a35da30 Basic support for XML output.
wenzelm
parents:
diff changeset
     2
    ID:         $Id$
14728
wenzelm
parents: 14714
diff changeset
     3
    Author:     David Aspinall, Stefan Berghofer and Markus Wenzel
12416
9b3e7a35da30 Basic support for XML output.
wenzelm
parents:
diff changeset
     4
    License:    GPL (GNU GENERAL PUBLIC LICENSE)
9b3e7a35da30 Basic support for XML output.
wenzelm
parents:
diff changeset
     5
13729
1a8dda49fd86 Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents: 12416
diff changeset
     6
Basic support for XML input and output.
12416
9b3e7a35da30 Basic support for XML output.
wenzelm
parents:
diff changeset
     7
*)
9b3e7a35da30 Basic support for XML output.
wenzelm
parents:
diff changeset
     8
9b3e7a35da30 Basic support for XML output.
wenzelm
parents:
diff changeset
     9
signature XML =
9b3e7a35da30 Basic support for XML output.
wenzelm
parents:
diff changeset
    10
sig
13729
1a8dda49fd86 Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents: 12416
diff changeset
    11
  datatype tree =
1a8dda49fd86 Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents: 12416
diff changeset
    12
      Elem of string * (string * string) list * tree list
1a8dda49fd86 Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents: 12416
diff changeset
    13
    | Text of string
12416
9b3e7a35da30 Basic support for XML output.
wenzelm
parents:
diff changeset
    14
  val element: string -> (string * string) list -> string list -> string
9b3e7a35da30 Basic support for XML output.
wenzelm
parents:
diff changeset
    15
  val text: string -> string
14713
6d203f6f0e8d Add cdata output. Add tabs in whitespace. Write two strings instead of Library.quote.
aspinall
parents: 14596
diff changeset
    16
  val cdata: string -> string
12416
9b3e7a35da30 Basic support for XML output.
wenzelm
parents:
diff changeset
    17
  val header: string
13729
1a8dda49fd86 Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents: 12416
diff changeset
    18
  val string_of_tree: tree -> string
1a8dda49fd86 Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents: 12416
diff changeset
    19
  val tree_of_string: string -> tree
14185
9b3841638c06 Tried to make parser a bit more standard-conforming.
berghofe
parents: 13729
diff changeset
    20
  val parse_content: string list -> tree list * string list
9b3841638c06 Tried to make parser a bit more standard-conforming.
berghofe
parents: 13729
diff changeset
    21
  val parse_elem: string list -> tree * string list
9b3841638c06 Tried to make parser a bit more standard-conforming.
berghofe
parents: 13729
diff changeset
    22
  val parse_document: string list -> (string option * tree) * string list
12416
9b3e7a35da30 Basic support for XML output.
wenzelm
parents:
diff changeset
    23
end;
9b3e7a35da30 Basic support for XML output.
wenzelm
parents:
diff changeset
    24
9b3e7a35da30 Basic support for XML output.
wenzelm
parents:
diff changeset
    25
structure XML: XML =
9b3e7a35da30 Basic support for XML output.
wenzelm
parents:
diff changeset
    26
struct
9b3e7a35da30 Basic support for XML output.
wenzelm
parents:
diff changeset
    27
9b3e7a35da30 Basic support for XML output.
wenzelm
parents:
diff changeset
    28
(* character data *)
9b3e7a35da30 Basic support for XML output.
wenzelm
parents:
diff changeset
    29
9b3e7a35da30 Basic support for XML output.
wenzelm
parents:
diff changeset
    30
fun encode "<" = "&lt;"
9b3e7a35da30 Basic support for XML output.
wenzelm
parents:
diff changeset
    31
  | encode ">" = "&gt;"
9b3e7a35da30 Basic support for XML output.
wenzelm
parents:
diff changeset
    32
  | encode "&" = "&amp;"
9b3e7a35da30 Basic support for XML output.
wenzelm
parents:
diff changeset
    33
  | encode "'" = "&apos;"
9b3e7a35da30 Basic support for XML output.
wenzelm
parents:
diff changeset
    34
  | encode "\"" = "&quot;"
9b3e7a35da30 Basic support for XML output.
wenzelm
parents:
diff changeset
    35
  | encode c = c;
9b3e7a35da30 Basic support for XML output.
wenzelm
parents:
diff changeset
    36
13729
1a8dda49fd86 Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents: 12416
diff changeset
    37
fun decode "&lt;" = "<"
1a8dda49fd86 Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents: 12416
diff changeset
    38
  | decode "&gt;" = ">"
1a8dda49fd86 Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents: 12416
diff changeset
    39
  | decode "&amp;" = "&"
1a8dda49fd86 Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents: 12416
diff changeset
    40
  | decode "&apos;" = "'"
1a8dda49fd86 Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents: 12416
diff changeset
    41
  | decode "&quot;" = "\""
1a8dda49fd86 Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents: 12416
diff changeset
    42
  | decode c = c;
1a8dda49fd86 Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents: 12416
diff changeset
    43
14596
c36e116b578b - tuned text function
berghofe
parents: 14185
diff changeset
    44
val text = String.translate (encode o String.str);
12416
9b3e7a35da30 Basic support for XML output.
wenzelm
parents:
diff changeset
    45
14713
6d203f6f0e8d Add cdata output. Add tabs in whitespace. Write two strings instead of Library.quote.
aspinall
parents: 14596
diff changeset
    46
val cdata_open  = "<![CDATA["
6d203f6f0e8d Add cdata output. Add tabs in whitespace. Write two strings instead of Library.quote.
aspinall
parents: 14596
diff changeset
    47
val cdata_close = "]]>"
6d203f6f0e8d Add cdata output. Add tabs in whitespace. Write two strings instead of Library.quote.
aspinall
parents: 14596
diff changeset
    48
6d203f6f0e8d Add cdata output. Add tabs in whitespace. Write two strings instead of Library.quote.
aspinall
parents: 14596
diff changeset
    49
fun cdata s = cdata_open ^ s ^ cdata_close;
12416
9b3e7a35da30 Basic support for XML output.
wenzelm
parents:
diff changeset
    50
14728
wenzelm
parents: 14714
diff changeset
    51
12416
9b3e7a35da30 Basic support for XML output.
wenzelm
parents:
diff changeset
    52
(* elements *)
9b3e7a35da30 Basic support for XML output.
wenzelm
parents:
diff changeset
    53
13729
1a8dda49fd86 Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents: 12416
diff changeset
    54
datatype tree =
1a8dda49fd86 Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents: 12416
diff changeset
    55
    Elem of string * (string * string) list * tree list
1a8dda49fd86 Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents: 12416
diff changeset
    56
  | Text of string;
1a8dda49fd86 Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents: 12416
diff changeset
    57
14728
wenzelm
parents: 14714
diff changeset
    58
fun attribute (a, x) = a ^ " = \"" ^ text x ^ "\"";
12416
9b3e7a35da30 Basic support for XML output.
wenzelm
parents:
diff changeset
    59
9b3e7a35da30 Basic support for XML output.
wenzelm
parents:
diff changeset
    60
fun element name atts cs =
9b3e7a35da30 Basic support for XML output.
wenzelm
parents:
diff changeset
    61
  let val elem = space_implode " " (name :: map attribute atts) in
9b3e7a35da30 Basic support for XML output.
wenzelm
parents:
diff changeset
    62
    if null cs then enclose "<" "/>" elem
9b3e7a35da30 Basic support for XML output.
wenzelm
parents:
diff changeset
    63
    else enclose "<" ">" elem ^ implode cs ^ enclose "</" ">" name
9b3e7a35da30 Basic support for XML output.
wenzelm
parents:
diff changeset
    64
  end;
9b3e7a35da30 Basic support for XML output.
wenzelm
parents:
diff changeset
    65
13729
1a8dda49fd86 Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents: 12416
diff changeset
    66
fun string_of_tree (Elem (name, atts, ts)) =
1a8dda49fd86 Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents: 12416
diff changeset
    67
      element name atts (map string_of_tree ts)
14728
wenzelm
parents: 14714
diff changeset
    68
  | string_of_tree (Text s) = s;
wenzelm
parents: 14714
diff changeset
    69
12416
9b3e7a35da30 Basic support for XML output.
wenzelm
parents:
diff changeset
    70
val header = "<?xml version=\"1.0\"?>\n";
9b3e7a35da30 Basic support for XML output.
wenzelm
parents:
diff changeset
    71
13729
1a8dda49fd86 Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents: 12416
diff changeset
    72
1a8dda49fd86 Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents: 12416
diff changeset
    73
(* parser *)
1a8dda49fd86 Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents: 12416
diff changeset
    74
14728
wenzelm
parents: 14714
diff changeset
    75
fun err s (xs, _) =
wenzelm
parents: 14714
diff changeset
    76
  "XML parsing error: " ^ s ^ "\nfound: " ^ quote (Symbol.beginning 100 xs);
13729
1a8dda49fd86 Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents: 12416
diff changeset
    77
14728
wenzelm
parents: 14714
diff changeset
    78
val scan_whspc = Scan.any Symbol.is_blank;
13729
1a8dda49fd86 Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents: 12416
diff changeset
    79
1a8dda49fd86 Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents: 12416
diff changeset
    80
val scan_special = $$ "&" ^^ Symbol.scan_id ^^ $$ ";" >> decode;
1a8dda49fd86 Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents: 12416
diff changeset
    81
14185
9b3841638c06 Tried to make parser a bit more standard-conforming.
berghofe
parents: 13729
diff changeset
    82
val parse_chars = Scan.repeat1 (Scan.unless (scan_whspc -- $$ "<")
9b3841638c06 Tried to make parser a bit more standard-conforming.
berghofe
parents: 13729
diff changeset
    83
  (scan_special || Scan.one Symbol.not_eof)) >> implode;
9b3841638c06 Tried to make parser a bit more standard-conforming.
berghofe
parents: 13729
diff changeset
    84
14910
f145696d4bb5 Scan.this_string;
wenzelm
parents: 14865
diff changeset
    85
val parse_cdata = Scan.this_string "<![CDATA[" |--
f145696d4bb5 Scan.this_string;
wenzelm
parents: 14865
diff changeset
    86
  (Scan.repeat (Scan.unless (Scan.this_string "]]>") (Scan.one Symbol.not_eof)) >>
f145696d4bb5 Scan.this_string;
wenzelm
parents: 14865
diff changeset
    87
    implode) --| Scan.this_string "]]>";
14185
9b3841638c06 Tried to make parser a bit more standard-conforming.
berghofe
parents: 13729
diff changeset
    88
9b3841638c06 Tried to make parser a bit more standard-conforming.
berghofe
parents: 13729
diff changeset
    89
val parse_att =
14865
8b9a372b3e90 Tuned parse_att.
berghofe
parents: 14863
diff changeset
    90
  Symbol.scan_id --| scan_whspc --| $$ "=" --| scan_whspc --
8b9a372b3e90 Tuned parse_att.
berghofe
parents: 14863
diff changeset
    91
  (($$ "\"" || $$ "'") :-- (fn s => (Scan.repeat (Scan.unless ($$ s)
8b9a372b3e90 Tuned parse_att.
berghofe
parents: 14863
diff changeset
    92
    (scan_special || Scan.one Symbol.not_eof)) >> implode) --| $$ s) >> snd);
14863
49afb368f4be Add alternative syntax for attributes
aspinall
parents: 14844
diff changeset
    93
14910
f145696d4bb5 Scan.this_string;
wenzelm
parents: 14865
diff changeset
    94
val parse_comment = Scan.this_string "<!--" --
f145696d4bb5 Scan.this_string;
wenzelm
parents: 14865
diff changeset
    95
  Scan.repeat (Scan.unless (Scan.this_string "-->") (Scan.one Symbol.not_eof)) --
f145696d4bb5 Scan.this_string;
wenzelm
parents: 14865
diff changeset
    96
  Scan.this_string "-->";
13729
1a8dda49fd86 Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents: 12416
diff changeset
    97
14910
f145696d4bb5 Scan.this_string;
wenzelm
parents: 14865
diff changeset
    98
val parse_pi = Scan.this_string "<?" |--
f145696d4bb5 Scan.this_string;
wenzelm
parents: 14865
diff changeset
    99
  Scan.repeat (Scan.unless (Scan.this_string "?>") (Scan.one Symbol.not_eof)) --|
f145696d4bb5 Scan.this_string;
wenzelm
parents: 14865
diff changeset
   100
  Scan.this_string "?>";
13729
1a8dda49fd86 Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents: 12416
diff changeset
   101
14185
9b3841638c06 Tried to make parser a bit more standard-conforming.
berghofe
parents: 13729
diff changeset
   102
fun parse_content xs =
9b3841638c06 Tried to make parser a bit more standard-conforming.
berghofe
parents: 13729
diff changeset
   103
  ((Scan.optional (scan_whspc |-- parse_chars >> (single o Text)) [] --
9b3841638c06 Tried to make parser a bit more standard-conforming.
berghofe
parents: 13729
diff changeset
   104
    (Scan.repeat (scan_whspc |--
9b3841638c06 Tried to make parser a bit more standard-conforming.
berghofe
parents: 13729
diff changeset
   105
       (   parse_elem >> single
9b3841638c06 Tried to make parser a bit more standard-conforming.
berghofe
parents: 13729
diff changeset
   106
        || parse_cdata >> (single o Text)
9b3841638c06 Tried to make parser a bit more standard-conforming.
berghofe
parents: 13729
diff changeset
   107
        || parse_pi >> K []
9b3841638c06 Tried to make parser a bit more standard-conforming.
berghofe
parents: 13729
diff changeset
   108
        || parse_comment >> K []) --
9b3841638c06 Tried to make parser a bit more standard-conforming.
berghofe
parents: 13729
diff changeset
   109
       Scan.optional (scan_whspc |-- parse_chars >> (single o Text)) []
9b3841638c06 Tried to make parser a bit more standard-conforming.
berghofe
parents: 13729
diff changeset
   110
         >> op @) >> flat) >> op @) --| scan_whspc) xs
9b3841638c06 Tried to make parser a bit more standard-conforming.
berghofe
parents: 13729
diff changeset
   111
9b3841638c06 Tried to make parser a bit more standard-conforming.
berghofe
parents: 13729
diff changeset
   112
and parse_elem xs =
9b3841638c06 Tried to make parser a bit more standard-conforming.
berghofe
parents: 13729
diff changeset
   113
  ($$ "<" |-- Symbol.scan_id --
14865
8b9a372b3e90 Tuned parse_att.
berghofe
parents: 14863
diff changeset
   114
    Scan.repeat (scan_whspc |-- parse_att) --| scan_whspc :-- (fn (s, _) =>
14185
9b3841638c06 Tried to make parser a bit more standard-conforming.
berghofe
parents: 13729
diff changeset
   115
      !! (err "Expected > or />")
14910
f145696d4bb5 Scan.this_string;
wenzelm
parents: 14865
diff changeset
   116
        (Scan.this_string "/>" >> K []
14185
9b3841638c06 Tried to make parser a bit more standard-conforming.
berghofe
parents: 13729
diff changeset
   117
         || $$ ">" |-- parse_content --|
9b3841638c06 Tried to make parser a bit more standard-conforming.
berghofe
parents: 13729
diff changeset
   118
            !! (err ("Expected </" ^ s ^ ">"))
14910
f145696d4bb5 Scan.this_string;
wenzelm
parents: 14865
diff changeset
   119
              (Scan.this_string ("</" ^ s) --| scan_whspc --| $$ ">"))) >>
13729
1a8dda49fd86 Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents: 12416
diff changeset
   120
    (fn ((s, atts), ts) => Elem (s, atts, ts))) xs;
1a8dda49fd86 Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents: 12416
diff changeset
   121
14185
9b3841638c06 Tried to make parser a bit more standard-conforming.
berghofe
parents: 13729
diff changeset
   122
val parse_document =
14910
f145696d4bb5 Scan.this_string;
wenzelm
parents: 14865
diff changeset
   123
  Scan.option (Scan.this_string "<!DOCTYPE" -- scan_whspc |--
14185
9b3841638c06 Tried to make parser a bit more standard-conforming.
berghofe
parents: 13729
diff changeset
   124
    (Scan.repeat (Scan.unless ($$ ">")
9b3841638c06 Tried to make parser a bit more standard-conforming.
berghofe
parents: 13729
diff changeset
   125
      (Scan.one Symbol.not_eof)) >> implode) --| $$ ">" --| scan_whspc) --
9b3841638c06 Tried to make parser a bit more standard-conforming.
berghofe
parents: 13729
diff changeset
   126
  parse_elem;
9b3841638c06 Tried to make parser a bit more standard-conforming.
berghofe
parents: 13729
diff changeset
   127
13729
1a8dda49fd86 Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents: 12416
diff changeset
   128
fun tree_of_string s =
14185
9b3841638c06 Tried to make parser a bit more standard-conforming.
berghofe
parents: 13729
diff changeset
   129
  (case Scan.finite Symbol.stopper (Scan.error (!! (err "Malformed element")
9b3841638c06 Tried to make parser a bit more standard-conforming.
berghofe
parents: 13729
diff changeset
   130
      (scan_whspc |-- parse_elem --| scan_whspc))) (Symbol.explode s) of
14728
wenzelm
parents: 14714
diff changeset
   131
    (x, []) => x
wenzelm
parents: 14714
diff changeset
   132
  | (_, ys) => error ("XML parsing error: Unprocessed input\n" ^ Symbol.beginning 100 ys));
13729
1a8dda49fd86 Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents: 12416
diff changeset
   133
12416
9b3e7a35da30 Basic support for XML output.
wenzelm
parents:
diff changeset
   134
end;