Skip to content

Library for parsing and generating Rust in OCaml

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT
Notifications You must be signed in to change notification settings

afdw/ocaml-rust-parser-generator

Repository files navigation

ocaml-rust-parser-generator

This is a library for parsing and generating Rust in OCaml. It is essentially (mostly automatically) generated bindings for syn and proc-macro2.

Architecture

Data is communicated to and from OCaml using the Marshal module. Data structures are serialized to and deserialized from the corresponding binary format and given to taken or from OCaml in the form of bytes. To simplify writing functions that are executed from OCaml, ocaml-rs is used.

The modules of the project are in following folders:

  • generator, a generator written in Rust
  • rust-lib, a Rust library that is loaded from OCaml
  • ocaml-lib, an OCaml library to be used by the end users

The main files containing code are:

API

Below SynType mean almost any of the syn types.

type rs_syn_type = ...

val token_stream_of_string : string -> rs_token_stream
val string_of_token_stream : rs_token_stream -> string
val span_of_token_stream : rs_token_stream -> rs_span

val parse_syn_type_from_token_stream : rs_token_stream -> rs_syn_type
val parse_syn_type_from_string : string -> rs_syn_type
val generate_syn_type_to_token_stream : rs_syn_type -> rs_token_stream
val generate_syn_type_to_string : rs_syn_type -> string
val span_of_syn_type : rs_syn_type -> rs_span

API

Building and running

The library can be built by executing the following command (Rust and OCaml with Dune are required):

make build

An example, which demonstrates the usage of the library (round tripping a Rust file and getting parts from it) and also how Rustfmt can be used to make the output better (the shexp package from OPAM is required), can be run by executing the following command:

make example

License

This project is licensed under either of

at your option.

About

Library for parsing and generating Rust in OCaml

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published