Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file printers.ml
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394openPpxlibopenStdLabelsopenAstlib.Pprintast(** {1 Pretty-printers} *)(** List of OCaml base types
- The named argument [loc] is necessary in order for
the [Ppxlib.Metaquot] quotations to expand to the appropriate
AST fragments representing the base types. *)letbase_types~(loc:Location.t):core_typelist=[[%type:int];[%type:int32];[%type:int64];[%type:nativeint];[%type:char];[%type:bool];[%type:unit];[%type:float];[%type:string]](** Alias for [Format.err_formatter] *)leterr_fmt:Format.formatter=Format.err_formatter(** Pretty-printer for [pattern]s *)letpp_pattern:pattern->unit=patternerr_fmt(** Pretty-printer for [core_type]s *)letpp_core_type:core_type->unit=core_typeerr_fmt(** Pretty-printer for [expression]s *)letpp_expression:expression->unit=expressionerr_fmt(** Pretty-printer for [structure_item]s *)letpp_structure_item:structure_item->unit=structure_itemerr_fmt(** Instantiates all type variables ['a] inside a type expression with [int]
by recursing over the structure of the type expression.
Base types are left unchanged.
Note: this function only recurses over type expressions when
they consist of:
- Type constructor applications ([Ptyp_constr])
- Tuples ([Ptyp_tuple])
- Arrow/function types ([Ptyp_arrow]). *)letrecmonomorphize(ty:core_type):core_type=letloc=ty.ptyp_locinmatchty.ptyp_descwith|_ty_descwhenList.memty~set:(base_types~loc)->ty|Ptyp_var_->[%type:int]|Ptyp_arrow(arg_lbl,t1,t2)->{tywithptyp_desc=Ptyp_arrow(arg_lbl,monomorphizet1,monomorphizet2)}|Ptyp_tupletys->{tywithptyp_desc=Ptyp_tuple(List.map~f:monomorphizetys)}|Ptyp_constr(ident,ty_params)->{tywithptyp_desc=Ptyp_constr(ident,List.map~f:monomorphizety_params)}|_->ty(** Converts a type expression [ty] to its capitalized, camel-case
string representation (for use as a constructor in an algebraic data type)
- The type expression is monomorphized prior to computing its string
representation (i.e. ['a] is instantiated to [int]).
- Note: polymoprhic variants, objects, extensions/attributes are
not supported by this function.
- Note: this function is slightly different from [Ppxlib.string_of_core_type]
due to its capitalization, camel-case & monomorphization functionalities. *)letrecstring_of_monomorphized_ty(ty:core_type):string=matchty.ptyp_descwith|Ptyp_var_|Ptyp_any->string_of_monomorphized_ty(monomorphizety)|Ptyp_constr({txt=ident;_},ty_params)->letty_constr_str=Astlib.Longident.flattenident|>String.concat~sep:""|>String.capitalize_asciiinletparams_str=String.concat~sep:""(List.map~f:string_of_monomorphized_tyty_params)inparams_str^ty_constr_str|Ptyp_tupletys->letty_strs=List.maptys~f:(funty->string_of_monomorphized_tyty|>String.capitalize_ascii)inString.concat~sep:""ty_strs^"Product"|Ptyp_arrow(_,t1,t2)->string_of_monomorphized_tyt1^string_of_monomorphized_tyt2|_->failwith"type expression not supported by string_of_monomorphized_ty"(** Retrieves the name of a type as a snake-case string
- e.g. [int list] becomes ["int_list"] *)letsnake_case_type_name(ty:core_type):string=Base.String.tr~target:' '~replacement:'_'(Ppxlib.string_of_core_typety)