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