src/Pure/PIDE/yxml.ML
author wenzelm
Sun Sep 04 15:21:50 2011 +0200 (2011-09-04 ago)
changeset 44698 0385292321a0
parent 43782 src/Pure/General/yxml.ML@834de29572d5
child 46831 4a6085849a37
permissions -rw-r--r--
moved XML/YXML to src/Pure/PIDE;
tuned comments;
wenzelm@44698
     1
(*  Title:      Pure/PIDE/yxml.ML
wenzelm@26528
     2
    Author:     Makarius
wenzelm@26528
     3
wenzelm@26540
     4
Efficient text representation of XML trees using extra characters X
wenzelm@44698
     5
and Y -- no escaping, may nest marked text verbatim.  Suitable for
wenzelm@44698
     6
direct inlining into plain text.
wenzelm@26528
     7
wenzelm@26540
     8
Markup <elem att="val" ...>...body...</elem> is encoded as:
wenzelm@26540
     9
wenzelm@26540
    10
  X Y name Y att=val ... X
wenzelm@26528
    11
  ...
wenzelm@26528
    12
  body
wenzelm@26528
    13
  ...
wenzelm@26540
    14
  X Y X
wenzelm@26528
    15
*)
wenzelm@26528
    16
wenzelm@26528
    17
signature YXML =
wenzelm@26528
    18
sig
wenzelm@43777
    19
  val X: Symbol.symbol
wenzelm@43777
    20
  val Y: Symbol.symbol
wenzelm@43772
    21
  val embed_controls: string -> string
wenzelm@43731
    22
  val detect: string -> bool
wenzelm@26540
    23
  val output_markup: Markup.T -> string * string
wenzelm@26528
    24
  val element: string -> XML.attributes -> string list -> string
wenzelm@43728
    25
  val string_of_body: XML.body -> string
wenzelm@26528
    26
  val string_of: XML.tree -> string
wenzelm@38266
    27
  val parse_body: string -> XML.body
wenzelm@26528
    28
  val parse: string -> XML.tree
wenzelm@26528
    29
end;
wenzelm@26528
    30
wenzelm@26528
    31
structure YXML: YXML =
wenzelm@26528
    32
struct
wenzelm@26528
    33
wenzelm@26540
    34
(** string representation **)
wenzelm@26528
    35
wenzelm@38265
    36
(* idempotent recoding of certain low ASCII control characters *)
wenzelm@34095
    37
wenzelm@34095
    38
fun pseudo_utf8 c =
wenzelm@34095
    39
  if Symbol.is_ascii_control c
wenzelm@34095
    40
  then chr 192 ^ chr (128 + ord c)
wenzelm@34095
    41
  else c;
wenzelm@34095
    42
wenzelm@43772
    43
fun embed_controls str =
wenzelm@34095
    44
  if exists_string Symbol.is_ascii_control str
wenzelm@34095
    45
  then translate_string pseudo_utf8 str
wenzelm@34095
    46
  else str;
wenzelm@34095
    47
wenzelm@34095
    48
wenzelm@26547
    49
(* markers *)
wenzelm@26547
    50
wenzelm@43777
    51
val X = chr 5;
wenzelm@43777
    52
val Y = chr 6;
wenzelm@26540
    53
val XY = X ^ Y;
wenzelm@26540
    54
val XYX = XY ^ X;
wenzelm@26528
    55
wenzelm@43782
    56
val detect = exists_string (fn s => s = X orelse s = Y);
wenzelm@43731
    57
wenzelm@26528
    58
wenzelm@26547
    59
(* output *)
wenzelm@26540
    60
wenzelm@27884
    61
fun output_markup (markup as (name, atts)) =
wenzelm@38474
    62
  if Markup.is_empty markup then Markup.no_output
wenzelm@27884
    63
  else (XY ^ name ^ implode (map (fn (a, x) => Y ^ a ^ "=" ^ x) atts) ^ X, XYX);
wenzelm@26540
    64
wenzelm@26528
    65
fun element name atts body =
wenzelm@26540
    66
  let val (pre, post) = output_markup (name, atts)
wenzelm@26540
    67
  in pre ^ implode body ^ post end;
wenzelm@26528
    68
wenzelm@43728
    69
fun string_of_body body =
wenzelm@26528
    70
  let
wenzelm@26528
    71
    fun attrib (a, x) =
wenzelm@26540
    72
      Buffer.add Y #>
wenzelm@26528
    73
      Buffer.add a #> Buffer.add "=" #> Buffer.add x;
wenzelm@38228
    74
    fun tree (XML.Elem ((name, atts), ts)) =
wenzelm@26540
    75
          Buffer.add XY #> Buffer.add name #> fold attrib atts #> Buffer.add X #>
wenzelm@43728
    76
          trees ts #>
wenzelm@26540
    77
          Buffer.add XYX
wenzelm@43728
    78
      | tree (XML.Text s) = Buffer.add s
wenzelm@43728
    79
    and trees ts = fold tree ts;
wenzelm@43728
    80
  in Buffer.empty |> trees body |> Buffer.content end;
wenzelm@43728
    81
wenzelm@43728
    82
val string_of = string_of_body o single;
wenzelm@26528
    83
wenzelm@26528
    84
wenzelm@26540
    85
wenzelm@26540
    86
(** efficient YXML parsing **)
wenzelm@26528
    87
wenzelm@26528
    88
local
wenzelm@26528
    89
wenzelm@26540
    90
(* splitting *)
wenzelm@26540
    91
wenzelm@26540
    92
fun is_char s c = ord s = Char.ord c;
wenzelm@26540
    93
wenzelm@26540
    94
val split_string =
wenzelm@26540
    95
  Substring.full #>
wenzelm@26540
    96
  Substring.tokens (is_char X) #>
wenzelm@26540
    97
  map (Substring.fields (is_char Y) #> map Substring.string);
wenzelm@26540
    98
wenzelm@26540
    99
wenzelm@26540
   100
(* structural errors *)
wenzelm@26528
   101
wenzelm@26528
   102
fun err msg = raise Fail ("Malformed YXML encoding: " ^ msg);
wenzelm@26528
   103
fun err_attribute () = err "bad attribute";
wenzelm@26528
   104
fun err_element () = err "bad element";
wenzelm@26528
   105
fun err_unbalanced "" = err "unbalanced element"
wenzelm@26528
   106
  | err_unbalanced name = err ("unbalanced element " ^ quote name);
wenzelm@26528
   107
wenzelm@26528
   108
wenzelm@26528
   109
(* stack operations *)
wenzelm@26528
   110
wenzelm@26528
   111
fun add x ((elem, body) :: pending) = (elem, x :: body) :: pending;
wenzelm@26528
   112
wenzelm@26528
   113
fun push "" _ _ = err_element ()
wenzelm@26528
   114
  | push name atts pending = ((name, atts), []) :: pending;
wenzelm@26528
   115
wenzelm@26528
   116
fun pop ((("", _), _) :: _) = err_unbalanced ""
wenzelm@38228
   117
  | pop ((markup, body) :: pending) = add (XML.Elem (markup, rev body)) pending;
wenzelm@26528
   118
wenzelm@26528
   119
wenzelm@26540
   120
(* parsing *)
wenzelm@26528
   121
wenzelm@26528
   122
fun parse_attrib s =
wenzelm@28025
   123
  (case first_field "=" s of
wenzelm@28023
   124
    NONE => err_attribute ()
wenzelm@28025
   125
  | SOME ("", _) => err_attribute ()
wenzelm@28025
   126
  | SOME att => att);
wenzelm@26528
   127
wenzelm@26540
   128
fun parse_chunk ["", ""] = pop
wenzelm@26540
   129
  | parse_chunk ("" :: name :: atts) = push name (map parse_attrib atts)
wenzelm@26540
   130
  | parse_chunk txts = fold (add o XML.Text) txts;
wenzelm@26528
   131
wenzelm@26528
   132
in
wenzelm@26528
   133
wenzelm@26528
   134
fun parse_body source =
noschinl@43615
   135
  (case fold parse_chunk (split_string source) [(("", []), [])] of
wenzelm@26528
   136
    [(("", _), result)] => rev result
wenzelm@26528
   137
  | ((name, _), _) :: _ => err_unbalanced name);
wenzelm@26528
   138
wenzelm@26528
   139
fun parse source =
wenzelm@26528
   140
  (case parse_body source of
wenzelm@27798
   141
    [result] => result
wenzelm@27798
   142
  | [] => XML.Text ""
wenzelm@27798
   143
  | _ => err "multiple results");
wenzelm@26528
   144
wenzelm@26528
   145
end;
wenzelm@26528
   146
wenzelm@26528
   147
end;
wenzelm@26528
   148