123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287openBaseopenAngstromopenMessage(* Angstrom does not expose [Let_syntax]. *)moduleLet_syntax=struct(* Suppress ``unused value'' warnings *)[@@@ocaml.warning"-32"]letreturn=returnletbindx~f=x>>=fletmapx~f=x>>|fletbothxy=return(funx->x,y)<*>returnxendletwith_header_bytecnext=let%bind(_:char)=charcinnext;;letnil=with_header_byteConstants.nil(returnNil)lettrue_=with_header_byteConstants.true_(return(Booleantrue))letfalse_=with_header_byteConstants.false_(return(Booleanfalse))letbool=true_<|>false_(* See [Constants] for why these are separate functions.
Also, see
https://github.com/msgpack/msgpack/blob/master/spec.md#formats
*)letapply_unmask~unmask~value=valuelandunmaskletunmask~mask~value=valuelandlnotmaskletcheck_mask~mask~value=letopenInt.Oinvaluelandmask=mask;;letpositive_fixint=letopenInt.Oinlet%mapc=satisfy(func->apply_unmask~unmask:Constants.positive_fixint_unmask~value:(Char.to_intc)=Char.to_intc)inChar.to_intc;;letnegative_fixint=let%mapc=satisfy(func->check_mask~mask:Constants.negative_fixint_mask~value:(Char.to_intc))inletn=unmask~mask:Constants.negative_fixint_mask~value:(Char.to_intc)in(* A negative fixint is stored as a 5-bit two's complement integer. To bit-extend this
to be the correct real integer, we set all bits besides the bottom 5.
The reason that we use the literal -32 is so that the mask works on
32-bit and 64-bit architectures.
32-bit computer -32: 11111111_11111111_11111111_11100000
64-bit computer -32: 11111111_11111111_11111111_11111111_11111111_11111111_11111111_11100000
*)lettop_bits_mask=-32innlortop_bits_mask;;letfixint=positive_fixint<|>negative_fixintletuint8=with_header_byteConstants.uint8_headerany_uint8letuint16=with_header_byteConstants.uint16_headerBE.any_uint16(* [Angstrom.BE] does not expose uint32 or uint64... *)letuint32=let%bind(_:char)=charConstants.uint32_headerinlet%mapbs=count2BE.any_uint16in(* Because we can't tell angstrom to automatically parse an unsigned 32-bit integer,
we instead need to roll our own. A number 0xYYZZ is equal to the sum 0xYY << 16 + 0xZZ
and is stored in big endian as YY ZZ.
*)List.fold~f:(funaccv->(acclsl16)+v)~init:0bs;;letuint64=let%mapv=with_header_byteConstants.uint64_headerBE.any_int64inUInt64v;;letint8=with_header_byteConstants.int8_headerany_int8letint16=with_header_byteConstants.int16_headerBE.any_int16letint32=let%bindresult=with_header_byteConstants.int32_headerBE.any_int32inmatchInt32.to_intresultwith|Somei->returni(* This will technically fail back to the outlying choice combinators, but that's
fine, because the spec is designed such that each of those will fail as well.
*)|None->fail"int32 value too big for native integers!";;letint64=let%mapresult=with_header_byteConstants.int64_headerBE.any_int64inInt64result;;letint=let%mapv=choice[fixint;uint8;uint16;uint32;int8;int16;int32]inIntegerv;;letfloat=with_header_byteConstants.float32_headerBE.any_floatletdouble=with_header_byteConstants.float64_headerBE.any_doubleletfloating=let%mapv=float<|>doubleinFloatingv;;letfixstr=(* We can't really use [check_mask] here, because the bit pattern for [fixstr] is
[101XXXXX], which will also accept [111XXXXX] if we mask naively.
*)let%bindc=satisfy(function|'\xa0'..'\xbf'->true|_->false)inletlen=unmask~mask:Constants.fixstr_mask~value:(Char.to_intc)intakelen;;letstr8=let%bindlen=with_header_byteConstants.str8_headerany_uint8intakelen;;letstr16=let%bindlen=with_header_byteConstants.str16_headerBE.any_uint16intakelen;;letstr32=let%bindlen=with_header_byteConstants.str32_headerBE.any_int32inmatchInt32.to_intlenwith|Somei->takei|None->fail"string value is too long!";;letstr=let%mapv=choice[fixstr;str8;str16;str32]inStringv;;letbin8=let%bindlen=with_header_byteConstants.bin8_headerany_uint8inlet%maps=takeleninBytes.of_strings;;letbin16=let%bindlen=with_header_byteConstants.bin16_headerBE.any_uint16inlet%maps=takeleninBytes.of_strings;;letbin32=let%bindlen=with_header_byteConstants.bin32_headerBE.any_int32inmatchInt32.to_intlenwith|None->fail"bytes value is too long!"|Somei->takei>>|Bytes.of_string;;letbin=let%mapv=choice[bin8;bin16;bin32]inBinaryv;;letfixarrayobj_parser=let%bindc=satisfy(function|'\x90'..'\x9f'->true|_->false)inletlen=unmask~mask:Constants.fixarray_mask~value:(Char.to_intc)incountlenobj_parser;;letarray16obj_parser=let%bindlen=with_header_byteConstants.array16_headerBE.any_uint16incountlenobj_parser;;letarray32obj_parser=let%bindlen=with_header_byteConstants.array32_headerBE.any_int32inmatchInt32.to_intlenwith|None->fail"array value is too long!"|Somei->countiobj_parser;;letarrayparser=let%mapv=choice[fixarrayparser;array16parser;array32parser]inArrayv;;letpairparser=let%binda=parserinlet%mapb=parserina,b;;letfixmapobj_parser=let%bindc=satisfy(function|'\x80'..'\x8f'->true|_->false)inletlen=unmask~mask:Constants.fixmap_mask~value:(Char.to_intc)incountlen(pairobj_parser);;letmap16obj_parser=let%bindlen=with_header_byteConstants.map16_headerBE.any_uint16incountlen(pairobj_parser);;letmap32obj_parser=let%bindlen=with_header_byteConstants.map32_headerBE.any_int32inmatchInt32.to_intlenwith|None->fail"map value is too long!"|Somei->counti(pairobj_parser);;letmapparser=let%mapv=choice[fixmapparser;map16parser;map32parser]inMapv;;letcreate_custom~type_id~data:customAngstrom.t=return{type_id;data=Bytes.of_stringdata};;letmake_fixext_parser~header~len=let%bindtype_id=with_header_byteheaderany_int8inlet%binddata=takelenincreate_custom~type_id~data;;letfixext1=make_fixext_parser~header:Constants.fixext1_header~len:1letfixext2=make_fixext_parser~header:Constants.fixext2_header~len:2letfixext4=make_fixext_parser~header:Constants.fixext4_header~len:4letfixext8=make_fixext_parser~header:Constants.fixext8_header~len:8letfixext16=make_fixext_parser~header:Constants.fixext16_header~len:16letext8=let%bindlen=with_header_byteConstants.ext8_headerany_uint8inlet%bindtype_id=any_int8inlet%binddata=takelenincreate_custom~type_id~data;;letext16=let%bindlen=with_header_byteConstants.ext16_headerBE.any_uint16inlet%bindtype_id=any_int8inlet%binddata=takelenincreate_custom~type_id~data;;letext32=let%bindlen=with_header_byteConstants.ext32_headerBE.any_int32inlet%bindtype_id=any_int8inmatchInt32.to_intlenwith|None->fail"map value is too long!"|Somei->let%binddata=takeiincreate_custom~type_id~data;;letext=let%mapv=choice[fixext1;fixext2;fixext4;fixext8;fixext16;ext8;ext16;ext32]inExtensionv;;letatom=choice[nil;bool;int;floating;str;bin;ext;uint64;int64]letmsg=fix(funmsg->choice[atom;arraymsg;mapmsg])letparses=parse_stringmsgs|>Result.map_error~f:(funs->Error.of_strings)