Skip to content

Commit

Permalink
Merge pull request #496 from Octachron/stabler_special_function
Browse files Browse the repository at this point in the history
Context_free: add a special_function' variant
  • Loading branch information
NathanReb authored Jun 3, 2024
2 parents e8c40b1 + 0006b5b commit fd9a7e0
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
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

0 comments on commit fd9a7e0

Please sign in to comment.