| author | bulwahn | 
| Thu, 21 Jan 2010 12:18:41 +0100 | |
| changeset 34954 | b206c70ea6f0 | 
| parent 34095 | c2f176a38448 | 
| child 38228 | ada3ab6b9085 | 
| permissions | -rw-r--r-- | 
| 26528 | 1 | (* Title: Pure/General/yxml.ML | 
| 2 | Author: Makarius | |
| 3 | ||
| 26540 
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
 wenzelm parents: 
26528diff
changeset | 4 | Efficient text representation of XML trees using extra characters X | 
| 
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
 wenzelm parents: 
26528diff
changeset | 5 | and Y -- no escaping, may nest marked text verbatim. | 
| 26528 | 6 | |
| 26540 
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
 wenzelm parents: 
26528diff
changeset | 7 | Markup <elem att="val" ...>...body...</elem> is encoded as: | 
| 
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
 wenzelm parents: 
26528diff
changeset | 8 | |
| 
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
 wenzelm parents: 
26528diff
changeset | 9 | X Y name Y att=val ... X | 
| 26528 | 10 | ... | 
| 11 | body | |
| 12 | ... | |
| 26540 
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
 wenzelm parents: 
26528diff
changeset | 13 | X Y X | 
| 26528 | 14 | *) | 
| 15 | ||
| 16 | signature YXML = | |
| 17 | sig | |
| 34095 
c2f176a38448
robust representation of low ASCII control characters within XML/YXML text;
 wenzelm parents: 
31469diff
changeset | 18 | val binary_text: string -> string | 
| 26540 
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
 wenzelm parents: 
26528diff
changeset | 19 | val output_markup: Markup.T -> string * string | 
| 26528 | 20 | val element: string -> XML.attributes -> string list -> string | 
| 21 | val string_of: XML.tree -> string | |
| 22 | val parse_body: string -> XML.tree list | |
| 23 | val parse: string -> XML.tree | |
| 24 | end; | |
| 25 | ||
| 26 | structure YXML: YXML = | |
| 27 | struct | |
| 28 | ||
| 26540 
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
 wenzelm parents: 
26528diff
changeset | 29 | (** string representation **) | 
| 26528 | 30 | |
| 34095 
c2f176a38448
robust representation of low ASCII control characters within XML/YXML text;
 wenzelm parents: 
31469diff
changeset | 31 | (* binary_text -- idempotent recoding *) | 
| 
c2f176a38448
robust representation of low ASCII control characters within XML/YXML text;
 wenzelm parents: 
31469diff
changeset | 32 | |
| 
c2f176a38448
robust representation of low ASCII control characters within XML/YXML text;
 wenzelm parents: 
31469diff
changeset | 33 | fun pseudo_utf8 c = | 
| 
c2f176a38448
robust representation of low ASCII control characters within XML/YXML text;
 wenzelm parents: 
31469diff
changeset | 34 | if Symbol.is_ascii_control c | 
| 
c2f176a38448
robust representation of low ASCII control characters within XML/YXML text;
 wenzelm parents: 
31469diff
changeset | 35 | then chr 192 ^ chr (128 + ord c) | 
| 
c2f176a38448
robust representation of low ASCII control characters within XML/YXML text;
 wenzelm parents: 
31469diff
changeset | 36 | else c; | 
| 
c2f176a38448
robust representation of low ASCII control characters within XML/YXML text;
 wenzelm parents: 
31469diff
changeset | 37 | |
| 
c2f176a38448
robust representation of low ASCII control characters within XML/YXML text;
 wenzelm parents: 
31469diff
changeset | 38 | fun binary_text str = | 
| 
c2f176a38448
robust representation of low ASCII control characters within XML/YXML text;
 wenzelm parents: 
31469diff
changeset | 39 | if exists_string Symbol.is_ascii_control str | 
| 
c2f176a38448
robust representation of low ASCII control characters within XML/YXML text;
 wenzelm parents: 
31469diff
changeset | 40 | then translate_string pseudo_utf8 str | 
| 
c2f176a38448
robust representation of low ASCII control characters within XML/YXML text;
 wenzelm parents: 
31469diff
changeset | 41 | else str; | 
| 
c2f176a38448
robust representation of low ASCII control characters within XML/YXML text;
 wenzelm parents: 
31469diff
changeset | 42 | |
| 
c2f176a38448
robust representation of low ASCII control characters within XML/YXML text;
 wenzelm parents: 
31469diff
changeset | 43 | |
| 26547 | 44 | (* markers *) | 
| 45 | ||
| 26540 
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
 wenzelm parents: 
26528diff
changeset | 46 | val X = Symbol.ENQ; | 
| 
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
 wenzelm parents: 
26528diff
changeset | 47 | val Y = Symbol.ACK; | 
| 
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
 wenzelm parents: 
26528diff
changeset | 48 | val XY = X ^ Y; | 
| 
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
 wenzelm parents: 
26528diff
changeset | 49 | val XYX = XY ^ X; | 
| 26528 | 50 | |
| 51 | ||
| 26547 | 52 | (* output *) | 
| 26540 
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
 wenzelm parents: 
26528diff
changeset | 53 | |
| 27884 | 54 | fun output_markup (markup as (name, atts)) = | 
| 29325 | 55 | if Markup.is_none markup then Markup.no_output | 
| 27884 | 56 | else (XY ^ name ^ implode (map (fn (a, x) => Y ^ a ^ "=" ^ x) atts) ^ X, XYX); | 
| 26540 
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
 wenzelm parents: 
26528diff
changeset | 57 | |
| 26528 | 58 | fun element name atts body = | 
| 26540 
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
 wenzelm parents: 
26528diff
changeset | 59 | let val (pre, post) = output_markup (name, atts) | 
| 
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
 wenzelm parents: 
26528diff
changeset | 60 | in pre ^ implode body ^ post end; | 
| 26528 | 61 | |
| 62 | fun string_of t = | |
| 63 | let | |
| 64 | fun attrib (a, x) = | |
| 26540 
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
 wenzelm parents: 
26528diff
changeset | 65 | Buffer.add Y #> | 
| 26528 | 66 | Buffer.add a #> Buffer.add "=" #> Buffer.add x; | 
| 67 | fun tree (XML.Elem (name, atts, ts)) = | |
| 26540 
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
 wenzelm parents: 
26528diff
changeset | 68 | Buffer.add XY #> Buffer.add name #> fold attrib atts #> Buffer.add X #> | 
| 26528 | 69 | fold tree ts #> | 
| 26540 
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
 wenzelm parents: 
26528diff
changeset | 70 | Buffer.add XYX | 
| 28033 | 71 | | tree (XML.Text s) = Buffer.add s; | 
| 26528 | 72 | in Buffer.empty |> tree t |> Buffer.content end; | 
| 73 | ||
| 74 | ||
| 26540 
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
 wenzelm parents: 
26528diff
changeset | 75 | |
| 
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
 wenzelm parents: 
26528diff
changeset | 76 | (** efficient YXML parsing **) | 
| 26528 | 77 | |
| 78 | local | |
| 79 | ||
| 26540 
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
 wenzelm parents: 
26528diff
changeset | 80 | (* splitting *) | 
| 
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
 wenzelm parents: 
26528diff
changeset | 81 | |
| 
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
 wenzelm parents: 
26528diff
changeset | 82 | fun is_char s c = ord s = Char.ord c; | 
| 
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
 wenzelm parents: 
26528diff
changeset | 83 | |
| 
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
 wenzelm parents: 
26528diff
changeset | 84 | val split_string = | 
| 
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
 wenzelm parents: 
26528diff
changeset | 85 | Substring.full #> | 
| 
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
 wenzelm parents: 
26528diff
changeset | 86 | Substring.tokens (is_char X) #> | 
| 
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
 wenzelm parents: 
26528diff
changeset | 87 | map (Substring.fields (is_char Y) #> map Substring.string); | 
| 
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
 wenzelm parents: 
26528diff
changeset | 88 | |
| 
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
 wenzelm parents: 
26528diff
changeset | 89 | |
| 
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
 wenzelm parents: 
26528diff
changeset | 90 | (* structural errors *) | 
| 26528 | 91 | |
| 92 | fun err msg = raise Fail ("Malformed YXML encoding: " ^ msg);
 | |
| 93 | fun err_attribute () = err "bad attribute"; | |
| 94 | fun err_element () = err "bad element"; | |
| 95 | fun err_unbalanced "" = err "unbalanced element" | |
| 96 |   | err_unbalanced name = err ("unbalanced element " ^ quote name);
 | |
| 97 | ||
| 98 | ||
| 99 | (* stack operations *) | |
| 100 | ||
| 101 | fun add x ((elem, body) :: pending) = (elem, x :: body) :: pending; | |
| 102 | ||
| 103 | fun push "" _ _ = err_element () | |
| 104 | | push name atts pending = ((name, atts), []) :: pending; | |
| 105 | ||
| 106 | fun pop ((("", _), _) :: _) = err_unbalanced ""
 | |
| 107 | | pop (((name, atts), body) :: pending) = add (XML.Elem (name, atts, rev body)) pending; | |
| 108 | ||
| 109 | ||
| 26540 
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
 wenzelm parents: 
26528diff
changeset | 110 | (* parsing *) | 
| 26528 | 111 | |
| 112 | fun parse_attrib s = | |
| 28025 | 113 | (case first_field "=" s of | 
| 28023 
92dd3ad302b7
simplified parse_attrib (find_substring instead of space_explode);
 wenzelm parents: 
27932diff
changeset | 114 | NONE => err_attribute () | 
| 28025 | 115 |   | SOME ("", _) => err_attribute ()
 | 
| 116 | | SOME att => att); | |
| 26528 | 117 | |
| 26540 
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
 wenzelm parents: 
26528diff
changeset | 118 | fun parse_chunk ["", ""] = pop | 
| 
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
 wenzelm parents: 
26528diff
changeset | 119 |   | parse_chunk ("" :: name :: atts) = push name (map parse_attrib atts)
 | 
| 
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
 wenzelm parents: 
26528diff
changeset | 120 | | parse_chunk txts = fold (add o XML.Text) txts; | 
| 26528 | 121 | |
| 122 | in | |
| 123 | ||
| 124 | fun parse_body source = | |
| 26540 
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
 wenzelm parents: 
26528diff
changeset | 125 |   (case fold parse_chunk (split_string source) [(("", []), [])] of
 | 
| 26528 | 126 |     [(("", _), result)] => rev result
 | 
| 127 | | ((name, _), _) :: _ => err_unbalanced name); | |
| 128 | ||
| 129 | fun parse source = | |
| 130 | (case parse_body source of | |
| 27798 
b96c73f11a23
YXML.parse: allow text without markup, potentially empty;
 wenzelm parents: 
26684diff
changeset | 131 | [result] => result | 
| 
b96c73f11a23
YXML.parse: allow text without markup, potentially empty;
 wenzelm parents: 
26684diff
changeset | 132 | | [] => XML.Text "" | 
| 
b96c73f11a23
YXML.parse: allow text without markup, potentially empty;
 wenzelm parents: 
26684diff
changeset | 133 | | _ => err "multiple results"); | 
| 26528 | 134 | |
| 135 | end; | |
| 136 | ||
| 137 | end; | |
| 138 |