123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137openPpxlibtypeconfig={variant_as_string:bool;(** Encode variants as string instead of string array. This option breaks compatibility with yojson derivers and
doesn't support constructors with a payload. *)polymorphic_variant_tuple:bool;(** Preserve the implicit tuple in a polymorphic variant. This option breaks compatibility with yojson derivers. *)ocaml_doc:bool;(** Use [ocaml.doc] attributes (i.e. [(** ... *)] comments) as a fallback for [[@jsonschema.description]] when the
explicit annotation is absent. *)}letstring_attrnamectx=Attribute.declarenamectxAst_pattern.(single_expr_payload(estring__'))(funx->x)letexpr_attrnamectx=Attribute.declarenamectxAst_pattern.(single_expr_payload__)(funx->x)letjsonschema_key=string_attr"jsonschema.key"Attribute.Context.label_declarationletjsonschema_ref=string_attr"jsonschema.ref"Attribute.Context.label_declarationletjsonschema_variant_name=string_attr"jsonschema.name"Attribute.Context.constructor_declarationletjsonschema_polymorphic_variant_name=string_attr"jsonschema.name"Attribute.Context.rtagletjsonschema_td_allow_extra_fields=Attribute.declare"jsonschema.allow_extra_fields"Attribute.Context.type_declarationAst_pattern.(pstrnil)(fun()->())letjsonschema_cd_allow_extra_fields=Attribute.declare"jsonschema.allow_extra_fields"Attribute.Context.constructor_declarationAst_pattern.(pstrnil)(fun()->())letjsonschema_option=Attribute.declare_flag"jsonschema.option"Attribute.Context.label_declarationletjsonschema_ld_description=string_attr"jsonschema.description"Attribute.Context.label_declarationletjsonschema_td_description=string_attr"jsonschema.description"Attribute.Context.type_declarationletjsonschema_cd_description=string_attr"jsonschema.description"Attribute.Context.constructor_declarationletjsonschema_ct_description=string_attr"jsonschema.description"Attribute.Context.core_typeletjsonschema_rtag_description=string_attr"jsonschema.description"Attribute.Context.rtagletjsonschema_td_format=string_attr"jsonschema.format"Attribute.Context.type_declarationletjsonschema_ld_format=string_attr"jsonschema.format"Attribute.Context.label_declarationletjsonschema_ct_format=string_attr"jsonschema.format"Attribute.Context.core_typeletjsonschema_td_maximum=expr_attr"jsonschema.maximum"Attribute.Context.type_declarationletjsonschema_ld_maximum=expr_attr"jsonschema.maximum"Attribute.Context.label_declarationletjsonschema_ct_maximum=expr_attr"jsonschema.maximum"Attribute.Context.core_typeletjsonschema_td_minimum=expr_attr"jsonschema.minimum"Attribute.Context.type_declarationletjsonschema_ld_minimum=expr_attr"jsonschema.minimum"Attribute.Context.label_declarationletjsonschema_ct_minimum=expr_attr"jsonschema.minimum"Attribute.Context.core_typeletjsonschema_ct_attrs=expr_attr"jsonschema.attrs"Attribute.Context.core_typeletjsonschema_td_attrs=expr_attr"jsonschema.attrs"Attribute.Context.type_declarationletjsonschema_ld_attrs=expr_attr"jsonschema.attrs"Attribute.Context.label_declarationletjsonschema_ld_default=expr_attr"jsonschema.default"Attribute.Context.label_declaration(* We intentionally do not use [Attribute.get] for [ocaml.doc]/[doc]. These are
compiler-reserved attributes, and [ppxlib] rejects registering them via
[Attribute.declare]. We therefore inspect the raw attribute list directly
with an [Ast_pattern] that matches both the name and the standard string
payload shape in one go. *)letdoc_attr_pattern=Ast_pattern.(attribute~name:(string"ocaml.doc"|||string"doc")~payload:(single_expr_payload(estring__')))(* A node can carry several [ocaml.doc]/[doc] attributes — e.g. a user writing
one doc comment before a record field and another after. We collect every
match and join them with a blank line so each comment reads as its own
paragraph. The returned location is that of the first matching attribute. *)letfind_doc_attrattrs=letmatches=List.filter_map(funattr->Ast_pattern.parse_resdoc_attr_patternattr.attr_locattrFun.id|>Result.to_option|>Option.map(fun({txt;loc}:stringLocation.loc)->{txt=String.trimtxt;loc}))attrsinmatchmatcheswith|[]->None|[single]->Somesingle|first::_asall->Some{txt=String.concat"\n\n"(List.map(funx->x.txt)all);loc=first.loc}letfallback_description~ocaml_docexplicit_descattrsnode=matchAttribute.getexplicit_descnodewith|Some_asx->x|None->ifocaml_docthenfind_doc_attrattrselseNoneletld_description~ocaml_doc(ld:label_declaration)=fallback_description~ocaml_docjsonschema_ld_descriptionld.pld_attributesldlettd_description~ocaml_doc(td:type_declaration)=fallback_description~ocaml_docjsonschema_td_descriptiontd.ptype_attributestdletcd_description~ocaml_doc(cd:constructor_declaration)=fallback_description~ocaml_docjsonschema_cd_descriptioncd.pcd_attributescdletct_description~ocaml_doc(ct:core_type)=fallback_description~ocaml_docjsonschema_ct_descriptionct.ptyp_attributesctletrtag_description~ocaml_doc(rf:row_field)=fallback_description~ocaml_docjsonschema_rtag_descriptionrf.prf_attributesrfletjsonschema_td_compact_variants=Attribute.declare_flag"jsonschema.compact_variants"Attribute.Context.type_declarationletattributes=[Attribute.Tjsonschema_key;Attribute.Tjsonschema_ref;Attribute.Tjsonschema_variant_name;Attribute.Tjsonschema_polymorphic_variant_name;Attribute.Tjsonschema_td_allow_extra_fields;Attribute.Tjsonschema_cd_allow_extra_fields;Attribute.Tjsonschema_option;Attribute.Tjsonschema_ld_description;Attribute.Tjsonschema_td_description;Attribute.Tjsonschema_cd_description;Attribute.Tjsonschema_ct_description;Attribute.Tjsonschema_rtag_description;Attribute.Tjsonschema_td_format;Attribute.Tjsonschema_ld_format;Attribute.Tjsonschema_ct_format;Attribute.Tjsonschema_td_maximum;Attribute.Tjsonschema_ld_maximum;Attribute.Tjsonschema_ct_maximum;Attribute.Tjsonschema_td_minimum;Attribute.Tjsonschema_ld_minimum;Attribute.Tjsonschema_ct_minimum;Attribute.Tjsonschema_ct_attrs;Attribute.Tjsonschema_td_attrs;Attribute.Tjsonschema_ld_attrs;Attribute.Tjsonschema_ld_default;Attribute.Tjsonschema_td_compact_variants;]letargs()=Deriving.Args.(empty+>flag"variant_as_string"+>flag"polymorphic_variant_tuple"+>flag"ocaml_doc")