Skip to content

Commit c47db5b

Browse files
authored
Merge pull request #1301 from kimdv/kimdv/format-codegen-file
Format codegen files
2 parents 03f748b + 021131a commit c47db5b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1436
-1014
lines changed

CodeGeneration/Package.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import PackageDescription
55
let package = Package(
66
name: "CodeGeneration",
77
platforms: [
8-
.macOS(.v10_15),
9-
],
8+
.macOS(.v10_15)
9+
],
1010
products: [
11-
.executable(name: "generate-swiftsyntax", targets: ["generate-swiftsyntax"]),
11+
.executable(name: "generate-swiftsyntax", targets: ["generate-swiftsyntax"])
1212
],
1313
dependencies: [
1414
.package(url: "https://github.com/apple/swift-syntax.git", revision: "06de1fa52ab79fba2d540b6a62fa3c2a1e7133f5"),
@@ -22,7 +22,7 @@ let package = Package(
2222
.product(name: "SwiftSyntaxBuilder", package: "swift-syntax"),
2323
.product(name: "ArgumentParser", package: "swift-argument-parser"),
2424
"SyntaxSupport",
25-
"Utils"
25+
"Utils",
2626
]
2727
),
2828
.target(
@@ -44,15 +44,15 @@ let package = Package(
4444
"TokenSpec.swift.gyb",
4545
"Traits.swift.gyb",
4646
"Trivia.swift.gyb",
47-
"TypeNodes.swift.gyb"
47+
"TypeNodes.swift.gyb",
4848
]
4949
),
5050
.target(
5151
name: "Utils",
5252
dependencies: [
5353
.product(name: "SwiftSyntax", package: "swift-syntax"),
5454
.product(name: "SwiftSyntaxBuilder", package: "swift-syntax"),
55-
"SyntaxSupport"
55+
"SyntaxSupport",
5656
]
5757
),
5858
]

CodeGeneration/Sources/SyntaxSupport/Child.swift

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public enum ChildKind {
3333
case collection(kind: String, collectionElementName: String)
3434
/// The child is a token that matches one of the given `choices`.
3535
case token(choices: [TokenChoice])
36-
36+
3737
public var isNodeChoices: Bool {
3838
if case .nodeChoices = self {
3939
return true
@@ -120,14 +120,16 @@ public class Child {
120120
/// SyntaxClassification in SyntaxClassifier.h.gyb
121121
/// If force_classification is also set to true, all child nodes (not only
122122
/// identifiers) inherit the syntax classification.
123-
init(name: String,
124-
kind: ChildKind,
125-
description: String? = nil,
126-
isOptional: Bool = false,
127-
classification: String? = nil,
128-
forceClassification: Bool = false,
129-
isIndented: Bool = false,
130-
requiresLeadingNewline: Bool = false) {
123+
init(
124+
name: String,
125+
kind: ChildKind,
126+
description: String? = nil,
127+
isOptional: Bool = false,
128+
classification: String? = nil,
129+
forceClassification: Bool = false,
130+
isIndented: Bool = false,
131+
requiresLeadingNewline: Bool = false
132+
) {
131133
self.name = name
132134
self.kind = kind
133135
self.description = description

CodeGeneration/Sources/SyntaxSupport/KeywordSpec.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public struct KeywordSpec {
3636

3737
public let KEYWORDS: [KeywordSpec] = [
3838
// Please keep these sorted alphabetically
39-
39+
4040
KeywordSpec("__consuming"),
4141
KeywordSpec("__owned"),
4242
KeywordSpec("__setter_access"),

CodeGeneration/Sources/SyntaxSupport/Node.swift

Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -76,19 +76,21 @@ public class Node {
7676
public var isVisitable: Bool {
7777
return !isBase
7878
}
79-
80-
init(name: String,
81-
nameForDiagnostics: String?,
82-
description: String? = nil,
83-
kind: String,
84-
traits: [String] = [],
85-
parserFunction: String? = nil,
86-
children: [Child] = [],
87-
element: String = "",
88-
elementName: String? = nil,
89-
elementChoices: [String]? = nil,
90-
omitWhenEmpty: Bool = false,
91-
elementsSeparatedByNewline: Bool = false) {
79+
80+
init(
81+
name: String,
82+
nameForDiagnostics: String?,
83+
description: String? = nil,
84+
kind: String,
85+
traits: [String] = [],
86+
parserFunction: String? = nil,
87+
children: [Child] = [],
88+
element: String = "",
89+
elementName: String? = nil,
90+
elementChoices: [String]? = nil,
91+
omitWhenEmpty: Bool = false,
92+
elementsSeparatedByNewline: Bool = false
93+
) {
9294
self.syntaxKind = name
9395
self.swiftSyntaxKind = lowercaseFirstWord(name: name)
9496
self.name = kindToType(kind: self.syntaxKind)
@@ -103,26 +105,31 @@ public class Node {
103105
} else {
104106
// Add implicitly generated UnexpectedNodes children between
105107
// any two defined children
106-
self.children = children.enumerated().flatMap { (i, child) -> [Child] in
107-
let unexpectedName: String
108-
if i == 0 {
109-
unexpectedName = "UnexpectedBefore\(child.name)"
110-
} else {
111-
unexpectedName = "UnexpectedBetween\(children[i - 1].name)And\(child.name)"
108+
self.children =
109+
children.enumerated().flatMap { (i, child) -> [Child] in
110+
let unexpectedName: String
111+
if i == 0 {
112+
unexpectedName = "UnexpectedBefore\(child.name)"
113+
} else {
114+
unexpectedName = "UnexpectedBetween\(children[i - 1].name)And\(child.name)"
115+
}
116+
return [
117+
Child(
118+
name: unexpectedName,
119+
kind: .collection(kind: "UnexpectedNodes", collectionElementName: unexpectedName),
120+
isOptional: true
121+
),
122+
child,
123+
]
112124
}
113-
return [
114-
Child(
115-
name: unexpectedName,
116-
kind: .collection(kind: "UnexpectedNodes", collectionElementName: unexpectedName),
117-
isOptional: true
118-
),
119-
child
120-
]
121-
} + (!children.isEmpty ? [Child(
122-
name: "UnexpectedAfter\(children.last!.name)",
123-
kind: .collection(kind: "UnexpectedNodes", collectionElementName: "UnexpectedAfter\(children.last!.name)"),
124-
isOptional: true
125-
)] : [])
125+
+ (!children.isEmpty
126+
? [
127+
Child(
128+
name: "UnexpectedAfter\(children.last!.name)",
129+
kind: .collection(kind: "UnexpectedNodes", collectionElementName: "UnexpectedAfter\(children.last!.name)"),
130+
isOptional: true
131+
)
132+
] : [])
126133
}
127134

128135
self.nonUnexpectedChildren = children.filter { !$0.isUnexpectedNodes }
@@ -139,7 +146,7 @@ public class Node {
139146
self.collectionElementName = elementName ?? self.collectionElement
140147
self.collectionElementChoices = elementChoices ?? []
141148
self.elementsSeparatedByNewline = elementsSeparatedByNewline
142-
149+
143150
// For SyntaxCollections make sure that the elementName is set.
144151
assert(!isSyntaxCollection || elementName != nil || element != "")
145152
}

CodeGeneration/Sources/SyntaxSupport/SyntaxNodes.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
public let SYNTAX_NODES: [Node] = (COMMON_NODES
13+
public let SYNTAX_NODES: [Node] =
14+
(COMMON_NODES
1415
+ EXPR_NODES
1516
+ DECL_NODES
1617
+ ATTRIBUTE_NODES
Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
//===----------------------------------------------------------------------===//
2-
//
3-
// This source file is part of the Swift.org open source project
4-
//
5-
// Copyright (c) 2014 - 2022 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-
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2022 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+
//===----------------------------------------------------------------------===//
1212

13-
public func generateCopyrightHeader(for generator: String) -> String {
14-
return """
15-
//// Automatically Generated by \(generator)
16-
//// Do Not Edit Directly!
17-
//===----------------------------------------------------------------------===//
18-
//
19-
// This source file is part of the Swift.org open source project
20-
//
21-
// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
22-
// Licensed under Apache License v2.0 with Runtime Library Exception
23-
//
24-
// See https://swift.org/LICENSE.txt for license information
25-
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
26-
//
27-
//===----------------------------------------------------------------------===//
13+
public func generateCopyrightHeader(for generator: String) -> String {
14+
return """
15+
//// Automatically Generated by \(generator)
16+
//// Do Not Edit Directly!
17+
//===----------------------------------------------------------------------===//
18+
//
19+
// This source file is part of the Swift.org open source project
20+
//
21+
// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
22+
// Licensed under Apache License v2.0 with Runtime Library Exception
23+
//
24+
// See https://swift.org/LICENSE.txt for license information
25+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
26+
//
27+
//===----------------------------------------------------------------------===//
2828
2929
30-
"""
31-
}
30+
"""
31+
}

CodeGeneration/Sources/Utils/String+Extensions.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
1413
public extension StringProtocol {
15-
var withFirstCharacterLowercased: String { prefix(1).lowercased() + dropFirst() }
16-
var withFirstCharacterUppercased: String { prefix(1).uppercased() + dropFirst() }
17-
var backticked: String { "`\(self)`" }
14+
var withFirstCharacterLowercased: String { prefix(1).lowercased() + dropFirst() }
15+
var withFirstCharacterUppercased: String { prefix(1).uppercased() + dropFirst() }
16+
var backticked: String { "`\(self)`" }
1817
}

CodeGeneration/Sources/Utils/SyntaxBuildableChild.swift

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -94,18 +94,26 @@ public extension Child {
9494

9595
var assertChoices: [ExprSyntax] = []
9696
if type.isOptional {
97-
assertChoices.append(ExprSyntax(SequenceExprSyntax {
98-
IdentifierExprSyntax(identifier: .identifier(varName))
99-
BinaryOperatorExprSyntax(text: "==")
100-
NilLiteralExprSyntax()
101-
}))
97+
assertChoices.append(
98+
ExprSyntax(
99+
SequenceExprSyntax {
100+
IdentifierExprSyntax(identifier: .identifier(varName))
101+
BinaryOperatorExprSyntax(text: "==")
102+
NilLiteralExprSyntax()
103+
}
104+
)
105+
)
102106
}
103107
for textChoice in choicesTexts {
104-
assertChoices.append(ExprSyntax(SequenceExprSyntax {
105-
MemberAccessExprSyntax(base: type.forceUnwrappedIfNeeded(expr: IdentifierExprSyntax(identifier: .identifier(varName))), name: "text")
106-
BinaryOperatorExprSyntax(text: "==")
107-
StringLiteralExprSyntax(content: textChoice)
108-
}))
108+
assertChoices.append(
109+
ExprSyntax(
110+
SequenceExprSyntax {
111+
MemberAccessExprSyntax(base: type.forceUnwrappedIfNeeded(expr: IdentifierExprSyntax(identifier: .identifier(varName))), name: "text")
112+
BinaryOperatorExprSyntax(text: "==")
113+
StringLiteralExprSyntax(content: textChoice)
114+
}
115+
)
116+
)
109117
}
110118
let disjunction = ExprListSyntax(assertChoices.flatMap { [$0, ExprSyntax(BinaryOperatorExprSyntax(text: "||"))] }.dropLast())
111119
return FunctionCallExprSyntax(callee: ExprSyntax("assert")) {

CodeGeneration/Sources/Utils/SyntaxBuildableNode.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public extension Node {
2727
} else {
2828
return SyntaxBuildableType(syntaxKind: baseKind)
2929
}
30-
30+
3131
}
3232

3333
/// If documentation exists for this node, return it as a single-line string.

CodeGeneration/Sources/Utils/Utils.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
1413
/// Trims leading and trailing whitespace from each line.
1514
public func dedented<Lines: Sequence>(lines: Lines) -> [String] where Lines.Element: StringProtocol {
1615
lines.map { $0.trimmingCharacters(in: .whitespacesAndNewlines) }

CodeGeneration/Sources/generate-swiftsyntax/GenerateSwiftSyntax.swift

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,17 @@ struct GenerateSwiftSyntax: ParsableCommand {
8585
let modules = Set(templates.map(\.module))
8686

8787
let previouslyGeneratedFilesLock = NSLock()
88-
var previouslyGeneratedFiles = Set(modules.flatMap { (module) -> [URL] in
89-
let generatedDir = URL(fileURLWithPath: destination)
90-
.appendingPathComponent(module)
91-
.appendingPathComponent("generated")
92-
return FileManager.default
93-
.enumerator(at: generatedDir, includingPropertiesForKeys: nil)!
94-
.compactMap { $0 as? URL}
95-
.filter { $0.pathExtension == "swift" }
96-
})
88+
var previouslyGeneratedFiles = Set(
89+
modules.flatMap { (module) -> [URL] in
90+
let generatedDir = URL(fileURLWithPath: destination)
91+
.appendingPathComponent(module)
92+
.appendingPathComponent("generated")
93+
return FileManager.default
94+
.enumerator(at: generatedDir, includingPropertiesForKeys: nil)!
95+
.compactMap { $0 as? URL }
96+
.filter { $0.pathExtension == "swift" }
97+
}
98+
)
9799

98100
var errors: [Error] = []
99101
DispatchQueue.concurrentPerform(iterations: templates.count) { index in
@@ -131,17 +133,18 @@ struct GenerateSwiftSyntax: ParsableCommand {
131133
private func generateTemplate(
132134
sourceFile: SourceFileSyntax,
133135
destination: URL,
134-
verbose: Bool) throws {
135-
try FileManager.default.createDirectory(
136-
atPath: destination.deletingLastPathComponent().path,
137-
withIntermediateDirectories: true,
138-
attributes: nil
139-
)
140-
141-
if verbose {
142-
print("Generating \(destination.path)...")
143-
}
144-
let syntax = sourceFile.formatted(using: CodeGenerationFormat())
145-
try "\(syntax)\n".write(to: destination, atomically: true, encoding: .utf8)
136+
verbose: Bool
137+
) throws {
138+
try FileManager.default.createDirectory(
139+
atPath: destination.deletingLastPathComponent().path,
140+
withIntermediateDirectories: true,
141+
attributes: nil
142+
)
143+
144+
if verbose {
145+
print("Generating \(destination.path)...")
146146
}
147+
let syntax = sourceFile.formatted(using: CodeGenerationFormat())
148+
try "\(syntax)\n".write(to: destination, atomically: true, encoding: .utf8)
149+
}
147150
}

0 commit comments

Comments
 (0)