src/Pure/PIDE/yxml.ML
author wenzelm
Wed Mar 07 20:49:18 2012 +0100 (2012-03-07 ago)
changeset 46831 4a6085849a37
parent 44698 0385292321a0
child 46832 050e344825c8
permissions -rw-r--r--
eliminated dead code;
tuned;
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@43728
    24
  val string_of_body: XML.body -> string
wenzelm@26528
    25
  val string_of: XML.tree -> string
wenzelm@38266
    26
  val parse_body: string -> XML.body
wenzelm@26528
    27
  val parse: string -> XML.tree
wenzelm@26528
    28
end;
wenzelm@26528
    29
wenzelm@26528
    30
structure YXML: YXML =
wenzelm@26528
    31
struct
wenzelm@26528
    32
wenzelm@26540
    33
(** string representation **)
wenzelm@26528
    34
wenzelm@38265
    35
(* idempotent recoding of certain low ASCII control characters *)
wenzelm@34095
    36
wenzelm@34095
    37
fun pseudo_utf8 c =
wenzelm@34095
    38
  if Symbol.is_ascii_control c
wenzelm@34095
    39
  then chr 192 ^ chr (128 + ord c)
wenzelm@34095
    40
  else c;
wenzelm@34095
    41
wenzelm@43772
    42
fun embed_controls str =
wenzelm@34095
    43
  if exists_string Symbol.is_ascii_control str
wenzelm@34095
    44
  then translate_string pseudo_utf8 str
wenzelm@34095
    45
  else str;
wenzelm@34095
    46
wenzelm@34095
    47
wenzelm@26547
    48
(* markers *)
wenzelm@26547
    49
wenzelm@43777
    50
val X = chr 5;
wenzelm@43777
    51
val Y = chr 6;
wenzelm@26540
    52
val XY = X ^ Y;
wenzelm@26540
    53
val XYX = XY ^ X;
wenzelm@26528
    54
wenzelm@43782
    55
val detect = exists_string (fn s => s = X orelse s = Y);
wenzelm@43731
    56
wenzelm@26528
    57
wenzelm@26547
    58
(* output *)
wenzelm@26540
    59
wenzelm@27884
    60
fun output_markup (markup as (name, atts)) =
wenzelm@38474
    61
  if Markup.is_empty markup then Markup.no_output
wenzelm@27884
    62
  else (XY ^ name ^ implode (map (fn (a, x) => Y ^ a ^ "=" ^ x) atts) ^ X, XYX);
wenzelm@26540
    63
wenzelm@43728
    64
fun string_of_body body =
wenzelm@26528
    65
  let
wenzelm@46831
    66
    fun attrib (a, x) = Buffer.add Y #> Buffer.add a #> Buffer.add "=" #> Buffer.add x;
wenzelm@38228
    67
    fun tree (XML.Elem ((name, atts), ts)) =
wenzelm@26540
    68
          Buffer.add XY #> Buffer.add name #> fold attrib atts #> Buffer.add X #>
wenzelm@43728
    69
          trees ts #>
wenzelm@26540
    70
          Buffer.add XYX
wenzelm@43728
    71
      | tree (XML.Text s) = Buffer.add s
wenzelm@43728
    72
    and trees ts = fold tree ts;
wenzelm@43728
    73
  in Buffer.empty |> trees body |> Buffer.content end;
wenzelm@43728
    74
wenzelm@43728
    75
val string_of = string_of_body o single;
wenzelm@26528
    76
wenzelm@26528
    77
wenzelm@26540
    78
wenzelm@26540
    79
(** efficient YXML parsing **)
wenzelm@26528
    80
wenzelm@26528
    81
local
wenzelm@26528
    82
wenzelm@26540
    83
(* splitting *)
wenzelm@26540
    84
wenzelm@26540
    85
fun is_char s c = ord s = Char.ord c;
wenzelm@26540
    86
wenzelm@26540
    87
val split_string =
wenzelm@26540
    88
  Substring.full #>
wenzelm@26540
    89
  Substring.tokens (is_char X) #>
wenzelm@26540
    90
  map (Substring.fields (is_char Y) #> map Substring.string);
wenzelm@26540
    91
wenzelm@26540
    92
wenzelm@26540
    93
(* structural errors *)
wenzelm@26528
    94
wenzelm@26528
    95
fun err msg = raise Fail ("Malformed YXML encoding: " ^ msg);
wenzelm@26528
    96
fun err_attribute () = err "bad attribute";
wenzelm@26528
    97
fun err_element () = err "bad element";
wenzelm@26528
    98
fun err_unbalanced "" = err "unbalanced element"
wenzelm@26528
    99
  | err_unbalanced name = err ("unbalanced element " ^ quote name);
wenzelm@26528
   100
wenzelm@26528
   101
wenzelm@26528
   102
(* stack operations *)
wenzelm@26528
   103
wenzelm@26528
   104
fun add x ((elem, body) :: pending) = (elem, x :: body) :: pending;
wenzelm@26528
   105
wenzelm@26528
   106
fun push "" _ _ = err_element ()
wenzelm@26528
   107
  | push name atts pending = ((name, atts), []) :: pending;
wenzelm@26528
   108
wenzelm@26528
   109
fun pop ((("", _), _) :: _) = err_unbalanced ""
wenzelm@38228
   110
  | pop ((markup, body) :: pending) = add (XML.Elem (markup, rev body)) pending;
wenzelm@26528
   111
wenzelm@26528
   112
wenzelm@26540
   113
(* parsing *)
wenzelm@26528
   114
wenzelm@26528
   115
fun parse_attrib s =
wenzelm@28025
   116
  (case first_field "=" s of
wenzelm@28023
   117
    NONE => err_attribute ()
wenzelm@28025
   118
  | SOME ("", _) => err_attribute ()
wenzelm@28025
   119
  | SOME att => att);
wenzelm@26528
   120
wenzelm@26540
   121
fun parse_chunk ["", ""] = pop
wenzelm@26540
   122
  | parse_chunk ("" :: name :: atts) = push name (map parse_attrib atts)
wenzelm@26540
   123
  | parse_chunk txts = fold (add o XML.Text) txts;
wenzelm@26528
   124
wenzelm@26528
   125
in
wenzelm@26528
   126
wenzelm@26528
   127
fun parse_body source =
noschinl@43615
   128
  (case fold parse_chunk (split_string source) [(("", []), [])] of
wenzelm@26528
   129
    [(("", _), result)] => rev result
wenzelm@26528
   130
  | ((name, _), _) :: _ => err_unbalanced name);
wenzelm@26528
   131
wenzelm@26528
   132
fun parse source =
wenzelm@26528
   133
  (case parse_body source of
wenzelm@27798
   134
    [result] => result
wenzelm@27798
   135
  | [] => XML.Text ""
wenzelm@27798
   136
  | _ => err "multiple results");
wenzelm@26528
   137
wenzelm@26528
   138
end;
wenzelm@26528
   139
wenzelm@26528
   140
end;
wenzelm@26528
   141