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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
use py_lex::*;
use terl::*;
type Result<T> = terl::Result<T, terl::ParseError>;

mod expr;
mod flow;
mod item;
mod stmt;
mod types;

pub use expr::*;
pub use flow::*;
pub use item::*;
pub use stmt::*;
pub use types::*;

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

impl std::ops::Deref for Ident {
    type Target = str;

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

impl std::fmt::Display for Ident {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.0.fmt(f)
    }
}

impl ParseUnit<Token> for Ident {
    type Target = Ident;

    fn parse(p: &mut Parser<Token>) -> ParseResult<Self, Token> {
        let Some(token) = p.next() else {
            return p.unmatch("expect a `Ident`, but no token left");
        };

        if token.chars().next().is_some_and(|c| c.is_ascii_digit()) {
            return p.unmatch("bad ident! ident should not start with a digit");
        }

        use py_lex::*;

        let keeps = &[
            ops::KEPPING_KEYWORDS,
            ops::sub_classes::KEPPING_KEYWORDS,
            preprocess::KEPPING_KEYWORDS,
            syntax::KEPPING_KEYWORDS,
            types::KEPPING_KEYWORDS,
        ];

        for keeps in keeps {
            if keeps.with(|keeps| keeps.contains(&**token)) {
                return p.unmatch("keeping keywords could not be ident");
            }
        }

        // keeping keywords cant be used as identifiers
        Ok(Ident(token.string.clone()))
    }
}

/// use to define a complex parse unit which could be one of its variants
#[macro_export]
macro_rules! complex_pu {
    (
        $(#[$metas:meta])*
        cpu $enum_name:ident {
        $(
            $(#[$v_metas:meta])*
            $variant:ident
        ),*
    }) => {
        #[derive(Debug, Clone)]
        $(#[$metas])*
        pub enum $enum_name {
            $(
                $(#[$v_metas])*
                $variant($variant),
            )*
        }

        $(
        impl From<$variant> for $enum_name {
             fn from(v: $variant) -> $enum_name {
                <$enum_name>::$variant(v)
            }
        }
        )*


        impl terl::ParseUnit<py_lex::Token> for $enum_name {
            type Target = $enum_name;

            fn parse(p: &mut terl::Parser<py_lex::Token>) -> terl::ParseResult<Self, py_lex::Token>
            {
                terl::Try::<$enum_name, _>::new(p)
                $(
                .or_try::<Self, _>(|p| {
                    p.once_no_try::<$variant ,_>($variant::parse)
                        .map(<$enum_name>::$variant)
                })
                )*
                .finish()
            }
        }
    };
}

#[cfg(test)]
mod tests {
    use crate::parse_test;

    use super::*;

    #[test]
    fn good_ident() {
        parse_test("*)(&%^&*a(*&^%", |p| {
            assert!(&*p.parse::<Ident>()? == "a");
            Ok(())
        })
    }

    #[test]
    #[should_panic]
    fn bad_ident() {
        parse_test("1*)(&%^&*a(*&^%", |p| {
            p.parse::<Ident>()?;
            Ok(())
        })
    }
}