Skip to content

Commit

Permalink
fix(parser): fix panic on multi-byte characaters (#233)
Browse files Browse the repository at this point in the history
* fix(oxc_parser): fix panic when EOF on a multi-byte character

relates #232

* fix(parser): fix panic on multi-byte char in private identifer

relates #232
  • Loading branch information
Boshen authored Apr 1, 2023
1 parent 4ccb9e8 commit 1743305
Show file tree
Hide file tree
Showing 8 changed files with 672 additions and 824 deletions.
17 changes: 1 addition & 16 deletions crates/oxc_parser/src/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ impl<'a> Parser<'a> {
/// # Errors
pub fn expect_without_advance(&mut self, kind: Kind) -> Result<()> {
if !self.at(kind) {
let range = self.current_range();
let range = self.cur_token().span();
return Err(
diagnostics::ExpectToken(kind.to_str(), self.cur_kind().to_str(), range).into()
);
Expand All @@ -190,21 +190,6 @@ impl<'a> Parser<'a> {
Ok(())
}

#[must_use]
pub fn current_range(&self) -> Span {
let cur_token = self.cur_token();
match self.cur_kind() {
Kind::Eof => {
if self.prev_token_end < cur_token.end {
Span::new(self.prev_token_end, self.prev_token_end)
} else {
Span::new(self.prev_token_end - 1, self.prev_token_end)
}
}
_ => cur_token.span(),
}
}

/// Expect the next next token to be a `JsxChild`, i.e. `<` or `{` or `JSXText`
/// # Errors
pub fn expect_jsx_child(&mut self, kind: Kind) -> Result<()> {
Expand Down
8 changes: 2 additions & 6 deletions crates/oxc_parser/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,9 @@ pub struct Flow(#[label] pub Span);
pub struct UnexpectedToken(#[label] pub Span);

#[derive(Debug, Error, Diagnostic)]
#[error("Expect token")]
#[error("Expected `{0}` but found `{1}`")]
#[diagnostic()]
pub struct ExpectToken(
pub &'static str,
pub &'static str,
#[label("Expect `{0}` here, but found `{1}`")] pub Span,
);
pub struct ExpectToken(pub &'static str, pub &'static str, #[label("`{0}` expected")] pub Span);

#[derive(Debug, Error, Diagnostic)]
#[error("Invalid escape sequence")]
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_parser/src/js/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ impl<'a> Parser<'a> {
.then(|| self.parse_expression())
.transpose()
.map_err(|_| {
let range = self.current_range();
let range = self.cur_token().span();
diagnostics::ExpectToken(Kind::Semicolon.to_str(), self.cur_kind().to_str(), range)
})?;
self.expect(Kind::Semicolon)?;
Expand Down
8 changes: 6 additions & 2 deletions crates/oxc_parser/src/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -726,11 +726,15 @@ impl<'a> Lexer<'a> {
self.identifier_unicode_escape_sequence(&mut builder, true);
}
Some(c) => {
self.error(diagnostics::InvalidCharacter(c, Span::new(start, self.offset() - 1)));
#[allow(clippy::cast_possible_truncation)]
self.error(diagnostics::InvalidCharacter(
c,
Span::new(start, start + c.len_utf8() as u32),
));
return Kind::Undetermined;
}
None => {
self.error(diagnostics::UnexpectedEnd(Span::new(start, self.offset() - 1)));
self.error(diagnostics::UnexpectedEnd(Span::new(start, start)));
return Kind::Undetermined;
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ impl<'a> Parser<'a> {
if self.cur_kind() == Kind::Undetermined {
return Err(self.errors.borrow_mut().pop().unwrap());
}
Err(diagnostics::UnexpectedToken(self.current_range()).into())
Err(diagnostics::UnexpectedToken(self.cur_token().span()).into())
}

/// Push a Syntax Error
Expand Down
Loading

0 comments on commit 1743305

Please sign in to comment.