How to work with refmt?

Hi,
I would like to work with the refmt tool in order to learn more about code is translated between the 3 syntax forms: rescript (res) reasonml (re) and also Ocaml (ml).

First question: How do you install the refmt command? If I install rescript with npm i -g bs-platform I don’t get a refmt command, it’s not linked into a PATH directory like /usr/local/bin.
Do you link it manually?

Second question: refmt --help only mentions the reason format (re). Is there actually a rescript format (res)?
Thanks
Ben

Edit:

Here is what happens when I try to use the res format:

/usr/local/lib/node_modules/bs-platform/darwin/refmt.exe --parse ml --print res test.ml
refmt: option `--print': invalid value `res', expected one of `ml', `re',
       `binary', `binary_reason', `ast' or `none'
# in your local npm project...
npm install --dev bs-platform
npx bsrefmt --help

bsrefmt is the vendored refmt shipped with bs-platform (aka ReScript), and is pinned to version 3.6.

The refmt command doesn’t support ReScript syntax, and the bsc -format cli doesn’t offer any option to print other syntaxes than ReScript (it can parse ml, and re though to print to ReScript). It’s a one-way converter.

If you are interested in how ReScript translates to Reason, you can use the Online Playground and toggle between .re / .res.

Printing .ml syntax is out of scope for our project, you’d need to use the Reason toolchain to translate between Reason and OCaml.

1 Like

Thanks for the replies. My first question is now answered, but for second one, I’m still not sure what to do

If I understand correctly there is no way of translating between Rescript and Ocaml. But what about Rescript and Reasonml? Is that possible at the cli?
You mention bsc -format … but how to use it? There is no --help and I could not find any documentition.
Any help is appreciated in my quest to at least in the end, even indirectly , translate between .res and .ml
Thanks.

Reason to ReScript printing is possible with the bsc command.

Usage is documented here: https://rescript-lang.org/docs/manual/latest/migrate-from-bucklescript-reason#upgrade-your-codebase

But this command only allows translation from Reason to ReScript (not the other way round), because it is usually used for upgrading codebases to ReScript and pretty printing ReScript source code

1 Like

do you know how it is done in the rescript playground? I think there you can switch between .re and .res bidirectionaly

As seen in the screenshot posted above, open the playground, click on the settings in the right pane, in the syntax section, press RE for toggling to Reason, RES for toggling to ReScript. The content of the editor should automatically adapt to the selected language.

1 Like

Sorry I was not clear enough with my question, I’ve meant on a technical (code base) level, how the is the translation done?

If you are interested in the low level bits, the source code for the current playground JS bundle is located in a pending rescript-compiler PR: https://github.com/rescript-lang/rescript-compiler/blob/8510a3d0ef5d5b1e8c84746d270a0603d5a4dee8/jscomp/refmt/jsoo_refmt_main.ml

There we are using the low level apis of the ReScript syntax parser to convert between syntaxes. These are all private apis though, because interfaces are still changing, and we don’t want ppl to run into breaking changes all the time.

edit: if this is not clear, the playground can do certain things that the CLI can not. It was too much effort to make back and forth translation completely lossless. I still added rudimentary conversion to the playground though, because i wanted to help previous Reason users converting ReScript snippets in a quick and accessible way.

3 Likes

Thanks for sharing. Is this packaged as a script somewhere? Thank you, sir.

…scrolls up