Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Context_free: add a special_function' variant #496

Merged
merged 1 commit into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ unreleased
- Fix `Longident.parse` so it also handles indexing operators such as
`.!()`, `.%(;..)<-`, or `Vec.(.%())` (#494, @octachron)

- Add a `special_function'` variant which directly takes a `Longident.t`
argument to avoid the issue that `Longident.t` cover distinct syntaxic classes
which cannot be easily parsed by a common parser (#496, @octachron).

0.32.1 (2024-04-23)
-------------------

Expand Down
3 changes: 3 additions & 0 deletions src/context_free.ml
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ module Rule = struct
let special_function id f =
T (Special_function, { name = id; ident = Longident.parse id; expand = f })

let special_function' ident f =
T (Special_function, { name = Longident.name ident; ident; expand = f })

let constant kind suffix expand = T (Constant, { suffix; kind; expand })

let attr_str_type_decl attribute expand =
Expand Down
6 changes: 5 additions & 1 deletion src/context_free.mli
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,18 @@ module Rule : sig
(** Rewrite an extension point *)

val special_function : string -> (expression -> expression option) -> t

val special_function' : Longident.t -> (expression -> 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. ["(+.+)"]).
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.
Expand Down
Loading