2 unstable releases

0.2.0 Apr 7, 2025
0.1.0 Apr 7, 2025

#6 in #text-parser

Download history 203/week @ 2025-04-05 30/week @ 2025-04-12 2/week @ 2025-04-19

235 downloads per month

Custom license

17KB
361 lines

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}"
);

Dependencies

~140KB