S-expression parsing and writing in Rust

Overview

rsexp

S-expression parsing and writing in Rust using nom parser combinators.

Build Status Latest version Documentation License

This implemantion aims at being compatible with OCaml's sexplib. The main type for S-expression is as follows:

pub enum Sexp {
    Atom(Vec<u8>),
    List(Vec<Sexp>),
}

Reading and Writing Sexp Files

Reading a sexp file can be done by first reading the whole file content and then converting the resulting slice into a Sexp object as follows:

    let contents = std::fs::read(input_filename)?;
    let sexp = rsexp::from_slice(&contents)?;

Writing a sexp file can be done by first serializing the data to a buffer then writing this buffer out. Alternatively, the sexp.write(w)? function can be used to directly output the data to a w object that implements the Write trait.

    let data = sexp.to_bytes();
    std::fs::write(output_filename, data)?;

Conversion to/from Native Types

The OfSexp and SexpOf traits define some functions to convert a given type from/to a Sexp. These traits are implemented for most basic types, including maps. Two associated derive macros can be used to define these traits on structs and enums.

#[derive(OfSexp, SexpOf)]
struct StructXYZ {
    x: i64,
    y: Option<(i32, i32)>,
    z: String,
}

#[derive(OfSexp, SexpOf)]
enum Abc {
    A(StructXYZ),
    B { x: i64, z: String },
    C,
}


    let sexp = abc.sexp_of();
    let abc: Abc = sexp.of_sexp()?;
You might also like...
Learn Rust by writing Entirely Too Many linked lists

Learn Rust by writing Entirely Too Many Linked Lists Read the pretty version at https://rust-unofficial.github.io/too-many-lists/. Building Building r

Tutorial for parsing with nom 5.

Nom Tutorial Nom is a wonderful parser combinators library written in Rust. It can handle binary and text files. Consider it where you would otherwise

Static-checked parsing of regexes into structs

Statically-checked regex parsing into structs. This avoids common regex pitfalls like Off by one capture indexes Trying to get nonexistent captures De

PE Parsing, but blazing fast

PE Parser A blazing fast 🔥 PE Parser written in Rust Motivation The main goals of pe-parser is to write something suitable for a PE Loader. Is editin

A fast little combinational parsing library.
A fast little combinational parsing library.

neure A fast little combinational parsing library Performance rel is mean release, fat is mean release with lto=fat See examples Example use neure::*;

Fast fail2ban-like tools for parsing nginx logs

Fast2ban This is simple fail2ban-like replacement written in Rust. Usage: ./fast2ban # reads default config.toml from current directory ./fast2ban co

A webring of people who make cool stuff. technology, music, art, writing, anything goes!
A webring of people who make cool stuff. technology, music, art, writing, anything goes!

a webring of people who make cool stuff. technology, music, art, writing, anything goes!

A language for writing swim practices.

swimscript A language for writing swim practices. Table of contents Purpose Language Spec Purpose The goal of swimscript is to create a universal lang

Comments
  • Add criterion microbenchmarks and optimize sexp_no_leading_blank.

    Add criterion microbenchmarks and optimize sexp_no_leading_blank.

    This PR adds some microbenchmarks which generate different example sexps of different lengths, depths, number of characters per atom, and quoted/unquoted atoms.

    These benchmarks were then used to optimize sexp_no_leading_blank. The main two changes are:

    • remove context() calls, saving a few % across the board.
    • replace nom::branch::alt in sexp_no_leading_blank with a rust-native if-condition.

    The same optimization turns out not to improve things much in atom.

    See report for details (tested on Intel Xeon Processor (Cascadelake), 2693 MHz via digitalocean). Also tested on Intel(R) Core(TM) i7-4850HQ CPU @ 2.30GHz with similar results.

    Use this command to save a baseline (default name is base):

    cargo bench --bench rsexp_benchmark -- --save-baseline
    

    And this command to run comparing against a saved baseline:

    cargo bench --bench rsexp_benchmark --  --baseline base
    
    opened by AdamCanady 1
Owner
Laurent Mazare
Laurent Mazare
An expression based data notation, aimed at transpiling itself to any cascaded data notation.

Lala An expression oriented data notation, aimed at transpiling itself to any cascaded data notation. Lala is separated into three components: Nana, L

null 37 Mar 9, 2022
Fast and simple datetime, date, time and duration parsing for rust.

speedate Fast and simple datetime, date, time and duration parsing for rust. speedate is a lax† RFC 3339 date and time parser, in other words, it pars

Samuel Colvin 43 Nov 25, 2022
:crab: Small exercises to get you used to reading and writing Rust code!

rustlings ?? ❤️ Greetings and welcome to rustlings. This project contains small exercises to get you used to reading and writing Rust code. This inclu

The Rust Programming Language 33.1k Jan 2, 2023
This project contains small exercises to get you used to reading and writing Rust code

rustlings ?? ❤️ Greetings and welcome to rustlings. This project contains small exercises to get you used to reading and writing Rust code. This inclu

Cynthia Tran 1 May 24, 2022
🕶 Assorted checks and validations for writing safer Solana programs.

vipers ?? Assorted checks and validations for writing safer Solana programs. Motivation Solana's fee mechanism is unlike Ethereum's, in that the numbe

Saber 131 Sep 14, 2022
Thread-safe clone-on-write container for fast concurrent writing and reading.

sync_cow Thread-safe clone-on-write container for fast concurrent writing and reading. SyncCow is a container for concurrent writing and reading of da

null 40 Jan 16, 2023
Lightweight parsing for Rust proc macros

Lightweight parsing for Rust proc macros Venial is a WIP parser for Rust proc macros. When writing proc macros that need to parse Rust code (such as a

Olivier FAURE 148 Dec 30, 2022
A Rust macro for writing nested loop expressions

loop_chain A Rust macro for writing nested loop expressions Usage | Examples | Docs Dependencies [dependencies] loop_chain = "0.1.1" Usage For express

Takayuki Maeda 5 Jul 30, 2021
Take your first step in writing a compiler. Implemented in Rust.

first-step-rust Take your first step in writing a compiler, using Rust. Building from Source Make sure the Rust toolchain is installed on your compute

PKU Compiler Course 13 Aug 28, 2022
Writing Interpreters in Rust: a Guide

Writing Interpreters in Rust: a Guide This is an online book covering the lower level topics involved in writing an interpreter in Rust including: mem

Languages Hosted in Rust Working Groups 254 Jan 5, 2023