2 unstable releases

0.2.0 Aug 5, 2025
0.1.0 Jan 25, 2023

#426 in Programming languages

Download history 27/week @ 2025-12-03

64 downloads per month

MIT license

18KB
167 lines

S-Expression Parser

A high-performance, zero-copy S-expression parser optimized for compiler use cases. Features fast parsing with minimal memory allocations through borrowed string slices and various performance optimizations.

Features

  • Zero-copy parsing: Uses borrowed string slices to avoid unnecessary allocations
  • Fast-path optimizations: Optimized number parsing and single-character symbols
  • Production error handling: Proper error types instead of panics
  • Memory efficient: Pre-allocated vectors and optimized tokenization

Quick Start

use sexpression::{Expression, read, ParseError};

fn main() -> Result<(), ParseError> {
    let source = "(define (factorial n) (if (= n 0) 1 (* n (factorial (- n 1)))))";
    let expr = read(source)?;
    println!("Parsed: {:?}", expr);
    Ok(())
}

Documentation

Run cargo doc --open to view the full documentation.


lib.rs:

S-Expression Parser Library

A high-performance, zero-copy S-expression parser optimized for compiler use cases. This library provides fast parsing with minimal memory allocations through borrowed string slices and various performance optimizations.

Features

  • Zero-copy parsing: Uses borrowed string slices to avoid unnecessary allocations
  • Fast-path optimizations: Optimized number parsing and single-character symbols
  • Production error handling: Proper error types instead of panics
  • Memory efficient: Pre-allocated vectors and optimized tokenization
  • Compiler-friendly: Designed for use in language compilers and interpreters

Quick Start

use sexpression::{Expression, read, ParseError};

fn main() -> Result<(), ParseError> {
    let source = "(define (factorial n) (if (= n 0) 1 (* n (factorial (- n 1)))))";
    let expr = read(source)?;
    println!("Parsed: {:?}", expr);
    Ok(())
}

API Overview

Core Types

Main Functions

  • read: Primary parsing function with error handling
  • read_unchecked: Convenience function that panics on error

Performance

The parser is optimized for:

  • Memory efficiency: Zero-copy parsing with borrowed slices
  • Speed: Fast-path checks and pre-allocated vectors
  • Compiler workloads: Designed for parsing large amounts of code

Examples

Basic Parsing

use sexpression::{read, Expression};

let result = read("(define x 42)");
match result {
    Ok(expr) => println!("Success: {:?}", expr),
    Err(e) => eprintln!("Error: {}", e),
}

Error Handling

use sexpression::{read, ParseError};

let result = read("(unclosed");
assert!(matches!(result, Err(ParseError::MissingClosingParen)));

Converting to Owned

use sexpression::{Expression, OwnedExpression};

let borrowed = Expression::Symbol("hello");
let owned = borrowed.to_owned();
assert_eq!(owned, OwnedExpression::Symbol("hello".to_string()));

Dependencies

~160–560KB
~13K SLoC