1#[derive(Debug, Clone, Copy, PartialEq, Eq)]
3#[repr(u8)]
4pub enum Keyword {
5 Auto,
7 Break,
9 Case,
11 Char,
13 Const,
15 Continue,
17 Default,
19 Do,
21 Double,
23 Else,
25 Enum,
27 Extern,
29 Float,
31 For,
33 Goto,
35 If,
37 Inline,
39 Int,
41 Long,
43 Register,
45 Restrict,
47 Return,
49 Short,
51 Signed,
53 SizeOf,
55 Static,
57 Struct,
59 Switch,
61 TypeDef,
63 Union,
65 Unsigned,
67 Void,
69 Volatile,
71 While,
73 AlignAs,
75 AlignOf,
77 Atomic,
79 Bool,
81 Complex,
83 Generic,
85 Imaginary,
87 NoReturn,
89 StaticAssert,
91 ThreadLocal,
93 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}