src/Pure/General/xml.ML
author wenzelm
Sat, 29 May 2004 15:11:06 +0200
changeset 14844 a15c65e66ee9
parent 14728 df34201f1a15
child 14863 49afb368f4be
permissions -rw-r--r--
Scan.this; tuned;
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
14844
a15c65e66ee9 Scan.this; tuned;
wenzelm
parents: 14728
diff changeset
    80
fun scan_lit s = Scan.this (Symbol.explode s) >> K s;
13729
1a8dda49fd86 Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents: 12416
diff changeset
    81
1a8dda49fd86 Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents: 12416
diff changeset
    82
val scan_special = $$ "&" ^^ Symbol.scan_id ^^ $$ ";" >> decode;
1a8dda49fd86 Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents: 12416
diff changeset
    83
14185
9b3841638c06 Tried to make parser a bit more standard-conforming.
berghofe
parents: 13729
diff changeset
    84
val parse_chars = Scan.repeat1 (Scan.unless (scan_whspc -- $$ "<")
9b3841638c06 Tried to make parser a bit more standard-conforming.
berghofe
parents: 13729
diff changeset
    85
  (scan_special || Scan.one Symbol.not_eof)) >> implode;
9b3841638c06 Tried to make parser a bit more standard-conforming.
berghofe
parents: 13729
diff changeset
    86
14844
a15c65e66ee9 Scan.this; tuned;
wenzelm
parents: 14728
diff changeset
    87
val parse_cdata = scan_lit "<![CDATA[" |--
a15c65e66ee9 Scan.this; tuned;
wenzelm
parents: 14728
diff changeset
    88
  (Scan.repeat (Scan.unless (scan_lit "]]>") (Scan.one Symbol.not_eof)) >>
a15c65e66ee9 Scan.this; tuned;
wenzelm
parents: 14728
diff changeset
    89
    implode) --| scan_lit "]]>";
14185
9b3841638c06 Tried to make parser a bit more standard-conforming.
berghofe
parents: 13729
diff changeset
    90
9b3841638c06 Tried to make parser a bit more standard-conforming.
berghofe
parents: 13729
diff changeset
    91
val parse_att =
9b3841638c06 Tried to make parser a bit more standard-conforming.
berghofe
parents: 13729
diff changeset
    92
  Symbol.scan_id --| scan_whspc --| $$ "=" --| scan_whspc --| $$ "\"" --
9b3841638c06 Tried to make parser a bit more standard-conforming.
berghofe
parents: 13729
diff changeset
    93
  (Scan.repeat (Scan.unless ($$ "\"")
9b3841638c06 Tried to make parser a bit more standard-conforming.
berghofe
parents: 13729
diff changeset
    94
    (scan_special || Scan.one Symbol.not_eof)) >> implode) --| $$ "\"";
9b3841638c06 Tried to make parser a bit more standard-conforming.
berghofe
parents: 13729
diff changeset
    95
14844
a15c65e66ee9 Scan.this; tuned;
wenzelm
parents: 14728
diff changeset
    96
val parse_comment = scan_lit "<!--" --
a15c65e66ee9 Scan.this; tuned;
wenzelm
parents: 14728
diff changeset
    97
  Scan.repeat (Scan.unless (scan_lit "-->") (Scan.one Symbol.not_eof)) --
a15c65e66ee9 Scan.this; tuned;
wenzelm
parents: 14728
diff changeset
    98
  scan_lit "-->";
13729
1a8dda49fd86 Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents: 12416
diff changeset
    99
14844
a15c65e66ee9 Scan.this; tuned;
wenzelm
parents: 14728
diff changeset
   100
val parse_pi = scan_lit "<?" |--
a15c65e66ee9 Scan.this; tuned;
wenzelm
parents: 14728
diff changeset
   101
  Scan.repeat (Scan.unless (scan_lit "?>") (Scan.one Symbol.not_eof)) --|
a15c65e66ee9 Scan.this; tuned;
wenzelm
parents: 14728
diff changeset
   102
  scan_lit "?>";
13729
1a8dda49fd86 Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents: 12416
diff changeset
   103
14185
9b3841638c06 Tried to make parser a bit more standard-conforming.
berghofe
parents: 13729
diff changeset
   104
fun parse_content xs =
9b3841638c06 Tried to make parser a bit more standard-conforming.
berghofe
parents: 13729
diff changeset
   105
  ((Scan.optional (scan_whspc |-- parse_chars >> (single o Text)) [] --
9b3841638c06 Tried to make parser a bit more standard-conforming.
berghofe
parents: 13729
diff changeset
   106
    (Scan.repeat (scan_whspc |--
9b3841638c06 Tried to make parser a bit more standard-conforming.
berghofe
parents: 13729
diff changeset
   107
       (   parse_elem >> single
9b3841638c06 Tried to make parser a bit more standard-conforming.
berghofe
parents: 13729
diff changeset
   108
        || parse_cdata >> (single o Text)
9b3841638c06 Tried to make parser a bit more standard-conforming.
berghofe
parents: 13729
diff changeset
   109
        || parse_pi >> K []
9b3841638c06 Tried to make parser a bit more standard-conforming.
berghofe
parents: 13729
diff changeset
   110
        || parse_comment >> K []) --
9b3841638c06 Tried to make parser a bit more standard-conforming.
berghofe
parents: 13729
diff changeset
   111
       Scan.optional (scan_whspc |-- parse_chars >> (single o Text)) []
9b3841638c06 Tried to make parser a bit more standard-conforming.
berghofe
parents: 13729
diff changeset
   112
         >> op @) >> flat) >> op @) --| scan_whspc) xs
9b3841638c06 Tried to make parser a bit more standard-conforming.
berghofe
parents: 13729
diff changeset
   113
9b3841638c06 Tried to make parser a bit more standard-conforming.
berghofe
parents: 13729
diff changeset
   114
and parse_elem xs =
9b3841638c06 Tried to make parser a bit more standard-conforming.
berghofe
parents: 13729
diff changeset
   115
  ($$ "<" |-- Symbol.scan_id --
9b3841638c06 Tried to make parser a bit more standard-conforming.
berghofe
parents: 13729
diff changeset
   116
    Scan.repeat (scan_whspc |-- parse_att) --| scan_whspc :-- (fn (s, _) =>
9b3841638c06 Tried to make parser a bit more standard-conforming.
berghofe
parents: 13729
diff changeset
   117
      !! (err "Expected > or />")
14844
a15c65e66ee9 Scan.this; tuned;
wenzelm
parents: 14728
diff changeset
   118
        (scan_lit "/>" >> K []
14185
9b3841638c06 Tried to make parser a bit more standard-conforming.
berghofe
parents: 13729
diff changeset
   119
         || $$ ">" |-- parse_content --|
9b3841638c06 Tried to make parser a bit more standard-conforming.
berghofe
parents: 13729
diff changeset
   120
            !! (err ("Expected </" ^ s ^ ">"))
14844
a15c65e66ee9 Scan.this; tuned;
wenzelm
parents: 14728
diff changeset
   121
              (scan_lit ("</" ^ s) --| scan_whspc --| $$ ">"))) >>
13729
1a8dda49fd86 Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents: 12416
diff changeset
   122
    (fn ((s, atts), ts) => Elem (s, atts, ts))) xs;
1a8dda49fd86 Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents: 12416
diff changeset
   123
14185
9b3841638c06 Tried to make parser a bit more standard-conforming.
berghofe
parents: 13729
diff changeset
   124
val parse_document =
14844
a15c65e66ee9 Scan.this; tuned;
wenzelm
parents: 14728
diff changeset
   125
  Scan.option (scan_lit "<!DOCTYPE" -- scan_whspc |--
14185
9b3841638c06 Tried to make parser a bit more standard-conforming.
berghofe
parents: 13729
diff changeset
   126
    (Scan.repeat (Scan.unless ($$ ">")
9b3841638c06 Tried to make parser a bit more standard-conforming.
berghofe
parents: 13729
diff changeset
   127
      (Scan.one Symbol.not_eof)) >> implode) --| $$ ">" --| scan_whspc) --
9b3841638c06 Tried to make parser a bit more standard-conforming.
berghofe
parents: 13729
diff changeset
   128
  parse_elem;
9b3841638c06 Tried to make parser a bit more standard-conforming.
berghofe
parents: 13729
diff changeset
   129
13729
1a8dda49fd86 Added XML parser (useful for parsing PGIP / PGML).
berghofe
parents: 12416
diff changeset
   130
fun tree_of_string s =
14185
9b3841638c06 Tried to make parser a bit more standard-conforming.
berghofe
parents: 13729
diff changeset
   131
  (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
   132
      (scan_whspc |-- parse_elem --| scan_whspc))) (Symbol.explode s) of
14728
wenzelm
parents: 14714
diff changeset
   133
    (x, []) => x
wenzelm
parents: 14714
diff changeset
   134
  | (_, 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
   135
12416
9b3e7a35da30 Basic support for XML output.
wenzelm
parents:
diff changeset
   136
end;