123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344(* 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.
*)openCommon(* A is the pattern, and B the concrete source code. For now
* we both use the same module, Ast_fuzzy, but they may differ later
* as the expressivity of the pattern language grows.
*)moduleA=Ast_fuzzymoduleB=Ast_fuzzy(*****************************************************************************)(* Prelude *)(*****************************************************************************)(*
* This module provides a big functor, X_VS_X, which can be used
* to match some Ast_fuzzy trees against other Ast_fuzzy trees in
* a flexible way.
*)(*****************************************************************************)(* Functor parameter combinators *)(*****************************************************************************)(* This is the interface of the structure that will be passed to the
* X_VS_X functor below.
*)moduletypePARAM=sig(* tin is for 'type in' and tout for 'type out' *)typetintype'xtout(* A matcher is something taking an element A and an element B
* (for X_VS_X below, A will be the AST of the pattern and B
* the AST of the program we want to match over), then some environment
* information tin, and it will return something (tout) that will
* encapsulate the possible matched element A and B.
*
* If you just want to do a simple matcher that just returns
* a boolean, then instantiate the PARAM struct with
* type tin = unit (* no environment information *)
* type ('a * 'b) tout = ('a * 'b) option
* and if the toplevel matching returns a None, then you know
* A didn't match B.
*)type('a,'b)matcher='a->'b->tin->('a*'b)tout(* The >>= combinator below allows you to configure the matching process
* anyway you want. Essentially this combinator takes a matcher,
* another matcher, and returns a matcher that combine the 2
* matcher arguments.
*
* In the case of a simple boolean matcher, you just need to write:
*
* let (>>=) m1 m2 = fun tin ->
* match m1 tin with
* | None -> None
* | Some (a,b) ->
* m2 (a, b) tin
*)val(>>=):(tin->('a*'b)tout)->('a*'b->(tin->('c*'d)tout))->(tin->('c*'d)tout)(* the disjunctive combinator *)val(>||>):(tin->'xtout)->(tin->'xtout)->(tin->'xtout)(* The classical monad combinators *)valreturn:('a*'b)->tin->('a*'b)toutvalfail:tin->('a*'b)toutvaltokenf:(Parse_info.t,Parse_info.t)matchervalenvf:(Metavars_fuzzy.mvar*Parse_info.t,Ast_fuzzy.trees)matcherend(*****************************************************************************)(* Helpers *)(*****************************************************************************)letis_NoTransfotok=matchtok.Parse_info.transfowith|Parse_info.NoTransfo->true|_->falseletis_Removetok=matchtok.Parse_info.transfowith|Parse_info.Remove->true|_->false(*****************************************************************************)(* Functor code, "X vs X" *)(*****************************************************************************)moduleX_VS_X=functor(X:PARAM)->structtype('a,'b)matcher='a->'b->X.tin->('a*'b)X.toutlet(>>=)=X.(>>=)(*let (>||>) = X.(>||>) *)letreturn=X.returnletfail()=X.fail(* ---------------------------------------------------------------------- *)(* tokens *)(* ---------------------------------------------------------------------- *)letm_tokab=X.tokenfab(* ---------------------------------------------------------------------- *)(* list of trees *)(* ---------------------------------------------------------------------- *)letrecm_list__m_treexsaxsb=matchxsa,xsbwith|[],[]->return([],[])(* iso: allow "..." to match any list of trees *)|[A.Dots_t],_bbs->(* todo: check no annot on t *)return(xsa,xsb)|xa::aas,xb::bbs->m_treexaxb>>=(fun(xa,xb)->m_list__m_treeaasbbs>>=(fun(aas,bbs)->return(xa::aas,xb::bbs)))|[],_::_|_::_,_->fail()(* ---------------------------------------------------------------------- *)(* list of trees in Parens context *)(* ---------------------------------------------------------------------- *)andm_argumentsxsaxsb=matchxsa,xsbwith|[],[]->return([],[])(* iso on ... *)|[Left[(A.Dotst)]],_bbs->(* less: if just Remove, then could apply the transfo on bbs? *)ifis_NoTransfotthenreturn(xsa,xsb)elsefailwith("transformation (minus or plus) on '...' not allowed, "^"rewrite your spatch")(* bugfix: we can have some Replace or AddAfter in the token of
* the comma. We need to apply it to the code.
*)|[Righta;Left[A.Dotsi]],Rightb::bbs->m_tokab>>=(fun(a,b)->return([Righta;Left[A.Dotsi]],Rightb::bbs))(* '...' can also match no argument *)|[Righta;Left[A.Dots_i]],[]->ifis_NoTransfoa||is_Removeathenreturn(xsa,xsb)elsefailwith("transformation (minus or plus) on ',' not allowed when used with "^"'...'. Rewrite your spatch: put your trailing comma on the line "^"with the '...'. See also "^"https://github.com/facebook/pfff/wiki/Spatch#wiki-spacing-issues")|[Right_;Left[A.Dots_i]],_bbs->raiseImpossible|Left[A.Dots_i]::_xs,_bbs->failwith"... is allowed for now only at the end. Give money to pad to get this feature"|xa::aas,xb::bbs->m_either_m_argumentxaxb>>=(fun(xa,xb)->m_argumentsaasbbs>>=(fun(aas,bbs)->return(xa::aas,xb::bbs)))|[],_|_::_,_->fail()andm_either_m_argumentab=matcha,bwith|Left[A.Metavar(s,tok)],Leftb->X.envf(s,tok)b>>=(function|((s,_a),b)->return(Left[A.Metavar(s,tok)],Leftb))|Lefta,Leftb->m_treesab>>=(fun(a,b)->return(Lefta,Leftb))|Righta,Rightb->m_tokab>>=(fun(a,b)->return(Righta,Rightb))|Left_,Right_|Right_,Left_->fail()(* ---------------------------------------------------------------------- *)(* tree *)(* ---------------------------------------------------------------------- *)andm_treeab=matcha,bwith|A.Metavar(s,tok),b->letok=matchbwith|B.Parens_->true(* we don't want metavars to match symbols *)|B.Tok(s,_)->s=~"^[a-zA-Z]"(* in some languages $xx is actually an ident *)|B.Metavar_->true|_->falseinifokthenX.envf(s,tok)[b]>>=(function|((s,_a),[b])->return(A.Metavar(s,tok),b)|_->raiseImpossible)elsefail()|A.Braces(a1,a2,a3),B.Braces(b1,b2,b3)->m_toka1b1>>=(fun(a1,b1)->m_treesa2b2>>=(fun(a2,b2)->m_toka3b3>>=(fun(a3,b3)->return(A.Braces(a1,a2,a3),B.Braces(b1,b2,b3)))))|A.Bracket(a1,a2,a3),B.Bracket(b1,b2,b3)->m_toka1b1>>=(fun(a1,b1)->m_treesa2b2>>=(fun(a2,b2)->m_toka3b3>>=(fun(a3,b3)->return(A.Bracket(a1,a2,a3),B.Bracket(b1,b2,b3)))))|A.Parens(a1,a2,a3),B.Parens(b1,b2,b3)->m_toka1b1>>=(fun(a1,b1)->m_argumentsa2b2>>=(fun(a2,b2)->m_toka3b3>>=(fun(a3,b3)->return(A.Parens(a1,a2,a3),B.Parens(b1,b2,b3)))))|A.Angle(a1,a2,a3),B.Angle(b1,b2,b3)->m_toka1b1>>=(fun(a1,b1)->m_treesa2b2>>=(fun(a2,b2)->m_toka3b3>>=(fun(a3,b3)->return(A.Angle(a1,a2,a3),B.Angle(b1,b2,b3)))))|A.Toka1,B.Tokb1->m_wrapa1b1>>=(fun(a1,b1)->return(A.Toka1,B.Tokb1))|A.Braces_,_|A.Bracket_,_|A.Parens_,_|A.Angle_,_|A.Tok_,_|A.Dots_,_->fail()andm_wrap(a1,a2)(b1,b2)=m_toka2b2>>=(fun(a2,b2)->return((a1,a2),(b1,b2)))andm_treesab=m_list__m_treeabend