1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677openResult.Oletok=Result.oktypet=Sexp.tletpeeks=matchStream.peekswith|Somev->okv|None->Error"unexpected end of file"letread_stringsl=letres=Bytes.makel' 'inletrecread=function|vwhenv=l->()|v->BytesLabels.setresv(Stream.nexts);read(v+1)intryread0;ok(Bytes.to_stringres)withStream.Failure->Error(Printf.sprintf"unexpected end of file in atom of size %i"l)letparsestream=letrecread_sizeacc=letc=Stream.nextstreaminifc=':'thenokaccelseletidx=int_of_charc-int_of_char'0'inifidx<0||idx>9thenError(Printf.sprintf"invalid character in size: %c"c)elseread_size((10*acc)+idx)inletrecparse()=peekstream>>=function|'('->Stream.junkstream;parse_list()>>|funl->Sexp.Listl|_->read_size0>>=read_stringstream>>|funx->Sexp.Atomxandparse_list()=peekstream>>=function|')'->Stream.junkstream;ok[]|':'->Error"missing size"|_->lethead=parse()inhead>>=funhead->parse_list()>>|funtail->head::tailinparse()letbuffer()=Buffer.create1024letto_buffer~bufsexp=letrecloop=function|Sexp.Atomstr->Buffer.add_stringbuf(string_of_int(String.lengthstr));Buffer.add_stringbuf":";Buffer.add_stringbufstr|Sexp.List(e:tlist)->Buffer.add_charbuf'(';ignore(List.map~f:loope);Buffer.add_charbuf')'inignore(loopsexp)letto_stringsexp=letbuf=buffer()into_buffersexp~buf;Buffer.contentsbuf