|
16 | 16 |
|
17 | 17 | A regular expression literal will be introduced using `/.../` delimiters, within which the compiler will parse a regular expression (the details of which are outlined in [the Regex Syntax pitch][internal-syntax]):
|
18 | 18 |
|
19 |
| -``` |
| 19 | +```swift |
20 | 20 | // Matches "<identifier> = <hexadecimal value>", extracting the identifier and hex number
|
21 | 21 | let regex = /([[:alpha:]]\w*) = ([0-9A-F]+)/
|
22 | 22 | ```
|
23 | 23 |
|
24 |
| -Forward slashes are a regex term of art, and are used as the delimiters for regex literals in Perl, JavaScript and Ruby (though Perl and Ruby also provide alternative choices). Due to its existing use in comment syntax and operators, there are some syntactic ambiguities to consider. While there are quite a few cases to consider, we do not feel that the impact of any individual case is particularly high. |
| 24 | +Forward slashes are a regex term of art, and are used as the delimiters for regex literals in Perl, JavaScript and Ruby (though Perl and Ruby also provide alternatives). Their ubiquity and familiarity makes them a compelling choice for Swift. |
25 | 25 |
|
26 |
| -**TODO: Do we want to present a stronger argument for `/.../`?** |
| 26 | +Due to the existing use of `/` in comment syntax and operators, there are some syntactic ambiguities to consider. While there are quite a few cases to consider, we do not feel that the impact of any individual case is sufficient to disqualify the syntax. |
27 | 27 |
|
28 | 28 | **TODO: Anything else we want to say here before segueing into the massive list?**
|
29 | 29 |
|
@@ -182,22 +182,22 @@ Additionally, introducing this syntax would introduce an inconsistency with raw
|
182 | 182 |
|
183 | 183 | ## Future Directions
|
184 | 184 |
|
185 |
| -**TODO: What do we want to say here?** |
| 185 | +**TODO: What do we want to say here? Talk about raw and multiline? Don't really have a good option for the latter tho** |
186 | 186 |
|
187 | 187 | ## Alternatives Considered
|
188 | 188 |
|
189 | 189 | ### Prefixed quote `re'...'`
|
190 | 190 |
|
191 | 191 | We could choose to use `re'...'` delimiters, for example:
|
192 | 192 |
|
193 |
| -``` |
| 193 | +```swift |
194 | 194 | // Matches "<identifier> = <hexadecimal value>", extracting the identifier and hex number
|
195 | 195 | let regex = re'([[:alpha:]]\w*) = ([0-9A-F]+)'
|
196 | 196 | ```
|
197 | 197 |
|
198 | 198 | The use of two letter prefix could potentially be used as a namespace for future literal types. However, it is unusual for a Swift literal to be prefixed in this way.
|
199 | 199 |
|
200 |
| -**TODO: Fill in reasons why not to pick this** |
| 200 | +**TODO: Any other reasons why not to pick this?** |
201 | 201 |
|
202 | 202 | **TODO: Mention that it nicely extends to raw and multiline?**
|
203 | 203 |
|
@@ -225,7 +225,7 @@ We could help distinguish it from a string literal by requiring e.g `'/.../'`, t
|
225 | 225 |
|
226 | 226 | We could opt for for a more explicitly spelled out literal syntax such as `#regex(...)`. This is a more heavyweight option, similar to `#selector(...)`. As such, it may be considered syntactically noisy as e.g a function argument `str.match(#regex([abc]+))` vs `str.match(/[abc]+/)`.
|
227 | 227 |
|
228 |
| -Such a syntax would require the containing regex to correctly balance capture group parentheses, otherwise the rest of the line might be incorrectly considered a regex. This could place additional cognitive burden on the user, and may lead to an awkward typing experience. For example, if the user is editing a previously written regex, the syntax highlighting for the rest of the line may change, and unhelpful spurious errors may be reported. With a different delimiter, the compiler would be able to detect and better diagnose unbalanced parentheses in the regex. |
| 228 | +Such a syntax would require the containing regex to correctly balance parentheses for groups, otherwise the rest of the line might be incorrectly considered a regex. This could place additional cognitive burden on the user, and may lead to an awkward typing experience. For example, if the user is editing a previously written regex, the syntax highlighting for the rest of the line may change, and unhelpful spurious errors may be reported. With a different delimiter, the compiler would be able to detect and better diagnose unbalanced parentheses in the regex. |
229 | 229 |
|
230 | 230 | We could avoid the parenthesis balancing issue by requiring an additional internal delimiter such as `#regex(/.../)`. However it is even more heavyweight, and it may be unclear that `/` is part of the delimiter rather than part of the literal. Alternatively, we could replace the internal delimiter with another character such as ```#regex`...` ```, `#regex{...}`, or `#regex/.../`. However those would be inconsistent with the existing `#literal(...)` syntax and the first two would overload the existing meanings for the ``` `` ``` and `{}` delimiters.
|
231 | 231 |
|
|
0 commit comments