Skip to content

Commit d1d0d57

Browse files
committed
Add comma disambiguation
1 parent 945219e commit d1d0d57

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

Documentation/Evolution/DelimiterSyntax.md

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ let regex = re'([[:alpha:]]\w*) = ([0-9A-F]+)'
2121

2222
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**
2323

24-
### Regex limitations
24+
### Regex syntax limitations
2525

2626
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")`.
2727

@@ -88,7 +88,7 @@ The obvious parsing ambiguity with `/.../` delimiters is with comment syntaxes.
8888

8989
- 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.
9090

91-
#### Regex limitations
91+
#### Regex syntax limitations
9292

9393
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.
9494

@@ -141,7 +141,7 @@ However we feel that starting a regex with a comma is likely to be a common case
141141
In addition to ambiguities listed above, there are also some parsing ambiguities that would require the following language changes:
142142

143143
- 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**
145145

146146
<details><summary>Rationale</summary>
147147

@@ -185,13 +185,18 @@ The above case seems uncommon, however note this may also occur when the closing
185185
foo(/, 2) + foo(/, 3)
186186
```
187187

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 `)`.
189195

190196
**TODO: More cases from slack discussion **
191197

192198
`foo(/, "(") / 2` !!!
193199

194-
195200
</details>
196201

197202
#### Editor Considerations

0 commit comments

Comments
 (0)