Skip to content
Merged
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
25 changes: 21 additions & 4 deletions crates/oxc_parser/src/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,29 @@ impl<'a> ParserImpl<'a> {
}

/// Get current source text
#[inline]
pub(crate) fn cur_src(&self) -> &'a str {
let range = self.cur_token().span();
// SAFETY:
// range comes from the lexer, which are ensured to meeting the criteria of `get_unchecked`.
self.token_source(&self.token)
}

unsafe { self.source_text.get_unchecked(range.start as usize..range.end as usize) }
/// Get source text for a token
#[inline]
pub(crate) fn token_source(&self, token: &Token) -> &'a str {
let span = token.span();
if cfg!(debug_assertions) {
&self.source_text[span.start as usize..span.end as usize]
} else {
// SAFETY:
// Span comes from the lexer, which ensures:
// * `start` and `end` are in bounds of source text.
// * `end >= start`.
// * `start` and `end` are both on UTF-8 char boundaries.
// * `self.source_text` is same text that `Token`s are generated from.
//
// TODO: I (@overlookmotel) don't think we should really be doing this.
// We don't have static guarantees of these properties.
unsafe { self.source_text.get_unchecked(span.start as usize..span.end as usize) }
}
}

/// Get current string
Expand Down
Loading