ocamlformat icon indicating copy to clipboard operation
ocamlformat copied to clipboard

Feature request: support BER MetaOCaml

Open ale64bit opened this issue 5 years ago • 6 comments

Is your feature request related to a problem? Please describe. When using ocamlformat with BER MetaOCaml sources, the bracket syntax is expanded to the corresponding attributes. For example:

let _ = .<1>.

expands to

let _ = (1 [@metaocaml.bracket])

Describe the solution you'd like I guess we could argue that BER MetaOCaml is a "dialect" of OCaml and the fix doesn't belong in ocamlformat. However, it would be great if it was supported. Concretely, the bracket syntax (both bracket .<expr>. and escape .~(expr)) should be kept as is.

Additional context none

ale64bit avatar Jan 18 '20 03:01 ale64bit

The problem is that we would need a way to tell the OCaml parser (that we rely on) that .<expr>. is a valid expression, formatting the corresponding attribute in the BER syntax is the easy part. Once #1026 is merged we would be able to format invalid files, so the parts where you use BER syntax would be ignored and the rest would be formatted.

gpetiot avatar Jan 21 '20 16:01 gpetiot

@gpetiot BER MetaOCaml is a fork of OCaml. Building OCamlformat with it automatically allows to parse BER syntax, since there is no new AST nodes. This issue is about OCamlformat's output. BER parser uses [@metaocaml. ..] attributes internally but OCamlformat prints them.

I would be in favor of this feature :) I wonder how compatibility with different version would work as we rely on ocaml-migrate-parsetree ? (OCamlformat only supports 4.07 to 4.09, though)

Julow avatar Jan 21 '20 18:01 Julow

This will at least make the test suite more complicated since we'll emit code that's invalid for the vanilla compiler, as @gpetiot said. So it would have to be gated behind a flag at least.

In any case, extensions are low priority at the moment. It would be easier to support than extensions like cppo or camlp4, but the focus for ocamlformat is vanilla ocaml code for now.

emillon avatar Jan 22 '20 09:01 emillon

I have a small (~50 line) patch that adds MetaOCaml dialect support here, which may be useful to anyone who just wants a quick solution. I have no comment on whether it's worth trying to maintain this upstream.

craigfe avatar Oct 19 '20 15:10 craigfe