| author | wenzelm | 
| Wed, 27 Mar 2013 17:58:07 +0100 | |
| changeset 51557 | 4e4b56b7a3a5 | 
| parent 49656 | 7ff712de5747 | 
| child 59433 | 9da5b2c61049 | 
| permissions | -rw-r--r-- | 
| 44698 | 1 | (* Title: Pure/PIDE/yxml.ML | 
| 26528 | 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 | 
| 44698 | 5 | and Y -- no escaping, may nest marked text verbatim. Suitable for | 
| 6 | direct inlining into plain text. | |
| 26528 | 7 | |
| 26540 
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
 wenzelm parents: 
26528diff
changeset | 8 | 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 | 9 | |
| 
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
 wenzelm parents: 
26528diff
changeset | 10 | X Y name Y att=val ... X | 
| 26528 | 11 | ... | 
| 12 | body | |
| 13 | ... | |
| 26540 
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
 wenzelm parents: 
26528diff
changeset | 14 | X Y X | 
| 26528 | 15 | *) | 
| 16 | ||
| 17 | signature YXML = | |
| 18 | sig | |
| 43777 | 19 | val X: Symbol.symbol | 
| 20 | val Y: Symbol.symbol | |
| 43772 
c825594fd0c1
clarified YXML.embed_controls -- this is idempotent and cannot be nested;
 wenzelm parents: 
43731diff
changeset | 21 | val embed_controls: string -> string | 
| 43731 
70072780e095
inner syntax supports inlined YXML according to Term_XML (particularly useful for producing text under program control);
 wenzelm parents: 
43728diff
changeset | 22 | val detect: string -> bool | 
| 26540 
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
 wenzelm parents: 
26528diff
changeset | 23 | val output_markup: Markup.T -> string * string | 
| 43728 | 24 | val string_of_body: XML.body -> string | 
| 26528 | 25 | val string_of: XML.tree -> string | 
| 49656 
7ff712de5747
treat wrapped markup elements as raw markup delimiters;
 wenzelm parents: 
46832diff
changeset | 26 | val output_markup_elem: Markup.T -> (string * string) * string | 
| 38266 
492d377ecfe2
type XML.body as basic data representation language;
 wenzelm parents: 
38265diff
changeset | 27 | val parse_body: string -> XML.body | 
| 26528 | 28 | val parse: string -> XML.tree | 
| 29 | end; | |
| 30 | ||
| 31 | structure YXML: YXML = | |
| 32 | struct | |
| 33 | ||
| 26540 
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
 wenzelm parents: 
26528diff
changeset | 34 | (** string representation **) | 
| 26528 | 35 | |
| 38265 
cc9fde54311f
renamed YXML.binary_text to YXML.escape_controls to emphasize what it actually does;
 wenzelm parents: 
38228diff
changeset | 36 | (* idempotent recoding of certain low ASCII control characters *) | 
| 34095 
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 pseudo_utf8 c = | 
| 
c2f176a38448
robust representation of low ASCII control characters within XML/YXML text;
 wenzelm parents: 
31469diff
changeset | 39 | if Symbol.is_ascii_control c | 
| 
c2f176a38448
robust representation of low ASCII control characters within XML/YXML text;
 wenzelm parents: 
31469diff
changeset | 40 | then chr 192 ^ chr (128 + ord c) | 
| 
c2f176a38448
robust representation of low ASCII control characters within XML/YXML text;
 wenzelm parents: 
31469diff
changeset | 41 | else c; | 
| 
c2f176a38448
robust representation of low ASCII control characters within XML/YXML text;
 wenzelm parents: 
31469diff
changeset | 42 | |
| 43772 
c825594fd0c1
clarified YXML.embed_controls -- this is idempotent and cannot be nested;
 wenzelm parents: 
43731diff
changeset | 43 | fun embed_controls str = | 
| 34095 
c2f176a38448
robust representation of low ASCII control characters within XML/YXML text;
 wenzelm parents: 
31469diff
changeset | 44 | if exists_string Symbol.is_ascii_control str | 
| 
c2f176a38448
robust representation of low ASCII control characters within XML/YXML text;
 wenzelm parents: 
31469diff
changeset | 45 | then translate_string pseudo_utf8 str | 
| 
c2f176a38448
robust representation of low ASCII control characters within XML/YXML text;
 wenzelm parents: 
31469diff
changeset | 46 | else str; | 
| 
c2f176a38448
robust representation of low ASCII control characters within XML/YXML text;
 wenzelm parents: 
31469diff
changeset | 47 | |
| 
c2f176a38448
robust representation of low ASCII control characters within XML/YXML text;
 wenzelm parents: 
31469diff
changeset | 48 | |
| 26547 | 49 | (* markers *) | 
| 50 | ||
| 43777 | 51 | val X = chr 5; | 
| 52 | val Y = chr 6; | |
| 26540 
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
 wenzelm parents: 
26528diff
changeset | 53 | 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 | 54 | val XYX = XY ^ X; | 
| 26528 | 55 | |
| 43782 | 56 | val detect = exists_string (fn s => s = X orelse s = Y); | 
| 43731 
70072780e095
inner syntax supports inlined YXML according to Term_XML (particularly useful for producing text under program control);
 wenzelm parents: 
43728diff
changeset | 57 | |
| 26528 | 58 | |
| 26547 | 59 | (* output *) | 
| 26540 
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
 wenzelm parents: 
26528diff
changeset | 60 | |
| 27884 | 61 | fun output_markup (markup as (name, atts)) = | 
| 38474 
e498dc2eb576
uniform Markup.empty/Markup.Empty in ML and Scala;
 wenzelm parents: 
38266diff
changeset | 62 | if Markup.is_empty markup then Markup.no_output | 
| 27884 | 63 | 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 | 64 | |
| 43728 | 65 | fun string_of_body body = | 
| 26528 | 66 | let | 
| 46831 | 67 | fun attrib (a, x) = Buffer.add Y #> Buffer.add a #> Buffer.add "=" #> Buffer.add x; | 
| 38228 
ada3ab6b9085
simplified type XML.tree: embed Markup.T directly, avoid slightly odd triple;
 wenzelm parents: 
34095diff
changeset | 68 | 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 | 69 | Buffer.add XY #> Buffer.add name #> fold attrib atts #> Buffer.add X #> | 
| 43728 | 70 | trees ts #> | 
| 26540 
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
 wenzelm parents: 
26528diff
changeset | 71 | Buffer.add XYX | 
| 43728 | 72 | | tree (XML.Text s) = Buffer.add s | 
| 73 | and trees ts = fold tree ts; | |
| 74 | in Buffer.empty |> trees body |> Buffer.content end; | |
| 75 | ||
| 76 | val string_of = string_of_body o single; | |
| 26528 | 77 | |
| 78 | ||
| 49656 
7ff712de5747
treat wrapped markup elements as raw markup delimiters;
 wenzelm parents: 
46832diff
changeset | 79 | (* wrapped elements *) | 
| 
7ff712de5747
treat wrapped markup elements as raw markup delimiters;
 wenzelm parents: 
46832diff
changeset | 80 | |
| 
7ff712de5747
treat wrapped markup elements as raw markup delimiters;
 wenzelm parents: 
46832diff
changeset | 81 | val Z = chr 0; | 
| 
7ff712de5747
treat wrapped markup elements as raw markup delimiters;
 wenzelm parents: 
46832diff
changeset | 82 | val Z_text = [XML.Text Z]; | 
| 
7ff712de5747
treat wrapped markup elements as raw markup delimiters;
 wenzelm parents: 
46832diff
changeset | 83 | |
| 
7ff712de5747
treat wrapped markup elements as raw markup delimiters;
 wenzelm parents: 
46832diff
changeset | 84 | fun output_markup_elem markup = | 
| 
7ff712de5747
treat wrapped markup elements as raw markup delimiters;
 wenzelm parents: 
46832diff
changeset | 85 | let val [bg1, bg2, en] = space_explode Z (string_of (XML.wrap_elem ((markup, Z_text), Z_text))) | 
| 
7ff712de5747
treat wrapped markup elements as raw markup delimiters;
 wenzelm parents: 
46832diff
changeset | 86 | in ((bg1, bg2), en) end; | 
| 
7ff712de5747
treat wrapped markup elements as raw markup delimiters;
 wenzelm parents: 
46832diff
changeset | 87 | |
| 
7ff712de5747
treat wrapped markup elements as raw markup delimiters;
 wenzelm parents: 
46832diff
changeset | 88 | |
| 26540 
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 | (** efficient YXML parsing **) | 
| 26528 | 91 | |
| 92 | local | |
| 93 | ||
| 26540 
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
 wenzelm parents: 
26528diff
changeset | 94 | (* splitting *) | 
| 
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
 wenzelm parents: 
26528diff
changeset | 95 | |
| 
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
 wenzelm parents: 
26528diff
changeset | 96 | 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 | 97 | |
| 
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
 wenzelm parents: 
26528diff
changeset | 98 | val split_string = | 
| 
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
 wenzelm parents: 
26528diff
changeset | 99 | Substring.full #> | 
| 
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
 wenzelm parents: 
26528diff
changeset | 100 | 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 | 101 | 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 | 102 | |
| 
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
 wenzelm parents: 
26528diff
changeset | 103 | |
| 
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
 wenzelm parents: 
26528diff
changeset | 104 | (* structural errors *) | 
| 26528 | 105 | |
| 46832 | 106 | fun err msg = raise Fail ("Malformed YXML: " ^ msg);
 | 
| 26528 | 107 | fun err_attribute () = err "bad attribute"; | 
| 108 | fun err_element () = err "bad element"; | |
| 109 | fun err_unbalanced "" = err "unbalanced element" | |
| 110 |   | err_unbalanced name = err ("unbalanced element " ^ quote name);
 | |
| 111 | ||
| 112 | ||
| 113 | (* stack operations *) | |
| 114 | ||
| 115 | fun add x ((elem, body) :: pending) = (elem, x :: body) :: pending; | |
| 116 | ||
| 117 | fun push "" _ _ = err_element () | |
| 118 | | push name atts pending = ((name, atts), []) :: pending; | |
| 119 | ||
| 120 | fun pop ((("", _), _) :: _) = err_unbalanced ""
 | |
| 38228 
ada3ab6b9085
simplified type XML.tree: embed Markup.T directly, avoid slightly odd triple;
 wenzelm parents: 
34095diff
changeset | 121 | | pop ((markup, body) :: pending) = add (XML.Elem (markup, rev body)) pending; | 
| 26528 | 122 | |
| 123 | ||
| 26540 
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
 wenzelm parents: 
26528diff
changeset | 124 | (* parsing *) | 
| 26528 | 125 | |
| 126 | fun parse_attrib s = | |
| 28025 | 127 | (case first_field "=" s of | 
| 28023 
92dd3ad302b7
simplified parse_attrib (find_substring instead of space_explode);
 wenzelm parents: 
27932diff
changeset | 128 | NONE => err_attribute () | 
| 28025 | 129 |   | SOME ("", _) => err_attribute ()
 | 
| 130 | | SOME att => att); | |
| 26528 | 131 | |
| 26540 
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
 wenzelm parents: 
26528diff
changeset | 132 | 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 | 133 |   | 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 | 134 | | parse_chunk txts = fold (add o XML.Text) txts; | 
| 26528 | 135 | |
| 136 | in | |
| 137 | ||
| 138 | fun parse_body source = | |
| 43615 
8e0f6cfa8eb2
reverted ce00462f,b3759dce, 7a165592: unwanted generalisation
 noschinl parents: 
42355diff
changeset | 139 |   (case fold parse_chunk (split_string source) [(("", []), [])] of
 | 
| 26528 | 140 |     [(("", _), result)] => rev result
 | 
| 141 | | ((name, _), _) :: _ => err_unbalanced name); | |
| 142 | ||
| 143 | fun parse source = | |
| 144 | (case parse_body source of | |
| 27798 
b96c73f11a23
YXML.parse: allow text without markup, potentially empty;
 wenzelm parents: 
26684diff
changeset | 145 | [result] => result | 
| 
b96c73f11a23
YXML.parse: allow text without markup, potentially empty;
 wenzelm parents: 
26684diff
changeset | 146 | | [] => XML.Text "" | 
| 
b96c73f11a23
YXML.parse: allow text without markup, potentially empty;
 wenzelm parents: 
26684diff
changeset | 147 | | _ => err "multiple results"); | 
| 26528 | 148 | |
| 149 | end; | |
| 150 | ||
| 151 | end; | |
| 152 |