| 1 |
open! Base |
| 2 |
open! Ppxlib |
| 3 |
open Ast_builder.Default |
| 4 |
|
| 5 |
let copy = |
| 6 |
object |
| 7 |
inherit Ast_traverse.map |
| 8 |
method! location loc = { loc with loc_ghost = true } |
| 9 |
method! attributes _ = [] |
| 10 |
end |
| 11 |
;; |
| 12 |
|
| 13 |
let unsupported ~loc string = |
| 14 |
Location.raise_errorf ~loc "sexp_grammar: %s are unsupported" string |
| 15 |
;; |
| 16 |
|
| 17 |
let ewith_tag ~loc ~key ~value grammar = |
| 18 |
[%expr { key = [%e key]; value = [%e value]; grammar = [%e grammar] }] |
| 19 |
;; |
| 20 |
|
| 21 |
let eno_tag ~loc grammar = [%expr No_tag [%e grammar]] |
| 22 |
let etag ~loc with_tag = [%expr Tag [%e with_tag]] |
| 23 |
let etagged ~loc with_tag = [%expr Tagged [%e with_tag]] |
| 24 |
|
| 25 |
let tag_of_doc_comment ~loc comment = |
| 26 |
( [%expr Ppx_sexp_conv_lib.Sexp_grammar.doc_comment_tag] |
| 27 |
, [%expr Atom [%e estring ~loc comment]] ) |
| 28 |
;; |
| 29 |
|
| 30 |
module Tags = struct |
| 31 |
type t = |
| 32 |
{ defined_using_tags : expression option |
| 33 |
; defined_using_tag : (expression * expression) list |
| 34 |
} |
| 35 |
|
| 36 |
let get x ~tags ~tag = |
| 37 |
{ defined_using_tags = Attribute.get tags x |
| 38 |
; defined_using_tag = Attribute.get tag x |> Option.value ~default:[] |
| 39 |
} |
| 40 |
;; |
| 41 |
end |
| 42 |
|
| 43 |
let rec with_tag_assoc_list grammar ~loc ~tags_expr ~wrap_tag ~wrap_tags = |
| 44 |
match tags_expr with |
| 45 |
| [%expr []] -> grammar |
| 46 |
| [%expr ([%e? key], [%e? value]) :: [%e? tags_expr]] -> |
| 47 |
wrap_tag |
| 48 |
~loc |
| 49 |
(ewith_tag |
| 50 |
~loc |
| 51 |
~key |
| 52 |
~value |
| 53 |
(with_tag_assoc_list grammar ~loc ~tags_expr ~wrap_tag ~wrap_tags)) |
| 54 |
| _ -> wrap_tags grammar ~loc ~tags_expr |
| 55 |
;; |
| 56 |
|
| 57 |
let with_tags grammar ~wrap_tag ~wrap_tags ~loc ~(tags : Tags.t) ~comments = |
| 58 |
let tags_from_comments = List.map comments ~f:(tag_of_doc_comment ~loc) in |
| 59 |
let init = |
| 60 |
match tags.defined_using_tags with |
| 61 |
| None -> grammar |
| 62 |
| Some tags_expr -> with_tag_assoc_list grammar ~loc ~tags_expr ~wrap_tag ~wrap_tags |
| 63 |
in |
| 64 |
List.fold_right |
| 65 |
(List.concat [ tags_from_comments; tags.defined_using_tag ]) |
| 66 |
~init |
| 67 |
~f:(fun (key, value) grammar -> wrap_tag ~loc (ewith_tag ~loc ~key ~value grammar)) |
| 68 |
;; |
| 69 |
|
| 70 |
let with_tags_as_list grammar ~core_type ~loc ~tags ~comments = |
| 71 |
let wrap_tags grammar ~loc ~tags_expr = |
| 72 |
[%expr |
| 73 |
Sexplib0.Sexp_conv.sexp_grammar_with_tag_list |
| 74 |
([%e grammar] : [%t core_type] Sexplib0.Sexp_grammar.with_tag_list) |
| 75 |
~tags:[%e tags_expr]] |
| 76 |
in |
| 77 |
with_tags (eno_tag ~loc grammar) ~wrap_tag:etag ~wrap_tags ~loc ~tags ~comments |
| 78 |
;; |
| 79 |
|
| 80 |
let with_tags_as_grammar grammar ~loc ~tags ~comments = |
| 81 |
let wrap_tags grammar ~loc ~tags_expr = |
| 82 |
[%expr Sexplib0.Sexp_conv.sexp_grammar_with_tags [%e grammar] ~tags:[%e tags_expr]] |
| 83 |
in |
| 84 |
with_tags grammar ~wrap_tag:etagged ~wrap_tags ~loc ~tags ~comments |
| 85 |
;; |
| 86 |
|
| 87 |
let grammar_name name = name ^ "_sexp_grammar" |
| 88 |
let tyvar_grammar_name name = grammar_name ("_'" ^ name) |
| 89 |
let estr { loc; txt } = estring ~loc txt |
| 90 |
|
| 91 |
let grammar_type ~loc core_type = |
| 92 |
[%type: [%t copy#core_type core_type] Sexplib0.Sexp_grammar.t] |
| 93 |
;; |
| 94 |
|
| 95 |
let abstract_grammar ~ctxt ~loc id = |
| 96 |
let module_name = |
| 97 |
ctxt |> Expansion_context.Deriver.code_path |> Code_path.fully_qualified_path |
| 98 |
in |
| 99 |
[%expr Any [%e estr { id with txt = String.concat ~sep:"." [ module_name; id.txt ] }]] |
| 100 |
;; |
| 101 |
|
| 102 |
let arrow_grammar ~loc = [%expr Sexplib0.Sexp_conv.fun_sexp_grammar.untyped] |
| 103 |
let opaque_grammar ~loc = [%expr Sexplib0.Sexp_conv.opaque_sexp_grammar.untyped] |
| 104 |
let any_grammar ~loc name = [%expr Any [%e estring ~loc name]] |
| 105 |
let list_grammar ~loc expr = [%expr List [%e expr]] |
| 106 |
let many_grammar ~loc expr = [%expr Many [%e expr]] |
| 107 |
let fields_grammar ~loc expr = [%expr Fields [%e expr]] |
| 108 |
let tyvar_grammar ~loc expr = [%expr Tyvar [%e expr]] |
| 109 |
let recursive_grammar ~loc name args = [%expr Recursive ([%e name], [%e args])] |
| 110 |
|
| 111 |
let tycon_grammar ~loc tycon_name params defns = |
| 112 |
[%expr Tycon ([%e tycon_name], [%e params], [%e defns])] |
| 113 |
;; |
| 114 |
|
| 115 |
let defns_type ~loc = [%type: Sexplib0.Sexp_grammar.defn Stdlib.List.t Stdlib.Lazy.t] |
| 116 |
|
| 117 |
let untyped_grammar ~loc expr = |
| 118 |
match expr with |
| 119 |
| [%expr { untyped = [%e? untyped] }] -> untyped |
| 120 |
| _ -> [%expr [%e expr].untyped] |
| 121 |
;; |
| 122 |
|
| 123 |
let typed_grammar ~loc expr = |
| 124 |
match expr with |
| 125 |
| [%expr [%e? typed].untyped] -> typed |
| 126 |
| _ -> [%expr { untyped = [%e expr] }] |
| 127 |
;; |
| 128 |
|
| 129 |
let annotated_grammar ~loc expr core_type = |
| 130 |
pexp_constraint ~loc expr (grammar_type ~loc core_type) |
| 131 |
;; |
| 132 |
|
| 133 |
let defn_expr ~loc ~tycon ~tyvars ~grammar = |
| 134 |
[%expr { tycon = [%e tycon]; tyvars = [%e tyvars]; grammar = [%e grammar] }] |
| 135 |
;; |
| 136 |
|
| 137 |
let union_grammar ~loc exprs = |
| 138 |
match exprs with |
| 139 |
| [] -> [%expr Union []] |
| 140 |
| [ expr ] -> expr |
| 141 |
| _ -> [%expr Union [%e elist ~loc exprs]] |
| 142 |
;; |
| 143 |
|
| 144 |
let tuple_grammar ~loc exprs = |
| 145 |
List.fold_right exprs ~init:[%expr Empty] ~f:(fun expr rest -> |
| 146 |
[%expr Cons ([%e expr], [%e rest])]) |
| 147 |
;; |
| 148 |
|
| 149 |
let atom_clause ~loc = [%expr Atom_clause] |
| 150 |
let list_clause ~loc args = [%expr List_clause { args = [%e args] }] |
| 151 |
|
| 152 |
module Variant_clause_type = struct |
| 153 |
type t = |
| 154 |
{ name : label loc |
| 155 |
; comments : string list |
| 156 |
; tags : Tags.t |
| 157 |
; clause_kind : expression |
| 158 |
} |
| 159 |
|
| 160 |
let to_grammar_expr { name; comments; tags; clause_kind } ~loc = |
| 161 |
[%expr { name = [%e estr name]; clause_kind = [%e clause_kind] }] |
| 162 |
|> with_tags_as_list |
| 163 |
~loc:name.loc |
| 164 |
~comments |
| 165 |
~tags |
| 166 |
~core_type:[%type: Sexplib0.Sexp_grammar.clause] |
| 167 |
;; |
| 168 |
end |
| 169 |
|
| 170 |
let variant_grammars ~loc ~case_sensitivity ~clauses = |
| 171 |
match List.is_empty clauses with |
| 172 |
| true -> [] |
| 173 |
| false -> |
| 174 |
let clause_exprs = List.map clauses ~f:(Variant_clause_type.to_grammar_expr ~loc) in |
| 175 |
let grammar = |
| 176 |
[%expr |
| 177 |
Variant |
| 178 |
{ case_sensitivity = [%e case_sensitivity] |
| 179 |
; clauses = [%e elist ~loc clause_exprs] |
| 180 |
}] |
| 181 |
in |
| 182 |
[ grammar ] |
| 183 |
;; |
| 184 |
|
| 185 |
|
| 186 |
let td_params_fun td expr = |
| 187 |
let loc = td.ptype_loc in |
| 188 |
let params = |
| 189 |
List.map td.ptype_params ~f:(fun param -> |
| 190 |
let { loc; txt } = get_type_param_name param in |
| 191 |
pvar ~loc (tyvar_grammar_name txt)) |
| 192 |
in |
| 193 |
eabstract ~loc params expr |
| 194 |
;; |
| 195 |
|
| 196 |
module Row_field_type = struct |
| 197 |
type t = |
| 198 |
| Inherit of core_type |
| 199 |
| Tag_no_arg of string loc |
| 200 |
| Tag_with_arg of string loc * core_type |
| 201 |
|
| 202 |
let of_row_field ~loc row_field = |
| 203 |
match row_field with |
| 204 |
| Rinherit core_type -> Inherit core_type |
| 205 |
| Rtag (name, possibly_no_arg, possible_type_args) -> |
| 206 |
(match possibly_no_arg, possible_type_args with |
| 207 |
| true, [] -> Tag_no_arg name |
| 208 |
| false, [ core_type ] -> Tag_with_arg (name, core_type) |
| 209 |
| false, [] -> unsupported ~loc "empty polymorphic variant types" |
| 210 |
| true, _ :: _ | false, _ :: _ :: _ -> unsupported ~loc "intersection types") |
| 211 |
;; |
| 212 |
end |
| 213 |
|
| 214 |
let attr_doc_comments attributes ~tags_of_doc_comments = |
| 215 |
match tags_of_doc_comments with |
| 216 |
| false -> [] |
| 217 |
| true -> |
| 218 |
let doc_pattern = Ast_pattern.(pstr (pstr_eval (estring __) nil ^:: nil)) in |
| 219 |
List.filter_map attributes ~f:(fun attribute -> |
| 220 |
match attribute.attr_name.txt with |
| 221 |
| "ocaml.doc" | "doc" -> |
| 222 |
Ast_pattern.parse |
| 223 |
doc_pattern |
| 224 |
attribute.attr_loc |
| 225 |
attribute.attr_payload |
| 226 |
~on_error:(fun () -> None) |
| 227 |
(fun doc -> Some doc) |
| 228 |
| _ -> None) |
| 229 |
;; |
| 230 |
|
| 231 |
let grammar_of_type_tags core_type grammar ~tags_of_doc_comments = |
| 232 |
let tags = Tags.get core_type ~tags:Attrs.tags_type ~tag:Attrs.tag_type in |
| 233 |
let loc = core_type.ptyp_loc in |
| 234 |
let comments = attr_doc_comments ~tags_of_doc_comments core_type.ptyp_attributes in |
| 235 |
with_tags_as_grammar grammar ~loc ~tags ~comments |
| 236 |
;; |
| 237 |
|
| 238 |
let grammar_of_field_tags field grammar ~tags_of_doc_comments = |
| 239 |
let tags = Tags.get field ~tags:Attrs.tags_ld ~tag:Attrs.tag_ld in |
| 240 |
let loc = field.pld_loc in |
| 241 |
let comments = attr_doc_comments ~tags_of_doc_comments field.pld_attributes in |
| 242 |
with_tags_as_list |
| 243 |
grammar |
| 244 |
~loc |
| 245 |
~tags |
| 246 |
~comments |
| 247 |
~core_type:[%type: Sexplib0.Sexp_grammar.field] |
| 248 |
;; |
| 249 |
|
| 250 |
let rec grammar_of_type core_type ~rec_flag ~tags_of_doc_comments = |
| 251 |
let loc = core_type.ptyp_loc in |
| 252 |
let grammar = |
| 253 |
let from_attribute = |
| 254 |
match |
| 255 |
( Attribute.get Attrs.grammar_custom core_type |
| 256 |
, Attribute.get Attrs.grammar_any core_type ) |
| 257 |
with |
| 258 |
| Some _, Some _ -> |
| 259 |
Some |
| 260 |
[%expr |
| 261 |
[%ocaml.warning |
| 262 |
"[@sexp_grammar.custom] and [@sexp_grammar.any] are mutually exclusive"]] |
| 263 |
| Some expr, None -> |
| 264 |
Some (untyped_grammar ~loc (annotated_grammar ~loc expr core_type)) |
| 265 |
| None, Some maybe_name -> |
| 266 |
Some (any_grammar ~loc (Option.value maybe_name ~default:"ANY")) |
| 267 |
| None, None -> |
| 268 |
|
| 269 |
|
| 270 |
(match Attribute.get Attrs.opaque core_type with |
| 271 |
| Some () -> Some (opaque_grammar ~loc) |
| 272 |
| None -> None) |
| 273 |
in |
| 274 |
match from_attribute with |
| 275 |
| Some expr -> expr |
| 276 |
| None -> |
| 277 |
(match Ppxlib_jane.Jane_syntax.Core_type.of_ast core_type with |
| 278 |
| Some (Jtyp_tuple ltps, _attrs) -> |
| 279 |
grammar_of_labeled_tuple ~loc ~rec_flag ~tags_of_doc_comments ltps |
| 280 |
| Some (Jtyp_layout _, _) | None -> |
| 281 |
(match core_type.ptyp_desc with |
| 282 |
| Ptyp_any -> any_grammar ~loc "_" |
| 283 |
| Ptyp_var name -> |
| 284 |
(match rec_flag with |
| 285 |
| Recursive -> |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
tyvar_grammar ~loc (estring ~loc name) |
| 290 |
| Nonrecursive -> |
| 291 |
|
| 292 |
|
| 293 |
unapplied_type_constr_conv |
| 294 |
~loc |
| 295 |
~f:tyvar_grammar_name |
| 296 |
(Located.lident ~loc name) |
| 297 |
|> untyped_grammar ~loc) |
| 298 |
| Ptyp_arrow _ -> arrow_grammar ~loc |
| 299 |
| Ptyp_tuple list -> |
| 300 |
List.map ~f:(grammar_of_type ~rec_flag ~tags_of_doc_comments) list |
| 301 |
|> tuple_grammar ~loc |
| 302 |
|> list_grammar ~loc |
| 303 |
| Ptyp_constr (id, args) -> |
| 304 |
List.map args ~f:(fun core_type -> |
| 305 |
let loc = core_type.ptyp_loc in |
| 306 |
grammar_of_type ~rec_flag ~tags_of_doc_comments core_type |
| 307 |
|> typed_grammar ~loc) |
| 308 |
|> type_constr_conv ~loc ~f:grammar_name id |
| 309 |
|> untyped_grammar ~loc |
| 310 |
| Ptyp_object _ -> unsupported ~loc "object types" |
| 311 |
| Ptyp_class _ -> unsupported ~loc "class types" |
| 312 |
| Ptyp_alias _ -> unsupported ~loc "type aliases" |
| 313 |
| Ptyp_variant (rows, closed_flag, (_ : string list option)) -> |
| 314 |
(match closed_flag with |
| 315 |
| Open -> unsupported ~loc "open polymorphic variant types" |
| 316 |
| Closed -> |
| 317 |
grammar_of_polymorphic_variant ~loc ~rec_flag ~tags_of_doc_comments rows) |
| 318 |
| Ptyp_poly _ -> unsupported ~loc "explicitly polymorphic types" |
| 319 |
| Ptyp_package _ -> unsupported ~loc "first-class module types" |
| 320 |
| Ptyp_extension _ -> unsupported ~loc "unexpanded ppx extensions" |
| 321 |
| Ptyp_open _ -> unsupported ~loc "local module open")) |
| 322 |
in |
| 323 |
grammar_of_type_tags core_type grammar ~tags_of_doc_comments |
| 324 |
|
| 325 |
and grammar_of_labeled_tuple ~loc ~rec_flag ~tags_of_doc_comments alist = |
| 326 |
assert (Labeled_tuple.is_valid alist); |
| 327 |
let fields = |
| 328 |
List.concat_map alist ~f:(fun (lbl, typ) -> |
| 329 |
let lbl = Labeled_tuple.atom_of_label lbl in |
| 330 |
let field = grammar_of_type ~rec_flag ~tags_of_doc_comments typ in |
| 331 |
let clauses : Variant_clause_type.t list = |
| 332 |
|
| 333 |
|
| 334 |
[ { name = { txt = lbl; loc } |
| 335 |
; comments = [] |
| 336 |
; tags = |
| 337 |
{ defined_using_tags = None; defined_using_tag = [] } |
| 338 |
|
| 339 |
|
| 340 |
; clause_kind = list_clause ~loc [%expr Cons ([%e field], Empty)] |
| 341 |
} |
| 342 |
] |
| 343 |
in |
| 344 |
let case_sensitivity = [%expr Case_sensitive] in |
| 345 |
variant_grammars ~loc ~case_sensitivity ~clauses) |
| 346 |
in |
| 347 |
list_grammar ~loc (tuple_grammar ~loc fields) |
| 348 |
|
| 349 |
and grammar_of_polymorphic_variant ~loc ~rec_flag ~tags_of_doc_comments rows = |
| 350 |
let inherits, clauses = |
| 351 |
List.partition_map rows ~f:(fun row : (_, Variant_clause_type.t) Either.t -> |
| 352 |
let tags = Tags.get row ~tags:Attrs.tags_poly ~tag:Attrs.tag_poly in |
| 353 |
let comments = attr_doc_comments ~tags_of_doc_comments row.prf_attributes in |
| 354 |
match Attribute.get Attrs.list_poly row with |
| 355 |
| Some () -> |
| 356 |
(match Row_field_type.of_row_field ~loc row.prf_desc with |
| 357 |
| Tag_with_arg (name, [%type: [%t? ty] list]) -> |
| 358 |
let clause_kind = |
| 359 |
grammar_of_type ~rec_flag ~tags_of_doc_comments ty |
| 360 |
|> many_grammar ~loc |
| 361 |
|> list_clause ~loc |
| 362 |
in |
| 363 |
Second { name; comments; tags; clause_kind } |
| 364 |
| _ -> Attrs.invalid_attribute ~loc Attrs.list_poly "_ list") |
| 365 |
| None -> |
| 366 |
(match Row_field_type.of_row_field ~loc row.prf_desc with |
| 367 |
| Inherit core_type -> |
| 368 |
First |
| 369 |
(grammar_of_type ~rec_flag ~tags_of_doc_comments core_type |
| 370 |
|> with_tags_as_grammar ~loc ~tags ~comments) |
| 371 |
| Tag_no_arg name -> |
| 372 |
Second { name; comments; tags; clause_kind = atom_clause ~loc } |
| 373 |
| Tag_with_arg (name, core_type) -> |
| 374 |
let clause_kind = |
| 375 |
[ grammar_of_type ~rec_flag ~tags_of_doc_comments core_type ] |
| 376 |
|> tuple_grammar ~loc |
| 377 |
|> list_clause ~loc |
| 378 |
in |
| 379 |
Second { name; comments; tags; clause_kind })) |
| 380 |
in |
| 381 |
variant_grammars ~loc ~case_sensitivity:[%expr Case_sensitive] ~clauses |
| 382 |
|> List.append inherits |
| 383 |
|> union_grammar ~loc |
| 384 |
;; |
| 385 |
|
| 386 |
let record_expr ~loc ~rec_flag ~tags_of_doc_comments ~extra_attr syntax fields = |
| 387 |
let fields = |
| 388 |
List.map fields ~f:(fun field -> |
| 389 |
let loc = field.pld_loc in |
| 390 |
let field_kind = Record_field_attrs.Of_sexp.create ~loc field in |
| 391 |
let required = |
| 392 |
match field_kind with |
| 393 |
| Specific Required -> true |
| 394 |
| Specific (Default _) |
| 395 |
| Sexp_bool | Sexp_option _ | Sexp_array _ | Sexp_list _ | Omit_nil -> false |
| 396 |
in |
| 397 |
let args = |
| 398 |
match field_kind with |
| 399 |
| Specific Required | Specific (Default _) | Omit_nil -> |
| 400 |
[%expr |
| 401 |
Cons |
| 402 |
([%e grammar_of_type ~tags_of_doc_comments ~rec_flag field.pld_type], Empty)] |
| 403 |
| Sexp_bool -> [%expr Empty] |
| 404 |
| Sexp_option ty -> |
| 405 |
[%expr Cons ([%e grammar_of_type ~tags_of_doc_comments ~rec_flag ty], Empty)] |
| 406 |
| Sexp_list ty | Sexp_array ty -> |
| 407 |
[%expr |
| 408 |
Cons |
| 409 |
(List (Many [%e grammar_of_type ~tags_of_doc_comments ~rec_flag ty]), Empty)] |
| 410 |
in |
| 411 |
[%expr |
| 412 |
{ name = [%e estr field.pld_name] |
| 413 |
; required = [%e ebool ~loc required] |
| 414 |
; args = [%e args] |
| 415 |
}] |
| 416 |
|> grammar_of_field_tags field ~tags_of_doc_comments) |
| 417 |
in |
| 418 |
let allow_extra_fields = |
| 419 |
match Attribute.get extra_attr syntax with |
| 420 |
| Some () -> true |
| 421 |
| None -> false |
| 422 |
in |
| 423 |
[%expr |
| 424 |
{ allow_extra_fields = [%e ebool ~loc allow_extra_fields] |
| 425 |
; fields = [%e elist ~loc fields] |
| 426 |
}] |
| 427 |
;; |
| 428 |
|
| 429 |
let grammar_of_variant ~loc ~rec_flag ~tags_of_doc_comments clause_decls = |
| 430 |
let clauses = |
| 431 |
List.map clause_decls ~f:(fun clause : Variant_clause_type.t -> |
| 432 |
let loc = clause.pcd_loc in |
| 433 |
let tags = Tags.get clause ~tags:Attrs.tags_cd ~tag:Attrs.tag_cd in |
| 434 |
let comments = attr_doc_comments ~tags_of_doc_comments clause.pcd_attributes in |
| 435 |
match Attribute.get Attrs.list_variant clause with |
| 436 |
| Some () -> |
| 437 |
(match clause.pcd_args with |
| 438 |
| Pcstr_tuple [ [%type: [%t? ty] list] ] -> |
| 439 |
let args = |
| 440 |
many_grammar ~loc (grammar_of_type ty ~rec_flag ~tags_of_doc_comments) |
| 441 |
in |
| 442 |
{ name = clause.pcd_name; comments; tags; clause_kind = list_clause ~loc args } |
| 443 |
| _ -> Attrs.invalid_attribute ~loc Attrs.list_variant "_ list") |
| 444 |
| None -> |
| 445 |
(match clause.pcd_args with |
| 446 |
| Pcstr_tuple [] -> |
| 447 |
{ name = clause.pcd_name; comments; tags; clause_kind = atom_clause ~loc } |
| 448 |
| Pcstr_tuple (_ :: _ as args) -> |
| 449 |
let args = |
| 450 |
tuple_grammar |
| 451 |
~loc |
| 452 |
(List.map args ~f:(grammar_of_type ~rec_flag ~tags_of_doc_comments)) |
| 453 |
in |
| 454 |
{ name = clause.pcd_name; comments; tags; clause_kind = list_clause ~loc args } |
| 455 |
| Pcstr_record fields -> |
| 456 |
let args = |
| 457 |
record_expr |
| 458 |
~loc |
| 459 |
~rec_flag |
| 460 |
~tags_of_doc_comments |
| 461 |
~extra_attr:Attrs.allow_extra_fields_cd |
| 462 |
clause |
| 463 |
fields |
| 464 |
|> fields_grammar ~loc |
| 465 |
in |
| 466 |
{ name = clause.pcd_name; comments; tags; clause_kind = list_clause ~loc args })) |
| 467 |
in |
| 468 |
variant_grammars |
| 469 |
~loc |
| 470 |
~case_sensitivity:[%expr Case_sensitive_except_first_character] |
| 471 |
~clauses |
| 472 |
|> union_grammar ~loc |
| 473 |
;; |
| 474 |
|
| 475 |
let grammar_of_td ~ctxt ~rec_flag ~tags_of_doc_comments td = |
| 476 |
let loc = td.ptype_loc in |
| 477 |
match td.ptype_kind with |
| 478 |
| Ptype_open -> unsupported ~loc "open types" |
| 479 |
| Ptype_record fields -> |
| 480 |
record_expr |
| 481 |
~loc |
| 482 |
~rec_flag |
| 483 |
~tags_of_doc_comments |
| 484 |
~extra_attr:Attrs.allow_extra_fields_td |
| 485 |
td |
| 486 |
fields |
| 487 |
|> fields_grammar ~loc |
| 488 |
|> list_grammar ~loc |
| 489 |
| Ptype_variant clauses -> |
| 490 |
grammar_of_variant ~loc ~rec_flag ~tags_of_doc_comments clauses |
| 491 |
| Ptype_abstract -> |
| 492 |
(match td.ptype_manifest with |
| 493 |
| None -> abstract_grammar ~ctxt ~loc td.ptype_name |
| 494 |
| Some core_type -> grammar_of_type ~rec_flag ~tags_of_doc_comments core_type) |
| 495 |
;; |
| 496 |
|
| 497 |
let pattern_of_td td = |
| 498 |
let { loc; txt } = td.ptype_name in |
| 499 |
ppat_constraint |
| 500 |
~loc |
| 501 |
(pvar ~loc (grammar_name txt)) |
| 502 |
(ptyp_poly |
| 503 |
~loc |
| 504 |
(List.map td.ptype_params ~f:get_type_param_name) |
| 505 |
(combinator_type_of_type_declaration td ~f:grammar_type)) |
| 506 |
;; |
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
let rec is_preallocated_constant expr = |
| 511 |
match expr.pexp_desc with |
| 512 |
| Pexp_constraint (expr, _) | Pexp_coerce (expr, _, _) | Pexp_open (_, expr) -> |
| 513 |
is_preallocated_constant expr |
| 514 |
| Pexp_constant _ -> true |
| 515 |
| Pexp_tuple args -> List.for_all ~f:is_preallocated_constant args |
| 516 |
| Pexp_variant (_, maybe_arg) | Pexp_construct (_, maybe_arg) -> |
| 517 |
Option.for_all ~f:is_preallocated_constant maybe_arg |
| 518 |
| Pexp_record (fields, maybe_template) -> |
| 519 |
List.for_all fields ~f:(fun (_, expr) -> is_preallocated_constant expr) |
| 520 |
&& Option.for_all ~f:is_preallocated_constant maybe_template |
| 521 |
| _ -> false |
| 522 |
;; |
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
let rec is_variable_access expr = |
| 528 |
match expr.pexp_desc with |
| 529 |
| Pexp_constraint (expr, _) | Pexp_coerce (expr, _, _) | Pexp_open (_, expr) -> |
| 530 |
is_variable_access expr |
| 531 |
| Pexp_ident _ -> true |
| 532 |
| Pexp_field (expr, _) -> is_variable_access expr |
| 533 |
| _ -> false |
| 534 |
;; |
| 535 |
|
| 536 |
let grammar_needs_lazy_wrapper expr = |
| 537 |
not (is_preallocated_constant expr || is_variable_access expr) |
| 538 |
;; |
| 539 |
|
| 540 |
let lazy_grammar ~loc td expr = |
| 541 |
if List.is_empty td.ptype_params |
| 542 |
|
| 543 |
&& grammar_needs_lazy_wrapper expr |
| 544 |
then [%expr Lazy (lazy [%e expr])] |
| 545 |
else expr |
| 546 |
;; |
| 547 |
|
| 548 |
let force_expr ~loc expr = [%expr Stdlib.Lazy.force [%e expr]] |
| 549 |
|
| 550 |
|
| 551 |
let nonrecursive_grammars ~ctxt ~loc ~tags_of_doc_comments td_lists = |
| 552 |
List.concat_map td_lists ~f:(fun tds -> |
| 553 |
List.map tds ~f:(fun td -> |
| 554 |
let td = name_type_params_in_td td in |
| 555 |
let loc = td.ptype_loc in |
| 556 |
let pat = pattern_of_td td in |
| 557 |
let expr = |
| 558 |
grammar_of_td ~ctxt ~rec_flag:Nonrecursive ~tags_of_doc_comments td |
| 559 |
|> lazy_grammar td ~loc |
| 560 |
|> typed_grammar ~loc |
| 561 |
|> td_params_fun td |
| 562 |
in |
| 563 |
value_binding ~loc ~pat ~expr) |
| 564 |
|> pstr_value_list ~loc Nonrecursive) |
| 565 |
;; |
| 566 |
|
| 567 |
|
| 568 |
let recursive_grammar_tycons tds = |
| 569 |
List.map tds ~f:(fun td -> |
| 570 |
let td = name_type_params_in_td td in |
| 571 |
let loc = td.ptype_loc in |
| 572 |
let pat = pattern_of_td td in |
| 573 |
let expr = |
| 574 |
recursive_grammar |
| 575 |
~loc |
| 576 |
(estr td.ptype_name) |
| 577 |
(List.map td.ptype_params ~f:(fun param -> |
| 578 |
let { loc; txt } = get_type_param_name param in |
| 579 |
tyvar_grammar_name txt |> evar ~loc |> untyped_grammar ~loc) |
| 580 |
|> elist ~loc) |
| 581 |
|> typed_grammar ~loc |
| 582 |
|> td_params_fun td |
| 583 |
in |
| 584 |
value_binding ~loc ~pat ~expr) |
| 585 |
;; |
| 586 |
|
| 587 |
|
| 588 |
let recursive_grammar_defns ~ctxt ~loc ~tags_of_doc_comments tds = |
| 589 |
List.map tds ~f:(fun td -> |
| 590 |
let td = name_type_params_in_td td in |
| 591 |
let loc = td.ptype_loc in |
| 592 |
let tycon = estr td.ptype_name in |
| 593 |
let tyvars = |
| 594 |
List.map td.ptype_params ~f:(fun param -> estr (get_type_param_name param)) |
| 595 |
|> elist ~loc |
| 596 |
in |
| 597 |
let grammar = grammar_of_td ~ctxt ~rec_flag:Recursive ~tags_of_doc_comments td in |
| 598 |
defn_expr ~loc ~tycon ~tyvars ~grammar) |
| 599 |
|> elist ~loc |
| 600 |
;; |
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
let recursive_grammar_expr ~defns_name td = |
| 605 |
let td = name_type_params_in_td td in |
| 606 |
let loc = td.ptype_loc in |
| 607 |
let pat = pattern_of_td td in |
| 608 |
let expr = |
| 609 |
let tyvars = |
| 610 |
List.map td.ptype_params ~f:(fun param -> |
| 611 |
let { loc; txt } = get_type_param_name param in |
| 612 |
tyvar_grammar_name txt |> evar ~loc |> untyped_grammar ~loc) |
| 613 |
|> elist ~loc |
| 614 |
in |
| 615 |
tycon_grammar |
| 616 |
~loc |
| 617 |
(estr td.ptype_name) |
| 618 |
tyvars |
| 619 |
(evar ~loc defns_name |> force_expr ~loc) |
| 620 |
|> lazy_grammar td ~loc |
| 621 |
|> typed_grammar ~loc |
| 622 |
|> td_params_fun td |
| 623 |
in |
| 624 |
value_binding ~loc ~pat ~expr |
| 625 |
;; |
| 626 |
|
| 627 |
|
| 628 |
let recursive_grammars ~ctxt ~loc ~tags_of_doc_comments tds = |
| 629 |
match List.is_empty tds with |
| 630 |
| true -> [] |
| 631 |
| false -> |
| 632 |
let defns_name = gen_symbol ~prefix:"grammars" () in |
| 633 |
let defns_item = |
| 634 |
let expr = |
| 635 |
recursive_grammar_defns ~ctxt ~loc ~tags_of_doc_comments tds |
| 636 |
|> pexp_let ~loc Nonrecursive (recursive_grammar_tycons tds) |
| 637 |
|> pexp_lazy ~loc |
| 638 |
in |
| 639 |
let pat = ppat_constraint ~loc (pvar ~loc defns_name) (defns_type ~loc) in |
| 640 |
pstr_value ~loc Nonrecursive [ value_binding ~loc ~pat ~expr ] |
| 641 |
in |
| 642 |
let grammars_item = |
| 643 |
List.map tds ~f:(recursive_grammar_expr ~defns_name) |> pstr_value ~loc Nonrecursive |
| 644 |
in |
| 645 |
[%str |
| 646 |
include struct |
| 647 |
open struct |
| 648 |
[%%i defns_item] |
| 649 |
end |
| 650 |
|
| 651 |
[%%i grammars_item] |
| 652 |
end] |
| 653 |
;; |
| 654 |
|
| 655 |
let partition_recursive_and_nonrecursive ~rec_flag tds = |
| 656 |
match (rec_flag : rec_flag) with |
| 657 |
| Nonrecursive -> [], [ tds ] |
| 658 |
| Recursive -> |
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
let rec loop tds ~acc = |
| 663 |
let obj = |
| 664 |
object |
| 665 |
inherit type_is_recursive Recursive tds |
| 666 |
method recursion td = {<type_names = [ td.ptype_name.txt ]>}#go () |
| 667 |
end |
| 668 |
in |
| 669 |
let recursive, nonrecursive = |
| 670 |
List.partition_tf tds ~f:(fun td -> |
| 671 |
match obj#recursion td with |
| 672 |
| Recursive -> true |
| 673 |
| Nonrecursive -> false) |
| 674 |
in |
| 675 |
if List.is_empty recursive || List.is_empty nonrecursive |
| 676 |
then recursive, nonrecursive :: acc |
| 677 |
else loop recursive ~acc:(nonrecursive :: acc) |
| 678 |
in |
| 679 |
loop tds ~acc:[] |
| 680 |
;; |
| 681 |
|
| 682 |
let str_type_decl ~ctxt (rec_flag, tds) tags_of_doc_comments = |
| 683 |
let loc = Expansion_context.Deriver.derived_item_loc ctxt in |
| 684 |
let recursive, nonrecursive = partition_recursive_and_nonrecursive ~rec_flag tds in |
| 685 |
[ recursive_grammars ~ctxt ~loc ~tags_of_doc_comments recursive |
| 686 |
; nonrecursive_grammars ~ctxt ~loc ~tags_of_doc_comments nonrecursive |
| 687 |
] |
| 688 |
|> List.concat |
| 689 |
;; |
| 690 |
|
| 691 |
let sig_type_decl ~ctxt:_ (_rec_flag, tds) = |
| 692 |
List.map tds ~f:(fun td -> |
| 693 |
let loc = td.ptype_loc in |
| 694 |
value_description |
| 695 |
~loc |
| 696 |
~name:(Loc.map td.ptype_name ~f:grammar_name) |
| 697 |
~type_:(combinator_type_of_type_declaration td ~f:grammar_type) |
| 698 |
~prim:[] |
| 699 |
|> psig_value ~loc) |
| 700 |
;; |
| 701 |
|
| 702 |
let extension_loc ~ctxt = |
| 703 |
let loc = Expansion_context.Extension.extension_point_loc ctxt in |
| 704 |
{ loc with loc_ghost = true } |
| 705 |
;; |
| 706 |
|
| 707 |
let core_type ~tags_of_doc_comments ~ctxt core_type = |
| 708 |
let loc = extension_loc ~ctxt in |
| 709 |
pexp_constraint |
| 710 |
~loc |
| 711 |
(core_type |
| 712 |
|> grammar_of_type ~rec_flag:Nonrecursive ~tags_of_doc_comments |
| 713 |
|> typed_grammar ~loc) |
| 714 |
(core_type |> grammar_type ~loc) |
| 715 |
|> Merlin_helpers.hide_expression |
| 716 |
;; |
| 717 |
|
| 718 |
let type_extension ~ctxt core_type = |
| 719 |
assert_no_attributes_in#core_type core_type; |
| 720 |
let loc = extension_loc ~ctxt in |
| 721 |
core_type |> grammar_type ~loc |
| 722 |
;; |