Skip to content

Commit d7be9b6

Browse files
authored
Merge pull request #1881 from ahoppen/ahoppen/attached-keyword
Detect `attached` attributes using a contextual keyword instead of string matching
2 parents 0d39318 + 754eb03 commit d7be9b6

File tree

4 files changed

+14
-12
lines changed

4 files changed

+14
-12
lines changed

CodeGeneration/Sources/SyntaxSupport/KeywordSpec.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ public let KEYWORDS: [KeywordSpec] = [
9595
KeywordSpec("associatedtype", isLexerClassified: true, requiresTrailingSpace: true),
9696
KeywordSpec("associativity"),
9797
KeywordSpec("async", requiresTrailingSpace: true),
98+
KeywordSpec("attached"),
9899
KeywordSpec("autoclosure"),
99100
KeywordSpec("availability"),
100101
KeywordSpec("available"),

Sources/SwiftParser/Attributes.swift

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ extension Parser {
5454
case _typeEraser
5555
case _unavailableFromAsync
5656
case `rethrows`
57+
case attached
5758
case available
5859
case backDeployed
5960
case derivative
@@ -88,6 +89,7 @@ extension Parser {
8889
case TokenSpec(._typeEraser): self = ._typeEraser
8990
case TokenSpec(._unavailableFromAsync): self = ._unavailableFromAsync
9091
case TokenSpec(.`rethrows`): self = .rethrows
92+
case TokenSpec(.attached): self = .attached
9193
case TokenSpec(.available): self = .available
9294
case TokenSpec(.backDeployed): self = .backDeployed
9395
case TokenSpec(.derivative): self = .derivative
@@ -126,6 +128,7 @@ extension Parser {
126128
case ._typeEraser: return .keyword(._typeEraser)
127129
case ._unavailableFromAsync: return .keyword(._unavailableFromAsync)
128130
case .`rethrows`: return .keyword(.rethrows)
131+
case .attached: return .keyword(.attached)
129132
case .available: return .keyword(.available)
130133
case .backDeployed: return .keyword(.backDeployed)
131134
case .derivative: return .keyword(.derivative)
@@ -313,6 +316,11 @@ extension Parser {
313316
return parseAttribute(argumentMode: .optional) { parser in
314317
return .unavailableFromAsyncArguments(parser.parseUnavailableFromAsyncArguments())
315318
}
319+
case .attached:
320+
return parseAttribute(argumentMode: .customAttribute) { parser in
321+
let arguments = parser.parseAttachedArguments()
322+
return .argumentList(RawTupleExprElementListSyntax(elements: arguments, arena: parser.arena))
323+
}
316324
case .rethrows:
317325
let (unexpectedBeforeAtSign, atSign) = self.expect(.atSign)
318326
let (unexpectedBeforeAttributeName, attributeName) = self.expect(TokenSpec(.rethrows, remapping: .identifier))
@@ -329,15 +337,8 @@ extension Parser {
329337
)
330338
)
331339
case nil:
332-
let isAttached = self.peek().isAttachedKeyword
333340
return parseAttribute(argumentMode: .customAttribute) { parser in
334-
let arguments: [RawTupleExprElementSyntax]
335-
if isAttached {
336-
arguments = parser.parseAttachedArguments()
337-
} else {
338-
arguments = parser.parseArgumentListElements(pattern: .none)
339-
}
340-
341+
let arguments = parser.parseArgumentListElements(pattern: .none)
341342
return .argumentList(RawTupleExprElementListSyntax(elements: arguments, arena: parser.arena))
342343
}
343344
}

Sources/SwiftParser/Types.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,10 +1076,6 @@ extension Lexer.Lexeme {
10761076
|| self.rawTokenKind == .prefixOperator
10771077
}
10781078

1079-
var isAttachedKeyword: Bool {
1080-
return self.rawTokenKind == .identifier && self.tokenText == "attached"
1081-
}
1082-
10831079
var isEllipsis: Bool {
10841080
return self.isAnyOperator && self.tokenText == "..."
10851081
}

Sources/SwiftSyntax/generated/Keyword.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public enum Keyword: UInt8, Hashable {
7171
case `associatedtype`
7272
case associativity
7373
case async
74+
case attached
7475
case autoclosure
7576
case availability
7677
case available
@@ -478,6 +479,8 @@ public enum Keyword: UInt8, Hashable {
478479
self = ._version
479480
case "accesses":
480481
self = .accesses
482+
case "attached":
483+
self = .attached
481484
case "compiler":
482485
self = .compiler
483486
case "continue":
@@ -817,6 +820,7 @@ public enum Keyword: UInt8, Hashable {
817820
"associatedtype",
818821
"associativity",
819822
"async",
823+
"attached",
820824
"autoclosure",
821825
"availability",
822826
"available",

0 commit comments

Comments
 (0)