24 lines
719 B
Rust
24 lines
719 B
Rust
use num::Num;
|
|
use yap::{IntoTokens, Tokens};
|
|
pub fn space(tokens: &mut impl Tokens<Item = char>) -> bool {
|
|
tokens.token(' ')
|
|
}
|
|
|
|
pub fn newline(tokens: &mut impl Tokens<Item = char>) -> bool {
|
|
tokens.tokens("\r\n".into_tokens()) || tokens.token('\n')
|
|
}
|
|
|
|
pub fn parse_number<T: Num>(tokens: &mut impl Tokens<Item = char>) -> Option<T> {
|
|
parse_number_with_radix(tokens, 10)
|
|
}
|
|
|
|
pub fn parse_number_with_radix<T: Num>(tokens: &mut impl Tokens<Item = char>, base: u32) -> Option<T> {
|
|
tokens.skip_tokens_while(|t| *t == ' ');
|
|
let digits: String = tokens.tokens_while(|c| c.is_digit(base)).collect();
|
|
if digits.len() > 0 {
|
|
T::from_str_radix(&digits, base).ok()
|
|
} else {
|
|
None
|
|
}
|
|
}
|