Jul 1, 2020

A New Syntax for BuckleScript

First announcements on our latest efforts for a better syntax parser.

Cheng Lou
Syntax & Tools

Important: This is an archived blog post, kept for historical reasons. Please note that this information might be outdated.

Update / August 13th 2020: BuckleScript is now called ReScript, more infos here.

What's new?

The release of BuckleScript 8.1 contains a new important addition: we've rewritten the vendored Reason syntax parser and printer.

The rewrite was done by a community member of ours, Maxim. Maxim was a main contributor to the old Reason repo, and together we've reached the conclusion a while ago that the codebase needed a revamp. After wrestling with it for the longest time, we've settled on a low-key rewrite.

Syntax discussions have always been churny, so we didn't want to prematurely announce something before it's ready for proper critiques. After testing this extensively, we now deem it solid enough for your consumption.

Here's what you need to know:

  • The new syntax comes directly with your BuckleScript >=8.1 installation. You won't have to install anything else. It does not depend on the old refmt.

  • There are a few differences in syntax between the Reason syntax, documented in the migration docs.

  • The migration docs also guides you toward converting your codebase to the new syntax, file by file or project by project.

  • This syntax isn't available on Reason native compilation.

  • As always, check our documentation's Editor Plugins section for your editor's support of this new syntax.

  • To avoid conflict, we've employed the new file extensions .res and .resi, for implementation and interface respectively.

  • This means the syntax sits alongside the existing Reason and ml syntax. Your existing code will keep working!

  • Issues for the new syntax go here.

This is how it looks like:

RES
// src/example.res if hasEaten { Js.log("more dessert please") } else { Js.log("dessert please") } let message = j`Hello ${userName->Js.String.toUpperCase}!` type student<'extraInfo> = { name: string, age: int, otherInfo: 'extraInfo, } @bs.val external window: {..} = "window" window["addEventListener"]("focus", onFocus)

Here is an example of an error message:

Syntax error! src/test.res:7:31-8:0 5 │ } 6 │ 7 │ let message = j`hello ${world}! 8 │ Did you forget to close this template expression with a backtick?

The short version: install bs-platform@8.1 or later, create a new .res file in your new/existing project, then have fun!

Quality-wise, the parser now features excellent error messages, is noticeably faster on projects of all scales, and should be much more robust. The rearchitecture also allows us to release in a more professional manner. We'll go into more details on the motivation and architecture in the future when the syntax pans out well; hopefully, these will prove to be very interesting and illuminating for all engineers. Stay tuned!

Maxim's effort is our community at its best and we hope that you'll enjoy his work as much as we did!

Stay safe!

Want to read more?
Back to Overview