-
Notifications
You must be signed in to change notification settings - Fork 15
Support for runtime metaprogramming #121
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
base: jane
Are you sure you want to change the base?
Conversation
535f727 to
3acbc08
Compare
|
|
||
| let double = | ||
| <[let x = <[42]> in | ||
| <[123 + $x]>]> |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.)
There was a problem hiding this comment.
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] ]>0f0176b to
98072f9
Compare
c165665 to
6ae29b6
Compare
lib/Exposed.ml
Outdated
| | Ptyp_quote _ -> true | ||
| | _ -> false | ||
|
|
||
| let rec expression exp = |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 = |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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]>]> |
There was a problem hiding this comment.
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] ]>
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 newlexer_directivetype.