123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281(* 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.
*)openCommonopenAst_fuzzymodulePI=Parse_info(*****************************************************************************)(* Prelude *)(*****************************************************************************)(* A few helpers function to build Ast_fuzzy tree from a list of tokens.
* It factorizes the language-independent part of those AST fuzzy builder.
*)(*****************************************************************************)(* Types *)(*****************************************************************************)type'tokhooks={kind:'tok->Parse_info.token_kind;tokf:'tok->Parse_info.t;}exceptionUnclosedofstring*Parse_info.t(* starting point *)(*****************************************************************************)(* Helpers *)(*****************************************************************************)letchar_of_token_kind=function|PI.RAngle->'>'|PI.RBracket->']'|PI.RBrace->'}'|_->raise(Impossible)(*****************************************************************************)(* Entry point *)(*****************************************************************************)(*
* less: I should also factorize with Parse_cpp.parse_fuzzy.
* put here also generic parts of token_views_of_xxx?
*
* less: check that it's consistent with the indentation?
* less: more fault tolerance? if col == 0 and { then reset?
*)letmk_treeshxs=(* filter comment tokens *)letxs=xs|>Common.exclude(funt->letkind=h.kindtinmatchkindwith|PI.Esthet_|PI.Eof->true|_->false)inletrecconsumexxs=matchxwith|tokwhenh.kindtok=PI.LBrace->letbody,closing,rest=look_closePI.RBracex[]xsinAst_fuzzy.Braces(h.tokfx,body,h.tokfclosing),rest|tokwhenh.kindtok=PI.LBracket->letbody,closing,rest=look_closePI.RBracketx[]xsinAst_fuzzy.Bracket(h.tokfx,body,h.tokfclosing),rest|tokwhenh.kindtok=PI.LAngle->letbody,closing,rest=look_closePI.RAnglex[]xsinAst_fuzzy.Angle(h.tokfx,body,h.tokfclosing),rest|tokwhenh.kindtok=PI.LPar->letbody,closing,rest=look_close_parenx[]xsinletbody'=split_commabodyinAst_fuzzy.Parens(h.tokfx,body',h.tokfclosing),rest|tok->Ast_fuzzy.Tok(PI.str_of_info(h.tokftok),h.tokfx),xs(*
(match Ast.str_of_info (tokext tok) with
| "..." -> Ast_fuzzy.Dots (tokext tok)
| s when Ast_fuzzy.is_metavar s -> Ast_fuzzy.Metavar (s, tokext tok)
| s -> Ast_fuzzy.Tok (s, tokext tok)
*)andauxxs=matchxswith|[]->[]|x::xs->letx',xs'=consumexxsinx'::auxxs'andlook_closeclose_kindtok_startaccbodyxs=matchxswith|[]->raise(Unclosed(spf"look_close '%c'"(char_of_token_kindclose_kind),h.tokftok_start))|x::xs->(matchxwith|tokwhenh.kindtok=close_kind->List.revaccbody,x,xs|_->let(x',xs')=consumexxsinlook_closeclose_kindtok_start(x'::accbody)xs')(* todo? diff with look_close PI.RPar ? *)andlook_close_parentok_startaccbodyxs=matchxswith|[]->raise(Unclosed("look_close_paren",h.tokftok_start))|x::xs->(matchxwith|tokwhenh.kindtok=PI.RPar->List.revaccbody,x,xs|_->let(x',xs')=consumexxsinlook_close_parentok_start(x'::accbody)xs')andsplit_commaxs=letrecauxaccxs=matchxswith|[]->ifnullaccthen[]else[Left(acc|>List.rev)]|x::xs->(matchxwith|Ast_fuzzy.Tok(",",info)->letbefore=acc|>List.revinifnullbeforethenaux[]xselse(Leftbefore)::(Right(info))::aux[]xs|_->aux(x::acc)xs)inaux[]xsinauxxsletmk_tokenshookstoks=toks|>List.map(funtok->hooks.kindtok,hooks.tokftok)(*****************************************************************************)(* Visitor *)(*****************************************************************************)typevisitor_out=trees->unittypevisitor_in={ktree:(tree->unit)*visitor_out->tree->unit;ktrees:(trees->unit)*visitor_out->trees->unit;ktok:(tok->unit)*visitor_out->tok->unit;}let(default_visitor:visitor_in)={ktree=(fun(k,_)x->kx);ktok=(fun(k,_)x->kx);ktrees=(fun(k,_)x->kx);}let(mk_visitor:visitor_in->visitor_out)=funvin->letrecv_treex=letkx=matchxwith|Braces((v1,v2,v3))->let_v1=v_tokv1and_v2=v_treesv2and_v3=v_tokv3in()|Parens((v1,v2,v3))->let_v1=v_tokv1and_v2=Ocaml.v_list(Ocaml.v_eitherv_treesv_tok)v2and_v3=v_tokv3in()|Angle((v1,v2,v3))->let_v1=v_tokv1and_v2=v_treesv2and_v3=v_tokv3in()|Bracket((v1,v2,v3))->let_v1=v_tokv1and_v2=v_treesv2and_v3=v_tokv3in()|Metavarv1->let_v1=v_wrapv1in()|Dotsv1->let_v1=v_tokv1in()|Tokv1->let_v1=v_wrapv1in()invin.ktree(k,all_functions)xandv_treesa=letkxs=matchxswith|[]->()|x::xs->v_treex;v_treesxs;invin.ktrees(k,all_functions)aandv_wrap(_s,x)=v_tokxandv_tokx=letk_x=()invin.ktok(k,all_functions)xandall_functionsx=v_treesxinall_functions(*****************************************************************************)(* Map *)(*****************************************************************************)typemap_visitor={mtok:(tok->tok)->tok->tok;}let(mk_mapper:map_visitor->(trees->trees))=funhook->letrecmap_tree=function|Braces((v1,v2,v3))->letv1=map_tokv1andv2=map_treesv2andv3=map_tokv3inBraces((v1,v2,v3))|Parens((v1,v2,v3))->letv1=map_tokv1andv2=List.map(Ocaml.map_of_eithermap_treesmap_tok)v2andv3=map_tokv3inParens((v1,v2,v3))|Angle((v1,v2,v3))->letv1=map_tokv1andv2=map_treesv2andv3=map_tokv3inAngle((v1,v2,v3))|Bracket((v1,v2,v3))->letv1=map_tokv1andv2=map_treesv2andv3=map_tokv3inBracket((v1,v2,v3))|Metavarv1->letv1=map_wrapv1inMetavar((v1))|Dotsv1->letv1=map_tokv1inDots((v1))|Tokv1->letv1=map_wrapv1inTok((v1))andmap_treesv=List.mapmap_treevandmap_tokv=letkv=vinhook.mtokkvandmap_wrap(s,t)=(s,map_tokt)inmap_trees(*****************************************************************************)(* Extractor *)(*****************************************************************************)let(toks_of_trees:trees->Parse_info.tlist)=funtrees->letglobals=ref[]inlethooks={default_visitorwithktok=(fun(_k,_)i->Common.pushiglobals)}inbeginletvout=mk_visitorhooksinvouttrees;List.rev!globalsend(*****************************************************************************)(* Abstract position *)(*****************************************************************************)letabstract_position_treestrees=lethooks={mtok=(fun(_k)i->{iwithParse_info.token=Parse_info.Ab})}inletmapper=mk_mapperhooksinmappertrees