Retoken
Build &str tokenizers using regex
Tokenizer Macro
You can create tokenizers using a simple macro
Example: Foo Lang
Foo lang is simple language it only allows you to assign variables to literal strings and have whitespace
my_var = "my string"
my_other_var = "my other string"
use retoken::{tokenize::Tokenize, tokenizer};
tokenizer! {
#[skip]
Skip = r#"\s+"#,
Ident = "[a-zA-Z_]+",
Equals = r#"="#,
#[no_variant]
Quote = r#"""#,
#[no_impl]
QuotedString,
}
impl<'a> Tokenize<'a> for QuotedString<'a> {
fn token_name() -> &'static str {
"QuotedString"
}
fn tokenize(str_src: &'a retoken::str_src::StrSrc<'a>) -> Result<Self, retoken::error::Error> {
let Quote {
idx: start,
value: _,
} = Quote::tokenize(str_src)?;
let end = loop {
let incr = str_src
.slice()?
.chars()
.nth(0)
.map(|el| el.len_utf8())
.unwrap_or_default();
str_src.incr(incr);
if let Ok(ok) = Quote::tokenize(&str_src) {
break ok.idx;
}
};
let value = str_src.slice_with_range((start + 1)..end)?;
Ok(Self { idx: start, value })
}
}