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
[@@@ocaml.warning "-27"]
open Pcaml
open GTCommon
let hdtl loc xs = (List.hd xs, List.tl xs)
let trait_proto_t typ trait = Printf.sprintf "%s_proto_%s" trait typ
EXTEND
GLOBAL: sig_item str_item class_expr class_sig_item expr ctyp type_decl
class_expr_simple extended_longident;
class_sig_item: [[
"inherit"; cs = class_signature -> <:class_sig_item< inherit $cs$ >>
]];
class_signature:
[ [ ci = class_type_longident ->
let last, pfx = Camlp5Helpers.sep_last ci in
assert (not (Camlp5Helpers.capitalized last)) ;
match pfx with
[] -> <:class_type< $lid:last$ >>
| h::t ->
let rec lirec li = function
h::t -> lirec <:extended_longident< $longid:li$ . $uid:h$ >> t
| [] -> li in
let li = lirec <:extended_longident< $uid:h$ >> t in
<:class_type< $longid:li$ . $lid:last$ >>
] ]
;
class_type_longident: [[
"@"; ci=qname; t=OPT trait ->
let n, q = hdtl loc (List.rev ci) in
let classname =
match t with
| None -> Naming.class_name_for_typ n
| Some t -> Naming.trait_class_name_for_typ ~trait:t n
in
List.rev (classname::q)
| "+"; ci=qname; t=trait ->
let n, q = hdtl loc (List.rev ci) in
List.rev ((trait_proto_t t n) :: q)
]]
;
class_expr_simple: BEFORE "simple" [[
"["; ct = ctyp; ","; ctcl = LIST1 ctyp SEP ","; "]"; ci = class_longident ->
<:class_expr< [ $list:(ct :: ctcl)$ ] $lilongid:ci$ >>
| "["; ct = ctyp; "]"; ci = class_longident ->
<:class_expr< [ $ct$ ] $lilongid:ci$ >>
| ci = class_longident -> <:class_expr< $lilongid:ci$ >>
]];
expr: BEFORE "simple" [
LEFTA [ "new"; i = V class_longident "list" -> <:expr< new $_lilongid:i$ >> ]
];
ctyp: BEFORE "simple" [[
"#"; id = V class_longident "list" -> <:ctyp< # $_lilongid:id$ >>
]];
class_longident: [[
"@"; ci=qname; t=OPT trait ->
let n, q = hdtl loc (List.rev ci) in
let classname =
match t with
| None -> Naming.class_name_for_typ n
| Some t -> Naming.trait_class_name_for_typ ~trait:t n
in
Asttools.longident_lident_of_string_list loc (List.rev (classname::q))
| "+"; ci=qname; t=trait ->
let n, q = hdtl loc (List.rev ci) in
Asttools.longident_lident_of_string_list loc (List.rev ((trait_proto_t n t) :: q))
| ci=qname -> Asttools.longident_lident_of_string_list loc ci
]];
qname: [[
n=LIDENT -> [n]
| m=UIDENT; "."; q=SELF -> m :: q
]];
trait: [[ "["; id=LIDENT; "]" -> id ]];
nonrec_: [[ "nonrec" -> () ]];
str_item: LEVEL "top" [[
"@"; "type"; nonrec_=(OPT nonrec_); t=LIST1 t_decl SEP "and" ->
Core2.generate_str (Option.is_some nonrec_) t loc
]];
sig_item: LEVEL "top" [[
"@"; "type"; nonrec_=(OPT nonrec_); t=LIST1 t_decl SEP "and" ->
Core2.generate_sig (Option.is_some nonrec_) t loc
]];
t_decl: [[
"["; t=type_decl; "]" -> t, []
| t=type_decl; d=OPT deriving ->
t, (match d with None -> [] | Some d -> d)
]];
deriving: [["with"; s=LIST1 LIDENT SEP "," -> s]];
END;