123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108(* Yoann Padioleau
*
* Copyright (C) 2019 r2c
*
* 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.
*)openCommon(*****************************************************************************)(* Prelude *)(*****************************************************************************)(* Wrappers around many languages to transform them in a fuzzy AST
* (see ast_fuzzy.ml)
*)(*****************************************************************************)(* Entry point *)(*****************************************************************************)(* This is similar to what I did for OPA. This is also similar
* to what I do for parsing hacks for C++, but this fuzzy AST can be useful
* on its own, e.g. for a not too bad sgrep/spatch.
*)letparse_and_tokens_with_langlangfile=matchlangwith|Lang_fuzzy.PHP->lettoks=Parse_php.tokensfile|>(funtoks->if!Flag_parsing.sgrep_modethenCommon.excludeToken_helpers_php.is_eoftokselsetoks)inlethooks={Lib_ast_fuzzy.tokf=Token_helpers_php.info_of_tok;kind=Token_helpers_php.token_kind_of_tok;}inLib_ast_fuzzy.mk_treeshookstoks,Lib_ast_fuzzy.mk_tokenshookstoks|Lang_fuzzy.ML->lettoks=Parse_ml.tokensfile|>(funtoks->if!Flag_parsing.sgrep_modethenCommon.excludeToken_helpers_ml.is_eoftokselsetoks)inlethooks={Lib_ast_fuzzy.tokf=Token_helpers_ml.info_of_tok;kind=Token_helpers_ml.token_kind_of_tok;}inLib_ast_fuzzy.mk_treeshookstoks,Lib_ast_fuzzy.mk_tokenshookstoks|Lang_fuzzy.Skip->lettoks=Parse_skip.tokensfile|>(funtoks->if!Flag_parsing.sgrep_modethenCommon.excludeToken_helpers_skip.is_eoftokselsetoks)inlethooks={Lib_ast_fuzzy.tokf=Token_helpers_skip.info_of_tok;kind=Token_helpers_skip.token_kind_of_tok;}inLib_ast_fuzzy.mk_treeshookstoks,Lib_ast_fuzzy.mk_tokenshookstoks|Lang_fuzzy.Java->lettoks=Parse_java.tokensfile|>(funtoks->if!Flag_parsing.sgrep_modethenCommon.excludeToken_helpers_java.is_eoftokselsetoks)inlethooks={Lib_ast_fuzzy.tokf=Token_helpers_java.info_of_tok;kind=Token_helpers_java.token_kind_of_tok;}inLib_ast_fuzzy.mk_treeshookstoks,Lib_ast_fuzzy.mk_tokenshookstoks|Lang_fuzzy.Javascript->lettoks=Parse_js.tokensfile|>(funtoks->if!Flag_parsing.sgrep_modethenCommon.excludeToken_helpers_js.is_eoftokselsetoks)inlethooks={Lib_ast_fuzzy.tokf=Token_helpers_js.info_of_tok;kind=Token_helpers_js.token_kind_of_tok;}inLib_ast_fuzzy.mk_treeshookstoks,Lib_ast_fuzzy.mk_tokenshookstoks|Lang_fuzzy.Cpp->Common.save_excursionFlag_parsing.verbose_lexingfalse(fun()->Parse_cpp.parse_fuzzyfile)letparse_with_langlangfile=parse_and_tokens_with_langlangfile|>fstletparsefile=matchLang_fuzzy.lang_of_filename_optfilewith|Somelang->parse_with_langlangfile|None->failwith(spf"unsupported file for fuzzy AST: %s"file)letparse_patternlangstr=Common.save_excursionFlag_parsing.sgrep_modetrue(fun()->Common2.with_tmp_file~str~ext:"xx"(funtmpfile->parse_with_langlangtmpfile))