123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2018 Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)(* *)(* Permission is hereby granted, free of charge, to any person obtaining a *)(* copy of this software and associated documentation files (the "Software"),*)(* to deal in the Software without restriction, including without limitation *)(* the rights to use, copy, modify, merge, publish, distribute, sublicense, *)(* and/or sell copies of the Software, and to permit persons to whom the *)(* Software is furnished to do so, subject to the following conditions: *)(* *)(* The above copyright notice and this permission notice shall be included *)(* in all copies or substantial portions of the Software. *)(* *)(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *)(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *)(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *)(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *)(* DEALINGS IN THE SOFTWARE. *)(* *)(*****************************************************************************)openAlpha_contextopenMichelineopenScript_tc_errorsopenScript_typed_irletdefault_now_annot=Some(`Var_annot"now")letdefault_amount_annot=Some(`Var_annot"amount")letdefault_balance_annot=Some(`Var_annot"balance")letdefault_steps_annot=Some(`Var_annot"steps")letdefault_source_annot=Some(`Var_annot"source")letdefault_sender_annot=Some(`Var_annot"sender")letdefault_self_annot=Some(`Var_annot"self")letdefault_arg_annot=Some(`Var_annot"arg")letdefault_param_annot=Some(`Var_annot"parameter")letdefault_storage_annot=Some(`Var_annot"storage")letdefault_car_annot=Some(`Field_annot"car")letdefault_cdr_annot=Some(`Field_annot"cdr")letdefault_contract_annot=Some(`Field_annot"contract")letdefault_addr_annot=Some(`Field_annot"address")letdefault_manager_annot=Some(`Field_annot"manager")letdefault_pack_annot=Some(`Field_annot"packed")letdefault_unpack_annot=Some(`Field_annot"unpacked")letdefault_slice_annot=Some(`Field_annot"slice")letdefault_elt_annot=Some(`Field_annot"elt")letdefault_key_annot=Some(`Field_annot"key")letdefault_hd_annot=Some(`Field_annot"hd")letdefault_tl_annot=Some(`Field_annot"tl")letdefault_some_annot=Some(`Field_annot"some")letdefault_left_annot=Some(`Field_annot"left")letdefault_right_annot=Some(`Field_annot"right")letdefault_binding_annot=Some(`Field_annot"bnd")letunparse_type_annot:type_annotoption->stringlist=function|None->[]|Some`Type_annota->[":"^a]letunparse_var_annot:var_annotoption->stringlist=function|None->[]|Some`Var_annota->["@"^a]letunparse_field_annot:field_annotoption->stringlist=function|None->[]|Some`Field_annota->["%"^a]letfield_to_var_annot:field_annotoption->var_annotoption=function|None->None|Some(`Field_annots)->Some(`Var_annots)lettype_to_var_annot:type_annotoption->var_annotoption=function|None->None|Some(`Type_annots)->Some(`Var_annots)letvar_to_field_annot:var_annotoption->field_annotoption=function|None->None|Some(`Var_annots)->Some(`Field_annots)letdefault_annot~default=function|None->default|annot->annotletgen_access_annot:var_annotoption->?default:field_annotoption->field_annotoption->var_annotoption=funvalue_annot?(default=None)field_annot->matchvalue_annot,field_annot,defaultwith|None,None,_|Some_,None,None|None,Some`Field_annot"",_->None|None,Some`Field_annotf,_->Some(`Var_annotf)|Some`Var_annotv,(None|Some`Field_annot""),Some`Field_annotf->Some(`Var_annot(String.concat"."[v;f]))|Some`Var_annotv,Some`Field_annotf,_->Some(`Var_annot(String.concat"."[v;f]))letmerge_type_annot:legacy:bool->type_annotoption->type_annotoption->type_annotoptiontzresult=fun~legacyannot1annot2->matchannot1,annot2with|None,None|Some_,None|None,Some_->okNone|Some`Type_annota1,Some`Type_annota2->iflegacy||String.equala1a2thenokannot1elseerror(Inconsistent_annotations(":"^a1,":"^a2))letmerge_field_annot:legacy:bool->field_annotoption->field_annotoption->field_annotoptiontzresult=fun~legacyannot1annot2->matchannot1,annot2with|None,None|Some_,None|None,Some_->okNone|Some`Field_annota1,Some`Field_annota2->iflegacy||String.equala1a2thenokannot1elseerror(Inconsistent_annotations("%"^a1,"%"^a2))letmerge_var_annot:var_annotoption->var_annotoption->var_annotoption=funannot1annot2->matchannot1,annot2with|None,None|Some_,None|None,Some_->None|Some`Var_annota1,Some`Var_annota2->ifString.equala1a2thenannot1elseNoneleterror_unexpected_annotlocannot=matchannotwith|[]->ok()|_::_->error(Unexpected_annotationloc)letfail_unexpected_annotlocannot=Lwt.return(error_unexpected_annotlocannot)letparse_annotsloc?(allow_special_var=false)?(allow_special_field=false)l=(* allow emtpty annotations as wildcards but otherwise only accept
annotations that start with [a-zA-Z_] *)letsub_or_wildcard~specialswrapsacc=letlen=String.lengthsinifCompare.Int.(len=1)thenok@@wrapNone::accelsematchs.[1]with|'a'..'z'|'A'..'Z'|'_'->ok@@wrap(Some(String.subs1(len-1)))::acc|'@'whenCompare.Int.(len=2)&&List.mem'@'specials->ok@@wrap(Some"@")::acc|'%'whenList.mem'%'specials->ifCompare.Int.(len=2)thenok@@wrap(Some"%")::accelseifCompare.Int.(len=3)&&Compare.Char.(s.[2]='%')thenok@@wrap(Some"%%")::accelseerror(Unexpected_annotationloc)|_->error(Unexpected_annotationloc)inList.fold_left(funaccs->acc>>?funacc->ifCompare.Int.(String.lengths=0)thenerror(Unexpected_annotationloc)elsematchs.[0]with|':'->sub_or_wildcard~specials:[](funa->`Type_annota)sacc|'@'->sub_or_wildcard~specials:(ifallow_special_varthen['%']else[])(funa->`Var_annota)sacc|'%'->sub_or_wildcard~specials:(ifallow_special_fieldthen['@']else[])(funa->`Field_annota)sacc|_->error(Unexpected_annotationloc))(ok[])l>|?List.revletopt_var_of_var_opt=function|`Var_annotNone->None|`Var_annotSomea->Some(`Var_annota)letopt_field_of_field_opt=function|`Field_annotNone->None|`Field_annotSomea->Some(`Field_annota)letopt_type_of_type_opt=function|`Type_annotNone->None|`Type_annotSomea->Some(`Type_annota)letclassify_annotlocl:(var_annotoptionlist*type_annotoptionlist*field_annotoptionlist)tzresult=trylet_,rv,_,rt,_,rf=List.fold_left(fun(in_v,rv,in_t,rt,in_f,rf)a->matcha,in_v,rv,in_t,rt,in_f,rfwith|(`Var_annot_asa),true,_,_,_,_,_|(`Var_annot_asa),false,[],_,_,_,_->true,opt_var_of_var_opta::rv,false,rt,false,rf|(`Type_annot_asa),_,_,true,_,_,_|(`Type_annot_asa),_,_,false,[],_,_->false,rv,true,opt_type_of_type_opta::rt,false,rf|(`Field_annot_asa),_,_,_,_,true,_|(`Field_annot_asa),_,_,_,_,false,[]->false,rv,false,rt,true,opt_field_of_field_opta::rf|_->raiseExit)(false,[],false,[],false,[])linok(List.revrv,List.revrt,List.revrf)withExit->error(Ungrouped_annotationsloc)letget_one_annotloc=function|[]->okNone|[a]->oka|_->error(Unexpected_annotationloc)letget_two_annotloc=function|[]->ok(None,None)|[a]->ok(a,None)|[a;b]->ok(a,b)|_->error(Unexpected_annotationloc)letparse_type_annot:int->stringlist->type_annotoptiontzresult=funlocannot->parse_annotslocannot>>?classify_annotloc>>?fun(vars,types,fields)->error_unexpected_annotlocvars>>?fun()->error_unexpected_annotlocfields>>?fun()->get_one_annotloctypesletparse_type_field_annot:int->stringlist->(type_annotoption*field_annotoption)tzresult=funlocannot->parse_annotslocannot>>?classify_annotloc>>?fun(vars,types,fields)->error_unexpected_annotlocvars>>?fun()->get_one_annotloctypes>>?funt->get_one_annotlocfields>|?funf->(t,f)letparse_composed_type_annot:int->stringlist->(type_annotoption*field_annotoption*field_annotoption)tzresult=funlocannot->parse_annotslocannot>>?classify_annotloc>>?fun(vars,types,fields)->error_unexpected_annotlocvars>>?fun()->get_one_annotloctypes>>?funt->get_two_annotlocfields>|?fun(f1,f2)->(t,f1,f2)letparse_field_annot:int->stringlist->field_annotoptiontzresult=funlocannot->parse_annotslocannot>>?classify_annotloc>>?fun(vars,types,fields)->error_unexpected_annotlocvars>>?fun()->error_unexpected_annotloctypes>>?fun()->get_one_annotlocfieldsletextract_field_annot:Script.node->(Script.node*field_annotoption)tzresult=function|Prim(loc,prim,args,annot)->letrecextract_firstacc=function|[]->None,annot|s::rest->ifCompare.Int.(String.lengths>0)&&Compare.Char.(s.[0]='%')thenSomes,List.rev_appendaccrestelseextract_first(s::acc)restinletfield_annot,annot=extract_first[]annotinletfield_annot=matchfield_annotwith|None->None|Somefield_annot->Some(`Field_annot(String.subfield_annot1(String.lengthfield_annot-1)))inok(Prim(loc,prim,args,annot),field_annot)|expr->ok(expr,None)letcheck_correct_field:field_annotoption->field_annotoption->unittzresult=funf1f2->matchf1,f2with|None,_|_,None->ok()|Some`Field_annots1,Some`Field_annots2->ifString.equals1s2thenok()elseerror(Inconsistent_field_annotations("%"^s1,"%"^s2))letparse_var_annot:int->?default:var_annotoption->stringlist->var_annotoptiontzresult=funloc?defaultannot->parse_annotslocannot>>?classify_annotloc>>?fun(vars,types,fields)->error_unexpected_annotloctypes>>?fun()->error_unexpected_annotlocfields>>?fun()->get_one_annotlocvars>|?function|Some_asa->a|None->matchdefaultwith|Somea->a|None->Noneletsplit_last_dot=function|None->None,None|Some`Field_annots->matchString.rindex_opts'.'with|None->None,Some(`Field_annots)|Somei->lets1=String.subs0iinlets2=String.subs(i+1)(String.lengths-i-1)inletf=ifCompare.String.equals2"car"||Compare.String.equals2"cdr"thenNoneelseSome(`Field_annots2)inSome(`Var_annots1),fletcommon_prefixv1v2=matchv1,v2with|Some(`Var_annots1),Some(`Var_annots2)whenCompare.String.equals1s2->v1|Some_,None->v1|None,Some_->v2|_,_->Noneletparse_constr_annot:int->?if_special_first:field_annotoption->?if_special_second:field_annotoption->stringlist->(var_annotoption*type_annotoption*field_annotoption*field_annotoption)tzresult=funloc?if_special_first?if_special_secondannot->parse_annots~allow_special_field:truelocannot>>?classify_annotloc>>?fun(vars,types,fields)->get_one_annotlocvars>>?funv->get_one_annotloctypes>>?funt->get_two_annotlocfields>>?fun(f1,f2)->beginmatchif_special_first,f1with|Somespecial_var,Some`Field_annot"@"->ok(split_last_dotspecial_var)|None,Some`Field_annot"@"->error(Unexpected_annotationloc)|_,_->ok(v,f1)end>>?fun(v1,f1)->beginmatchif_special_second,f2with|Somespecial_var,Some`Field_annot"@"->ok(split_last_dotspecial_var)|None,Some`Field_annot"@"->error(Unexpected_annotationloc)|_,_->ok(v,f2)end>|?fun(v2,f2)->letv=matchvwith|None->common_prefixv1v2|Some_->vin(v,t,f1,f2)letparse_two_var_annot:int->stringlist->(var_annotoption*var_annotoption)tzresult=funlocannot->parse_annotslocannot>>?classify_annotloc>>?fun(vars,types,fields)->error_unexpected_annotloctypes>>?fun()->error_unexpected_annotlocfields>>?fun()->get_two_annotlocvarsletparse_destr_annot:int->stringlist->default_accessor:field_annotoption->field_name:field_annotoption->pair_annot:var_annotoption->value_annot:var_annotoption->(var_annotoption*field_annotoption)tzresult=funlocannot~default_accessor~field_name~pair_annot~value_annot->parse_annotsloc~allow_special_var:trueannot>>?classify_annotloc>>?fun(vars,types,fields)->error_unexpected_annotloctypes>>?fun()->get_one_annotlocvars>>?funv->get_one_annotlocfields>|?funf->letdefault=gen_access_annotpair_annotfield_name~default:default_accessorinletv=matchvwith|Some`Var_annot"%"->field_to_var_annotfield_name|Some`Var_annot"%%"->default|Some_->v|None->value_annotin(v,f)letparse_entrypoint_annot:int->?default:var_annotoption->stringlist->(var_annotoption*field_annotoption)tzresult=funloc?defaultannot->parse_annotslocannot>>?classify_annotloc>>?fun(vars,types,fields)->error_unexpected_annotloctypes>>?fun()->get_one_annotlocfields>>?funf->get_one_annotlocvars>|?function|Some_asa->(a,f)|None->matchdefaultwith|Somea->(a,f)|None->(None,f)letparse_var_type_annot:int->stringlist->(var_annotoption*type_annotoption)tzresult=funlocannot->parse_annotslocannot>>?classify_annotloc>>?fun(vars,types,fields)->error_unexpected_annotlocfields>>?fun()->get_one_annotlocvars>>?funv->get_one_annotloctypes>|?funt->(v,t)