Rust parser in OCaml?

Hello,

Is anyone aware of a Rust parser, or at least Rust AST written in OCaml ?

Thanks for any anwser :slight_smile:
Giltho

Early Rust versions were written in OCaml. You can start from that, even though Rust syntax changed a lot. See this question on Reddit https://www.reddit.com/r/rust/comments/18b808/is_the_original_ocaml_compiler_still_available/

4 Likes

Another option, I forgot to tell about - the Tree Sitter, it generates highly efficient C runtime without dependencies, and the only parser generator that can produce incremental parsers. There is a Rust grammar for tree sitter already, but no OCaml bindings yet. You can see the example how Haskell bindings or Python bindings were done, and implement in a similar way. With this you would be able to parse Rust on the fly, as it is being typed in editor. There will be three advantages of this approach:

  • Better polished and modern Rust grammar, also being updated by a huge community
  • Creating OCaml bindings for tree-sitter will enable access for parsing other languages as well: Python, JavaScript, TypeScript, Ruby, Haskell, C/C++, Bash, Java, HTML, OCaml itself, Verilog, and many more…
  • Improving OCaml support in Tree-Sitter will make support of OCaml in GitHub semantic library closer, which they use for semantic code analysis in github.com site, which will lead to OCaml support in semantic code navigation at GitHub. You can check their documentation on adding new languages support for more information.

There are actually ocaml bindings: https://github.com/onivim/reason-tree-sitter

2 Likes

It depends exactly what you mean by “incremental” (what kind of changes you are robust again), but it’s fairly easy to use Menhir in an incremental way, in the sense that the prefix of the token stream that is identical to the previous run (or a previous memoized run) is not re-parsed. We described this in the Merlin paper (pages 9-11).

1 Like

From their site - the design of Tree-sitter was greatly influenced by the following research papers:

1 Like

@Khady Reason is not OCaml though, would be nice to have something without JavaScript inside.

It uses the reason syntax but not bucklescript unless I missed something. So this is native and not using any Javascript.

3 Likes

Just to clarify something–you can’t write bindings to a C library from BuckleScript (i.e. ReasonML JavaScript target). The only way is to use the ReasonML native OCaml target. So as @Khady said–the binding is using Reason syntax but native OCaml target.

For future readers: the repository has moved to https://github.com/onivim/oni2/tree/master/src/reason-tree-sitter. Currently it seems to be using its own fork of tree-sitter, as noted by the installation steps.

It’s not a fork, just vendoring and added a package.json to makes life easier with esy