Skip to content

Commit cc2ea59

Browse files
committed
Move names for diagnostics from SwiftSyntax to SwiftParserDiagnostics
These names never belonged in `SwiftSyntax`. It was just most convenient to put them there. Move them to `SwiftParserDiagnostics` so we can mark them as `internal`.
1 parent 1ca3d9e commit cc2ea59

27 files changed

+1079
-5840
lines changed

CodeGeneration/Sources/generate-swiftsyntax/GenerateSwiftSyntax.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ private let generatedDirName = "generated"
2222
private let swiftBasicFormatGeneratedDir = ["SwiftBasicFormat", generatedDirName]
2323
private let ideUtilsGeneratedDir = ["IDEUtils", generatedDirName]
2424
private let swiftParserGeneratedDir = ["SwiftParser", generatedDirName]
25+
private let swiftParserDiagnosticsGeneratedDir = ["SwiftParserDiagnostics", generatedDirName]
2526
private let swiftSyntaxGeneratedDir = ["SwiftSyntax", generatedDirName]
2627
private let swiftSyntaxBuilderGeneratedDir = ["SwiftSyntaxBuilder", generatedDirName]
2728
private let BASE_KIND_FILES = [
@@ -90,6 +91,11 @@ struct GenerateSwiftSyntax: ParsableCommand {
9091
GeneratedFileSpec(swiftParserGeneratedDir + ["TokenSpecStaticMembers.swift"], tokenSpecStaticMembersFile),
9192
GeneratedFileSpec(swiftParserGeneratedDir + ["TypeAttribute.swift"], typeAttributeFile),
9293

94+
// SwiftParserDiagnostics
95+
GeneratedFileSpec(swiftParserDiagnosticsGeneratedDir + ["ChildNameForDiagnostics.swift"], childNameForDiagnosticFile),
96+
GeneratedFileSpec(swiftParserDiagnosticsGeneratedDir + ["SyntaxKindNameForDiagnostics.swift"], syntaxKindNameForDiagnosticFile),
97+
GeneratedFileSpec(swiftParserDiagnosticsGeneratedDir + ["TokenNameForDiagnostics.swift"], tokenNameForDiagnosticFile),
98+
9399
// SwiftSyntax
94100
GeneratedFileSpec(swiftSyntaxGeneratedDir + ["Keyword.swift"], keywordFile),
95101
GeneratedFileSpec(swiftSyntaxGeneratedDir + ["Misc.swift"], miscFile),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
import SwiftSyntax
14+
import SwiftSyntaxBuilder
15+
import SyntaxSupport
16+
import Utils
17+
18+
let childNameForDiagnosticFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
19+
DeclSyntax("import SwiftSyntax")
20+
21+
try! FunctionDeclSyntax(
22+
"private func childNameForDiagnostics(_ keyPath: AnyKeyPath) -> String?"
23+
) {
24+
try! SwitchExprSyntax("switch keyPath") {
25+
for node in NON_BASE_SYNTAX_NODES where !node.isSyntaxCollection {
26+
for child in node.children {
27+
if let nameForDiagnostics = child.nameForDiagnostics {
28+
SwitchCaseSyntax("case \\\(raw: node.type.syntaxBaseName).\(raw: child.swiftName):") {
29+
StmtSyntax(#"return "\#(raw: nameForDiagnostics)""#)
30+
}
31+
}
32+
}
33+
}
34+
SwitchCaseSyntax(
35+
"""
36+
default:
37+
return nil
38+
"""
39+
)
40+
}
41+
}
42+
43+
DeclSyntax(
44+
"""
45+
private func getKeyPath<T: SyntaxProtocol>(_ node: T) -> AnyKeyPath? {
46+
guard let parent = node.parent else {
47+
return nil
48+
}
49+
guard case .layout(let childrenKeyPaths) = parent.kind.syntaxNodeType.structure else {
50+
return nil
51+
}
52+
return childrenKeyPaths[node.indexInParent]
53+
}
54+
"""
55+
)
56+
57+
DeclSyntax(
58+
"""
59+
extension SyntaxProtocol {
60+
var childNameInParent: String? {
61+
guard let keyPath = getKeyPath(self) else {
62+
return nil
63+
}
64+
return childNameForDiagnostics(keyPath)
65+
}
66+
}
67+
"""
68+
)
69+
70+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
import SwiftSyntax
14+
import SwiftSyntaxBuilder
15+
import SyntaxSupport
16+
import Utils
17+
18+
let syntaxKindNameForDiagnosticFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
19+
DeclSyntax("import SwiftSyntax")
20+
21+
try! ExtensionDeclSyntax("extension SyntaxKind") {
22+
try VariableDeclSyntax("var nameForDiagnostics: String?") {
23+
try SwitchExprSyntax("switch self") {
24+
SwitchCaseSyntax("case .token:") {
25+
StmtSyntax(#"return "token""#)
26+
}
27+
28+
for node in NON_BASE_SYNTAX_NODES {
29+
if let nameForDiagnostics = node.nameForDiagnostics {
30+
SwitchCaseSyntax("case .\(raw: node.swiftSyntaxKind):") {
31+
StmtSyntax("return \"\(raw: nameForDiagnostics)\"")
32+
}
33+
}
34+
}
35+
SwitchCaseSyntax(
36+
"""
37+
default:
38+
return nil
39+
"""
40+
)
41+
}
42+
}
43+
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
import SwiftSyntax
14+
import SwiftSyntaxBuilder
15+
import SyntaxSupport
16+
import Utils
17+
18+
let tokenNameForDiagnosticFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
19+
DeclSyntax("@_spi(RawSyntax) import SwiftSyntax")
20+
21+
try! ExtensionDeclSyntax("extension TokenKind") {
22+
try! VariableDeclSyntax("var nameForDiagnostics: String") {
23+
try! SwitchExprSyntax("switch self") {
24+
SwitchCaseSyntax("case .eof:") {
25+
StmtSyntax(#"return "end of file""#)
26+
}
27+
28+
for token in SYNTAX_TOKENS where token.swiftKind != "keyword" {
29+
SwitchCaseSyntax("case .\(raw: token.swiftKind):") {
30+
StmtSyntax("return #\"\(raw: token.nameForDiagnostics)\"#")
31+
}
32+
}
33+
SwitchCaseSyntax("case .keyword(let keyword):") {
34+
StmtSyntax("return String(syntaxText: keyword.defaultText)")
35+
}
36+
}
37+
}
38+
}
39+
}

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

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,6 @@ let lookupTable = ArrayExprSyntax(leftSquare: .leftSquareBracketToken(trailingTr
2222
}
2323

2424
let keywordFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
25-
DeclSyntax(
26-
"""
27-
/// Make `StaticString` equatable so we can use it as the raw value for Keyword.
28-
extension StaticString: Equatable {
29-
public static func == (lhs: StaticString, rhs: StaticString) -> Bool {
30-
return SyntaxText(lhs) == SyntaxText(rhs)
31-
}
32-
}
33-
"""
34-
)
35-
3625
try! EnumDeclSyntax(
3726
"""
3827
@frozen // FIXME: Not actually stable, works around a miscompile

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

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -50,23 +50,5 @@ let miscFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
5050
}
5151
}
5252
}
53-
54-
try VariableDeclSyntax("public var nameForDiagnostics: String?") {
55-
try SwitchExprSyntax("switch self") {
56-
SwitchCaseSyntax("case .token:") {
57-
StmtSyntax(#"return "token""#)
58-
}
59-
60-
for node in NON_BASE_SYNTAX_NODES {
61-
SwitchCaseSyntax("case .\(raw: node.swiftSyntaxKind):") {
62-
if let nameForDiagnostics = node.nameForDiagnostics {
63-
StmtSyntax("return \"\(raw: nameForDiagnostics)\"")
64-
} else {
65-
StmtSyntax("return nil")
66-
}
67-
}
68-
}
69-
}
70-
}
7153
}
7254
}

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -232,14 +232,6 @@ let syntaxBaseNodesFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
232232

233233
StmtSyntax("return .choices(\(choices))")
234234
}
235-
236-
DeclSyntax(
237-
"""
238-
public func childNameForDiagnostics(_ index: SyntaxChildrenIndex) -> String? {
239-
return Syntax(self).childNameForDiagnostics(index)
240-
}
241-
"""
242-
)
243235
}
244236

245237
DeclSyntax(

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -314,14 +314,6 @@ let syntaxCollectionsFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
314314
}
315315
"""
316316
)
317-
318-
DeclSyntax(
319-
"""
320-
public func childNameForDiagnostics(_ index: SyntaxChildrenIndex) -> String? {
321-
return nil
322-
}
323-
"""
324-
)
325317
}
326318

327319
try! ExtensionDeclSyntax(

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

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -258,27 +258,6 @@ func syntaxNode(emitKind: String) -> SourceFileSyntax {
258258

259259
StmtSyntax("return .layout(\(layout))")
260260
}
261-
262-
try! FunctionDeclSyntax("public func childNameForDiagnostics(_ index: SyntaxChildrenIndex) -> String?") {
263-
try! SwitchExprSyntax("switch index.data?.indexInParent") {
264-
for (index, child) in node.children.enumerated() {
265-
SwitchCaseSyntax("case \(raw: index):") {
266-
if let nameForDiagnostics = child.nameForDiagnostics {
267-
StmtSyntax(#"return "\#(raw: nameForDiagnostics)""#)
268-
} else {
269-
StmtSyntax("return nil")
270-
}
271-
}
272-
}
273-
274-
SwitchCaseSyntax(
275-
"""
276-
default:
277-
fatalError("Invalid index")
278-
"""
279-
)
280-
}
281-
}
282261
}
283262

284263
try! ExtensionDeclSyntax("extension \(raw: node.name): CustomReflectable") {

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

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -96,23 +96,6 @@ let tokenKindFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
9696
}
9797
}
9898

99-
try! VariableDeclSyntax("public var nameForDiagnostics: String") {
100-
try! SwitchExprSyntax("switch self") {
101-
SwitchCaseSyntax("case .eof:") {
102-
StmtSyntax(#"return "end of file""#)
103-
}
104-
105-
for token in SYNTAX_TOKENS where token.swiftKind != "keyword" {
106-
SwitchCaseSyntax("case .\(raw: token.swiftKind):") {
107-
StmtSyntax("return #\"\(raw: token.nameForDiagnostics)\"#")
108-
}
109-
}
110-
SwitchCaseSyntax("case .keyword(let keyword):") {
111-
StmtSyntax("return String(syntaxText: keyword.defaultText)")
112-
}
113-
}
114-
}
115-
11699
try VariableDeclSyntax(
117100
"""
118101
/// Returns `true` if the token is a Swift keyword.

Sources/SwiftParserDiagnostics/CMakeLists.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@ add_swift_host_library(SwiftParserDiagnostics
1616
ParseDiagnosticsGenerator.swift
1717
PresenceUtils.swift
1818
SyntaxExtensions.swift
19-
Utils.swift)
19+
Utils.swift
20+
21+
generated/ChildNameForDiagnostics.swift
22+
generated/SyntaxKindNameForDiagnostics.swift
23+
generated/TokenNameForDiagnostics.swift
24+
)
2025

2126
target_link_libraries(SwiftParserDiagnostics PUBLIC
2227
SwiftBasicFormat

0 commit comments

Comments
 (0)