Skip to content

Make keyword precedence exhaustive #2151

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 125 additions & 8 deletions Sources/SwiftParser/TokenPrecedence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -192,29 +192,33 @@ enum TokenPrecedence: Comparable {
case // Literals
.Self, .false, .nil, .`self`, .super, .true:
self = .identifierLike

// MARK: Expr keyword
case // Keywords
.as, .is, .try,
.as, .is, .some, .try,
.await, .each, .copy,
// We don't know much about which contextual keyword it is, be conservative an allow considering it as unexpected.
// Keywords in function types (we should be allowed to skip them inside parenthesis)
.rethrows, .throws,
.rethrows, .throws, .reasync, .async,
// Consider 'any' a prefix operator to a type and a type is expression-like.
.Any,
// 'where' can only occur in the signature of declarations. Consider the signature expression-like.
.where,
// 'in' occurs in closure input/output definitions and for loops. Consider both constructs expression-like.
.in:
self = .exprKeyword

case // Control-flow constructs
.defer, .do, .for, .guard, .if, .repeat, .switch, .while,
// Secondary parts of control-flow constructs
.case, .catch, .default, .else,
// Return-like statements
.break, .continue, .fallthrough, .return, .throw, .then, .yield:
self = .stmtKeyword

// MARK: Decl keywords
case // Types
.associatedtype, .class, .enum, .extension, .protocol, .struct, .typealias,
.associatedtype, .class, .enum, .extension, .protocol, .struct, .typealias, .actor, .macro,
// Access modifiers
.fileprivate, .internal, .private, .public, .static,
// Functions
Expand All @@ -225,14 +229,127 @@ enum TokenPrecedence: Comparable {
.operator, .precedencegroup,
// Declaration Modifiers
.__consuming, .final, .required, .optional, .lazy, .dynamic, .infix, .postfix, .prefix, .mutating, .nonmutating, .convenience, .override, .package, .open,
.__setter_access, .indirect, .nonisolated, .distributed, ._local,
.inout, ._mutating, ._borrowing, ._consuming,
.__setter_access, .indirect, .isolated, .nonisolated, .distributed, ._local,
.inout, ._mutating, ._borrow, ._borrowing, .borrowing, ._consuming, .consuming, .consume,
// Accessors
.get, .set, .didSet, .willSet, .unsafeAddress, .addressWithOwner, .addressWithNativeOwner, .unsafeMutableAddress,
.mutableAddressWithOwner, .mutableAddressWithNativeOwner, ._read, ._modify,
// Misc
.import:
self = .declKeyword
default:
// Treat all keywords that weren't handled above as expression keywords as a fallback option.
// FIXME: We should assign a token precedence to all keywords

case // `TypeAttribute`
._noMetadata,
._opaqueReturnTypeOf,
.autoclosure,
.convention,
.differentiable,
.escaping,
.noDerivative,
.noescape,
.Sendable,
.unchecked:
self = .exprKeyword

case // `DeclarationAttributeWithSpecialSyntax`
._alignment,
._backDeploy,
._cdecl,
._documentation,
._dynamicReplacement,
._effects,
._expose,
._implements,
._nonSendable,
._objcImplementation,
._objcRuntimeName,
._optimize,
._originallyDefinedIn,
._private,
._projectedValueProperty,
._semantics,
._specialize,
._spi,
._spi_available,
._swift_native_objc_runtime_base,
._typeEraser,
._unavailableFromAsync,
.attached,
.available,
.backDeployed,
.derivative,
.exclusivity,
.inline,
.objc,
.transpose:
self = .exprKeyword

case // Treat all other keywords as expression keywords in the absence of any better information.
.__owned,
.__shared,
._Class,
._compilerInitialized,
._const,
._forward,
._linear,
._move,
._NativeClass,
._NativeRefCountedObject,
._PackageDescription,
._RefCountedObject,
._Trivial,
._TrivialAtMost,
._underlyingVersion,
._UnknownLayout,
._version,
.accesses,
.any,
.assignment,
.associativity,
.availability,
.before,
.block,
.canImport,
.compiler,
.cType,
.deprecated,
.exported,
.file,
.discard,
.forward,
.higherThan,
.initializes,
.introduced,
.kind,
.left,
.line,
.linear,
.lowerThan,
.message,
.metadata,
.module,
.noasync,
.none,
.obsoleted,
.of,
.Protocol,
.renamed,
.reverse,
.right,
.safe,
.sourceFile,
.spi,
.spiModule,
.swift,
.target,
.Type,
.unavailable,
.unowned,
.visibility,
.weak,
.witness_method,
.wrt,
.unsafe:
self = .exprKeyword
}
}
Expand Down