use nom::{bytes::complete::take_while1, combinator::{opt, recognize}, AsChar, IResult, Parser};
use crate::parser::doc::StofParseError;
pub fn ident(input: &str) -> IResult<&str, &str, StofParseError> {
recognize(
(
take_while1(|c| AsChar::is_alpha(c) || c == '_' || c == '@' || c == '<'),
opt(take_while1(|c| AsChar::is_alphanum(c) || c == '_' || c == '@' || c == '-' || c == '<' || c == '>'))
)
).parse(input)
}
pub fn ident_type(input: &str) -> IResult<&str, &str, StofParseError> {
recognize(
(
take_while1(|c| AsChar::is_alpha(c) || c == '_' || c == '@'),
opt(take_while1(|c| AsChar::is_alphanum(c) || c == '_' || c == '@' || c == '-'))
)
).parse(input)
}
#[cfg(test)]
mod tests {
use crate::parser::ident::ident;
#[test]
fn ident_parse() {
assert_eq!(ident("a").unwrap().1, "a");
assert_eq!(ident("a1345: str").unwrap().1, "a1345");
assert!(ident("1").is_err());
}
#[test]
fn start_underscore() {
assert_eq!(ident("_a").unwrap().1, "_a");
assert_eq!(ident("__a1345: str").unwrap().1, "__a1345");
}
#[test]
fn start_at() {
assert_eq!(ident("@a").unwrap().1, "@a");
assert_eq!(ident("@a13@45: str").unwrap().1, "@a13@45");
}
}