Debian Codegraph @

apps/xml/src/rocq_elpi_xml_plugin.ml

raw symbols SCIP
1 open Elpi.API
2
3 (* todo, move this in Elpi, there are already two copies of this code
4 in rocq-elpi *)
5 let listC (d : ('a,'h,'cst) ContextualConversion.t) =
6 let embed ~depth h cst s l =
7 let s, l, eg = Utils.map_acc (d.ContextualConversion.embed ~depth h cst ) s l in
8 s, Utils.list_to_lp_list l, eg in
9 let readback ~depth h cst s t =
10 Utils.map_acc (d.ContextualConversion.readback ~depth h cst) s
11 (Utils.lp_list_to_list ~depth t)
12 in
13 let pp fmt l =
14 Format.fprintf fmt "[%a]" (RawPp.list d.ContextualConversion.pp ~boxed:true "; ") l in
15 { ContextualConversion.embed; readback;
16 ty = TyApp ("list",d.ContextualConversion.ty,[]);
17 pp;
18 pp_doc = (fun fmt () -> ()) }
19
20
21 open BuiltInData
22 open Elpi.Builtin
23
24 let xml : Xml.xml Conversion.t = AlgebraicData.(declare {
25 ty = Conversion.TyName "xml";
26 doc = "Xml.xml";
27 pp = (fun fmt x -> Format.fprintf fmt "%s" (Xml.to_string_fmt x));
28 constructors = [
29 K("xml.element","Attributes are sorted by key",A(string,A(list (pair string string),C(listC,N))),
30 B (fun tag attrs bo -> Element(tag,attrs,bo)),
31 M (fun ~ok ~ko -> function Element(t,a,b) ->
32 let a = List.stable_sort (fun (x,_) (y,_) -> String.compare x y) a in
33 ok t a b | _ -> ko ()));
34 K("xml.pcdata","",A(string,N),B (fun x -> PCData x),M (fun ~ok ~ko -> function Xml.PCData s -> ok s | _ -> ko ()));
35 ];
36 }
37 ) |> ContextualConversion.(!<)
38
39 open BuiltIn
40 open BuiltInPredicate
41 open BuiltInPredicate.Notation
42 let xml_parse_file = MLCode(Pred("xml.parse_file",
43 In(string,"File",
44 Out(xml,"XML",
45 Out(diagnostic,"D",
46 Easy {|like Xml.parse_file reads XML from File.
47 If D is error then XML is not assigned.|}))),
48 (fun f _ _ ~depth ->
49 try !: (Xml.parse_file f) +! mkOK
50 with
51 | Xml.Error e -> ?: None +! (mkERROR (Xml.error e))
52 | Xml.File_not_found s | Xml_light_errors.File_not_found s -> ?: None +! (mkERROR s)
53 )),DocAbove)
54
55 let xml_parse_string = MLCode(Pred("xml.parse_string",
56 In(string,"String",
57 Out(xml,"XML",
58 Out(diagnostic,"D",
59 Easy {|like Xml.parse_string reads XML from String.
60 If D is error then XML is not assigned.|}))),
61 (fun f _ _ ~depth ->
62 try !: (Xml.parse_string f) +! mkOK
63 with Xml.Error e | Xml_light_errors.Xml_error e -> ?: None +! (mkERROR (Xml.error e))
64 )),DocAbove)
65
66 let xml_to_string = MLCode(Pred("xml.to_string",
67 In(xml,"XML",
68 Out(string,"S",
69 Out(diagnostic,"D",
70 Easy {|like Xml.to_string write XML to a compact string S.|}))),
71 (fun j _ _ ~depth ->
72 !: (Xml.to_string j) +! mkOK
73 )),DocAbove)
74
75 let xml_to_string_fmt = MLCode(Pred("xml.to_string_fmt",
76 In(xml,"XML",
77 Out(string,"S",
78 Out(diagnostic,"D",
79 Easy {|like Xml.to_string_fmt write XML to a nice string S.|}))),
80 (fun j _ _ ~depth ->
81 !: (Xml.to_string_fmt j) +! mkOK
82 )),DocAbove)
83
84 let builtins =
85 declare ~file_name:"xml.elpi" [
86 LPDoc {|Xml-light bindings for Elpi|};
87 LPDoc {|license: GNU Lesser General Public License Version 2.1 or later|};
88 LPDoc {|------------------------------------------------------------------------|};
89 LPDoc {|This code is not Rocq specific, i.e. could go to an Elpi specific package.|};
90 MLData xml;
91 xml_parse_file;
92 xml_parse_string;
93 xml_to_string;
94 xml_to_string_fmt;
95 LPDoc {|------------------------------------------------------------------------|};
96 LPDoc {|Rocq-Elpi APIs|};
97 LPDoc {|------------------------------------------------------------------------|};
98 LPCode {|
99 func coq.xml->string xml -> string.
100 coq.xml->string XML S :- xml.to_string_fmt XML S ok.
101
102 func coq.string->xml string -> xml.
103 coq.string->xml S XML :- xml.parse_string S XML ok.
104
105 % [coq.extra-dep->xml Dep XML D] reads a xml file declared as an extra
106 % dependency, see the coq.extra-dep API.
107 func coq.extra-dep->xml string -> xml, diagnostic.
108 coq.extra-dep->xml S XML D :- coq.extra-dep S (some P), !, xml.parse_file P XML D.
109 coq.extra-dep->xml S _ (error Msg) :- Msg is "unknown Extra Dependency " ^ S.
110
111 |}
112 ]