clex/
keyword.rs

1/// Keyword token
2#[derive(Debug, Clone, Copy, PartialEq, Eq)]
3#[repr(u8)]
4pub enum Keyword {
5    /// auto
6    Auto,
7    /// break
8    Break,
9    /// case
10    Case,
11    /// char
12    Char,
13    /// const
14    Const,
15    /// continue
16    Continue,
17    /// default
18    Default,
19    /// do
20    Do,
21    /// double
22    Double,
23    /// else
24    Else,
25    /// enum
26    Enum,
27    /// extern
28    Extern,
29    /// float
30    Float,
31    /// for
32    For,
33    /// goto
34    Goto,
35    /// if
36    If,
37    /// inline
38    Inline,
39    /// int
40    Int,
41    /// long
42    Long,
43    /// register
44    Register,
45    /// restrict
46    Restrict,
47    /// return
48    Return,
49    /// short
50    Short,
51    /// signed
52    Signed,
53    /// sizeof
54    SizeOf,
55    /// static
56    Static,
57    /// struct
58    Struct,
59    /// switch
60    Switch,
61    /// typedef
62    TypeDef,
63    /// union
64    Union,
65    /// unsigned
66    Unsigned,
67    /// void
68    Void,
69    /// volatile
70    Volatile,
71    /// while
72    While,
73    /// _Alignas
74    AlignAs,
75    /// _Alignof
76    AlignOf,
77    /// _Atomic
78    Atomic,
79    /// _Bool
80    Bool,
81    /// _Complex
82    Complex,
83    /// _Generic
84    Generic,
85    /// _Imaginary
86    Imaginary,
87    /// _Noreturn
88    NoReturn,
89    /// _Static_assert
90    StaticAssert,
91    /// _Thread_local
92    ThreadLocal,
93    /// __func__
94    FuncName,
95}
96
97impl std::str::FromStr for Keyword {
98    type Err = ();
99
100    fn from_str(s: &str) -> Result<Self, Self::Err> {
101        KEYWORDS.get(s).copied().ok_or(())
102    }
103}
104
105static KEYWORDS: phf::Map<&'static str, Keyword> = phf::phf_map! {
106    "auto" => Keyword::Auto,
107    "break" => Keyword::Break,
108    "case" => Keyword::Case,
109    "char" => Keyword::Char,
110    "const" => Keyword::Const,
111    "continue" => Keyword::Continue,
112    "default" => Keyword::Default,
113    "do" => Keyword::Do,
114    "double" => Keyword::Double,
115    "else" => Keyword::Else,
116    "enum" => Keyword::Enum,
117    "extern" => Keyword::Extern,
118    "float" => Keyword::Float,
119    "for" => Keyword::For,
120    "goto" => Keyword::Goto,
121    "if" => Keyword::If,
122    "inline" => Keyword::Inline,
123    "int" => Keyword::Int,
124    "long" => Keyword::Long,
125    "register" => Keyword::Register,
126    "restrict" => Keyword::Restrict,
127    "return" => Keyword::Return,
128    "short" => Keyword::Short,
129    "signed" => Keyword::Signed,
130    "sizeof" => Keyword::SizeOf,
131    "static" => Keyword::Static,
132    "struct" => Keyword::Struct,
133    "switch" => Keyword::Switch,
134    "typedef" => Keyword::TypeDef,
135    "union" => Keyword::Union,
136    "unsigned" => Keyword::Unsigned,
137    "void" => Keyword::Void,
138    "volatile" => Keyword::Volatile,
139    "while" => Keyword::While,
140    "_Alignas" => Keyword::AlignAs,
141    "_Alignof" => Keyword::AlignOf,
142    "_Atomic" => Keyword::Atomic,
143    "_Bool" => Keyword::Bool,
144    "_Complex" => Keyword::Complex,
145    "_Generic" => Keyword::Generic,
146    "_Imaginary" => Keyword::Imaginary,
147    "_Noreturn" => Keyword::NoReturn,
148    "_Static_assert" => Keyword::StaticAssert,
149    "_Thread_local" => Keyword::ThreadLocal,
150    "__func__" => Keyword::FuncName,
151};
152
153#[cfg(test)]
154mod test {
155    use super::*;
156
157    #[test]
158    fn keyword() {
159        assert_eq!("auto".parse::<Keyword>(), Ok(Keyword::Auto));
160        assert_eq!("extern".parse::<Keyword>(), Ok(Keyword::Extern));
161        assert_eq!("do".parse::<Keyword>(), Ok(Keyword::Do));
162        assert_eq!("__func__".parse::<Keyword>(), Ok(Keyword::FuncName));
163    }
164
165    #[test]
166    fn not_a_keyword() {
167        assert_eq!("123".parse::<Keyword>(), Err(()));
168        assert_eq!("done".parse::<Keyword>(), Err(()));
169    }
170}