declare fully_qualified_name context payload_pattern k
declares an attribute. k
is used to build the value resulting from parsing the payload.
For instance if a rewriter named "foo" expect the attribute @@default
on record field declaration with an expression as payload:
let default =
Attribute.declare "foo.default"
Attribute.Context.label_declaration
Ast_pattern.(pstr (pstr_eval __ nil))
(fun x -> x)
;;
fully_qualified_name
is expected to be a dot-separated list of names. When matching, any full suffix will be accepted. So for instance an attribute declared with name "foo.bar.default" will match exactly these attribute names: "default", "bar.default" and "foo.bar.default".
When matching against a list of attributes on an item, if several matches are possible, the longest one is used. For instance using the attribute "foo.default" declared in the previous example, on this code it will match the @foo.default 0
attribute:
type t =
{ x : int [@default 42] [@foo.default 0]
}
This is to allow the user to specify a @default
attribute for all re-writers that use it but still put a specific one for one specific re-writer.
It is not allowed to declare an attribute with a name that matches a previously-defined one on the same context. For instance trying to declare the same attribute twice will fail.