identifier/
identifier.rs

1//! A basic identifier parser using [parseme::xid].
2
3use parseme::Source;
4
5fn parse_iden<'a>(input: &mut Source<'a>) -> Result<&'a str, ()> {
6    let start_pos = input.pos();
7
8    parseme::iter::next_if(input, parseme::xid::is_start).ok_or(())?;
9    parseme::iter::advance_while(input, parseme::xid::is_continue);
10
11    Ok(&input.src()[start_pos..input.pos()])
12}
13
14fn main() {
15    let ident1 = "hello";
16    let ident2 = "hello123";
17
18    let bad1 = "123";
19    let bad2 = "_ident";
20
21    assert_eq!(parse_iden(&mut Source::new(ident1)), Ok("hello"));
22    assert_eq!(parse_iden(&mut Source::new(ident2)), Ok("hello123"));
23    assert_eq!(parse_iden(&mut Source::new(bad1)), Err(()));
24    assert_eq!(parse_iden(&mut Source::new(bad2)), Err(()));
25}