[ANN] ReScript parser for Tree-sitter

Hi folks! After NeoVim 0.5.0 was finally released last month I tried syntax highlighting based on Tree-sitter and—wow—it definitely delivers a new level of comfort. Different things have different colors based on the semantic of a language. This makes source code easier to scan and reduces cognitive load.

I haven’t tried Tree-sitter based movements yet but suspect they are awesome too.

So, to have the same comfort as built-in languages provide, I made a Tree-sitter parser for ReScript. You can grab it here:

For a quick comparison take a look of my Vim with stock nvim-rescript and with extra nvim-treesitter-rescript:

Before

After


The parser is imperfect yet. Some stuff is missing. Notably, module meta-vodoo (functors, etc) and expressions inside template strings. Some precedences might be messed up and I didn’t optimize performance yet (albeit it parses at orders of a few ms on my typical files). It is because I still don’t understand how Tree-sitter works and made the grammar basically this way:

image

Contributions are welcome. I’d like the repos to be eventually merged/acquired by nvim-rescript and tree-sitter itself.

18 Likes

I’m still working on it. My codebases are 100% passing now. A few contributors made the parser yet more compatible, thanks men! Now I’m taking the heaviest ReScript codebases from GitHub and polish the parser until they 100% passing. One cornerstone now are the nested /* /* comments */ */.

Folks, can someone remember a popular language where the multiline comments are allowed to nest as in ReScript? I’m going to steal the C/C++ scanner implementation :speak_no_evil: All langs I aware terminate any level of comments as soon as a closing token (like */) is found.

/*
  Who knows /* a language */
  that behaves cool /* like /* ReScript */ */
  ?
*/
1 Like

Well, there’s OCaml, the language that ReScript is based on. I haven’t worked with Tree-sitter myself before, but I imagine that there’s probably an OCaml Tree-sitter project out there somewhere.

2 Likes

Ah, of course, OCaml! Thank you.

Yes, OCaml has Tree-sitter project and it has nested comments. Just what I need.

2 Likes