package ppxlib
Install
Dune Dependency
Authors
Maintainers
Sources
sha256=89a98c95ddd0bfbac17b5a936f6811af7097be3258c482d5859b73e9de9b4552
sha512=b19306473d867252d382e58e9b697531c5edccdc9283b5eaf72f524803c2fca2a58a5e8f25bee198b00de82cf8ef805b43f7488791c3ac5beb0ffba938ded826
doc/ppxlib/Ppxlib/Context_free/Rule/index.html
Module Context_free.Rule
Source
Local rewriting rules.
This module lets you define local rewriting rules, such as extension point expanders. It is not completely generic and you cannot define any kind of rewriting, it currently focuses on what is commonly used. New scheme can be added on demand.
We have some ideas to make this fully generic, but this hasn't been a priority so far.
Rewrite an extension point
val special_function :
string ->
(Astlib.Ast_502.Parsetree.expression ->
Astlib.Ast_502.Parsetree.expression option) ->
t
val special_function' :
Longident.t ->
(Astlib.Ast_502.Parsetree.expression ->
Astlib.Ast_502.Parsetree.expression option) ->
t
special_function id expand
is a rule to rewrite a function call at parsing time. id
is the identifier to match on and expand
is used to expand the full function application (it gets the Pexp_apply node). If the function is found in the tree without being applied, expand
gets only the identifier (Pexp_ident node) so you should handle both cases.
If id
is an operator identifier and contains dots, it should be parenthesized (e.g. "(+.+)"
). Another option is to use the special_function'
variant which takes directly a Longident.t
argument.
expand
must decide whether the expression it receive can be rewritten or not. Especially ppxlib makes the assumption that expand
is idempotent. It will loop if it is not.
Used for the constant
function.
val constant :
Constant_kind.t ->
char ->
(Location.t -> string -> Ppxlib_ast.Parsetree.expression) ->
t
constant kind suffix expander
Registers an extension for transforming constants literals, based on the suffix character.
The rest of this API is for rewriting rules that apply when a certain attribute is present. The API is not complete and is currently only enough to implement deriving.
type ('a, 'b, 'c) attr_group_inline =
('b, 'c) Attribute.t ->
(ctxt:Expansion_context.Deriver.t ->
Ppxlib_ast.Asttypes.rec_flag ->
'b list ->
'c option list ->
'a list) ->
t
Match the attribute on a group of items, such as a group of recursive type definitions (Pstr_type, Psig_type). The expander will be triggered if any of the item has the attribute. The expander is called as follow:
expand ~loc ~path rec_flag items values
where values
is the list of values associated to the attribute for each item in items
. expand
must return a list of element to add after the group. For instance a list of structure item to add after a group of type definitions.
val attr_str_type_decl :
(Astlib.Ast_502.Parsetree.structure_item,
Astlib.Ast_502.Parsetree.type_declaration,
_)
attr_group_inline
val attr_sig_type_decl :
(Astlib.Ast_502.Parsetree.signature_item,
Astlib.Ast_502.Parsetree.type_declaration,
_)
attr_group_inline
val attr_str_type_decl_expect :
(Astlib.Ast_502.Parsetree.structure_item,
Astlib.Ast_502.Parsetree.type_declaration,
_)
attr_group_inline
The _expect variants are for producing code that is compared to what the user wrote in the source code.
val attr_sig_type_decl_expect :
(Astlib.Ast_502.Parsetree.signature_item,
Astlib.Ast_502.Parsetree.type_declaration,
_)
attr_group_inline
type ('a, 'b, 'c) attr_inline =
('b, 'c) Attribute.t ->
(ctxt:Expansion_context.Deriver.t -> 'b -> 'c -> 'a list) ->
t
Same as attr_group_inline
but for elements that are not part of a group, such as exceptions and type extensions
val attr_str_module_type_decl :
(Astlib.Ast_502.Parsetree.structure_item,
Astlib.Ast_502.Parsetree.module_type_declaration,
_)
attr_inline
val attr_sig_module_type_decl :
(Astlib.Ast_502.Parsetree.signature_item,
Astlib.Ast_502.Parsetree.module_type_declaration,
_)
attr_inline
val attr_str_module_type_decl_expect :
(Astlib.Ast_502.Parsetree.structure_item,
Astlib.Ast_502.Parsetree.module_type_declaration,
_)
attr_inline
val attr_sig_module_type_decl_expect :
(Astlib.Ast_502.Parsetree.signature_item,
Astlib.Ast_502.Parsetree.module_type_declaration,
_)
attr_inline
val attr_str_type_ext :
(Astlib.Ast_502.Parsetree.structure_item,
Astlib.Ast_502.Parsetree.type_extension,
_)
attr_inline
val attr_sig_type_ext :
(Astlib.Ast_502.Parsetree.signature_item,
Astlib.Ast_502.Parsetree.type_extension,
_)
attr_inline
val attr_str_type_ext_expect :
(Astlib.Ast_502.Parsetree.structure_item,
Astlib.Ast_502.Parsetree.type_extension,
_)
attr_inline
val attr_sig_type_ext_expect :
(Astlib.Ast_502.Parsetree.signature_item,
Astlib.Ast_502.Parsetree.type_extension,
_)
attr_inline
val attr_str_exception :
(Astlib.Ast_502.Parsetree.structure_item,
Astlib.Ast_502.Parsetree.type_exception,
_)
attr_inline
val attr_sig_exception :
(Astlib.Ast_502.Parsetree.signature_item,
Astlib.Ast_502.Parsetree.type_exception,
_)
attr_inline
val attr_str_exception_expect :
(Astlib.Ast_502.Parsetree.structure_item,
Astlib.Ast_502.Parsetree.type_exception,
_)
attr_inline
val attr_sig_exception_expect :
(Astlib.Ast_502.Parsetree.signature_item,
Astlib.Ast_502.Parsetree.type_exception,
_)
attr_inline
val attr_str_class_type_decl :
(Astlib.Ast_502.Parsetree.structure_item,
Astlib.Ast_502.Parsetree.class_type Astlib.Ast_502.Parsetree.class_infos,
_)
attr_group_inline
val attr_sig_class_type_decl :
(Astlib.Ast_502.Parsetree.signature_item,
Astlib.Ast_502.Parsetree.class_type Astlib.Ast_502.Parsetree.class_infos,
_)
attr_group_inline
val attr_str_class_type_decl_expect :
(Astlib.Ast_502.Parsetree.structure_item,
Astlib.Ast_502.Parsetree.class_type Astlib.Ast_502.Parsetree.class_infos,
_)
attr_group_inline
val attr_sig_class_type_decl_expect :
(Astlib.Ast_502.Parsetree.signature_item,
Astlib.Ast_502.Parsetree.class_type Astlib.Ast_502.Parsetree.class_infos,
_)
attr_group_inline
type ('item, 'parsed_payload) attr_floating_inline =
('item, 'parsed_payload) Attribute.Floating.t ->
(ctxt:Expansion_context.Deriver.t -> 'parsed_payload -> 'item list) ->
t
val attr_str_floating_expect_and_expand :
(Astlib.Ast_502.Parsetree.structure_item, _) attr_floating_inline
val attr_sig_floating_expect_and_expand :
(Astlib.Ast_502.Parsetree.signature_item, _) attr_floating_inline