Skip to content

Remove compiling argument label #306

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 20, 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
4 changes: 2 additions & 2 deletions Documentation/Evolution/RegexLiterals.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ In *[Regex Type and Overview][regex-type]* we introduced the `Regex` type, which

```swift
let pattern = #"(\w+)\s\s+(\S+)\s\s+((?:(?!\s\s).)*)\s\s+(.*)"#
let regex = try! Regex(compiling: pattern)
let regex = try! Regex(pattern)
// regex: Regex<AnyRegexOutput>
```

Expand Down Expand Up @@ -366,7 +366,7 @@ However we decided against this because:

### No custom literal

Instead of adding a custom regex literal, we could require users to explicitly write `try! Regex(compiling: "[abc]+")`. This would be similar to `NSRegularExpression`, and loses all the benefits of parsing the literal at compile time. This would mean:
Instead of adding a custom regex literal, we could require users to explicitly write `try! Regex("[abc]+")`. This would be similar to `NSRegularExpression`, and loses all the benefits of parsing the literal at compile time. This would mean:

- No source tooling support (e.g syntax highlighting, refactoring actions) would be available.
- Parse errors would be diagnosed at run time rather than at compile time.
Expand Down
4 changes: 2 additions & 2 deletions Documentation/Evolution/RegexSyntaxRunTimeConstruction.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ We propose run-time construction of `Regex` from a best-in-class treatment of fa

```swift
let pattern = #"(\w+)\s\s+(\S+)\s\s+((?:(?!\s\s).)*)\s\s+(.*)"#
let regex = try! Regex(compiling: pattern)
let regex = try! Regex(pattern)
// regex: Regex<AnyRegexOutput>

let regex: Regex<(Substring, Substring, Substring, Substring, Substring)> =
try! Regex(compiling: pattern)
try! Regex(pattern)
```

### Syntax
Expand Down
6 changes: 3 additions & 3 deletions Documentation/Evolution/RegexTypeOverview.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,11 @@ Regexes can be created at run time from a string containing familiar regex synta

```swift
let pattern = #"(\w+)\s\s+(\S+)\s\s+((?:(?!\s\s).)*)\s\s+(.*)"#
let regex = try! Regex(compiling: pattern)
let regex = try! Regex(pattern)
// regex: Regex<AnyRegexOutput>

let regex: Regex<(Substring, Substring, Substring, Substring, Substring)> =
try! Regex(compiling: pattern)
try! Regex(pattern)
```

*Note*: The syntax accepted and further details on run-time compilation, including `AnyRegexOutput` and extended syntaxes, are discussed in [Run-time Regex Construction][pitches].
Expand Down Expand Up @@ -300,7 +300,7 @@ Regex targets [UTS\#18 Level 2](https://www.unicode.org/reports/tr18/#Extended_U
```swift
/// A regex represents a string processing algorithm.
///
/// let regex = try Regex(compiling: "a(.*)b")
/// let regex = try Regex("a(.*)b")
/// let match = "cbaxb".firstMatch(of: regex)
/// print(match.0) // "axb"
/// print(match.1) // "x"
Expand Down
2 changes: 1 addition & 1 deletion Sources/Exercises/Participants/RegexParticipant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ private func graphemeBreakPropertyDataLiteral(
forLine line: String
) -> GraphemeBreakEntry? {
let regex = try! Regex(
compiling: #"([0-9A-F]+)(?:\.\.([0-9A-F]+))?\s+;\s+(\w+).*"#,
#"([0-9A-F]+)(?:\.\.([0-9A-F]+))?\s+;\s+(\w+).*"#,
as: (Substring, Substring, Substring?, Substring).self)
return graphemeBreakPropertyData(forLine: line, using: regex)
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/_StringProcessing/Regex/AnyRegexOutput.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
@available(SwiftStdlib 5.7, *)
extension Regex where Output == AnyRegexOutput {
/// Parse and compile `pattern`, resulting in an existentially-typed capture list.
public init(compiling pattern: String) throws {
public init(_ pattern: String) throws {
self.init(ast: try parse(pattern, .traditional))
}
}
Expand All @@ -23,7 +23,7 @@ extension Regex where Output == AnyRegexOutput {
extension Regex {
/// Parse and compile `pattern`, resulting in a strongly-typed capture list.
public init(
compiling pattern: String,
_ pattern: String,
as: Output.Type = Output.self
) throws {
self.init(ast: try parse(pattern, .traditional))
Expand Down
2 changes: 1 addition & 1 deletion Sources/_StringProcessing/Regex/Core.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public protocol RegexComponent {

/// A regex represents a string processing algorithm.
///
/// let regex = try Regex(compiling: "a(.*)b")
/// let regex = try Regex("a(.*)b")
/// let match = "cbaxb".firstMatch(of: regex)
/// print(match.0) // "axb"
/// print(match.1) // "x"
Expand Down
4 changes: 2 additions & 2 deletions Tests/RegexBuilderTests/MotivationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ private func processWithRuntimeDynamicRegex(
_ line: String
) -> Transaction? {
// FIXME: Shouldn't this init throw?
let regex = try! Regex(compiling: pattern)
let regex = try! Regex(pattern)

// guard let result = line.match(regex) else { return nil }
//
Expand All @@ -156,7 +156,7 @@ private func processWithRuntimeDynamicRegex(
@available(macOS 12.0, *)
private func processWithRuntimeStaticRegex(_ line: String) -> Transaction? {
let regex: Regex<(Substring, Substring, Substring, Substring, Substring)>
= try! Regex(compiling: pattern)
= try! Regex(pattern)

return process(line, using: regex)
}
Expand Down
6 changes: 3 additions & 3 deletions Tests/RegexBuilderTests/RegexDSLTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ class RegexDSLTests: XCTestCase {

do {
let regexLiteral = try Regex(
compiling: #"([0-9A-F]+)(?:\.\.([0-9A-F]+))?\s+;\s+(\w+).*"#,
#"([0-9A-F]+)(?:\.\.([0-9A-F]+))?\s+;\s+(\w+).*"#,
as: (Substring, Substring, Substring?, Substring).self)
let maybeMatchResult = line.wholeMatch(of: regexLiteral)
let matchResult = try XCTUnwrap(maybeMatchResult)
Expand All @@ -680,7 +680,7 @@ class RegexDSLTests: XCTestCase {

func testDynamicCaptures() throws {
do {
let regex = try Regex(compiling: "aabcc.")
let regex = try Regex("aabcc.")
let line = "aabccd"
let match = try XCTUnwrap(line.wholeMatch(of: regex))
XCTAssertEqual(match.0, line[...])
Expand All @@ -689,7 +689,7 @@ class RegexDSLTests: XCTestCase {
}
do {
let regex = try Regex(
compiling: #"""
#"""
(?<lower>[0-9A-F]+)(?:\.\.(?<upper>[0-9A-F]+))?\s+;\s+(?<desc>\w+).*
"""#)
let line = """
Expand Down
10 changes: 5 additions & 5 deletions Tests/RegexTests/AlgorithmsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class RegexConsumerTests: XCTestCase {
_ expected: [Range<Int>],
file: StaticString = #file, line: UInt = #line
) {
let regex = try! Regex(compiling: regex)
let regex = try! Regex(regex)

let actualSeq: [Range<Int>] = string[...].ranges(of: regex).map(string.offsets(of:))
XCTAssertEqual(actualSeq, expected, file: file, line: line)
Expand Down Expand Up @@ -69,7 +69,7 @@ class RegexConsumerTests: XCTestCase {
_ expected: [Substring],
file: StaticString = #file, line: UInt = #line
) {
let regex = try! Regex(compiling: regex)
let regex = try! Regex(regex)
let actual = Array(string.split(by: regex))
XCTAssertEqual(actual, expected, file: file, line: line)
}
Expand All @@ -89,7 +89,7 @@ class RegexConsumerTests: XCTestCase {
_ expected: String,
file: StaticString = #file, line: UInt = #line
) {
let regex = try! Regex(compiling: regex)
let regex = try! Regex(regex)
let actual = string.replacing(regex, with: replacement)
XCTAssertEqual(actual, expected, file: file, line: line)
}
Expand All @@ -108,7 +108,7 @@ class RegexConsumerTests: XCTestCase {
}

func testAdHoc() {
let r = try! Regex(compiling: "a|b+")
let r = try! Regex("a|b+")

XCTAssert("palindrome".contains(r))
XCTAssert("botany".contains(r))
Expand Down Expand Up @@ -142,7 +142,7 @@ class RegexConsumerTests: XCTestCase {
let s = "aaa | aaaaaa | aaaaaaaaaa"
let s1 = s.dropFirst(6) // "aaaaaa | aaaaaaaaaa"
let s2 = s1.dropLast(17) // "aa"
let regex = try! Regex(compiling: "a+")
let regex = try! Regex("a+")

XCTAssertEqual(s.firstMatch(of: regex)?.0, "aaa")
XCTAssertEqual(s1.firstMatch(of: regex)?.0, "aaaaaa")
Expand Down
10 changes: 5 additions & 5 deletions Tests/RegexTests/MatchTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1290,11 +1290,11 @@ extension RegexTests {
04: Arkansas
05: California
"""
XCTAssertTrue(string.contains(try Regex(compiling: #"^\d+"#)))
XCTAssertEqual(string.ranges(of: try Regex(compiling: #"^\d+"#)).count, 1)
XCTAssertEqual(string.ranges(of: try Regex(compiling: #"(?m)^\d+"#)).count, 5)
XCTAssertTrue(string.contains(try Regex(#"^\d+"#)))
XCTAssertEqual(string.ranges(of: try Regex(#"^\d+"#)).count, 1)
XCTAssertEqual(string.ranges(of: try Regex(#"(?m)^\d+"#)).count, 5)

let regex = try Regex(compiling: #"^\d+: [\w ]+$"#)
let regex = try Regex(#"^\d+: [\w ]+$"#)
XCTAssertFalse(string.contains(regex))
let allRanges = string.ranges(of: regex.anchorsMatchLineEndings())
XCTAssertEqual(allRanges.count, 5)
Expand Down Expand Up @@ -1333,7 +1333,7 @@ extension RegexTests {
}

func testOptionMethods() throws {
let regex = try Regex(compiling: "c.f.")
let regex = try Regex("c.f.")
XCTAssertTrue ("cafe".contains(regex))
XCTAssertFalse("CaFe".contains(regex))

Expand Down