Skip to content

Commit a381b0d

Browse files
authored
Merge pull request #2151 from kimdv/kimdv/2072-make-the-switch-over-keywords-in-tokenprecedenceswift-exhaustive
Make keyword precedence exhaustive
2 parents 669eb75 + ad8eba6 commit a381b0d

File tree

1 file changed

+125
-8
lines changed

1 file changed

+125
-8
lines changed

Sources/SwiftParser/TokenPrecedence.swift

Lines changed: 125 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -192,29 +192,33 @@ enum TokenPrecedence: Comparable {
192192
case // Literals
193193
.Self, .false, .nil, .`self`, .super, .true:
194194
self = .identifierLike
195+
195196
// MARK: Expr keyword
196197
case // Keywords
197-
.as, .is, .try,
198+
.as, .is, .some, .try,
199+
.await, .each, .copy,
198200
// We don't know much about which contextual keyword it is, be conservative an allow considering it as unexpected.
199201
// Keywords in function types (we should be allowed to skip them inside parenthesis)
200-
.rethrows, .throws,
202+
.rethrows, .throws, .reasync, .async,
201203
// Consider 'any' a prefix operator to a type and a type is expression-like.
202204
.Any,
203205
// 'where' can only occur in the signature of declarations. Consider the signature expression-like.
204206
.where,
205207
// 'in' occurs in closure input/output definitions and for loops. Consider both constructs expression-like.
206208
.in:
207209
self = .exprKeyword
210+
208211
case // Control-flow constructs
209212
.defer, .do, .for, .guard, .if, .repeat, .switch, .while,
210213
// Secondary parts of control-flow constructs
211214
.case, .catch, .default, .else,
212215
// Return-like statements
213216
.break, .continue, .fallthrough, .return, .throw, .then, .yield:
214217
self = .stmtKeyword
218+
215219
// MARK: Decl keywords
216220
case // Types
217-
.associatedtype, .class, .enum, .extension, .protocol, .struct, .typealias,
221+
.associatedtype, .class, .enum, .extension, .protocol, .struct, .typealias, .actor, .macro,
218222
// Access modifiers
219223
.fileprivate, .internal, .private, .public, .static,
220224
// Functions
@@ -225,14 +229,127 @@ enum TokenPrecedence: Comparable {
225229
.operator, .precedencegroup,
226230
// Declaration Modifiers
227231
.__consuming, .final, .required, .optional, .lazy, .dynamic, .infix, .postfix, .prefix, .mutating, .nonmutating, .convenience, .override, .package, .open,
228-
.__setter_access, .indirect, .nonisolated, .distributed, ._local,
229-
.inout, ._mutating, ._borrowing, ._consuming,
232+
.__setter_access, .indirect, .isolated, .nonisolated, .distributed, ._local,
233+
.inout, ._mutating, ._borrow, ._borrowing, .borrowing, ._consuming, .consuming, .consume,
234+
// Accessors
235+
.get, .set, .didSet, .willSet, .unsafeAddress, .addressWithOwner, .addressWithNativeOwner, .unsafeMutableAddress,
236+
.mutableAddressWithOwner, .mutableAddressWithNativeOwner, ._read, ._modify,
230237
// Misc
231238
.import:
232239
self = .declKeyword
233-
default:
234-
// Treat all keywords that weren't handled above as expression keywords as a fallback option.
235-
// FIXME: We should assign a token precedence to all keywords
240+
241+
case // `TypeAttribute`
242+
._noMetadata,
243+
._opaqueReturnTypeOf,
244+
.autoclosure,
245+
.convention,
246+
.differentiable,
247+
.escaping,
248+
.noDerivative,
249+
.noescape,
250+
.Sendable,
251+
.unchecked:
252+
self = .exprKeyword
253+
254+
case // `DeclarationAttributeWithSpecialSyntax`
255+
._alignment,
256+
._backDeploy,
257+
._cdecl,
258+
._documentation,
259+
._dynamicReplacement,
260+
._effects,
261+
._expose,
262+
._implements,
263+
._nonSendable,
264+
._objcImplementation,
265+
._objcRuntimeName,
266+
._optimize,
267+
._originallyDefinedIn,
268+
._private,
269+
._projectedValueProperty,
270+
._semantics,
271+
._specialize,
272+
._spi,
273+
._spi_available,
274+
._swift_native_objc_runtime_base,
275+
._typeEraser,
276+
._unavailableFromAsync,
277+
.attached,
278+
.available,
279+
.backDeployed,
280+
.derivative,
281+
.exclusivity,
282+
.inline,
283+
.objc,
284+
.transpose:
285+
self = .exprKeyword
286+
287+
case // Treat all other keywords as expression keywords in the absence of any better information.
288+
.__owned,
289+
.__shared,
290+
._Class,
291+
._compilerInitialized,
292+
._const,
293+
._forward,
294+
._linear,
295+
._move,
296+
._NativeClass,
297+
._NativeRefCountedObject,
298+
._PackageDescription,
299+
._RefCountedObject,
300+
._Trivial,
301+
._TrivialAtMost,
302+
._underlyingVersion,
303+
._UnknownLayout,
304+
._version,
305+
.accesses,
306+
.any,
307+
.assignment,
308+
.associativity,
309+
.availability,
310+
.before,
311+
.block,
312+
.canImport,
313+
.compiler,
314+
.cType,
315+
.deprecated,
316+
.exported,
317+
.file,
318+
.discard,
319+
.forward,
320+
.higherThan,
321+
.initializes,
322+
.introduced,
323+
.kind,
324+
.left,
325+
.line,
326+
.linear,
327+
.lowerThan,
328+
.message,
329+
.metadata,
330+
.module,
331+
.noasync,
332+
.none,
333+
.obsoleted,
334+
.of,
335+
.Protocol,
336+
.renamed,
337+
.reverse,
338+
.right,
339+
.safe,
340+
.sourceFile,
341+
.spi,
342+
.spiModule,
343+
.swift,
344+
.target,
345+
.Type,
346+
.unavailable,
347+
.unowned,
348+
.visibility,
349+
.weak,
350+
.witness_method,
351+
.wrt,
352+
.unsafe:
236353
self = .exprKeyword
237354
}
238355
}

0 commit comments

Comments
 (0)