You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Documentation/Evolution/DelimiterSyntax.md
+10-5Lines changed: 10 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -21,7 +21,7 @@ let regex = re'([[:alpha:]]\w*) = ([0-9A-F]+)'
21
21
22
22
The use of a two letter prefix allows for easy future extensibility of such literals, by allowing different prefixes to indicate different types of literal. **TODO: examples**
23
23
24
-
### Regex limitations
24
+
### Regex syntax limitations
25
25
26
26
There are a few items of regex grammar that use the single quote character as a metacharacter. These include named group definitions and references such as `(?'name')`, `(?('name'))`, `\g'name'`, `\k'name'`, as well as callout syntax `(?C'arg')`. The use of a single quote conflicts with the `re'...'` delimiter as it will be considered the end of the literal. Fortunately, alternative syntax exists for all of these constructs, e.g `(?<name>)`, `\k<name>`, and `(?C"arg")`.
27
27
@@ -88,7 +88,7 @@ The obvious parsing ambiguity with `/.../` delimiters is with comment syntaxes.
88
88
89
89
- Finally, 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 `x + /y/` for regex literal interpretation.
90
90
91
-
#### Regex limitations
91
+
#### Regex syntax limitations
92
92
93
93
In order to help avoid further parsing ambiguities, a regex literal will not be parsed if it starts with a space, tab, or `)` character. Though the latter is already invalid regex syntax.
94
94
@@ -141,7 +141,7 @@ However we feel that starting a regex with a comma is likely to be a common case
141
141
In addition to ambiguities listed above, there are also some parsing ambiguities that would require the following language changes:
142
142
143
143
- Deprecation of prefix operators containing the `/` character.
144
-
-Potentially parsing `/,` as the start of a regex literal rather than an unapplied operator in an argument list. For example, `fn(/, /)` becomes a regex literal rather than 2 unapplied operator arguments. **TODO: Or do we want to ban it as the starting character? Seems like a common regex case**
144
+
-Parsing `/,` 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 2 unapplied operator arguments. **TODO: Or do we want to ban it as the starting character? Seems like a common regex case**
145
145
146
146
<details><summary>Rationale</summary>
147
147
@@ -185,13 +185,18 @@ The above case seems uncommon, however note this may also occur when the closing
185
185
foo(/, 2) +foo(/, 3)
186
186
```
187
187
188
-
This would also become a regex literal, i.e it would be parsed as the argument `/, 2) + foo(/`.
188
+
This would also become a regex literal, i.e it would be parsed as the argument `/, 2) + foo(/`. If users wish to disambiguate, they will need to surround at least the opening `/` with parentheses, e.g:
189
+
190
+
```swift
191
+
foo((/), 2) +foo(/, 3)
192
+
```
193
+
194
+
This takes advantage of the fact that a regex literal will not be parsed if the first character is `)`.
0 commit comments