Skip to content

Fix string interpolation parsing errors in CodeGeneration #1944

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 1 commit into from
Jul 25, 2023
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
26 changes: 17 additions & 9 deletions CodeGeneration/Sources/Utils/SyntaxBuildableChild.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,19 @@ public extension Child {
return self.type.optionalWrapped(type: SimpleTypeIdentifierSyntax(name: .identifier(parameterBaseType)))
}

/// If the child node has a default value, return an expression of the form
/// ` = default_value` that can be used as the default value to for a
/// function parameter. Otherwise, return `nil`.
var defaultInitialization: InitializerClauseSyntax? {
var defaultValue: ExprSyntax? {
if isOptional || isUnexpectedNodes {
if type.isBaseType && kind.isNodeChoicesEmpty {
return InitializerClauseSyntax(value: ExprSyntax("\(type.buildable).none"))
return ExprSyntax("\(type.buildable).none")
} else {
return InitializerClauseSyntax(value: NilLiteralExprSyntax())
return ExprSyntax("nil")
}
}
guard let token = token, isToken else {
return type.defaultValue.map { InitializerClauseSyntax(value: $0) }
return type.defaultValue
}
if token.text != nil {
return InitializerClauseSyntax(value: ExprSyntax(".\(raw: token.swiftKind)Token()"))
return ExprSyntax(".\(raw: token.swiftKind)Token()")
}
guard case .token(let choices, _, _) = kind, choices.count == 1, token.associatedValueClass != nil else {
return nil
Expand All @@ -85,7 +82,18 @@ public extension Child {
if textChoice == "init" {
textChoice = "`init`"
}
return InitializerClauseSyntax(value: ExprSyntax(".\(raw: token.swiftKind)(.\(raw: textChoice))"))
return ExprSyntax(".\(raw: token.swiftKind)(.\(raw: textChoice))")
}

/// If the child node has a default value, return an expression of the form
/// ` = default_value` that can be used as the default value to for a
/// function parameter. Otherwise, return `nil`.
var defaultInitialization: InitializerClauseSyntax? {
if let defaultValue {
return InitializerClauseSyntax(equal: .equalToken(leadingTrivia: .space, trailingTrivia: .space), value: defaultValue)
} else {
return nil
}
}

/// If this node is a token that can't contain arbitrary text, generate a Swift
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,5 +198,5 @@ fileprivate func convertFromSyntaxProtocolToSyntaxType(child: Child, useDeprecat
if child.type.isBaseType && !child.kind.isNodeChoices {
return ExprSyntax("\(raw: child.type.syntaxBaseName)(fromProtocol: \(raw: childName))")
}
return ExprSyntax("\(raw: childName)")
return ExprSyntax("\(raw: childName.backtickedIfNeeded)")
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ let rawSyntaxNodesFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
try! StructDeclSyntax(
"""
@_spi(RawSyntax)
public struct \(node.kind.rawType): \(node.kind.isBase ? node.kind.rawProtocolType : node.base.rawProtocolType))
public struct \(node.kind.rawType): \(node.kind.isBase ? node.kind.rawProtocolType : node.base.rawProtocolType)
"""
) {
for (name, choices) in node.childrenChoicesEnums {
Expand Down