1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071(* 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.
*)moduleV=Lib_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=Lib_ast_fuzzy.toks_of_treesshorterinmatches_with_env|>List.iter(funenv->hookenvmatched_tokens);krestendendelse(* at least recurse *)kxs);}in(V.mk_visitorhook)ast