swc_ecma_lexer/common/lexer/
token.rs

1use num_bigint::BigInt;
2use swc_atoms::Atom;
3use swc_ecma_ast::{AssignOp, BinaryOp};
4
5use super::LexResult;
6use crate::common::{context::Context, input::Tokens};
7
8pub trait TokenFactory<'a, TokenAndSpan, I: Tokens<TokenAndSpan>>: Sized + PartialEq {
9    type Lexer: super::Lexer<'a, TokenAndSpan>;
10    type Buffer: crate::common::parser::buffer::Buffer<
11        'a,
12        I = I,
13        Token = Self,
14        TokenAndSpan = TokenAndSpan,
15    >;
16
17    const FROM: Self;
18    const FOR: Self;
19    const INSTANCEOF: Self;
20    const SATISFIES: Self;
21    const THROW: Self;
22    const AS: Self;
23    const NAMESPACE: Self;
24    const RETURN: Self;
25    const AT: Self;
26    const EXPORT: Self;
27    const DECLARE: Self;
28    const ASSERTS: Self;
29    const ASSERT: Self;
30    const JSX_TAG_END: Self;
31    const JSX_TAG_START: Self;
32    const DOLLAR_LBRACE: Self;
33    const BACKQUOTE: Self;
34    const HASH: Self;
35    const IN: Self;
36    const IS: Self;
37    const CONST: Self;
38    const DOT: Self;
39    const TARGET: Self;
40    const GET: Self;
41    const SET: Self;
42    const DOTDOTDOT: Self;
43    const NULLISH_ASSIGN: Self;
44    const NULLISH_COALESCING: Self;
45    const QUESTION: Self;
46    const COLON: Self;
47    const COMMA: Self;
48    const BIT_AND: Self;
49    const BIT_AND_EQ: Self;
50    const BIT_OR: Self;
51    const BIT_OR_EQ: Self;
52    const LOGICAL_AND: Self;
53    const LOGICAL_AND_EQ: Self;
54    const LOGICAL_OR: Self;
55    const LOGICAL_OR_EQ: Self;
56    const MUL: Self;
57    const MUL_EQ: Self;
58    const MOD: Self;
59    const MOD_EQ: Self;
60    const EXP: Self;
61    const EXP_EQ: Self;
62    const DIV: Self;
63    const DIV_EQ: Self;
64    const EQUAL: Self;
65    const LSHIFT: Self;
66    const LSHIFT_EQ: Self;
67    const LESS: Self;
68    const GLOBAL: Self;
69    const LESS_EQ: Self;
70    const RSHIFT: Self;
71    const RSHIFT_EQ: Self;
72    const GREATER: Self;
73    const GREATER_EQ: Self;
74    const ZERO_FILL_RSHIFT: Self;
75    const ZERO_FILL_RSHIFT_EQ: Self;
76    const NULL: Self;
77    const ANY: Self;
78    const BOOLEAN: Self;
79    const BIGINT: Self;
80    const NEVER: Self;
81    const NUMBER: Self;
82    const OBJECT: Self;
83    const STRING: Self;
84    const SYMBOL: Self;
85    const UNKNOWN: Self;
86    const UNDEFINED: Self;
87    const INTRINSIC: Self;
88    const TRUE: Self;
89    const TRY: Self;
90    const FALSE: Self;
91    const ENUM: Self;
92    const YIELD: Self;
93    const LET: Self;
94    const VAR: Self;
95    const STATIC: Self;
96    const IMPLEMENTS: Self;
97    const INTERFACE: Self;
98    const TYPE: Self;
99    const PACKAGE: Self;
100    const PRIVATE: Self;
101    const PROTECTED: Self;
102    const PUBLIC: Self;
103    const READONLY: Self;
104    const ARROW: Self;
105    const REQUIRE: Self;
106    const AWAIT: Self;
107    const BREAK: Self;
108    const CONTINUE: Self;
109    const THIS: Self;
110    const SUPER: Self;
111    const WHILE: Self;
112    const DO: Self;
113    const LPAREN: Self;
114    const RPAREN: Self;
115    const LBRACKET: Self;
116    const RBRACKET: Self;
117    const LBRACE: Self;
118    const FINALLY: Self;
119    const CATCH: Self;
120    const SWITCH: Self;
121    const RBRACE: Self;
122    const FUNCTION: Self;
123    const IF: Self;
124    const ELSE: Self;
125    const CLASS: Self;
126    const NEW: Self;
127    const ABSTRACT: Self;
128    const ACCESSOR: Self;
129    const IMPORT: Self;
130    const PLUS: Self;
131    const MINUS: Self;
132    const BANG: Self;
133    const TILDE: Self;
134    const PLUS_PLUS: Self;
135    const MINUS_MINUS: Self;
136    const DELETE: Self;
137    const TYPEOF: Self;
138    const VOID: Self;
139    const EXTENDS: Self;
140    const SEMI: Self;
141    const OF: Self;
142    const KEYOF: Self;
143    const UNIQUE: Self;
144    const INFER: Self;
145    const USING: Self;
146    const WITH: Self;
147    const ASYNC: Self;
148    const CASE: Self;
149    const DEFAULT: Self;
150    const DEBUGGER: Self;
151    const EOF: Self;
152
153    fn jsx_name(name: &'a str, lexer: &mut Self::Lexer) -> Self;
154    fn is_jsx_name(&self) -> bool;
155    fn take_jsx_name(self, buffer: &mut Self::Buffer) -> Atom;
156
157    fn str(value: Atom, raw: Atom, lexer: &mut Self::Lexer) -> Self;
158    fn is_str(&self) -> bool;
159    fn is_str_raw_content(&self, content: &str, buffer: &Self::Buffer) -> bool;
160    fn take_str(self, buffer: &mut Self::Buffer) -> (Atom, Atom);
161
162    fn template(cooked: LexResult<Atom>, raw: Atom, lexer: &mut Self::Lexer) -> Self;
163    fn is_template(&self) -> bool;
164    fn take_template(self, buffer: &mut Self::Buffer) -> (LexResult<Atom>, Atom);
165
166    fn jsx_text(value: Atom, raw: Atom, lexer: &mut Self::Lexer) -> Self;
167    fn is_jsx_text(&self) -> bool;
168    fn take_jsx_text(self, buffer: &mut Self::Buffer) -> (Atom, Atom);
169
170    fn regexp(content: Atom, flags: Atom, lexer: &mut Self::Lexer) -> Self;
171    fn is_regexp(&self) -> bool;
172    fn take_regexp(self, buffer: &mut Self::Buffer) -> (Atom, Atom);
173
174    fn num(value: f64, raw: Atom, lexer: &mut Self::Lexer) -> Self;
175    fn is_num(&self) -> bool;
176    fn take_num(self, buffer: &mut Self::Buffer) -> (f64, Atom);
177
178    fn bigint(value: Box<BigInt>, raw: Atom, lexer: &mut Self::Lexer) -> Self;
179    fn is_bigint(&self) -> bool;
180    fn take_bigint(self, buffer: &mut Self::Buffer) -> (Box<BigInt>, Atom);
181
182    fn shebang(value: Atom, lexer: &mut Self::Lexer) -> Self;
183    fn is_shebang(&self) -> bool;
184    fn take_shebang(self, buffer: &mut Self::Buffer) -> Atom;
185
186    fn unknown_ident(value: Atom, lexer: &mut Self::Lexer) -> Self;
187    fn is_unknown_ident(&self) -> bool;
188    fn take_unknown_ident(self, buffer: &mut Self::Buffer) -> Atom;
189    fn take_unknown_ident_ref<'b>(&'b self, buffer: &'b Self::Buffer) -> &'b Atom;
190
191    fn is_known_ident(&self) -> bool;
192    fn take_known_ident(&self) -> Atom;
193
194    fn starts_expr(&self) -> bool;
195    fn to_string(&self, buffer: &Self::Buffer) -> String;
196
197    fn is_error(&self) -> bool;
198    fn take_error(self, buffer: &mut Self::Buffer) -> crate::error::Error;
199
200    fn is_word(&self) -> bool;
201    fn take_word(self, buffer: &Self::Buffer) -> Option<Atom>;
202    fn is_keyword(&self) -> bool;
203
204    fn is_reserved(&self, ctx: super::Context) -> bool;
205    fn into_atom(self, lexer: &mut Self::Lexer) -> Option<Atom>;
206    fn follows_keyword_let(&self) -> bool;
207
208    fn is_bin_op(&self) -> bool;
209    fn as_bin_op(&self) -> Option<BinaryOp>;
210
211    fn is_assign_op(&self) -> bool;
212    fn as_assign_op(&self) -> Option<AssignOp>;
213
214    #[inline(always)]
215    fn is_less(&self) -> bool {
216        Self::LESS.eq(self)
217    }
218    #[inline(always)]
219    fn is_less_eq(&self) -> bool {
220        Self::LESS_EQ.eq(self)
221    }
222    #[inline(always)]
223    fn is_greater(&self) -> bool {
224        Self::GREATER.eq(self)
225    }
226    #[inline(always)]
227    fn is_colon(&self) -> bool {
228        Self::COLON.eq(self)
229    }
230    #[inline(always)]
231    fn is_comma(&self) -> bool {
232        Self::COMMA.eq(self)
233    }
234    #[inline(always)]
235    fn is_equal(&self) -> bool {
236        Self::EQUAL.eq(self)
237    }
238    #[inline(always)]
239    fn is_question(&self) -> bool {
240        Self::QUESTION.eq(self)
241    }
242    #[inline(always)]
243    fn is_null(&self) -> bool {
244        Self::NULL.eq(self)
245    }
246    #[inline(always)]
247    fn is_lshift(&self) -> bool {
248        Self::LSHIFT.eq(self)
249    }
250    #[inline(always)]
251    fn is_rshift(&self) -> bool {
252        Self::RSHIFT.eq(self)
253    }
254    #[inline(always)]
255    fn is_rshift_eq(&self) -> bool {
256        Self::RSHIFT_EQ.eq(self)
257    }
258    #[inline(always)]
259    fn is_greater_eq(&self) -> bool {
260        Self::GREATER_EQ.eq(self)
261    }
262    #[inline(always)]
263    fn is_true(&self) -> bool {
264        Self::TRUE.eq(self)
265    }
266    #[inline(always)]
267    fn is_false(&self) -> bool {
268        Self::FALSE.eq(self)
269    }
270    #[inline(always)]
271    fn is_enum(&self) -> bool {
272        Self::ENUM.eq(self)
273    }
274    #[inline(always)]
275    fn is_yield(&self) -> bool {
276        Self::YIELD.eq(self)
277    }
278    #[inline(always)]
279    fn is_let(&self) -> bool {
280        Self::LET.eq(self)
281    }
282    #[inline(always)]
283    fn is_var(&self) -> bool {
284        Self::VAR.eq(self)
285    }
286    #[inline(always)]
287    fn is_static(&self) -> bool {
288        Self::STATIC.eq(self)
289    }
290    #[inline(always)]
291    fn is_extends(&self) -> bool {
292        Self::EXTENDS.eq(self)
293    }
294    #[inline(always)]
295    fn is_implements(&self) -> bool {
296        Self::IMPLEMENTS.eq(self)
297    }
298    #[inline(always)]
299    fn is_interface(&self) -> bool {
300        Self::INTERFACE.eq(self)
301    }
302    #[inline(always)]
303    fn is_type(&self) -> bool {
304        Self::TYPE.eq(self)
305    }
306    #[inline(always)]
307    fn is_package(&self) -> bool {
308        Self::PACKAGE.eq(self)
309    }
310    #[inline(always)]
311    fn is_private(&self) -> bool {
312        Self::PRIVATE.eq(self)
313    }
314    #[inline(always)]
315    fn is_protected(&self) -> bool {
316        Self::PROTECTED.eq(self)
317    }
318    #[inline(always)]
319    fn is_public(&self) -> bool {
320        Self::PUBLIC.eq(self)
321    }
322    #[inline(always)]
323    fn is_readonly(&self) -> bool {
324        Self::READONLY.eq(self)
325    }
326    #[inline(always)]
327    fn is_await(&self) -> bool {
328        Self::AWAIT.eq(self)
329    }
330    #[inline(always)]
331    fn is_break(&self) -> bool {
332        Self::BREAK.eq(self)
333    }
334    #[inline(always)]
335    fn is_continue(&self) -> bool {
336        Self::CONTINUE.eq(self)
337    }
338    #[inline(always)]
339    fn is_arrow(&self) -> bool {
340        Self::ARROW.eq(self)
341    }
342    #[inline(always)]
343    fn is_this(&self) -> bool {
344        Self::THIS.eq(self)
345    }
346    #[inline(always)]
347    fn is_super(&self) -> bool {
348        Self::SUPER.eq(self)
349    }
350    #[inline(always)]
351    fn is_using(&self) -> bool {
352        Self::USING.eq(self)
353    }
354    #[inline(always)]
355    fn is_backquote(&self) -> bool {
356        Self::BACKQUOTE.eq(self)
357    }
358    #[inline(always)]
359    fn is_lparen(&self) -> bool {
360        Self::LPAREN.eq(self)
361    }
362    #[inline(always)]
363    fn is_rparen(&self) -> bool {
364        Self::RPAREN.eq(self)
365    }
366    #[inline(always)]
367    fn is_lbracket(&self) -> bool {
368        Self::LBRACKET.eq(self)
369    }
370    #[inline(always)]
371    fn is_rbracket(&self) -> bool {
372        Self::RBRACKET.eq(self)
373    }
374    #[inline(always)]
375    fn is_lbrace(&self) -> bool {
376        Self::LBRACE.eq(self)
377    }
378    #[inline(always)]
379    fn is_rbrace(&self) -> bool {
380        Self::RBRACE.eq(self)
381    }
382    #[inline(always)]
383    fn is_function(&self) -> bool {
384        Self::FUNCTION.eq(self)
385    }
386    #[inline(always)]
387    fn is_class(&self) -> bool {
388        Self::CLASS.eq(self)
389    }
390    #[inline(always)]
391    fn is_if(&self) -> bool {
392        Self::IF.eq(self)
393    }
394    #[inline(always)]
395    fn is_return(&self) -> bool {
396        Self::RETURN.eq(self)
397    }
398    #[inline(always)]
399    fn is_switch(&self) -> bool {
400        Self::SWITCH.eq(self)
401    }
402    #[inline(always)]
403    fn is_throw(&self) -> bool {
404        Self::THROW.eq(self)
405    }
406    #[inline(always)]
407    fn is_catch(&self) -> bool {
408        Self::CATCH.eq(self)
409    }
410    #[inline(always)]
411    fn is_finally(&self) -> bool {
412        Self::FINALLY.eq(self)
413    }
414    #[inline(always)]
415    fn is_try(&self) -> bool {
416        Self::TRY.eq(self)
417    }
418    #[inline(always)]
419    fn is_with(&self) -> bool {
420        Self::WITH.eq(self)
421    }
422    #[inline(always)]
423    fn is_while(&self) -> bool {
424        Self::WHILE.eq(self)
425    }
426    #[inline(always)]
427    fn is_new(&self) -> bool {
428        Self::NEW.eq(self)
429    }
430    #[inline(always)]
431    fn is_ident_ref(&self, ctx: Context) -> bool {
432        self.is_word() && !self.is_reserved(ctx)
433    }
434    #[inline(always)]
435    fn is_import(&self) -> bool {
436        Self::IMPORT.eq(self)
437    }
438    #[inline(always)]
439    fn is_export(&self) -> bool {
440        Self::EXPORT.eq(self)
441    }
442    #[inline(always)]
443    fn is_dot(&self) -> bool {
444        Self::DOT.eq(self)
445    }
446    #[inline(always)]
447    fn is_do(&self) -> bool {
448        Self::DO.eq(self)
449    }
450    #[inline(always)]
451    fn is_for(&self) -> bool {
452        Self::FOR.eq(self)
453    }
454    #[inline(always)]
455    fn is_from(&self) -> bool {
456        Self::FROM.eq(self)
457    }
458    #[inline(always)]
459    fn is_dotdotdot(&self) -> bool {
460        Self::DOTDOTDOT.eq(self)
461    }
462    #[inline(always)]
463    fn is_plus(&self) -> bool {
464        Self::PLUS.eq(self)
465    }
466    #[inline(always)]
467    fn is_minus(&self) -> bool {
468        Self::MINUS.eq(self)
469    }
470    #[inline(always)]
471    fn is_bang(&self) -> bool {
472        Self::BANG.eq(self)
473    }
474    #[inline(always)]
475    fn is_tilde(&self) -> bool {
476        Self::TILDE.eq(self)
477    }
478    #[inline(always)]
479    fn is_plus_plus(&self) -> bool {
480        Self::PLUS_PLUS.eq(self)
481    }
482    #[inline(always)]
483    fn is_minus_minus(&self) -> bool {
484        Self::MINUS_MINUS.eq(self)
485    }
486    #[inline(always)]
487    fn is_delete(&self) -> bool {
488        Self::DELETE.eq(self)
489    }
490    #[inline(always)]
491    fn is_typeof(&self) -> bool {
492        Self::TYPEOF.eq(self)
493    }
494    #[inline(always)]
495    fn is_of(&self) -> bool {
496        Self::OF.eq(self)
497    }
498    #[inline(always)]
499    fn is_void(&self) -> bool {
500        Self::VOID.eq(self)
501    }
502    #[inline(always)]
503    fn is_hash(&self) -> bool {
504        Self::HASH.eq(self)
505    }
506    #[inline(always)]
507    fn is_in(&self) -> bool {
508        Self::IN.eq(self)
509    }
510    #[inline(always)]
511    fn is_const(&self) -> bool {
512        Self::CONST.eq(self)
513    }
514    #[inline(always)]
515    fn is_star(&self) -> bool {
516        Self::MUL.eq(self)
517    }
518    #[inline(always)]
519    fn is_mod(&self) -> bool {
520        Self::MOD.eq(self)
521    }
522    #[inline(always)]
523    fn is_semi(&self) -> bool {
524        Self::SEMI.eq(self)
525    }
526    #[inline(always)]
527    fn is_slash(&self) -> bool {
528        Self::DIV.eq(self)
529    }
530    #[inline(always)]
531    fn is_slash_eq(&self) -> bool {
532        Self::DIV_EQ.eq(self)
533    }
534    #[inline(always)]
535    fn is_jsx_tag_start(&self) -> bool {
536        Self::JSX_TAG_START.eq(self)
537    }
538    #[inline(always)]
539    fn is_jsx_tag_end(&self) -> bool {
540        Self::JSX_TAG_END.eq(self)
541    }
542    #[inline(always)]
543    fn is_asserts(&self) -> bool {
544        Self::ASSERTS.eq(self)
545    }
546    #[inline(always)]
547    fn is_is(&self) -> bool {
548        Self::IS.eq(self)
549    }
550    #[inline(always)]
551    fn is_as(&self) -> bool {
552        Self::AS.eq(self)
553    }
554    #[inline(always)]
555    fn is_satisfies(&self) -> bool {
556        Self::SATISFIES.eq(self)
557    }
558    #[inline(always)]
559    fn is_instanceof(&self) -> bool {
560        Self::INSTANCEOF.eq(self)
561    }
562    #[inline(always)]
563    fn is_async(&self) -> bool {
564        Self::ASYNC.eq(self)
565    }
566    #[inline(always)]
567    fn is_case(&self) -> bool {
568        Self::CASE.eq(self)
569    }
570    #[inline(always)]
571    fn is_default(&self) -> bool {
572        Self::DEFAULT.eq(self)
573    }
574    #[inline(always)]
575    fn is_debugger(&self) -> bool {
576        Self::DEBUGGER.eq(self)
577    }
578    #[inline(always)]
579    fn is_bit_and(&self) -> bool {
580        Self::BIT_AND.eq(self)
581    }
582    #[inline(always)]
583    fn is_bit_or(&self) -> bool {
584        Self::BIT_OR.eq(self)
585    }
586    #[inline(always)]
587    fn is_exp(&self) -> bool {
588        Self::EXP.eq(self)
589    }
590    #[inline(always)]
591    fn is_eof(&self) -> bool {
592        Self::EOF.eq(self)
593    }
594    fn is_no_substitution_template_literal(&self) -> bool;
595    fn is_template_head(&self) -> bool;
596}