This is a library for parsing and generating Rust in OCaml. It is essentially (mostly automatically) generated bindings for syn and proc-macro2.
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 Rustrust-lib
, a Rust library that is loaded from OCamlocaml-lib
, an OCaml library to be used by the end users
The main files containing code are:
generator/src/main.rs
, which generates from thesyn.json
file the following files:rust-lib/src/generated.rs
with marshaling code and parsing/generating code (if applicable) for eachsyn
typeocaml-lib/ocaml_rust_parser_generator.mli
with types definitions and functionsocaml-lib/ocaml_rust_parser_generator.ml
with implementations that call the functions from theMarshal
module and functions from the Rust library
rust-lib/src/lib.rs
withMarshalingOutput
andMarshalingInput
structs for marshaling OCaml values and hand-written marshaling code for some particular types
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
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
This project is licensed under either of
- Apache License, Version 2.0, (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.