-
Notifications
You must be signed in to change notification settings - Fork 125
/
ocaml_types.trx
101 lines (81 loc) · 3.95 KB
/
ocaml_types.trx
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
(*
Copyright © 2011 MLstate
This file is part of OPA.
OPA is free software: you can redistribute it and/or modify it under the
terms of the GNU Affero General Public License, version 3, as published by
the Free Software Foundation.
OPA is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
more details.
You should have received a copy of the GNU Affero General Public License
along with OPA. If not, see <http://www.gnu.org/licenses/>.
*)
read default.trx global (spacing)
spacing <- (Default.space $ / Default.mlcomment)* $
read ocaml_keywords.trx
typeconst <- (IntT / Int64T / FloatT / StringT / UnitT / BoolT) {{ TypeConst __1 }}
IntT <- "int" {{ TypeInt }}
Int64T <- "int64" {{ TypeInt64 }}
FloatT <- "float" {{ TypeFloat }}
StringT <- "string" {{ TypeString }}
UnitT <- "unit" {{ TypeUnit }}
BoolT <- "bool" {{ TypeBool }}
typeIdent_cont <- [a-zA-Z0-9_]
typeIdent_aux <- [a-z] typeIdent_cont* $_ / [_] typeIdent_cont+ $_
typeIdent <- !(Ocaml_keywords.KEYWORDS !typeIdent_cont) typeIdent_aux spacing {{ __2 }}
typeconstructor <- ([A-Z] typeIdent_cont* $_) spacing {{ __1 }}
typevar <- [\'] typeIdent {{ TypeVar ("'" ^ __2) }}
typename <- typeconstructor [.] typename {{ __1 :: __3 }}
/ typeIdent {{ [__1] }}
{{
open Base
open Ocaml
}}
typevars <- typevar {{ [__1] }}
/ Default.lparen typevar (Default.comma typevar {{ __2 }})* Default.rparen {{ __2 :: __3 }}
typevarorname <- typevar / !(typeconst !typeIdent_cont) typename {{ TypeName ([], __2) }} / typeconst spacing {{ __1 }}
{{ let compute_vars = function None -> [] | Some l -> List.map (function TypeVar s -> s | _ -> assert false) l}}
+typedeclaration : {(string list * string * Ocaml.type_expr) list}
<- Ocaml_keywords.TYPE typevars? typeIdent Default.equal typedeflist (Ocaml_keywords.AND typevars? typeIdent Default.equal typedeflist {{compute_vars __2, __3, __5}})*
{{ (compute_vars __2, __3, __5) :: __6 }}
+test : {unit} <- spacing typedeclaration ";;" spacing test {{ }} / Default.eof {{ }}
lineskipper <- !(spacing Ocaml_keywords.TYPE) (![\n] .)* [\n] $
+typefinder : {(string list * string * Ocaml.type_expr) list option} <- lineskipper* spacing typedeclaration? {{ __3 }}
typedeflist <- Default.bar? typedef (Default.bar typedef {{ __2 }})* {{
match __2, __3 with
| _, [] -> __2
| TypeConstructor l1, l2 ->
TypeConstructor ( l1 @ List.flatten (List.map (function | TypeConstructor l -> l | _ -> failwith "@Ocaml_types.typedeflist : must be a constructor") l2))
| _x, _xl -> failwith "@Ocaml_types.typedeflist : impossible"
}}
typedef_base <-
/ typeSum / typerecord / typevarorname
/ Default.lparen typedef Default.rparen {{ __2 }}
typeparam <-
/ Default.lparen typedef (Default.comma typedef {{__2}})* Default.rparen {{ __2 :: __3 }}
/ typedef_base {{ [__1] }}
typedef_aux <-
/ typeparam typename+ {{
match __2 with
| hd :: tl ->
List.fold_left (fun acc x -> TypeName ([acc], x)) (TypeName (__1, hd)) tl
| _ -> assert false
}}
/ typedef_base
typedef_no_arrow <- typedef_aux ([*] spacing typedef_aux {{__3}})* {{
match (__1::__2) with
| [] -> failwith "@Ocaml_types.typedef_no_arrow : empty type tuple"
| [td] -> td
| tdl -> TypeTuple tdl
}}
typedef <-
/ typedef_no_arrow Default.rightarrow typedef {{ TypeArrow ( __1, __3) }}
/ typedef_no_arrow {{ __1 }}
#typedef_rewrite <- typedef {{ rewrite_typedef __1 }}
onetyperecord <- Ocaml_keywords.MUTABLE? typeIdent Default.colon typedef {{ (match __1 with None -> false | _ -> true), __2, __4 }}
typerecord_aux <- onetyperecord (Default.semi onetyperecord {{ __2 }})* Default.semi? {{ __1 ::__2 }}
typeSum <- typeconstructor ![.] (Ocaml_keywords.OF typedef {{__2}})? {{ TypeConstructor [__1, __3] }}
#/ typeIdent {{ [__2, `typeconst TyUnit] }}
let in_accol e = Default.laccol e Default.raccol {{ __2 }}
typerecord = in_accol typerecord_aux {{ TypeRecord __1 }}