Crates.io | simple-tokenizer |
lib.rs | simple-tokenizer |
version | 0.4.2 |
source | src |
created_at | 2023-12-17 16:02:03.239625 |
updated_at | 2024-06-27 06:19:00.268812 |
description | A tiny no_std tokenizer with line & column tracking. |
homepage | |
repository | https://github.com/Solar-Falcon/simple-tokenizer |
max_upload_size | |
id | 1072588 |
size | 25,351 |
A tiny no_std tokenizer with line & column tracking.
Goals:
no_std
, no allocations and zero/minimal dependencies.This isn't a parser combinator library and it won't ever become one. There are plenty of other choices already.
use simple_tokenizer::*;
// A small parser that would parse function calls with number/ident arguments.
let source = r"function(123, other_argument, 456)";
let mut tokens = source.as_tokens(); // AsTokens is implemented for any AsRef<str>
let fn_name = tokens.take_while(|ch| ch.is_ascii_alphabetic() || ch == '_').to_string();
// Empty => there was nothing matching a function name
if fn_name.is_empty() {
// use better error handling youself
panic!("error at {}", tokens.position());
}
tokens.take_while(char::is_whitespace); // skip whitespace
let mut args = Vec::new();
if !tokens.token("(") {
panic!("error at {}", tokens.position());
}
// if the call succeeded, than '(' is consumed
tokens.take_while(char::is_whitespace); // skip whitespace
// for the sake of simplicity, I'm gonna stop checking for empty strings
args.push(tokens.take_while(|ch| ch.is_ascii_alphanumeric() || ch == '_').to_string());
tokens.take_while(char::is_whitespace); // skip whitespace
while tokens.token(",") {
tokens.take_while(char::is_whitespace); // skip whitespace
args.push(tokens.take_while(|ch| ch.is_ascii_alphanumeric() || ch == '_').to_string());
tokens.take_while(char::is_whitespace); // skip whitespace
}
if !tokens.token(")") {
panic!("error at {}", tokens.position());
}
assert!(tokens.is_at_end());
assert_eq!(fn_name, "function");
assert_eq!(args.as_slice(), &["123", "other_argument", "456"]);
yap
(off by default): adds a wrapper for Tokens<'_>
that implements yap::Tokens
.