Skip to content

Detect attached attributes using a contextual keyword instead of string matching #1881

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
merged 1 commit into from
Jul 7, 2023
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions CodeGeneration/Sources/SyntaxSupport/KeywordSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public let KEYWORDS: [KeywordSpec] = [
KeywordSpec("associatedtype", isLexerClassified: true, requiresTrailingSpace: true),
KeywordSpec("associativity"),
KeywordSpec("async", requiresTrailingSpace: true),
KeywordSpec("attached"),
KeywordSpec("autoclosure"),
KeywordSpec("availability"),
KeywordSpec("available"),
Expand Down
17 changes: 9 additions & 8 deletions Sources/SwiftParser/Attributes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ extension Parser {
case _typeEraser
case _unavailableFromAsync
case `rethrows`
case attached
case available
case backDeployed
case derivative
Expand Down Expand Up @@ -88,6 +89,7 @@ extension Parser {
case TokenSpec(._typeEraser): self = ._typeEraser
case TokenSpec(._unavailableFromAsync): self = ._unavailableFromAsync
case TokenSpec(.`rethrows`): self = .rethrows
case TokenSpec(.attached): self = .attached
case TokenSpec(.available): self = .available
case TokenSpec(.backDeployed): self = .backDeployed
case TokenSpec(.derivative): self = .derivative
Expand Down Expand Up @@ -126,6 +128,7 @@ extension Parser {
case ._typeEraser: return .keyword(._typeEraser)
case ._unavailableFromAsync: return .keyword(._unavailableFromAsync)
case .`rethrows`: return .keyword(.rethrows)
case .attached: return .keyword(.attached)
case .available: return .keyword(.available)
case .backDeployed: return .keyword(.backDeployed)
case .derivative: return .keyword(.derivative)
Expand Down Expand Up @@ -313,6 +316,11 @@ extension Parser {
return parseAttribute(argumentMode: .optional) { parser in
return .unavailableFromAsyncArguments(parser.parseUnavailableFromAsyncArguments())
}
case .attached:
return parseAttribute(argumentMode: .customAttribute) { parser in
let arguments = parser.parseAttachedArguments()
return .argumentList(RawTupleExprElementListSyntax(elements: arguments, arena: parser.arena))
}
case .rethrows:
let (unexpectedBeforeAtSign, atSign) = self.expect(.atSign)
let (unexpectedBeforeAttributeName, attributeName) = self.expect(TokenSpec(.rethrows, remapping: .identifier))
Expand All @@ -329,15 +337,8 @@ extension Parser {
)
)
case nil:
let isAttached = self.peek().isAttachedKeyword
return parseAttribute(argumentMode: .customAttribute) { parser in
let arguments: [RawTupleExprElementSyntax]
if isAttached {
arguments = parser.parseAttachedArguments()
} else {
arguments = parser.parseArgumentListElements(pattern: .none)
}

let arguments = parser.parseArgumentListElements(pattern: .none)
return .argumentList(RawTupleExprElementListSyntax(elements: arguments, arena: parser.arena))
}
}
Expand Down
4 changes: 0 additions & 4 deletions Sources/SwiftParser/Types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1076,10 +1076,6 @@ extension Lexer.Lexeme {
|| self.rawTokenKind == .prefixOperator
}

var isAttachedKeyword: Bool {
return self.rawTokenKind == .identifier && self.tokenText == "attached"
}

var isEllipsis: Bool {
return self.isAnyOperator && self.tokenText == "..."
}
Expand Down
4 changes: 4 additions & 0 deletions Sources/SwiftSyntax/generated/Keyword.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public enum Keyword: UInt8, Hashable {
case `associatedtype`
case associativity
case async
case attached
case autoclosure
case availability
case available
Expand Down Expand Up @@ -478,6 +479,8 @@ public enum Keyword: UInt8, Hashable {
self = ._version
case "accesses":
self = .accesses
case "attached":
self = .attached
case "compiler":
self = .compiler
case "continue":
Expand Down Expand Up @@ -817,6 +820,7 @@ public enum Keyword: UInt8, Hashable {
"associatedtype",
"associativity",
"async",
"attached",
"autoclosure",
"availability",
"available",
Expand Down