Skip to content

Don't parse a character property containing a backslash #301

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 19, 2022
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
8 changes: 8 additions & 0 deletions Sources/_RegexParser/Regex/Parse/LexicalAnalysis.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1176,6 +1176,14 @@ extension Source {
// character property name anyway, and it's nice not to have diverging
// logic for these cases.
return true
case "\\":
// An escape sequence, which may include e.g '\Q :] \E'. ICU bails here
// for all its known escape sequences (e.g '\a', '\e' '\f', ...). It
// seems character class escapes e.g '\d' are excluded, however it's not
// clear that is intentional. Let's apply the rule for any escape, as a
// backslash would never be a valid character property name, and we can
// diagnose any invalid escapes when parsing as a character class.
return true
default:
// We may want to handle other metacharacters here, e.g '{', '(', ')',
// as they're not valid character property names. However for now
Expand Down
21 changes: 20 additions & 1 deletion Tests/RegexTests/ParseTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,25 @@ extension RegexTests {
parseTest(#"[[:{]]"#, charClass(charClass(":", "{")))
parseTest(#"[[:}:]]"#, charClass(charClass(":", "}", ":")))

parseTest(
#"[:[:space:]:]"#,
charClass(":", posixProp_m(.binary(.whitespace)), ":")
)
parseTest(
#"[:a[:space:]b:]"#,
charClass(":", "a", posixProp_m(.binary(.whitespace)), "b", ":")
)

// ICU parses a custom character class if it sees any of its known escape
// sequences in a POSIX character property (though it appears to exclude
// character class escapes e.g '\d'). We do so for any escape sequence as
// '\' is not a valid character property character.
parseTest(#"[:\Q:]\E]"#, charClass(":", quote_m(":]")))
parseTest(#"[:\a:]"#, charClass(":", atom_m(.escaped(.alarm)), ":"))
parseTest(#"[:\d:]"#, charClass(":", atom_m(.escaped(.decimalDigit)), ":"))
parseTest(#"[:\\:]"#, charClass(":", "\\", ":"))
parseTest(#"[:\:]"#, charClass(":", ":"))

parseTest(
#"\D\S\W"#,
concat(
Expand Down Expand Up @@ -2319,7 +2338,7 @@ extension RegexTests {
diagnosticTest(#"\p{x=y}"#, .unknownProperty(key: "x", value: "y"))
diagnosticTest(#"\p{aaa(b)}"#, .unknownProperty(key: nil, value: "aaa(b)"))
diagnosticTest("[[:a():]]", .unknownProperty(key: nil, value: "a()"))
diagnosticTest(#"\p{aaa\p{b}}"#, .unknownProperty(key: nil, value: #"aaa\p{b"#))
diagnosticTest(#"\p{aaa\p{b}}"#, .unknownProperty(key: nil, value: "aaa"))
diagnosticTest(#"[[:{:]]"#, .unknownProperty(key: nil, value: "{"))

// MARK: Matching options
Expand Down