Skip to content

Commit d5c8158

Browse files
authored
Merge pull request #1862 from kimdv/kimdv/1834-drop-keyword-suffix-from-tokenkindpoundavailablekeyword-etc
2 parents 05a90b3 + 80a775c commit d5c8158

36 files changed

+462
-361
lines changed

CodeGeneration/Sources/SyntaxSupport/TokenSpec.swift

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,35 +15,26 @@ public class TokenSpec {
1515
public let name: String
1616
public let nameForDiagnostics: String
1717
public let text: String?
18-
public let isKeyword: Bool
1918
public let associatedValueClass: String?
2019

2120
public var swiftKind: String {
22-
let name = lowercaseFirstWord(name: self.name)
23-
24-
if isKeyword {
25-
return name + "Keyword"
26-
} else {
27-
return name
28-
}
21+
return lowercaseFirstWord(name: self.name)
2922
}
3023

3124
init(
3225
name: String,
3326
nameForDiagnostics: String,
3427
text: String? = nil,
35-
isKeyword: Bool = false,
3628
associatedValueClass: String? = nil
3729
) {
3830
self.name = name
3931
self.nameForDiagnostics = nameForDiagnostics
4032
self.text = text
41-
self.isKeyword = isKeyword
4233
self.associatedValueClass = associatedValueClass
4334
}
4435
}
4536

46-
public class PoundKeywordSpec: TokenSpec {
37+
public class PoundSpec: TokenSpec {
4738
init(
4839
name: String,
4940
nameForDiagnostics: String? = nil,
@@ -52,13 +43,12 @@ public class PoundKeywordSpec: TokenSpec {
5243
super.init(
5344
name: name,
5445
nameForDiagnostics: nameForDiagnostics ?? text,
55-
text: text,
56-
isKeyword: true
46+
text: text
5747
)
5848
}
5949
}
6050

61-
public class PoundObjectLiteralSpec: PoundKeywordSpec {
51+
public class PoundObjectLiteralSpec: PoundSpec {
6252
let `protocol`: String
6353

6454
init(
@@ -76,9 +66,9 @@ public class PoundObjectLiteralSpec: PoundKeywordSpec {
7666
}
7767
}
7868

79-
public class PoundConfigSpec: PoundKeywordSpec {}
69+
public class PoundConfigSpec: PoundSpec {}
8070

81-
public class PoundDirectiveKeywordSpec: PoundKeywordSpec {
71+
public class PoundDirectiveSpec: PoundSpec {
8272
init(
8373
name: String,
8474
text: String
@@ -90,7 +80,7 @@ public class PoundDirectiveKeywordSpec: PoundKeywordSpec {
9080
}
9181
}
9282

93-
public class PoundConditionalDirectiveKeywordSpec: PoundDirectiveKeywordSpec {
83+
public class PoundConditionalDirectiveSpec: PoundDirectiveSpec {
9484
override init(
9585
name: String,
9686
text: String
@@ -110,8 +100,7 @@ public class PunctuatorSpec: TokenSpec {
110100
super.init(
111101
name: name,
112102
nameForDiagnostics: text,
113-
text: text,
114-
isKeyword: false
103+
text: text
115104
)
116105
}
117106
}
@@ -149,11 +138,11 @@ public let SYNTAX_TOKENS: [TokenSpec] = [
149138
PunctuatorSpec(name: "PostfixQuestionMark", text: "?"),
150139
PunctuatorSpec(name: "Pound", text: "#"),
151140
PoundConfigSpec(name: "PoundAvailable", text: "#available"),
152-
PoundConditionalDirectiveKeywordSpec(name: "PoundElse", text: "#else"),
153-
PoundConditionalDirectiveKeywordSpec(name: "PoundElseif", text: "#elseif"),
154-
PoundConditionalDirectiveKeywordSpec(name: "PoundEndif", text: "#endif"),
155-
PoundConditionalDirectiveKeywordSpec(name: "PoundIf", text: "#if"),
156-
PoundDirectiveKeywordSpec(name: "PoundSourceLocation", text: "#sourceLocation"),
141+
PoundConditionalDirectiveSpec(name: "PoundElse", text: "#else"),
142+
PoundConditionalDirectiveSpec(name: "PoundElseif", text: "#elseif"),
143+
PoundConditionalDirectiveSpec(name: "PoundEndif", text: "#endif"),
144+
PoundConditionalDirectiveSpec(name: "PoundIf", text: "#if"),
145+
PoundDirectiveSpec(name: "PoundSourceLocation", text: "#sourceLocation"),
157146
PoundConfigSpec(name: "PoundUnavailable", text: "#unavailable"),
158147
PunctuatorSpec(name: "PrefixAmpersand", text: "&"),
159148
MiscSpec(name: "PrefixOperator", nameForDiagnostics: "prefix operator"),

CodeGeneration/Sources/Utils/SyntaxBuildableChild.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,6 @@ public extension Child {
7171
guard let token = token, isToken else {
7272
return type.defaultValue.map { InitializerClauseSyntax(value: $0) }
7373
}
74-
if token.isKeyword {
75-
return InitializerClauseSyntax(value: ExprSyntax(".\(raw: token.swiftKind)()"))
76-
}
7774
if token.text != nil {
7875
return InitializerClauseSyntax(value: ExprSyntax(".\(raw: token.swiftKind)Token()"))
7976
}

CodeGeneration/Sources/Utils/SyntaxBuildableType.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,7 @@ public struct SyntaxBuildableType: Hashable {
6363
if isOptional {
6464
return ExprSyntax(NilLiteralExprSyntax())
6565
} else if let token = token {
66-
if token.isKeyword {
67-
return ExprSyntax(".\(raw: token.swiftKind)()")
68-
} else if token.text != nil {
66+
if token.text != nil {
6967
return ExprSyntax(".\(raw: lowercaseFirstWord(name: token.name))Token()")
7068
}
7169
}

CodeGeneration/Sources/generate-swiftsyntax/templates/swiftparser/IsLexerClassifiedFile.swift

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,6 @@ let isLexerClassifiedFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
5858
"""
5959
) {
6060
try! SwitchExprSyntax("switch self") {
61-
for token in SYNTAX_TOKENS where token.isKeyword {
62-
SwitchCaseSyntax("case .\(raw: token.swiftKind):") {
63-
StmtSyntax("return true")
64-
}
65-
}
66-
6761
SwitchCaseSyntax("case .keyword(let keyword):") {
6862
StmtSyntax("return keyword.isLexerClassified")
6963
}

CodeGeneration/Sources/generate-swiftsyntax/templates/swiftsyntax/RawSyntaxValidationFile.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ let rawSyntaxValidationFile = try! SourceFileSyntax(leadingTrivia: copyrightHead
2828
IfConfigDeclSyntax(
2929
clauses: try IfConfigClauseListSyntax {
3030
IfConfigClauseSyntax(
31-
poundKeyword: .poundIfKeyword(),
31+
poundKeyword: .poundIfToken(),
3232
condition: ExprSyntax("SWIFTSYNTAX_ENABLE_RAWSYNTAX_VALIDATION"),
3333
elements: .statements(
3434
try CodeBlockItemListSyntax {

CodeGeneration/Sources/generate-swiftsyntax/templates/swiftsyntax/SyntaxRewriterFile.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ let syntaxRewriterFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
199199
""",
200200
clauses: IfConfigClauseListSyntax {
201201
IfConfigClauseSyntax(
202-
poundKeyword: .poundIfKeyword(),
202+
poundKeyword: .poundIfToken(),
203203
condition: ExprSyntax("DEBUG"),
204204
elements: .statements(
205205
try CodeBlockItemListSyntax {
@@ -250,7 +250,7 @@ let syntaxRewriterFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
250250
)
251251
)
252252
IfConfigClauseSyntax(
253-
poundKeyword: .poundElseKeyword(),
253+
poundKeyword: .poundElseToken(),
254254
elements: .statements(
255255
CodeBlockItemListSyntax {
256256
try! FunctionDeclSyntax("private func visit(_ data: SyntaxData) -> Syntax") {

CodeGeneration/Sources/generate-swiftsyntax/templates/swiftsyntax/TokensFile.swift

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,7 @@ import Utils
1818
let tokensFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
1919
try! ExtensionDeclSyntax("extension TokenSyntax") {
2020
for token in SYNTAX_TOKENS {
21-
if token.isKeyword {
22-
DeclSyntax(
23-
"""
24-
public static func \(raw: token.swiftKind)(
25-
leadingTrivia: Trivia = [],
26-
trailingTrivia: Trivia = [],
27-
presence: SourcePresence = .present
28-
) -> TokenSyntax {
29-
return TokenSyntax(
30-
.\(raw: token.swiftKind),
31-
leadingTrivia: leadingTrivia,
32-
trailingTrivia: trailingTrivia,
33-
presence: presence
34-
)
35-
}
36-
"""
37-
)
38-
} else if let text = token.text {
21+
if let text = token.text {
3922
DeclSyntax(
4023
"""
4124
public static func \(raw: token.swiftKind)Token(

Sources/SwiftBasicFormat/BasicFormat.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -239,10 +239,10 @@ open class BasicFormat: SyntaxRewriter {
239239
(.multilineStringQuote, .stringSegment), // segment starting a multi-line string literal
240240
(.stringSegment, .multilineStringQuote), // ending a multi-line string literal that has a string interpolation segment at its end
241241
(.rightParen, .multilineStringQuote), // ending a multi-line string literal that has a string interpolation segment at its end
242-
(.poundEndifKeyword, _),
243-
(_, .poundElseKeyword),
244-
(_, .poundElseifKeyword),
245-
(_, .poundEndifKeyword),
242+
(.poundEndif, _),
243+
(_, .poundElse),
244+
(_, .poundElseif),
245+
(_, .poundEndif),
246246
(_, .rightBrace):
247247
return true
248248
default:
@@ -283,7 +283,7 @@ open class BasicFormat: SyntaxRewriter {
283283
(.postfixQuestionMark, .leftParen), // init?() or myOptionalClosure?()
284284
(.postfixQuestionMark, .period), // someOptional?.someProperty
285285
(.pound, _),
286-
(.poundUnavailableKeyword, .leftParen), // #unavailable(...)
286+
(.poundUnavailable, .leftParen), // #unavailable(...)
287287
(.prefixAmpersand, _),
288288
(.prefixOperator, _),
289289
(.rawStringDelimiter, .leftParen), // opening raw string delimiter should never be separate by a space

Sources/SwiftIDEUtils/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors
88

99
add_swift_host_library(SwiftIDEUtils
10+
SwiftIDEUtilsCompatibility.swift
1011
Syntax+Classifications.swift
1112
SyntaxClassification.swift
1213
SyntaxClassifier.swift
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
// This file provides compatiblity aliases to keep dependents of SwiftSyntax building.
14+
// All users of the declarations in this file should transition away from them ASAP.
15+
16+
public extension SyntaxClassification {
17+
/// A `#` keyword like `#warning`.
18+
@available(*, deprecated, renamed: "poundDirective")
19+
static var poundDirectiveKeyword: SyntaxClassification {
20+
return .poundDirective
21+
}
22+
}
23+
24+
//==========================================================================//
25+
// IMPORTANT: If you are tempted to add a compatiblity layer code here //
26+
// please insert it in alphabetical order above //
27+
//==========================================================================//

Sources/SwiftIDEUtils/SyntaxClassification.swift

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ public enum SyntaxClassification {
4343
case objectLiteral
4444
/// An identifier referring to an operator.
4545
case operatorIdentifier
46-
/// A `#` keyword like `#warning`.
47-
case poundDirectiveKeyword
46+
/// A `#` token like `#warning`.
47+
case poundDirective
4848
/// A regex literal, including multiline regex literals.
4949
case regexLiteral
5050
/// The opening and closing parenthesis of string interpolation.
@@ -156,20 +156,20 @@ extension RawTokenKind {
156156
return .none
157157
case .pound:
158158
return .none
159-
case .poundAvailableKeyword:
160-
return .keyword
161-
case .poundElseKeyword:
162-
return .poundDirectiveKeyword
163-
case .poundElseifKeyword:
164-
return .poundDirectiveKeyword
165-
case .poundEndifKeyword:
166-
return .poundDirectiveKeyword
167-
case .poundIfKeyword:
168-
return .poundDirectiveKeyword
169-
case .poundSourceLocationKeyword:
170-
return .poundDirectiveKeyword
171-
case .poundUnavailableKeyword:
172-
return .keyword
159+
case .poundAvailable:
160+
return .none
161+
case .poundElse:
162+
return .poundDirective
163+
case .poundElseif:
164+
return .poundDirective
165+
case .poundEndif:
166+
return .poundDirective
167+
case .poundIf:
168+
return .poundDirective
169+
case .poundSourceLocation:
170+
return .poundDirective
171+
case .poundUnavailable:
172+
return .none
173173
case .prefixAmpersand:
174174
return .none
175175
case .prefixOperator:

Sources/SwiftParser/Attributes.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
extension Parser {
1616
mutating func parseAttributeList() -> RawAttributeListSyntax? {
17-
guard self.at(.atSign, .poundIfKeyword) else {
17+
guard self.at(.atSign, .poundIf) else {
1818
return nil
1919
}
2020

@@ -23,7 +23,7 @@ extension Parser {
2323
repeat {
2424
let attribute = self.parseAttribute()
2525
elements.append(attribute)
26-
} while self.at(.atSign, .poundIfKeyword) && loopProgress.evaluate(currentToken)
26+
} while self.at(.atSign, .poundIf) && loopProgress.evaluate(currentToken)
2727
return RawAttributeListSyntax(elements: elements, arena: self.arena)
2828
}
2929
}
@@ -213,7 +213,7 @@ extension Parser {
213213
}
214214

215215
mutating func parseAttribute() -> RawAttributeListSyntax.Element {
216-
if self.at(.poundIfKeyword) {
216+
if self.at(.poundIf) {
217217
return .ifConfigDecl(
218218
self.parsePoundIfDirective { (parser, _) -> RawAttributeListSyntax.Element in
219219
return parser.parseAttribute()

Sources/SwiftParser/Declarations.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ extension TokenConsumer {
5353
allowInitDecl: Bool = true,
5454
allowRecovery: Bool = false
5555
) -> Bool {
56-
if self.at(.poundIfKeyword) {
56+
if self.at(.poundIf) {
5757
return true
5858
}
5959

@@ -85,12 +85,12 @@ extension TokenConsumer {
8585
}
8686

8787
if hasAttribute {
88-
if subparser.at(.rightBrace) || subparser.at(.endOfFile) || subparser.at(.poundEndifKeyword) {
88+
if subparser.at(.rightBrace) || subparser.at(.endOfFile) || subparser.at(.poundEndif) {
8989
return true
9090
}
9191
}
9292

93-
if subparser.at(.poundIfKeyword) {
93+
if subparser.at(.poundIf) {
9494
var attrLookahead = subparser.lookahead()
9595
return attrLookahead.consumeIfConfigOfAttributes()
9696
}
@@ -196,7 +196,7 @@ extension Parser {
196196
mutating func parseDeclaration(inMemberDeclList: Bool = false) -> RawDeclSyntax {
197197
// If we are at a `#if` of attributes, the `#if` directive should be
198198
// parsed when we're parsing the attributes.
199-
if self.at(.poundIfKeyword) && !self.withLookahead({ $0.consumeIfConfigOfAttributes() }) {
199+
if self.at(.poundIf) && !self.withLookahead({ $0.consumeIfConfigOfAttributes() }) {
200200
let directive = self.parsePoundIfDirective { (parser, _) in
201201
let parsedDecl = parser.parseDeclaration()
202202
let semicolon = parser.consume(if: .semicolon)
@@ -722,7 +722,7 @@ extension Parser {
722722
}
723723

724724
let decl: RawDeclSyntax
725-
if self.at(.poundSourceLocationKeyword) {
725+
if self.at(.poundSourceLocation) {
726726
decl = RawDeclSyntax(self.parsePoundSourceLocationDirective())
727727
} else {
728728
decl = self.parseDeclaration(inMemberDeclList: true)

0 commit comments

Comments
 (0)