69227
|
1 |
{- generated by Isabelle -}
|
69226
|
2 |
|
69225
|
3 |
{- Title: Tools/Haskell/YXML.hs
|
|
4 |
Author: Makarius
|
|
5 |
LICENSE: BSD 3-clause (Isabelle)
|
|
6 |
|
|
7 |
Efficient text representation of XML trees. Suitable for direct
|
|
8 |
inlining into plain text.
|
|
9 |
-}
|
|
10 |
|
|
11 |
|
|
12 |
module Isabelle.YXML (charX, charY, strX, strY, detect,
|
|
13 |
buffer_body, buffer, string_of_body, string_of, parse_body, parse)
|
|
14 |
where
|
|
15 |
|
|
16 |
import qualified Data.Char as Char
|
|
17 |
import qualified Data.List as List
|
|
18 |
|
|
19 |
import Isabelle.Library
|
|
20 |
import qualified Isabelle.Markup as Markup
|
|
21 |
import qualified Isabelle.XML as XML
|
|
22 |
import qualified Isabelle.Buffer as Buffer
|
|
23 |
|
|
24 |
|
|
25 |
{- markers -}
|
|
26 |
|
|
27 |
charX, charY :: Char
|
|
28 |
charX = Char.chr 5
|
|
29 |
charY = Char.chr 6
|
|
30 |
|
|
31 |
strX, strY, strXY, strXYX :: String
|
|
32 |
strX = [charX]
|
|
33 |
strY = [charY]
|
|
34 |
strXY = strX ++ strY
|
|
35 |
strXYX = strXY ++ strX
|
|
36 |
|
|
37 |
detect :: String -> Bool
|
|
38 |
detect = any (\c -> c == charX || c == charY)
|
|
39 |
|
|
40 |
|
|
41 |
{- output -}
|
|
42 |
|
|
43 |
buffer_attrib (a, x) =
|
|
44 |
Buffer.add strY #> Buffer.add a #> Buffer.add "=" #> Buffer.add x
|
|
45 |
|
|
46 |
buffer_body :: XML.Body -> Buffer.T -> Buffer.T
|
|
47 |
buffer_body = fold buffer
|
|
48 |
|
|
49 |
buffer :: XML.Tree -> Buffer.T -> Buffer.T
|
|
50 |
buffer (XML.Elem (name, atts) ts) =
|
|
51 |
Buffer.add strXY #> Buffer.add name #> fold buffer_attrib atts #> Buffer.add strX #>
|
|
52 |
buffer_body ts #>
|
|
53 |
Buffer.add strXYX
|
|
54 |
buffer (XML.Text s) = Buffer.add s
|
|
55 |
|
|
56 |
string_of_body :: XML.Body -> String
|
|
57 |
string_of_body body = Buffer.empty |> buffer_body body |> Buffer.content
|
|
58 |
|
|
59 |
string_of :: XML.Tree -> String
|
|
60 |
string_of = string_of_body . single
|
|
61 |
|
|
62 |
|
|
63 |
{- parse -}
|
|
64 |
|
|
65 |
-- split: fields or non-empty tokens
|
|
66 |
|
|
67 |
split :: Bool -> Char -> String -> [String]
|
|
68 |
split _ _ [] = []
|
|
69 |
split fields sep str = splitting str
|
|
70 |
where
|
|
71 |
splitting rest =
|
|
72 |
case span (/= sep) rest of
|
|
73 |
(_, []) -> cons rest []
|
|
74 |
(prfx, _ : rest') -> cons prfx (splitting rest')
|
|
75 |
cons item = if fields || not (null item) then (:) item else id
|
|
76 |
|
|
77 |
|
|
78 |
-- structural errors
|
|
79 |
|
|
80 |
err msg = error ("Malformed YXML: " ++ msg)
|
|
81 |
err_attribute = err "bad attribute"
|
|
82 |
err_element = err "bad element"
|
|
83 |
err_unbalanced "" = err "unbalanced element"
|
|
84 |
err_unbalanced name = err ("unbalanced element " ++ quote name)
|
|
85 |
|
|
86 |
|
|
87 |
-- stack operations
|
|
88 |
|
|
89 |
add x ((elem, body) : pending) = (elem, x : body) : pending
|
|
90 |
|
|
91 |
push "" _ _ = err_element
|
|
92 |
push name atts pending = ((name, atts), []) : pending
|
|
93 |
|
|
94 |
pop ((("", _), _) : _) = err_unbalanced ""
|
|
95 |
pop ((markup, body) : pending) = add (XML.Elem markup (reverse body)) pending
|
|
96 |
|
|
97 |
|
|
98 |
-- parsing
|
|
99 |
|
|
100 |
parse_attrib s =
|
|
101 |
case List.elemIndex '=' s of
|
|
102 |
Just i | i > 0 -> (take i s, drop (i + 1) s)
|
|
103 |
_ -> err_attribute
|
|
104 |
|
|
105 |
parse_chunk ["", ""] = pop
|
|
106 |
parse_chunk ("" : name : atts) = push name (map parse_attrib atts)
|
|
107 |
parse_chunk txts = fold (add . XML.Text) txts
|
|
108 |
|
|
109 |
parse_body :: String -> XML.Body
|
|
110 |
parse_body source =
|
|
111 |
case fold parse_chunk chunks [(("", []), [])] of
|
|
112 |
[(("", _), result)] -> reverse result
|
|
113 |
((name, _), _) : _ -> err_unbalanced name
|
|
114 |
where chunks = split False charX source |> map (split True charY)
|
|
115 |
|
|
116 |
parse :: String -> XML.Tree
|
|
117 |
parse source =
|
|
118 |
case parse_body source of
|
|
119 |
[result] -> result
|
|
120 |
[] -> XML.Text ""
|
|
121 |
_ -> err "multiple results"
|