Skip to content

Update CodeGeneration to latest SwiftSyntax version #1230

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CodeGeneration/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ let package = Package(
.executable(name: "generate-swiftsyntax", targets: ["generate-swiftsyntax"]),
],
dependencies: [
.package(url: "https://github.com/apple/swift-syntax.git", revision: "d856f486d15a61625e90c1740729b3fb5244fad1"),
.package(url: "https://github.com/apple/swift-syntax.git", revision: "dc89ef5759b58298f04c5edee48aed5e678e80f0"),
.package(url: "https://github.com/apple/swift-argument-parser.git", .upToNextMinor(from: "1.1.4")),
],
targets: [
Expand Down
34 changes: 17 additions & 17 deletions CodeGeneration/Sources/Utils/SyntaxBuildableChild.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public extension Child {
}
}

var parameterType: Type {
return self.type.optionalWrapped(type: SimpleTypeIdentifier(name: .identifier(parameterBaseType)))
var parameterType: TypeSyntax {
return self.type.optionalWrapped(type: SimpleTypeIdentifierSyntax(name: .identifier(parameterBaseType)))
}

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

var defaultInitialization: Expr? {
var defaultInitialization: ExprSyntax? {
if self.textChoices.count == 1, let token = token, token.associatedValueClass != nil {
var textChoice = self.textChoices[0]
if textChoice == "init" {
textChoice = "`init`"
}
return Expr(".\(raw: token.swiftKind)(.\(raw: textChoice))")
return ExprSyntax(".\(raw: token.swiftKind)(.\(raw: textChoice))")
} else {
return type.defaultInitialization
}
Expand All @@ -59,7 +59,7 @@ public extension Child {
/// If this node is a token that can't contain arbitrary text, generate a Swift
/// `assert` statement that verifies the variable with name var_name and of type
/// `TokenSyntax` contains one of the supported text options. Otherwise return `nil`.
func generateAssertStmtTextChoices(varName: String) -> FunctionCallExpr? {
func generateAssertStmtTextChoices(varName: String) -> FunctionCallExprSyntax? {
guard type.isToken else {
return nil
}
Expand All @@ -76,24 +76,24 @@ public extension Child {
return nil
}

var assertChoices: [Expr] = []
var assertChoices: [ExprSyntax] = []
if type.isOptional {
assertChoices.append(Expr(SequenceExpr {
IdentifierExpr(identifier: .identifier(varName))
BinaryOperatorExpr(text: "==")
NilLiteralExpr()
assertChoices.append(ExprSyntax(SequenceExprSyntax {
IdentifierExprSyntax(identifier: .identifier(varName))
BinaryOperatorExprSyntax(text: "==")
NilLiteralExprSyntax()
}))
}
for textChoice in choices {
assertChoices.append(Expr(SequenceExpr {
MemberAccessExpr(base: type.forceUnwrappedIfNeeded(expr: IdentifierExpr(identifier: .identifier(varName))), name: "text")
BinaryOperatorExpr(text: "==")
StringLiteralExpr(content: textChoice)
assertChoices.append(ExprSyntax(SequenceExprSyntax {
MemberAccessExprSyntax(base: type.forceUnwrappedIfNeeded(expr: IdentifierExprSyntax(identifier: .identifier(varName))), name: "text")
BinaryOperatorExprSyntax(text: "==")
StringLiteralExprSyntax(content: textChoice)
}))
}
let disjunction = ExprList(assertChoices.flatMap { [$0, Expr(BinaryOperatorExpr(text: "||"))] }.dropLast())
return FunctionCallExpr(callee: Expr("assert")) {
TupleExprElement(expression: SequenceExpr(elements: disjunction))
let disjunction = ExprListSyntax(assertChoices.flatMap { [$0, ExprSyntax(BinaryOperatorExprSyntax(text: "||"))] }.dropLast())
return FunctionCallExprSyntax(callee: ExprSyntax("assert")) {
TupleExprElementSyntax(expression: SequenceExprSyntax(elements: disjunction))
}
}
}
26 changes: 13 additions & 13 deletions CodeGeneration/Sources/Utils/SyntaxBuildableType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ public struct SyntaxBuildableType: Hashable {
/// with fixed test), return an expression of the form ` = defaultValue`
/// that can be used as the default value for a function parameter.
/// Otherwise, return the empty string.
public var defaultInitialization: Expr? {
public var defaultInitialization: ExprSyntax? {
if isOptional {
return Expr(NilLiteralExpr())
return ExprSyntax(NilLiteralExprSyntax())
} else if isToken {
if let token = token, token.text != nil {
return Expr(MemberAccessExpr(base: "TokenSyntax", name: lowercaseFirstWord(name: token.name).backticked))
return ExprSyntax(MemberAccessExprSyntax(base: "TokenSyntax", name: lowercaseFirstWord(name: token.name).backticked))
} else if tokenKind == "EOFToken" {
return Expr(MemberAccessExpr(base: "TokenSyntax", name: "eof"))
return ExprSyntax(MemberAccessExprSyntax(base: "TokenSyntax", name: "eof"))
}
}
return nil
Expand All @@ -86,8 +86,8 @@ public struct SyntaxBuildableType: Hashable {
/// - For base kinds: `<BaseKind>Buildable`, e.g. `ExprBuildable` (these are implemented as protocols)
/// - For token: `TokenSyntax` (tokens don't have a dedicated type in SwiftSyntaxBuilder)
/// If the type is optional, the type is wrapped in an `OptionalType`.
public var buildable: Type {
optionalWrapped(type: SimpleTypeIdentifier(name: .identifier(syntaxBaseName)))
public var buildable: TypeSyntax {
optionalWrapped(type: SimpleTypeIdentifierSyntax(name: .identifier(syntaxBaseName)))
}

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

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

public var parameterType: TypeSyntax {
return optionalWrapped(type: SimpleTypeIdentifier(name: .identifier(parameterBaseType)))
return optionalWrapped(type: SimpleTypeIdentifierSyntax(name: .identifier(parameterBaseType)))
}

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

/// Wraps a type in an optional chaining depending on whether `isOptional` is true.
public func optionalChained(expr: ExprSyntaxProtocol) -> Expr {
public func optionalChained(expr: ExprSyntaxProtocol) -> ExprSyntax {
if isOptional {
return Expr(OptionalChainingExpr(expression: expr))
return ExprSyntax(OptionalChainingExprSyntax(expression: expr))
} else {
return Expr(expr)
return ExprSyntax(expr)
}
}

/// Wraps a type in a force unwrap expression depending on whether `isOptional` is true.
public func forceUnwrappedIfNeeded(expr: ExprSyntaxProtocol) -> ExprSyntax {
if isOptional {
return ExprSyntax(ForcedValueExpr(expression: expr))
return ExprSyntax(ForcedValueExprSyntax(expression: expr))
} else {
return ExprSyntax(expr)
}
Expand Down
26 changes: 13 additions & 13 deletions CodeGeneration/Sources/generate-swiftsyntax/KeywordFile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import SwiftSyntaxBuilder
import SyntaxSupport
import Utils

let keywordFile = SourceFile {
ExtensionDecl(
let keywordFile = SourceFileSyntax {
ExtensionDeclSyntax(
"""
\(raw: generateCopyrightHeader(for: "generate-swiftparser"))

Expand All @@ -29,22 +29,22 @@ let keywordFile = SourceFile {
"""
)

EnumDecl("""
EnumDeclSyntax("""
@frozen // FIXME: Not actually stable, works around a miscompile
public enum Keyword: StaticString
""") {
for keyword in KEYWORDS {
EnumCaseDecl("case \(raw: keyword.escapedName)")
EnumCaseDeclSyntax("case \(raw: keyword.escapedName)")
}

InitializerDecl("@_spi(RawSyntax) public init?(_ text: SyntaxText)") {
SwitchStmt(expression: Expr("text.count")) {
InitializerDeclSyntax("@_spi(RawSyntax) public init?(_ text: SyntaxText)") {
SwitchStmtSyntax(expression: ExprSyntax("text.count")) {
for (length, keywords) in keywordsByLength() {
SwitchCaseSyntax("case \(raw: length):") {
SwitchStmt(expression: Expr("text")) {
SwitchStmtSyntax(expression: ExprSyntax("text")) {
for keyword in keywords {
SwitchCaseSyntax(#"case "\#(raw: keyword.name)":"#) {
Expr("self = .\(raw: keyword.escapedName)")
ExprSyntax("self = .\(raw: keyword.escapedName)")
}
}
SwitchCaseSyntax("default: return nil")
Expand All @@ -55,17 +55,17 @@ let keywordFile = SourceFile {
}
}

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

VariableDecl(
VariableDeclSyntax(
"""
var defaultText: SyntaxText {
return SyntaxText(self.rawValue)
Expand Down
Loading