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
+35-14Lines changed: 35 additions & 14 deletions
Original file line number
Diff line number
Diff line change
@@ -14,6 +14,21 @@ let re = /[0-9]+/
14
14
15
15
This proposal helps complete the story told in [Regex Type and Overview][regex-type] and [elsewhere][pitch-status]. Literals are compiled directly, allowing errors to be found at compile time, rather than at run time. Using a literal also allows editors to support features such as syntax coloring inside the literal, highlighting sub-structure of the regex, and conversion of the literal to an equivalent result builder DSL (see [Regex builder DSL][regex-dsl]). It would be difficult to support all of this if regexes could only be defined inside a string.
16
16
17
+
A regex literal also allows for seamless composition with the Regex DSL, enabling the intermixing of a regex syntax with other elements of the builder:
18
+
19
+
```swift
20
+
// A regex literal for parsing an amount of currency in dollars or pounds.
21
+
let regex =Regex {
22
+
/([$£])/
23
+
TryCapture {
24
+
OneOrMore(.digit)
25
+
"."
26
+
Repeat(.digit, count: 2)
27
+
} transform: { Amount(twoDecimalPlaces: $0) }
28
+
}
29
+
```
30
+
31
+
This flexibility allows for terse matching syntax to be used when it's suitable, and more explicit syntax where clarity and strong types are required.
17
32
18
33
## Proposed solution
19
34
@@ -94,35 +109,41 @@ In order to help avoid further parsing ambiguities, a `/.../` regex literal will
94
109
95
110
#### Rationale
96
111
97
-
This is due to 2 main ambiguities. The first of which arises when a `/.../` regex literal starts a new line. This is particularly problematic for result builders, where we expect it to be frequently used, for example:
112
+
This is due to 2 main parsing ambiguities. The first of which arises when a `/.../` regex literal starts a new line. This is particularly problematic for result builders, where we expect it to be frequently used, in particular within a `Regex` builder:
This is parsed as a single operator chain, however it is likely the user is expecting a regex literal. To resolve this ambiguity, a regex literal may not start with a space or tab character. The above therefore remains an operator chain. This takes advantage of the fact that infix operators require consistent spacing on either side.
126
+
Instead of being parsed as 3 result builder elements, the second of which being a regex literal, this is instead parsed as a single operator chain with the operands `digit`, `[+-]`, and `digit`. This will therefore be diagnosed as semantically invalid.
127
+
128
+
To avoid this issue, a regex literal may not start with a space or tab character. This takes advantage of the fact that infix operators require consistent spacing on either side.
108
129
109
130
If a space or tab is needed as the first character, it must be either escaped, e.g:
0 commit comments