cursortanium 0.1.0

A solid cursor for Rust
Documentation
  • Coverage
  • 0%
    0 out of 24 items documented0 out of 20 items with examples
  • Size
  • Source code size: 12.3 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 432.78 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 9s Average build duration of successful builds.
  • all releases: 9s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Homepage
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • clitetailor

Why cursortanium?

While regexp and PEGs are declarative and elegant, it can be hard control when coming to much more complex problems.

cursortanium is a general purpose parser. It provides imperative and programmatic interface to interact with the input document. Which means that you can customize and run the parser your own way without relying on any additional syntax or grammar. This makes cursortanium much more powerful in comparison with prior libraries.

Example

The following function takes an input cursor then extracts the string value, which may contain escape sequences, from the document and returns the corresponding value:

fn parse_string(cursor: &mut Cursor) -> Option<String> {
    if cursor.starts_with("\"") {
        cursor.next_mut(1);
    } else {
        return None;
    };

    let mut chunks = vec![];
    let mut marker = cursor.clone();

    while !cursor.starts_with("\"") && !cursor.is_eof() {
        if cursor.starts_with("\\") {
            chunks.push(marker.take_until(&cursor));
            cursor.next_mut(1);
            marker = cursor.clone();
            cursor.next_mut(1);
        } else {
            cursor.next_mut(1);
        };
    };

    chunks.push(marker.take_until(&cursor));

    if cursor.starts_with("\"") {
        cursor.next_mut(1);
    } else {
        return None;
    };

    Some(chunks.concat())
}

To use this function, we will create a cursor at the start of the string and passing it as mutable parameter:

use cursortanium::Cursor;

fn main() {
    let mut cursor = Cursor::from(String::from(r#""Hello, \"World\"!"#));

    if let Some(value) = parse_string(&mut cursor) {
        println!("{}", cursor.is_eof())
        println!("{}", value);
    };
}

Run the above code with cargo run will give the following output:

$ cargo run

true

Hello, "World"!

License

MIT License