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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
use crate::tokens::{CommentKind, Keyword, NumberKind, Punct};

#[derive(PartialEq, Debug, Clone, Copy)]
pub enum RawToken {
    /// `true` of `false`
    Boolean(bool),
    /// The end of the file
    EoF,
    /// An identifier this will be either a variable name
    /// or a function/method name
    Ident,
    /// A word that has been reserved to not be used as an identifier
    Keyword(RawKeyword),
    /// A `null` literal value
    Null,
    /// A number, this includes integers (`1`), decimals (`0.1`),
    /// hex (`0x8f`), binary (`0b010011010`), and octal (`0o273`)
    Number(NumberKind),
    /// A punctuation mark, this includes all mathematical operators
    /// logical operators and general syntax punctuation
    Punct(Punct),
    /// A string literal, either double or single quoted, the associated
    /// value will be the unquoted string
    String {
        kind: StringKind,
        new_line_count: usize,
        last_len: usize,
        found_octal_escape: bool,
    },
    /// A regular expression literal.
    /// ```js
    /// let regex = /[a-zA-Z]+/g;
    /// ```
    RegEx(usize),
    /// The string parts of a template string
    /// ```js
    ///    `things and stuff times ${10}`
    /// //  ^^^^^^^^^^^^^^^^^^^^^^      ^
    /// ```
    Template {
        kind: TemplateKind,
        new_line_count: usize,
        last_len: usize,
        has_octal_escape: bool,
        found_invalid_unicode_escape: bool,
        found_invalid_hex_escape: bool,
    },
    /// A comment, the associated value will contain the raw comment
    /// This will capture both inline comments `// I am an inline comment`
    /// and multi-line comments
    /// ```js
    /// /*multi lines
    /// * comments
    /// */
    /// ```
    Comment {
        kind: CommentKind,
        new_line_count: usize,
        last_len: usize,
        end_index: usize,
    },
}

impl Copy for Keyword<()> {}

impl RawToken {
    pub fn is_punct(&self) -> bool {
        if let RawToken::Punct(_) = self {
            true
        } else {
            false
        }
    }

    pub fn is_comment(&self) -> bool {
        if let RawToken::Comment { .. } = self {
            true
        } else {
            false
        }
    }
    pub fn is_div_punct(&self) -> bool {
        if let RawToken::Punct(ref p) = self {
            match p {
                Punct::ForwardSlash => true,
                Punct::ForwardSlashEqual => true,
                _ => false,
            }
        } else {
            false
        }
    }
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub enum StringKind {
    Double,
    Single,
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub enum TemplateKind {
    NoSub,
    Head,
    Body,
    Tail,
}
#[derive(PartialEq, Debug, Clone, Copy)]
pub enum RawKeyword {
    Await,
    Break,
    Case,
    Catch,
    Class,
    Const,
    Continue,
    Debugger,
    Default,
    Delete,
    Do,
    Else,
    Enum,
    Export,
    Extends,
    Finally,
    For,
    Function,
    If,
    Implements,
    Import,
    In,
    InstanceOf,
    Interface,
    Let,
    New,
    Package,
    Private,
    Protected,
    Public,
    Return,
    Static,
    Super,
    Switch,
    This,
    Throw,
    Try,
    TypeOf,
    Var,
    Void,
    While,
    With,
    Yield,
}

impl RawKeyword {
    pub fn with_str(self, s: &str) -> crate::tokens::Keyword<&str> {
        match self {
            RawKeyword::Await => Keyword::Await(s),
            RawKeyword::Break => Keyword::Break(s),
            RawKeyword::Case => Keyword::Case(s),
            RawKeyword::Catch => Keyword::Catch(s),
            RawKeyword::Class => Keyword::Class(s),
            RawKeyword::Const => Keyword::Const(s),
            RawKeyword::Continue => Keyword::Continue(s),
            RawKeyword::Debugger => Keyword::Debugger(s),
            RawKeyword::Default => Keyword::Default(s),
            RawKeyword::Delete => Keyword::Delete(s),
            RawKeyword::Do => Keyword::Do(s),
            RawKeyword::Else => Keyword::Else(s),
            RawKeyword::Enum => Keyword::Enum(s),
            RawKeyword::Export => Keyword::Export(s),
            RawKeyword::Extends => Keyword::Extends(s),
            RawKeyword::Finally => Keyword::Finally(s),
            RawKeyword::For => Keyword::For(s),
            RawKeyword::Function => Keyword::Function(s),
            RawKeyword::If => Keyword::If(s),
            RawKeyword::Implements => Keyword::Implements(s),
            RawKeyword::Import => Keyword::Import(s),
            RawKeyword::In => Keyword::In(s),
            RawKeyword::InstanceOf => Keyword::InstanceOf(s),
            RawKeyword::Interface => Keyword::Interface(s),
            RawKeyword::Let => Keyword::Let(s),
            RawKeyword::New => Keyword::New(s),
            RawKeyword::Package => Keyword::Package(s),
            RawKeyword::Private => Keyword::Private(s),
            RawKeyword::Protected => Keyword::Protected(s),
            RawKeyword::Public => Keyword::Public(s),
            RawKeyword::Return => Keyword::Return(s),
            RawKeyword::Static => Keyword::Static(s),
            RawKeyword::Super => Keyword::Super(s),
            RawKeyword::Switch => Keyword::Switch(s),
            RawKeyword::This => Keyword::This(s),
            RawKeyword::Throw => Keyword::Throw(s),
            RawKeyword::Try => Keyword::Try(s),
            RawKeyword::TypeOf => Keyword::TypeOf(s),
            RawKeyword::Var => Keyword::Var(s),
            RawKeyword::Void => Keyword::Void(s),
            RawKeyword::While => Keyword::While(s),
            RawKeyword::With => Keyword::With(s),
            RawKeyword::Yield => Keyword::Yield(s),
        }
    }
}

impl<T> From<&Keyword<T>> for RawKeyword {
    fn from(k: &Keyword<T>) -> Self {
        match k {
            Keyword::Await(_) => RawKeyword::Await,
            Keyword::Break(_) => RawKeyword::Break,
            Keyword::Case(_) => RawKeyword::Case,
            Keyword::Catch(_) => RawKeyword::Catch,
            Keyword::Class(_) => RawKeyword::Class,
            Keyword::Const(_) => RawKeyword::Const,
            Keyword::Continue(_) => RawKeyword::Continue,
            Keyword::Debugger(_) => RawKeyword::Debugger,
            Keyword::Default(_) => RawKeyword::Default,
            Keyword::Delete(_) => RawKeyword::Delete,
            Keyword::Do(_) => RawKeyword::Do,
            Keyword::Else(_) => RawKeyword::Else,
            Keyword::Enum(_) => RawKeyword::Enum,
            Keyword::Export(_) => RawKeyword::Export,
            Keyword::Extends(_) => RawKeyword::Extends,
            Keyword::Finally(_) => RawKeyword::Finally,
            Keyword::For(_) => RawKeyword::For,
            Keyword::Function(_) => RawKeyword::Function,
            Keyword::If(_) => RawKeyword::If,
            Keyword::Implements(_) => RawKeyword::Implements,
            Keyword::Import(_) => RawKeyword::Import,
            Keyword::In(_) => RawKeyword::In,
            Keyword::InstanceOf(_) => RawKeyword::InstanceOf,
            Keyword::Interface(_) => RawKeyword::Interface,
            Keyword::Let(_) => RawKeyword::Let,
            Keyword::New(_) => RawKeyword::New,
            Keyword::Package(_) => RawKeyword::Package,
            Keyword::Private(_) => RawKeyword::Private,
            Keyword::Protected(_) => RawKeyword::Protected,
            Keyword::Public(_) => RawKeyword::Public,
            Keyword::Return(_) => RawKeyword::Return,
            Keyword::Static(_) => RawKeyword::Static,
            Keyword::Super(_) => RawKeyword::Super,
            Keyword::Switch(_) => RawKeyword::Switch,
            Keyword::This(_) => RawKeyword::This,
            Keyword::Throw(_) => RawKeyword::Throw,
            Keyword::Try(_) => RawKeyword::Try,
            Keyword::TypeOf(_) => RawKeyword::TypeOf,
            Keyword::Var(_) => RawKeyword::Var,
            Keyword::Void(_) => RawKeyword::Void,
            Keyword::While(_) => RawKeyword::While,
            Keyword::With(_) => RawKeyword::With,
            Keyword::Yield(_) => RawKeyword::Yield,
        }
    }
}