swc_ecma_parser/lexer/
token.rs

1use num_bigint::BigInt;
2use swc_atoms::{atom, Atom};
3use swc_common::Span;
4use swc_ecma_ast::AssignOp;
5use swc_ecma_lexer::common::context::Context;
6
7use super::LexResult;
8use crate::input::Tokens;
9
10#[derive(Debug, Clone)]
11pub enum TokenValue {
12    /// unknown ident, jsx name and shebang
13    Word(Atom),
14    Template {
15        raw: Atom,
16        cooked: LexResult<Atom>,
17    },
18    // string, jsx text
19    Str {
20        value: Atom,
21        raw: Atom,
22    },
23    // regexp
24    Regex {
25        value: Atom,
26        flags: Atom,
27    },
28    Num {
29        value: f64,
30        raw: Atom,
31    },
32    BigInt {
33        value: Box<num_bigint::BigInt>,
34        raw: Atom,
35    },
36    Error(swc_ecma_lexer::error::Error),
37}
38
39#[derive(Clone, Copy, PartialEq, Eq)]
40#[repr(u8)]
41pub enum Token {
42    // Single character tokens
43    /// `(`
44    LParen,
45    /// `)`
46    RParen,
47    /// `{`
48    LBrace,
49    /// `}`
50    RBrace,
51    /// `[`
52    LBracket,
53    /// `]`
54    RBracket,
55    /// `;`
56    Semi,
57    /// `,`
58    Comma,
59    /// `.`
60    Dot,
61    /// `:`
62    Colon,
63    /// `?`
64    QuestionMark,
65    /// `!`
66    Bang,
67    /// `~`
68    Tilde,
69    /// `+`
70    Plus,
71    /// `-`
72    Minus,
73    /// `*`
74    Asterisk,
75    /// `/`
76    Slash,
77    /// `%`
78    Percent,
79    /// `<`
80    Lt,
81    /// `>`
82    Gt,
83    /// `|`
84    Pipe,
85    /// `^`
86    Caret,
87    /// `&`
88    Ampersand,
89    /// `=`
90    Eq,
91    /// `@`
92    At,
93    /// `#`
94    Hash,
95    /// '`'
96    BackQuote,
97    /// `=>`
98    Arrow,
99    /// `...`
100    DotDotDot,
101
102    // Compound operators
103    /// `++`
104    PlusPlus,
105    /// `--`
106    MinusMinus,
107    /// `+=`
108    PlusEq,
109    /// `-=`
110    MinusEq,
111
112    // More compound operators and keywords
113    /// `*=`
114    MulEq,
115    /// `/=`
116    DivEq,
117    /// `%=`
118    ModEq,
119    /// `<<=`
120    LShiftEq,
121    /// `>>=`
122    RShiftEq,
123    /// `>>>=`
124    ZeroFillRShiftEq,
125    /// `|=`
126    BitOrEq,
127    /// `^=`
128    BitXorEq,
129    /// `&=`
130    BitAndEq,
131    /// `**=`
132    ExpEq,
133    /// `||=`
134    LogicalOrEq,
135    /// `&&=`
136    LogicalAndEq,
137    /// `??=`
138    NullishEq,
139    /// `?.`
140    OptionalChain,
141
142    /// `==`
143    EqEq,
144    /// `!=`
145    NotEq,
146    /// `===`
147    EqEqEq,
148    /// `!==`
149    NotEqEq,
150
151    /// `<=`
152    LtEq,
153    /// `>=`
154    GtEq,
155    /// `<<`
156    LShift,
157    /// `>>`
158    RShift,
159    /// `>>>`
160    ZeroFillRShift,
161
162    /// `**`
163    Exp,
164    /// `||`
165    LogicalOr,
166    /// `&&`
167    LogicalAnd,
168    /// `??`
169    NullishCoalescing,
170
171    /// `</`
172    LessSlash,
173    /// `${`
174    DollarLBrace,
175
176    // JSX-related tokens
177    JSXTagStart,
178    JSXTagEnd,
179
180    // Literals
181    Str,
182    Num,
183    BigInt,
184    Regex,
185    Template,
186    NoSubstitutionTemplateLiteral,
187    TemplateHead,
188    TemplateMiddle,
189    TemplateTail,
190    JSXName,
191    JSXText,
192    // Identifiers and keyword
193    Ident,
194    // Reserved keyword tokens
195    Await,
196    Break,
197    Case,
198    Catch,
199    Class,
200    Const,
201    Continue,
202    Debugger,
203    Default,
204    Delete,
205    Do,
206    Else,
207    Export,
208    Extends,
209    False,
210    Finally,
211    For,
212    Function,
213    If,
214    Import,
215    In,
216    InstanceOf,
217    Let,
218    New,
219    Null,
220    Return,
221    Super,
222    Switch,
223    This,
224    Throw,
225    True,
226    Try,
227    TypeOf,
228    Var,
229    Void,
230    While,
231    With,
232    Yield,
233    Module,
234
235    // TypeScript-related keywords
236    Abstract,
237    Any,
238    As,
239    Asserts,
240    Assert,
241    Async,
242    Bigint,
243    Boolean,
244    Constructor,
245    Declare,
246    Enum,
247    From,
248    Get,
249    Global,
250    Implements,
251    Interface,
252    Intrinsic,
253    Is,
254    Keyof,
255    Namespace,
256    Never,
257    Number,
258    Object,
259    Of,
260    Out,
261    Override,
262    Package,
263    Private,
264    Protected,
265    Public,
266    Readonly,
267    Require,
268    Set,
269    Static,
270    String,
271    Symbol,
272    Type,
273    Undefined,
274    Unique,
275    Unknown,
276    Using,
277    Accessor,
278    Infer,
279    Satisfies,
280    Meta,
281    Target,
282
283    // Special tokens
284    Shebang,
285    Error,
286    Eof,
287}
288
289impl swc_ecma_lexer::common::lexer::state::TokenKind for Token {
290    #[inline(always)]
291    fn is_dot(self) -> bool {
292        self == Token::Dot
293    }
294
295    #[inline(always)]
296    fn is_bin_op(self) -> bool {
297        Token::is_bin_op(self)
298    }
299
300    #[inline(always)]
301    fn is_semi(self) -> bool {
302        self == Token::Semi
303    }
304
305    #[inline(always)]
306    fn is_template(self) -> bool {
307        self == Token::Template
308    }
309
310    #[inline(always)]
311    fn is_keyword(self) -> bool {
312        Token::is_keyword(self)
313    }
314
315    #[inline(always)]
316    fn is_colon(self) -> bool {
317        self == Token::Colon
318    }
319
320    #[inline(always)]
321    fn is_lbrace(self) -> bool {
322        self == Token::LBrace
323    }
324
325    #[inline(always)]
326    fn is_rbrace(self) -> bool {
327        self == Token::RBrace
328    }
329
330    #[inline(always)]
331    fn is_lparen(self) -> bool {
332        self == Token::LParen
333    }
334
335    #[inline(always)]
336    fn is_rparen(self) -> bool {
337        self == Token::RParen
338    }
339
340    #[inline(always)]
341    fn is_keyword_fn(self) -> bool {
342        self == Token::Function
343    }
344
345    #[inline(always)]
346    fn is_keyword_return(self) -> bool {
347        self == Token::Return
348    }
349
350    #[inline(always)]
351    fn is_keyword_yield(self) -> bool {
352        self == Token::Yield
353    }
354
355    #[inline(always)]
356    fn is_keyword_else(self) -> bool {
357        self == Token::Else
358    }
359
360    #[inline(always)]
361    fn is_keyword_class(self) -> bool {
362        self == Token::Class
363    }
364
365    #[inline(always)]
366    fn is_keyword_let(self) -> bool {
367        self == Token::Let
368    }
369
370    #[inline(always)]
371    fn is_keyword_var(self) -> bool {
372        self == Token::Var
373    }
374
375    #[inline(always)]
376    fn is_keyword_const(self) -> bool {
377        self == Token::Const
378    }
379
380    #[inline(always)]
381    fn is_keyword_if(self) -> bool {
382        self == Token::If
383    }
384
385    #[inline(always)]
386    fn is_keyword_while(self) -> bool {
387        self == Token::While
388    }
389
390    #[inline(always)]
391    fn is_keyword_for(self) -> bool {
392        self == Token::For
393    }
394
395    #[inline(always)]
396    fn is_keyword_with(self) -> bool {
397        self == Token::With
398    }
399
400    #[inline(always)]
401    fn is_lt(self) -> bool {
402        self == Token::Lt
403    }
404
405    #[inline(always)]
406    fn is_gt(self) -> bool {
407        self == Token::Gt
408    }
409
410    #[inline(always)]
411    fn is_arrow(self) -> bool {
412        self == Token::Arrow
413    }
414
415    #[inline(always)]
416    fn is_ident(self) -> bool {
417        self == Token::Ident || self.is_known_ident()
418    }
419
420    #[inline(always)]
421    fn is_known_ident_of(self) -> bool {
422        self == Token::Of
423    }
424
425    #[inline(always)]
426    fn is_slash(self) -> bool {
427        self == Token::Slash
428    }
429
430    #[inline(always)]
431    fn is_dollar_lbrace(self) -> bool {
432        self == Token::DollarLBrace
433    }
434
435    #[inline(always)]
436    fn is_plus_plus(self) -> bool {
437        self == Token::PlusPlus
438    }
439
440    #[inline(always)]
441    fn is_minus_minus(self) -> bool {
442        self == Token::MinusMinus
443    }
444
445    #[inline(always)]
446    fn is_back_quote(self) -> bool {
447        self == Token::BackQuote
448    }
449
450    #[inline(always)]
451    fn is_jsx_tag_start(self) -> bool {
452        self == Token::JSXTagStart
453    }
454
455    #[inline(always)]
456    fn is_jsx_tag_end(self) -> bool {
457        self == Token::JSXTagEnd
458    }
459
460    #[inline(always)]
461    fn before_expr(self) -> bool {
462        self.before_expr()
463    }
464}
465
466impl swc_ecma_lexer::common::lexer::state::TokenType for Token {
467    fn is_other_and_before_expr_is_false(self) -> bool {
468        !self.is_keyword()
469            && !self.is_bin_op()
470            && !self.before_expr()
471            && !matches!(
472                self,
473                Token::Template
474                    | Token::Dot
475                    | Token::Colon
476                    | Token::LBrace
477                    | Token::RParen
478                    | Token::Semi
479                    | Token::JSXName
480                    | Token::JSXText
481                    | Token::JSXTagStart
482                    | Token::JSXTagEnd
483                    | Token::Arrow
484            )
485    }
486
487    fn is_other_and_can_have_trailing_comment(self) -> bool {
488        matches!(
489            self,
490            Token::Num
491                | Token::Str
492                | Token::Ident
493                | Token::DollarLBrace
494                | Token::Regex
495                | Token::BigInt
496                | Token::JSXText
497                | Token::RBrace
498        ) || self.is_known_ident()
499    }
500}
501
502impl<'a, I: Tokens> swc_ecma_lexer::common::lexer::token::TokenFactory<'a, TokenAndSpan, I>
503    for Token
504{
505    type Buffer = crate::input::Buffer<I>;
506    type Lexer = crate::Lexer<'a>;
507
508    const ABSTRACT: Self = Token::Abstract;
509    const ACCESSOR: Self = Token::Accessor;
510    const ANY: Self = Token::Any;
511    const ARROW: Self = Token::Arrow;
512    const AS: Self = Token::As;
513    const ASSERT: Self = Token::Assert;
514    const ASSERTS: Self = Token::Asserts;
515    const ASYNC: Self = Token::Async;
516    const AT: Self = Token::At;
517    const AWAIT: Self = Token::Await;
518    const BACKQUOTE: Self = Token::BackQuote;
519    const BANG: Self = Self::Bang;
520    const BIGINT: Self = Token::Bigint;
521    const BIT_AND: Self = Self::Ampersand;
522    const BIT_AND_EQ: Self = Self::BitAndEq;
523    const BIT_OR: Self = Self::Pipe;
524    const BIT_OR_EQ: Self = Self::BitOrEq;
525    const BOOLEAN: Self = Token::Boolean;
526    const BREAK: Self = Token::Break;
527    const CASE: Self = Token::Case;
528    const CATCH: Self = Token::Catch;
529    const CLASS: Self = Self::Class;
530    const COLON: Self = Self::Colon;
531    const COMMA: Self = Token::Comma;
532    const CONST: Self = Self::Const;
533    const CONTINUE: Self = Token::Continue;
534    const DEBUGGER: Self = Token::Debugger;
535    const DECLARE: Self = Token::Declare;
536    const DEFAULT: Self = Token::Default;
537    const DELETE: Self = Self::Delete;
538    const DIV: Self = Token::Slash;
539    const DIV_EQ: Self = Token::DivEq;
540    const DO: Self = Token::Do;
541    const DOLLAR_LBRACE: Self = Token::DollarLBrace;
542    const DOT: Self = Self::Dot;
543    const DOTDOTDOT: Self = Self::DotDotDot;
544    const ELSE: Self = Self::Else;
545    const ENUM: Self = Token::Enum;
546    const EOF: Self = Token::Eof;
547    const EQUAL: Self = Token::Eq;
548    const EXP: Self = Token::Exp;
549    const EXPORT: Self = Token::Export;
550    const EXP_EQ: Self = Token::ExpEq;
551    const EXTENDS: Self = Token::Extends;
552    const FALSE: Self = Token::False;
553    const FINALLY: Self = Token::Finally;
554    const FOR: Self = Token::For;
555    const FROM: Self = Token::From;
556    const FUNCTION: Self = Self::Function;
557    const GET: Self = Token::Get;
558    const GLOBAL: Self = Token::Global;
559    const GREATER: Self = Token::Gt;
560    const GREATER_EQ: Self = Token::GtEq;
561    const HASH: Self = Self::Hash;
562    const IF: Self = Self::If;
563    const IMPLEMENTS: Self = Token::Implements;
564    const IMPORT: Self = Self::Import;
565    const IN: Self = Self::In;
566    const INFER: Self = Token::Infer;
567    const INSTANCEOF: Self = Token::InstanceOf;
568    const INTERFACE: Self = Token::Interface;
569    const INTRINSIC: Self = Token::Intrinsic;
570    const IS: Self = Token::Is;
571    const JSX_TAG_END: Self = Token::JSXTagEnd;
572    const JSX_TAG_START: Self = Token::JSXTagStart;
573    const KEYOF: Self = Token::Keyof;
574    const LBRACE: Self = Self::LBrace;
575    const LBRACKET: Self = Self::LBracket;
576    const LESS: Self = Token::Lt;
577    const LESS_EQ: Self = Token::LtEq;
578    const LET: Self = Token::Let;
579    const LOGICAL_AND: Self = Token::LogicalAnd;
580    const LOGICAL_AND_EQ: Self = Self::LogicalAndEq;
581    const LOGICAL_OR: Self = Token::LogicalOr;
582    const LOGICAL_OR_EQ: Self = Self::LogicalOrEq;
583    const LPAREN: Self = Self::LParen;
584    const LSHIFT: Self = Token::LShift;
585    const LSHIFT_EQ: Self = Token::LShiftEq;
586    const MINUS: Self = Self::Minus;
587    const MINUS_MINUS: Self = Self::MinusMinus;
588    const MOD: Self = Token::Percent;
589    const MOD_EQ: Self = Token::ModEq;
590    const MUL: Self = Token::Asterisk;
591    const MUL_EQ: Self = Token::MulEq;
592    const NAMESPACE: Self = Token::Namespace;
593    const NEVER: Self = Token::Never;
594    const NEW: Self = Self::New;
595    const NULL: Self = Token::Null;
596    const NULLISH_ASSIGN: Self = Token::NullishEq;
597    const NULLISH_COALESCING: Self = Token::NullishCoalescing;
598    const NUMBER: Self = Token::Number;
599    const OBJECT: Self = Token::Object;
600    const OF: Self = Token::Of;
601    const PACKAGE: Self = Token::Package;
602    const PLUS: Self = Self::Plus;
603    const PLUS_PLUS: Self = Self::PlusPlus;
604    const PRIVATE: Self = Token::Private;
605    const PROTECTED: Self = Token::Protected;
606    const PUBLIC: Self = Token::Public;
607    const QUESTION: Self = Token::QuestionMark;
608    const RBRACE: Self = Self::RBrace;
609    const RBRACKET: Self = Self::RBracket;
610    const READONLY: Self = Token::Readonly;
611    const REQUIRE: Self = Token::Require;
612    const RETURN: Self = Token::Return;
613    const RPAREN: Self = Self::RParen;
614    const RSHIFT: Self = Token::RShift;
615    const RSHIFT_EQ: Self = Token::RShiftEq;
616    const SATISFIES: Self = Token::Satisfies;
617    const SEMI: Self = Token::Semi;
618    const SET: Self = Token::Set;
619    const STATIC: Self = Token::Static;
620    const STRING: Self = Token::String;
621    const SUPER: Self = Self::Super;
622    const SWITCH: Self = Token::Switch;
623    const SYMBOL: Self = Token::Symbol;
624    const TARGET: Self = Token::Target;
625    const THIS: Self = Token::This;
626    const THROW: Self = Token::Throw;
627    const TILDE: Self = Self::Tilde;
628    const TRUE: Self = Token::True;
629    const TRY: Self = Token::Try;
630    const TYPE: Self = Token::Type;
631    const TYPEOF: Self = Self::TypeOf;
632    const UNDEFINED: Self = Token::Undefined;
633    const UNIQUE: Self = Token::Unique;
634    const UNKNOWN: Self = Token::Unknown;
635    const USING: Self = Self::Using;
636    const VAR: Self = Self::Var;
637    const VOID: Self = Self::Void;
638    const WHILE: Self = Token::While;
639    const WITH: Self = Token::With;
640    const YIELD: Self = Token::Yield;
641    const ZERO_FILL_RSHIFT: Self = Token::ZeroFillRShift;
642    const ZERO_FILL_RSHIFT_EQ: Self = Token::ZeroFillRShiftEq;
643
644    #[inline(always)]
645    fn jsx_name(name: &str, lexer: &mut crate::Lexer) -> Self {
646        let name = lexer.atoms.atom(name);
647        lexer.set_token_value(Some(TokenValue::Word(name)));
648        Token::JSXName
649    }
650
651    #[inline(always)]
652    fn is_jsx_name(&self) -> bool {
653        Token::JSXName.eq(self)
654    }
655
656    #[inline(always)]
657    fn take_jsx_name(self, buffer: &mut Self::Buffer) -> Atom {
658        buffer.expect_word_token_value()
659    }
660
661    #[inline(always)]
662    fn str(value: Atom, raw: Atom, lexer: &mut crate::Lexer<'a>) -> Self {
663        lexer.set_token_value(Some(TokenValue::Str { value, raw }));
664        Token::Str
665    }
666
667    #[inline(always)]
668    fn template(cooked: LexResult<Atom>, raw: Atom, lexer: &mut crate::Lexer<'a>) -> Self {
669        lexer.set_token_value(Some(TokenValue::Template { cooked, raw }));
670        Token::Template
671    }
672
673    #[inline(always)]
674    fn regexp(content: Atom, flags: Atom, lexer: &mut crate::Lexer<'a>) -> Self {
675        lexer.set_token_value(Some(TokenValue::Regex {
676            value: content,
677            flags,
678        }));
679        Token::Regex
680    }
681
682    #[inline(always)]
683    fn num(value: f64, raw: Atom, lexer: &mut crate::Lexer<'a>) -> Self {
684        lexer.set_token_value(Some(TokenValue::Num { value, raw }));
685        Self::Num
686    }
687
688    #[inline(always)]
689    fn bigint(value: Box<num_bigint::BigInt>, raw: Atom, lexer: &mut crate::Lexer<'a>) -> Self {
690        lexer.set_token_value(Some(TokenValue::BigInt { value, raw }));
691        Self::BigInt
692    }
693
694    #[inline(always)]
695    fn unknown_ident(value: Atom, lexer: &mut crate::Lexer<'a>) -> Self {
696        lexer.set_token_value(Some(TokenValue::Word(value)));
697        Token::Ident
698    }
699
700    #[inline(always)]
701    fn is_reserved(&self, ctx: swc_ecma_lexer::common::context::Context) -> bool {
702        self.is_reserved(ctx)
703    }
704
705    #[inline(always)]
706    fn into_atom(self, lexer: &mut crate::Lexer<'a>) -> Option<Atom> {
707        let value = lexer.get_token_value();
708        self.as_word_atom(value)
709    }
710
711    #[inline(always)]
712    fn is_error(&self) -> bool {
713        Token::Error.eq(self)
714    }
715
716    #[inline(always)]
717    fn take_error(self, buffer: &mut Self::Buffer) -> swc_ecma_lexer::error::Error {
718        buffer.expect_error_token_value()
719    }
720
721    #[inline(always)]
722    fn is_str(&self) -> bool {
723        Self::Str.eq(self)
724    }
725
726    #[inline(always)]
727    fn is_str_raw_content(&self, content: &str, buffer: &Self::Buffer) -> bool {
728        Self::Str.eq(self)
729            && if let Some(TokenValue::Str { raw, .. }) = buffer.get_token_value() {
730                raw == content
731            } else {
732                unreachable!()
733            }
734    }
735
736    #[inline(always)]
737    fn take_str(self, buffer: &mut Self::Buffer) -> (Atom, Atom) {
738        buffer.expect_string_token_value()
739    }
740
741    #[inline(always)]
742    fn is_num(&self) -> bool {
743        Self::Num.eq(self)
744    }
745
746    #[inline(always)]
747    fn take_num(self, buffer: &mut Self::Buffer) -> (f64, Atom) {
748        buffer.expect_number_token_value()
749    }
750
751    #[inline(always)]
752    fn is_bigint(&self) -> bool {
753        Self::BigInt.eq(self)
754    }
755
756    #[inline(always)]
757    fn take_bigint(self, buffer: &mut Self::Buffer) -> (Box<BigInt>, Atom) {
758        buffer.expect_bigint_token_value()
759    }
760
761    #[inline(always)]
762    fn is_word(&self) -> bool {
763        (*self).is_word()
764    }
765
766    #[inline]
767    fn take_word(self, buffer: &Self::Buffer) -> Option<Atom> {
768        self.as_word_atom(buffer.get_token_value())
769    }
770
771    #[inline(always)]
772    fn is_unknown_ident(&self) -> bool {
773        Token::Ident.eq(self)
774    }
775
776    #[inline(always)]
777    fn take_unknown_ident(self, buffer: &mut Self::Buffer) -> Atom {
778        buffer.expect_word_token_value()
779    }
780
781    #[inline(always)]
782    fn is_keyword(&self) -> bool {
783        Token::is_keyword(*self)
784    }
785
786    #[inline(always)]
787    fn is_known_ident(&self) -> bool {
788        Token::is_known_ident(*self)
789    }
790
791    #[inline(always)]
792    fn take_known_ident(&self) -> Atom {
793        self.as_known_ident_atom().unwrap()
794    }
795
796    #[inline(always)]
797    fn is_regexp(&self) -> bool {
798        Token::Regex.eq(self)
799    }
800
801    #[inline(always)]
802    fn take_unknown_ident_ref<'b>(&'b self, buffer: &'b Self::Buffer) -> &'b Atom {
803        buffer.expect_word_token_value_ref()
804    }
805
806    #[inline(always)]
807    fn is_template(&self) -> bool {
808        Token::Template.eq(self)
809    }
810
811    #[inline(always)]
812    fn take_template(self, buffer: &mut Self::Buffer) -> (LexResult<Atom>, Atom) {
813        buffer.expect_template_token_value()
814    }
815
816    #[inline(always)]
817    fn jsx_text(value: Atom, raw: Atom, lexer: &mut Self::Lexer) -> Self {
818        lexer.set_token_value(Some(TokenValue::Str { value, raw }));
819        Token::JSXText
820    }
821
822    #[inline(always)]
823    fn is_jsx_text(&self) -> bool {
824        Token::JSXText.eq(self)
825    }
826
827    #[inline(always)]
828    fn take_jsx_text(self, buffer: &mut Self::Buffer) -> (Atom, Atom) {
829        buffer.expect_string_token_value()
830    }
831
832    #[inline(always)]
833    fn starts_expr(&self) -> bool {
834        (*self).starts_expr()
835    }
836
837    #[inline(always)]
838    fn to_string(&self, buffer: &Self::Buffer) -> String {
839        (*self).to_string(buffer.get_token_value())
840    }
841
842    #[inline(always)]
843    fn is_bin_op(&self) -> bool {
844        (*self).is_bin_op()
845    }
846
847    #[inline(always)]
848    fn as_assign_op(&self) -> Option<AssignOp> {
849        (*self).as_assign_op()
850    }
851
852    #[inline(always)]
853    fn as_bin_op(&self) -> Option<swc_ecma_ast::BinaryOp> {
854        (*self).as_bin_op()
855    }
856
857    #[inline(always)]
858    fn follows_keyword_let(&self) -> bool {
859        (*self).follows_keyword_let()
860    }
861
862    #[inline(always)]
863    fn is_assign_op(&self) -> bool {
864        (*self).is_assign_op()
865    }
866
867    #[inline(always)]
868    fn take_regexp(self, buffer: &mut Self::Buffer) -> (Atom, Atom) {
869        buffer.expect_regex_token_value()
870    }
871
872    #[inline(always)]
873    fn shebang(value: Atom, lexer: &mut Self::Lexer) -> Self {
874        lexer.set_token_value(Some(TokenValue::Word(value)));
875        Token::Shebang
876    }
877
878    #[inline(always)]
879    fn is_shebang(&self) -> bool {
880        Token::Shebang.eq(self)
881    }
882
883    #[inline(always)]
884    fn take_shebang(self, buffer: &mut Self::Buffer) -> Atom {
885        buffer.expect_word_token_value()
886    }
887
888    #[inline(always)]
889    fn is_no_substitution_template_literal(&self) -> bool {
890        Token::NoSubstitutionTemplateLiteral.eq(self)
891    }
892
893    #[inline(always)]
894    fn is_template_head(&self) -> bool {
895        Token::TemplateHead.eq(self)
896    }
897}
898
899impl std::fmt::Debug for Token {
900    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
901        let s = match self {
902            Token::Str => "<string literal>",
903            Token::Num => "<number literal>",
904            Token::BigInt => "<bigint literal>",
905            Token::Regex => "<regexp literal>",
906            Token::Template | Token::NoSubstitutionTemplateLiteral => "<template literal>",
907            Token::TemplateHead => "<template head `...${ >",
908            Token::TemplateMiddle => "<template middle ...${ >",
909            Token::TemplateTail => "<template tail ` >",
910            Token::JSXName => "<jsx name>",
911            Token::JSXText => "<jsx text>",
912            Token::Ident => "<identifier>",
913            Token::Error => "<error>",
914            _ => &self.to_string(None),
915        };
916        f.write_str(s)
917    }
918}
919
920impl Token {
921    pub(crate) fn is_reserved(&self, ctx: Context) -> bool {
922        match self {
923            Token::Let | Token::Static => ctx.contains(Context::Strict),
924            Token::Await => {
925                ctx.contains(Context::InAsync)
926                    || ctx.contains(Context::InStaticBlock)
927                    || ctx.contains(Context::Strict)
928            }
929            Token::Yield => ctx.contains(Context::InGenerator) || ctx.contains(Context::Strict),
930
931            Token::Null
932            | Token::True
933            | Token::False
934            | Token::Break
935            | Token::Case
936            | Token::Catch
937            | Token::Continue
938            | Token::Debugger
939            | Token::Default
940            | Token::Do
941            | Token::Export
942            | Token::Else
943            | Token::Finally
944            | Token::For
945            | Token::Function
946            | Token::If
947            | Token::Return
948            | Token::Switch
949            | Token::Throw
950            | Token::Try
951            | Token::Var
952            | Token::Const
953            | Token::While
954            | Token::With
955            | Token::New
956            | Token::This
957            | Token::Super
958            | Token::Class
959            | Token::Extends
960            | Token::Import
961            | Token::In
962            | Token::InstanceOf
963            | Token::TypeOf
964            | Token::Void
965            | Token::Delete => true,
966
967            // Future reserved word
968            Token::Enum => true,
969
970            Token::Implements
971            | Token::Package
972            | Token::Protected
973            | Token::Interface
974            | Token::Private
975            | Token::Public
976                if ctx.contains(Context::Strict) =>
977            {
978                true
979            }
980
981            _ => false,
982        }
983    }
984
985    #[cold]
986    pub(crate) fn to_string(self, value: Option<&TokenValue>) -> String {
987        match self {
988            Token::LParen => "(",
989            Token::RParen => ")",
990            Token::LBrace => "{",
991            Token::RBrace => "}",
992            Token::LBracket => "[",
993            Token::RBracket => "]",
994            Token::Semi => ";",
995            Token::Comma => ",",
996            Token::Dot => ".",
997            Token::Colon => ":",
998            Token::QuestionMark => "?",
999            Token::Bang => "!",
1000            Token::Tilde => "~",
1001            Token::Plus => "+",
1002            Token::Minus => "-",
1003            Token::Asterisk => "*",
1004            Token::Slash => "/",
1005            Token::Percent => "%",
1006            Token::Lt => "<",
1007            Token::Gt => ">",
1008            Token::Pipe => "|",
1009            Token::Caret => "^",
1010            Token::Ampersand => "&",
1011            Token::Eq => "=",
1012            Token::At => "@",
1013            Token::Hash => "#",
1014            Token::BackQuote => "`",
1015            Token::Arrow => "=>",
1016            Token::DotDotDot => "...",
1017            Token::PlusPlus => "++",
1018            Token::MinusMinus => "--",
1019            Token::PlusEq => "+",
1020            Token::MinusEq => "-",
1021            Token::MulEq => "*",
1022            Token::DivEq => "/=",
1023            Token::ModEq => "%=",
1024            Token::LShiftEq => "<<=",
1025            Token::RShiftEq => ">>=",
1026            Token::ZeroFillRShiftEq => ">>>=",
1027            Token::BitOrEq => "|=",
1028            Token::BitXorEq => "^=",
1029            Token::BitAndEq => "&=",
1030            Token::ExpEq => "**=",
1031            Token::LogicalOrEq => "||=",
1032            Token::LogicalAndEq => "&&=",
1033            Token::NullishEq => "??=",
1034            Token::OptionalChain => "?.",
1035            Token::EqEq => "==",
1036            Token::NotEq => "!=",
1037            Token::EqEqEq => "===",
1038            Token::NotEqEq => "!==",
1039            Token::LtEq => "<=",
1040            Token::GtEq => ">=",
1041            Token::LShift => "<<",
1042            Token::RShift => ">>",
1043            Token::ZeroFillRShift => ">>>",
1044            Token::Exp => "**",
1045            Token::LogicalOr => "||",
1046            Token::LogicalAnd => "&&",
1047            Token::NullishCoalescing => "??",
1048            Token::DollarLBrace => "${",
1049            Token::JSXTagStart => "jsx tag start",
1050            Token::JSXTagEnd => "jsx tag end",
1051            Token::JSXText => {
1052                let Some(TokenValue::Str { raw, .. }) = value else {
1053                    unreachable!("{:#?}", value)
1054                };
1055                return format!("jsx text ({raw})");
1056            }
1057            Token::Str => {
1058                let Some(TokenValue::Str { value, raw, .. }) = value else {
1059                    unreachable!("{:#?}", value)
1060                };
1061                return format!("string literal ({value}, {raw})");
1062            }
1063            Token::Num => {
1064                let Some(TokenValue::Num { value, raw, .. }) = value else {
1065                    unreachable!("{:#?}", value)
1066                };
1067                return format!("numeric literal ({value}, {raw})");
1068            }
1069            Token::BigInt => {
1070                let Some(TokenValue::BigInt { value, raw, .. }) = value else {
1071                    unreachable!("{:#?}", value)
1072                };
1073                return format!("bigint literal ({value}, {raw})");
1074            }
1075            Token::Regex => {
1076                let Some(TokenValue::Regex { value, flags, .. }) = value else {
1077                    unreachable!("{:#?}", value)
1078                };
1079                return format!("regexp literal ({value}, {flags})");
1080            }
1081            Token::Template => {
1082                let Some(TokenValue::Template { raw, .. }) = value else {
1083                    unreachable!("{:#?}", value)
1084                };
1085                return format!("template token ({raw})");
1086            }
1087            Token::JSXName => {
1088                let Some(TokenValue::Word(w)) = value else {
1089                    unreachable!("{:#?}", value)
1090                };
1091                return format!("jsx name ({w})");
1092            }
1093            Token::Error => {
1094                let Some(TokenValue::Error(e)) = value else {
1095                    unreachable!("{:#?}", value)
1096                };
1097                return format!("<lexing error: {e:?}>");
1098            }
1099            Token::Ident => {
1100                let Some(TokenValue::Word(w)) = value else {
1101                    unreachable!("{:#?}", value)
1102                };
1103                w.as_ref()
1104            }
1105            Token::NoSubstitutionTemplateLiteral
1106            | Token::TemplateHead
1107            | Token::TemplateMiddle
1108            | Token::TemplateTail => {
1109                let Some(TokenValue::Template { raw, .. }) = value else {
1110                    unreachable!("{:#?}", value)
1111                };
1112                raw
1113            }
1114            Token::Await => "await",
1115            Token::Break => "break",
1116            Token::Case => "case",
1117            Token::Catch => "catch",
1118            Token::Class => "class",
1119            Token::Const => "const",
1120            Token::Continue => "continue",
1121            Token::Debugger => "debugger",
1122            Token::Default => "default",
1123            Token::Delete => "delete",
1124            Token::Do => "do",
1125            Token::Else => "else",
1126            Token::Export => "export",
1127            Token::Extends => "extends",
1128            Token::False => "false",
1129            Token::Finally => "finally",
1130            Token::For => "for",
1131            Token::Function => "function",
1132            Token::If => "if",
1133            Token::Import => "import",
1134            Token::In => "in",
1135            Token::InstanceOf => "instanceOf",
1136            Token::Let => "let",
1137            Token::New => "new",
1138            Token::Null => "null",
1139            Token::Return => "return",
1140            Token::Super => "super",
1141            Token::Switch => "switch",
1142            Token::This => "this",
1143            Token::Throw => "throw",
1144            Token::True => "true",
1145            Token::Try => "try",
1146            Token::TypeOf => "typeOf",
1147            Token::Var => "var",
1148            Token::Void => "void",
1149            Token::While => "while",
1150            Token::With => "with",
1151            Token::Yield => "yield",
1152            Token::Module => "module",
1153            Token::Abstract => "abstract",
1154            Token::Any => "any",
1155            Token::As => "as",
1156            Token::Asserts => "asserts",
1157            Token::Assert => "assert",
1158            Token::Async => "async",
1159            Token::Bigint => "bigint",
1160            Token::Boolean => "boolean",
1161            Token::Constructor => "constructor",
1162            Token::Declare => "declare",
1163            Token::Enum => "enum",
1164            Token::From => "from",
1165            Token::Get => "get",
1166            Token::Global => "global",
1167            Token::Implements => "implements",
1168            Token::Interface => "interface",
1169            Token::Intrinsic => "intrinsic",
1170            Token::Is => "is",
1171            Token::Keyof => "keyof",
1172            Token::Namespace => "namespace",
1173            Token::Never => "never",
1174            Token::Number => "number",
1175            Token::Object => "object",
1176            Token::Of => "of",
1177            Token::Out => "out",
1178            Token::Override => "override",
1179            Token::Package => "package",
1180            Token::Private => "private",
1181            Token::Protected => "protected",
1182            Token::Public => "public",
1183            Token::Readonly => "readonly",
1184            Token::Require => "require",
1185            Token::Set => "set",
1186            Token::Static => "static",
1187            Token::String => "string",
1188            Token::Symbol => "symbol",
1189            Token::Type => "type",
1190            Token::Undefined => "undefined",
1191            Token::Unique => "unique",
1192            Token::Unknown => "unknown",
1193            Token::Using => "using",
1194            Token::Accessor => "accessor",
1195            Token::Infer => "infer",
1196            Token::Satisfies => "satisfies",
1197            Token::Meta => "meta",
1198            Token::Target => "target",
1199            Token::Shebang => "#!",
1200            Token::LessSlash => "</",
1201            Token::Eof => "<eof>",
1202        }
1203        .to_string()
1204    }
1205
1206    pub(crate) fn as_keyword_atom(self) -> Option<Atom> {
1207        let atom = match self {
1208            Token::Await => atom!("await"),
1209            Token::Break => atom!("break"),
1210            Token::Case => atom!("case"),
1211            Token::Catch => atom!("catch"),
1212            Token::Class => atom!("class"),
1213            Token::Const => atom!("const"),
1214            Token::Continue => atom!("continue"),
1215            Token::Debugger => atom!("debugger"),
1216            Token::Default => atom!("default"),
1217            Token::Delete => atom!("delete"),
1218            Token::Do => atom!("do"),
1219            Token::Else => atom!("else"),
1220            Token::Export => atom!("export"),
1221            Token::Extends => atom!("extends"),
1222            Token::Finally => atom!("finally"),
1223            Token::For => atom!("for"),
1224            Token::Function => atom!("function"),
1225            Token::If => atom!("if"),
1226            Token::Import => atom!("import"),
1227            Token::In => atom!("in"),
1228            Token::InstanceOf => atom!("instanceof"),
1229            Token::Let => atom!("let"),
1230            Token::New => atom!("new"),
1231            Token::Return => atom!("return"),
1232            Token::Super => atom!("super"),
1233            Token::Switch => atom!("switch"),
1234            Token::This => atom!("this"),
1235            Token::Throw => atom!("throw"),
1236            Token::Try => atom!("try"),
1237            Token::TypeOf => atom!("typeof"),
1238            Token::Var => atom!("var"),
1239            Token::Void => atom!("void"),
1240            Token::While => atom!("while"),
1241            Token::With => atom!("with"),
1242            Token::Yield => atom!("yield"),
1243            Token::Module => atom!("module"),
1244            _ => return None,
1245        };
1246        Some(atom)
1247    }
1248
1249    pub(crate) const fn is_keyword(self) -> bool {
1250        let t = self as u8;
1251        t >= Token::Await as u8 && t <= Token::Module as u8
1252    }
1253
1254    pub(crate) fn as_known_ident_atom(self) -> Option<Atom> {
1255        let atom = match self {
1256            Token::Abstract => atom!("abstract"),
1257            Token::Any => atom!("any"),
1258            Token::As => atom!("as"),
1259            Token::Asserts => atom!("asserts"),
1260            Token::Assert => atom!("assert"),
1261            Token::Async => atom!("async"),
1262            Token::Bigint => atom!("bigint"),
1263            Token::Boolean => atom!("boolean"),
1264            Token::Constructor => atom!("constructor"),
1265            Token::Declare => atom!("declare"),
1266            Token::Enum => atom!("enum"),
1267            Token::From => atom!("from"),
1268            Token::Get => atom!("get"),
1269            Token::Global => atom!("global"),
1270            Token::Implements => atom!("implements"),
1271            Token::Interface => atom!("interface"),
1272            Token::Intrinsic => atom!("intrinsic"),
1273            Token::Is => atom!("is"),
1274            Token::Keyof => atom!("keyof"),
1275            Token::Namespace => atom!("namespace"),
1276            Token::Never => atom!("never"),
1277            Token::Number => atom!("number"),
1278            Token::Object => atom!("object"),
1279            Token::Of => atom!("of"),
1280            Token::Out => atom!("out"),
1281            Token::Override => atom!("override"),
1282            Token::Package => atom!("package"),
1283            Token::Private => atom!("private"),
1284            Token::Protected => atom!("protected"),
1285            Token::Public => atom!("public"),
1286            Token::Readonly => atom!("readonly"),
1287            Token::Require => atom!("require"),
1288            Token::Set => atom!("set"),
1289            Token::Static => atom!("static"),
1290            Token::String => atom!("string"),
1291            Token::Symbol => atom!("symbol"),
1292            Token::Type => atom!("type"),
1293            Token::Undefined => atom!("undefined"),
1294            Token::Unique => atom!("unique"),
1295            Token::Unknown => atom!("unknown"),
1296            Token::Using => atom!("using"),
1297            Token::Accessor => atom!("accessor"),
1298            Token::Infer => atom!("infer"),
1299            Token::Satisfies => atom!("satisfies"),
1300            Token::Meta => atom!("meta"),
1301            Token::Target => atom!("target"),
1302            _ => return None,
1303        };
1304        Some(atom)
1305    }
1306
1307    #[inline(always)]
1308    pub(crate) const fn is_known_ident(self) -> bool {
1309        let t = self as u8;
1310        t >= Token::Abstract as u8 && t <= Token::Target as u8
1311    }
1312
1313    pub(crate) const fn is_word(self) -> bool {
1314        matches!(
1315            self,
1316            Token::Null | Token::True | Token::False | Token::Ident
1317        ) || self.is_known_ident()
1318            || self.is_keyword()
1319    }
1320
1321    pub(crate) fn as_word_atom(self, value: Option<&TokenValue>) -> Option<Atom> {
1322        match self {
1323            Token::Null => Some(atom!("null")),
1324            Token::True => Some(atom!("true")),
1325            Token::False => Some(atom!("false")),
1326            Token::Ident => {
1327                let Some(TokenValue::Word(w)) = value else {
1328                    unreachable!("{:#?}", value)
1329                };
1330                Some(w.clone())
1331            }
1332            _ => self
1333                .as_known_ident_atom()
1334                .or_else(|| self.as_keyword_atom()),
1335        }
1336    }
1337
1338    #[inline(always)]
1339    pub(crate) const fn is_bin_op(self) -> bool {
1340        let t = self as u8;
1341        (t >= Token::EqEq as u8 && t <= Token::NullishCoalescing as u8)
1342            || (t >= Token::Plus as u8 && t <= Token::Ampersand as u8)
1343    }
1344
1345    pub(crate) fn as_bin_op(self) -> Option<swc_ecma_ast::BinaryOp> {
1346        match self {
1347            Token::EqEq => Some(swc_ecma_ast::BinaryOp::EqEq),
1348            Token::NotEq => Some(swc_ecma_ast::BinaryOp::NotEq),
1349            Token::EqEqEq => Some(swc_ecma_ast::BinaryOp::EqEqEq),
1350            Token::NotEqEq => Some(swc_ecma_ast::BinaryOp::NotEqEq),
1351            Token::Lt => Some(swc_ecma_ast::BinaryOp::Lt),
1352            Token::LtEq => Some(swc_ecma_ast::BinaryOp::LtEq),
1353            Token::Gt => Some(swc_ecma_ast::BinaryOp::Gt),
1354            Token::GtEq => Some(swc_ecma_ast::BinaryOp::GtEq),
1355            Token::LShift => Some(swc_ecma_ast::BinaryOp::LShift),
1356            Token::RShift => Some(swc_ecma_ast::BinaryOp::RShift),
1357            Token::ZeroFillRShift => Some(swc_ecma_ast::BinaryOp::ZeroFillRShift),
1358            Token::Plus => Some(swc_ecma_ast::BinaryOp::Add),
1359            Token::Minus => Some(swc_ecma_ast::BinaryOp::Sub),
1360            Token::Asterisk => Some(swc_ecma_ast::BinaryOp::Mul),
1361            Token::Slash => Some(swc_ecma_ast::BinaryOp::Div),
1362            Token::Percent => Some(swc_ecma_ast::BinaryOp::Mod),
1363            Token::Pipe => Some(swc_ecma_ast::BinaryOp::BitOr),
1364            Token::Caret => Some(swc_ecma_ast::BinaryOp::BitXor),
1365            Token::Ampersand => Some(swc_ecma_ast::BinaryOp::BitAnd),
1366            Token::LogicalOr => Some(swc_ecma_ast::BinaryOp::LogicalOr),
1367            Token::LogicalAnd => Some(swc_ecma_ast::BinaryOp::LogicalAnd),
1368            // Token::In => Some(swc_ecma_ast::BinaryOp::In),
1369            // Token::InstanceOf => Some(swc_ecma_ast::BinaryOp::InstanceOf),
1370            Token::Exp => Some(swc_ecma_ast::BinaryOp::Exp),
1371            Token::NullishCoalescing => Some(swc_ecma_ast::BinaryOp::NullishCoalescing),
1372            _ => None,
1373        }
1374    }
1375
1376    #[inline(always)]
1377    pub(crate) const fn is_assign_op(self) -> bool {
1378        let t = self as u8;
1379        matches!(self, Token::Eq) || (t >= Token::PlusEq as u8 && t <= Token::NullishEq as u8)
1380    }
1381
1382    pub(crate) fn as_assign_op(self) -> Option<swc_ecma_ast::AssignOp> {
1383        match self {
1384            Self::Eq => Some(AssignOp::Assign),
1385            Self::PlusEq => Some(AssignOp::AddAssign),
1386            Self::MinusEq => Some(AssignOp::SubAssign),
1387            Self::MulEq => Some(AssignOp::MulAssign),
1388            Self::DivEq => Some(AssignOp::DivAssign),
1389            Self::ModEq => Some(AssignOp::ModAssign),
1390            Self::LShiftEq => Some(AssignOp::LShiftAssign),
1391            Self::RShiftEq => Some(AssignOp::RShiftAssign),
1392            Self::ZeroFillRShiftEq => Some(AssignOp::ZeroFillRShiftAssign),
1393            Self::BitOrEq => Some(AssignOp::BitOrAssign),
1394            Self::BitXorEq => Some(AssignOp::BitXorAssign),
1395            Self::BitAndEq => Some(AssignOp::BitAndAssign),
1396            Self::ExpEq => Some(AssignOp::ExpAssign),
1397            Self::LogicalAndEq => Some(AssignOp::AndAssign),
1398            Self::LogicalOrEq => Some(AssignOp::OrAssign),
1399            Self::NullishEq => Some(AssignOp::NullishAssign),
1400            _ => None,
1401        }
1402    }
1403
1404    #[inline(always)]
1405    pub(crate) const fn before_expr(self) -> bool {
1406        match self {
1407            Self::Await
1408            | Self::Case
1409            | Self::Default
1410            | Self::Do
1411            | Self::Else
1412            | Self::Return
1413            | Self::Throw
1414            | Self::New
1415            | Self::Extends
1416            | Self::Yield
1417            | Self::In
1418            | Self::InstanceOf
1419            | Self::TypeOf
1420            | Self::Void
1421            | Self::Delete
1422            | Self::Arrow
1423            | Self::DotDotDot
1424            | Self::Bang
1425            | Self::LParen
1426            | Self::LBrace
1427            | Self::LBracket
1428            | Self::Semi
1429            | Self::Comma
1430            | Self::Colon
1431            | Self::DollarLBrace
1432            | Self::QuestionMark
1433            | Self::PlusPlus
1434            | Self::MinusMinus
1435            | Self::Tilde
1436            | Self::JSXText => true,
1437            _ => self.is_bin_op() || self.is_assign_op(),
1438        }
1439    }
1440
1441    #[inline(always)]
1442    pub(crate) const fn starts_expr(self) -> bool {
1443        matches!(
1444            self,
1445            Self::Ident
1446                | Self::JSXName
1447                | Self::Plus
1448                | Self::Minus
1449                | Self::Bang
1450                | Self::LParen
1451                | Self::LBrace
1452                | Self::LBracket
1453                | Self::TemplateHead
1454                | Self::NoSubstitutionTemplateLiteral
1455                | Self::DollarLBrace
1456                | Self::PlusPlus
1457                | Self::MinusMinus
1458                | Self::Tilde
1459                | Self::Str
1460                | Self::Regex
1461                | Self::Num
1462                | Self::BigInt
1463                | Self::JSXTagStart
1464                | Self::Await
1465                | Self::Function
1466                | Self::Throw
1467                | Self::New
1468                | Self::This
1469                | Self::Super
1470                | Self::Class
1471                | Self::Import
1472                | Self::Yield
1473                | Self::TypeOf
1474                | Self::Void
1475                | Self::Delete
1476                | Self::Null
1477                | Self::True
1478                | Self::False
1479                | Self::BackQuote
1480        ) || self.is_known_ident()
1481    }
1482
1483    pub(crate) fn follows_keyword_let(self) -> bool {
1484        match self {
1485            Token::Let
1486            | Token::LBrace
1487            | Token::LBracket
1488            | Token::Ident
1489            | Token::Yield
1490            | Token::Await => true,
1491            _ if self.is_known_ident() => true,
1492            _ => false,
1493        }
1494    }
1495
1496    pub(crate) fn should_rescan_into_gt_in_jsx(self) -> bool {
1497        matches!(
1498            self,
1499            Token::GtEq
1500                | Token::RShift
1501                | Token::RShiftEq
1502                | Token::ZeroFillRShift
1503                | Token::ZeroFillRShiftEq
1504        )
1505    }
1506}
1507
1508#[derive(Clone, Copy, Debug)]
1509pub struct TokenAndSpan {
1510    pub token: Token,
1511    /// Had a line break before this token?
1512    pub had_line_break: bool,
1513    pub span: Span,
1514}
1515
1516impl swc_ecma_lexer::common::parser::token_and_span::TokenAndSpan for TokenAndSpan {
1517    type Token = Token;
1518
1519    #[inline(always)]
1520    fn new(token: Token, span: Span, had_line_break: bool) -> Self {
1521        Self {
1522            token,
1523            had_line_break,
1524            span,
1525        }
1526    }
1527
1528    #[inline(always)]
1529    fn token(&self) -> &Token {
1530        &self.token
1531    }
1532
1533    #[inline(always)]
1534    fn take_token(self) -> Token {
1535        self.token
1536    }
1537
1538    #[inline(always)]
1539    fn had_line_break(&self) -> bool {
1540        self.had_line_break
1541    }
1542
1543    #[inline]
1544    fn span(&self) -> Span {
1545        self.span
1546    }
1547}
1548
1549#[derive(Clone)]
1550pub struct NextTokenAndSpan {
1551    pub token_and_span: TokenAndSpan,
1552    pub value: Option<TokenValue>,
1553}
1554
1555impl swc_ecma_lexer::common::parser::buffer::NextTokenAndSpan for NextTokenAndSpan {
1556    type Token = Token;
1557
1558    #[inline(always)]
1559    fn token(&self) -> &Self::Token {
1560        &self.token_and_span.token
1561    }
1562
1563    #[inline(always)]
1564    fn span(&self) -> Span {
1565        self.token_and_span.span
1566    }
1567
1568    #[inline(always)]
1569    fn had_line_break(&self) -> bool {
1570        self.token_and_span.had_line_break
1571    }
1572}