1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use faststr::FastStr;
use nom::{
    bytes::complete::take_while,
    character::complete::{char, satisfy},
    combinator::{map, recognize},
    multi::many0,
    sequence::tuple,
    IResult,
};

use std::ops::Deref;

use super::Parser;

#[derive(Debug, Clone)]
pub struct Ident(pub FastStr);

impl Deref for Ident {
    type Target = FastStr;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl Parser for Ident {
    fn parse(input: &str) -> IResult<&str, Ident> {
        map(
            recognize(tuple((
                many0(char('_')),
                satisfy(|c| c.is_ascii_alphabetic()),
                take_while(|c: char| c.is_ascii_alphanumeric() || c == '_'),
            ))),
            |ident: &str| -> Ident { Ident(ident.into()) },
        )(input)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_ident() {
        let input = "_Foo";
        match super::Ident::parse(input) {
            Ok((remain, ident)) => {
                assert_eq!(remain, "");
                assert_eq!(ident.0, "_Foo");
            }
            Err(e) => panic!("Error: {e:?}"),
        }
    }
}