esdl/parser/
ident.rs

1use nom::{
2    branch::alt,
3    character::complete::{alphanumeric1, satisfy},
4    combinator::recognize,
5    multi::many0,
6    sequence::pair,
7};
8use nom_supreme::tag::complete::tag;
9
10use super::{IResult, Span};
11
12pub fn parse_camel_ident(input: Span) -> IResult<Span, Span> {
13    recognize(pair(
14        satisfy(|c: char| c.is_alphabetic() && c.is_uppercase()),
15        many0(alphanumeric1),
16    ))(input)
17}
18
19pub fn parse_snake_ident(input: Span) -> IResult<Span, Span> {
20    recognize(pair(
21        alt((
22            recognize(satisfy(|c| c.is_alphabetic() && c.is_lowercase())),
23            tag("_"),
24        )),
25        many0(alt((
26            recognize(satisfy(|c| {
27                if c.is_alphabetic() {
28                    c.is_lowercase()
29                } else {
30                    c.is_numeric()
31                }
32            })),
33            tag("_"),
34        ))),
35    ))(input)
36}
37
38// #[cfg(test)]
39// mod tests {
40//     use crate::Span;
41
42//     use super::{parse_camel_ident, parse_snake_ident};
43
44//     #[test]
45//     fn it_parses_snake_idents() {
46//         assert_eq!(parse_snake_ident("hello").unwrap(), ("", "hello"));
47//         assert_eq!(
48//             parse_snake_ident("hello_world").unwrap(),
49//             ("", "hello_world")
50//         );
51//         assert_eq!(parse_snake_ident("_hello").unwrap(), ("", "_hello"));
52//         assert_eq!(
53//             parse_snake_ident("_hello_world").unwrap(),
54//             ("", "_hello_world")
55//         );
56//         assert_eq!(parse_snake_ident("hello123").unwrap(), ("", "hello123"));
57//         assert_eq!(parse_snake_ident("hello_123").unwrap(), ("", "hello_123"));
58//         assert_eq!(parse_snake_ident("helloWorld").unwrap(), ("World", "hello"));
59//         assert_eq!(
60//             parse_snake_ident("hello_World").unwrap(),
61//             ("World", "hello_")
62//         );
63//         assert!(parse_snake_ident("Hello").is_err());
64//         assert!(parse_snake_ident("9Hey").is_err());
65//     }
66
67//     #[test]
68//     fn it_parses_camel_idents() {
69//         // Starts with capital letter
70//         assert_eq!(
71//             parse_camel_ident(Span::new("Hello")).unwrap(),
72//             (Span::new("Hello"), Span::new("Hello"))
73//         );
74
75//         // // Has all capitals
76//         // assert_eq!(parse_camel_ident("HelloWorld").unwrap(), ("", "HelloWorld"));
77
78//         // // Starts with lowercase
79//         // assert!(parse_camel_ident("hello").is_err());
80
81//         // // Starts with _
82//         // assert!(parse_camel_ident("_Hello").is_err());
83
84//         // // Does not contain _
85//         // assert_eq!(parse_camel_ident("Hello_").unwrap(), ("_", "Hello"));
86
87//         // // Starts with number
88//         // assert!(parse_camel_ident("9Hello").is_err());
89
90//         // // Contains number
91//         // assert_eq!(
92//         //     parse_camel_ident("Hello9World").unwrap(),
93//         //     ("", "Hello9World")
94//         // );
95//     }
96// }