123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271(****************************************************************************)(* *)(* This file is part of MOPSA, a Modular Open Platform for Static Analysis. *)(* *)(* Copyright (C) 2017-2019 The MOPSA Project. *)(* *)(* This program is free software: you can redistribute it and/or modify *)(* it under the terms of the GNU Lesser General Public License as published *)(* by the Free Software Foundation, either version 3 of the License, or *)(* (at your option) any later version. *)(* *)(* 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 *)(* GNU Lesser General Public License for more details. *)(* *)(* You should have received a copy of the GNU Lesser General Public License *)(* along with this program. If not, see <http://www.gnu.org/licenses/>. *)(* *)(****************************************************************************)(** Pretty printer of the Python extension to the AST. *)openMopsaopenAstopenUniversal.AstopenAddropenFormatlet()=register_constant_compare(fundefaultc1c2->matchc1,c2with|C_py_imagf1,C_py_imagf2->Stdlib.comparef1f2|C_py_none,C_py_none|C_py_ellipsis,C_py_ellipsis|C_py_not_implemented,C_py_not_implemented|_->defaultc1c2);register_expr_compare(fundefaulte1e2->matchekinde1,ekinde2with|E_py_ll_getattr(e11,e12),E_py_ll_getattr(e21,e22)|E_py_ll_hasattr(e11,e12),E_py_ll_hasattr(e21,e22)->Compare.compose[(fun()->compare_expre11e21);(fun()->compare_expre21e22);]|E_py_ll_setattr(e11,e12,oe13),E_py_ll_setattr(e21,e22,oe23)->Compare.compose[(fun()->compare_expre11e21);(fun()->compare_expre21e22);(fun()->OptionExt.comparecompare_exproe13oe23);]|E_py_annote1,E_py_annote2->compare_expre1e2|E_py_undefinedb1,E_py_undefinedb2->Stdlib.compareb1b2|E_py_object(a1,oe1),E_py_object(a2,oe2)->Compare.compose[(fun()->compare_addra1a2);(fun()->Compare.optioncompare_exproe1oe2);]|E_py_tuplel1,E_py_tuplel2|E_py_listl1,E_py_listl2|E_py_setl1,E_py_setl2->Compare.listcompare_exprl1l2|E_py_index_subscript(o1,i1),E_py_index_subscript(o2,i2)->Compare.compose[(fun()->compare_expro1o2);(fun()->compare_expri1i2);]|E_py_slice_subscript(o1,s1,e1,st1),E_py_slice_subscript(o2,s2,e2,st2)->Compare.compose(List.fold_left2(funaccel1el2->(fun()->compare_exprel1el2)::acc)[][o1;s1;e1;st1][o2;s2;e2;st2])|E_py_attribute(e1,s1),E_py_attribute(e2,s2)->Compare.compose[(fun()->compare_expre1e2);(fun()->Stdlib.compares1s2);]|E_py_dict(k1,v1),E_py_dict(k2,v2)->Compare.compose[(fun()->Compare.listcompare_exprk1k2);(fun()->Compare.listcompare_exprv1v2);]|E_py_generator_comprehension(e,comprs),E_py_generator_comprehension(e',comprs')|E_py_list_comprehension(e,comprs),E_py_list_comprehension(e',comprs')|E_py_set_comprehension(e,comprs),E_py_set_comprehension(e',comprs')->Compare.compose[(fun()->compare_expree');(fun()->Compare.list(fun(target,iter,conds)(target',iter',conds')->Compare.compose[(fun()->compare_exprtargettarget');(fun()->compare_expriteriter');(fun()->Compare.listcompare_exprcondsconds');])comprscomprs');]|E_py_dict_comprehension(k,v,comprs),E_py_dict_comprehension(k',v',comprs')->Compare.compose[(fun()->compare_exprkk');(fun()->compare_exprvv');(fun()->Compare.list(fun(target,iter,conds)(target',iter',conds')->Compare.compose[(fun()->compare_exprtargettarget');(fun()->compare_expriteriter');(fun()->Compare.listcompare_exprcondsconds');])comprscomprs');]|E_py_call(f1,args1,kwargs1),E_py_call(f2,args2,kwargs2)->Compare.compose[(fun()->compare_exprf1f2);(fun()->Compare.listcompare_exprargs1args2);(fun()->Compare.list(fun(so1,e1)(so2,e2)->Compare.compose[(fun()->Compare.optionStdlib.compareso1so2);(fun()->compare_expre1e2)])kwargs1kwargs2);]|E_py_yield_frome1,E_py_yield_frome2|E_py_yielde1,E_py_yielde2->compare_expre1e2|E_py_if(t1,b1,e1),E_py_if(t2,b2,e2)->Compare.compose[(fun()->compare_exprt1t2);(fun()->compare_exprb1b2);(fun()->compare_expre1e2);]|E_py_bytesb1,E_py_bytesb2->Stdlib.compareb1b2|E_py_lambda_,E_py_lambda_->Exceptions.panic"compare py lambdas"|E_py_multi_compare(l1,op,l2),E_py_multi_compare(l1',op',l2')->Compare.compose[(fun()->compare_exprl1l1');(fun()->Compare.listcompare_operatoropop');(fun()->Compare.listcompare_exprl2l2');]|E_py_check_annot(e11,e12),E_py_check_annot(e21,e22)->Compare.compose[(fun()->compare_expre11e12);(fun()->compare_expre12e22);]|_->defaulte1e2);register_stmt_compare(fundefaults1s2->matchskinds1,skinds2with|S_py_classc1,S_py_classc2->Compare.compose[(fun()->compare_varc1.py_cls_varc2.py_cls_var);(fun()->compare_stmtc1.py_cls_bodyc2.py_cls_body);(fun()->Compare.listcompare_varc1.py_cls_static_attributesc2.py_cls_static_attributes);(fun()->Compare.listcompare_exprc1.py_cls_basesc2.py_cls_bases);(fun()->Compare.listcompare_exprc1.py_cls_decorsc2.py_cls_decors);(fun()->Compare.list(Compare.pair(Compare.optionStdlib.compare)compare_expr)c1.py_cls_keywordsc2.py_cls_keywords);(fun()->compare_rangec1.py_cls_rangec2.py_cls_range);]|S_py_functionf1,S_py_functionf2->Compare.compose[(fun()->compare_varf1.py_func_varf2.py_func_var);(fun()->Compare.listcompare_varf1.py_func_parametersf2.py_func_parameters);(fun()->Compare.list(Compare.optioncompare_expr)f1.py_func_defaultsf2.py_func_defaults);(fun()->compare_stmtf1.py_func_bodyf2.py_func_body);(fun()->Stdlib.comparef1.py_func_is_generatorf2.py_func_is_generator);(fun()->Compare.listcompare_exprf1.py_func_decorsf2.py_func_decors);(fun()->compare_rangef1.py_func_rangef2.py_func_range);]|S_py_try(b1,e1,l1,f1),S_py_try(b2,e2,l2,f2)->Compare.compose[(fun()->compare_stmtb1b2);(fun()->Compare.list(fune1e2->Compare.compose[(fun()->Compare.optioncompare_expre1.py_excpt_typee2.py_excpt_type);(fun()->Compare.optioncompare_vare1.py_excpt_namee2.py_excpt_name);(fun()->compare_stmte1.py_excpt_bodye2.py_excpt_body);])e1e2);(fun()->compare_stmtl1l2);(fun()->compare_stmtf1f2);]|S_py_raiseo1,S_py_raiseo2->Compare.optioncompare_expro1o2|S_py_if(c1,t1,e1),S_py_if(c2,t2,e2)|S_py_while(c1,t1,e1),S_py_while(c2,t2,e2)->Compare.compose[(fun()->compare_exprc1c2);(fun()->compare_stmtt1t2);(fun()->compare_stmte1e2);]|S_py_multi_assign(ls1,r1),S_py_multi_assign(ls2,r2)->Compare.compose[(fun()->Compare.listcompare_exprls1ls2);(fun()->compare_exprr1r2);]|S_py_aug_assign(l1,o1,r1),S_py_aug_assign(l2,o2,r2)->Compare.compose[(fun()->compare_exprl1l2);(fun()->compare_operatoro1o2);(fun()->compare_exprr1r2);]|S_py_annot(v1,ty1),S_py_annot(v2,ty2)->Compare.compose[(fun()->compare_exprv1v2);(fun()->compare_exprty1ty2);]|S_py_check_annot(v1,ty1),S_py_check_annot(v2,ty2)->Compare.compose[(fun()->compare_exprv1v2);(fun()->compare_exprty1ty2);]|S_py_for(t1,i1,b1,e1),S_py_for(t2,i2,b2,e2)->Compare.compose[(fun()->compare_exprt1t2);(fun()->compare_expri1i2);(fun()->compare_stmtb1b2);(fun()->compare_stmte1e2);]|S_py_import(m1,a1,r1),S_py_import(m2,a2,r2)->Compare.compose[(fun()->Stdlib.comparem1m2);(fun()->Compare.optioncompare_vara1a2);(fun()->compare_varr1r2);]|S_py_import_from(m1,n1,r1,v1),S_py_import_from(m2,n2,r2,v2)->Compare.compose[(fun()->Stdlib.comparem1m2);(fun()->Stdlib.comparen1n2);(fun()->compare_varr1r2);(fun()->compare_varv1v2);]|S_py_deletee1,S_py_deletee2->compare_expre1e2|S_py_assert(e1,o1),S_py_assert(e2,o2)->Compare.compose[(fun()->compare_expre1e2);(fun()->Compare.optioncompare_expro1o2);]|S_py_with(c1,a1,b1),S_py_with(c2,a2,b2)->Compare.compose[(fun()->compare_exprc1c2);(fun()->Compare.optioncompare_expra1a2);(fun()->compare_stmtb1b2);]|_->defaults1s2)