swc_ecma_lexer/common/parser/
state.rs

1use std::ops::{Deref, DerefMut};
2
3use rustc_hash::FxHashMap;
4use swc_atoms::Atom;
5use swc_common::{BytePos, Span};
6
7#[derive(Clone, Default)]
8pub struct State {
9    pub labels: Vec<Atom>,
10    /// Start position of an assignment expression.
11    pub potential_arrow_start: Option<BytePos>,
12    /// Start position of an AST node and the span of its trailing comma.
13    pub trailing_commas: FxHashMap<BytePos, Span>,
14}
15
16pub struct WithState<'a, 'w, Parser: super::Parser<'a>> {
17    pub(super) inner: &'w mut Parser,
18    pub(super) orig_state: crate::common::parser::state::State,
19    pub(super) marker: std::marker::PhantomData<&'a ()>,
20}
21
22impl<'a, Parser: super::Parser<'a>> Deref for WithState<'a, '_, Parser> {
23    type Target = Parser;
24
25    fn deref(&self) -> &Parser {
26        self.inner
27    }
28}
29impl<'a, Parser: super::Parser<'a>> DerefMut for WithState<'a, '_, Parser> {
30    fn deref_mut(&mut self) -> &mut Parser {
31        self.inner
32    }
33}
34
35impl<'a, Parser: super::Parser<'a>> Drop for WithState<'a, '_, Parser> {
36    fn drop(&mut self) {
37        std::mem::swap(self.inner.state_mut(), &mut self.orig_state);
38    }
39}