oxc_parser 0.125.0

A collection of JavaScript tools written in Rust.
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
//! ECMAScript Token Kinds

use std::fmt::{self, Display};

use oxc_data_structures::fieldless_enum;

// `fieldless_enum!` macro provides `Kind::VARIANTS` constant listing all variants
fieldless_enum! {
    /// Lexer token kind
    ///
    /// Exported for other oxc crates to use. You generally don't need to use this directly.
    #[derive(Debug, Default, Clone, Copy, Eq, PartialEq)]
    #[repr(u8)]
    #[non_exhaustive]
    pub enum Kind {
        #[default]
        Eof = 0,
        Undetermined,
        Skip, // Whitespace, line breaks, comments
        // 12.5 Hashbang Comments
        HashbangComment,
        // 12.7.1 identifier
        Ident,
        // 12.7.2 keyword
        Await,
        Break,
        Case,
        Catch,
        Class,
        Const,
        Continue,
        Debugger,
        Default,
        Delete,
        Do,
        Else,
        Enum,
        Export,
        Extends,
        Finally,
        For,
        Function,
        If,
        Import,
        In,
        Instanceof,
        New,
        Return,
        Super,
        Switch,
        This,
        Throw,
        Try,
        Typeof,
        Var,
        Void,
        While,
        With,
        // Contextual Keywords
        Async,
        From,
        Get,
        Meta, // import.meta
        Of,
        Set,
        Target,   // new.target
        Accessor, // keyword from https://github.com/tc39/proposal-decorators
        Source,   // import.source https://github.com/tc39/proposal-source-phase-imports
        Defer,    // import.defer https://github.com/tc39/proposal-defer-import-eval
        // TypeScript Contextual Keywords
        Abstract,
        As,
        Asserts,
        Assert,
        Any,
        Boolean,
        Constructor,
        Declare,
        Infer,
        Intrinsic,
        Is,
        KeyOf,
        Module,
        Namespace,
        Never,
        Out,
        Readonly,
        Require,
        Number, // the "number" keyword for TypeScript
        Object,
        Satisfies,
        String, // the "string" keyword for TypeScript
        Symbol,
        Type,
        Undefined,
        Unique,
        Using,
        Unknown,
        Global,
        BigInt, // the "bigint" keyword for TypeScript
        Override,
        // Future keywords (strict mode reserved words)
        Implements,
        Interface,
        Let,
        Package,
        Private,
        Protected,
        Public,
        Static,
        Yield,
        // 12.9.1 Null Literals
        // 12.9.2 Boolean Literals
        // Moved here to make all keywords contiguous for range check optimization
        True,
        False,
        Null,
        // 12.8 punctuators
        Amp, // &
        Amp2,
        Amp2Eq,
        AmpEq,
        Bang, // !
        Caret,
        CaretEq,
        Colon,
        Comma,
        Dot,
        Dot3, // ...
        Eq,
        Eq2,
        Eq3,
        GtEq, // >=
        LAngle,
        LBrack,
        LCurly,
        LParen,
        LtEq, // <=
        Minus,
        Minus2,
        MinusEq,
        Neq,
        Neq2,
        Percent,
        PercentEq,
        Pipe,
        Pipe2,
        Pipe2Eq,
        PipeEq,
        Plus,
        Plus2,
        PlusEq,
        Question,
        Question2,
        Question2Eq,
        QuestionDot,
        RAngle,
        RBrack,
        RCurly,
        RParen,
        Semicolon,
        ShiftLeft,     // <<
        ShiftLeftEq,   // <<=
        ShiftRight,    // >>
        ShiftRight3,   // >>>
        ShiftRight3Eq, // >>>=
        ShiftRightEq,  // >>=
        Slash,
        SlashEq,
        Star,
        Star2,
        Star2Eq,
        StarEq,
        Tilde,
        // arrow function
        Arrow,
        // 12.9.3 Numeric Literals
        Decimal,
        Float,
        Binary,
        Octal,
        Hex,
        // for `1e10`, `1e+10`
        PositiveExponential,
        // for `1e-10`
        NegativeExponential,
        // BigInt Literals (numeric literals with 'n' suffix)
        DecimalBigInt,
        BinaryBigInt,
        OctalBigInt,
        HexBigInt,
        // 12.9.4 String Literals
        /// String Type
        Str,
        // 12.9.5 Regular Expression Literals
        RegExp,
        // 12.9.6 Template Literal
        NoSubstitutionTemplate,
        TemplateHead,
        TemplateMiddle,
        TemplateTail,
        // es2022 Private Identifier
        PrivateIdentifier,
        // JSX
        JSXText,
        // Decorator
        At,
    }
}

#[allow(clippy::enum_glob_use, clippy::allow_attributes)]
use Kind::*;

impl Kind {
    #[inline]
    pub const fn is_eof(self) -> bool {
        matches!(self, Eof)
    }

    #[rustfmt::skip]
    #[inline]
    pub const fn is_number(self) -> bool {
        matches!(
            self,
            Decimal | Float | Binary | Octal | Hex | PositiveExponential | NegativeExponential
            | DecimalBigInt | BinaryBigInt | OctalBigInt | HexBigInt
        )
    }

    #[inline] // Inline into `read_non_decimal` - see comment there as to why
    pub fn matches_number_byte(self, b: u8) -> bool {
        match self {
            Decimal => b.is_ascii_digit(),
            Binary => matches!(b, b'0'..=b'1'),
            Octal => matches!(b, b'0'..=b'7'),
            Hex => b.is_ascii_hexdigit(),
            _ => unreachable!(),
        }
    }

    /// [Identifiers](https://tc39.es/ecma262/#sec-identifiers)
    /// `IdentifierReference`
    #[inline]
    pub const fn is_identifier_reference(
        self,
        is_yield_context: bool,
        is_await_context: bool,
    ) -> bool {
        self.is_identifier()
            || (!is_yield_context && matches!(self, Yield))
            || (!is_await_context && matches!(self, Await))
    }

    /// `BindingIdentifier`
    #[inline]
    pub const fn is_binding_identifier(self) -> bool {
        self.is_identifier() || matches!(self, Yield | Await)
    }

    /// `LabelIdentifier`
    #[inline]
    pub const fn is_label_identifier(self, is_yield_context: bool, is_await_context: bool) -> bool {
        self.is_identifier()
            || (!is_yield_context && matches!(self, Yield))
            || (!is_await_context && matches!(self, Await))
    }

    /// Identifier
    /// `IdentifierName` but not `ReservedWord`
    #[inline]
    pub const fn is_identifier(self) -> bool {
        self.is_identifier_name() && !self.is_reserved_keyword()
    }

    /// TypeScript Identifier
    ///
    /// <https://github.com/microsoft/TypeScript/blob/15392346d05045742e653eab5c87538ff2a3c863/src/compiler/parser.ts#L2316-L2335>
    #[inline]
    pub const fn is_ts_identifier(self, is_yield_context: bool, is_await_context: bool) -> bool {
        self.is_identifier_reference(is_yield_context, is_await_context)
            && !self.is_strict_mode_contextual_keyword()
            && !self.is_contextual_keyword()
    }

    /// `IdentifierName`
    /// All identifier names are either `Ident` or keywords (Await..=Null in the enum).
    #[inline]
    pub const fn is_identifier_name(self) -> bool {
        matches!(self, Ident) || matches!(self as u8, x if x >= Await as u8 && x <= Null as u8)
    }

    /// Check the succeeding token of a `let` keyword.
    ///
    /// ```javascript
    /// let { a, b } = c, let [a, b] = c, let ident
    /// ```
    #[inline]
    pub const fn is_after_let(self) -> bool {
        !matches!(self, In | Instanceof)
            && (matches!(self, LCurly | LBrack | Ident) || self.is_any_keyword())
    }

    /// Section 13.2.4 Literals
    /// Literal :
    ///     `NullLiteral`
    ///     `BooleanLiteral`
    ///     `NumericLiteral`
    ///     `StringLiteral`
    #[inline]
    pub const fn is_literal(self) -> bool {
        matches!(self, Null | True | False | Str | RegExp) || self.is_number()
    }

    #[inline]
    pub const fn is_after_await_or_yield(self) -> bool {
        !self.is_binary_operator() && (self.is_literal() || self.is_identifier_name())
    }

    /// Section 13.2.6 Object Initializer
    /// `LiteralPropertyName` :
    ///     `IdentifierName`
    ///     `StringLiteral`
    ///     `NumericLiteral`
    #[inline]
    pub const fn is_literal_property_name(self) -> bool {
        self.is_identifier_name() || matches!(self, Str) || self.is_number()
    }

    #[inline]
    pub const fn is_identifier_or_keyword(self) -> bool {
        self.is_literal_property_name() || matches!(self, PrivateIdentifier)
    }

    #[rustfmt::skip]
    #[inline]
    pub const fn is_assignment_operator(self) -> bool {
        matches!(
            self,
            Eq | PlusEq | MinusEq | StarEq | SlashEq | PercentEq | ShiftLeftEq | ShiftRightEq
            | ShiftRight3Eq | Pipe2Eq | Amp2Eq | PipeEq | CaretEq | AmpEq | Question2Eq | Star2Eq
        )
    }

    #[rustfmt::skip]
    #[inline]
    pub const fn is_binary_operator(self) -> bool {
        matches!(
            self,
            Eq2 | Neq | Eq3 | Neq2 | LAngle | LtEq | RAngle | GtEq | ShiftLeft | ShiftRight | ShiftRight3
            | Plus | Minus | Star | Slash | Percent | Pipe | Caret | Amp | In | Instanceof | Star2
        )
    }

    #[inline]
    pub const fn is_logical_operator(self) -> bool {
        matches!(self, Pipe2 | Amp2 | Question2)
    }

    #[inline]
    pub const fn is_unary_operator(self) -> bool {
        matches!(self, Minus | Plus | Bang | Tilde | Typeof | Void | Delete)
    }

    #[inline]
    pub const fn is_update_operator(self) -> bool {
        matches!(self, Plus2 | Minus2)
    }

    /// [Keywords and Reserved Words](https://tc39.es/ecma262/#sec-keywords-and-reserved-words)
    #[inline]
    pub const fn is_any_keyword(self) -> bool {
        // Note: `is_future_reserved_keyword` is a subset of `is_strict_mode_contextual_keyword`,
        // so the last arm is redundant. We include it anyway so each spec category is represented:
        // - Reserved words: https://tc39.es/ecma262/#prod-ReservedWord
        // - Contextual keywords: https://tc39.es/ecma262/#sec-keywords-and-reserved-words
        // - Strict mode reserved words: https://tc39.es/ecma262/#sec-strict-mode-of-ecmascript
        // - Future reserved words: https://tc39.es/ecma262/#sec-future-reserved-words
        // The compiler optimizes all four calls into a single range check (2 instructions total),
        // so keeping `is_future_reserved_keyword` has no performance impact.
        self.is_reserved_keyword()
            || self.is_contextual_keyword()
            || self.is_strict_mode_contextual_keyword()
            || self.is_future_reserved_keyword()
    }

    #[rustfmt::skip]
    #[inline]
    pub const fn is_reserved_keyword(self) -> bool {
        matches!(
            self,
            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 | Yield
        )
    }

    #[rustfmt::skip]
    #[inline]
    pub const fn is_strict_mode_contextual_keyword(self) -> bool {
        matches!(self, Let | Static | Implements | Interface | Package | Private | Protected | Public)
    }

    #[rustfmt::skip]
    #[inline]
    pub const fn is_contextual_keyword(self) -> bool {
        matches!(
            self,
            Async | From | Get | Meta | Of | Set | Target | Accessor | Abstract | As | Asserts | Assert
            | Any | Boolean | Constructor | Declare | Infer | Intrinsic | Is | KeyOf | Module | Namespace
            | Never | Out | Readonly | Require | Number | Object | Satisfies | String | Symbol | Type
            | Undefined | Unique | Unknown | Using | Global | BigInt | Override | Source | Defer
        )
    }

    #[rustfmt::skip]
    #[inline]
    pub const fn is_future_reserved_keyword(self) -> bool {
        matches!(self, Implements | Interface | Package | Private | Protected | Public | Static)
    }

    #[inline]
    pub const fn is_template_start_of_tagged_template(self) -> bool {
        matches!(self, NoSubstitutionTemplate | TemplateHead)
    }

    #[rustfmt::skip]
    #[inline]
    pub const fn is_modifier_kind(self) -> bool {
        matches!(
            self,
            Abstract | Accessor | Async | Const | Declare
            | In | Out | Public | Private | Protected | Readonly | Static | Override
            | Default | Export
        )
    }

    #[inline]
    pub const fn is_binding_identifier_or_private_identifier_or_pattern(self) -> bool {
        matches!(self, LCurly | LBrack | PrivateIdentifier) || self.is_binding_identifier()
    }

    #[cold]
    pub fn match_keyword(s: &str) -> Self {
        let len = s.len();
        // SAFETY: Already checked `len <= 1`.
        if len <= 1 || len >= 12 || !unsafe { s.as_bytes().get_unchecked(0) }.is_ascii_lowercase() {
            return Ident;
        }
        Self::match_keyword_impl(s)
    }

    fn match_keyword_impl(s: &str) -> Self {
        match s {
            "as" => As,
            "do" => Do,
            "if" => If,
            "in" => In,
            "is" => Is,
            "of" => Of,

            "any" => Any,
            "for" => For,
            "get" => Get,
            "let" => Let,
            "new" => New,
            "out" => Out,
            "set" => Set,
            "try" => Try,
            "var" => Var,

            "case" => Case,
            "else" => Else,
            "enum" => Enum,
            "from" => From,
            "meta" => Meta,
            "null" => Null,
            "this" => This,
            "true" => True,
            "type" => Type,
            "void" => Void,
            "with" => With,

            "async" => Async,
            "await" => Await,
            "break" => Break,
            "catch" => Catch,
            "class" => Class,
            "const" => Const,
            "false" => False,
            "infer" => Infer,
            "keyof" => KeyOf,
            "never" => Never,
            "super" => Super,
            "throw" => Throw,
            "using" => Using,
            "while" => While,
            "yield" => Yield,
            "defer" => Defer,

            "assert" => Assert,
            "bigint" => BigInt,
            "delete" => Delete,
            "export" => Export,
            "global" => Global,
            "import" => Import,
            "module" => Module,
            "number" => Number,
            "object" => Object,
            "public" => Public,
            "return" => Return,
            "static" => Static,
            "string" => String,
            "switch" => Switch,
            "symbol" => Symbol,
            "target" => Target,
            "typeof" => Typeof,
            "unique" => Unique,
            "source" => Source,

            "asserts" => Asserts,
            "boolean" => Boolean,
            "declare" => Declare,
            "default" => Default,
            "extends" => Extends,
            "finally" => Finally,
            "package" => Package,
            "private" => Private,
            "require" => Require,
            "unknown" => Unknown,

            "abstract" => Abstract,
            "accessor" => Accessor,
            "continue" => Continue,
            "debugger" => Debugger,
            "function" => Function,
            "override" => Override,
            "readonly" => Readonly,

            "interface" => Interface,
            "intrinsic" => Intrinsic,
            "namespace" => Namespace,
            "protected" => Protected,
            "satisfies" => Satisfies,
            "undefined" => Undefined,

            "implements" => Implements,
            "instanceof" => Instanceof,

            "constructor" => Constructor,
            _ => Ident,
        }
    }

    pub fn to_str(self) -> &'static str {
        #[expect(clippy::match_same_arms)]
        match self {
            Undetermined => "Unknown",
            Eof => "EOF",
            Skip => "Skipped",
            HashbangComment => "#!",
            Ident => "Identifier",
            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",
            Finally => "finally",
            For => "for",
            Function => "function",
            Using => "using",
            If => "if",
            Import => "import",
            In => "in",
            Instanceof => "instanceof",
            New => "new",
            Return => "return",
            Super => "super",
            Switch => "switch",
            This => "this",
            Throw => "throw",
            Try => "try",
            Typeof => "typeof",
            Var => "var",
            Void => "void",
            While => "while",
            With => "with",
            As => "as",
            Async => "async",
            From => "from",
            Get => "get",
            Meta => "meta",
            Of => "of",
            Set => "set",
            Asserts => "asserts",
            Accessor => "accessor",
            Abstract => "abstract",
            Readonly => "readonly",
            Declare => "declare",
            Override => "override",
            Type => "type",
            Target => "target",
            Source => "source",
            Defer => "defer",
            Implements => "implements",
            Interface => "interface",
            Package => "package",
            Private => "private",
            Protected => "protected",
            Public => "public",
            Static => "static",
            Let => "let",
            Yield => "yield",
            Amp => "&",
            Amp2 => "&&",
            Amp2Eq => "&&=",
            AmpEq => "&=",
            Bang => "!",
            Caret => "^",
            CaretEq => "^=",
            Colon => ":",
            Comma => ",",
            Dot => ".",
            Dot3 => "...",
            Eq => "=",
            Eq2 => "==",
            Eq3 => "===",
            GtEq => ">=",
            LAngle => "<",
            LBrack => "[",
            LCurly => "{",
            LParen => "(",
            LtEq => "<=",
            Minus => "-",
            Minus2 => "--",
            MinusEq => "-=",
            Neq => "!=",
            Neq2 => "!==",
            Percent => "%",
            PercentEq => "%=",
            Pipe => "|",
            Pipe2 => "||",
            Pipe2Eq => "||=",
            PipeEq => "|=",
            Plus => "+",
            Plus2 => "++",
            PlusEq => "+=",
            Question => "?",
            Question2 => "??",
            Question2Eq => "??=",
            QuestionDot => "?.",
            RAngle => ">",
            RBrack => "]",
            RCurly => "}",
            RParen => ")",
            Semicolon => ";",
            ShiftLeft => "<<",
            ShiftLeftEq => "<<=",
            ShiftRight => ">>",
            ShiftRight3 => ">>>",
            ShiftRight3Eq => ">>>=",
            ShiftRightEq => ">>=",
            Slash => "/",
            SlashEq => "/=",
            Star => "*",
            Star2 => "**",
            Star2Eq => "**=",
            StarEq => "*=",
            Tilde => "~",
            Arrow => "=>",
            Null => "null",
            True => "true",
            False => "false",
            Decimal => "decimal",
            Float | PositiveExponential | NegativeExponential => "float",
            Binary => "binary",
            Octal => "octal",
            Hex => "hex",
            DecimalBigInt => "decimal bigint",
            BinaryBigInt => "binary bigint",
            OctalBigInt => "octal bigint",
            HexBigInt => "hex bigint",
            Str | String => "string",
            RegExp => "/regexp/",
            NoSubstitutionTemplate => "${}",
            TemplateHead => "${",
            TemplateMiddle => "${expr}",
            TemplateTail => "}",
            PrivateIdentifier => "#identifier",
            JSXText => "jsx",
            At => "@",
            Assert => "assert",
            Any => "any",
            Boolean => "boolean",
            Constructor => "constructor",
            Infer => "infer",
            Intrinsic => "intrinsic",
            Is => "is",
            KeyOf => "keyof",
            Module => "module",
            Namespace => "namaespace",
            Never => "never",
            Out => "out",
            Require => "require",
            Number => "number",
            Object => "object",
            Satisfies => "satisfies",
            Symbol => "symbol",
            Undefined => "undefined",
            Unique => "unique",
            Unknown => "unknown",
            Global => "global",
            BigInt => "bigint",
        }
    }
}

impl Display for Kind {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.to_str().fmt(f)
    }
}