wood_parse 0.2.0

A library for lexing
Documentation
  • Coverage
  • 65%
    13 out of 20 items documented0 out of 0 items with examples
  • Size
  • Source code size: 17.48 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 3.12 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 13s Average build duration of successful builds.
  • all releases: 12s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • WoodieMaster/wood_parse
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • WoodieMaster

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