kataan 0.0.3

A high-performance JavaScript engine written in pure Rust. Library, C FFI, and CLI.
Documentation
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
//! Token kinds, keywords, and the [`Token`] type produced by the [`Lexer`].
//!
//! [`Lexer`]: super::Lexer

use crate::common::Span;

/// A single lexical token: its [`TokenKind`], the source [`Span`] it covers,
/// and whether a line terminator preceded it (the signal Automatic Semicolon
/// Insertion needs).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Token {
    /// What kind of token this is.
    pub kind: TokenKind,
    /// The byte range in the source this token covers.
    pub span: Span,
    /// Whether at least one line terminator (or a block comment containing
    /// one) appeared in the trivia immediately before this token.
    pub newline_before: bool,
}

impl Token {
    /// The raw source text of this token.
    #[inline]
    #[must_use]
    pub fn text<'s>(&self, source: &'s str) -> &'s str {
        self.span.slice(source)
    }
}

/// The lexical category of a [`Token`].
///
/// Literal tokens ([`Number`](Self::Number), [`String`](Self::String),
/// [`Regex`](Self::Regex), the template parts, …) carry no decoded value: the
/// value is recovered from the token's source [`Span`] by the parser. This
/// keeps `TokenKind` a cheap `Copy` enum and defers cooking strings/numbers to
/// the stage that needs them.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum TokenKind {
    // --- end of input ---
    /// End of the source text.
    Eof,

    // --- names & literals ---
    /// An identifier or a contextual keyword used as a name.
    Identifier,
    /// A reserved word; the specific keyword is carried inline.
    Keyword(Keyword),
    /// A private class member name, e.g. `#count`.
    PrivateName,
    /// A numeric literal (decimal, hex/octal/binary, float, exponent).
    Number,
    /// A `BigInt` literal (a numeric literal with the `n` suffix).
    BigInt,
    /// A string literal (single- or double-quoted), escapes not yet decoded.
    String,
    /// A regular-expression literal `/pattern/flags`.
    Regex,
    /// A template with no substitutions: `` `text` ``.
    NoSubstitutionTemplate,
    /// The head of a template up to the first `${`: `` `text${ ``.
    TemplateHead,
    /// A template part between two substitutions: `` }text${ ``.
    TemplateMiddle,
    /// The tail of a template after the last substitution: `` }text` ``.
    TemplateTail,

    // --- brackets ---
    /// `{`
    LBrace,
    /// `}`
    RBrace,
    /// `(`
    LParen,
    /// `)`
    RParen,
    /// `[`
    LBracket,
    /// `]`
    RBracket,

    // --- punctuation ---
    /// `;`
    Semicolon,
    /// `,`
    Comma,
    /// `.`
    Dot,
    /// `...`
    DotDotDot,
    /// `:`
    Colon,
    /// `?`
    Question,
    /// `?.`
    QuestionDot,
    /// `??`
    QuestionQuestion,
    /// `=>`
    Arrow,

    // --- operators ---
    /// `<`
    Lt,
    /// `>`
    Gt,
    /// `<=`
    LtEq,
    /// `>=`
    GtEq,
    /// `==`
    EqEq,
    /// `!=`
    BangEq,
    /// `===`
    EqEqEq,
    /// `!==`
    BangEqEq,
    /// `+`
    Plus,
    /// `-`
    Minus,
    /// `*`
    Star,
    /// `/`
    Slash,
    /// `%`
    Percent,
    /// `**`
    StarStar,
    /// `++`
    PlusPlus,
    /// `--`
    MinusMinus,
    /// `<<`
    Shl,
    /// `>>`
    Shr,
    /// `>>>`
    Ushr,
    /// `&`
    Amp,
    /// `|`
    Pipe,
    /// `^`
    Caret,
    /// `!`
    Bang,
    /// `~`
    Tilde,
    /// `&&`
    AmpAmp,
    /// `||`
    PipePipe,

    // --- assignment ---
    /// `=`
    Eq,
    /// `+=`
    PlusEq,
    /// `-=`
    MinusEq,
    /// `*=`
    StarEq,
    /// `/=`
    SlashEq,
    /// `%=`
    PercentEq,
    /// `**=`
    StarStarEq,
    /// `<<=`
    ShlEq,
    /// `>>=`
    ShrEq,
    /// `>>>=`
    UshrEq,
    /// `&=`
    AmpEq,
    /// `|=`
    PipeEq,
    /// `^=`
    CaretEq,
    /// `&&=`
    AmpAmpEq,
    /// `||=`
    PipePipeEq,
    /// `??=`
    QuestionQuestionEq,
}

impl TokenKind {
    /// Whether this token is a template part that introduces or continues a
    /// substitution context (`` `…${ `` or `` }…${ ``).
    #[must_use]
    pub fn is_template_open(self) -> bool {
        matches!(self, TokenKind::TemplateHead | TokenKind::TemplateMiddle)
    }
}

/// The ECMAScript reserved words and contextual keywords the lexer recognizes.
///
/// This includes the always-reserved words, the strict-mode-reserved words,
/// and the common contextual keywords (`async`, `await`, `let`, `of`,
/// `yield`, `static`, `get`, `set`, …). Whether a contextual keyword acts as a
/// keyword or an identifier in a given position is the parser's job; the lexer
/// classifies the spelling and lets the parser decide.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
#[allow(missing_docs)] // each variant is its own keyword; the name is the doc
pub enum Keyword {
    // Always reserved.
    Await,
    Break,
    Case,
    Catch,
    Class,
    Const,
    Continue,
    Debugger,
    Default,
    Delete,
    Do,
    Else,
    Enum,
    Export,
    Extends,
    False,
    Finally,
    For,
    Function,
    If,
    Import,
    In,
    Instanceof,
    New,
    Null,
    Return,
    Super,
    Switch,
    This,
    Throw,
    True,
    Try,
    Typeof,
    Var,
    Void,
    While,
    With,
    // Strict-mode reserved.
    Implements,
    Interface,
    Let,
    Package,
    Private,
    Protected,
    Public,
    Static,
    Yield,
    // Common contextual keywords.
    As,
    Async,
    From,
    Get,
    Of,
    Set,
    Target,
    Accessor,
}

impl Keyword {
    /// Maps a spelling to a [`Keyword`], or `None` if it is an ordinary
    /// identifier. (Not `FromStr`: a non-keyword is `None`, not an error.)
    #[must_use]
    #[allow(clippy::should_implement_trait)]
    pub fn from_str(s: &str) -> Option<Keyword> {
        use Keyword::*;
        Some(match s {
            "await" => Await,
            "break" => Break,
            "case" => Case,
            "catch" => Catch,
            "class" => Class,
            "const" => Const,
            "continue" => Continue,
            "debugger" => Debugger,
            "default" => Default,
            "delete" => Delete,
            "do" => Do,
            "else" => Else,
            "enum" => Enum,
            "export" => Export,
            "extends" => Extends,
            "false" => False,
            "finally" => Finally,
            "for" => For,
            "function" => Function,
            "if" => If,
            "import" => Import,
            "in" => In,
            "instanceof" => Instanceof,
            "new" => New,
            "null" => Null,
            "return" => Return,
            "super" => Super,
            "switch" => Switch,
            "this" => This,
            "throw" => Throw,
            "true" => True,
            "try" => Try,
            "typeof" => Typeof,
            "var" => Var,
            "void" => Void,
            "while" => While,
            "with" => With,
            "implements" => Implements,
            "interface" => Interface,
            "let" => Let,
            "package" => Package,
            "private" => Private,
            "protected" => Protected,
            "public" => Public,
            "static" => Static,
            "yield" => Yield,
            "as" => As,
            "async" => Async,
            "from" => From,
            "get" => Get,
            "of" => Of,
            "set" => Set,
            "target" => Target,
            "accessor" => Accessor,
            _ => return None,
        })
    }

    /// The canonical spelling of this keyword.
    #[must_use]
    pub fn as_str(self) -> &'static str {
        use Keyword::*;
        match self {
            Await => "await",
            Break => "break",
            Case => "case",
            Catch => "catch",
            Class => "class",
            Const => "const",
            Continue => "continue",
            Debugger => "debugger",
            Default => "default",
            Delete => "delete",
            Do => "do",
            Else => "else",
            Enum => "enum",
            Export => "export",
            Extends => "extends",
            False => "false",
            Finally => "finally",
            For => "for",
            Function => "function",
            If => "if",
            Import => "import",
            In => "in",
            Instanceof => "instanceof",
            New => "new",
            Null => "null",
            Return => "return",
            Super => "super",
            Switch => "switch",
            This => "this",
            Throw => "throw",
            True => "true",
            Try => "try",
            Typeof => "typeof",
            Var => "var",
            Void => "void",
            While => "while",
            With => "with",
            Implements => "implements",
            Interface => "interface",
            Let => "let",
            Package => "package",
            Private => "private",
            Protected => "protected",
            Public => "public",
            Static => "static",
            Yield => "yield",
            As => "as",
            Async => "async",
            From => "from",
            Get => "get",
            Of => "of",
            Set => "set",
            Target => "target",
            Accessor => "accessor",
        }
    }

    /// Whether this is a *contextual* keyword — one that is only reserved in
    /// certain positions and is otherwise usable as an ordinary identifier
    /// (e.g. `async`, `of`, `get`, `from`). The always-reserved words and the
    /// strict-mode reserved words return `false`.
    #[must_use]
    pub fn is_contextual(self) -> bool {
        use Keyword::*;
        matches!(self, As | Async | From | Get | Of | Set | Target | Accessor)
    }

    /// Whether, when this keyword is the previous significant token, a
    /// following `/` should be read as the start of a regex literal rather
    /// than division. Keywords that can end an expression (`this`, `super`,
    /// `true`, `false`, `null`) are followed by division; keywords that
    /// introduce an expression (`return`, `typeof`, `case`, `in`, …) are
    /// followed by a regex.
    #[must_use]
    pub fn before_expression(self) -> bool {
        use Keyword::*;
        !matches!(self, This | Super | True | False | Null)
    }
}

#[cfg(test)]
mod tests {
    use super::{Keyword, TokenKind};

    #[test]
    fn keyword_roundtrip() {
        for kw in [
            Keyword::Await,
            Keyword::Function,
            Keyword::Yield,
            Keyword::Of,
        ] {
            assert_eq!(Keyword::from_str(kw.as_str()), Some(kw));
        }
        assert_eq!(Keyword::from_str("notakeyword"), None);
        assert_eq!(Keyword::from_str("Function"), None); // case-sensitive
    }

    #[test]
    fn template_open_classification() {
        assert!(TokenKind::TemplateHead.is_template_open());
        assert!(TokenKind::TemplateMiddle.is_template_open());
        assert!(!TokenKind::TemplateTail.is_template_open());
        assert!(!TokenKind::NoSubstitutionTemplate.is_template_open());
    }

    #[test]
    fn regex_after_keyword() {
        assert!(Keyword::Return.before_expression());
        assert!(Keyword::Typeof.before_expression());
        assert!(!Keyword::This.before_expression());
        assert!(!Keyword::True.before_expression());
    }
}