123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172(* 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.
*)openCommonmoduleV=Ast_fuzzy(*****************************************************************************)(* Prelude *)(*****************************************************************************)(* See https://github.com/facebook/pfff/wiki/Sgrep
*)(*****************************************************************************)(* Type *)(*****************************************************************************)typepattern=Ast_fuzzy.treelist(*****************************************************************************)(* Main entry point *)(*****************************************************************************)letsgrep~hookpatternast=letlen=List.lengthpatternin(* visit AST and try to match pattern on it *)lethook={V.default_visitorwithV.ktrees=(fun(k,_)xs->ifList.lengthxs>=lenthenbeginletshorter,rest=Common2.splitAtlenxsin(* pr (Ocaml.string_of_v (Ast_fuzzy.vof_trees shorter));*)letmatches_with_env=Matching_fuzzy.match_trees_treespatternshorterinifmatches_with_env=[]then(* recurse on sublists *)kxselsebegin(* could also recurse to find nested matching inside
* the matched code itself
*)letmatched_tokens=Ast_fuzzy.toks_of_treesshorterinmatches_with_env+>List.iter(funenv->hookenvmatched_tokens);krestendendelse(* at least recurse *)kxs);}in(V.mk_visitorhook)ast