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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
use std::str::FromStr;


pub const KEYWORD_ASYNC: &[char]      = &['a', 's', 'y', 'n', 'c'];
pub const KEYWORD_AWAIT: &[char]      = &['a', 'w', 'a', 'i', 't'];
pub const KEYWORD_BREAK: &[char]      = &['b', 'r', 'e', 'a', 'k'];
pub const KEYWORD_CASE: &[char]       = &['c', 'a', 's', 'e'];
pub const KEYWORD_CATCH: &[char]      = &['c', 'a', 't', 'c', 'h'];
pub const KEYWORD_CLASS: &[char]      = &['c', 'l', 'a', 's', 's'];
pub const KEYWORD_CONST: &[char]      = &['c', 'o', 'n', 's', 't'];
pub const KEYWORD_CONTINUE: &[char]   = &['c', 'o', 'n', 't', 'i', 'n', 'u', 'e'];
pub const KEYWORD_DEBUGGER: &[char]   = &['d', 'e', 'b', 'u', 'g', 'g', 'e', 'r'];
pub const KEYWORD_DEFAULT: &[char]    = &['d', 'e', 'f', 'a', 'u', 'l', 't'];
pub const KEYWORD_DELETE: &[char]     = &['d', 'e', 'l', 'e', 't', 'e'];
pub const KEYWORD_DO: &[char]         = &['d', 'o'];
pub const KEYWORD_ELSE: &[char]       = &['e', 'l', 's', 'e'];
pub const KEYWORD_EXPORT: &[char]     = &['e', 'x', 'p', 'o', 'r', 't'];
pub const KEYWORD_EXTENDS: &[char]    = &['e', 'x', 't', 'e', 'n', 'd', 's'];
pub const KEYWORD_FINALLY: &[char]    = &['f', 'i', 'n', 'a', 'l', 'l', 'y'];
pub const KEYWORD_FOR: &[char]        = &['f', 'o', 'r'];
pub const KEYWORD_FUNCTION: &[char]   = &['f', 'u', 'n', 'c', 't', 'i', 'o', 'n'];
pub const KEYWORD_IF: &[char]         = &['i', 'f'];
pub const KEYWORD_IMPORT: &[char]     = &['i', 'm', 'p', 'o', 'r', 't'];
pub const KEYWORD_IN: &[char]         = &['i', 'n'];
pub const KEYWORD_INSTANCEOF: &[char] = &['i', 'n', 's', 't', 'a', 'n', 'c', 'e', 'o', 'f'];
pub const KEYWORD_NEW: &[char]        = &['n', 'e', 'w'];
pub const KEYWORD_RETURN: &[char]     = &['r', 'e', 't', 'u', 'r', 'n'];
pub const KEYWORD_SUPER: &[char]      = &['s', 'u', 'p', 'e', 'r'];
pub const KEYWORD_SWITCH: &[char]     = &['s', 'w', 'i', 't', 'c', 'h'];
pub const KEYWORD_THIS: &[char]       = &['t', 'h', 'i', 's'];
pub const KEYWORD_THROW: &[char]      = &['t', 'h', 'r', 'o', 'w'];
pub const KEYWORD_TRY: &[char]        = &['t', 'r', 'y'];
pub const KEYWORD_TYPEOF: &[char]     = &['t', 'y', 'p', 'e', 'o', 'f'];
pub const KEYWORD_VAR: &[char]        = &['v', 'a', 'r'];
pub const KEYWORD_VOID: &[char]       = &['v', 'o', 'i', 'd'];
pub const KEYWORD_WHILE: &[char]      = &['w', 'h', 'i', 'l', 'e'];
pub const KEYWORD_WITH: &[char]       = &['w', 'i', 't', 'h'];
pub const KEYWORD_YIELD: &[char]      = &['y', 'i', 'e', 'l', 'd'];
pub const KEYWORD_LET: &[char]        = &['l', 'e', 't'];
pub const KEYWORD_STATIC: &[char]     = &['s', 't', 'a', 't', 'i', 'c'];
pub const KEYWORD_ENUM: &[char]       = &['e', 'n', 'u', 'm'];
pub const KEYWORD_IMPLEMENTS: &[char] = &['i', 'm', 'p', 'l', 'e', 'm', 'e', 'n', 't', 's'];
pub const KEYWORD_PROTECTED: &[char]  = &['p', 'r', 'o', 't', 'e', 'c', 't', 'e', 'd'];
pub const KEYWORD_INTERFACE: &[char]  = &['i', 'n', 't', 'e', 'r', 'f', 'a', 'c', 'e'];
pub const KEYWORD_PRIVATE: &[char]    = &['p', 'r', 'i', 'v', 'a', 't', 'e'];
pub const KEYWORD_PUBLIC: &[char]     = &['p', 'u', 'b', 'l', 'i', 'c'];
pub const KEYWORD_PACKAGE: &[char]    = &['p', 'a', 'c', 'k', 'a', 'g', 'e'];


// https://www.ecma-international.org/ecma-262/9.0/index.html#sec-keywords
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
pub enum KeywordKind {
    Async,
    Await,
    Break,
    Case,
    Catch,
    Class,
    Const,
    Continue,
    Debugger,
    Default,
    Delete,
    Do,
    Else,
    Export,
    Extends,
    Finally,
    For,
    Function,
    If,
    Import,
    In,
    InstanceOf,
    New,
    Return,
    Super,
    Switch,
    This,
    Throw,
    Try,
    TypeOf,
    Var,
    Void,
    While,
    With,
    Yield,

    Let,
    Static,

    // FutureReservedWord
    // https://www.ecma-international.org/ecma-262/9.0/index.html#sec-future-reserved-words
    Enum,
    // Use of the following tokens within strict mode code is also reserved
    Implements,
    Package,
    Protected,
    Interface,
    Private,
    Public,
}

impl KeywordKind {
    pub fn is_future_reserved(&self) -> bool {
        // https://www.ecma-international.org/ecma-262/9.0/index.html#prod-FutureReservedWord
        use self::KeywordKind::*;

        match *self {
            Enum => true,
            Implements
            | Package
            | Protected
            | Interface
            | Private
            | Public => true,
            _ => false,
        }
    }

    pub fn try_from(value: &[char]) -> Result<Self, ()> {
        use self::KeywordKind::*;

        match value {
            KEYWORD_ASYNC => Ok(Async),
            KEYWORD_AWAIT => Ok(Await),
            KEYWORD_BREAK => Ok(Break),
            KEYWORD_CASE => Ok(Case),
            KEYWORD_CATCH => Ok(Catch),
            KEYWORD_CLASS => Ok(Class),
            KEYWORD_CONST => Ok(Const),
            KEYWORD_CONTINUE => Ok(Continue),
            KEYWORD_DEBUGGER => Ok(Debugger),
            KEYWORD_DEFAULT => Ok(Default),
            KEYWORD_DELETE => Ok(Delete),
            KEYWORD_DO => Ok(Do),
            KEYWORD_ELSE => Ok(Else),
            KEYWORD_EXPORT => Ok(Export),
            KEYWORD_EXTENDS => Ok(Extends),
            KEYWORD_FINALLY => Ok(Finally),
            KEYWORD_FOR => Ok(For),
            KEYWORD_FUNCTION => Ok(Function),
            KEYWORD_IF => Ok(If),
            KEYWORD_IMPORT => Ok(Import),
            KEYWORD_IN => Ok(In),
            KEYWORD_INSTANCEOF => Ok(InstanceOf),
            KEYWORD_NEW => Ok(New),
            KEYWORD_RETURN => Ok(Return),
            KEYWORD_SUPER => Ok(Super),
            KEYWORD_SWITCH => Ok(Switch),
            KEYWORD_THIS => Ok(This),
            KEYWORD_THROW => Ok(Throw),
            KEYWORD_TRY => Ok(Try),
            KEYWORD_TYPEOF => Ok(TypeOf),
            KEYWORD_VAR => Ok(Var),
            KEYWORD_VOID => Ok(Void),
            KEYWORD_WHILE => Ok(While),
            KEYWORD_WITH => Ok(With),
            KEYWORD_YIELD => Ok(Yield),
            KEYWORD_LET => Ok(Let),
            KEYWORD_STATIC => Ok(Static),
            KEYWORD_ENUM => Ok(Enum),
            KEYWORD_IMPLEMENTS => Ok(Implements),
            KEYWORD_PACKAGE => Ok(Package),
            KEYWORD_PROTECTED => Ok(Protected),
            KEYWORD_INTERFACE => Ok(Interface),
            KEYWORD_PRIVATE => Ok(Private),
            KEYWORD_PUBLIC => Ok(Public),
            _ => Err(()),
        }
    }
}

impl FromStr for KeywordKind {
    type Err = ();

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        use self::KeywordKind::*;

        match s {
            "async" => Ok(Async),
            "await" => Ok(Await),
            "break" => Ok(Break),
            "case" => Ok(Case),
            "catch" => Ok(Catch),
            "class" => Ok(Class),
            "const" => Ok(Const),
            "continue" => Ok(Continue),
            "debugger" => Ok(Debugger),
            "default" => Ok(Default),
            "delete" => Ok(Delete),
            "do" => Ok(Do),
            "else" => Ok(Else),
            "export" => Ok(Export),
            "extends" => Ok(Extends),
            "finally" => Ok(Finally),
            "for" => Ok(For),
            "function" => Ok(Function),
            "if" => Ok(If),
            "import" => Ok(Import),
            "in" => Ok(In),
            "instanceof" => Ok(InstanceOf),
            "new" => Ok(New),
            "return" => Ok(Return),
            "super" => Ok(Super),
            "switch" => Ok(Switch),
            "this" => Ok(This),
            "throw" => Ok(Throw),
            "try" => Ok(Try),
            "typeof" => Ok(TypeOf),
            "var" => Ok(Var),
            "void" => Ok(Void),
            "while" => Ok(While),
            "with" => Ok(With),
            "yield" => Ok(Yield),
            "let" => Ok(Let),
            "static" => Ok(Static),
            "enum" => Ok(Enum),
            "implements" => Ok(Implements),
            "package" => Ok(Package),
            "protected" => Ok(Protected),
            "interface" => Ok(Interface),
            "private" => Ok(Private),
            "public" => Ok(Public),
            _ => Err(())
        }
    }
}