Skip to content

Commit

Permalink
feat(regular_expression): implement Display for RegularExpression
Browse files Browse the repository at this point in the history
… type. (#5304)

Part of #5060
  • Loading branch information
rzvxa committed Sep 3, 2024
1 parent 0abfc50 commit c0b6269
Show file tree
Hide file tree
Showing 5 changed files with 588 additions and 26 deletions.
32 changes: 18 additions & 14 deletions crates/oxc_regular_expression/src/body_parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::{
body_parser::{diagnostics, reader::Reader, state::State, unicode, unicode_property},
options::ParserOptions,
span::SpanFactory,
surroage_pair,
};

pub struct PatternParser<'a> {
Expand Down Expand Up @@ -1847,14 +1848,14 @@ impl<'a> PatternParser<'a> {
let span_start = self.reader.offset();

if let Some(lead_surrogate) =
self.reader.peek().filter(|&cp| unicode::is_lead_surrogate(cp))
self.reader.peek().filter(|&cp| surroage_pair::is_lead_surrogate(cp))
{
if let Some(trail_surrogate) =
self.reader.peek2().filter(|&cp| unicode::is_trail_surrogate(cp))
self.reader.peek2().filter(|&cp| surroage_pair::is_trail_surrogate(cp))
{
self.reader.advance();
self.reader.advance();
let cp = unicode::combine_surrogate_pair(lead_surrogate, trail_surrogate);
let cp = surroage_pair::combine_surrogate_pair(lead_surrogate, trail_surrogate);

// [SS:EE] RegExpIdentifierStart :: UnicodeLeadSurrogate UnicodeTrailSurrogate
// It is a Syntax Error if the RegExpIdentifierCodePoint of RegExpIdentifierStart is not matched by the UnicodeIDStart lexical grammar production.
Expand Down Expand Up @@ -1907,15 +1908,15 @@ impl<'a> PatternParser<'a> {
let span_start = self.reader.offset();

if let Some(lead_surrogate) =
self.reader.peek().filter(|&cp| unicode::is_lead_surrogate(cp))
self.reader.peek().filter(|&cp| surroage_pair::is_lead_surrogate(cp))
{
if let Some(trail_surrogate) =
self.reader.peek2().filter(|&cp| unicode::is_trail_surrogate(cp))
self.reader.peek2().filter(|&cp| surroage_pair::is_trail_surrogate(cp))
{
self.reader.advance();
self.reader.advance();

let cp = unicode::combine_surrogate_pair(lead_surrogate, trail_surrogate);
let cp = surroage_pair::combine_surrogate_pair(lead_surrogate, trail_surrogate);
// [SS:EE] RegExpIdentifierPart :: UnicodeLeadSurrogate UnicodeTrailSurrogate
// It is a Syntax Error if the RegExpIdentifierCodePoint of RegExpIdentifierPart is not matched by the UnicodeIDContinue lexical grammar production.
if !unicode::is_unicode_id_continue(cp) {
Expand Down Expand Up @@ -1953,15 +1954,16 @@ impl<'a> PatternParser<'a> {
let checkpoint = self.reader.checkpoint();

// HexLeadSurrogate + HexTrailSurrogate
if let Some(lead_surrogate) =
self.consume_fixed_hex_digits(4).filter(|&cp| unicode::is_lead_surrogate(cp))
if let Some(lead_surrogate) = self
.consume_fixed_hex_digits(4)
.filter(|&cp| surroage_pair::is_lead_surrogate(cp))
{
if self.reader.eat2('\\', 'u') {
if let Some(trail_surrogate) = self
.consume_fixed_hex_digits(4)
.filter(|&cp| unicode::is_trail_surrogate(cp))
.filter(|&cp| surroage_pair::is_trail_surrogate(cp))
{
return Ok(Some(unicode::combine_surrogate_pair(
return Ok(Some(surroage_pair::combine_surrogate_pair(
lead_surrogate,
trail_surrogate,
)));
Expand All @@ -1971,16 +1973,18 @@ impl<'a> PatternParser<'a> {
self.reader.rewind(checkpoint);

// HexLeadSurrogate
if let Some(lead_surrogate) =
self.consume_fixed_hex_digits(4).filter(|&cp| unicode::is_lead_surrogate(cp))
if let Some(lead_surrogate) = self
.consume_fixed_hex_digits(4)
.filter(|&cp| surroage_pair::is_lead_surrogate(cp))
{
return Ok(Some(lead_surrogate));
}
self.reader.rewind(checkpoint);

// HexTrailSurrogate
if let Some(trail_surrogate) =
self.consume_fixed_hex_digits(4).filter(|&cp| unicode::is_trail_surrogate(cp))
if let Some(trail_surrogate) = self
.consume_fixed_hex_digits(4)
.filter(|&cp| surroage_pair::is_trail_surrogate(cp))
{
return Ok(Some(trail_surrogate));
}
Expand Down
12 changes: 0 additions & 12 deletions crates/oxc_regular_expression/src/body_parser/unicode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,18 +114,6 @@ pub fn is_identifier_part_char(cp: u32) -> bool {
char::from_u32(cp).map_or(false, |ch| unicode_id_start::is_id_continue(ch) || ch == '$')
}

pub fn is_lead_surrogate(cp: u32) -> bool {
(0xd800..=0xdbff).contains(&cp)
}

pub fn is_trail_surrogate(cp: u32) -> bool {
(0xdc00..=0xdfff).contains(&cp)
}

pub fn combine_surrogate_pair(lead: u32, trail: u32) -> u32 {
(lead - 0xd800) * 0x400 + trail - 0xdc00 + 0x10000
}

pub fn map_control_escape(cp: u32) -> Option<u32> {
match char::from_u32(cp) {
Some('f') => Some(0x0c),
Expand Down
Loading

0 comments on commit c0b6269

Please sign in to comment.