Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -1875,13 +1875,26 @@ impl<'a> PatternParser<'a> {
) -> Result<Option<(Str<'a>, Option<Str<'a>>, bool)>> {
let checkpoint = self.reader.checkpoint();

// Both alternatives share this prefix. Delay materializing it until
// `=` selects a property name, or the whole lone value is consumed.
while let Some(cp) = self.reader.peek() {
if !character::is_unicode_property_name_character(cp) {
break;
}
self.reader.advance();
}
// Preserve the property scanners' EOF behavior, including partial names.
if self.reader.peek().is_none() {
return Ok(None);
}

// UnicodePropertyName=UnicodePropertyValue
if let Some(name) = self.consume_unicode_property_name()
&& self.reader.eat('=')
{
if self.reader.checkpoint() != checkpoint && self.reader.peek() == Some('=' as u32) {
let name = self.unicode_property_text(checkpoint);
self.reader.advance();
let span_start = self.reader.offset();

if let Some(value) = self.consume_unicode_property_value() {
if let Some(value) = self.consume_unicode_property_value(self.reader.checkpoint()) {
// [SS:EE] UnicodePropertyValueExpression :: UnicodePropertyName = UnicodePropertyValue
// It is a Syntax Error if the source text matched by UnicodePropertyName is not a Unicode property name or property alias listed in the “Property name and aliases” column of Table 65.
// [SS:EE] UnicodePropertyValueExpression :: UnicodePropertyName = UnicodePropertyValue
Expand All @@ -1895,12 +1908,13 @@ impl<'a> PatternParser<'a> {

return Ok(Some((name, Some(value), false)));
}
// Keep malformed name=value inputs on the original lone-value fallback.
self.reader.rewind(checkpoint);
}
self.reader.rewind(checkpoint);

let span_start = self.reader.offset();
let span_start = checkpoint.1;
// LoneUnicodePropertyNameOrValue
if let Some(name_or_value) = self.consume_unicode_property_value() {
if let Some(name_or_value) = self.consume_unicode_property_value(checkpoint) {
// [SS:EE] UnicodePropertyValueExpression :: LoneUnicodePropertyNameOrValue
// It is a Syntax Error if the source text matched by LoneUnicodePropertyNameOrValue is not a Unicode property value or property value alias for the General_Category (gc) property listed in PropertyValueAliases.txt, nor a binary property or binary property alias listed in the “Property name and aliases” column of Table 66, nor a binary property of strings listed in the “Property name” column of Table 67.
if unicode_property::is_valid_unicode_property("General_Category", &name_or_value) {
Expand Down Expand Up @@ -1931,21 +1945,7 @@ impl<'a> PatternParser<'a> {
Ok(None)
}

fn consume_unicode_property_name(&mut self) -> Option<Str<'a>> {
let checkpoint = self.reader.checkpoint();
while character::is_unicode_property_name_character(self.reader.peek()?) {
self.reader.advance();
}

if checkpoint == self.reader.checkpoint() {
return None;
}

Some(self.unicode_property_text(checkpoint))
}

fn consume_unicode_property_value(&mut self) -> Option<Str<'a>> {
let checkpoint = self.reader.checkpoint();
fn consume_unicode_property_value(&mut self, checkpoint: (usize, u32)) -> Option<Str<'a>> {
while character::is_unicode_property_value_character(self.reader.peek()?) {
self.reader.advance();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use oxc_allocator::Allocator;
use oxc_regular_expression::{ConstructorParser, LiteralParser, Options, ast::Term};

#[test]
fn unicode_property_error_ranges() {
let allocator = Allocator::default();
for (pattern, message, start, len) in [
(r"\p{foo1bar}", "Invalid unicode property name and/or value", 3, 7),
(r"\p{1ASCII}", "Invalid unicode property name and/or value", 3, 6),
(r"\p{foo1bar=Latin}", "Invalid unicode property name and/or value", 3, 7),
(r"\p{Script=}", "Invalid unicode property name and/or value", 3, 6),
(r"\p{Script=Latin", "Invalid unicode property name and/or value", 3, 6),
(r"\p{ASCII=}", "Unterminated unicode property escape", 0, 8),
(r"\p{ASCII", "Unterminated unicode property escape", 0, 8),
(r"\p{A1b", "Unterminated unicode property escape", 0, 6),
(r"\p{}", "Unterminated unicode property escape", 0, 3),
(r"\p{=Latin}", "Unterminated unicode property escape", 0, 3),
(r"\p{", "Unterminated unicode property escape", 0, 3),
] {
let error = LiteralParser::new(&allocator, pattern, Some("v"), Options::default())
.parse()
.unwrap_err();
assert!(error.to_string().ends_with(message), "/{pattern}/v: {error}");
assert_eq!(error.labels.len(), 1, "/{pattern}/v");
assert_eq!((error.labels[0].offset(), error.labels[0].len()), (start, len), "/{pattern}/v");
}
}

#[test]
fn constructor_unicode_property_names() {
let allocator = Allocator::default();
for (source, expected_name, expected_value) in [
(r#""\\p{\x41SCII}""#, "ASCII", None),
(r#""\\p{\x53cript=Latin}""#, "Script", Some("Latin")),
(r#""\\p{Script=\x4catin}""#, "Script", Some("Latin")),
] {
let parsed = ConstructorParser::new(&allocator, source, Some(r#""u""#), Options::default())
.parse()
.unwrap();
let Term::UnicodePropertyEscape(property) = &parsed.body.body[0].body[0] else {
panic!("expected Unicode property escape");
};
assert_eq!(property.name.as_str(), expected_name);
assert_eq!(property.value.as_ref().map(oxc_str::Str::as_str), expected_value);
}
}
Loading