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 2 times, most recently from 204aad5 to 535f727 Compare October 14, 2025 17:01
@Dreian Dreian force-pushed the runtime-metaprogramming branch 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.

and syntax_directive =
{
psyn_mode: string loc;
psyn_toggle: bool;
Copy link

Choose a reason for hiding this comment

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

It's not obvious to me what psyn_mode and psyn_toggle represent - maybe a comment?

{
plex_desc: lexer_directive_desc;
plex_loc: Location.t
}
Copy link

Choose a reason for hiding this comment

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

It's not immediately obvious to me that we need to add these bits to parser-standard, since I think it is not used for printing. But maybe I'm failing to consider some aspect of the round trip tests?

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