mirror of
https://github.com/swc-project/swc.git
synced 2024-10-05 12:49:21 +03:00
Make capturing fast (#570)
swc_ecma_parser: - made capturing fast (Closes #533)
This commit is contained in:
parent
8e3827403e
commit
02d4fb60f2
@ -1,7 +1,6 @@
|
||||
use swc_common;
|
||||
|
||||
use std::sync::Arc;
|
||||
use swc_common::{
|
||||
self,
|
||||
errors::{ColorConfig, Handler},
|
||||
FileName, SourceMap,
|
||||
};
|
||||
|
@ -5,7 +5,7 @@ use crate::{
|
||||
Context, JscTarget, Syntax,
|
||||
};
|
||||
use lexer::TokenContexts;
|
||||
use std::mem;
|
||||
use std::{cell::RefCell, mem, rc::Rc};
|
||||
use swc_common::{BytePos, Span, DUMMY_SP};
|
||||
|
||||
pub trait Tokens: Clone + Iterator<Item = TokenAndSpan> {
|
||||
@ -84,19 +84,19 @@ impl Tokens for TokensInput {
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Capturing<I: Tokens> {
|
||||
inner: I,
|
||||
captured: Vec<TokenAndSpan>,
|
||||
captured: Rc<RefCell<Vec<TokenAndSpan>>>,
|
||||
}
|
||||
|
||||
impl<I: Tokens> Capturing<I> {
|
||||
pub fn new(input: I) -> Self {
|
||||
Capturing {
|
||||
inner: input,
|
||||
captured: vec![],
|
||||
captured: Default::default(),
|
||||
}
|
||||
}
|
||||
/// Take captured tokens
|
||||
pub fn take(&mut self) -> Vec<TokenAndSpan> {
|
||||
mem::replace(&mut self.captured, vec![])
|
||||
mem::replace(&mut *self.captured.borrow_mut(), Default::default())
|
||||
}
|
||||
}
|
||||
|
||||
@ -106,7 +106,7 @@ impl<I: Tokens> Iterator for Capturing<I> {
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
let next = self.inner.next();
|
||||
|
||||
self.captured.extend(next.clone());
|
||||
self.captured.borrow_mut().extend(next.clone());
|
||||
next
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user