wood_parse

Crates.iowood_parse
lib.rswood_parse
version0.2.0
created_at2025-04-07 14:17:06.234136+00
updated_at2025-04-07 14:28:01.982345+00
descriptionA library for lexing
homepage
repositoryhttps://github.com/WoodieMaster/wood_parse
max_upload_size
id1624201
size17,480
WoodMaster (WoodieMaster)

documentation

https://docs.rs/wood_parse/latest/

README

Lexer Package

This crate provides some methods for parsing text.

I mostly developed it for a custom language I want to build.

Features

This crate exposes two main structs:

  • TextParser: The base parser that loads the text from a Read trait.
  • Peeker: Allows you to read ahead without consuming the previous characters until you consume all read characters.

Usage

To use this package, add the the crate to your Cargo.toml file or run cargo add wood-parse

Then, import into your Rust code:

use wood_parse::{text_parser::TextParser, util::TextParserResult};

Example

This code will remove all whitespace from a file:

let input = "a   b   c";
let expected = "abc";

// create the parser and get the peeker
let mut parser = TextParser::new(input.as_bytes());
let mut peeker = parser.peeker();

let mut parsed_string = String::new();

// Loop until the end is hit or an error occurs
loop {
    // skip whitespace
    let _ = peeker.consume_while(|ch: char| ch.is_whitespace());

    // get the next character
    let (result, _) = peeker.next();
    match result {
        TextParserResult::Ok(ch) => parsed_string.push(ch),
        TextParserResult::End => break,
        _ => {}
    }
}

//compare results
assert!(
    parsed_string == expected,
    "Expected {expected}, got {parsed_string}"
);
Commit count: 17

cargo fmt