123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137(* Yoann Padioleau
*
* Copyright (C) 2010 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.
*)openCommonmoduleFlag=Flag_parsingmodulePI=Parse_infomoduleTH=Token_helpers_java(*****************************************************************************)(* Prelude *)(*****************************************************************************)(* Lots of copy paste with my other parsers (e.g. C++, PHP, sql) but
* copy paste is sometimes ok.
*)(*****************************************************************************)(* Types *)(*****************************************************************************)(* the token list contains also the comment-tokens *)typeprogram_and_tokens=Ast_java.programoption*Parser_java.tokenlist(*****************************************************************************)(* Error diagnostic *)(*****************************************************************************)leterror_msg_toktok=Parse_info.error_message_info(TH.info_of_toktok)(*****************************************************************************)(* Lexing only *)(*****************************************************************************)lettokens2file=lettoken=Lexer_java.tokeninParse_info.tokenize_all_and_adjust_posfiletokenTH.visitor_info_of_tokTH.is_eoflettokensa=Common.profile_code"Java parsing.tokens"(fun()->tokens2a)(*****************************************************************************)(* Main entry point *)(*****************************************************************************)letparse2filename=letstat=Parse_info.default_statfilenameinletfilelines=Common2.cat_arrayfilenameinlettoks=tokensfilenameinlettoks=Parsing_hacks_java.fix_tokenstoksinlettr,lexer,lexbuf_fake=Parse_info.mk_lexer_for_yacctoksTH.is_commentinletcheckpoint=TH.line_of_toktr.PI.currentinletelems=try((* -------------------------------------------------- *)(* Call parser *)(* -------------------------------------------------- *)Left(Common.profile_code"Parser_java.main"(fun()->Parser_java.goallexerlexbuf_fake)))withParsing.Parse_error->letline_error=TH.line_of_toktr.PI.currentinlet_passed_before_error=tr.PI.passedinletcurrent=tr.PI.currentin(* no error recovery, the whole file is discarded *)tr.PI.passed<-List.revtoks;letinfo_of_bads=Common2.map_eff_revTH.info_of_toktr.PI.passedinRight(info_of_bads,line_error,current)inmatchelemswith|Leftxs->stat.PI.correct<-(Common.catfilename|>List.length);(Somexs,toks),stat|Right(_info_of_bads,line_error,cur)->ifnot!Flag.error_recoverythenraise(PI.Parsing_error(TH.info_of_tokcur));if!Flag.show_parsing_errorthenpr2("parse error \n = "^error_msg_tokcur);letcheckpoint2=Common.catfilename|>List.lengthinif!Flag.show_parsing_errorthenParse_info.print_badline_error(checkpoint,checkpoint2)filelines;stat.PI.bad<-Common.catfilename|>List.length;(None,toks),statletparsea=Common.profile_code"Parse_java.parse"(fun()->parse2a)letparse_programfile=let((ast,_toks),_stat)=parsefileinCommon2.someastletparse_string(w:string):(Ast_java.programoption*Parser_java.tokenlist)*Parse_info.parsing_stat=Common2.with_tmp_file~str:w~ext:"java"parse(*****************************************************************************)(* Sub parsers *)(*****************************************************************************)(* for sgrep/spatch *)letany_of_strings=Common2.with_tmp_file~str:s~ext:"java"(funfile->lettoks=tokensfileinlettoks=Parsing_hacks_java.fix_tokenstoksinlet_tr,lexer,lexbuf_fake=PI.mk_lexer_for_yacctoksTH.is_commentinParser_java.sgrep_spatch_patternlexerlexbuf_fake)