Skip to content

Commit fbed6c2

Browse files
committed
Make TokenSyntax expressible by string literal and remove convenience initializers that take a string
rdar://104090088
1 parent 8b8eb44 commit fbed6c2

File tree

10 files changed

+93
-777
lines changed

10 files changed

+93
-777
lines changed

CodeGeneration/Sources/generate-swiftsyntax/templates/swiftsyntaxbuilder/BuildableNodesFile.swift

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -109,22 +109,6 @@ private func createConvenienceInitializer(node: Node) -> InitializerDeclSyntax?
109109
"@\(builderInitializableType.resultBuilderBaseName) \(child.swiftName)Builder: () -> \(builderInitializableType.syntax) = \(defaultArgument)",
110110
for: .functionParameters
111111
))
112-
} else if let token = child.type.token, token.text == nil, (child.textChoices.count != 1 || token.associatedValueClass == nil) {
113-
// Allow initializing identifiers and other tokens without default text with a String
114-
shouldCreateInitializer = true
115-
let paramType = child.type.optionalWrapped(type: "\(raw: token.associatedValueClass ?? "String")" as TypeSyntax)
116-
let tokenExpr = MemberAccessExprSyntax("TokenSyntax.\(raw: token.swiftKind.withFirstCharacterLowercased.backticked)")
117-
if child.type.isOptional {
118-
produceExpr = ExprSyntax(FunctionCallExprSyntax("\(raw: child.swiftName).map { \(tokenExpr)($0) }"))
119-
} else {
120-
produceExpr = ExprSyntax(FunctionCallExprSyntax("\(tokenExpr)(\(raw: child.swiftName))"))
121-
}
122-
normalParameters.append(FunctionParameterSyntax(
123-
firstName: .identifier(child.swiftName),
124-
colon: .colonToken(),
125-
type: paramType,
126-
defaultArgument: child.defaultInitialization.map { InitializerClauseSyntax(value: $0) }
127-
))
128112
} else {
129113
produceExpr = convertFromSyntaxProtocolToSyntaxType(child: child)
130114
normalParameters.append(FunctionParameterSyntax(
@@ -142,11 +126,7 @@ private func createConvenienceInitializer(node: Node) -> InitializerDeclSyntax?
142126
}
143127

144128
return InitializerDeclSyntax(
145-
leadingTrivia: [
146-
"/// A convenience initializer that allows:",
147-
"/// - Initializing syntax collections using result builders",
148-
"/// - Initializing tokens without default text using strings",
149-
].map { .docLineComment($0) + .newline }.reduce([], +),
129+
leadingTrivia: .docLineComment("/// A convenience initializer that allows initializing syntax collections using result builders") + .newline,
150130
modifiers: [DeclModifierSyntax(name: .keyword(.public))],
151131
signature: FunctionSignatureSyntax(
152132
input: ParameterClauseSyntax {

Sources/SwiftSyntaxBuilder/ConvenienceInitializers.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ extension BinaryOperatorExprSyntax {
2525

2626
extension BooleanLiteralExprSyntax: ExpressibleByBooleanLiteral {
2727
public init(_ value: Bool) {
28-
self.init(booleanLiteral: value ? .true : .false)
28+
self.init(booleanLiteral: value ? .keyword(.true) : .keyword(.false))
2929
}
3030

3131
public init(booleanLiteral value: Bool) {
@@ -130,7 +130,7 @@ extension ExprSyntax {
130130

131131
extension FloatLiteralExprSyntax: ExpressibleByFloatLiteral {
132132
public init(_ value: Float) {
133-
self.init(floatingDigits: String(value))
133+
self.init(floatingDigits: .floatingLiteral(String(value)))
134134
}
135135

136136
public init(floatLiteral value: Float) {
@@ -213,7 +213,7 @@ extension IfStmtSyntax {
213213

214214
extension IntegerLiteralExprSyntax: ExpressibleByIntegerLiteral {
215215
public init(_ value: Int) {
216-
self.init(digits: String(value))
216+
self.init(digits: .integerLiteral(String(value)))
217217
}
218218

219219
public init(integerLiteral value: Int) {
@@ -402,7 +402,7 @@ extension VariableDeclSyntax {
402402
leadingTrivia: leadingTrivia,
403403
attributes: attributes?.withTrailingTrivia(.space),
404404
modifiers: modifiers,
405-
letOrVarKeyword: letOrVarKeyword
405+
letOrVarKeyword: .keyword(letOrVarKeyword)
406406
) {
407407
PatternBindingSyntax(
408408
pattern: name,
@@ -425,7 +425,7 @@ extension VariableDeclSyntax {
425425
leadingTrivia: leadingTrivia,
426426
attributes: attributes?.withTrailingTrivia(.space),
427427
modifiers: modifiers,
428-
letOrVarKeyword: .var
428+
letOrVarKeyword: .keyword(.var)
429429
) {
430430
PatternBindingSyntax(
431431
pattern: name,

Sources/SwiftSyntaxBuilder/Syntax+StringInterpolation.swift

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
import SwiftBasicFormat
1414
import SwiftDiagnostics
15-
import SwiftSyntax
15+
@_spi(RawSyntax) import SwiftSyntax
1616

1717
/// An individual interpolated syntax node.
1818
struct InterpolatedSyntaxNode {
@@ -266,7 +266,7 @@ extension ExpressibleByLiteralSyntax where Self: BinaryInteger {
266266
public func makeLiteralSyntax() -> IntegerLiteralExprSyntax {
267267
// TODO: Radix selection? Thousands separators?
268268
let digits = String(self, radix: 10)
269-
return IntegerLiteralExprSyntax(digits: digits)
269+
return IntegerLiteralExprSyntax(digits: .integerLiteral(digits))
270270
}
271271
}
272272
extension Int: ExpressibleByLiteralSyntax {}
@@ -303,7 +303,7 @@ extension ExpressibleByLiteralSyntax where Self: FloatingPoint, Self: LosslessSt
303303
case .negativeNormal, .negativeSubnormal, .positiveZero, .positiveSubnormal, .positiveNormal:
304304
// TODO: Thousands separators?
305305
let digits = String(self)
306-
return ExprSyntax(FloatLiteralExprSyntax(floatingDigits: digits))
306+
return ExprSyntax(FloatLiteralExprSyntax(floatingDigits: .floatingLiteral(digits)))
307307
}
308308

309309
}
@@ -438,3 +438,12 @@ extension Optional: ExpressibleByLiteralSyntax where Wrapped: ExpressibleByLiter
438438
}
439439
}
440440
}
441+
442+
extension TokenSyntax: SyntaxExpressibleByStringInterpolation {
443+
public init(stringInterpolationOrThrow stringInterpolation: SyntaxStringInterpolation) throws {
444+
let string = stringInterpolation.sourceText.withUnsafeBufferPointer { buf in
445+
return String(syntaxText: SyntaxText(buffer: buf))
446+
}
447+
self = .identifier(string)
448+
}
449+
}

0 commit comments

Comments
 (0)