Skip to content

Ban ] as literal first character of custom character class #404

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
May 12, 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
10 changes: 0 additions & 10 deletions Sources/_RegexParser/Regex/Parse/Parse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -465,16 +465,6 @@ extension Parser {
var members: Array<Member> = []
try parseCCCMembers(into: &members)

// If we didn't parse any semantic members, we can eat a ']' character, as
// PCRE, Oniguruma, and ICU forbid empty character classes, and assume an
// initial ']' is literal.
if members.none(\.isSemantic) {
if let loc = source.tryEatWithLoc("]") {
members.append(.atom(.init(.char("]"), loc)))
try parseCCCMembers(into: &members)
}
}

// If we have a binary set operator, parse it and the next members. Note
// that this means we left associate for a chain of operators.
// TODO: We may want to diagnose and require users to disambiguate, at least
Expand Down
1 change: 0 additions & 1 deletion Tests/RegexTests/MatchTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1282,7 +1282,6 @@ extension RegexTests {
firstMatchTest(#"(?xx)[ \t]+"#, input: " \t \t", match: "\t")

firstMatchTest("(?xx)[ a && ab ]+", input: " aaba ", match: "aa")
firstMatchTest("(?xx)[ ] a ]+", input: " a]]a ] ", match: "a]]a")
}

func testASCIIClasses() {
Expand Down
31 changes: 10 additions & 21 deletions Tests/RegexTests/ParseTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -528,23 +528,7 @@ extension RegexTests {
))

parseTest("[-]", charClass("-"))

// Empty character classes are forbidden, therefore these are character
// classes containing literal ']'.
parseTest("[]]", charClass("]"))
parseTest("[]a]", charClass("]", "a"))
parseTest("(?x)[ ]]", concat(
changeMatchingOptions(matchingOptions(adding: .extended)),
charClass("]")
))
parseTest("(?x)[ ] ]", concat(
changeMatchingOptions(matchingOptions(adding: .extended)),
charClass("]")
))
parseTest("(?x)[ ] a ]", concat(
changeMatchingOptions(matchingOptions(adding: .extended)),
charClass("]", "a")
))
parseTest(#"[\]]"#, charClass("]"))

// These are metacharacters in certain contexts, but normal characters
// otherwise.
Expand Down Expand Up @@ -2497,10 +2481,15 @@ extension RegexTests {

diagnosticTest("[a", .expected("]"))

// The first ']' of a custom character class is literal, so these are
// missing the closing bracket.
diagnosticTest("[]", .expected("]"))
diagnosticTest("(?x)[ ]", .expected("]"))
// Character classes may not be empty.
diagnosticTest("[]", .expectedCustomCharacterClassMembers)
diagnosticTest("[]]", .expectedCustomCharacterClassMembers)
diagnosticTest("[]a]", .expectedCustomCharacterClassMembers)
diagnosticTest("(?x)[ ]", .expectedCustomCharacterClassMembers)
diagnosticTest("(?x)[ ] ]", .expectedCustomCharacterClassMembers)
diagnosticTest("(?x)[ ] a ]", .expectedCustomCharacterClassMembers)
diagnosticTest("(?xx)[ ] a ]+", .expectedCustomCharacterClassMembers)
diagnosticTest("(?x)[ ]]", .expectedCustomCharacterClassMembers)

diagnosticTest("[&&]", .expectedCustomCharacterClassMembers)
diagnosticTest("[a&&]", .expectedCustomCharacterClassMembers)
Expand Down