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.jsonfile the following files:rust-lib/src/generated.rswith marshaling code and parsing/generating code (if applicable) for eachsyntypeocaml-lib/ocaml_rust_parser_generator.mliwith types definitions and functionsocaml-lib/ocaml_rust_parser_generator.mlwith implementations that call the functions from theMarshalmodule and functions from the Rust library
rust-lib/src/lib.rswithMarshalingOutputandMarshalingInputstructs 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_spanThe library can be built by executing the following command (Rust and OCaml with Dune are required):
make buildAn 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 exampleThis 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.
