Source file fuzzy_vs_fuzzy.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
open Common
module A = Ast_fuzzy
module B = Ast_fuzzy
module type PARAM =
sig
type tin
type 'x tout
type ('a, 'b) matcher = 'a -> 'b -> tin -> ('a * 'b) tout
val (>>=):
(tin -> ('a * 'b) tout) ->
('a * 'b -> (tin -> ('c * 'd) tout)) ->
(tin -> ('c * 'd) tout)
val (>||>) :
(tin -> 'x tout) ->
(tin -> 'x tout) ->
(tin -> 'x tout)
val return : ('a * 'b) -> tin -> ('a *'b) tout
val fail : tin -> ('a * 'b) tout
val tokenf : (Parse_info.info, Parse_info.info) matcher
val envf : (Metavars_fuzzy.mvar * Parse_info.info, Ast_fuzzy.trees)
matcher
end
let is_NoTransfo tok =
match tok.Parse_info.transfo with
| Parse_info.NoTransfo -> true
| _ -> false
let is_Remove tok =
match tok.Parse_info.transfo with
| Parse_info.Remove -> true
| _ -> false
module X_VS_X =
functor (X : PARAM) ->
struct
type ('a, 'b) matcher = 'a -> 'b -> X.tin -> ('a * 'b) X.tout
let (>>=) = X.(>>=)
let return =
X.return
let fail () =
X.fail
let m_tok a b =
X.tokenf a b
let rec m_list__m_tree xsa xsb =
match xsa, xsb with
| [], [] ->
return ([], [])
| [A.Dots _t], _bbs ->
return (
xsa,
xsb
)
| xa::aas, xb::bbs ->
m_tree xa xb >>= (fun (xa, xb) ->
m_list__m_tree aas bbs >>= (fun (aas, bbs) ->
return (
xa::aas,
xb::bbs
)
)
)
| [], _::_
| _::_, _ ->
fail ()
and m_arguments xsa xsb =
match xsa, xsb with
| [], [] ->
return ([], [])
| [Left [(A.Dots t)]], _bbs ->
if is_NoTransfo t then
return (
xsa,
xsb
)
else failwith
("transformation (minus or plus) on '...' not allowed, " ^
"rewrite your spatch")
| [Right a; Left [A.Dots i]], Right b::bbs ->
m_tok a b >>= (fun (a, b) ->
return (
[Right a; Left [A.Dots i]],
Right b::bbs
)
)
| [Right a; Left [A.Dots _i]], [] ->
if is_NoTransfo a || is_Remove a
then
return (
xsa,
xsb
)
else failwith
("transformation (minus or plus) on ',' not allowed when used with " ^
"'...'. Rewrite your spatch: put your trailing comma on the line " ^
"with the '...'. See also " ^
"https://github.com/facebook/pfff/wiki/Spatch#wiki-spacing-issues")
| [Right _; Left [A.Dots _i]], _bbs ->
raise Impossible
| Left [A.Dots _i]::_xs, _bbs ->
failwith "... is allowed for now only at the end. Give money to pad to get this feature"
| xa::aas, xb::bbs ->
m_either_m_argument xa xb >>= (fun (xa, xb) ->
m_arguments aas bbs >>= (fun (aas, bbs) ->
return (
xa::aas,
xb::bbs
)
)
)
| [], _
| _::_, _ ->
fail ()
and m_either_m_argument a b =
match a, b with
| Left [A.Metavar (s, tok)], Left b ->
X.envf (s, tok) b >>= (function
| ((s, _a), b) ->
return (
Left [A.Metavar (s, tok)],
Left b
)
)
| Left a, Left b ->
m_trees a b >>= (fun (a, b) ->
return (
Left a, Left b
))
| Right a, Right b ->
m_tok a b >>= (fun (a, b) ->
return (
Right a, Right b
))
| Left _, Right _
| Right _, Left _ ->
fail ()
and m_tree a b =
match a, b with
| A.Metavar (s, tok), b ->
let ok =
match b with
| B.Parens _ -> true
| B.Tok (s,_) -> s =~ "^[a-zA-Z]"
| B.Metavar _ -> true
| _ -> false
in
if ok then
X.envf (s, tok) [b] >>= (function
| ((s, _a), [b]) ->
return (
A.Metavar (s, tok),
b
)
| _ -> raise Impossible
)
else fail ()
| A.Braces (a1, a2, a3), B.Braces (b1, b2, b3) ->
m_tok a1 b1 >>= (fun (a1, b1) ->
m_trees a2 b2 >>= (fun (a2, b2) ->
m_tok a3 b3 >>= (fun (a3, b3) ->
return (
A.Braces (a1, a2, a3),
B.Braces (b1, b2, b3)
)
)))
| A.Bracket (a1, a2, a3), B.Bracket (b1, b2, b3) ->
m_tok a1 b1 >>= (fun (a1, b1) ->
m_trees a2 b2 >>= (fun (a2, b2) ->
m_tok a3 b3 >>= (fun (a3, b3) ->
return (
A.Bracket (a1, a2, a3),
B.Bracket (b1, b2, b3)
)
)))
| A.Parens (a1, a2, a3), B.Parens (b1, b2, b3) ->
m_tok a1 b1 >>= (fun (a1, b1) ->
m_arguments a2 b2 >>= (fun (a2, b2) ->
m_tok a3 b3 >>= (fun (a3, b3) ->
return (
A.Parens (a1, a2, a3),
B.Parens (b1, b2, b3)
)
)))
| A.Angle (a1, a2, a3), B.Angle (b1, b2, b3) ->
m_tok a1 b1 >>= (fun (a1, b1) ->
m_trees a2 b2 >>= (fun (a2, b2) ->
m_tok a3 b3 >>= (fun (a3, b3) ->
return (
A.Angle (a1, a2, a3),
B.Angle (b1, b2, b3)
)
)))
| A.Tok a1, B.Tok b1 ->
m_wrap a1 b1 >>= (fun (a1, b1) ->
return (
A.Tok a1,
B.Tok b1
)
)
| A.Braces _, _
| A.Bracket _, _
| A.Parens _, _
| A.Angle _, _
| A.Tok _, _
| A.Dots _, _
-> fail ()
and m_wrap (a1, a2) (b1, b2) =
m_tok a2 b2 >>= (fun (a2, b2) ->
return (
(a1, a2),
(b1, b2)
)
)
and m_trees a b = m_list__m_tree a b
end