Skip to content

Commit b9d7988

Browse files
ext_attrs everywhere !
Add ext_attrs to exception and fixes ext with regards to opens
1 parent ac06d48 commit b9d7988

File tree

13 files changed

+80
-124
lines changed

13 files changed

+80
-124
lines changed

lib/Ast.ml

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -333,15 +333,15 @@ module Structure_item = struct
333333
:: _ )
334334
|Pstr_open
335335
{popen_attributes= {attrs_before= atrs1; attrs_after= atrs2; _}; _}
336-
|Pstr_exception
337-
{ ptyexn_attributes= atrs1
338-
; ptyexn_constructor= {pext_attributes= atrs2; _}
339-
; _ }
340336
|Pstr_modtype
341337
{pmtd_ext_attrs= {attrs_before= atrs1; attrs_after= atrs2; _}; _} ->
342338
List.exists ~f:Attr.is_doc atrs1 || List.exists ~f:Attr.is_doc atrs2
343339
(* three attribute lists *)
344-
| Pstr_include
340+
| Pstr_exception
341+
{ ptyexn_attributes= {attrs_before= atrs1; attrs_after= atrs2; _}
342+
; ptyexn_constructor= {pext_attributes= atrs3; _}
343+
; _ }
344+
|Pstr_include
345345
{ pincl_mod= {pmod_attributes= atrs1; _}
346346
; pincl_attributes= {attrs_before= atrs2; attrs_after= atrs3; _}
347347
; _ }
@@ -445,16 +445,16 @@ module Signature_item = struct
445445
{pmtd_ext_attrs= {attrs_before= atrs1; attrs_after= atrs2; _}; _}
446446
|Psig_modsubst
447447
{pms_ext_attrs= {attrs_before= atrs1; attrs_after= atrs2; _}; _}
448-
|Psig_exception
449-
{ ptyexn_attributes= atrs1
450-
; ptyexn_constructor= {pext_attributes= atrs2; _}
451-
; _ }
452448
|Psig_open
453449
{popen_attributes= {attrs_before= atrs1; attrs_after= atrs2; _}; _}
454450
->
455451
List.exists ~f:Attr.is_doc atrs1 || List.exists ~f:Attr.is_doc atrs2
456452
(* three attribute list *)
457-
| Psig_recmodule
453+
| Psig_exception
454+
{ ptyexn_attributes= {attrs_before= atrs1; attrs_after= atrs2; _}
455+
; ptyexn_constructor= {pext_attributes= atrs3; _}
456+
; _ }
457+
|Psig_recmodule
458458
( { pmd_type= {pmty_attributes= atrs1; _}
459459
; pmd_ext_attrs= {attrs_before= atrs2; attrs_after= atrs3; _}
460460
; _ }

lib/Exposed.ml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,10 @@ module Right = struct
9595

9696
(* exception C of ... * ... * < ... > *)
9797
let type_exception = function
98-
| {ptyexn_attributes= _ :: _; _} -> false
98+
| { ptyexn_attributes=
99+
{attrs_before= _ :: _; _} | {attrs_after= _ :: _; _}
100+
; _ } ->
101+
false
99102
| {ptyexn_constructor; _} -> extension_constructor ptyexn_constructor
100103

101104
(* val x : < ... > *)

lib/Fmt_ast.ml

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -587,12 +587,6 @@ let rec fmt_extension_aux c ctx ~key (ext, pld) =
587587
assert (not (Cmts.has_before c.cmts pstr_loc)) ;
588588
assert (not (Cmts.has_after c.cmts pstr_loc)) ;
589589
hvbox 0 (fmt_quoted_string (Ext.Key.to_string key) ext str delim)
590-
| _, PStr [({pstr_loc; _} as si)], (Pld _ | Str _ | Top)
591-
when Source.extension_using_sugar ~name:ext ~payload:pstr_loc ->
592-
fmt_structure_item c ~last:true ~ext ~semisemi:false (sub_str ~ctx si)
593-
| _, PSig [({psig_loc; _} as si)], (Pld _ | Sig _ | Top)
594-
when Source.extension_using_sugar ~name:ext ~payload:psig_loc ->
595-
fmt_signature_item c ~ext (sub_sig ~ctx si)
596590
| _, PPat (({ppat_loc; _} as pat), _), (Pld _ | Top)
597591
when Source.extension_using_sugar ~name:ext ~payload:ppat_loc ->
598592
fmt_pattern c ~ext (sub_pat ~ctx pat)
@@ -3328,7 +3322,9 @@ and fmt_type_extension c ctx
33283322
; ptyext_loc } =
33293323
let c = update_config_attrs c ptyext_attributes in
33303324
let ext = ptyext_attributes.attrs_extension in
3331-
let doc, _doc_after, attrs_before, attrs_after = fmt_docstring_around_item_attrs ~force_before:true c ptyext_attributes in
3325+
let doc, _doc_after, attrs_before, attrs_after =
3326+
fmt_docstring_around_item_attrs ~force_before:true c ptyext_attributes
3327+
in
33323328
let fmt_ctor ctor = hvbox 0 (fmt_extension_constructor c ctx ctor) in
33333329
Cmts.fmt c ptyext_loc
33343330
@@ hvbox 2
@@ -3354,17 +3350,24 @@ and fmt_type_extension c ctx
33543350
and fmt_type_exception ~pre c ctx
33553351
{ptyexn_attributes= item_attrs; ptyexn_constructor; ptyexn_loc} =
33563352
let {pext_attributes= cons_attrs; _} = ptyexn_constructor in
3357-
let docs, item_attrs = extract_doc_attrs [] item_attrs in
3353+
let docs, attrs_before = extract_doc_attrs [] item_attrs.attrs_before in
3354+
let docs, attrs_after = extract_doc_attrs docs item_attrs.attrs_after in
33583355
let docs, cons_attrs = extract_doc_attrs docs cons_attrs in
33593356
let doc_before, doc_after = fmt_docstring_around_item' c docs in
33603357
let ptyexn_constructor =
33613358
{ptyexn_constructor with pext_attributes= cons_attrs}
33623359
in
3360+
let ext = item_attrs.attrs_extension in
33633361
Cmts.fmt c ptyexn_loc
33643362
(hvbox 0
33653363
( doc_before
3366-
$ hvbox 2 (pre $ fmt_extension_constructor c ctx ptyexn_constructor)
3367-
$ fmt_item_attributes c ~pre:(Break (1, 0)) item_attrs
3364+
$ hvbox 2
3365+
( pre
3366+
$ fmt_extension_suffix c ext
3367+
$ fmt_attributes c ~pre:(Break (1, 0)) attrs_before
3368+
$ fmt "@ "
3369+
$ fmt_extension_constructor c ctx ptyexn_constructor )
3370+
$ fmt_item_attributes c ~pre:(Break (1, 0)) attrs_after
33683371
$ doc_after ) )
33693372

33703373
and fmt_extension_constructor c ctx ec =
@@ -3514,7 +3517,7 @@ and fmt_signature c ctx itms =
35143517
let ast x = Sig x in
35153518
fmt_item_list c ctx update_config ast fmt_item itms
35163519

3517-
and fmt_signature_item c ?ext {ast= si; _} =
3520+
and fmt_signature_item c {ast= si; _} =
35183521
protect c (Sig si)
35193522
@@
35203523
let fmt_cmts_before = Cmts.Toplevel.fmt_before c si.psig_loc in
@@ -3525,7 +3528,7 @@ and fmt_signature_item c ?ext {ast= si; _} =
35253528
match si.psig_desc with
35263529
| Psig_attribute attr -> fmt_floating_attributes_and_docstrings c [attr]
35273530
| Psig_exception exc ->
3528-
let pre = str "exception" $ fmt_extension_suffix c ext $ fmt "@ " in
3531+
let pre = str "exception" in
35293532
hvbox 2 (fmt_type_exception ~pre c ctx exc)
35303533
| Psig_extension (ext, atrs) ->
35313534
let doc_before, doc_after, atrs = fmt_docstring_around_item c atrs in
@@ -4095,8 +4098,8 @@ and fmt_type c ?eq rec_flag decls ctx =
40954098
let ast x = Td x in
40964099
fmt_item_list c ctx update_config ast fmt_decl decls
40974100

4098-
and fmt_structure_item c ~last:last_item ?ext ~semisemi
4099-
{ctx= parent_ctx; ast= si} =
4101+
and fmt_structure_item c ~last:last_item ~semisemi {ctx= parent_ctx; ast= si}
4102+
=
41004103
protect c (Str si)
41014104
@@
41024105
let ctx = Str si in
@@ -4115,7 +4118,7 @@ and fmt_structure_item c ~last:last_item ?ext ~semisemi
41154118
$ cbox 0 ~name:"eval" (fmt_expression c (sub_exp ~ctx exp))
41164119
$ fmt_item_attributes c ~pre:Space atrs
41174120
| Pstr_exception extn_constr ->
4118-
let pre = str "exception" $ fmt_extension_suffix c ext $ fmt "@ " in
4121+
let pre = str "exception" in
41194122
hvbox 2 ~name:"exn" (fmt_type_exception ~pre c ctx extn_constr)
41204123
| Pstr_include {pincl_mod; pincl_attributes= attributes; pincl_loc} ->
41214124
update_config_maybe_disabled_attrs c pincl_loc attributes
@@ -4133,9 +4136,8 @@ and fmt_structure_item c ~last:last_item ?ext ~semisemi
41334136
fmt_or_k
41344137
(is_override popen_override)
41354138
( str "open!"
4136-
$ fmt_if (Option.is_some attributes.attrs_extension) "@ "
4137-
$ opt ext (fun _ -> str " " $ fmt_extension_suffix c ext) )
4138-
(str "open" $ fmt_extension_suffix c ext)
4139+
$ fmt_if (Option.is_some attributes.attrs_extension) "@ " )
4140+
(str "open")
41394141
in
41404142
fmt_module_statement c ~attributes ~keyword (sub_mod ~ctx popen_expr)
41414143
| Pstr_primitive vd -> fmt_value_description c ctx vd

test/passing/tests/js_source.ml.ocp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ class type%foo [@foo] x = x
184184

185185
external%foo [@foo] x : _ = ""
186186

187-
exception%foo X [@foo]
187+
exception%foo [@foo] X
188188

189189
module%foo [@foo] M = M
190190

@@ -206,7 +206,7 @@ module type S = sig
206206

207207
type%foo [@foo] t += T
208208

209-
exception%foo X [@foo]
209+
exception%foo [@foo] X
210210

211211
module%foo [@foo] M : S
212212

test/passing/tests/js_source.ml.ref

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ class type%foo [@foo] x = x
184184

185185
external%foo [@foo] x : _ = ""
186186

187-
exception%foo X [@foo]
187+
exception%foo [@foo] X
188188

189189
module%foo [@foo] M = M
190190

@@ -206,7 +206,7 @@ module type S = sig
206206

207207
type%foo [@foo] t += T
208208

209-
exception%foo X [@foo]
209+
exception%foo [@foo] X
210210

211211
module%foo [@foo] M : S
212212

test/passing/tests/source.ml.ref

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ class type%foo [@foo] x = x
224224

225225
external%foo [@foo] x : _ = ""
226226

227-
exception%foo X [@foo]
227+
exception%foo [@foo] X
228228

229229
module%foo [@foo] M = M
230230

@@ -249,7 +249,7 @@ module type S = sig
249249

250250
type%foo [@foo] t += T
251251

252-
exception%foo X [@foo]
252+
exception%foo [@foo] X
253253

254254
module%foo [@foo] M : S
255255

vendor/parser-extended/ast_helper.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -519,12 +519,12 @@ module Te = struct
519519
ptyext_attributes = add_docs_attrs' docs attrs;
520520
}
521521

522-
let mk_exception ?(loc = !default_loc) ?(attrs = []) ?(docs = empty_docs)
522+
let mk_exception ?(loc = !default_loc) ?(attrs = Attr.ext_attrs ()) ?(docs = empty_docs)
523523
constructor =
524524
{
525525
ptyexn_constructor = constructor;
526526
ptyexn_loc = loc;
527-
ptyexn_attributes = add_docs_attrs docs attrs;
527+
ptyexn_attributes = add_docs_attrs' docs attrs;
528528
}
529529

530530
let constructor ?(loc = !default_loc) ?(attrs = [])

vendor/parser-extended/ast_helper.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ module Te:
235235
?params:(core_type * variance_and_injectivity) list ->
236236
?priv:private_flag -> lid -> extension_constructor list -> type_extension
237237

238-
val mk_exception: ?loc:loc -> ?attrs:attrs -> ?docs:docs ->
238+
val mk_exception: ?loc:loc -> ?attrs:ext_attrs -> ?docs:docs ->
239239
extension_constructor -> type_exception
240240

241241
val constructor: ?loc:loc -> ?attrs:attrs -> ?docs:docs -> ?info:info ->

vendor/parser-extended/ast_mapper.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ module T = struct
257257
let map_type_exception sub
258258
{ptyexn_constructor; ptyexn_loc; ptyexn_attributes} =
259259
let loc = sub.location sub ptyexn_loc in
260-
let attrs = sub.attributes sub ptyexn_attributes in
260+
let attrs = sub.ext_attrs sub ptyexn_attributes in
261261
Te.mk_exception ~loc ~attrs
262262
(sub.extension_constructor sub ptyexn_constructor)
263263

vendor/parser-extended/parser.mly

Lines changed: 17 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,12 @@ let mkconst ~loc c = Const.mk ~loc:(make_loc loc) c
7171

7272
let pstr_type (nr, tys) =
7373
Pstr_type (nr, tys)
74-
let pstr_exception (te, ext) =
75-
(Pstr_exception te, ext)
7674

7775
let psig_type (nr, tys) =
7876
Psig_type (nr, tys)
7977
let psig_typesubst (nr, tys) =
8078
assert (nr = Recursive); (* see [no_nonrec_flag] *)
8179
Psig_typesubst tys
82-
let psig_exception (te, ext) =
83-
(Psig_exception te, ext)
8480

8581
let mkctf ~loc ?attrs ?docs d =
8682
Ctf.mk ~loc:(make_loc loc) ?attrs ?docs d
@@ -133,8 +129,6 @@ let mkpatvar ~loc name =
133129
let ghexp ~loc d = Exp.mk ~loc:(ghost_loc loc) d
134130
let ghpat ~loc d = Pat.mk ~loc:(ghost_loc loc) d
135131
let ghtyp ~loc d = Typ.mk ~loc:(ghost_loc loc) d
136-
let ghstr ~loc d = Str.mk ~loc:(ghost_loc loc) d
137-
let ghsig ~loc d = Sig.mk ~loc:(ghost_loc loc) d
138132

139133
let mkinfix arg1 op arg2 =
140134
Pexp_infix(op, arg1, arg2)
@@ -287,22 +281,6 @@ let wrap_mod_attrs ~loc:_ attrs body =
287281
let wrap_mty_attrs ~loc:_ attrs body =
288282
{body with pmty_attributes = attrs @ body.pmty_attributes}
289283

290-
let wrap_str_ext ~loc body ext =
291-
match ext with
292-
| None -> body
293-
| Some id -> ghstr ~loc (Pstr_extension ((id, PStr [body]), []))
294-
295-
let wrap_mkstr_ext ~loc (item, ext) =
296-
wrap_str_ext ~loc (mkstr ~loc item) ext
297-
298-
let wrap_sig_ext ~loc body ext =
299-
match ext with
300-
| None -> body
301-
| Some id -> ghsig ~loc (Psig_extension ((id, PSig [body]), []))
302-
303-
let wrap_mksig_ext ~loc (item, ext) =
304-
wrap_sig_ext ~loc (mksig ~loc item) ext
305-
306284
let mk_quotedext ~loc (id, idloc, str, strloc, delim) =
307285
let exp_id = mkloc id idloc in
308286
let const = Const.mk ~loc:strloc (Pconst_string (str, strloc, delim)) in
@@ -1241,10 +1219,8 @@ structure_item:
12411219
{ Pstr_typext $1 }
12421220
| type_declarations
12431221
{ pstr_type $1 }
1244-
)
1245-
| wrap_mkstr_ext(
1246-
str_exception_declaration
1247-
{ pstr_exception $1 }
1222+
| str_exception_declaration
1223+
{ Pstr_exception $1 }
12481224
)
12491225
{ $1 }
12501226
;
@@ -1497,11 +1473,8 @@ signature_item:
14971473
{ psig_typesubst $1 }
14981474
| open_description
14991475
{ Psig_open $1 }
1500-
)
1501-
{ $1 }
1502-
| wrap_mksig_ext(
1503-
sig_exception_declaration
1504-
{ psig_exception $1 }
1476+
| str_exception_declaration
1477+
{ Psig_exception $1 }
15051478
)
15061479
{ $1 }
15071480

@@ -2866,32 +2839,34 @@ str_exception_declaration:
28662839
{ $1 }
28672840
| EXCEPTION
28682841
ext = ext
2869-
attrs1 = attributes
2842+
before = attributes
28702843
id = mkrhs(constr_ident)
28712844
EQUAL
28722845
lid = mkrhs(constr_longident)
2873-
attrs2 = attributes
2874-
attrs = post_item_attributes
2846+
attrs_inside = attributes
2847+
after = post_item_attributes
28752848
{ let loc = make_loc $sloc in
28762849
let docs = symbol_docs $sloc in
2850+
let attrs = Attr.ext_attrs ~before ~after ?ext () in
28772851
Te.mk_exception ~attrs
2878-
(Te.rebind id lid ~attrs:(attrs1 @ attrs2) ~loc ~docs)
2879-
, ext }
2852+
(Te.rebind id lid ~attrs:attrs_inside ~loc ~docs)
2853+
}
28802854
;
28812855
sig_exception_declaration:
28822856
EXCEPTION
28832857
ext = ext
2884-
attrs1 = attributes
2858+
before = attributes
28852859
id = mkrhs(constr_ident)
28862860
vars_args_res = generalized_constructor_arguments
2887-
attrs2 = attributes
2888-
attrs = post_item_attributes
2861+
attrs_inside = attributes
2862+
after = post_item_attributes
28892863
{ let vars, args, res = vars_args_res in
2890-
let loc = make_loc ($startpos, $endpos(attrs2)) in
2864+
let loc = make_loc ($startpos, $endpos(attrs_inside)) in
28912865
let docs = symbol_docs $sloc in
2866+
let attrs = Attr.ext_attrs ~before ~after ?ext () in
28922867
Te.mk_exception ~attrs
2893-
(Te.decl id ~vars ~args ?res ~attrs:(attrs1 @ attrs2) ~loc ~docs)
2894-
, ext }
2868+
(Te.decl id ~vars ~args ?res ~attrs:attrs_inside ~loc ~docs)
2869+
}
28952870
;
28962871
%inline let_exception_declaration:
28972872
mkrhs(constr_ident) generalized_constructor_arguments attributes

0 commit comments

Comments
 (0)