package pfff

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file sgrep_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
(* Yoann Padioleau
 *
 * Copyright (C) 2013 Facebook
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * version 2.1 as published by the Free Software Foundation, with the
 * special exception on linking described in file license.txt.
 * 
 * This library 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 file
 * license.txt for more details.
 *)

module V = Lib_ast_fuzzy

(*****************************************************************************)
(* Prelude *)
(*****************************************************************************)
(* See https://github.com/facebook/pfff/wiki/Sgrep 
*)

(*****************************************************************************)
(* Type *)
(*****************************************************************************)

type pattern = Ast_fuzzy.tree list

(*****************************************************************************)
(* Main entry point *)
(*****************************************************************************)

let sgrep ~hook pattern ast =

  let len = List.length pattern in

  (* visit AST and try to match pattern on it *)
  let hook =
    { V.default_visitor with
      V.ktrees = (fun (k, _) xs ->
        if List.length xs >= len then begin
          let shorter, rest = Common2.splitAt len xs in

          (* pr (Ocaml.string_of_v (Ast_fuzzy.vof_trees shorter));*)

          let matches_with_env =
            Matching_fuzzy.match_trees_trees pattern shorter
          in
          if matches_with_env = []
          then
            (* recurse on sublists *)
            k xs
          else begin
            (* could also recurse to find nested matching inside 
             * the matched code itself
             *)
            let matched_tokens = Lib_ast_fuzzy.toks_of_trees shorter in
            matches_with_env |> List.iter (fun env ->
              hook env matched_tokens
            );
            k rest
          end
        end
        else 
          (* at least recurse *)
          k xs
      );
    }
  in
  (V.mk_visitor hook) ast
OCaml

Innovation. Community. Security.