Skip to content

Allow POSIX character properties outside of custom character classes #272

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 13, 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
17 changes: 7 additions & 10 deletions Sources/_RegexParser/Regex/Parse/LexicalAnalysis.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1064,14 +1064,13 @@ extension Source {
}

mutating func lexCustomCCStart(
context: ParsingContext
) throws -> Located<CustomCC.Start>? {
recordLoc { src in
// Make sure we don't have a POSIX character property. This may require
// walking to its ending to make sure we have a closing ':]', as otherwise
// we have a custom character class.
// TODO: This behavior seems subtle, could we warn?
guard !src.canLexPOSIXCharacterProperty(context: context) else {
guard !src.canLexPOSIXCharacterProperty() else {
return nil
}
if src.tryEat("[") {
Expand Down Expand Up @@ -1104,11 +1103,8 @@ extension Source {
}

private mutating func lexPOSIXCharacterProperty(
context: ParsingContext
) throws -> Located<AST.Atom.CharacterProperty>? {
// Only allowed in a custom character class.
guard context.isInCustomCharacterClass else { return nil }
return try recordLoc { src in
try recordLoc { src in
try src.tryEating { src in
guard src.tryEat(sequence: "[:") else { return nil }
let inverted = src.tryEat("^")
Expand All @@ -1127,10 +1123,10 @@ extension Source {
}
}

private func canLexPOSIXCharacterProperty(context: ParsingContext) -> Bool {
private func canLexPOSIXCharacterProperty() -> Bool {
do {
var src = self
return try src.lexPOSIXCharacterProperty(context: context) != nil
return try src.lexPOSIXCharacterProperty() != nil
} catch {
// We want to tend on the side of lexing a POSIX character property, so
// even if it is invalid in some way (e.g invalid property names), still
Expand Down Expand Up @@ -1818,8 +1814,9 @@ extension Source {
if !customCC && (src.peek() == ")" || src.peek() == "|") { return nil }
// TODO: Store customCC in the atom, if that's useful

// POSIX character property.
if let prop = try src.lexPOSIXCharacterProperty(context: context)?.value {
// POSIX character property. Like \p{...} this is also allowed outside of
// a custom character class.
if let prop = try src.lexPOSIXCharacterProperty()?.value {
return .property(prop)
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/_RegexParser/Regex/Parse/Parse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ extension Parser {
}

// Check if we have the start of a custom character class '['.
if let cccStart = try source.lexCustomCCStart(context: context) {
if let cccStart = try source.lexCustomCCStart() {
return .customCharacterClass(
try parseCustomCharacterClass(cccStart))
}
Expand Down Expand Up @@ -487,7 +487,7 @@ extension Parser {
while source.peek() != "]" && source.peekCCBinOp() == nil {

// Nested custom character class.
if let cccStart = try source.lexCustomCCStart(context: context) {
if let cccStart = try source.lexCustomCCStart() {
members.append(.custom(try parseCustomCharacterClass(cccStart)))
continue
}
Expand Down
5 changes: 5 additions & 0 deletions Sources/_StringProcessing/Utility/ASTBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,11 @@ func prop(
) -> AST.Node {
atom(.property(.init(kind, isInverted: inverted, isPOSIX: false)))
}
func posixProp(
_ kind: AST.Atom.CharacterProperty.Kind, inverted: Bool = false
) -> AST.Node {
atom(.property(.init(kind, isInverted: inverted, isPOSIX: true)))
}

// Raw atom constructing variant
func atom_a(
Expand Down
11 changes: 8 additions & 3 deletions Tests/RegexTests/ParseTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -477,12 +477,9 @@ extension RegexTests {
// These are custom character classes, not invalid POSIX character classes.
// TODO: This behavior is subtle, we ought to warn.
parseTest("[[:space]]", charClass(charClass(":", "s", "p", "a", "c", "e")))
parseTest("[:space:]", charClass(":", "s", "p", "a", "c", "e", ":"))
parseTest("[:a]", charClass(":", "a"))
parseTest("[a:]", charClass("a", ":"))
parseTest("[:]", charClass(":"))
parseTest("[::]", charClass(":", ":"))
parseTest("[:=:]", charClass(":", "=", ":"))
parseTest("[[:]]", charClass(charClass(":")))
parseTest("[[:a=b=c:]]", charClass(charClass(":", "a", "=", "b", "=", "c", ":")))

Expand Down Expand Up @@ -522,6 +519,12 @@ extension RegexTests {
posixProp_m(.binary(.uppercase), inverted: true),
"c", "d"))

// Like ICU, we allow POSIX character properties outside of custom character
// classes. This also appears to be suggested by UTS#18.
// TODO: We should likely emit a warning.
parseTest("[:space:]", posixProp(.binary(.whitespace)))
parseTest("[:script=Greek:]", posixProp(.script(.greek)))

parseTest("[[[:space:]]]", charClass(charClass(
posixProp_m(.binary(.whitespace))
)))
Expand Down Expand Up @@ -2252,6 +2255,8 @@ extension RegexTests {
diagnosticTest("[[:a:", .expected("]"))
diagnosticTest("[[:a[:]", .expected("]"))

diagnosticTest("[::]", .emptyProperty)
diagnosticTest("[:=:]", .emptyProperty)
diagnosticTest("[[::]]", .emptyProperty)
diagnosticTest("[[:=:]]", .emptyProperty)

Expand Down