Skip to content

Commit

Permalink
Merge pull request #396 from janestreet/attribute.declare-with-attr-loc
Browse files Browse the repository at this point in the history
Add `Attribute.declare_with_attr_loc`.
  • Loading branch information
ceastlund authored Mar 14, 2023
2 parents 3006ef6 + e394211 commit 7159be3
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
unreleased
------------------

- Add `Attribute.declare_with_attr_loc` (#396, @dvulakh)

- Add "ns" and "res" as reserved namespaces(#388, @davesnx)

0.29.1 (14/02/2023)
Expand Down
19 changes: 14 additions & 5 deletions src/attribute.ml
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,8 @@ type packed_context =

type _ payload_parser =
| Payload_parser :
(payload, 'a, 'b) Ast_pattern.t * (name_loc:Location.t -> 'a)
(payload, 'a, 'b) Ast_pattern.t
* (attr_loc:Location.t -> name_loc:Location.t -> 'a)
-> 'b payload_parser

type ('a, 'b) t = {
Expand All @@ -277,7 +278,7 @@ let registrar =
| On_item t -> Some (Context.desc t)
| Floating t -> Some (Floating_context.desc t ^ " (floating)"))

let declare_with_name_loc name context pattern k =
let declare_with_all_args name context pattern k =
Name.Registrar.register ~kind:`Attribute registrar (On_item context) name;
{
name = Name.Pattern.make name;
Expand All @@ -286,7 +287,15 @@ let declare_with_name_loc name context pattern k =
}

let declare name context pattern k =
declare_with_name_loc name context pattern (fun ~name_loc:_ -> k)
declare_with_all_args name context pattern (fun ~attr_loc:_ ~name_loc:_ -> k)

let declare_with_name_loc name context pattern k =
declare_with_all_args name context pattern (fun ~attr_loc:_ ~name_loc ->
k ~name_loc)

let declare_with_attr_loc name context pattern k =
declare_with_all_args name context pattern (fun ~attr_loc ~name_loc:_ ->
k ~attr_loc)

module Attribute_table = Caml.Hashtbl.Make (struct
type t = string loc
Expand Down Expand Up @@ -332,7 +341,7 @@ let convert ?(do_mark_as_seen = true) pattern attr =
Ast_pattern.parse_res pattern
(Common.loc_of_payload attr)
attr.attr_payload
(k ~name_loc:attr.attr_name.loc)
(k ~attr_loc:attr.attr_loc ~name_loc:attr.attr_name.loc)

let get_res t ?mark_as_seen:do_mark_as_seen x =
let open Result in
Expand Down Expand Up @@ -420,7 +429,7 @@ module Floating = struct
{
name = Name.Pattern.make name;
context;
payload = Payload_parser (pattern, fun ~name_loc:_ -> k);
payload = Payload_parser (pattern, fun ~attr_loc:_ ~name_loc:_ -> k);
}

let convert_res ts x =
Expand Down
8 changes: 8 additions & 0 deletions src/attribute.mli
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,14 @@ val declare_with_name_loc :
(** Same as [declare] but the callback receives the location of the name of the
attribute. *)

val declare_with_attr_loc :
string ->
'a Context.t ->
(payload, 'b, 'c) Ast_pattern.t ->
(attr_loc:Location.t -> 'b) ->
('a, 'c) t
(** Same as [declare] but the callback receives the location of the attribute. *)

val name : _ t -> string
val context : ('a, _) t -> 'a Context.t

Expand Down

0 comments on commit 7159be3

Please sign in to comment.