这是indexloc提供的服务,不要输入任何密码
Skip to content

Conversation

@Dreian
Copy link

@Dreian Dreian commented Oct 10, 2025

Adds support for formatting runtime quotations and splices to ocamlformat.

The appropriate changes are added to the lexing, parsing, and formatting.

At the level of individual files, runtime metaprogramming can be enabled or disabled using a syntax directive: #syntax quotations [on|off]. This requires that we add such directives as a construct that does not lead to an error in the lexer; in fact, it has to get propagated through the parser and the formatter. These lexer directives are represented using the new lexer_directive type.

@Dreian Dreian marked this pull request as draft October 10, 2025 14:04
@Dreian Dreian force-pushed the runtime-metaprogramming branch 3 times, most recently from 535f727 to 3acbc08 Compare October 14, 2025 17:11
@Dreian Dreian marked this pull request as ready for review October 14, 2025 18:10

let double =
<[let x = <[42]> in
<[123 + $x]>]>
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need more thorough tests of the kinds of things that usually trip ocamlformat up:

  • quotes/spices of very long identifiers, splices split over multiple lines
  • Every way a comment can interact with a piece of metaprogramming syntax
  • Every way an attribute can interact with a piece of metaprogramming syntax

It may be instructive to look at @dvulakh's recent test for block indices for the kind of thoroughness we aim for. This may seem like overkill, but in practice we generally find that such tests catch bugs.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added some more tests with comments and long identifiers. No attribute tests yet, these are coming next.
(Waiting for builds to be reproducible.)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding those tests. To be a bit more explicit, when I suggested a test of "every way a comment can interact with a piece of metaprogramming syntax" I meant something more like this:

let _ = (* 1 *) <[ (* 2 *) 42 (* 3 *) + (* 4 *)
                     $(* 5 *)( (* 6 *) f (* 7 *) x (* 8 *) ) (* 9 *) ]> (* 10 *)

Which probably seems insane, but in practice these tests catch real bugs. This one does seem to work fine, though, happily! A similar test for the type-level syntax would be good.

On attributes, it would be good to have a test for attributes that are specifically applied to the new syntax nodes. Testing quickly, this seems to hit the round trip check, so we're probably not printing attributes in all the spots we need to:

let _ = <[ $(x + y) [@bar] ]>

@Dreian Dreian force-pushed the runtime-metaprogramming branch from 0f0176b to 98072f9 Compare November 11, 2025 12:35
@Dreian Dreian force-pushed the runtime-metaprogramming branch from c165665 to 6ae29b6 Compare November 13, 2025 12:18
lib/Exposed.ml Outdated
| Ptyp_quote _ -> true
| _ -> false

let rec expression exp =
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was going to say that this looks incomplete (for example, I think it would need Pexp_cons). But I went to look at use sites and couldn't find any - dead code maybe?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are correct that it is dead code. I thought that I might need it but didn't in the end. Removing.

lib/Exposed.ml Outdated
| PTyp t -> core_type t
| PPat _ -> false

let rec expression exp =
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also don't see any uses of this. Probably I'm just missing something.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused (same as with the previous comment). Removing.


let double =
<[let x = <[42]> in
<[123 + $x]>]>
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding those tests. To be a bit more explicit, when I suggested a test of "every way a comment can interact with a piece of metaprogramming syntax" I meant something more like this:

let _ = (* 1 *) <[ (* 2 *) 42 (* 3 *) + (* 4 *)
                     $(* 5 *)( (* 6 *) f (* 7 *) x (* 8 *) ) (* 9 *) ]> (* 10 *)

Which probably seems insane, but in practice these tests catch real bugs. This one does seem to work fine, though, happily! A similar test for the type-level syntax would be good.

On attributes, it would be good to have a test for attributes that are specifically applied to the new syntax nodes. Testing quickly, this seems to hit the round trip check, so we're probably not printing attributes in all the spots we need to:

let _ = <[ $(x + y) [@bar] ]>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants