123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133(*****************************************************************************)(* *)(* 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. *)(* *)(*****************************************************************************)openProtocolopenTezos_michelineopenMicheline_parseropenMichelinetype'primparser_result={source:string;unexpanded:stringcanonical;expanded:'primcanonical;expansion_table:(int*(Micheline_parser.location*intlist))list;unexpansion_table:(int*int)list;}typeparsed=Michelson_v1_primitives.primparser_resultletcompare_parsed=Stdlib.compare(* Unexpanded toplevel expression should be a sequence *)letexpand_allsourceasterrors=letunexpanded,loc_table=extract_locationsastinletexpanded,expansion_errors=Michelson_v1_macros.expand_rec(rootunexpanded)inletexpanded,unexpansion_table=extract_locationsexpandedinletexpansion_table=letsorted=List.sort(fun(_,a)(_,b)->Stdlib.compareab)unexpansion_tableinletgrouped=letrecgroup=function|acc,[]->acc|[],(u,e)::r->group([(e,[u])],r)|((pe,us)::raccasacc),(u,e)::r->ife=pethengroup((e,u::us)::racc,r)elsegroup((e,[u])::acc,r)ingroup([],sorted)inmatchList.map2~when_different_lengths:()(fun(l,ploc)(l',elocs)->assert(l=l');(l,(ploc,elocs)))(List.sortStdlib.compareloc_table)(List.sortStdlib.comparegrouped)with|Okv->v|Error()->invalid_arg"Michelson_v1_parser.expand_all"in({source;unexpanded;expanded;expansion_table;unexpansion_table},errors@expansion_errors)letexpand_all_and_recognize_primssourceasterrors=letparsed,errors=expand_allsourceasterrorsinmatchMichelson_v1_primitives.prims_of_stringsparsed.expandedwith|Okexpanded->({parsedwithexpanded},errors)|Errorerrs->leterrs=Environment.wrap_tztraceerrsinletexpanded=Micheline.strip_locations(Seq((),[]))in({parsedwithexpanded},errors@errs)typemicheline_parser=Toplevel|Expressiontype'primprim_type=|Michelson_prim:Michelson_v1_primitives.primprim_type|String:stringprim_typeletparse(typeprim)micheline_parser(prim_type:primprim_type)?checksource=lettokens,lexing_errors=Micheline_parser.tokenizesourceinletast,parsing_errors=matchmicheline_parserwith|Toplevel->letasts,parsing_errors=Micheline_parser.parse_toplevel?checktokensinletstart=min_pointastsandstop=max_pointastsin(Seq({start;stop},asts),parsing_errors)|Expression->Micheline_parser.parse_expression?checktokensinletexpand:string->(location,string)Micheline.node->errortrace->primparser_resultMicheline_parser.parsing_result=matchprim_typewith|Michelson_prim->expand_all_and_recognize_prims|String->expand_allinexpandsourceast(lexing_errors@parsing_errors)letparse_toplevel=parseToplevelMichelson_primletexpand_toplevel=parseToplevelStringletparse_expression=parseExpressionMichelson_primletexpand_expression=parseExpressionStringletexpand_all_and_recognize_prims~source~original=expand_all_and_recognize_primssourceoriginal[]letunrecognize_primsparsed={parsedwithexpanded=Michelson_v1_primitives.strings_of_primsparsed.expanded;}