21 releases (4 stable)

2.2.0 Apr 23, 2021
2.1.1 Apr 23, 2021
0.3.6 Jan 22, 2021
0.3.3 Sep 2, 2020
0.1.7 Jul 29, 2020

#1631 in Asynchronous

Download history 14/week @ 2024-07-20 40/week @ 2024-07-27 12/week @ 2024-08-03 13/week @ 2024-08-10 12/week @ 2024-08-17 19/week @ 2024-08-24 41/week @ 2024-08-31 26/week @ 2024-09-07 30/week @ 2024-09-14 73/week @ 2024-09-21 21/week @ 2024-09-28 43/week @ 2024-10-05 31/week @ 2024-10-12 10/week @ 2024-10-19 16/week @ 2024-10-26 29/week @ 2024-11-02

100 downloads per month
Used in 5 crates (2 directly)

Custom license

20KB
438 lines

Charred

This crate provides a generic asynchronous lexer that operates on files with tokio. Tokens are parsed with provided async closures.

Usage

use crate::error::TapeResult;
use crate::input_reader::InputReader;
use crate::lexer::Lexer;
use crate::token::{Token, TokenCheckerFn};
use std::io::Cursor;
use std::sync::Arc;

struct NumberToken(i32);
struct StringToken(String);
struct WhiteSpaceToken;

async fn parse_number_token(reader: &mut InputReader) -> TapeResult<Option<Token>> {
    let mut num = String::new();
    while !reader.check_eof().await && reader.peek().await?.is_numeric() {
        num.push(reader.consume().await?);
    }
    if num.is_empty() {
        Ok(None)
    } else {
        Ok(Some(Token::new(NumberToken(num.parse::<i32>().unwrap()))))
    }
}

async fn parse_whitespace_token(reader: &mut InputReader) -> TapeResult<Option<Token>> {
    let mut count = 0;
    while !reader.check_eof().await && reader.peek().await?.is_whitespace() {
        reader.consume().await?;
        count += 1;
    }
    if count > 0 {
        Ok(Some(Token::new(WhiteSpaceToken)))
    } else {
        Ok(None)
    }
}

async fn parse_string_token(reader: &mut InputReader) -> TapeResult<Option<Token>> {
    let mut value = String::new();
    while !reader.check_eof().await
        && !reader.peek().await?.is_numeric()
        && !reader.peek().await?.is_whitespace()
    {
        value.push(reader.consume().await?);
    }
    if value.is_empty() {
        Ok(None)
    } else {
        Ok(Some(Token::new(StringToken(value))))
    }
}

#[tokio::main]
async fn main() {
    // functions that try to parse the token into an object
    let checkers: Vec<TokenCheckerFn> = vec![
        Arc::new(|reader| Box::pin(parse_number_token(reader))),
        Arc::new(|reader| Box::pin(parse_whitespace_token(reader))),
        Arc::new(|reader| Box::pin(parse_string_token(reader))),
    ];
    // input reader encapsulates (almost) any type that implements AsyncBufRead
    let input_reader = InputReader::new(Cursor::new("Word 12"));
    let mut lexer = Lexer::new(input_reader, checkers);

    // scan starts scanning the provided input
    let tokens = lexer.scan().await.unwrap();
    assert!(!tokens.is_empty());

    let mut tokens = tokens.into_iter();
    // use the is, try_as and try_into methods on the token type to get the underlying value
    assert!(tokens.next().unwrap().is::<StringToken>());
    assert!(tokens.next().unwrap().is::<WhiteSpaceToken>());
    assert!(tokens.next().unwrap().is::<NumberToken>());
}

License

Apache-2.0

Dependencies

~2.4–8.5MB
~68K SLoC