123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155(* Yoann Padioleau
*
* Copyright (C) 2010 Facebook
* Copyright (C) 2019 r2c
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License (GPL)
* version 2 as published by the Free Software Foundation.
*
* This program 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_parsingmoduleTH=Token_helpers_pythonmodulePI=Parse_infomoduleLexer=Lexer_python(*****************************************************************************)(* Prelude *)(*****************************************************************************)(*****************************************************************************)(* Types *)(*****************************************************************************)typeprogram_and_tokens=Ast_python.programoption*Parser_python.tokenlist(*****************************************************************************)(* Error diagnostic *)(*****************************************************************************)leterror_msg_toktok=Parse_info.error_message_info(TH.info_of_toktok)(*****************************************************************************)(* Lexing only *)(*****************************************************************************)lettokens2file=letstate=Lexer.create()inlettokenlexbuf=matchLexer.top_modestatewith|Lexer.STATE_TOKEN->Lexer.tokenstatelexbuf|Lexer.STATE_OFFSET->failwith"impossibe STATE_OFFSET in python lexer"|Lexer.STATE_UNDERSCORE_TOKEN->lettok=Lexer._tokenstatelexbufin(matchtokwith|Parser_python.TCommentSpace_->()|Parser_python.FSTRING_START_->()|_->Lexer.set_modestateLexer.STATE_TOKEN);tok|Lexer.STATE_IN_FSTRING_SINGLE->Lexer.fstring_singlestatelexbuf|Lexer.STATE_IN_FSTRING_DOUBLE->Lexer.fstring_doublestatelexbuf|Lexer.STATE_IN_FSTRING_TRIPLE->Lexer.fstring_triplestatelexbufinParse_info.tokenize_all_and_adjust_posfiletokenTH.visitor_info_of_tokTH.is_eoflettokensa=Common.profile_code"Parse_python.tokens"(fun()->tokens2a)(*****************************************************************************)(* Main entry point *)(*****************************************************************************)letparse2filename=letstat=Parse_info.default_statfilenamein(* this can throw Parse_info.Lexical_error *)lettoks=tokensfilenameinlettoks_final=toks|>Common.excludeTH.is_specialinlettr,lexer,lexbuf_fake=Parse_info.mk_lexer_for_yacctoksTH.is_commentintry(* -------------------------------------------------- *)(* Call parser *)(* -------------------------------------------------- *)letxs=Common.profile_code"Parser_python.main"(fun()->Parser_python.mainlexerlexbuf_fake)instat.PI.correct<-(Common.catfilename|>List.length);(Somexs,toks_final),statwithParsing.Parse_error->letcur=tr.PI.currentinifnot!Flag.error_recoverythenraise(PI.Parsing_error(TH.info_of_tokcur));if!Flag.show_parsing_errorthenbeginpr2("parse error \n = "^error_msg_tokcur);letfilelines=Common2.cat_arrayfilenameinletcheckpoint2=Common.catfilename|>List.lengthinletline_error=PI.line_of_info(TH.info_of_tokcur)inParse_info.print_badline_error(0,checkpoint2)filelines;end;stat.PI.bad<-Common.catfilename|>List.length;(None,toks_final),statletparsea=Common.profile_code"Parse_python.parse"(fun()->parse2a)letparse_programfile=let((astopt,_toks),_stat)=parsefileinCommon2.someastopt(*****************************************************************************)(* Sub parsers *)(*****************************************************************************)let(program_of_string:string->Ast_python.program)=funs->Common2.with_tmp_file~str:s~ext:"py"(funfile->parse_programfile)(* for sgrep/spatch *)letany_of_strings=Common2.with_tmp_file~str:s~ext:"py"(funfile->lettoks=tokensfileinlet_tr,lexer,lexbuf_fake=PI.mk_lexer_for_yacctoksTH.is_commentin(* -------------------------------------------------- *)(* Call parser *)(* -------------------------------------------------- *)Parser_python.sgrep_spatch_patternlexerlexbuf_fake)(*****************************************************************************)(* Fuzzy parsing *)(*****************************************************************************)(*
let parse_fuzzy file =
let toks = tokens file in
let trees = Parse_fuzzy.mk_trees { Parse_fuzzy.
tokf = TH.info_of_tok;
kind = TH.token_kind_of_tok;
} toks
in
trees, toks
*)