123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496(* Yoann Padioleau
*
* Copyright (C) 2019 r2c
*
* 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.
*)openCommonopenCst_mlmoduleA=Ast_ml(*****************************************************************************)(* Prelude *)(*****************************************************************************)(*
*)(*****************************************************************************)(* Helpers *)(*****************************************************************************)letxxx_listof_axs=xs|>Common.map_filter(function|Common.Leftx->Some(of_ax)|Common.Right_->None)letv_parenof_a(_,x,_)=of_axletv_brace=v_parenletv_bracket=v_parenletv_star_list=xxx_listletv_pipe_list=xxx_listletv_semicolon_list=xxx_listletv_comma_list=xxx_listletv_and_list=xxx_listletv_stringx=xletopt_to_nop=function|None->A.Nop|Somex->xletfake_info()=Parse_info.fake_info"FAKE"letrecv_infox=xandv_tokv=v_infovandv_wrap:'a.('a->'b)->'awrap->'bwrap=fun_of_a(v1,v2)->letv1=_of_av1andv2=v_infov2inv1,v2andv_name=function|Namev1->v1andv_long_name(v1,v2)=letv1=v_qualifierv1andv2=v_namev2inv1,v2andv_qualifierv=v|>List.map(fun(v1,v2)->letv1=v_namev1and_v2=v_tokv2inv1)andv_tyx=matchxwith|TyNamev1->letv1=v_long_namev1inA.TyNamev1|TyVar((v1,v2))->let_v1=v_tokv1andv2=v_namev2inA.TyVarv2|TyTuplev1->letv1=v_star_listv_tyv1inA.TyTuplev1|TyTuple2v1->letv1=v_paren(v_star_listv_ty)v1inA.TyTuplev1|TyFunction((v1,v2,v3))->letv1=v_tyv1and_v2=v_tokv2andv3=v_tyv3inA.TyFunction(v1,v3)|TyApp((v1,v2))->letv1=v_ty_argsv1andv2=v_long_namev2inA.TyApp(v1,v2)|TyTodo->failwith"TyTodo"andv_type_declarationx=matchxwith|TyAbstract((v1,v2))->letv1=v_ty_paramsv1andv2=v_namev2in{A.tname=v2;tparams=v1;tbody=A.AbstractType}|TyDef((v1,v2,v3,v4))->letv1=v_ty_paramsv1andv2=v_namev2and_v3=v_tokv3andv4=v_type_def_kindv4in{A.tname=v2;tparams=v1;tbody=v4}andv_type_def_kind=function|TyCorev1->letv1=v_tyv1inA.CoreTypev1|TyAlgebricv1->letv1=v_pipe_listv_constructor_declarationv1inA.AlgebricTypev1|TyRecordv1->letv1=v_brace(v_semicolon_listv_label_declaration)v1inA.RecordTypev1andv_constructor_declaration(v1,v2)=letv1=v_namev1andv2=v_constructor_argumentsv2inv1,v2andv_constructor_arguments=function|NoConstrArg->[]|Of((v1,v2))->let_v1=v_tokv1andv2=v_star_listv_tyv2inv2andv_label_declarationx=matchxwith{fld_mutable=v_fld_mutable;fld_name=v_fld_name;fld_tok=v_fld_tok;fld_type=v_fld_type}->letv1=v_fld_mutableinletv2=v_namev_fld_nameinlet_v3=v_tokv_fld_tokinletv4=v_tyv_fld_typeinv2,v4,v1andv_ty_args=function|TyArg1v1->letv1=v_tyv1in[v1]|TyArgMultiv1->letv1=v_paren(v_comma_listv_ty)v1inv1andv_ty_params=function|TyNoParam->[]|TyParam1v1->letv1=v_ty_parameterv1in[v1]|TyParamMultiv1->letv1=v_paren(v_comma_listv_ty_parameter)v1inv1andv_ty_parameter(v1,v2)=let_v1=v_tokv1andv2=v_namev2inv2andv_exprv=matchvwith|Cv1->letv1=v_constantv1inA.Lv1|Lv1->letv1=v_long_namev1inA.Namev1|Constr((v1,v2))->letv1=v_long_namev1andv2=Common.map_optv_exprv2inA.Constructor(v1,v2)|Tuplev1->letv1=v_comma_listv_exprv1inA.Tuplev1|Listv1->letv1=v_bracket(v_semicolon_listv_expr)v1inA.Listv1|ParenExprv1->letv1=v_parenv_exprv1inv1|Sequencev1->letv1=v_parenv_seq_exprv1inA.Sequencev1|Prefix((v1,v2))->letv1=v_wrapv_stringv1andv2=v_exprv2inA.Prefix(v1,v2)|Infix((v1,v2,v3))->letv1=v_exprv1andv2=v_wrapv_stringv2andv3=v_exprv3inA.Infix(v1,v2,v3)|FunCallSimple((v1,v2))->letv1=v_long_namev1andv2=List.mapv_argumentv2inA.Call(A.Namev1,v2)|FunCall((v1,v2))->letv1=v_exprv1andv2=List.mapv_argumentv2inA.Call(v1,v2)|RefAccess((v1,v2))->letv1=v_tokv1andv2=v_exprv2inA.RefAccess(v1,v2)|RefAssign((v1,v2,v3))->letv1=v_exprv1andv2=v_tokv2andv3=v_exprv3inA.RefAssign(v1,v2,v3)|FieldAccess((v1,v2,v3))->letv1=v_exprv1and_v2=v_tokv2andv3=v_long_namev3inA.FieldAccess(v1,v3)|FieldAssign((v1,v2,v3,v4,v5))->letv1=v_exprv1and_v2=v_tokv2andv3=v_long_namev3and_v4=v_tokv4andv5=v_exprv5inA.FieldAssign(v1,v3,v5)|Recordv1->let(a,b)=v_bracev_record_exprv1inA.Record(a,b)|ObjAccess((v1,v2,v3))->letv1=v_exprv1and_v2=v_tokv2andv3=v_namev3inA.ObjAccess(v1,v3)|New((v1,v2))->letv1=v_tokv1andv2=v_long_namev2inA.New(v1,v2)|LetIn((v1,v2,v3,v4,v5))->let_v1=v_tokv1andv2=v_rec_optv2andv3=v_and_listv_let_bindingv3and_v4=v_tokv4andv5=v_seq_expr1v5inA.LetIn(v3,v5,v2)|Fun((v1,v2,v3))->let_v1=v_tokv1and__v2=List.mapv_parameterv2and__v3=v_match_actionv3inraiseTodo|Function((v1,v2))->let_v1=v_tokv1and__v2=v_pipe_listv_match_casev2inraiseCommon.Todo|If((v1,v2,v3,v4,v5))->let_v1=v_tokv1andv2=v_seq_expr1v2and_v3=v_tokv3andv4=v_exprv4andv5=Common.map_opt(fun(v1,v2)->let_v1=v_tokv1andv2=v_exprv2inv2)v5inA.If(v2,v4,v5|>opt_to_nop)|Match((v1,v2,v3,v4))->let_v1=v_tokv1andv2=v_seq_expr1v2and_v3=v_tokv3andv4=v_pipe_listv_match_casev4inA.Match(v2,v4)|Try((v1,v2,v3,v4))->let_v1=v_tokv1andv2=v_seq_expr1v2and_v3=v_tokv3andv4=v_pipe_listv_match_casev4inA.Try(v2,v4)|While((v1,v2,v3,v4,v5))->let_v1=v_tokv1andv2=v_seq_expr1v2and_v3=v_tokv3andv4=v_seq_expr1v4and_v5=v_tokv5inA.While(v2,v4)|For((v1,v2,v3,v4,v5,v6,v7,v8,v9))->let_v1=v_tokv1andv2=v_namev2and_v3=v_tokv3andv4=v_seq_expr1v4andv5=v_for_directionv5andv6=v_seq_expr1v6and_v7=v_tokv7andv8=v_seq_expr1v8and_v9=v_tokv9inA.For(v2,v4,v5,v6,v8)|ExprTodo->failwith"ExprTodo"andv_constant=function|Intv1->letv1=v_wrapv_stringv1inA.Intv1|Floatv1->letv1=v_wrapv_stringv1inA.Floatv1|Charv1->letv1=v_wrapv_stringv1inA.Charv1|Stringv1->letv1=v_wrapv_stringv1inA.Stringv1andv_record_expr=function|RecordNormalv1->letv1=v_semicolon_listv_field_and_exprv1inNone,v1|RecordWith((v1,v2,v3))->letv1=v_exprv1and_v2=v_tokv2andv3=v_semicolon_listv_field_and_exprv3inSomev1,v3andv_field_and_exprx=matchxwith|FieldExpr((v1,v2,v3))->letv1=v_long_namev1and_v2=v_tokv2andv3=v_exprv3inv1,v3|FieldImplicitExprv1->letv1=v_long_namev1inv1,A.Namev1(* remove qualifier? *)andv_argumentv=matchvwith|ArgExprv1->letv1=v_exprv1inA.Argv1|ArgLabelTilde((v1,v2))->letv1=v_namev1andv2=v_exprv2inA.ArgKwd(v1,v2)|ArgImplicitTildeExpr((v1,v2))->let_v1=v_tokv1andv2=v_namev2inA.ArgKwd(v2,A.Name([],v2))|ArgLabelQuestion((v1,v2))->let__v1=v_namev1and__v2=v_exprv2inraiseTodo|ArgImplicitQuestionExpr((v1,v2))->let_v1=v_tokv1and__v2=v_namev2inraiseTodoandv_match_action=function|Action((v1,v2))->let_v1=v_tokv1andv2=v_seq_expr1v2inv2,None|WhenAction((v1,v2,v3,v4))->let_v1=v_tokv1andv2=v_seq_expr1v2and_v3=v_tokv3andv4=v_seq_expr1v4inv4,Somev2andv_match_case(v1,v2)=letv1=v_patternv1andv2=v_match_actionv2inv1,v2andv_for_direction=function|Tov1->letv1=v_tokv1inA.Tov1|Downtov1->letv1=v_tokv1inA.Downtov1andv_seq_exprv=v_semicolon_listv_exprvandv_seq_expr1xs=matchv_seq_exprxswith|[]->raiseCommon.Impossible|[x]->x|xs->A.Sequencexsandv_patternx=matchxwith|PatVarv1->letv1=v_namev1inA.PatVarv1|PatConstantv1->letv1=v_signed_constantv1inA.PatLiteralv1|PatConstr((v1,v2))->letv1=v_long_namev1andv2=Common.map_optv_patternv2inA.PatConstructor(v1,v2)|PatConsInfix((v1,v2,v3))->letv1=v_patternv1andv2=v_tokv2andv3=v_patternv3inA.PatConsInfix(v1,v2,v3)|PatTuplev1->letv1=v_comma_listv_patternv1inA.PatTuplev1|PatListv1->letv1=v_bracket(v_semicolon_listv_pattern)v1inA.PatListv1|PatUnderscorev1->letv1=v_tokv1inA.PatUnderscorev1|PatRecordv1->letv1=v_brace(v_semicolon_listv_field_pattern)v1inA.PatRecordv1|PatAs((v1,v2,v3))->letv1=v_patternv1and_v2=v_tokv2andv3=v_namev3inA.PatAs(v1,v3)|PatDisj((v1,v2,v3))->letv1=v_patternv1and_v2=v_tokv2andv3=v_patternv3inA.PatDisj(v1,v3)|PatTyped((v1,v2,v3,v4,v5))->let_v1=v_tokv1andv2=v_patternv2and_v3=v_tokv3andv4=v_tyv4and_v5=v_tokv5inA.PatTyped(v2,v4)|ParenPatv1->letv1=v_parenv_patternv1inv1|PatTodo->failwith"PatTodo"andv_labeled_simple_patternv=v_parametervandv_parameterx=matchxwith|ParamPatv1->letv1=v_patternv1inv1|ParamTodo->failwith"ParamTodo"andv_field_patternx=matchxwith|PatField((v1,v2,v3))->letv1=v_long_namev1and_v2=v_tokv2andv3=v_patternv3inv1,v3|PatImplicitFieldv1->letv1=v_long_namev1inv1,A.PatVar(sndv1)andv_signed_constant=function|C2v1->letv1=v_constantv1inv1|CMinus((v1,v2))->let_v1=v_tokv1andv2=v_constantv2inv2(* TODO: should append - to literal? *)|CPlus((v1,v2))->let_v1=v_tokv1andv2=v_constantv2inv2andv_let_bindingx=matchxwith|LetClassicv1->letv1=v_let_defv1inA.LetClassicv1|LetPattern((v1,v2,v3))->letv1=v_patternv1and_v2=v_tokv2andv3=v_seq_expr1v3inA.LetPattern(v1,v3)andv_let_defx=matchxwith{l_name=v_l_name;l_params=v_l_args;l_tok=v_l_tok;l_body=v_l_body}->letv1=v_namev_l_nameinletv2=List.mapv_labeled_simple_patternv_l_argsinlet_v3=v_tokv_l_tokinletv4=v_seq_expr1v_l_bodyin{A.lname=v1;lparams=v2;lbody=v4}andv_module_exprv=matchvwith|ModuleNamev1->letv1=v_long_namev1inA.ModuleNamev1|ModuleStruct(v1,v2,v3)->let_v1=v_tokv1inletv2=List.mapv_itemv2inlet_v3=v_tokv3inA.ModuleStructv2|ModuleTodo->failwith"ModuleTodo"andv_itemx=matchxwith|Type((v1,v2))->let_v1=v_tokv1andv2=v_and_listv_type_declarationv2inA.Typev2|Exception((v1,v2,v3))->let_v1=v_tokv1andv2=v_namev2andv3=v_constructor_argumentsv3inA.Exception(v2,v3)|External((v1,v2,v3,v4,v5,v6))->let_v1=v_tokv1andv2=v_namev2and_v3=v_tokv3andv4=v_tyv4and_v5=v_tokv5andv6=List.map(v_wrapv_string)v6inA.External(v2,v4,v6)|Open((v1,v2))->let_v1=v_tokv1andv2=v_long_namev2inA.Open(v2)|Val((v1,v2,v3,v4))->let_v1=v_tokv1andv2=v_namev2and_v3=v_tokv3andv4=v_tyv4inA.Val(v2,v4)|Let((v1,v2,v3))->let_v1=v_tokv1andv2=v_rec_optv2andv3=v_and_listv_let_bindingv3inA.Let(v2,v3)|Module((v1,v2,v3,v4))->let_v1=v_tokv1andv2=v_namev2and_v3=v_tokv3andv4=v_module_exprv4inA.Module({A.mname=v2;mbody=v4})|ItemTodo_v->failwith"ItemTodo"andv_rec_optv=Common.map_optv_tokvandv_toplevelx=matchxwith|TopItemv1->letv1=v_itemv1in[v1]|ScScv1->let_v1=v_infov1in[]|TopSeqExprv1->letv1=v_seq_expr1v1in[A.Let(None,[A.LetPattern(A.PatUnderscore(fake_info()),v1)])]|TopDirectivev1->let_v1=v_infov1in[]andprogramv=List.mapv_toplevelv|>List.flatten(*
and v_any = function
| Ty v1 -> let v1 = v_ty v1 in ()
| Expr v1 -> let v1 = v_expr v1 in ()
| Pattern v1 -> let v1 = v_pattern v1 in ()
| Item v1 -> let v1 = v_item v1 in ()
| Toplevel v1 -> let v1 = v_toplevel v1 in ()
| Program v1 -> let v1 = v_program v1 in ()
| TypeDeclaration v1 -> let v1 = v_type_declaration v1 in ()
| TypeDefKind v1 -> let v1 = v_type_def_kind v1 in ()
| MatchCase v1 -> let v1 = v_match_case v1 in ()
| FieldDeclaration v1 -> let v1 = v_label_declaration v1 in ()
| LetBinding v1 -> let v1 = v_let_binding v1 in ()
| Constant v1 -> let v1 = v_constant v1 in ()
| Argument v1 -> let v1 = v_argument v1 in ()
| Body v1 -> let v1 = v_seq_expr v1 in ()
| Info v1 -> let v1 = v_info v1 in ()
| InfoList v1 -> let v1 = Ocaml.v_list v_info v1 in ()
*)