-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathforward-slash-regex.swift
501 lines (391 loc) · 15.6 KB
/
forward-slash-regex.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
// RUN: %target-typecheck-verify-swift -enable-bare-slash-regex -disable-availability-checking -typo-correction-limit 0
// REQUIRES: swift_swift_parser
// REQUIRES: concurrency
prefix operator /
prefix operator ^/
prefix operator /^/
prefix func ^/ <T> (_ x: T) -> T { x }
prefix operator !!
prefix func !! <T>(_ x: T) -> T { x }
prefix operator ^^
prefix func ^^ <T>(_ x: T) -> T { x }
precedencegroup P {
associativity: left
}
// The divisions in the body of the below operators make sure we don't try and
// consider them to be ending delimiters of a regex.
infix operator /^/ : P
func /^/ (lhs: Int, rhs: Int) -> Int { 1 / 2 }
infix operator /^ : P
func /^ (lhs: Int, rhs: Int) -> Int { 1 / 2 }
infix operator ^^/ : P
func ^^/ (lhs: Int, rhs: Int) -> Int { 1 / 2 }
let i = 0 /^/ 1/^/3
let x = /abc/
_ = /abc/
_ = /x/.self
_ = /\//
_ = /\\/
// This is just here to appease typo correction.
let y = 0
// These unfortunately become prefix `=` and infix `=/` respectively. We could
// likely improve the diagnostic though.
do {
let z=/0/
// expected-error@-1 {{type annotation missing in pattern}}
// expected-error@-2 {{consecutive statements on a line must be separated by ';'}}
// expected-error@-3 {{expected expression}}
}
do {
_=/0/
// expected-error@-1 {{'_' can only appear in a pattern or on the left side of an assignment}}
// expected-error@-2 {{cannot find operator '=/' in scope}}
}
// No closing '/' so a prefix operator.
_ = /x
// expected-error@-1 {{'/' is not a prefix unary operator}}
_ = !/x/
// expected-error@-1 {{cannot convert value of type 'Regex<Substring>' to expected argument type 'Bool'}}
_ = (!/x/)
// expected-error@-1 {{cannot convert value of type 'Regex<Substring>' to expected argument type 'Bool'}}
_ = !/ /
// expected-error@-1 {{regex literal may not start with space; add backslash to escape}}
// expected-error@-2 {{cannot convert value of type 'Regex<Substring>' to expected argument type 'Bool'}}
_ = !!/ /
// expected-error@-1 {{regex literal may not start with space; add backslash to escape}}
_ = !!/x/
_ = (!!/x/)
_ = /^)
// expected-error@-1 {{unterminated regex literal}}
// expected-error@-2 {{closing ')' does not balance any groups openings}}
_ = /x/! // expected-error {{cannot force unwrap value of non-optional type 'Regex<Substring>'}}
_ = /x/ + /y/ // expected-error {{binary operator '+' cannot be applied to two 'Regex<Substring>' operands}}
_ = /x/+/y/
// expected-error@-1 {{cannot find operator '+/' in scope}}
// expected-error@-2 {{'/' is not a postfix unary operator}}
_ = /x/?.blah
// expected-error@-1 {{cannot use optional chaining on non-optional value of type 'Regex<Substring>'}}
// expected-error@-2 {{value of type 'Regex<Substring>' has no member 'blah'}}
_ = /x/!.blah
// expected-error@-1 {{cannot force unwrap value of non-optional type 'Regex<Substring>'}}
// expected-error@-2 {{value of type 'Regex<Substring>' has no member 'blah'}}
do {
// expected-error@+2 {{cannot find operator '/?' in scope}}
// expected-error@+1 {{'/' is not a prefix unary operator}}
_ = /x /?
.blah
// expected-error@-1 {{cannot infer contextual base in reference to member 'blah'}}
}
_ = /x/? // expected-error {{cannot use optional chaining on non-optional value of type 'Regex<Substring>'}}
.blah // expected-error {{value of type 'Regex<Substring>' has no member 'blah'}}
_ = 0; /x/ // expected-warning {{regular expression literal is unused}}
do {
_ = 0; /x /
} // expected-error {{expected expression after operator}}
_ = /x/ ? 0 : 1 // expected-error {{cannot convert value of type 'Regex<Substring>' to expected condition type 'Bool'}}
do {
_ = /x / ? 0 : 1 // expected-error@:12 {{expected expression after operator}}
}
_ = .random() ? /x/ : .blah // expected-error {{type 'Regex<Substring>' has no member 'blah'}}
_ = /x/ ?? /x/ // expected-warning {{left side of nil coalescing operator '??' has non-optional type 'Regex<Substring>', so the right side is never used}}
do {
_ = /x / ?? /x / // expected-error {{unary operator cannot be separated from its operand}}
} // expected-error {{expected expression after operator}}
_ = /x/??/x/ // expected-error {{'/' is not a postfix unary operator}}
// expected-error@-1 2 {{cannot use optional chaining on non-optional value of type 'Regex<Substring>'}}
_ = /x/ ... /y/ // expected-error {{referencing operator function '...' on 'Comparable' requires that 'Regex<Substring>' conform to 'Comparable'}}
_ = /x/.../y/
// expected-error@-1 {{missing whitespace between '...' and '/' operators}}
// expected-error@-2 {{'/' is not a postfix unary operator}}
_ = /x/...
// expected-error@-1 {{unary operator '...' cannot be applied to an operand of type 'Regex<Substring>'}}
do {
_ = /x /...
// expected-error@-1 {{'/' is not a prefix unary operator}}
// expected-error@-2 {{consecutive statements on a line must be separated by ';'}}
// expected-error@-3 {{operator with postfix spacing cannot start a subexpression}}
} // expected-error {{expected expression}}
do {
_ = true / false /; // expected-error {{expected expression after operator}}
}
_ = "\(/x/)"
func defaulted(x: Regex<Substring> = /x/) {}
func foo<T>(_ x: T, y: T) {} // expected-note {{'foo(_:y:)' declared here}}
foo(/abc/, y: /abc/)
// TODO: The parser ought to have better recovery in cases where a binary
// operator chain is missing an operand, currently we throw everything away.
foo(/abc/, y: /abc /)
// expected-error@-1:21 {{expected expression after operator}}
// expected-error@-2 {{missing argument for parameter 'y' in call}}
func bar<T>(_ x: inout T) {}
bar(&/x/) // expected-error {{cannot pass immutable value as inout argument: literals are not mutable}}
struct S {
subscript(x: Regex<Substring>) -> Void { () } // expected-note {{'subscript(_:)' declared here}}
subscript(fn: (Int, Int) -> Int) -> Int { 0 }
}
func testSubscript(_ x: S) {
x[/x/]
x[/x /]
// expected-error@-1:9 {{expected expression after operator}}
// expected-error@-2 {{missing argument for parameter #1 in subscript}}
_ = x[/] / 2
}
func testReturn() -> Regex<Substring> {
if .random() {
return /x/
}
return /x /
} // expected-error {{expected expression after operator}}
func testThrow() throws {
throw /x/ // expected-error {{thrown expression type 'Regex<Substring>' does not conform to 'Error'}}
}
do {
_ = [/abc/, /abc /] // expected-error@:21 {{expected expression after operator}}
}
do {
_ = [/abc /: /abc /]
// expected-error@-1:14 {{expected expression after operator}}
}
_ = [/abc/:/abc/] // expected-error {{generic struct 'Dictionary' requires that 'Regex<Substring>' conform to 'Hashable'}}
_ = [/abc/ : /abc/] // expected-error {{generic struct 'Dictionary' requires that 'Regex<Substring>' conform to 'Hashable'}}
_ = [/abc/:/abc/] // expected-error {{generic struct 'Dictionary' requires that 'Regex<Substring>' conform to 'Hashable'}}
_ = [/abc/: /abc/] // expected-error {{generic struct 'Dictionary' requires that 'Regex<Substring>' conform to 'Hashable'}}
_ = (/abc/, /abc/)
_ = ((/abc/))
do {
_ = ((/abc /))
// expected-error@-1:15 {{expected expression after operator}}
}
_ = { /abc/ }
_ = {
/abc/
}
let _: () -> Int = {
0
/ 1 /
2
}
let _: () -> Int = {
0
/1 / // expected-error {{'/' is not a prefix unary operator}}
2
}
_ = {
0 // expected-warning {{integer literal is unused}}
/1/ // expected-warning {{regular expression literal is unused}}
2 // expected-warning {{integer literal is unused}}
}
// Operator chain, as a regex literal may not start with space.
_ = 2
/ 1 / .bitWidth
_ = 2
/1/ .bitWidth // expected-error {{value of type 'Regex<Substring>' has no member 'bitWidth'}}
_ = 2
/ 1 /
.bitWidth
_ = 2
/1 /
.bitWidth
// expected-error@-2 {{'/' is not a prefix unary operator}}
_ = !!/1/ .bitWidth // expected-error {{value of type 'Regex<Substring>' has no member 'bitWidth'}}
_ = !!/1 / .bitWidth // expected-error {{cannot find operator '!!/' in scope}}
let z =
/y/
// While '.' is technically an operator character, it seems more likely that
// the user hasn't written the member name yet.
_ = 0. / 1 / 2 // expected-error {{expected member name following '.'}}
_ = 0 . / 1 / 2 // expected-error {{expected member name following '.'}}
switch "" {
case _ where /x/:
// expected-error@-1 {{cannot convert value of type 'Regex<Substring>' to expected condition type 'Bool'}}
break
default:
break
}
do {} catch /x/ {}
// expected-error@-1 {{expression pattern of type 'Regex<Substring>' cannot match values of type 'any Error'}}
// expected-warning@-2 {{'catch' block is unreachable because no errors are thrown in 'do' block}}
switch /x/ {
default:
break
}
if /x/ {} // expected-error {{cannot convert value of type 'Regex<Substring>' to expected condition type 'Bool'}}
if /x/.smth {} // expected-error {{value of type 'Regex<Substring>' has no member 'smth'}}
func testGuard() {
guard /x/ else { return } // expected-error {{cannot convert value of type 'Regex<Substring>' to expected condition type 'Bool'}}
}
for x in [0] where /x/ {} // expected-error {{cannot convert value of type 'Regex<Substring>' to expected condition type 'Bool'}}
typealias Magic<T> = T
_ = /x/ as Magic
_ = /x/ as! String // expected-warning {{cast from 'Regex<Substring>' to unrelated type 'String' always fails}}
_ = type(of: /x/)
do {
let /x/ // expected-error {{expected pattern}}
}
do {
_ = try /x/; _ = try /x /
// expected-warning@-1 {{no calls to throwing functions occur within 'try' expression}}
} // expected-error {{expected expression after operator}}
do {
_ = try? /x/; _ = try? /x /
// expected-warning@-1 {{no calls to throwing functions occur within 'try' expression}}
} // expected-error {{expected expression after operator}}
do {
_ = try! /x/; _ = try! /x /
// expected-warning@-1 {{no calls to throwing functions occur within 'try' expression}}
} // expected-error {{expected expression after operator}}
_ = await /x/ // expected-warning {{no 'async' operations occur within 'await' expression}}
/x/ = 0 // expected-error {{cannot assign to value: literals are not mutable}}
/x/() // expected-error {{cannot call value of non-function type 'Regex<Substring>'}}
// We treat the following as comments, as it seems more likely the user has
// written a comment and is still in the middle of writing the characters before
// it.
_ = /x// comment
// expected-error@-1 {{'/' is not a prefix unary operator}}
_ = /x // comment
// expected-error@-1 {{'/' is not a prefix unary operator}}
_ = /x/*comment*/
// expected-error@-1 {{'/' is not a prefix unary operator}}
// MARK: Unapplied operators
// These become regex literals, unless last character is space, or are surrounded in parens.
func baz(_ x: (Int, Int) -> Int, _ y: (Int, Int) -> Int) {} // expected-note 3{{'baz' declared here}}
baz(/, /)
baz(/,/)
// expected-error@-1 {{cannot convert value of type 'Regex<Substring>' to expected argument type '(Int, Int) -> Int'}}
// expected-error@-2 {{missing argument for parameter #2 in call}}
baz((/), /)
baz(/^, /)
baz(/^,/)
// expected-error@-1 {{cannot convert value of type 'Regex<Substring>' to expected argument type '(Int, Int) -> Int'}}
// expected-error@-2 {{missing argument for parameter #2 in call}}
baz((/^), /)
baz(^^/, /)
baz(^^/,/) // expected-error {{missing argument for parameter #2 in call}}
baz((^^/), /)
func bazbaz(_ x: (Int, Int) -> Int, _ y: Int) {}
bazbaz(/, 0)
bazbaz(^^/, 0)
func qux<T>(_ x: (Int, Int) -> Int, _ y: T) -> Int { 0 }
_ = qux(/, 1) / 2
do {
_ = qux(/, "(") / 2
_ = qux(/, "(")/2
// expected-error@-1 {{cannot convert value of type 'Regex<(Substring, Substring)>' to expected argument type '(Int, Int) -> Int'}}
// expected-error@-2:19 {{expected ',' separator}}
}
_ = qux((/), "(") / 2
_ = qux(/, 1) // this comment tests to make sure we don't try and end the regex on the starting '/' of '//'.
_ = qux(/, 1) /* same thing with a block comment */
@discardableResult
func quxqux(_ x: (Int, Int) -> Int) -> Int { 0 }
quxqux(/^/) // expected-error {{cannot convert value of type 'Regex<Substring>' to expected argument type '(Int, Int) -> Int'}}
quxqux((/^/)) // expected-error {{cannot convert value of type 'Regex<Substring>' to expected argument type '(Int, Int) -> Int'}}
quxqux({ $0 /^/ $1 })
quxqux(!/^/)
// expected-error@-1 {{cannot convert value of type 'Bool' to expected argument type '(Int, Int) -> Int'}}
// expected-error@-2 {{cannot convert value of type 'Regex<Substring>' to expected argument type 'Bool'}}
quxqux(/^)
_ = quxqux(/^) / 1
let arr: [Double] = [2, 3, 4]
_ = arr.reduce(1, /) / 3
_ = arr.reduce(1, /) + arr.reduce(1, /)
// MARK: ')' disambiguation behavior
_ = (/x)
// expected-error@-1 {{'/' is not a prefix unary operator}}
_ = (/x)/
// expected-error@-1 {{'/' is not a prefix unary operator}}
// expected-error@-2 {{'/' is not a postfix unary operator}}
_ = (/[(0)])/
// expected-error@-1 {{'/' is not a prefix unary operator}}
// expected-error@-2 {{'/' is not a postfix unary operator}}
_ = /[(0)]/
_ = /(x)/
_ = /[)]/
_ = /[a\])]/
_ = /([)])/
_ = /]]][)]/
_ = /
// expected-error@-1 {{unterminated regex literal}}
_ = /)
// expected-error@-1 {{unterminated regex literal}}
// expected-error@-2 {{closing ')' does not balance any groups openings}}
let fn: (Int, Int) -> Int = (/)
_ = /\()/
// expected-error@-1 {{'/' is not a prefix unary operator}}
// expected-error@-2 {{'/' is not a postfix unary operator}}
// expected-error@-3 {{invalid component of Swift key path}}
do {
let _: Regex = (/whatever\)/
// expected-note@-1 {{to match this opening '('}}
} // expected-error {{expected ')' in expression list}}
do {
_ = /(()()))/
// expected-error@-1 {{'/' is not a prefix unary operator}}
// expected-error@-2 {{consecutive statements on a line must be separated by ';'}}
// expected-error@-3 {{expected expression}}
// expected-error@-4 {{cannot call value of non-function type '()'}}
}
do {
_ = /[x])/
// expected-error@-1 {{'/' is not a prefix unary operator}}
// expected-error@-2 {{consecutive statements on a line must be separated by ';'}}
// expected-error@-3 {{expected expression}}
}
do {
_ = /[\]])/
// expected-error@-1 {{expected expression path in Swift key path}}
}
_ = ^/x/
// expected-error@-1 {{'^' is not a prefix unary operator}}
_ = (^/x)/
// expected-error@-1 {{'/' is not a postfix unary operator}}
_ = (!!/x/)
_ = ^/"/"
// expected-error@-1 {{'^' is not a prefix unary operator}}
// expected-error@-2 {{unterminated string literal}}
_ = ^/"[/"
// expected-error@-1 {{'^' is not a prefix unary operator}}
// expected-error@-2 {{unterminated string literal}}
_ = (^/)("/")
// MARK: Starting characters
// Fine.
_ = /./
// You need to escape if you want a regex literal to start with these characters.
_ = /\ /
_ = / / // expected-error {{regex literal may not start with space; add backslash to escape}} {{6-6=\}}
_ = / /
// expected-error@-1 {{regex literal may not start with space; add backslash to escape}} {{6-6=\}}
// expected-error@-2 {{regex literal may not end with space; use extended literal instead}} {{5-5=#}} {{9-9=#}}
_ = #/ /#
_ = /x\ /
_ = /\ \ /
// There are intentionally trailing spaces here
_ = /
// expected-error@-1 {{unterminated regex literal}}
// expected-error@-2 {{regex literal may not start with space; add backslash to escape}} {{6-6=\}}
// There are intentionally trailing spaces here
_ = /^
// expected-error@-1 {{unterminated regex literal}}
_ = /\)/
_ = /)/ // expected-error {{closing ')' does not balance any groups openings}}
_ = /,/
_ = /}/
_ = /]/
_ = /:/
_ = /;/
// Don't emit diagnostics here, as we re-lex.
_ = /0xG/
_ = /0oG/
_ = /"/
_ = /'/
_ = /<#placeholder#>/
_ = ^^/0xG/
_ = ^^/0oG/
_ = ^^/"/
_ = ^^/'/
_ = ^^/<#placeholder#>/
_ = (^^/0xG/)
_ = (^^/0oG/)
_ = (^^/"/)
_ = (^^/'/)
_ = (^^/<#placeholder#>/)