-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathgrammar-test.swift
335 lines (295 loc) · 9.28 KB
/
grammar-test.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
#!/usr/bin/swift
// a comment
/* a block comment */noLongerAComment()
/*/ still just a block comment */noLongerAComment()
/**/thatWasATinyBlockComment()
/* block comments /* can be nested, */ like this! */noLongerAComment()
import Foo // whitespace ok
import Foo.Submodule
import func Foo.Submodule.`func`
import func Control.Monad.>>=
// MARK: Conditional compilation / compiler directives
#if false // a comment
This is not code.
#elseif false // a comment
This isn't either.
#else // a comment
thisIsCode() // a comment
#elseif os(macOS) || os(Linux) || foo_flag || arch(x86_64) && 1+2 && swift(>=4.2.6) //a comment
#elseif compiler(<5) || canImport(Foundation) || targetEnvironment(simulator) || targetEnvironment(UIKitForMac)
#endif
#sourceLocation(file: "foo", line: 123) // a comment
if #available(macOS 10.12, iOS 9.1.2, UIKitForMac 1, *) {}
if #unavailable(iOS 13, *) { loadMainWindow() }
#selector(MyClass.func)
#selector(getter: MyClass.func) #selector(setter: MyClass.func)
#keyPath(self.parent.name)
#colorLiteral(), #imageLiteral(), #fileLiteral()
#file, #line, #function, #dsohandle, #filePath
__FILE__, __LINE__, __FUNCTION__, __DSO_HANDLE__
// MARK: Attributes
@available(
macOS 1.2, macOSApplicationExtension 1.2, OSX, tvOS 1.4, iOS, watchOS,
swift 5, UIKitForMac,
noasync,
introduced, introduced: 1,
deprecated, deprecated: 1,
obsoleted, obsoleted: 1,
message, message: "don't use this",
renamed, renamed: "somethingElse",
*, unavailable: no args)
@objc(thisIs:aSelector:) @objc(forgotAColon:afterThis)
@arbitraryAttr(with args)
// MARK: Builtins
x.dropFirst, x.dropFirst(3), x.dropFirst { /* no closure param */ }
x.contains, x.contains(y), x.contains { $0 == y }
autoreleasepool { }
withExtendedLifetime { /* requires an arg */ }
withExtendedLifetime(x) { }
Process.foo, Process.argc, Process.unsafeArgv, Foo.argc
obj+startIndex, obj.startIndex
func foo() -> Never { fatalError() }
// MARK: Types
func foo(
builtin: Int, x: String, x: Sequence,
optional: Int!, x: Int?, x: Int!?!,
collection: Int, x: [Int], x: [Int: String], x: [Int: String: Invalid],
tuple: (Int, [Int], [Int: String], [Int: String: Invalid]),
boundGeneric: Any<Int, String, (Int, Never)>, differsFrom invalid: Int, String,
function: Int -> Void, x: (Int) throws -> String, x: (@escaping (Int) throws -> Void) rethrows -> Int,
writeback: inout Int,
variadic: Int...,
composition: Sequence & Collection, oldStyle: protocol<Sequence, Collection>,
metatype: Foo.Type, x: Foo.Protocol
){}
func opaqueTypes() -> some View {}
struct Foo {
let some: Int? = .some(42)
var body: some View
}
// MARK: Type definitions
struct Foo { }
class Foo { }
class Foo: Bar { }
class Foo<T where T: Equatable>: Bar { }
class Foo<T>: Bar where T: Equatable { }
class `var` {}
class var x: Int
protocol Foo {
associatedtype T: Equatable
associatedtype T = Int
associatedtype T: Equatable = Int
func f<T: P3>(_: T) where T.A == Self.A, T.A: C // trailing comment still allows where to end
func functionBodyNotAllowedHere<T>() async throws -> Int {}
init(norHere: Int) async throws {}
init(norHere: Int) throws async {}
}
protocol Foo: Equatable {}
protocol Foo: Equatable, Indexable {}
protocol Foo: class, Equatable {}
protocol SE0142 : Sequence where Iterator.Element == Int { associatedtype Foo }
protocol SE0142 {
associatedtype Iterator : IteratorProtocol
associatedtype SubSequence : Sequence where SubSequence.Iterator.Element == Iterator.Element
}
protocol Foo { init(x: Int) }
func bar() { /* this is valid */ }
enum Foo {
case foo
case foo, bar baz
case foo,
bar
case foo(Int), bar(val: Int, labelNotAllowed val2: Int), baz(val: Int)
case foo(_ x: Int)
indirect case foo
case rawValue = 42, xx = "str", xx = true, xx = [too, complex], xx
}
typealias Foo = Bar
typealias Foo<T> = Bar<T, Int> // comment
// MARK: Actors
actor BankAccount {
let accountNumber: Int
var balance: Double
init(accountNumber: Int, initialDeposit: Double) {
self.accountNumber = accountNumber
self.balance = initialDeposit
}
}
@objc actor MyActor {
let accountNumber: Int
nonisolated let accountNumber: Int
}
extension BankAccount {
func deposit(amount: Double, to account: isolated BankAccount)
nonisolated func safeAccountNumberDisplayString() -> String
nonisolated var description: String {}
}
// MARK: Extensions
extension T {}
extension String {}
extension Array: Equatable {}
extension Array where Element: Equatable, Foo == Int {}
extension Array: Equatable, Foo where Element: Equatable, Foo == Int {}
// MARK: Functions
func something(
_ unlabeledArg: Int,
label separateFromInternalName: Int,
labelSameAsInternalName: Int
missed: a comma,
foo: bar,
){}
func foo() -> Int {}
func foo() throws -> (Int, String) {}
func foo() rethrows {}
func +++(arg: Int) {}
func `func`(arg: Int){}
func generic<T>(arg: Int){}
func ++<T>(arg: Int){}
func < <T>(arg: Int){}
func <<T>(arg: Int){}
func <+<<T>(arg: Int){}
// MARK: SE-0335 Existential `any`
let p1: P = S() // error
let p2: any P = S() // okay
let pq1: P & Q = S() // error
let pq2: any P & Q = S() // okay
let pObject: any AnyObject & P = C()
let existentialMetatype: any P.Type = S.self
let compositionMetatype: any (P & Q).Type = S.self
let protocolMetatype: (any P).Type = (any P).self
typealias AnyP = any P
let any = any // any is still usable as an identifier
// MARK: async/await
func foo() async {
let x = await y
let z = async // async is only a contextual keyword
let newURL = await server.redirectURL(for: url)
let (data, response) = try await session.dataTask(with: newURL)
let (data, response) = await try session.dataTask(with: newURL) // not allowed
let (data, response) = await (try session.dataTask(with: newURL)) // ok
async let dog = getDoggo()
async let pup: Dog = getDoggo()
}
callMe { // async closure
async let hello = greet()
return await hello
}
func foo() async -> Int {}
func foo() async throws -> (Int, String) {}
func foo() throws async -> (Int, String) {}
func foo() async rethrows {}
func foo() rethrows async {}
init() throws async {}
struct FunctionTypes {
var syncNonThrowing: () -> Void
var syncThrowing: () throws -> Void
var asyncNonThrowing: () async -> Void
var asyncThrowing: () async throws -> Void = x
}
let closure = { _ = await getInt() } // implicitly async
let closure = { (x: Int) async -> Int in 42 } // explicitly async
let closure = { (x: Int) throws -> Int in 42 }
let closure = { (x: Int) rethrows -> Int in 42 }
let closure = { (x: Int) async throws -> Int in 42 }
let closure = { (x: Int) throws async -> Int in 42 }
init(arg: Value) {}
init<T>(arg: Value) {}
func generic<A, B, C>() {}
func generic<OldStyle where T: Equatable>(arg: Int) throws -> Int {}
func generic<NewStyle>(arg: Int) throws -> Int where T: Equatable, T == Int {}
// MARK: Operators
x+y, x++y, x +++ y
x...y // TODO: probably shouldn't be variable
x..<y
x<<.y // not a dot operator
x?.y, x!.y
// old style
infix operator *.* { associativity left precedence 100 assignment }
// new style
infix operator *.* : AssignmentPrecedence { invalid }
precedencegroup ExamplePrecedence {
higherThan: LogicalConjunctionPrecedence
lowerThan: SomeOtherPrecedence
associativity: left assignment: true
}
// MARK: Other expressions
compoundFunctionName(_:arg1:arg2:), #selector(foo(bar:))
functionCall(arg1: "stuff", labels notRecognized: "stuff")
let tuple = (arg1: "stuff", labels notRecognized: "stuff")
subscriptCall[arg1: "stuff", labels notRecognized: "stuff"]
foo(a ? b : c)
foo(a ?, b : c)
foo(flag ? foo as Bar : nil)
foo(flag ? foo : nil, bar: nil)
foo(
flag ?
foo :
nil,
bar: nil
)
foo(
flag
? foo
: nil,
bar: nil
)
0.1, -4_2.5, 6.022e23, 10E-5
-0x1.ap2_3, 0x31p-4
0b010, 0b1_0
0o1, 0o7_3
02, 3_456
0x4, 0xF_7
0x1p, 0x1p_2, 0x1.5pa, 0x1.1p+1f, 0x1pz, 0x1.5w
0x1.f, 0x1.property
-.5, .2f
1.-.5
0b_0_1, 0x_1p+3q
tuple.0, tuple.42
0b12.5, 0xG
print("a\0b\nc\u{1}d \(interpolation) a \(1 + foo(x: 4)) nested: \(1+"string"+2) x"#)
print(#"raw: a\0b\nc\u{1}d \(interpolation) a \(1 + foo(x: 4)) nested: \(1+"string"+2) x"##)
print(#"raw: a\#0b\#nc\#u{1}d \#(interpolation) a \#(1 + foo(x: 4)) nested: \#(1+"string"+2) x"##)
print(##"raw: a\#0b\#nc\#u{1}d \#(interpolation) a \#(1 + foo(x: 4)) nested: \#(1+"string"+2) x"###)
print(##"raw: a\##0b\##nc\##u{1}d \##(interpolation) a \##(1 + foo(x: 4)) nested: \##(1+"string"+2) x"###)
"invalid newline
"
#"invalid newline
"#
##"invalid newline
"##
let SE0168 = """ illegal
my, what a large…
\(1 + foo(x: 4))
\("""
more \( """
s
""")
""")
…string you have!
illegal"""
let SE0168 = #""" illegal
my, what a large…
\#(1 + foo(x: 4))
\#(#"""
more \#( #"""
s
"""#)
"""#)
…string you have!
illegal"""#
let SE0168 = ##""" illegal
my, what a large…
\#(1 + foo(x: 4))
\#(#"""
more \#( #"""
s
"""#)
"""#)
…string you have!
illegal"""##
associatedtype, class, deinit, enum, extension, func, import, init, inout,
let, operator, $123, precedencegroup, protocol, struct, subscript, typealias,
var, fileprivate, internal, private, public, static, defer, if, guard, do,
repeat, else, for, in, while, return, break, continue, as?, fallthrough,
switch, case, default, where, catch, as, Any, false, is, nil, rethrows,
super, self, Self, throw, true, try, throws, nil