package lablqml

  1. Overview
  2. Docs

Source file testdemo.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
open Ppxlib

let dummy_loc = Location.in_file "dummy.ml"

type stream_item = arg_label * expression

type 'a parse_result = Failed | Parsed of 'a * stream_item list

type 'a parser = stream_item list -> 'a parse_result

let return x xs = Parsed (x, xs)

let pnil : unit parser =
 fun xs -> match xs with [] -> return () [] | _ -> Failed

let myparse p onOK xs =
  Ast_pattern.parse p dummy_loc xs ~on_error:(fun () -> Failed) onOK

let args_end : unit parser =
 fun xs ->
  (*  Format.printf "calling args_end %s %d\n%!" __FILE__ __LINE__;*)
  Ast_pattern.parse Ast_pattern.nil dummy_loc xs
    ~on_error:(fun () -> Failed)
    (Parsed ((), []))

let singleton : unit parser =
  let open Ast_pattern in
  let p = (no_label @@ pexp_ident (lident (string "singleton"))) ^:: __ in
  myparse p (fun tl -> Parsed ((), tl))

let named_string name : string parser =
  let open Ast_pattern in
  let p =
    pair (labelled (string name)) (pexp_constant @@ pconst_string __ __ none)
    ^:: __
  in
  myparse p (fun name _ tl -> Parsed (name, tl))

let name = named_string "name"

let the_ident : string -> unit parser =
 fun s stream ->
  let open Ast_pattern in
  let p = (no_label @@ pexp_ident (lident (string s))) ^:: __ in
  myparse p (fun xs -> Parsed ((), xs)) stream

let pident : string parser =
  let open Ast_pattern in
  let p = (no_label @@ pexp_ident (lident __)) ^:: __ in
  myparse p (fun s xs -> Parsed (s, xs))

let ( >>= ) : 'a parser -> ('a -> 'b parser) -> 'b parser =
 fun p f xs -> match p xs with Failed -> Failed | Parsed (x, tl) -> f x tl

let many : 'a parser -> 'a list parser =
 fun p ->
  let rec helper acc input =
    match p input with
    | Failed -> return (List.rev acc) input
    | Parsed (x, tl) -> helper (x :: acc) tl
  in
  helper []

let ( >> ) : 'a parser -> 'b parser -> 'b parser = fun p q -> p >>= fun _ -> q

let ( <* ) : 'a parser -> 'b parser -> 'a parser =
 fun p q ->
  p >>= fun x ->
  q >>= fun _ -> return x

let ( <|> ) p q xs = match p xs with Failed -> q xs | r -> r

let choice ps input =
  let rec helper = function
    | [] -> Failed
    | h :: ps -> (
        match h input with
        | Failed ->
            (*          Format.printf "choiced parser failed: %s %d\n%!" __FILE__ __LINE__;*)
            helper ps
        | Parsed (r, tl) -> Parsed (r, tl))
  in
  helper ps

let wrap head parsef e ~onfail cond =
  match head e with
  | Failed ->
      Format.printf "head failed : %s %d\n%!" __FILE__ __LINE__;
      onfail
  | Parsed (head_result, xs) -> (
      (*      Format.printf "Ars length = %d. %s %d\n%!" (List.length xs) __FILE__ __LINE__;*)
      match parsef xs with
      | Failed ->
          (*        Format.printf "%s %d\n%!" __FILE__ __LINE__;*)
          onfail
      | Parsed (rez, []) ->
          (*          Format.printf "calling cond \n%!";*)
          cond head_result rez
      | Parsed (_, _) ->
          (*          Format.printf "%s %d\n%!" __FILE__ __LINE__;*)
          onfail)

let loc = dummy_loc

(* ******************************************************** *)
type prop_name = string [@@deriving show]

type prop_typ = string [@@deriving show]

type prop_info = {
  mutable p_read : string option;
  mutable p_write : string option;
  mutable p_notify : string option;
}
[@@deriving show]

let make_prop_info () = { p_read = None; p_write = None; p_notify = None }

let prop_head : expression -> (prop_name * prop_typ) parse_result =
 fun e ->
  let p =
    let open Ppxlib.Ast_pattern in
    pexp_apply
      (pexp_constraint (pexp_ident (lident __)) (ptyp_constr (lident __) nil))
      __
  in
  Ast_pattern.parse p dummy_loc e
    ~on_error:(fun () -> Failed)
    (fun name typ xs -> Parsed ((name, typ), xs))

let try_ p xs =
  match p xs with
  | Parsed (x, tl) -> return (Some x) tl
  | Failed -> return None xs

let parse_body : prop_info parser =
  let ptag_wrap name =
    let open Ast_pattern in
    let p =
      (no_label @@ pexp_construct (lident (string name)) none)
      ^:: (no_label @@ pexp_ident (lident __))
      ^:: __
    in
    myparse p (fun name tl -> Parsed (name, tl))
  in
  let pwrite = ptag_wrap "WRITE" in
  let pread = ptag_wrap "READ" in
  let pnotify = ptag_wrap "NOTIFY" in
  fun xs ->
    let rez = make_prop_info () in
    xs
    |> (many
        @@ choice
             [
               ( pwrite >>= fun s ->
                 rez.p_write <- Some s;
                 return () );
               ( pread >>= fun s ->
                 rez.p_read <- Some s;
                 return () );
               ( pnotify >>= fun s ->
                 rez.p_notify <- Some s;
                 return () );
             ]
       >> args_end >> return rez)

let wrap_prop e cond = wrap prop_head parse_body e cond

(* ************************************************************************** *)
type info = {
  mutable is_singleton : bool;
  mutable name : string option;
  mutable namespace : string option;
  mutable props : (prop_name * prop_typ * prop_info) list;
}
(* [@@deriving show] *)

let empty_info =
  { is_singleton = false; name = None; namespace = None; props = [] }

type prop_item = prop_name * prop_typ * prop_info [@@deriving show]

type data_item =
  | ISingleton of bool
  | IProp of prop_item
  | IName of string
  | INamespace of string
[@@deriving show]

(*let data_items = data_item list [@@deriving show]*)
let show_data_items xs = List.map show_data_item xs |> String.concat ","

let parse_props : data_item parser =
 fun stream ->
  match stream with
  | [] -> Failed
  | (_lab, h) :: other_exprs -> (
      let p = function
        | [] -> Failed
        | (_, x) :: _ -> (
            match prop_head x with
            | Failed -> Failed
            | Parsed ((_, _), []) ->
                (* not enough arguments for property description *)
                Failed
            | Parsed ((name, typ), other_prop_info) ->
                (parse_body >>= fun pinfo -> return (name, typ, pinfo))
                  other_prop_info)
      in
      match (p <* pnil) [ (Nolabel, h) ] with
      | Failed -> Failed
      | Parsed (p, []) -> return (IProp p) other_exprs
      | Parsed (_, _) -> assert false)

let all : info parser =
 fun xs ->
  xs
  |> ( many
     @@ choice
          [
            (named_string "name" >>= fun s -> return (IName s));
            (named_string "namespace" >>= fun s -> return (INamespace s));
            (singleton >>= fun () -> return (ISingleton true));
            parse_props;
          ]
     >>= fun extra ->
       args_end
       >> (*    Format.printf "%s\n%!" (show_data_items extra);*)
       return
       @@ List.fold_left
            (fun rez x ->
              match x with
              | IName s -> { rez with name = Some s }
              | INamespace s -> { rez with namespace = Some s }
              | ISingleton b -> { rez with is_singleton = b }
              | IProp p -> { rez with props = rez.props @ [ p ] })
            empty_info extra )

let qmlargs : expression -> unit parse_result =
  let p =
    let open Ppxlib.Ast_pattern in
    pexp_apply (pexp_ident (lident (string "qml"))) __
  in
  myparse p (fun xs -> Parsed ((), xs))

let singleton : expression -> unit parse_result =
  let p =
    let open Ppxlib.Ast_pattern in
    pexp_apply (pexp_ident (lident (string "singleton"))) __
  in
  fun xs ->
    let myOK _ =
      match xs.pexp_desc with
      | Pexp_apply (e, args) -> Parsed ((), (Nolabel, e) :: args)
      | _ -> failwith "should not happen"
    in
    myparse p myOK xs

let wrap_qml e cond = wrap qmlargs all e (fun () -> cond)

let wrap_singleton e cond = wrap singleton all e (fun () -> cond)

(*let%test _ =
  wrap_prop ~onfail:false
    [%expr (author: string) READ getAuthor ]
    (fun (name,typ) rez ->
      (name="author") && (typ="string") && (rez.p_read = Some "getAuthor")
      && (rez.p_notify = None) && (rez.p_write = None)
    )*)

(*let%test _ =
  wrap_prop ~onfail:false
    [%expr (author: string) READ getAuthor WRITE setAuthor NOTIFY authorChanged ]
    (fun (name,typ) rez ->
(*      Format.printf "name=%s, typ=%s, rez = %s\n%!" name typ (show_prop_info rez);*)
      (name="author")
      && (typ="string")
      && (rez.p_read   = Some "getAuthor")
      && (rez.p_write  = Some "setAuthor")
      && (rez.p_notify = Some "authorChanged")
    )*)

let%test _ =
  wrap_singleton ~onfail:false [%expr singleton ~name:"mySingleton"] (fun rez ->
      (*      Format.printf "rez = %s\n%!" (show_info  rez);*)
      rez.is_singleton && rez.name = Some "mySingleton")

let%test _ =
  wrap_singleton ~onfail:false
    [%expr
      singleton ~name:"qwe"
        ((author : string) READ getAuthor WRITE setAuthor NOTIFY authorChanged)
        ((event : int) READ event NOTIFY eventChanged)]
    (fun rez ->
      rez.name = Some "qwe"
      &&
      match rez.props with
      | [
       ( "author",
         "string",
         {
           p_read = Some "getAuthor";
           p_write = Some "setAuthor";
           p_notify = Some "authorChanged";
         } );
       ( "event",
         "int",
         {
           p_read = Some "event";
           p_write = None;
           p_notify = Some "eventChanged";
         } );
      ] ->
          true
      | _ -> false)

let%test _ =
  wrap_singleton ~onfail:false [%expr singleton ~name:"MyApi"] (fun rez ->
      (* print_endline @@ show_info rez; *)
      rez.is_singleton && rez.name = Some "MyApi" && [] = rez.props)

let parse_singleton e =
  wrap singleton all e ~onfail:None (fun () -> Base.Option.some)
OCaml

Innovation. Community. Security.