This repository has been archived by the owner on Jun 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 205
/
main_sgrep.ml
413 lines (359 loc) · 13 KB
/
main_sgrep.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
(*
* The author disclaims copyright to this source code. In place of
* a legal notice, here is a blessing:
*
* May you do good and not evil.
* May you find forgiveness for yourself and forgive others.
* May you share freely, never taking more than you give.
*)
open Common
module PI = Parse_info
module S = Scope_code
(*****************************************************************************)
(* Purpose *)
(*****************************************************************************)
(*
* A syntactical grep. https://github.com/facebook/pfff/wiki/Sgrep
* Right now there is support for PHP, C/C++/ObjectiveC, OCaml, Java, and
* Javascript.
*
* opti: git grep foo | xargs sgrep -e 'foo(...)'
*
* related:
* - SSR http://www.jetbrains.com/idea/documentation/ssr.html
* - ack http://beyondgrep.com/
* - cgrep http://awgn.github.io/cgrep/
* - hound https://codeascraft.com/2015/01/27/announcing-hound-a-lightning-fast-code-search-tool/
*
* See also codequery for more structural queries.
*)
(*****************************************************************************)
(* Flags *)
(*****************************************************************************)
let use_multiple_patterns = ref false
let verbose = ref false
let pattern_file = ref ""
let pattern_string = ref ""
(* todo: infer from basename argv(0) ? *)
let lang = ref "php"
let case_sensitive = ref false
let match_format = ref Matching_report.Normal
let mvars = ref ([]: Metavars_fuzzy.mvar list)
let layer_file = ref (None: filename option)
(* action mode *)
let action = ref ""
(*****************************************************************************)
(* Helpers *)
(*****************************************************************************)
(* for -gen_layer *)
let _matching_tokens = ref []
(* TODO? could do slicing of function relative to the pattern, so
* would see where the parameters come from :)
*)
let print_match mvars mvar_binding ii_of_any tokens_matched_code =
(match mvars with
| [] ->
Matching_report.print_match ~format:!match_format tokens_matched_code
| xs ->
(* similar to the code of Lib_matcher.print_match, maybe could
* factorize code a bit.
* This assumes there is no FakeTok in tokens_matched_code.
* Currently the only fake tokens generated in parser_php.mly are
* for abstract methods and sgrep/spatch do not have metavariables
* to match such construct so we should be safe.
*)
let (mini, _maxi) =
PI.min_max_ii_by_pos tokens_matched_code in
let (file, line) =
PI.file_of_info mini, PI.line_of_info mini in
let strings_metavars =
xs +> List.map (fun x ->
match Common2.assoc_opt x mvar_binding with
| Some any ->
ii_of_any any
+> List.map PI.str_of_info
+> Matching_report.join_with_space_if_needed
| None ->
failwith (spf "the metavariable '%s' was not binded" x)
)
in
pr (spf "%s:%d: %s" file line (Common.join ":" strings_metavars));
);
tokens_matched_code +> List.iter (fun x -> Common.push x _matching_tokens)
let print_simple_match tokens_matched_code =
print_match [] [] tokens_matched_code
(* a layer need readable path, hence the ~root argument *)
let gen_layer ~root ~query file =
ignore(query);
pr2 ("generating layer in " ^ file);
let root = Common2.relative_to_absolute root in
let toks = !_matching_tokens in
let kinds = ["m" (* match *), "red"] in
(* todo: could now use Layer_code.simple_layer_of_parse_infos *)
let files_and_lines = toks +> List.map (fun tok ->
let file = PI.file_of_info tok in
let line = PI.line_of_info tok in
let file' = Common2.relative_to_absolute file in
Common.readable root file', line
)
in
let group = Common.group_assoc_bykey_eff files_and_lines in
let layer = { Layer_code.
title = "Sgrep";
description = "output of sgrep";
kinds = kinds;
files = group +> List.map (fun (file, lines) ->
let lines = Common2.uniq lines in
(file, { Layer_code.
micro_level = (lines +> List.map (fun l -> l, "m"));
macro_level = if null lines then [] else ["m", 1.];
})
);
}
in
Layer_code.save_layer layer file;
()
let ast_fuzzy_of_string str =
Common2.with_tmp_file ~str ~ext:"cpp" (fun tmpfile ->
Parse_cpp.parse_fuzzy tmpfile +> fst
)
(*****************************************************************************)
(* Language specific *)
(*****************************************************************************)
type ast_t =
| Fuzzy of Ast_fuzzy.tree list
| Php of Ast_php.program
let create_ast file =
match !lang with
| "php" ->
Php
(try
(Parse_php.parse_program file)
with Parse_php.Parse_error _err ->
Common.pr2 (spf "warning: parsing problem in %s" file);
[])
| _ ->
Fuzzy
(try
(match !lang with
| ("c" | "c++") ->
Common.save_excursion Flag_parsing_cpp.verbose_lexing false (fun () ->
Parse_cpp.parse_fuzzy file +> fst
)
| "java" ->
Parse_java.parse_fuzzy file +> fst
| "js" ->
Parse_js.parse_fuzzy file +> fst
| "ml" ->
Parse_ml.parse_fuzzy file +> fst
| "phpfuzzy" ->
Parse_php.parse_fuzzy file +> fst
| _ ->
failwith ("unsupported language: " ^ !lang))
with exn ->
pr2 (spf "PB with %s, exn = %s" file (Common.exn_to_s exn));
[])
let parse_pattern str =
match !lang with
| "php" -> Left (Sgrep_php.parse str)
(* for now we abuse the fuzzy parser of cpp for ml for the pattern as
* we should not use comments in patterns
*)
| "c" | "c++" | "ml" | "java" | "js" | "phpfuzzy" ->
Right (ast_fuzzy_of_string str)
| _ -> failwith ("unsupported language: " ^ !lang)
let read_patterns name =
let ic = open_in name in
let try_read () =
try Some (input_line ic) with End_of_file -> None in
let rec loop acc = match try_read () with
| Some s -> loop ((parse_pattern s) :: acc)
| None -> close_in ic; List.rev acc in
loop []
let sgrep_ast pattern any_ast =
match !lang, pattern, any_ast with
| ("c" | "c++"), Right pattern, Fuzzy ast ->
Sgrep_fuzzy.sgrep
~hook:(fun env matched_tokens ->
print_match !mvars env Ast_fuzzy.toks_of_trees matched_tokens
)
pattern ast
| "java", Right pattern, Fuzzy ast ->
Sgrep_fuzzy.sgrep
~hook:(fun env matched_tokens ->
print_match !mvars env Ast_fuzzy.toks_of_trees matched_tokens
)
pattern ast
| "js", Right pattern, Fuzzy ast ->
Sgrep_fuzzy.sgrep
~hook:(fun env matched_tokens ->
print_match !mvars env Ast_fuzzy.toks_of_trees matched_tokens
)
pattern ast
| "ml", Right pattern, Fuzzy ast ->
Sgrep_fuzzy.sgrep
~hook:(fun env matched_tokens ->
print_match !mvars env Ast_fuzzy.toks_of_trees matched_tokens
)
pattern ast
| "php", Left pattern, Php ast ->
Sgrep_php.sgrep_ast
~case_sensitive:!case_sensitive
~hook:(fun env matched_tokens ->
print_match !mvars env Lib_parsing_php.ii_of_any matched_tokens
)
pattern ast
| "phpfuzzy", Right pattern, Fuzzy ast ->
Sgrep_fuzzy.sgrep
~hook:(fun env matched_tokens ->
print_match !mvars env Ast_fuzzy.toks_of_trees matched_tokens
)
pattern ast
| _ ->
failwith ("unsupported language: " ^ !lang)
(*****************************************************************************)
(* Main action *)
(*****************************************************************************)
let main_action xs =
let patterns, query_string =
match !pattern_file, !pattern_string, !use_multiple_patterns with
| "", "", _ ->
failwith "I need a pattern; use -f or -e"
| file, _, true when file <> "" ->
read_patterns file, "multi"
| file, _, _ when file <> "" ->
let s = Common.read_file file in
[parse_pattern s], s
| _, s, true when s <> ""->
failwith "cannot combine -multi with -e"
| _, s, _ when s <> ""->
[parse_pattern s], s
| _ -> raise Impossible
in
Logger.log Config_pfff.logger "sgrep" (Some query_string);
let files =
Find_source.files_of_dir_or_files ~lang:!lang ~verbose:!verbose xs in
files +> List.iter (fun file ->
if !verbose then pr2 (spf "processing: %s" file);
let ast = create_ast file in
let sgrep pattern = sgrep_ast pattern ast in
List.iter sgrep patterns
);
!layer_file +> Common.do_option (fun file ->
let root = Common2.common_prefix_of_files_or_dirs xs in
gen_layer ~root ~query:query_string file
);
()
(*****************************************************************************)
(* Extra actions *)
(*****************************************************************************)
let dump_sgrep_php_pattern file =
let any = Parse_php.parse_any file in
let s = Export_ast_php.ml_pattern_string_of_any any in
pr s
(*---------------------------------------------------------------------------*)
(* Regression testing *)
(*---------------------------------------------------------------------------*)
open OUnit
let test () =
let suite = "sgrep" >::: [
(* ugly: todo: use a toy fuzzy parser instead of the one in lang_cpp/ *)
Unit_matcher.sgrep_unittest ~ast_fuzzy_of_string;
Unit_matcher_php.sgrep_unittest;
]
in
OUnit.run_test_tt suite +> ignore;
()
(*---------------------------------------------------------------------------*)
(* the command line flags *)
(*---------------------------------------------------------------------------*)
let sgrep_extra_actions () = [
(*
"-dump_php_pattern", " <file> (internal)",
Common.mk_action_1_arg dump_sgrep_php_pattern;
*)
"-test", " run regression tests",
Common.mk_action_0_arg test;
]
(*****************************************************************************)
(* The options *)
(*****************************************************************************)
let all_actions () =
sgrep_extra_actions()@
[]
let options () =
[
"-lang", Arg.Set_string lang,
(spf " <str> choose language (default = %s)" !lang);
"-e", Arg.Set_string pattern_string,
" <pattern> expression pattern";
"-f", Arg.Set_string pattern_file,
" <file> obtain pattern from file";
"-multi", Arg.Set use_multiple_patterns,
" combine with -f <file> to obtain multiple patterns from file, one per line";
"-case_sensitive", Arg.Set case_sensitive,
" match code in a case sensitive manner";
"-emacs", Arg.Unit (fun () -> match_format := Matching_report.Emacs ),
" print matches on the same line than the match position";
"-oneline", Arg.Unit (fun () -> match_format := Matching_report.OneLine),
" print matches on one line, in normalized form";
"-pvar", Arg.String (fun s -> mvars := Common.split "," s),
" <metavars> print the metavariables, not the matched code";
"-gen_layer", Arg.String (fun s -> layer_file := Some s),
" <file> save result in a pfff layer file\n";
"-verbose", Arg.Unit (fun () ->
verbose := true;
Flag_matcher.verbose := true;
Flag_matcher_php.verbose := true;
),
" ";
] @
(* old: Flag_parsing_php.cmdline_flags_pp () ++ *)
Common.options_of_actions action (all_actions()) @
Common2.cmdline_flags_devel () @
[
"-version", Arg.Unit (fun () ->
pr2 (spf "sgrep version: %s" Config_pfff.version);
exit 0;
),
" guess what";
] @
[]
(*****************************************************************************)
(* Main entry point *)
(*****************************************************************************)
let main () =
let usage_msg =
spf "Usage: %s [options] <pattern> <file or dir> \nDoc: %s\nOptions:"
(Common2.basename Sys.argv.(0))
"https://github.com/facebook/pfff/wiki/Sgrep"
in
(* does side effect on many global flags *)
let args = Common.parse_options (options()) usage_msg Sys.argv in
(* must be done after Arg.parse, because Common.profile is set by it *)
Common.profile_code "Main total" (fun () ->
(match args with
(* --------------------------------------------------------- *)
(* actions, useful to debug subpart *)
(* --------------------------------------------------------- *)
| xs when List.mem !action (Common.action_list (all_actions())) ->
Common.do_action !action xs (all_actions())
| _ when not (Common.null_string !action) ->
failwith ("unrecognized action or wrong params: " ^ !action)
(* --------------------------------------------------------- *)
(* main entry *)
(* --------------------------------------------------------- *)
| x::xs ->
main_action (x::xs)
(* --------------------------------------------------------- *)
(* empty entry *)
(* --------------------------------------------------------- *)
| [] ->
Common.usage usage_msg (options())
)
)
(*****************************************************************************)
let _ =
Common.main_boilerplate (fun () ->
main ();
)