Skip to main content

mirascript_core/lexer/
keyword.rs

1use 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    // constants
20    #[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    // pseudo variable
32    #[strum(to_string = "_")]
33    Underscore,
34    Global,
35
36    // operators
37    In,
38    #[strum(to_string = "not in")]
39    NotIn,
40    Is,
41    And,
42    Or,
43    Not,
44
45    // pseudo function
46    Type,
47
48    // control flow
49    #[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    // declaration
71    Fn,
72    #[strum(props(reserved = true))]
73    Op,
74    Let,
75    Const,
76    Mut,
77    // #[strum(props(reserved = true))]
78    // Type,
79    #[strum(props(reserved = true))]
80    Where,
81
82    // type Date(string) {
83    //   if it::matches(`(\d{4})-(\d{2})-(\d{2})`) { it }
84    //   else if it::matches(`(\d{4})/(\d{2})/(\d{2})`) { it::replace("/", "-") }
85    //   else { nil }
86    // }
87    // type Complex(number, number);
88    // type Color(red: number, green: number, blue: number) { red > 0 && red < 256 && green > 0 && green < 256 && blue > 0 && blue < 256 }
89    // Date("2021-01-01") -> "2021-01-01"
90    // Complex(1, 2) -> { 0: 1, 1: 2 }
91    // Color(1, 2, 3) -> { red: 1, green: 2, blue: 3 }
92    // Color(-1, 2, 3) -> nil
93
94    // module
95    // mod x {
96    //   pub fn foo() { }
97    //   pub const @VAL = 42;
98    //   pub let mut counter = 0;
99    //   pub mod inner {}
100    // }
101    // x.foo();
102    Mod,
103    Pub,
104    #[strum(props(reserved = true))]
105    Use,
106
107    // algebraic effects
108    #[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    /// Returns `true` if the keyword is a control flow keyword.
124    pub fn is_control(&self) -> bool {
125        self.get_bool("control").unwrap_or(false)
126    }
127
128    /// Returns `true` if the keyword is a constant.
129    pub fn is_constant(&self) -> bool {
130        self.get_bool("constant").unwrap_or(false)
131    }
132
133    /// Returns `true` if the keyword is reserved and cannot be used as an identifier.
134    pub fn is_reserved(&self) -> bool {
135        self.get_bool("reserved").unwrap_or(false)
136    }
137
138    /// Returns `true` if the keyword is numeric.
139    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}