69240
|
1 |
{- generated by Isabelle -}
|
|
2 |
|
|
3 |
{- Title: Tools/Haskell/XML/Decode.hs
|
|
4 |
Author: Makarius
|
|
5 |
LICENSE: BSD 3-clause (Isabelle)
|
|
6 |
|
|
7 |
XML as data representation language.
|
69280
|
8 |
|
|
9 |
See also "$ISABELLE_HOME/src/Pure/PIDE/xml.ML".
|
69240
|
10 |
-}
|
|
11 |
|
|
12 |
module Isabelle.XML.Decode (
|
|
13 |
A, T, V,
|
|
14 |
|
|
15 |
int_atom, bool_atom, unit_atom,
|
|
16 |
|
|
17 |
tree, properties, string, init, bool, unit, pair, triple, list, variant
|
|
18 |
)
|
|
19 |
where
|
|
20 |
|
|
21 |
import Data.List ((!!))
|
|
22 |
|
|
23 |
import Isabelle.Library
|
|
24 |
import qualified Isabelle.Value as Value
|
|
25 |
import qualified Isabelle.Properties as Properties
|
|
26 |
import qualified Isabelle.XML as XML
|
|
27 |
|
|
28 |
|
|
29 |
type A a = String -> a
|
|
30 |
type T a = XML.Body -> a
|
|
31 |
type V a = ([String], XML.Body) -> a
|
|
32 |
|
|
33 |
err_atom = error "Malformed XML atom"
|
|
34 |
err_body = error "Malformed XML body"
|
|
35 |
|
|
36 |
|
|
37 |
{- atomic values -}
|
|
38 |
|
|
39 |
int_atom :: A Int
|
|
40 |
int_atom s =
|
|
41 |
case Value.parse_int s of
|
|
42 |
Just i -> i
|
|
43 |
Nothing -> err_atom
|
|
44 |
|
|
45 |
bool_atom :: A Bool
|
|
46 |
bool_atom "0" = False
|
|
47 |
bool_atom "1" = True
|
|
48 |
bool_atom _ = err_atom
|
|
49 |
|
|
50 |
unit_atom :: A ()
|
|
51 |
unit_atom "" = ()
|
|
52 |
unit_atom _ = err_atom
|
|
53 |
|
|
54 |
|
|
55 |
{- structural nodes -}
|
|
56 |
|
69290
|
57 |
node (XML.Elem ((":", []), ts)) = ts
|
69240
|
58 |
node _ = err_body
|
|
59 |
|
|
60 |
vector atts =
|
|
61 |
map_index (\(i, (a, x)) -> if int_atom a == i then x else err_atom) atts
|
|
62 |
|
69290
|
63 |
tagged (XML.Elem ((name, atts), ts)) = (int_atom name, (vector atts, ts))
|
69240
|
64 |
tagged _ = err_body
|
|
65 |
|
|
66 |
|
|
67 |
{- representation of standard types -}
|
|
68 |
|
|
69 |
tree :: T XML.Tree
|
|
70 |
tree [t] = t
|
|
71 |
tree _ = err_body
|
|
72 |
|
|
73 |
properties :: T Properties.T
|
69290
|
74 |
properties [XML.Elem ((":", props), [])] = props
|
69240
|
75 |
properties _ = err_body
|
|
76 |
|
|
77 |
string :: T String
|
|
78 |
string [] = ""
|
|
79 |
string [XML.Text s] = s
|
|
80 |
string _ = err_body
|
|
81 |
|
|
82 |
int :: T Int
|
|
83 |
int = int_atom . string
|
|
84 |
|
|
85 |
bool :: T Bool
|
|
86 |
bool = bool_atom . string
|
|
87 |
|
|
88 |
unit :: T ()
|
|
89 |
unit = unit_atom . string
|
|
90 |
|
|
91 |
pair :: T a -> T b -> T (a, b)
|
|
92 |
pair f g [t1, t2] = (f (node t1), g (node t2))
|
|
93 |
pair _ _ _ = err_body
|
|
94 |
|
|
95 |
triple :: T a -> T b -> T c -> T (a, b, c)
|
|
96 |
triple f g h [t1, t2, t3] = (f (node t1), g (node t2), h (node t3))
|
|
97 |
triple _ _ _ _ = err_body
|
|
98 |
|
|
99 |
list :: T a -> T [a]
|
|
100 |
list f ts = map (f . node) ts
|
|
101 |
|
|
102 |
option :: T a -> T (Maybe a)
|
|
103 |
option _ [] = Nothing
|
|
104 |
option f [t] = Just (f (node t))
|
|
105 |
option _ _ = err_body
|
|
106 |
|
|
107 |
variant :: [V a] -> T a
|
|
108 |
variant fs [t] = (fs !! tag) (xs, ts)
|
|
109 |
where (tag, (xs, ts)) = tagged t
|
|
110 |
variant _ _ = err_body
|