Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file ppx_protocol_driver.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209openProtocol_convopenStdLabelsmoduleStringMap=Map.Make(String)moduletypeDriver=sigtypetvalto_string_hum:t->stringvalto_list:t->tlistvalof_list:tlist->tvalis_list:t->boolvalto_array:t->tarrayvalof_array:tarray->tvalto_alist:t->(string*t)listvalof_alist:(string*t)list->tvalis_alist:t->boolvalto_int:t->intvalof_int:int->tvalto_int32:t->int32valof_int32:int32->tvalto_int64:t->int64valof_int64:int64->tvalto_float:t->floatvalof_float:float->tvalto_string:t->stringvalof_string:string->tvalis_string:t->boolvalto_bool:t->boolvalof_bool:bool->tvalnull:tvalis_null:t->boolendletstring_map~fstr=letcs=ref[]inString.iter~f:(func->cs:=c::!cs)str;letcs=f(List.rev!cs)inletbytes=Bytes.create(List.lengthcs)inList.iteri~f:(funic->bytes.[i]<-c)cs;Bytes.to_stringbytes(* Convert a_bcd_e_ to aBcdE *)letmangle:string->string=funs->letrecinner:charlist->charlist=function|'_'::c::cs->(Char.uppercasec)::(innercs)|'_'::[]->[]|c::cs->c::(innercs)|[]->[]instring_map~f:innersmoduleMake(Driver:Driver)=structtypet=Driver.ttypeflag=[`Mangleof(string->string)]type'aflags=?flags:flag->'aexceptionProtocol_errorofstring*t(* Register exception printer *)let()=Printexc.register_printer(function|Protocol_error(s,t)->Some(Printf.sprintf"%s, %s"s(Driver.to_string_humt))|_->None)letraise_errorftfmt=Printf.kprintf(funs->raise(Protocol_error(s,t)))fmtletof_variant?flags:_destructt=matchdestructtwith|name,[]->Driver.of_stringname|name,args->Driver.of_list(Driver.of_stringname::args)letto_variant?flags:_constr=function|twhenDriver.is_stringt->constr(Driver.to_stringt,[])|twhenDriver.is_listt->beginmatchDriver.to_listtwith|name::tswhenDriver.is_stringname->constr(Driver.to_stringname,ts)|_->raise_errorft"Variant list must start with a string"end|t->raise_errorft"Variants must be a string or a list"(* Get all the strings, and create a mapping from string to id? *)letto_record:typeab.?flags:flag->(t,a,b)Runtime.structure->a->t->b=fun?flagsspecconstr->letopenRuntimeinletfield_funcx=matchflagswith|None->x|Some(`Manglef)->fxinletrecinner:typeab.orig:t->(t,a,b)Runtime.structure->a->'c->b=fun~orig->function|Cons((field,to_value_func),xs)->letfield_name=field_funcfieldinletcont=innerxsinfunconstrt->letv=tryStringMap.findfield_namet|>to_value_funcwith|Not_found->raise_errorforig"Field not found: %s"field_nameincont~orig(constrv)t|Nil->funa_t->ainletf=innerspecconstrinfunt->letvalues=Driver.to_alistt|>List.fold_left~f:(funm(k,v)->StringMap.addkvm)~init:StringMap.emptyinf~orig:tvaluesletof_record:?flags:flag->(string*t)list->t=fun?flagsassoc->letassoc=matchflagswith|None->assoc|Some`Manglemangle->List.map~f:(fun(k,v)->(manglek,v))associnDriver.of_alistassocletrecto_tuple:typeab.?flags:flag->(t,a,b)Runtime.structure->a->t->b=fun?flags->letopenRuntimeinfunction|Cons((_field,to_value_func),xs)->funconstructort->letl=Driver.to_listtinletv=to_value_func(List.hdl)into_tuple?flagsxs(constructorv)(Driver.of_list(List.tll))|Nil->funa_t->aletof_tuple?flags:_t=Driver.of_list(List.map~f:sndt)letget_option=function|twhenDriver.is_alistt->beginmatchDriver.to_alisttwith|[("__option",t)]->Somet|_->Noneend|_->None(* If the type is an empty list, thats also null. *)letto_option:?flags:flag->(t->'a)->t->'aoption=fun?flags:_to_value_fun->function|twhenDriver.is_nullt->None|t->lett=match(get_optiont)withSomet->t|None->tinSome(to_value_funt)letof_option:?flags:flag->('a->t)->'aoption->t=fun?flags:_of_value_fun->function|None->Driver.null|Somev->letmk_optiont=Driver.of_alist[("__option",t)]inmatchof_value_funvwith|twhenDriver.is_nullt->mk_optiont|twhen(get_optiont)<>None->mk_optiont|t->tletto_list:?flags:flag->(t->'a)->t->'alist=fun?flags:_to_value_funt->List.map~f:to_value_fun(Driver.to_listt)letof_list:?flags:flag->('a->t)->'alist->t=fun?flags:_of_value_funv->List.map~f:of_value_funv|>Driver.of_listletto_array:?flags:flag->(t->'a)->t->'aarray=fun?flags:_to_value_funt->to_listto_value_funt|>Array.of_listletof_array:?flags:flag->('a->t)->'aarray->t=fun?flags:_of_value_funv->Array.to_listv|>of_listof_value_funletto_lazy_t:?flags:flag->(t->'a)->t->'alazy_t=fun?flags:_to_value_funt->Lazy.from_fun(fun()->to_value_funt)letof_lazy_t:?flags:flag->('a->t)->'alazy_t->t=fun?flags:_of_value_funv->Lazy.forcev|>of_value_funletto_int?flags:_t=tryDriver.to_inttwith_->raise_errorft"int expected"letof_int?flags:_v=Driver.of_intvletto_int32?flags:_t=tryDriver.to_int32twith_->raise_errorft"int32 expected"letof_int32?flags:_v=Driver.of_int32vletto_int64?flags:_t=tryDriver.to_int64twith_->raise_errorft"int64 expected"letof_int64?flags:_v=Driver.of_int64vletto_string?flags:_t=tryDriver.to_stringtwith_->raise_errorft"string expected"letof_string?flags:_v=Driver.of_stringvletto_float?flags:_t=tryDriver.to_floattwith_->raise_errorft"float expected"letof_float?flags:_v=Driver.of_floatvletto_bool?flags:_t=tryDriver.to_booltwith_->raise_errorft"bool expected"letof_bool?flags:_v=Driver.of_boolvletto_unit?flagst=to_tuple?flagsRuntime.Nil()tletof_unit?flags()=of_tuple?flags[]end