diff --git a/src/parse.rs b/src/parse.rs index 2287cc6..268a27e 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -950,7 +950,8 @@ where } } - 'k' => { + // [+NamedCaptureGroups] k GroupName + 'k' if self.flags.unicode || !self.named_group_indices.is_empty() => { self.consume('k'); // The sequence `\k` must be the start of a backreference to a named capture group. @@ -967,6 +968,16 @@ where error("Unexpected end of named backreference") } } + + // [~NamedCaptureGroups] k GroupName + 'k' => { + self.consume('k'); + Ok(ir::Node::Char { + c: self.fold_if_icase(c), + icase: self.flags.icase, + }) + } + _ => { let c = self.consume_character_escape()?; Ok(ir::Node::Char { diff --git a/tests/tests.rs b/tests/tests.rs index 1b5d9cf..0a7fb9d 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -1205,28 +1205,42 @@ fn run_regexp_named_capture_groups_tc(tc: TestConfig) { } #[test] -#[rustfmt::skip] -fn run_regexp_named_groups_unicode_malformed_tc() { +fn run_regexp_named_groups_unicode_malformed() { + test_with_configs(run_regexp_named_groups_unicode_malformed_tc) +} + +fn run_regexp_named_groups_unicode_malformed_tc(tc: TestConfig) { // From 262 test/annexB/built-ins/RegExp/named-groups/non-unicode-malformed-lookbehind.js - test_parse_fails(r#"\k(?<=>)a"#); - test_parse_fails(r#"(?<=>)\k"#); - test_parse_fails(r#"\k(?)\k"#); + tc.compile(r#"\k(?<=>)a"#).test_succeeds(r#"ka"#); + tc.compile(r#"(?<=>)\k"#).test_succeeds(r#">k"#); + tc.compile(r#"\k(?a"#); + tc.compile(r#"(?)\k"#).test_succeeds(r#"k"#); + + // Negative parse tests in unicode mode. + test_parse_fails_flags(r#"\k(?<=>)a"#, "u"); + test_parse_fails_flags(r#"(?<=>)\k"#, "u"); + test_parse_fails_flags(r#"\k(?)\k"#, "u"); // From 262 test/annexB/built-ins/RegExp/named-groups/non-unicode-malformed.js - test_parse_fails(r#"\k"#); - test_parse_fails(r#"\k<4>"#); - test_parse_fails(r#"\k\a)"#); - - test_parse_fails(r#"\k"#); - test_parse_fails(r#"\k(x)"#); - test_parse_fails(r#"\k\1"#); - test_parse_fails(r#"\1(b)\k"#); + tc.compile(r#"\k"#).test_succeeds(r#"k"#); + tc.compile(r#"\k<4>"#).test_succeeds(r#"k<4>"#); + tc.compile(r#"\k\a)"#).test_succeeds(r#"a"#); + tc.compile(r#"\k(x)"#).test_succeeds(r#"kx"#); + tc.compile(r#"\k\1"#).test_succeeds("k\u{1}"); + tc.compile(r#"\1(b)\k"#).test_succeeds(r#"bk"#); + + // Negative parse tests in unicode mode. + test_parse_fails_flags(r#"\k"#, "u"); + test_parse_fails_flags(r#"\k<4>"#, "u"); + test_parse_fails_flags(r#"\k\a)"#, "u"); + test_parse_fails_flags(r#"\k(x)"#, "u"); + test_parse_fails_flags(r#"\k\1"#, "u"); + test_parse_fails_flags(r#"\1(b)\k"#, "u"); // From 262 test/language/literals/regexp/named-groups/invalid-duplicate-groupspecifier.js test_parse_fails(r#"(?a)(?a)"#);