123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200openCommonmoduleFlag=Flag_parsingmoduleJ=Json_typemodulePI=Parse_info(*****************************************************************************)(* Subsystem testing *)(*****************************************************************************)lettest_tokens_jsfile=ifnot(file=~".*\\.js")thenpr2"warning: seems not a .js file";Flag.verbose_lexing:=true;Flag.verbose_parsing:=true;lettoks=Parse_js.tokensfile(* |> Parsing_hacks_js.fix_tokens *)intoks|>List.iter(funx->pr2_genx);()lettest_parse_commonxsfullxsext=letdirname_opt,fullxs=matchxswith|[x]whenCommon2.is_directoryx->letskip_list=ifSys.file_exists(x^"/skip_list.txt")thenSkip_code.load(x^"/skip_list.txt")else[]inSomex,Skip_code.filter_filesskip_listxfullxs|_->None,fullxsinletstat_list=ref[]inletnewscore=Common2.empty_score()inCommon2.check_stack_nbfiles(List.lengthfullxs);fullxs|>Console.progress(funk->List.iter(funfile->k();let(_xs,stat)=Common.save_excursionFlag.error_recoverytrue(fun()->Common.save_excursionFlag.exn_when_lexical_errorfalse(fun()->Parse_js.parsefile))inCommon.pushstatstat_list;lets=spf"bad = %d"stat.Parse_info.badinifstat.Parse_info.bad=0thenHashtbl.addnewscorefile(Common2.Ok)elseHashtbl.addnewscorefile(Common2.Pbs)));Parse_info.print_parsing_stat_list!stat_list;letscore_path=Filename.concatConfig_pfff.path"tmp"indirname_opt|>Common.do_option(fundirname->letdirname=Common.fullpathdirnameinpr2"--------------------------------";pr2"regression testing information";pr2"--------------------------------";letstr=Str.global_replace(Str.regexp"/")"__"dirnameinCommon2.regression_testingnewscore(Filename.concatscore_path(spf"score_parsing__%s%s.marshalled"strext)));()lettest_parse_jsxs=letfullxs=Lib_parsing_js.find_source_files_of_dir_or_files~include_scripts:falsexsintest_parse_commonxsfullxs"js"moduleFT=File_typelettest_parse_tsxs=letfullxs=Common.files_of_dir_or_files_no_vcs_nofilterxs|>List.filter(funfilename->matchFT.file_type_of_filefilenamewith|FT.PL(FT.WebFT.TypeScript)->true|_->false)|>Common.sortin(* typescript and JSX have lexing conflicts *)Common.save_excursionFlag_parsing_js.jsxfalse(fun()->test_parse_commonxsfullxs"ts")lettest_dump_jsfile=letast=Parse_js.parse_programfileinletv=Meta_cst_js.vof_programastinlets=Ocaml.string_of_vvinprslettest_dump_tsfile=(* typescript and JSX have lexing conflicts *)Common.save_excursionFlag_parsing_js.jsxfalse(fun()->letast=Parse_js.parse_programfileinletv=Meta_cst_js.vof_programastinlets=Ocaml.string_of_vvinprs)(*****************************************************************************)(* JSON output *)(*****************************************************************************)letinfo_to_json_rangeinfo=letloc=PI.token_location_of_infoinfoinJ.Object["line",J.Intloc.PI.line;"col",J.Intloc.PI.column;],J.Object["line",J.Intloc.PI.line;"col",J.Int(loc.PI.column+String.lengthloc.PI.str);]letparse_js_r2cxs=letfullxs=Lib_parsing_js.find_source_files_of_dir_or_files~include_scripts:falsexsinletjson=J.Array(fullxs|>Common.map_filter(funfile->letnblines=Common.catfile|>List.lengthintrylet(_xs,_stat)=Common.save_excursionFlag.error_recoveryfalse(fun()->Common.save_excursionFlag.exn_when_lexical_errortrue(fun()->Common.save_excursionFlag.show_parsing_errortrue(fun()->Parse_js.parsefile)))in(* only return a finding if there was a parse error so we can
* sort by the number of parse errors in the triage tool
*)Nonewith(Parse_info.Parsing_error(info)|Parse_info.Lexical_error(_,info))asexn->let(startp,endp)=info_to_json_rangeinfoinletmessage=matchexnwith|Parse_info.Parsing_error_->"parse error"|Parse_info.Lexical_error(s,_)->"lexical error: "^s|_->raiseImpossibleinSome(J.Object["check_id",J.String"pfff-parse_js_r2c";"path",J.Stringfile;"start",startp;"end",endp;"extra",J.Object["size",J.Intnblines;"message",J.Stringmessage;(*
"correct", J.Int stat.PI.correct;
"bad", J.Int stat.PI.bad;
"timeout", J.Bool stat.PI.have_timeout;
*)]])))inletjson=J.Object["results",json]inlets=Json_io.string_of_jsonjsoninprs(*****************************************************************************)(* Main entry for Arg *)(*****************************************************************************)letactions()=["-tokens_js"," <file>",Common.mk_action_1_argtest_tokens_js;"-parse_js"," <file or dir>",Common.mk_action_n_argtest_parse_js;"-parse_ts"," <file or dir>",Common.mk_action_n_argtest_parse_ts;"-dump_js"," <file>",Common.mk_action_1_argtest_dump_js;"-dump_ts"," <file>",Common.mk_action_1_argtest_dump_ts;"-parse_js_r2c"," <file or dir>",Common.mk_action_n_argparse_js_r2c;(* old:
"-json_js", " <file> export the AST of file into JSON",
Common.mk_action_1_arg test_json_js;
"-parse_esprima_json", " <file> ",
Common.mk_action_1_arg test_esprima;
*)]