Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(es/parser): Remove unused raw: Raw params #9048

Merged
merged 1 commit into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 12 additions & 47 deletions crates/swc_ecma_parser/src/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,11 +429,7 @@ impl<'a> Lexer<'a> {
/// Read an escaped character for string literal.
///
/// In template literal, we should preserve raw string.
fn read_escaped_char(
&mut self,
raw: &mut Raw,
in_template: bool,
) -> LexResult<Option<Vec<Char>>> {
fn read_escaped_char(&mut self, in_template: bool) -> LexResult<Option<Vec<Char>>> {
debug_assert_eq!(self.cur(), Some('\\'));

let start = self.cur_pos();
Expand All @@ -447,7 +443,6 @@ impl<'a> Lexer<'a> {

macro_rules! push_c_and_ret {
($c:expr) => {{
raw.push(c);
$c
}};
}
Expand All @@ -461,35 +456,23 @@ impl<'a> Lexer<'a> {
'v' => push_c_and_ret!('\u{000b}'),
'f' => push_c_and_ret!('\u{000c}'),
'\r' => {
raw.push_str("\r");

self.bump(); // remove '\r'

if self.eat(b'\n') {
raw.push_str("\n");
}
self.eat(b'\n');

return Ok(None);
}
'\n' | '\u{2028}' | '\u{2029}' => {
match c {
'\n' => raw.push_str("\n"),
'\u{2028}' => raw.push_str("\u{2028}"),
'\u{2029}' => raw.push_str("\u{2029}"),
_ => unreachable!(),
}
self.bump();

return Ok(None);
}

// read hexadecimal escape sequences
'x' => {
raw.push_str("x");

self.bump(); // 'x'

match self.read_int_u32::<16>(2, raw)? {
match self.read_int_u32::<16>(2, &mut Raw(None))? {
Some(val) => return Ok(Some(vec![Char::from(val)])),
None => self.error(
start,
Expand All @@ -501,15 +484,13 @@ impl<'a> Lexer<'a> {
}

// read unicode escape sequences
'u' => match self.read_unicode_escape(raw) {
'u' => match self.read_unicode_escape() {
Ok(chars) => return Ok(Some(chars)),
Err(err) => self.error(start, err.into_kind())?,
},

// octal escape sequences
'0'..='7' => {
raw.push(c);

self.bump();

let first_c = if c == '0' {
Expand Down Expand Up @@ -550,7 +531,6 @@ impl<'a> Lexer<'a> {
};

self.bump();
raw.push(cur.unwrap());
}
_ => return Ok(Some(vec![Char::from(value as u32)])),
}
Expand All @@ -562,10 +542,7 @@ impl<'a> Lexer<'a> {

return Ok(Some(vec![Char::from(value as char)]));
}
_ => {
raw.push(c);
c
}
_ => c,
};

unsafe {
Expand Down Expand Up @@ -860,7 +837,7 @@ impl<'a> Lexer<'a> {

has_escape = true;

let chars = l.read_unicode_escape(&mut Raw(None))?;
let chars = l.read_unicode_escape()?;

if let Some(c) = chars.first() {
let valid = if first {
Expand Down Expand Up @@ -890,24 +867,20 @@ impl<'a> Lexer<'a> {
})
}

fn read_unicode_escape(&mut self, raw: &mut Raw) -> LexResult<Vec<Char>> {
fn read_unicode_escape(&mut self) -> LexResult<Vec<Char>> {
debug_assert_eq!(self.cur(), Some('u'));

let mut chars = vec![];
let mut is_curly = false;

self.bump(); // 'u'

raw.push_str("u");

if self.eat(b'{') {
is_curly = true;

raw.push('{');
}

let state = self.input.cur_pos();
let c = match self.read_int_u32::<16>(if is_curly { 0 } else { 4 }, raw) {
let c = match self.read_int_u32::<16>(if is_curly { 0 } else { 4 }, &mut Raw(None)) {
Ok(Some(val)) => {
if 0x0010_ffff >= val {
char::from_u32(val)
Expand Down Expand Up @@ -985,12 +958,8 @@ impl<'a> Lexer<'a> {
}
}

if is_curly {
if !self.eat(b'}') {
self.error(state, SyntaxError::InvalidUnicodeEscape)?
}

raw.push('}');
if is_curly && !self.eat(b'}') {
self.error(state, SyntaxError::InvalidUnicodeEscape)?
}

Ok(chars)
Expand Down Expand Up @@ -1033,9 +1002,7 @@ impl<'a> Lexer<'a> {
});
}
'\\' => {
let mut wrapped = Raw(Some(Default::default()));

if let Some(chars) = l.read_escaped_char(&mut wrapped, false)? {
if let Some(chars) = l.read_escaped_char(false)? {
for c in chars {
out.extend(c);
}
Expand Down Expand Up @@ -1208,9 +1175,7 @@ impl<'a> Lexer<'a> {
if c == '\\' {
consume_cooked!();

let mut wrapped = Raw(None);

match self.read_escaped_char(&mut wrapped, true) {
match self.read_escaped_char(true) {
Ok(Some(chars)) => {
if let Ok(ref mut cooked) = cooked {
for c in chars {
Expand Down
7 changes: 0 additions & 7 deletions crates/swc_ecma_parser/src/lexer/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,6 @@ use crate::{
pub(super) struct Raw(pub Option<SmartString<LazyCompact>>);

impl Raw {
#[inline]
pub fn push_str(&mut self, s: &str) {
if let Some(ref mut st) = self.0 {
st.push_str(s)
}
}

#[inline]
pub fn push(&mut self, c: char) {
if let Some(ref mut st) = self.0 {
Expand Down
Loading