Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file pb_codegen_ocaml_type_dump.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229(*
The MIT License (MIT)
Copyright (c) 2016 Maxime Ransan <maxime.ransan@gmail.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.
*)moduleOt=Pb_codegen_ocaml_typemoduleF=Pb_codegen_formatting(** OCaml type representation dumping plugin *)modulePP=structopenOt(* Helper function to convert payload_kind to string *)letstring_of_payload_kindpk=matchpkwith|Pk_varintzigzag->Printf.sprintf"Pk_varint (zigzag: %b)"zigzag|Pk_bits32->"Pk_bits32"|Pk_bits64->"Pk_bits64"|Pk_bytes->"Pk_bytes"(* Helper function to convert basic_type to string *)letstring_of_basic_typebt=matchbtwith|Bt_string->"Bt_string"|Bt_float->"Bt_float"|Bt_int->"Bt_int"|Bt_int32->"Bt_int32"|Bt_uint32->"Bt_uint32"|Bt_int64->"Bt_int64"|Bt_uint64->"Bt_uint64"|Bt_bytes->"Bt_bytes"|Bt_bool->"Bt_bool"(* Helper function to convert repeated_type to string *)letstring_of_repeated_typert=matchrtwith|Rt_list->"Rt_list"|Rt_repeated_field->"Rt_repeated_field"(* Helper function to convert associative_type to string *)letstring_of_associative_typeat=matchatwith|At_list->"At_list"|At_hashtable->"At_hashtable"(* Helper function to convert constant to string *)letstring_of_constantconstant=matchconstantwith|Pb_option.Constant_strings->Printf.sprintf"Constant_string %S"(String.escapeds)|Constant_boolb->Printf.sprintf"Constant_bool %b"b|Constant_inti->Printf.sprintf"Constant_int %d"i|Constant_floatf->Printf.sprintf"Constant_float %f"f|Constant_literals->Printf.sprintf"Constant_literal %S"(String.escapeds)(* Helper function to convert default_value to string *)letstring_of_default_valuedv=matchdvwith|None->"None"|Somevalue->string_of_constantvalue(* Recursive function to print a record field type *)letrecprint_record_field_typescrf_type=matchrf_typewith|Rft_nolabel(ftype,enc_num,pk)->F.linepsc" Rft_nolabel (Field Type: %s, Encoding: %d, Payload Kind: %s)"(string_of_field_typeftype)enc_num(string_of_payload_kindpk)|Rft_required(ftype,enc_num,pk,dv)->F.linepsc" Rft_required (Field Type: %s, Encoding: %d, Payload Kind: %s, \
Default Value: %s)"(string_of_field_typeftype)enc_num(string_of_payload_kindpk)(string_of_default_valuedv)|Rft_optional(ftype,enc_num,pk,dv)->F.linepsc" Rft_optional (Field Type: %s, Encoding: %d, Payload Kind: %s, \
Default Value: %s)"(string_of_field_typeftype)enc_num(string_of_payload_kindpk)(string_of_default_valuedv)|Rft_repeated(rt,ftype,enc_num,pk,packed)->F.linepsc" Rft_repeated (Repeated Type: %s, Field Type: %s, Encoding: %d, \
Payload Kind: %s, Packed: %b)"(string_of_repeated_typert)(string_of_field_typeftype)enc_num(string_of_payload_kindpk)packed|Rft_associative(at,enc_num,(bt,pk1),(ftype,pk2))->F.linepsc" Rft_associative (Associative Type: %s, Encoding: %d, Basic Type: \
%s, Payload Kind1: %s, Field Type: %s, Payload Kind2: %s)"(string_of_associative_typeat)enc_num(string_of_basic_typebt)(string_of_payload_kindpk1)(string_of_field_typeftype)(string_of_payload_kindpk2)|Rft_variantv->F.linepsc" Rft_variant: %s"v.v_name(* Helper function to convert field_type to string *)andstring_of_field_typeft=matchftwith|Ft_unit->"Ft_unit"|Ft_basic_typebt->"Ft_basic_type: "^string_of_basic_typebt|Ft_user_defined_typeudt->"Ft_user_defined_type: "^udt.udt_type_name|Ft_wrapper_typewt->Printf.sprintf"Ft_wrapper_type: Basic Type: %s, Payload Kind: %s"(string_of_basic_typewt.wt_type)(string_of_payload_kindwt.wt_pk)(* Recursive function to print a variant *)letrecprint_variantscvariant=F.linepsc"Variant: %s"variant.v_name;List.iter(print_variant_constructorsc)variant.v_constructors(* Recursive function to print a variant constructor *)andprint_variant_constructorscvc=F.linepsc" Constructor: %s"vc.vc_constructor;F.linepsc" Field Type: %s\n"(string_of_variant_constructor_typevc.vc_field_type);F.linepsc" Encoding Number: %d, Payload Kind: %s"vc.vc_encoding_number(string_of_payload_kindvc.vc_payload_kind);F.linepsc" Options: %s"(Format.asprintf"%a"Pb_option.pp_setvc.vc_options)(* Helper function to convert variant_constructor_type to string *)andstring_of_variant_constructor_typevct=matchvctwith|Vct_nullary->"Vct_nullary"|Vct_non_nullary_constructorft->"Vct_non_nullary_constructor: "^string_of_field_typeft(* Recursive function to print a record *)letrecprint_recordscrecord=F.linepsc"Record: %s"record.r_name;List.iter(print_record_fieldsc)record.r_fields(* Recursive function to print a record field *)andprint_record_fieldscrecord_field=F.linepsc"- Field: %s"record_field.rf_label;print_record_field_typescrecord_field.rf_field_type;F.linepsc" Field options: %s"(Format.asprintf"%a"Pb_option.pp_setrecord_field.rf_options)(* Recursive function to print a const_variant *)letrecprint_const_variantscconst_variant=F.linepsc"Const Variant: %s"const_variant.cv_name;List.iter(print_const_variant_constructorsc)const_variant.cv_constructors(* Recursive function to print a const_variant constructor *)andprint_const_variant_constructorsccvc=F.linepsc" Constructor: %s"cvc.cvc_name;F.linepsc" Binary Value: %d, String Value: %s"cvc.cvc_binary_valuecvc.cvc_string_value;F.linepsc" Options: %s"(Format.asprintf"%a"Pb_option.pp_setcvc.cvc_options)(* Recursive function to print the type_spec *)letprint_type_specsctype_spec=matchtype_specwith|Recordrecord->print_recordscrecord|Variantvariant->print_variantscvariant|Const_variantconst_variant->print_const_variantscconst_variant|Unitempty_record->F.linepsc"Empty Record: %s"empty_record.er_name(* Entry point to start printing *)letprint_typesctype_=F.linepsc"Module Prefix: %s"type_.module_prefix;print_type_specsctype_.spec;F.linepsc"Options: %s"(Format.asprintf"%a"Pb_option.pp_settype_.type_options);matchtype_.type_level_ppx_extensionwith|Someext->F.linepsc"PPX Extension: %s"ext|None->()endletgen_struct?and_~mode:_tsc=(matchand_with|Some_->()|None->F.linesc"(* ----------------------------------------------------- *)");F.linesc"(*";F.sub_scopesc(funsc->PP.print_typesct);F.linesc"*)";trueletgen_sig?and_~mode:_tsc=ignoreand_;ignoret;ignoresc;trueletocamldoc_title="Dump of internal representation for generated OCaml types"letplugin:Pb_codegen_plugin.t=letmoduleP=structletgen_sig=gen_sigletgen_struct=gen_structletocamldoc_title=ocamldoc_titleendin(moduleP)