-
Notifications
You must be signed in to change notification settings - Fork 465
/
Copy pathNamedArgs.ml
50 lines (46 loc) · 1.77 KB
/
NamedArgs.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
open GenTypeCommon
type groupedArg = Group of fields | Arg of type_
(**
For convenient processing turns consecutive named arguments into a
`NamedArgs` group, and individual non-named arguments into `Arg`s.
*)
let rec groupReversed ~revCurGroup ~revResult labeledTypes =
match (revCurGroup, labeledTypes) with
| [], (Nolabel, type_) :: tl ->
groupReversed ~revCurGroup:[] ~revResult:(Arg type_ :: revResult) tl
| _, (OptLabel name, type_) :: tl ->
(* Add it to the current group, not result. *)
groupReversed
~revCurGroup:
({mutable_ = Immutable; nameJS = name; optional = Optional; type_}
:: revCurGroup)
~revResult tl
| _, (Label name, type_) :: tl ->
groupReversed
~revCurGroup:
({mutable_ = Immutable; nameJS = name; optional = Mandatory; type_}
:: revCurGroup)
~revResult tl
| [], [] -> revResult
| _grpHd :: _grpTl, ([] as _tl) | _grpHd :: _grpTl, (Nolabel, _) :: _tl ->
(* Just form the group, and recurse ignoring the (None, t) in that case.
t will be handled in recursion. *)
groupReversed ~revCurGroup:[]
~revResult:(Group revCurGroup :: revResult)
labeledTypes
(** Special reverse that not only reverses the entire list but also the order of
items in the NamedArgs grouping. *)
let rec reverse ?(soFar = []) lst =
match lst with
| [] -> soFar
| [Arg type_] when type_ = unitT && soFar = [] ->
(* treat a single argument of type unit as no argument *)
[]
| Arg type_ :: tl -> reverse ~soFar:({aName = ""; aType = type_} :: soFar) tl
| Group fields :: tl ->
reverse
~soFar:
({aName = ""; aType = GroupOfLabeledArgs (List.rev fields)} :: soFar)
tl
let group labeledTypes =
labeledTypes |> groupReversed ~revCurGroup:[] ~revResult:[] |> reverse