-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtypes_parser.mly
More file actions
77 lines (66 loc) · 1.86 KB
/
Copy pathtypes_parser.mly
File metadata and controls
77 lines (66 loc) · 1.86 KB
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
%{
(* The 'intermediate' type expression. *)
type ityp =
| ITyFun of { arg: ityp; argname: string option; optional: bool; retty: ityp }
| ITyApp of { args: ityp list; destty: ityp } (* string list, or (int, int) func *)
| ITyTuple of ityp list
| ITyConst of string
| ITyParen of ityp
let rec postprocess (ity:ityp): OcamlTypes.typ =
match ity with
| ITyFun x -> OcamlTypes.TyFun {
arg = postprocess x.arg;
argname = x.argname;
optional = x.optional;
retty = postprocess x.retty
}
| ITyApp x -> OcamlTypes.TyApp {
args = List.map postprocess x.args;
destty = postprocess x.destty
}
| ITyTuple tys -> OcamlTypes.TyTuple (List.map postprocess tys)
| ITyConst s -> OcamlTypes.TyConst s
| ITyParen t -> postprocess t
%}
%token <string> TYPEVAR IDENT
%token ARROW LPAREN RPAREN STAR COMMA COLON QUESTION_MARK EOF
%start typ_expr
%type <OcamlTypes.typ> typ_expr
%right ARROW
%left STAR
%left COMMA
%%
typ_expr:
| typ EOF { postprocess $1 }
(* a -> b -> ... *)
typ:
| typ_tuple { $1 }
| typ ARROW typ { ITyFun { arg = $1 ; argname = None; optional = false; retty = $3 } }
| IDENT COLON typ ARROW typ {
ITyFun { arg = $3; argname = Some $1; optional = false; retty = $5 }
}
| QUESTION_MARK IDENT COLON typ ARROW typ {
ITyFun { arg = $4; argname = Some $2; optional = true; retty = $6 }
}
(* a * b * ... *)
typ_tuple:
| typ_app STAR typ_tuple {
match $3 with
| ITyTuple ls -> ITyTuple ($1::ls)
| _ -> ITyTuple [$1;$3]
}
| typ_app { $1 }
(* a b ... *)
typ_app:
| typ_atom typ_app { ITyApp { args = [$1]; destty = $2 } }
| LPAREN typ_args RPAREN typ_app { ITyApp { args = $2; destty = $4 } }
| typ_atom { $1 }
(* a, b, ... *)
typ_args:
| typ COMMA typ_args { $1 :: $3 }
| typ { [$1] }
typ_atom:
| TYPEVAR { ITyConst $1 }
| IDENT { ITyConst $1 }
| LPAREN typ RPAREN { ITyParen $2 }
%%