Skip to content

Commit e09c64d

Browse files
authored
Merge pull request #1230 from ahoppen/ahoppen/update-codegeneration-2022-01-13
Update CodeGeneration to latest SwiftSyntax version
2 parents e7fc8a9 + d37b82a commit e09c64d

33 files changed

+1655
-1621
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: "d856f486d15a61625e90c1740729b3fb5244fad1"),
14+
.package(url: "https://github.com/apple/swift-syntax.git", revision: "dc89ef5759b58298f04c5edee48aed5e678e80f0"),
1515
.package(url: "https://github.com/apple/swift-argument-parser.git", .upToNextMinor(from: "1.1.4")),
1616
],
1717
targets: [

CodeGeneration/Sources/Utils/SyntaxBuildableChild.swift

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ public extension Child {
3434
}
3535
}
3636

37-
var parameterType: Type {
38-
return self.type.optionalWrapped(type: SimpleTypeIdentifier(name: .identifier(parameterBaseType)))
37+
var parameterType: TypeSyntax {
38+
return self.type.optionalWrapped(type: SimpleTypeIdentifierSyntax(name: .identifier(parameterBaseType)))
3939
}
4040

4141
/// If the child node has documentation associated with it, return it as single
@@ -44,13 +44,13 @@ public extension Child {
4444
flattened(indentedDocumentation: description ?? "")
4545
}
4646

47-
var defaultInitialization: Expr? {
47+
var defaultInitialization: ExprSyntax? {
4848
if self.textChoices.count == 1, let token = token, token.associatedValueClass != nil {
4949
var textChoice = self.textChoices[0]
5050
if textChoice == "init" {
5151
textChoice = "`init`"
5252
}
53-
return Expr(".\(raw: token.swiftKind)(.\(raw: textChoice))")
53+
return ExprSyntax(".\(raw: token.swiftKind)(.\(raw: textChoice))")
5454
} else {
5555
return type.defaultInitialization
5656
}
@@ -59,7 +59,7 @@ public extension Child {
5959
/// If this node is a token that can't contain arbitrary text, generate a Swift
6060
/// `assert` statement that verifies the variable with name var_name and of type
6161
/// `TokenSyntax` contains one of the supported text options. Otherwise return `nil`.
62-
func generateAssertStmtTextChoices(varName: String) -> FunctionCallExpr? {
62+
func generateAssertStmtTextChoices(varName: String) -> FunctionCallExprSyntax? {
6363
guard type.isToken else {
6464
return nil
6565
}
@@ -76,24 +76,24 @@ public extension Child {
7676
return nil
7777
}
7878

79-
var assertChoices: [Expr] = []
79+
var assertChoices: [ExprSyntax] = []
8080
if type.isOptional {
81-
assertChoices.append(Expr(SequenceExpr {
82-
IdentifierExpr(identifier: .identifier(varName))
83-
BinaryOperatorExpr(text: "==")
84-
NilLiteralExpr()
81+
assertChoices.append(ExprSyntax(SequenceExprSyntax {
82+
IdentifierExprSyntax(identifier: .identifier(varName))
83+
BinaryOperatorExprSyntax(text: "==")
84+
NilLiteralExprSyntax()
8585
}))
8686
}
8787
for textChoice in choices {
88-
assertChoices.append(Expr(SequenceExpr {
89-
MemberAccessExpr(base: type.forceUnwrappedIfNeeded(expr: IdentifierExpr(identifier: .identifier(varName))), name: "text")
90-
BinaryOperatorExpr(text: "==")
91-
StringLiteralExpr(content: textChoice)
88+
assertChoices.append(ExprSyntax(SequenceExprSyntax {
89+
MemberAccessExprSyntax(base: type.forceUnwrappedIfNeeded(expr: IdentifierExprSyntax(identifier: .identifier(varName))), name: "text")
90+
BinaryOperatorExprSyntax(text: "==")
91+
StringLiteralExprSyntax(content: textChoice)
9292
}))
9393
}
94-
let disjunction = ExprList(assertChoices.flatMap { [$0, Expr(BinaryOperatorExpr(text: "||"))] }.dropLast())
95-
return FunctionCallExpr(callee: Expr("assert")) {
96-
TupleExprElement(expression: SequenceExpr(elements: disjunction))
94+
let disjunction = ExprListSyntax(assertChoices.flatMap { [$0, ExprSyntax(BinaryOperatorExprSyntax(text: "||"))] }.dropLast())
95+
return FunctionCallExprSyntax(callee: ExprSyntax("assert")) {
96+
TupleExprElementSyntax(expression: SequenceExprSyntax(elements: disjunction))
9797
}
9898
}
9999
}

CodeGeneration/Sources/Utils/SyntaxBuildableType.swift

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,14 @@ public struct SyntaxBuildableType: Hashable {
5353
/// with fixed test), return an expression of the form ` = defaultValue`
5454
/// that can be used as the default value for a function parameter.
5555
/// Otherwise, return the empty string.
56-
public var defaultInitialization: Expr? {
56+
public var defaultInitialization: ExprSyntax? {
5757
if isOptional {
58-
return Expr(NilLiteralExpr())
58+
return ExprSyntax(NilLiteralExprSyntax())
5959
} else if isToken {
6060
if let token = token, token.text != nil {
61-
return Expr(MemberAccessExpr(base: "TokenSyntax", name: lowercaseFirstWord(name: token.name).backticked))
61+
return ExprSyntax(MemberAccessExprSyntax(base: "TokenSyntax", name: lowercaseFirstWord(name: token.name).backticked))
6262
} else if tokenKind == "EOFToken" {
63-
return Expr(MemberAccessExpr(base: "TokenSyntax", name: "eof"))
63+
return ExprSyntax(MemberAccessExprSyntax(base: "TokenSyntax", name: "eof"))
6464
}
6565
}
6666
return nil
@@ -86,8 +86,8 @@ public struct SyntaxBuildableType: Hashable {
8686
/// - For base kinds: `<BaseKind>Buildable`, e.g. `ExprBuildable` (these are implemented as protocols)
8787
/// - For token: `TokenSyntax` (tokens don't have a dedicated type in SwiftSyntaxBuilder)
8888
/// If the type is optional, the type is wrapped in an `OptionalType`.
89-
public var buildable: Type {
90-
optionalWrapped(type: SimpleTypeIdentifier(name: .identifier(syntaxBaseName)))
89+
public var buildable: TypeSyntax {
90+
optionalWrapped(type: SimpleTypeIdentifierSyntax(name: .identifier(syntaxBaseName)))
9191
}
9292

9393
/// Whether parameters of this type should be initializable by a result builder.
@@ -120,7 +120,7 @@ public struct SyntaxBuildableType: Hashable {
120120
/// which will eventually get built from `SwiftSyntaxBuilder`. If the type
121121
/// is optional, this terminates with a `?`.
122122
public var syntax: TypeSyntax {
123-
return optionalWrapped(type: SimpleTypeIdentifier(name: .identifier(syntaxBaseName)))
123+
return optionalWrapped(type: SimpleTypeIdentifierSyntax(name: .identifier(syntaxBaseName)))
124124
}
125125

126126
/// The type that is used for paramters in SwiftSyntaxBuilder that take this
@@ -134,7 +134,7 @@ public struct SyntaxBuildableType: Hashable {
134134
}
135135

136136
public var parameterType: TypeSyntax {
137-
return optionalWrapped(type: SimpleTypeIdentifier(name: .identifier(parameterBaseType)))
137+
return optionalWrapped(type: SimpleTypeIdentifierSyntax(name: .identifier(parameterBaseType)))
138138
}
139139

140140
/// Assuming that this is a collection type, the non-optional type of the result builder
@@ -161,25 +161,25 @@ public struct SyntaxBuildableType: Hashable {
161161
/// Wraps a type in an optional depending on whether `isOptional` is true.
162162
public func optionalWrapped<TypeNode: TypeSyntaxProtocol>(type: TypeNode) -> TypeSyntax {
163163
if isOptional {
164-
return TypeSyntax(OptionalType(wrappedType: type))
164+
return TypeSyntax(OptionalTypeSyntax(wrappedType: type))
165165
} else {
166166
return TypeSyntax(type)
167167
}
168168
}
169169

170170
/// Wraps a type in an optional chaining depending on whether `isOptional` is true.
171-
public func optionalChained(expr: ExprSyntaxProtocol) -> Expr {
171+
public func optionalChained(expr: ExprSyntaxProtocol) -> ExprSyntax {
172172
if isOptional {
173-
return Expr(OptionalChainingExpr(expression: expr))
173+
return ExprSyntax(OptionalChainingExprSyntax(expression: expr))
174174
} else {
175-
return Expr(expr)
175+
return ExprSyntax(expr)
176176
}
177177
}
178178

179179
/// Wraps a type in a force unwrap expression depending on whether `isOptional` is true.
180180
public func forceUnwrappedIfNeeded(expr: ExprSyntaxProtocol) -> ExprSyntax {
181181
if isOptional {
182-
return ExprSyntax(ForcedValueExpr(expression: expr))
182+
return ExprSyntax(ForcedValueExprSyntax(expression: expr))
183183
} else {
184184
return ExprSyntax(expr)
185185
}

CodeGeneration/Sources/generate-swiftsyntax/KeywordFile.swift

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ import SwiftSyntaxBuilder
1515
import SyntaxSupport
1616
import Utils
1717

18-
let keywordFile = SourceFile {
19-
ExtensionDecl(
18+
let keywordFile = SourceFileSyntax {
19+
ExtensionDeclSyntax(
2020
"""
2121
\(raw: generateCopyrightHeader(for: "generate-swiftparser"))
2222
@@ -29,22 +29,22 @@ let keywordFile = SourceFile {
2929
"""
3030
)
3131

32-
EnumDecl("""
32+
EnumDeclSyntax("""
3333
@frozen // FIXME: Not actually stable, works around a miscompile
3434
public enum Keyword: StaticString
3535
""") {
3636
for keyword in KEYWORDS {
37-
EnumCaseDecl("case \(raw: keyword.escapedName)")
37+
EnumCaseDeclSyntax("case \(raw: keyword.escapedName)")
3838
}
3939

40-
InitializerDecl("@_spi(RawSyntax) public init?(_ text: SyntaxText)") {
41-
SwitchStmt(expression: Expr("text.count")) {
40+
InitializerDeclSyntax("@_spi(RawSyntax) public init?(_ text: SyntaxText)") {
41+
SwitchStmtSyntax(expression: ExprSyntax("text.count")) {
4242
for (length, keywords) in keywordsByLength() {
4343
SwitchCaseSyntax("case \(raw: length):") {
44-
SwitchStmt(expression: Expr("text")) {
44+
SwitchStmtSyntax(expression: ExprSyntax("text")) {
4545
for keyword in keywords {
4646
SwitchCaseSyntax(#"case "\#(raw: keyword.name)":"#) {
47-
Expr("self = .\(raw: keyword.escapedName)")
47+
ExprSyntax("self = .\(raw: keyword.escapedName)")
4848
}
4949
}
5050
SwitchCaseSyntax("default: return nil")
@@ -55,17 +55,17 @@ let keywordFile = SourceFile {
5555
}
5656
}
5757

58-
VariableDecl(
58+
VariableDeclSyntax(
5959
leadingTrivia: [
6060
.docLineComment("/// Whether the token kind is switched from being an identifier to being an identifier to a keyword in the lexer."),
6161
.newlines(1),
6262
.docLineComment("/// This is true for keywords that used to be considered non-contextual."),
6363
.newlines(1)
6464
],
65-
modifiers: [DeclModifier(name: .public)],
65+
modifiers: [DeclModifierSyntax(name: .keyword(.public))],
6666
name: "isLexerClassified",
67-
type: TypeAnnotation(type: Type("Bool"))) {
68-
SwitchStmt(expression: Expr("self")) {
67+
type: TypeAnnotationSyntax(type: TypeSyntax("Bool"))) {
68+
SwitchStmtSyntax(expression: ExprSyntax("self")) {
6969
for keyword in KEYWORDS {
7070
if keyword.isLexerClassified {
7171
SwitchCaseSyntax("case .\(raw: keyword.escapedName): return true")
@@ -75,7 +75,7 @@ let keywordFile = SourceFile {
7575
}
7676
}
7777

78-
VariableDecl(
78+
VariableDeclSyntax(
7979
"""
8080
var defaultText: SyntaxText {
8181
return SyntaxText(self.rawValue)

0 commit comments

Comments
 (0)