| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
open Xml_datatype |
| 12 |
|
| 13 |
exception Marshal_error of string * xml |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
let rec get_attr attr = function |
| 18 |
| [] -> raise Not_found |
| 19 |
| (k, v) :: l when CString.equal k attr -> v |
| 20 |
| _ :: l -> get_attr attr l |
| 21 |
|
| 22 |
let massoc x l = |
| 23 |
try get_attr x l |
| 24 |
with Not_found -> raise (Marshal_error("attribute " ^ x,PCData "not there")) |
| 25 |
|
| 26 |
let constructor t c args = Element (t, ["val", c], args) |
| 27 |
let do_match t mf = function |
| 28 |
| Element (s, attrs, args) when CString.equal s t -> |
| 29 |
let c = massoc "val" attrs in |
| 30 |
mf c args |
| 31 |
| x -> raise (Marshal_error (t,x)) |
| 32 |
|
| 33 |
let singleton = function |
| 34 |
| [x] -> x |
| 35 |
| l -> raise (Marshal_error |
| 36 |
("singleton",PCData ("list of length " ^ string_of_int (List.length l)))) |
| 37 |
|
| 38 |
let empty = function |
| 39 |
| [] -> () |
| 40 |
| l -> raise (Marshal_error |
| 41 |
("empty",PCData ("list of length " ^ string_of_int (List.length l)))) |
| 42 |
|
| 43 |
let raw_string = function |
| 44 |
| [] -> "" |
| 45 |
| [PCData s] -> s |
| 46 |
| x::_ -> raise (Marshal_error("raw string",x)) |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
let of_unit () = Element ("unit", [], []) |
| 51 |
let to_unit : xml -> unit = function |
| 52 |
| Element ("unit", [], []) -> () |
| 53 |
| x -> raise (Marshal_error ("unit",x)) |
| 54 |
|
| 55 |
let of_bool (b : bool) : xml = |
| 56 |
if b then constructor "bool" "true" [] |
| 57 |
else constructor "bool" "false" [] |
| 58 |
let to_bool : xml -> bool = do_match "bool" (fun s _ -> match s with |
| 59 |
| "true" -> true |
| 60 |
| "false" -> false |
| 61 |
| x -> raise (Marshal_error("bool",PCData x))) |
| 62 |
|
| 63 |
let of_list (f : 'a -> xml) (l : 'a list) = |
| 64 |
Element ("list", [], List.map f l) |
| 65 |
let to_list (f : xml -> 'a) : xml -> 'a list = function |
| 66 |
| Element ("list", [], l) -> List.map f l |
| 67 |
| x -> raise (Marshal_error("list",x)) |
| 68 |
|
| 69 |
let of_option (f : 'a -> xml) : 'a option -> xml = function |
| 70 |
| None -> Element ("option", ["val", "none"], []) |
| 71 |
| Some x -> Element ("option", ["val", "some"], [f x]) |
| 72 |
let to_option (f : xml -> 'a) : xml -> 'a option = function |
| 73 |
| Element ("option", ["val", "none"], []) -> None |
| 74 |
| Element ("option", ["val", "some"], [x]) -> Some (f x) |
| 75 |
| x -> raise (Marshal_error("option",x)) |
| 76 |
|
| 77 |
let of_string (s : string) : xml = Element ("string", [], [PCData s]) |
| 78 |
let to_string : xml -> string = function |
| 79 |
| Element ("string", [], l) -> raw_string l |
| 80 |
| x -> raise (Marshal_error("string",x)) |
| 81 |
|
| 82 |
let of_int (i : int) : xml = Element ("int", [], [PCData (string_of_int i)]) |
| 83 |
let to_int : xml -> int = function |
| 84 |
| Element ("int", [], [PCData s]) -> |
| 85 |
(try int_of_string s with Failure _ -> raise(Marshal_error("int",PCData s))) |
| 86 |
| x -> raise (Marshal_error("int",x)) |
| 87 |
|
| 88 |
let of_pair (f : 'a -> xml) (g : 'b -> xml) (x : 'a * 'b) : xml = |
| 89 |
Element ("pair", [], [f (fst x); g (snd x)]) |
| 90 |
let to_pair (f : xml -> 'a) (g : xml -> 'b) : xml -> 'a * 'b = function |
| 91 |
| Element ("pair", [], [x; y]) -> (f x, g y) |
| 92 |
| x -> raise (Marshal_error("pair",x)) |
| 93 |
|
| 94 |
let of_union (f : 'a -> xml) (g : 'b -> xml) : ('a,'b) CSig.union -> xml = function |
| 95 |
| CSig.Inl x -> Element ("union", ["val","in_l"], [f x]) |
| 96 |
| CSig.Inr x -> Element ("union", ["val","in_r"], [g x]) |
| 97 |
let to_union (f : xml -> 'a) (g : xml -> 'b) : xml -> ('a,'b) CSig.union = function |
| 98 |
| Element ("union", ["val","in_l"], [x]) -> CSig.Inl (f x) |
| 99 |
| Element ("union", ["val","in_r"], [x]) -> CSig.Inr (g x) |
| 100 |
| x -> raise (Marshal_error("union",x)) |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
let of_edit_id i = Element ("edit_id",["val",string_of_int i],[]) |
| 105 |
let to_edit_id = function |
| 106 |
| Element ("edit_id",["val",i],[]) -> |
| 107 |
let id = int_of_string i in |
| 108 |
assert (id <= 0 ); |
| 109 |
id |
| 110 |
| x -> raise (Marshal_error("edit_id",x)) |
| 111 |
|
| 112 |
let of_loc loc = |
| 113 |
let { |
| 114 |
Loc.fname |
| 115 |
; line_nb |
| 116 |
; bol_pos |
| 117 |
; line_nb_last |
| 118 |
; bol_pos_last |
| 119 |
; bp |
| 120 |
; ep |
| 121 |
} = loc in |
| 122 |
let fname = match fname with |
| 123 |
| ToplevelInput -> [] |
| 124 |
| InFile { dirpath = None; file } -> [ ("file", file) ] |
| 125 |
| InFile { dirpath = Some dir; file } -> [ ("dirpath", dir); ("file", file) ] in |
| 126 |
Element ("loc", |
| 127 |
fname @ |
| 128 |
[ ("line_nb", string_of_int line_nb) |
| 129 |
; ("bol_pos", string_of_int bol_pos) |
| 130 |
; ("line_nb_last", string_of_int line_nb_last) |
| 131 |
; ("bol_pos_last", string_of_int bol_pos_last) |
| 132 |
; ("start", string_of_int bp) |
| 133 |
; ("stop", string_of_int ep ) |
| 134 |
], |
| 135 |
[]) |
| 136 |
|
| 137 |
let to_loc xml = |
| 138 |
match xml with |
| 139 |
| Element ("loc", l,[]) as x -> |
| 140 |
(try |
| 141 |
let fname = |
| 142 |
let dirpath = try Some (get_attr "dirpath" l) with Not_found -> None in |
| 143 |
let file = try Some (get_attr "file" l) with Not_found -> None in |
| 144 |
match dirpath, file with |
| 145 |
| None, None -> Loc.ToplevelInput |
| 146 |
| dirpath, Some file -> Loc.InFile { dirpath; file } |
| 147 |
| Some _, None -> raise (Marshal_error("loc",x)) in |
| 148 |
let line_nb = massoc "line_nb" l |> int_of_string in |
| 149 |
let bol_pos = massoc "bol_pos" l |> int_of_string in |
| 150 |
let line_nb_last = massoc "line_nb_last" l |> int_of_string in |
| 151 |
let bol_pos_last = massoc "bol_pos_last" l |> int_of_string in |
| 152 |
let bp = massoc "start" l |> int_of_string in |
| 153 |
let ep = massoc "stop" l |> int_of_string in |
| 154 |
{ Loc.fname |
| 155 |
; line_nb |
| 156 |
; bol_pos |
| 157 |
; line_nb_last |
| 158 |
; bol_pos_last |
| 159 |
; bp |
| 160 |
; ep } |
| 161 |
with Not_found | Invalid_argument _ -> |
| 162 |
raise (Marshal_error("loc",x))) |
| 163 |
| x -> raise (Marshal_error("loc",x)) |
| 164 |
|
| 165 |
let of_xml x = Element ("xml", [], [x]) |
| 166 |
let to_xml xml = match xml with |
| 167 |
| Element ("xml", [], [x]) -> x |
| 168 |
| x -> raise (Marshal_error("xml",x)) |