mirascript_core/lexer/
keyword.rs1use strum::{Display, EnumProperty, EnumString, IntoStaticStr, VariantArray};
2
3use super::prelude::*;
4
5#[derive(
6 Debug,
7 Clone,
8 Copy,
9 PartialEq,
10 Eq,
11 EnumString,
12 Display,
13 VariantArray,
14 EnumProperty,
15 IntoStaticStr,
16)]
17#[strum(serialize_all = "snake_case")]
18pub enum Keyword {
19 #[strum(props(constant = true))]
21 True,
22 #[strum(props(constant = true))]
23 False,
24 #[strum(props(constant = true))]
25 Nil,
26 #[strum(props(constant = true, numeric = true))]
27 Nan,
28 #[strum(props(constant = true, numeric = true))]
29 Inf,
30
31 #[strum(to_string = "_")]
33 Underscore,
34 Global,
35
36 In,
38 #[strum(to_string = "not in")]
39 NotIn,
40 Is,
41 And,
42 Or,
43 Not,
44
45 Type,
47
48 #[strum(props(control = true))]
50 If,
51 #[strum(props(control = true))]
52 Else,
53 #[strum(props(control = true))]
54 Match,
55 #[strum(props(control = true))]
56 Case,
57 #[strum(props(control = true))]
58 For,
59 #[strum(props(control = true))]
60 While,
61 #[strum(props(control = true))]
62 Loop,
63 #[strum(props(control = true))]
64 Break,
65 #[strum(props(control = true))]
66 Continue,
67 #[strum(props(control = true))]
68 Return,
69
70 Fn,
72 #[strum(props(reserved = true))]
73 Op,
74 Let,
75 Const,
76 Mut,
77 #[strum(props(reserved = true))]
80 Where,
81
82 Mod,
103 Pub,
104 #[strum(props(reserved = true))]
105 Use,
106
107 #[strum(props(reserved = true))]
109 Effect,
110 #[strum(props(reserved = true))]
111 Try,
112 #[strum(props(reserved = true))]
113 Handle,
114 #[strum(props(reserved = true))]
115 Finally,
116 #[strum(props(reserved = true))]
117 Perform,
118 #[strum(props(reserved = true))]
119 Resume,
120}
121
122impl Keyword {
123 pub fn is_control(&self) -> bool {
125 self.get_bool("control").unwrap_or(false)
126 }
127
128 pub fn is_constant(&self) -> bool {
130 self.get_bool("constant").unwrap_or(false)
131 }
132
133 pub fn is_reserved(&self) -> bool {
135 self.get_bool("reserved").unwrap_or(false)
136 }
137
138 pub fn is_numeric(&self) -> bool {
140 self.get_bool("numeric").unwrap_or(false)
141 }
142}
143
144impl From<Keyword> for TokenKind<'_> {
145 fn from(kw: Keyword) -> Self {
146 TokenKind::Keyword(kw)
147 }
148}
149
150impl PartialEq<Token<'_>> for Keyword {
151 fn eq(&self, other: &Token<'_>) -> bool {
152 matches!(&other.kind, TokenKind::Keyword(kw) if kw == self)
153 }
154}
155
156impl PartialEq<Keyword> for Token<'_> {
157 fn eq(&self, other: &Keyword) -> bool {
158 matches!(&self.kind, TokenKind::Keyword(kw) if kw == other)
159 }
160}
161
162impl PartialEq<TokenKind<'_>> for Keyword {
163 fn eq(&self, other: &TokenKind<'_>) -> bool {
164 matches!(other, TokenKind::Keyword(kw) if kw == self)
165 }
166}
167
168impl PartialEq<Keyword> for TokenKind<'_> {
169 fn eq(&self, other: &Keyword) -> bool {
170 matches!(self, TokenKind::Keyword(kw) if kw == other)
171 }
172}
173
174#[test]
175fn test_keyword() {
176 use std::str::FromStr;
177
178 assert_eq!(Keyword::from_str("true"), Ok(Keyword::True));
179 assert_eq!(Keyword::from_str("false"), Ok(Keyword::False));
180 assert_eq!(Keyword::from_str("nil"), Ok(Keyword::Nil));
181 assert_eq!(Keyword::from_str("nan"), Ok(Keyword::Nan));
182 assert_eq!(Keyword::from_str("inf"), Ok(Keyword::Inf));
183
184 assert_eq!(Keyword::from_str("_"), Ok(Keyword::Underscore));
185 assert_eq!(Keyword::from_str("global"), Ok(Keyword::Global));
186
187 assert_eq!(Keyword::from_str("in"), Ok(Keyword::In));
188 assert_eq!(Keyword::from_str("not in"), Ok(Keyword::NotIn));
189 assert_eq!(Keyword::from_str("is"), Ok(Keyword::Is));
190
191 assert_eq!(
192 Keyword::from_str("Is"),
193 Err(strum::ParseError::VariantNotFound)
194 );
195 assert_eq!(
196 Keyword::from_str("Underscore"),
197 Err(strum::ParseError::VariantNotFound)
198 );
199 assert_eq!(
200 Keyword::from_str("underscore"),
201 Err(strum::ParseError::VariantNotFound)
202 );
203}