11 stable releases

Uses old Rust 2015

2.2.3 Jul 6, 2017
2.2.1 Jul 5, 2017
2.1.3 Jun 26, 2017
1.1.4 Jun 23, 2017

#2084 in Parser implementations

Download history 24/week @ 2023-10-31 4/week @ 2023-11-07 19/week @ 2023-11-14 16/week @ 2023-11-21 38/week @ 2023-11-28 18/week @ 2023-12-05 4/week @ 2023-12-12 15/week @ 2023-12-19 25/week @ 2023-12-26 15/week @ 2024-01-02 4/week @ 2024-01-09 4/week @ 2024-01-16 14/week @ 2024-01-23 64/week @ 2024-01-30 4/week @ 2024-02-06 78/week @ 2024-02-13

161 downloads per month

MIT license

63KB
1K SLoC

Atoms

crates.io docs.rs Build Status

This repository is under heavy construction at the moment and will be making breaking changes until v2.0.0; do not use this crate until v2.0.0.

A small, simple, self-contained, s-expression parser and pretty-printer, forked from sexp.

Documentation

See the API Docs.


lib.rs:

A lightweight, self-contained s-expression parser and data format. Use parse to get an s-expression from its string representation, and the Display trait to serialize it, potentially by doing sexp.to_string().

Atoms is a basic S-expression parser. It parses strings and produces a tree of Cons-cells and atomic values of discrete type. Currently, only the following primitive types are available, and are represented with the Value enum:

  • Nil
  • Cons
  • Symbol
  • String
  • Int
  • Float

The trees are, as expected, formed by Cons cells.

Parsing

Parsing expressions requires a parser to be attached to the given string.

use atoms::{Parser, StringValue};
let text = "(this is a series of symbols)";
let parser = Parser::new(&text);
let parsed = parser.parse_basic().unwrap();
assert_eq!(
    StringValue::into_list(
        vec!["this", "is", "a", "series", "of", "symbols"], 
        |s| StringValue::symbol(s).unwrap()
    ),
    parsed
);

Custom parsing of symbols can be fairly easily defined allowing for restricted symbol sets with parsing errors. See Parser::parse for more.

Rendering

To render out an S-expression, simply use ToString or display it directly.

use atoms::StringValue;
let value = StringValue::cons(
    StringValue::symbol("this").unwrap(),
    StringValue::cons(
        StringValue::string("is"),
        StringValue::cons(
            StringValue::int(4),
            StringValue::symbol("s-expression").unwrap(),
        )
    )
);
assert_eq!(value.to_string(), "(this \"is\" 4 . s-expression)");

Dependencies