Skip to content

Commit e0fbd9f

Browse files
committed
Update swift-syntax
1 parent 5c98b24 commit e0fbd9f

27 files changed

+548
-715
lines changed

CodeGeneration/Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ let package = Package(
1111
.executable(name: "generate-swiftsyntax", targets: ["generate-swiftsyntax"]),
1212
],
1313
dependencies: [
14-
.package(url: "https://github.com/apple/swift-syntax.git", revision: "05916332d79994097ed7780f2fe34d33b16ddd31"),
14+
.package(url: "https://github.com/apple/swift-syntax.git", revision: "5c98b247aa5e8499aed08bf08cdd6f61b70f58d3"),
1515
.package(url: "https://github.com/apple/swift-argument-parser.git", .upToNextMinor(from: "1.1.4")),
1616
],
1717
targets: [

CodeGeneration/Sources/Utils/CodeGenerationFormat.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ public class CodeGenerationFormat: BasicFormat {
1919

2020
public func ensuringTwoLeadingNewlines<NodeType: SyntaxProtocol>(node: NodeType) -> NodeType {
2121
if node.leadingTrivia?.first?.isNewline ?? false {
22-
return node.withLeadingTrivia(indentedNewline + (node.leadingTrivia ?? []))
22+
return node.with(\.leadingTrivia, indentedNewline + (node.leadingTrivia ?? []))
2323
} else {
24-
return node.withLeadingTrivia(indentedNewline + indentedNewline + (node.leadingTrivia ?? []))
24+
return node.with(\.leadingTrivia, indentedNewline + indentedNewline + (node.leadingTrivia ?? []))
2525
}
2626
}
2727

CodeGeneration/Sources/generate-swiftsyntax/KeywordFile.swift

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

2424
let keywordFile = SourceFileSyntax {
25-
ExtensionDeclSyntax(
25+
DeclSyntax(
2626
"""
2727
\(raw: generateCopyrightHeader(for: "generate-swiftparser"))
2828
@@ -35,19 +35,19 @@ let keywordFile = SourceFileSyntax {
3535
"""
3636
)
3737

38-
EnumDeclSyntax("""
38+
try! EnumDeclSyntax("""
3939
@frozen // FIXME: Not actually stable, works around a miscompile
4040
public enum Keyword: UInt8, Hashable
4141
""") {
4242
for (index, keyword) in KEYWORDS.enumerated() {
43-
EnumCaseDeclSyntax("case \(raw: keyword.escapedName)")
43+
DeclSyntax("case \(raw: keyword.escapedName)")
4444
}
4545

46-
InitializerDeclSyntax("@_spi(RawSyntax) public init?(_ text: SyntaxText)") {
47-
SwitchStmtSyntax(expression: ExprSyntax("text.count")) {
46+
try! InitializerDeclSyntax("@_spi(RawSyntax) public init?(_ text: SyntaxText)") {
47+
try! SwitchStmtSyntax("switch text.count") {
4848
for (length, keywords) in keywordsByLength() {
4949
SwitchCaseSyntax("case \(raw: length):") {
50-
SwitchStmtSyntax(expression: ExprSyntax("text")) {
50+
try! SwitchStmtSyntax("switch text") {
5151
for keyword in keywords {
5252
SwitchCaseSyntax(#"case "\#(raw: keyword.name)":"#) {
5353
ExprSyntax("self = .\(raw: keyword.escapedName)")
@@ -61,17 +61,12 @@ let keywordFile = SourceFileSyntax {
6161
}
6262
}
6363

64-
VariableDeclSyntax(
65-
leadingTrivia: [
66-
.docLineComment("/// Whether the token kind is switched from being an identifier to being an identifier to a keyword in the lexer."),
67-
.newlines(1),
68-
.docLineComment("/// This is true for keywords that used to be considered non-contextual."),
69-
.newlines(1)
70-
],
71-
modifiers: [DeclModifierSyntax(name: .keyword(.public))],
72-
name: "isLexerClassified",
73-
type: TypeAnnotationSyntax(type: TypeSyntax("Bool"))) {
74-
SwitchStmtSyntax(expression: ExprSyntax("self")) {
64+
try! VariableDeclSyntax("""
65+
/// Whether the token kind is switched from being an identifier to being an identifier to a keyword in the lexer.
66+
/// This is true for keywords that used to be considered non-contextual.
67+
public var isLexerClassified: Bool
68+
""") {
69+
try! SwitchStmtSyntax("switch self") {
7570
for keyword in KEYWORDS {
7671
if keyword.isLexerClassified {
7772
SwitchCaseSyntax("case .\(raw: keyword.escapedName): return true")
@@ -81,22 +76,20 @@ let keywordFile = SourceFileSyntax {
8176
}
8277
}
8378

84-
VariableDeclSyntax("""
85-
/// This is really unfortunate. Really, we should have a `switch` in
86-
/// `Keyword.defaultText` to return the keyword's kind but the constant lookup
87-
/// table is significantly faster. Ideally, we could also get the compiler to
88-
/// constant-evaluate `Keyword.spi.defaultText` to a `SyntaxText` but I don't
89-
/// see how that's possible right now.
90-
private static let keywordTextLookupTable: [SyntaxText] = \(lookupTable)
91-
""")
79+
DeclSyntax("""
80+
/// This is really unfortunate. Really, we should have a `switch` in
81+
/// `Keyword.defaultText` to return the keyword's kind but the constant lookup
82+
/// table is significantly faster. Ideally, we could also get the compiler to
83+
/// constant-evaluate `Keyword.spi.defaultText` to a `SyntaxText` but I don't
84+
/// see how that's possible right now.
85+
private static let keywordTextLookupTable: [SyntaxText] = \(lookupTable)
86+
""")
9287

93-
VariableDeclSyntax(
94-
"""
88+
DeclSyntax("""
9589
@_spi(RawSyntax)
9690
public var defaultText: SyntaxText {
9791
return Keyword.keywordTextLookupTable[Int(self.rawValue)]
9892
}
99-
"""
100-
)
93+
""")
10194
}
10295
}

CodeGeneration/Sources/generate-swiftsyntax/templates/basicformat/BasicFormatFile.swift

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,19 @@ import SyntaxSupport
1616
import Utils
1717

1818
let basicFormatFile = SourceFileSyntax {
19-
ImportDeclSyntax(
20-
leadingTrivia: .docLineComment(generateCopyrightHeader(for: "generate-swiftbasicformat")),
21-
path: [AccessPathComponentSyntax(name: "SwiftSyntax")]
22-
)
19+
DeclSyntax("""
20+
\(raw: generateCopyrightHeader(for: "generate-swiftbasicformat"))
21+
import SwiftSyntax
22+
""")
2323

24-
ClassDeclSyntax("open class BasicFormat: SyntaxRewriter") {
25-
VariableDeclSyntax("public var indentationLevel: Int = 0")
26-
VariableDeclSyntax("open var indentation: TriviaPiece { .spaces(indentationLevel * 4) }")
27-
VariableDeclSyntax("public var indentedNewline: Trivia { Trivia(pieces: [.newlines(1), indentation]) }")
28-
VariableDeclSyntax("private var lastRewrittenToken: TokenSyntax?")
29-
VariableDeclSyntax("private var putNextTokenOnNewLine: Bool = false")
24+
try! ClassDeclSyntax("open class BasicFormat: SyntaxRewriter") {
25+
DeclSyntax("public var indentationLevel: Int = 0")
26+
DeclSyntax("open var indentation: TriviaPiece { .spaces(indentationLevel * 4) }")
27+
DeclSyntax("public var indentedNewline: Trivia { Trivia(pieces: [.newlines(1), indentation]) }")
28+
DeclSyntax("private var lastRewrittenToken: TokenSyntax?")
29+
DeclSyntax("private var putNextTokenOnNewLine: Bool = false")
3030

31-
FunctionDeclSyntax("""
31+
DeclSyntax("""
3232
open override func visitPre(_ node: Syntax) {
3333
if let keyPath = getKeyPath(node), shouldIndent(keyPath) {
3434
indentationLevel += 1
@@ -39,7 +39,7 @@ let basicFormatFile = SourceFileSyntax {
3939
}
4040
"""
4141
)
42-
FunctionDeclSyntax("""
42+
DeclSyntax("""
4343
open override func visitPost(_ node: Syntax) {
4444
if let keyPath = getKeyPath(node), shouldIndent(keyPath) {
4545
indentationLevel -= 1
@@ -48,7 +48,7 @@ let basicFormatFile = SourceFileSyntax {
4848
"""
4949
)
5050

51-
FunctionDeclSyntax("""
51+
DeclSyntax("""
5252
open override func visit(_ node: TokenSyntax) -> TokenSyntax {
5353
var leadingTrivia = node.leadingTrivia
5454
var trailingTrivia = node.trailingTrivia
@@ -76,53 +76,53 @@ let basicFormatFile = SourceFileSyntax {
7676
"""
7777
)
7878

79-
FunctionDeclSyntax("open func shouldIndent(_ keyPath: AnyKeyPath) -> Bool") {
80-
SwitchStmtSyntax(expression: ExprSyntax("keyPath")) {
79+
try! FunctionDeclSyntax("open func shouldIndent(_ keyPath: AnyKeyPath) -> Bool") {
80+
try! SwitchStmtSyntax("switch keyPath") {
8181
for node in SYNTAX_NODES where !node.isBase {
8282
for child in node.children where child.isIndented {
8383
SwitchCaseSyntax("case \\\(raw: node.type.syntaxBaseName).\(raw: child.swiftName):") {
84-
ReturnStmtSyntax("return true")
84+
StmtSyntax("return true")
8585
}
8686
}
8787
}
8888
SwitchCaseSyntax("default:") {
89-
ReturnStmtSyntax("return false")
89+
StmtSyntax("return false")
9090
}
9191
}
9292
}
9393

94-
FunctionDeclSyntax("open func requiresLeadingNewline(_ keyPath: AnyKeyPath) -> Bool") {
95-
SwitchStmtSyntax(expression: ExprSyntax("keyPath")) {
94+
try! FunctionDeclSyntax("open func requiresLeadingNewline(_ keyPath: AnyKeyPath) -> Bool") {
95+
try! SwitchStmtSyntax("switch keyPath") {
9696
for node in SYNTAX_NODES where !node.isBase {
9797
for child in node.children where child.requiresLeadingNewline {
9898
SwitchCaseSyntax("case \\\(raw: node.type.syntaxBaseName).\(raw: child.swiftName):") {
99-
ReturnStmtSyntax("return true")
99+
StmtSyntax("return true")
100100
}
101101
}
102102
}
103103
SwitchCaseSyntax("default:") {
104-
ReturnStmtSyntax("return putNextTokenOnNewLine")
104+
StmtSyntax("return putNextTokenOnNewLine")
105105
}
106106
}
107107
}
108108

109-
FunctionDeclSyntax("open func childrenSeparatedByNewline(_ node: Syntax) -> Bool") {
110-
SwitchStmtSyntax(expression: ExprSyntax("node.as(SyntaxEnum.self)")) {
109+
try! FunctionDeclSyntax("open func childrenSeparatedByNewline(_ node: Syntax) -> Bool") {
110+
try! SwitchStmtSyntax("switch node.as(SyntaxEnum.self)") {
111111
for node in SYNTAX_NODES where !node.isBase {
112112
if node.elementsSeparatedByNewline {
113113
SwitchCaseSyntax("case .\(raw: node.swiftSyntaxKind):") {
114-
ReturnStmtSyntax("return true")
114+
StmtSyntax("return true")
115115
}
116116
}
117117
}
118118
SwitchCaseSyntax("default:") {
119-
ReturnStmtSyntax("return false")
119+
StmtSyntax("return false")
120120
}
121121
}
122122
}
123123

124-
FunctionDeclSyntax("open func requiresLeadingSpace(_ token: TokenSyntax) -> Bool") {
125-
SwitchStmtSyntax("""
124+
try! FunctionDeclSyntax("open func requiresLeadingSpace(_ token: TokenSyntax) -> Bool") {
125+
StmtSyntax("""
126126
switch (token.previousToken(viewMode: .sourceAccurate)?.tokenKind, token.tokenKind) {
127127
case (.leftParen, .binaryOperator): // Ensures there is no space in @available(*, deprecated)
128128
return false
@@ -131,27 +131,27 @@ let basicFormatFile = SourceFileSyntax {
131131
}
132132
""")
133133

134-
SwitchStmtSyntax(expression: ExprSyntax("token.tokenKind")) {
134+
try! SwitchStmtSyntax("switch token.tokenKind") {
135135
for token in SYNTAX_TOKENS {
136136
if token.requiresLeadingSpace {
137137
SwitchCaseSyntax("case .\(raw: token.swiftKind):") {
138-
ReturnStmtSyntax("return true")
138+
StmtSyntax("return true")
139139
}
140140
}
141141
}
142142
for keyword in KEYWORDS where keyword.requiresLeadingSpace {
143143
SwitchCaseSyntax("case .keyword(.\(raw: keyword.escapedName)):") {
144-
ReturnStmtSyntax("return true")
144+
StmtSyntax("return true")
145145
}
146146
}
147147
SwitchCaseSyntax("default:") {
148-
ReturnStmtSyntax("return false")
148+
StmtSyntax("return false")
149149
}
150150
}
151151
}
152152

153-
FunctionDeclSyntax("open func requiresTrailingSpace(_ token: TokenSyntax) -> Bool") {
154-
SwitchStmtSyntax("""
153+
try! FunctionDeclSyntax("open func requiresTrailingSpace(_ token: TokenSyntax) -> Bool") {
154+
StmtSyntax("""
155155
switch (token.tokenKind, token.parent?.kind) {
156156
case (.colon, .dictionaryExpr): // Ensures there is not space in `[:]`
157157
return false
@@ -163,7 +163,7 @@ let basicFormatFile = SourceFileSyntax {
163163
}
164164
""")
165165

166-
SwitchStmtSyntax("""
166+
StmtSyntax("""
167167
switch (token.tokenKind, token.nextToken(viewMode: .sourceAccurate)?.tokenKind) {
168168
case (.keyword(.as), .exclamationMark), // Ensures there is not space in `as!`
169169
(.keyword(.as), .postfixQuestionMark), // Ensures there is not space in `as?`
@@ -183,26 +183,26 @@ let basicFormatFile = SourceFileSyntax {
183183
}
184184
""")
185185

186-
SwitchStmtSyntax(expression: ExprSyntax("token.tokenKind")) {
186+
try! SwitchStmtSyntax("switch token.tokenKind") {
187187
for token in SYNTAX_TOKENS {
188188
if token.requiresTrailingSpace {
189189
SwitchCaseSyntax("case .\(raw: token.swiftKind):") {
190-
ReturnStmtSyntax("return true")
190+
StmtSyntax("return true")
191191
}
192192
}
193193
}
194194
for keyword in KEYWORDS where keyword.requiresTrailingSpace {
195195
SwitchCaseSyntax("case .keyword(.\(raw: keyword.escapedName)):") {
196-
ReturnStmtSyntax("return true")
196+
StmtSyntax("return true")
197197
}
198198
}
199199
SwitchCaseSyntax("default:") {
200-
ReturnStmtSyntax("return false")
200+
StmtSyntax("return false")
201201
}
202202
}
203203
}
204204

205-
FunctionDeclSyntax("""
205+
DeclSyntax("""
206206
private func getKeyPath(_ node: Syntax) -> AnyKeyPath? {
207207
guard let parent = node.parent else {
208208
return nil

CodeGeneration/Sources/generate-swiftsyntax/templates/ideutils/SyntaxClassificationFile.swift

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,26 +30,22 @@ fileprivate var node_child_classifications: [ChildClassification] {
3030
}
3131

3232
let syntaxClassificationFile = SourceFileSyntax {
33-
ImportDeclSyntax(
34-
"""
33+
DeclSyntax("""
3534
\(raw: generateCopyrightHeader(for: "generate-ideutils"))
3635
@_spi(RawSyntax) import SwiftSyntax
37-
"""
38-
)
36+
""")
3937

40-
EnumDeclSyntax("public enum SyntaxClassification") {
38+
try! EnumDeclSyntax("public enum SyntaxClassification") {
4139
for classification in SYNTAX_CLASSIFICATIONS {
42-
EnumCaseDeclSyntax(
43-
"""
40+
DeclSyntax("""
4441
/// \(raw: classification.description)
4542
case \(raw: classification.swiftName)
46-
"""
47-
)
43+
""")
4844
}
4945
}
5046

51-
ExtensionDeclSyntax("extension SyntaxClassification") {
52-
FunctionDeclSyntax(
47+
try! ExtensionDeclSyntax("extension SyntaxClassification") {
48+
try! FunctionDeclSyntax(
5349
"""
5450
/// Checks if a node has a classification attached via its syntax definition.
5551
/// - Parameters:
@@ -62,24 +58,24 @@ let syntaxClassificationFile = SourceFileSyntax {
6258
parentKind: SyntaxKind, indexInParent: Int, childKind: SyntaxKind
6359
) -> (SyntaxClassification, Bool)?
6460
""") {
65-
IfStmtSyntax("""
61+
try! IfStmtSyntax("""
6662
// Separate checks for token nodes (most common checks) versus checks for layout nodes.
6763
if childKind == .token
6864
""") {
69-
SwitchStmtSyntax(expression: ExprSyntax("(parentKind, indexInParent)")) {
65+
try! SwitchStmtSyntax("switch (parentKind, indexInParent)") {
7066
for childClassification in node_child_classifications where childClassification.isToken {
7167
SwitchCaseSyntax("case (.\(raw: childClassification.parent.swiftSyntaxKind), \(raw: childClassification.childIndex)):") {
72-
ReturnStmtSyntax("return (.\(raw: childClassification.classification!.swiftName), \(raw: childClassification.force))")
68+
StmtSyntax("return (.\(raw: childClassification.classification!.swiftName), \(raw: childClassification.force))")
7369
}
7470
}
7571

7672
SwitchCaseSyntax("default: return nil")
7773
}
7874
} else: {
79-
SwitchStmtSyntax(expression: ExprSyntax("(parentKind, indexInParent)")) {
75+
try! SwitchStmtSyntax("switch (parentKind, indexInParent)") {
8076
for childClassification in node_child_classifications where !childClassification.isToken {
8177
SwitchCaseSyntax("case (.\(raw: childClassification.parent.swiftSyntaxKind), \(raw: childClassification.childIndex)):") {
82-
ReturnStmtSyntax("return (.\(raw: childClassification.classification!.swiftName), \(raw: childClassification.force))")
78+
StmtSyntax("return (.\(raw: childClassification.classification!.swiftName), \(raw: childClassification.force))")
8379
}
8480
}
8581

@@ -89,23 +85,20 @@ let syntaxClassificationFile = SourceFileSyntax {
8985
}
9086
}
9187

92-
ExtensionDeclSyntax("extension RawTokenKind") {
93-
VariableDeclSyntax(
94-
modifiers: [DeclModifierSyntax(name: .keyword(.internal))],
95-
name: IdentifierPatternSyntax("classification"),
96-
type: TypeAnnotationSyntax(type: TypeSyntax("SyntaxClassification"))) {
97-
SwitchStmtSyntax(expression: ExprSyntax("self.base")) {
88+
try! ExtensionDeclSyntax("extension RawTokenKind") {
89+
try! VariableDeclSyntax("internal var classification: SyntaxClassification") {
90+
try! SwitchStmtSyntax("switch self.base") {
9891
for token in SYNTAX_TOKENS {
9992
SwitchCaseSyntax("case .\(raw: token.swiftKind):") {
10093
if let classification = token.classification {
101-
ReturnStmtSyntax("return .\(raw: classification.swiftName)")
94+
StmtSyntax("return .\(raw: classification.swiftName)")
10295
} else {
103-
ReturnStmtSyntax("return .none)")
96+
StmtSyntax("return .none)")
10497
}
10598
}
10699
}
107100
SwitchCaseSyntax("case .eof:") {
108-
ReturnStmtSyntax("return .none")
101+
StmtSyntax("return .none")
109102
}
110103
}
111104
}

0 commit comments

Comments
 (0)