Skip to content

Commit bf7702f

Browse files
committed
Update pitch
- Add Source Compatibility section - Condense comment syntax ambiguity section - Mention `/.../` being less popular in some communities
1 parent e31262d commit bf7702f

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

Documentation/Evolution/DelimiterSyntax.md

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,6 @@ Due to the existing use of `/` in comment syntax and operators, there are some s
6262

6363
## Detailed design
6464

65-
### Upgrade path
66-
67-
Due to the source breaking changes needed for the `/.../` syntax, it will be introduced in Swift 6 mode. However, projects will be able to adopt it earlier by using the compiler flag `-enable-regex-literals`. Note this does not affect the extended literal `#/.../#`, which will be usable immediately.
68-
6965
### Named typed captures
7066

7167
Regex literals have their capture types statically determined by the capture groups present. This follows the same inference behavior as [the DSL][regex-dsl], and is explored in more detail in *[Strongly Typed Captures][strongly-typed-captures]*. One aspect of this that is currently unique to the literal is the ability to infer labeled tuple elements for named capture groups. For example:
@@ -147,26 +143,22 @@ This mode is supported with any (non-zero) number of `#` characters in the delim
147143

148144
### Ambiguities with comment syntax
149145

150-
Perhaps the most obvious parsing ambiguity with `/.../` delimiters is with comment syntax.
151-
152-
- An empty regex literal would conflict with line comment syntax `//`. But an empty regex isn't a particularly useful thing to express, and can be disallowed without significant impact.
146+
Line comment syntax `//` and block comment syntax `/*` will continue to be parsed as comments. An empty regex literal is not a particularly useful thing to express, but can be written as `#//#` if desired. `*` would be an invalid starting character of a regex, and therefore does not pose an issue.
153147

154-
- There is a conflict with block comment syntax, when surrounding a regex literal ending with `*`, for example:
148+
A parsing conflict does however arise when a block comment surrounds a regex literal ending with `*`, for example:
155149

156150
```swift
157151
/*
158152
let regex = /[0-9]*/
159153
*/
160154
```
161155

162-
In this case, the block comment would prematurely end on the second line, rather than extending all the way to the third line as the user would expect. This is already an issue today with `*/` in a string literal, though it is more likely to occur in a regex given the prevalence of the `*` quantifier. This issue can be avoided in many cases by using line comment syntax `//` instead, which it should be noted is the syntax that Xcode uses when commenting out multiple lines.
163-
164-
- Block comment syntax also means that a regex literal would not be able to start with the `*` character, however this is less of a concern as it would not be valid regex syntax.
156+
In this case, the block comment prematurely ends on the second line, rather than extending all the way to the third line as the user would expect. This is already an issue today with `*/` in a string literal, though it is more likely to occur in a regex given the prevalence of the `*` quantifier. This issue can be avoided in many cases by using line comment syntax `//` instead, which it should be noted is the syntax that Xcode uses when commenting out multiple lines.
165157

166158

167159
### Ambiguity with infix operators
168160

169-
There would be a minor ambiguity with infix operators used with regex literals. When used without whitespace, e.g `x+/y/`, the expression will be treated as using an infix operator `+/`. Whitespace is therefore required for regex literal interpretation, e.g `x + /y/`. Alternatively, extended literals may be used, e.g `x+#/y/#`.
161+
There is a minor ambiguity when infix operators are used with regex literals. When used without whitespace, e.g `x+/y/`, the expression will be treated as using an infix operator `+/`. Whitespace is therefore required for regex literal interpretation, e.g `x + /y/`. Alternatively, extended literals may be used, e.g `x+#/y/#`.
170162

171163
### Regex syntax limitations
172164

@@ -273,7 +265,7 @@ func baz(_ x: S) -> Int {
273265
}
274266
```
275267

276-
`foo(/, /)` is currently parsed as 2 unapplied operator arguments. `bar(/, 2) + bar(/, 3)` is currently parsed as two independent calls that each take an unapplied `/` operator reference. Both of these would become regex literals arguments, `/, /` and `/, 2) + bar(/` respectively (though the latter would produce a regex error).
268+
`foo(/, /)` is currently parsed as 2 unapplied operator arguments. `bar(/, 2) + bar(/, 3)` is currently parsed as two independent calls that each take an unapplied `/` operator reference. Both of these will become regex literals arguments, `/, /` and `/, 2) + bar(/` respectively (though the latter will produce a regex error).
277269

278270
To disambiguate these cases, users will need to surround at least the opening `/` with parentheses, e.g:
279271

@@ -290,6 +282,15 @@ This takes advantage of the fact that a regex literal will not be parsed if the
290282

291283
</details>
292284

285+
## Source Compatibility
286+
287+
As explored above, two source breaking changes are needed for `/.../` syntax:
288+
289+
- Deprecation of prefix operators containing the `/` character.
290+
- Parsing `/,` and `/]` as the start of a regex literal if a closing `/` is found, rather than an unapplied operator in an argument list. For example, `fn(/, /)` becomes a regex literal rather than two unapplied operator arguments.
291+
292+
As such, both these changes and the `/.../` syntax will be introduced in Swift 6 mode. However, projects will be able to adopt the syntax earlier by passing the compiler flag `-enable-bare-regex-syntax`. Note this does not affect the extended delimiter syntax `#/.../#`, which will be usable immediately.
293+
293294
## Future Directions
294295

295296
### Modern literal syntax
@@ -300,7 +301,7 @@ However, such a syntax would lose out on the familiarity benefits of standard re
300301

301302
## Alternatives Considered
302303

303-
Given the fact that `/` is an existing term of art for regular expressions, we feel it should be the preferred delimiter syntax. While it has some syntactic ambiguities, we do not feel that they are sufficient to disqualify the syntax. To evaluate this trade-off, below is a list of alternative delimiters that would not have the same ambiguities.
304+
Given the fact that `/.../` is an existing term of art for regular expressions, we feel it should be the preferred delimiter syntax. It should be noted that the syntax has become less popular in some communities such as Perl, however we still feel that it is a compelling choice, especially with extended delimiters `#/.../#`. Additionally, while there has some syntactic ambiguities, we do not feel that they are sufficient to disqualify the syntax. To evaluate this trade-off, below is a list of alternative delimiters that would not have the same ambiguities.
304305

305306
### Prefixed quote `re'...'`
306307

0 commit comments

Comments
 (0)