-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.ml
582 lines (464 loc) · 17.4 KB
/
types.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
let language_version = (0, 0, 3)
module Name : sig
type t
val pp : Format.formatter -> t -> unit
val compare : t -> t -> int
val from_snake_case : string -> t option
val to_snake_case : t -> string
val to_lower_camel_case : t -> string
val to_upper_camel_case : t -> string
val bool : t
val int : t
val string : t
val list : t
val option : t
end = struct
include NameScheme
let make_exn s =
match from_snake_case s with
| Some(name) -> name
| None -> assert false
let int = make_exn "int"
let bool = make_exn "bool"
let string = make_exn "string"
let list = make_exn "list"
let option = make_exn "option"
end
module Variable : sig
type t
val pp : Format.formatter -> t -> unit
val compare : t -> t -> int
val from_snake_case : string -> t option
val to_snake_case : t -> string
val to_lower_camel_case : t -> string
val to_upper_camel_case : t -> string
end = struct
include NameScheme
end
module Key : sig
type t
val pp : Format.formatter -> t -> unit
val compare : t -> t -> int
val from_snake_case : string -> t option
val to_snake_case : t -> string
val to_lower_camel_case : t -> string
val to_upper_camel_case : t -> string
end = struct
include NameScheme
end
module Constructor : sig
type t
val pp : Format.formatter -> t -> unit
val compare : t -> t -> int
val from_upper_camel_case : string -> t option
val to_snake_case : t -> string
val to_upper_camel_case : t -> string
val none : t
val some : t
end = struct
include NameScheme
let none =
match from_upper_camel_case "None" with
| Some(c) -> c | None -> assert false
let some =
match from_upper_camel_case "Some" with
| Some(c) -> c | None -> assert false
end
module CommonConstant : sig
val label_field : string
val arg_field : string
val key_for_json : Key.t -> string
val label_for_json : Constructor.t -> string
end = struct
let label_field = "_label"
let arg_field = "_arg"
let key_for_json = Key.to_snake_case
let label_for_json = Constructor.to_upper_camel_case
end
type 'a ranged = Range.t * 'a
[@@deriving show { with_path = false; }]
type constructor = Constructor.t
[@@deriving show { with_path = false; }]
type parsed_name = string
[@@deriving show { with_path = false; }]
type parsed_variable = string
[@@deriving show { with_path = false; }]
type parsed_key = string
[@@deriving show { with_path = false; }]
type parsed_constructor = string
[@@deriving show { with_path = false; }]
type meta_value =
| VString of string
| VList of (meta_value ranged) list
type parsed_dictionary =
((string ranged * meta_value ranged) list) ranged
type parsed_message =
| PVariable of parsed_variable ranged
| PName of string ranged * parsed_message list
[@@deriving show { with_path = false; }]
type parsed_variant = (parsed_constructor ranged * parsed_message option) list
[@@deriving show { with_path = false; }]
type parsed_record = (parsed_key ranged * parsed_message) list
[@@deriving show { with_path = false; }]
type parsed_external = (string ranged * parsed_dictionary) list
type built_in =
| BBool
| BInt
| BString
| BList of string
| BOption of string
type parsed_definition_main =
| PBuiltIn of built_in
| PGivenNormal of parsed_message
| PGivenVariant of parsed_variant
| PGivenRecord of parsed_record
| PGivenExternal of parsed_external
type parsed_definition = {
pdef_params : (Range.t * parsed_variable) list;
pdef_main : parsed_definition_main;
}
type parsed_declarations = (parsed_name ranged * parsed_definition) list
type meta_spec =
| MetaOutput of string ranged * parsed_dictionary
| MetaLanguageVersion of string ranged
module Dict = Map.Make(String)
type dictionary = ((meta_value ranged) Dict.t) ranged
type top_level = meta_spec list * parsed_declarations
module RecordMap = Map.Make(Key)
let pp_record_map pp ppf rcdmap =
Format.fprintf ppf "@[<hov2>*R{@ ";
rcdmap |> RecordMap.iter (fun k v ->
Format.fprintf ppf "%a ->@ @[<hov2>%a@],@ " Key.pp k pp v
);
Format.fprintf ppf "@]}"
module VariantMap = Map.Make(Constructor)
let pp_variant_map pp ppf variant =
Format.fprintf ppf "@[<hov2>*V(@ ";
variant |> VariantMap.iter (fun ctor vopt ->
match vopt with
| None -> Format.fprintf ppf "%a,@ " Constructor.pp ctor
| Some(v) -> Format.fprintf ppf "%a@ ->@ @[%a@],@ " Constructor.pp ctor pp v
);
Format.fprintf ppf "@])"
type message =
| Variable of Variable.t ranged
| Name of Name.t ranged * message list
[@@deriving show { with_path = false; }]
type variant = (message option) VariantMap.t
[@printer (pp_variant_map pp_message)]
type record = message RecordMap.t
[@printer (pp_record_map pp_message)]
module ExternalMap = Map.Make(String)
type extern = dictionary ExternalMap.t
module DeclMap = Map.Make(Name)
type definition_main =
| BuiltIn of built_in
| GivenNormal of message
| GivenVariant of variant
| GivenRecord of record
| GivenExternal of extern
type definition = {
def_params : (Range.t * Variable.t) list;
def_main : definition_main;
}
type declarations = definition DeclMap.t
type version = int * int * int
[@@deriving show { with_path = false; }]
type error =
| NoLanguageVersionFound
| MalformedVersionString of string
| IncompatibleVersion of { providing : version; got : version }
| LexingInvalidCharacter of { character : char; range : Range.t; }
| EndOfLineInsideStringLiteral of { start : Range.t; }
| EndOfLineInsideComment of { start : Range.t; }
| UnknownMeta of string
| MandatoryKeyNotFound of { range : Range.t; key : string; }
| ParseErrorDetected of { range : Range.t; }
| KeySpecifiedMoreThanOnce of { range : Range.t; key : string; }
| MalformedName of { raw : string; range : Range.t; }
| MalformedVariable of { raw : string; range : Range.t; }
| MalformedKey of { raw : string; range : Range.t; }
| MalformedConstructor of { raw : string; range : Range.t; }
| UnsupportedTarget of { target : string; }
| MessageNameDefinedMoreThanOnce of { name : Name.t; range : Range.t; }
| FieldDefinedMoreThanOnce of { key : Key.t; range : Range.t; }
| ConstructorDefinedMoreThanOnce of { constructor : Constructor.t; range : Range.t; }
| UndefinedMessageName of { name : Name.t; }
| UndefinedVariable of { variable : Variable.t; range : Range.t; }
| VariableBoundMoreThanOnce of { variable : Variable.t; }
| InvalidMessageNameApplication of {
name : Name.t;
expected_arity : int;
actual_arity : int;
range : Range.t;
}
| ExternalDefinedMoreThanOnce of { key : string; range : Range.t; }
| NoExternal of { format : string; name : Name.t; }
| NoParameterAllowedForExternal
| NotAStringValue of Range.t
| NotAListValue of Range.t
[@@deriving show { with_path = false; }]
module ResultMonad : sig
val ( >>= ) : ('a, error) result -> ('a -> ('b, error) result) -> ('b, error) result
val error : error -> ('a, error) result
val return : 'a -> ('a, error) result
end = struct
let ( >>= ) res f =
match res with
| Ok(v) -> f v
| Error(_) as err -> err
let error e =
Error(e)
let return v =
Ok(v)
end
module Alist : sig
type 'a t
val empty : 'a t
val extend : 'a t -> 'a -> 'a t
val append : 'a t -> 'a list -> 'a t
val to_list : 'a t -> 'a list
end = struct
type 'a t = 'a list
let empty = []
let extend (acc : 'a t) (v : 'a) = v :: acc
let append (acc : 'a t) (vs : 'a list) = List.rev_append vs acc
let to_list = List.rev
end
let make_name ((rng, lower) : string ranged) : (Name.t ranged, error) result =
let open ResultMonad in
match Name.from_snake_case lower with
| None -> error (MalformedName{ raw = lower; range = rng; })
| Some(name) -> return (rng, name)
let make_variable ((rng, lower) : parsed_variable ranged) : (Variable.t ranged, error) result =
let open ResultMonad in
match Variable.from_snake_case lower with
| None -> error (MalformedVariable{ raw = lower; range = rng; })
| Some(x) -> return (rng, x)
let make_key ((rng, lower) : string ranged) : (Key.t ranged, error) result =
let open ResultMonad in
match Key.from_snake_case lower with
| None -> error (MalformedKey{ raw = lower; range = rng; })
| Some(key) -> return (rng, key)
let make_constructor ((rng, sctor) : string ranged) : (Constructor.t ranged, error) result =
let open ResultMonad in
match Constructor.from_upper_camel_case sctor with
| None -> error (MalformedConstructor{ raw = sctor; range = rng; })
| Some(ctor) -> return (rng, ctor)
let normalize_dictionary ((rngd, pfields) : parsed_dictionary) : (dictionary, error) result =
let open ResultMonad in
pfields |> List.fold_left (fun res ((_, k), rv) ->
res >>= fun dict ->
if dict |> Dict.mem k then
error (KeySpecifiedMoreThanOnce{ range = rngd; key = k; })
else
return (dict |> Dict.add k rv)
) (return Dict.empty) >>= fun dict ->
return (rngd, dict)
let rec normalize_message (pmsg : parsed_message) : (message, error) result =
let open ResultMonad in
match pmsg with
| PVariable(rlower) ->
make_variable rlower >>= fun rx ->
return (Variable(rx))
| PName(rlower, pargs) ->
make_name rlower >>= fun rname ->
pargs |> List.fold_left (fun res argpmsg ->
res >>= fun acc ->
normalize_message argpmsg >>= fun argmsg ->
return (Alist.extend acc argmsg)
) (return Alist.empty) >>= fun acc ->
return (Name(rname, acc |> Alist.to_list))
let normalize_variant (pvariant : parsed_variant) : (variant, error) result =
let open ResultMonad in
pvariant |> List.fold_left (fun res (rupper, pmsgsubopt) ->
res >>= fun variantmap ->
make_constructor rupper >>= fun (rng, ctor) ->
if variantmap |> VariantMap.mem ctor then
error (ConstructorDefinedMoreThanOnce{ constructor = ctor; range = rng; })
else
begin
match pmsgsubopt with
| None -> return None
| Some(pmsgsub) -> normalize_message pmsgsub >>= fun msgsub -> return (Some(msgsub))
end >>= fun msgsubopt ->
return (variantmap |> VariantMap.add ctor msgsubopt)
) (return VariantMap.empty) >>= fun variantmap ->
return variantmap
let normalize_record (prcd : parsed_record) : (record, error) result =
let open ResultMonad in
prcd |> List.fold_left (fun res (rlower, pmsgsub) ->
res >>= fun rcdmap ->
make_key rlower >>= fun (rng, key) ->
if rcdmap |> RecordMap.mem key then
error (FieldDefinedMoreThanOnce{ key = key; range = rng; })
else
normalize_message pmsgsub >>= fun msgsub ->
return (rcdmap |> RecordMap.add key msgsub)
) (return RecordMap.empty) >>= fun rcdmap ->
return rcdmap
let normalize_external (pextern : parsed_external) : (extern, error) result =
let open ResultMonad in
pextern |> List.fold_left (fun res ((rng, format), pdict) ->
res >>= fun extmap ->
if extmap |> ExternalMap.mem format then
error (ExternalDefinedMoreThanOnce{ key = format; range = rng; })
else
normalize_dictionary pdict >>= fun dict ->
return (extmap |> ExternalMap.add format dict)
) (return ExternalMap.empty)
let normalize_declarations (pdecls : parsed_declarations) : (declarations, error) result =
let open ResultMonad in
pdecls |> List.fold_left (fun res (rlower, pdef) ->
res >>= fun declmap ->
make_name rlower >>= fun (rng, name) ->
if declmap |> DeclMap.mem name then
error (MessageNameDefinedMoreThanOnce{ name = name; range = rng; })
else
begin
match pdef.pdef_main with
| PBuiltIn(info) ->
return (BuiltIn(info))
| PGivenNormal(pmsg) ->
normalize_message pmsg >>= fun msg ->
return (GivenNormal(msg))
| PGivenVariant(pvariant) ->
normalize_variant pvariant >>= fun variant ->
return (GivenVariant(variant))
| PGivenRecord(precord) ->
normalize_record precord >>= fun record ->
return (GivenRecord(record))
| PGivenExternal(pextern) ->
normalize_external pextern >>= fun extern ->
return (GivenExternal(extern))
end >>= fun defmain ->
pdef.pdef_params |> List.fold_left (fun res rlower ->
res >>= fun paramacc ->
make_variable rlower >>= fun rx ->
return (Alist.extend paramacc rx)
) (return Alist.empty) >>= fun paramacc ->
let def = { def_params = Alist.to_list paramacc; def_main = defmain } in
return (declmap |> DeclMap.add name def)
) (return DeclMap.empty)
let rec validate_message (find_arity : Name.t -> int option) (is_defined_variable : Variable.t -> bool) (msg : message) : (unit, error) result =
let iter = validate_message find_arity is_defined_variable in
let open ResultMonad in
match msg with
| Variable((rng, x)) ->
if is_defined_variable x then
return ()
else
error (UndefinedVariable{ variable = x; range = rng; })
| Name((rng, name), args) ->
begin
match find_arity name with
| Some(n_expected) ->
let n_actual = List.length args in
if n_actual == n_expected then
args |> List.fold_left (fun res argmsg ->
res >>= fun () ->
iter argmsg
) (return ())
else
error (InvalidMessageNameApplication{
name = name;
expected_arity = n_expected;
actual_arity = n_actual;
range = rng;
})
| None ->
error (UndefinedMessageName{ name = name; })
end
let validate_variant (find_arity : Name.t -> int option) (is_defined_variable : Variable.t -> bool) (variant : variant) : (unit, error) result =
let open ResultMonad in
VariantMap.fold (fun _ctor argmsgopt res ->
res >>= fun () ->
match argmsgopt with
| None -> return ()
| Some(argmsg) -> validate_message find_arity is_defined_variable argmsg
) variant (return ())
let validate_record (find_arity : Name.t -> int option) (is_defined_variable : Variable.t -> bool) (record : record) : (unit, error) result =
let open ResultMonad in
RecordMap.fold (fun _key vmsg res ->
res >>= fun () ->
validate_message find_arity is_defined_variable vmsg
) record (return ())
module ParamSet = Set.Make(Variable)
let validate_parameters (params : (Variable.t ranged) list) : (ParamSet.t, error) result =
let open ResultMonad in
params |> List.fold_left (fun res (_, x) ->
res >>= fun set ->
if set |> ParamSet.mem x then
error (VariableBoundMoreThanOnce{ variable = x; })
else
return (set |> ParamSet.add x)
) (return ParamSet.empty)
let validate_declarations (decls : declarations) : (unit, error) result =
let find_arity name =
match decls |> DeclMap.find_opt name with
| None -> None
| Some(def) -> Some(List.length def.def_params)
in
let open ResultMonad in
DeclMap.fold (fun _ def res ->
res >>= fun () ->
validate_parameters def.def_params >>= fun set ->
let is_defined_variable v = set |> ParamSet.mem v in
match def.def_main with
| BuiltIn(_) ->
return ()
| GivenNormal(msg) ->
validate_message find_arity is_defined_variable msg
| GivenVariant(variant) ->
validate_variant find_arity is_defined_variable variant
| GivenRecord(record) ->
validate_record find_arity is_defined_variable record
| GivenExternal(_) ->
return ()
) decls (return ())
let built_in_declarations : parsed_declarations =
let ( !@ ) t = (Range.dummy t, t) in
let ( ==> ) (x : string ranged) ((params, info) : (parsed_variable ranged) list * built_in) =
(x, { pdef_params = params; pdef_main = PBuiltIn(info); })
in
[
!@ "bool" ==> ([], BBool);
!@ "int" ==> ([], BInt);
!@ "string" ==> ([], BString);
!@ "option" ==> ([(Range.dummy "option", "v")], BOption("v"));
!@ "list" ==> ([(Range.dummy "list", "v")], BList("v"));
]
let get_value ((_, dict)) key =
dict |> Dict.find_opt key
let get_mandatory_value ((rngd, dict) : dictionary) (key : string) : (meta_value ranged, error) result =
let open ResultMonad in
match dict |> Dict.find_opt key with
| None -> error (MandatoryKeyNotFound{ range = rngd; key = key; })
| Some(rv) -> return rv
let validate_string_value ((rng, mv) : meta_value ranged) : (string, error) result =
let open ResultMonad in
match mv with
| VString(s) -> return s
| _ -> error (NotAStringValue(rng))
let get_mandatory_string (rdict : dictionary) (key : string) : (string, error) result =
let open ResultMonad in
get_mandatory_value rdict key >>= fun rmv ->
validate_string_value rmv
let get_list validatef (rdict : dictionary) (key : string) default =
let open ResultMonad in
match get_value rdict key with
| None ->
return default
| Some((rng, mv)) ->
begin
match mv with
| VList(rmvs) ->
rmvs |> List.fold_left (fun res rmv ->
res >>= fun acc ->
validatef rmv >>= fun v ->
return (Alist.extend acc v)
) (return Alist.empty) >>= fun acc ->
return (acc |> Alist.to_list)
| _ ->
error (NotAListValue(rng))
end