swc_ecma_lexer/
input.rs

1use std::{cell::RefCell, mem, mem::take, rc::Rc};
2
3use lexer::TokenContexts;
4use swc_common::{BytePos, Span};
5use swc_ecma_ast::EsVersion;
6
7use crate::{
8    common::{
9        input::Tokens,
10        lexer::token::TokenFactory,
11        parser::token_and_span::TokenAndSpan as TokenAndSpanTrait,
12        syntax::{Syntax, SyntaxFlags},
13    },
14    error::Error,
15    lexer::{self},
16    token::*,
17    Context,
18};
19
20#[derive(Clone)]
21pub struct TokensInput {
22    iter: <Vec<TokenAndSpan> as IntoIterator>::IntoIter,
23    ctx: Context,
24    syntax: SyntaxFlags,
25    start_pos: BytePos,
26    target: EsVersion,
27    token_ctx: TokenContexts,
28    errors: Rc<RefCell<Vec<Error>>>,
29    module_errors: Rc<RefCell<Vec<Error>>>,
30}
31
32impl TokensInput {
33    pub fn new(tokens: Vec<TokenAndSpan>, ctx: Context, syntax: Syntax, target: EsVersion) -> Self {
34        let start_pos = tokens.first().map(|t| t.span.lo).unwrap_or(BytePos(0));
35
36        TokensInput {
37            iter: tokens.into_iter(),
38            ctx,
39            syntax: syntax.into_flags(),
40            start_pos,
41            target,
42            token_ctx: Default::default(),
43            errors: Default::default(),
44            module_errors: Default::default(),
45        }
46    }
47}
48
49impl Iterator for TokensInput {
50    type Item = TokenAndSpan;
51
52    fn next(&mut self) -> Option<Self::Item> {
53        self.iter.next()
54    }
55}
56
57impl Tokens<TokenAndSpan> for TokensInput {
58    type Checkpoint = Self;
59
60    fn checkpoint_save(&self) -> Self::Checkpoint {
61        self.clone()
62    }
63
64    fn checkpoint_load(&mut self, checkpoint: Self::Checkpoint) {
65        *self = checkpoint;
66    }
67
68    fn set_ctx(&mut self, ctx: Context) {
69        if ctx.contains(Context::Module) && !self.module_errors.borrow().is_empty() {
70            let mut module_errors = self.module_errors.borrow_mut();
71            self.errors.borrow_mut().append(&mut *module_errors);
72        }
73        self.ctx = ctx;
74    }
75
76    fn ctx_mut(&mut self) -> &mut Context {
77        &mut self.ctx
78    }
79
80    #[inline(always)]
81    fn ctx(&self) -> Context {
82        self.ctx
83    }
84
85    #[inline(always)]
86    fn syntax(&self) -> SyntaxFlags {
87        self.syntax
88    }
89
90    #[inline(always)]
91    fn target(&self) -> EsVersion {
92        self.target
93    }
94
95    #[inline(always)]
96    fn start_pos(&self) -> BytePos {
97        self.start_pos
98    }
99
100    #[inline(always)]
101    fn set_expr_allowed(&mut self, _: bool) {}
102
103    #[inline(always)]
104    fn set_next_regexp(&mut self, _: Option<BytePos>) {}
105
106    #[inline(always)]
107    fn token_context(&self) -> &TokenContexts {
108        &self.token_ctx
109    }
110
111    #[inline(always)]
112    fn token_context_mut(&mut self) -> &mut TokenContexts {
113        &mut self.token_ctx
114    }
115
116    #[inline(always)]
117    fn set_token_context(&mut self, c: TokenContexts) {
118        self.token_ctx = c;
119    }
120
121    #[inline(always)]
122    fn add_error(&mut self, error: Error) {
123        self.errors.borrow_mut().push(error);
124    }
125
126    #[inline(always)]
127    fn add_module_mode_error(&mut self, error: Error) {
128        if self.ctx.contains(Context::Module) {
129            self.add_error(error);
130            return;
131        }
132        self.module_errors.borrow_mut().push(error);
133    }
134
135    #[inline(always)]
136    fn take_errors(&mut self) -> Vec<Error> {
137        take(&mut self.errors.borrow_mut())
138    }
139
140    #[inline(always)]
141    fn take_script_module_errors(&mut self) -> Vec<Error> {
142        take(&mut self.module_errors.borrow_mut())
143    }
144
145    fn end_pos(&self) -> BytePos {
146        self.iter
147            .as_slice()
148            .last()
149            .map(|t| t.span.hi)
150            .unwrap_or(self.start_pos)
151    }
152
153    #[inline]
154    fn update_token_flags(&mut self, _: impl FnOnce(&mut lexer::TokenFlags)) {
155        // TODO: Implement this method if needed.
156    }
157
158    fn token_flags(&self) -> lexer::TokenFlags {
159        Default::default()
160    }
161}
162
163/// Note: Lexer need access to parser's context to lex correctly.
164#[derive(Debug)]
165pub struct Capturing<I: Tokens<TokenAndSpan>> {
166    inner: I,
167    captured: Rc<RefCell<Vec<TokenAndSpan>>>,
168}
169
170impl<I: Tokens<TokenAndSpan>> Clone for Capturing<I> {
171    fn clone(&self) -> Self {
172        Capturing {
173            inner: self.inner.clone(),
174            captured: self.captured.clone(),
175        }
176    }
177}
178
179impl<I: Tokens<TokenAndSpan>> Capturing<I> {
180    pub fn new(input: I) -> Self {
181        Capturing {
182            inner: input,
183            captured: Default::default(),
184        }
185    }
186
187    pub fn tokens(&self) -> Rc<RefCell<Vec<TokenAndSpan>>> {
188        self.captured.clone()
189    }
190
191    /// Take captured tokens
192    pub fn take(&mut self) -> Vec<TokenAndSpan> {
193        mem::take(&mut *self.captured.borrow_mut())
194    }
195}
196
197impl<I: Tokens<TokenAndSpan>> Iterator for Capturing<I> {
198    type Item = TokenAndSpan;
199
200    fn next(&mut self) -> Option<Self::Item> {
201        let next = self.inner.next();
202
203        match next {
204            Some(ts) => {
205                let mut v = self.captured.borrow_mut();
206
207                // remove tokens that could change due to backtracing
208                while let Some(last) = v.last() {
209                    if last.span.lo >= ts.span.lo {
210                        v.pop();
211                    } else {
212                        break;
213                    }
214                }
215
216                v.push(ts.clone());
217
218                Some(ts)
219            }
220            None => None,
221        }
222    }
223}
224
225impl<I: Tokens<TokenAndSpan>> Tokens<TokenAndSpan> for Capturing<I> {
226    type Checkpoint = I::Checkpoint;
227
228    fn checkpoint_save(&self) -> Self::Checkpoint {
229        self.inner.checkpoint_save()
230    }
231
232    fn checkpoint_load(&mut self, checkpoint: Self::Checkpoint) {
233        self.inner.checkpoint_load(checkpoint);
234    }
235
236    #[inline(always)]
237    fn set_ctx(&mut self, ctx: Context) {
238        self.inner.set_ctx(ctx)
239    }
240
241    fn ctx_mut(&mut self) -> &mut Context {
242        self.inner.ctx_mut()
243    }
244
245    #[inline(always)]
246    fn ctx(&self) -> Context {
247        self.inner.ctx()
248    }
249
250    #[inline(always)]
251    fn syntax(&self) -> SyntaxFlags {
252        self.inner.syntax()
253    }
254
255    #[inline(always)]
256    fn target(&self) -> EsVersion {
257        self.inner.target()
258    }
259
260    #[inline(always)]
261    fn start_pos(&self) -> BytePos {
262        self.inner.start_pos()
263    }
264
265    #[inline(always)]
266    fn set_expr_allowed(&mut self, allow: bool) {
267        self.inner.set_expr_allowed(allow)
268    }
269
270    #[inline(always)]
271    fn set_next_regexp(&mut self, start: Option<BytePos>) {
272        self.inner.set_next_regexp(start);
273    }
274
275    #[inline(always)]
276    fn token_context(&self) -> &TokenContexts {
277        self.inner.token_context()
278    }
279
280    #[inline(always)]
281    fn token_context_mut(&mut self) -> &mut TokenContexts {
282        self.inner.token_context_mut()
283    }
284
285    #[inline(always)]
286    fn set_token_context(&mut self, c: TokenContexts) {
287        self.inner.set_token_context(c)
288    }
289
290    #[inline(always)]
291    fn add_error(&mut self, error: Error) {
292        self.inner.add_error(error);
293    }
294
295    #[inline(always)]
296    fn add_module_mode_error(&mut self, error: Error) {
297        self.inner.add_module_mode_error(error)
298    }
299
300    #[inline(always)]
301    fn take_errors(&mut self) -> Vec<Error> {
302        self.inner.take_errors()
303    }
304
305    fn take_script_module_errors(&mut self) -> Vec<Error> {
306        self.inner.take_script_module_errors()
307    }
308
309    fn end_pos(&self) -> BytePos {
310        self.inner.end_pos()
311    }
312
313    #[inline]
314    fn update_token_flags(&mut self, _: impl FnOnce(&mut lexer::TokenFlags)) {
315        // TODO: Implement this method if needed.
316    }
317
318    fn token_flags(&self) -> lexer::TokenFlags {
319        Default::default()
320    }
321}
322
323/// This struct is responsible for managing current token and peeked token.
324#[derive(Clone)]
325pub struct Buffer<I: Tokens<TokenAndSpan>> {
326    pub iter: I,
327    /// Span of the previous token.
328    pub prev_span: Span,
329    pub cur: TokenAndSpan,
330    /// Peeked token
331    pub next: Option<TokenAndSpan>,
332}
333
334impl<I: Tokens<TokenAndSpan>> Buffer<I> {
335    fn bump(&mut self) -> Token {
336        let next = if let Some(next) = self.next.take() {
337            next
338        } else if let Some(next) = self.iter.next() {
339            next
340        } else {
341            TokenAndSpan::new(Token::Eof, self.prev_span, true)
342        };
343        let prev = mem::replace(&mut self.cur, next);
344        self.prev_span = prev.span();
345        prev.token
346    }
347}
348
349impl<'a, I: Tokens<TokenAndSpan>> crate::common::parser::buffer::Buffer<'a> for Buffer<I> {
350    type I = I;
351    type Next = TokenAndSpan;
352    type Token = Token;
353    type TokenAndSpan = TokenAndSpan;
354
355    fn new(lexer: I) -> Self {
356        let start_pos = lexer.start_pos();
357        let prev_span = Span::new_with_checked(start_pos, start_pos);
358        Buffer {
359            iter: lexer,
360            cur: TokenAndSpan::new(Token::Eof, prev_span, false),
361            prev_span,
362            next: None,
363        }
364    }
365
366    #[inline(always)]
367    fn set_cur(&mut self, token: TokenAndSpan) {
368        self.cur = token;
369    }
370
371    #[inline(always)]
372    fn next(&self) -> Option<&TokenAndSpan> {
373        self.next.as_ref()
374    }
375
376    #[inline(always)]
377    fn set_next(&mut self, token: Option<TokenAndSpan>) {
378        self.next = token;
379    }
380
381    #[inline(always)]
382    fn next_mut(&mut self) -> &mut Option<TokenAndSpan> {
383        &mut self.next
384    }
385
386    #[inline]
387    fn cur(&self) -> &Token {
388        &self.cur.token
389    }
390
391    fn peek<'b>(&'b mut self) -> Option<&'b Token>
392    where
393        TokenAndSpan: 'b,
394    {
395        debug_assert!(
396            self.cur() != &Token::Eof,
397            "parser should not call peek() without knowing current token"
398        );
399
400        if self.next().is_none() {
401            let next = self.iter.next();
402            self.set_next(next);
403        }
404
405        self.next().map(|ts| &ts.token)
406    }
407
408    #[inline(always)]
409    fn get_cur(&self) -> &TokenAndSpan {
410        &self.cur
411    }
412
413    #[inline(always)]
414    fn prev_span(&self) -> Span {
415        self.prev_span
416    }
417
418    #[inline(always)]
419    fn iter(&self) -> &I {
420        &self.iter
421    }
422
423    #[inline(always)]
424    fn iter_mut(&mut self) -> &mut I {
425        &mut self.iter
426    }
427
428    fn bump(&mut self) {
429        self.bump();
430    }
431
432    fn expect_word_token_and_bump(&mut self) -> swc_atoms::Atom {
433        let t = self.bump();
434        t.take_word(self).unwrap()
435    }
436
437    fn expect_jsx_name_token_and_bump(&mut self) -> swc_atoms::Atom {
438        let t = self.bump();
439        t.take_jsx_name(self)
440    }
441
442    fn expect_jsx_text_token_and_bump(&mut self) -> (swc_atoms::Atom, swc_atoms::Atom) {
443        let t = self.bump();
444        t.take_jsx_text(self)
445    }
446
447    fn expect_number_token_and_bump(&mut self) -> (f64, swc_atoms::Atom) {
448        let t = self.bump();
449        t.take_num(self)
450    }
451
452    fn expect_string_token_and_bump(&mut self) -> (swc_atoms::Atom, swc_atoms::Atom) {
453        let t = self.bump();
454        t.take_str(self)
455    }
456
457    fn expect_bigint_token_and_bump(&mut self) -> (Box<num_bigint::BigInt>, swc_atoms::Atom) {
458        let t = self.bump();
459        t.take_bigint(self)
460    }
461
462    fn expect_regex_token_and_bump(&mut self) -> (swc_atoms::Atom, swc_atoms::Atom) {
463        let t = self.bump();
464        t.take_regexp(self)
465    }
466
467    fn expect_template_token_and_bump(
468        &mut self,
469    ) -> (
470        crate::common::lexer::LexResult<swc_atoms::Atom>,
471        swc_atoms::Atom,
472    ) {
473        let t = self.bump();
474        t.take_template(self)
475    }
476
477    fn expect_error_token_and_bump(&mut self) -> crate::error::Error {
478        let t = self.bump();
479        t.take_error(self)
480    }
481
482    fn expect_shebang_token_and_bump(&mut self) -> swc_atoms::Atom {
483        let t = self.bump();
484        t.take_shebang(self)
485    }
486
487    #[cold]
488    #[inline(never)]
489    fn dump_cur(&self) -> String {
490        let cur = self.cur();
491        format!("{cur:?}")
492    }
493}