1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#if swift(>=6)
public import SwiftSyntax
#else
import SwiftSyntax
#endif
// MARK: - PartialSyntaxNode
@available(*, deprecated, renamed: "SyntaxNodeString")
public typealias PartialSyntaxNodeString = SyntaxNodeString
/// A type that is expressible by string interpolation the same way that syntax
/// nodes are, but instead of producing a node, it stores the string interpolation
/// text.
///
/// Used to represent partial syntax nodes in initializers that take a
/// trailing code block.
///
/// This type should always be constructed using string interpolation.
public struct SyntaxNodeString: SyntaxExpressibleByStringInterpolation {
let sourceText: [UInt8]
public init(stringInterpolation: SyntaxStringInterpolation) {
self.sourceText = stringInterpolation.sourceText
}
}
extension SyntaxStringInterpolation {
public mutating func appendInterpolation(_ value: SyntaxNodeString) {
sourceText.append(contentsOf: value.sourceText)
self.lastIndentation = nil
}
}
// MARK: - HasTrailingCodeBlock
public protocol HasTrailingCodeBlock: WithCodeBlockSyntax {
/// Constructs a syntax node where `header` builds the text of the node before the body in braces and `bodyBuilder` is used to build the node’s body.
///
/// For example, you can construct
///
/// ```swift
/// while x < 5 {
/// x += 1
/// }
/// ```
///
/// using this call
///
/// ```swift
/// try WhileStmtSyntax("while x < 5") {
/// ExprSyntax("x += 1")
/// }
/// ```
///
/// Throws an error if `header` defines a different node type than the type the initializer is called on. E.g. if calling `try IfStmtSyntax("while x < 5") {}`
init(_ header: SyntaxNodeString, @CodeBlockItemListBuilder bodyBuilder: () throws -> CodeBlockItemListSyntax) rethrows
}
extension HasTrailingCodeBlock where Self: StmtSyntaxProtocol {
public init(
_ header: SyntaxNodeString,
@CodeBlockItemListBuilder bodyBuilder: () throws -> CodeBlockItemListSyntax
) throws {
let stmt = StmtSyntax("\(header) {}")
guard let castedStmt = stmt.as(Self.self) else {
throw SyntaxStringInterpolationInvalidNodeTypeError(expectedType: Self.self, actualNode: stmt)
}
self = castedStmt
self.body = try CodeBlockSyntax(statements: bodyBuilder())
}
}
extension CatchClauseSyntax: HasTrailingCodeBlock {
public init(
_ header: SyntaxNodeString,
@CodeBlockItemListBuilder bodyBuilder: () throws -> CodeBlockItemListSyntax
) rethrows {
self = CatchClauseSyntax("\(header) {}")
self.body = try CodeBlockSyntax(statements: bodyBuilder())
}
}
extension DeferStmtSyntax: HasTrailingCodeBlock {}
extension DoStmtSyntax: HasTrailingCodeBlock {}
extension ForStmtSyntax: HasTrailingCodeBlock {}
extension GuardStmtSyntax: HasTrailingCodeBlock {}
extension WhileStmtSyntax: HasTrailingCodeBlock {}
// MARK: - WithOptionalCodeBlockSyntax
extension WithOptionalCodeBlockSyntax where Self: DeclSyntaxProtocol {
/// Constructs a syntax node where `header` builds the text of the node before the body in braces and `bodyBuilder` is used to build the node’s body.
///
/// For example, you can construct
///
/// ```swift
/// func addOne(_ base: Int) -> Int {
/// return base + 1
/// }
/// ```
///
/// using this call
///
/// ```swift
/// try FunctionDeclSyntax("func addOne(_ base: Int) -> Int") {
/// ExprSyntax("return base + 1")
/// }
/// ```
///
/// Throws an error if `header` defines a different node type than the type the initializer is called on. E.g. if calling `try FunctionDeclSyntax("init") {}`
public init(
_ header: SyntaxNodeString,
@CodeBlockItemListBuilder bodyBuilder: () throws -> CodeBlockItemListSyntax
) throws {
let decl = DeclSyntax("\(header) {}")
guard let castedDecl = decl.as(Self.self) else {
throw SyntaxStringInterpolationInvalidNodeTypeError(expectedType: Self.self, actualNode: decl)
}
self = castedDecl
self.body = try CodeBlockSyntax(statements: bodyBuilder())
}
}
// MARK: HasTrailingMemberDeclBlock
public protocol HasTrailingMemberDeclBlock {
var memberBlock: MemberBlockSyntax { get set }
/// Constructs a syntax node where `header` builds the text of the node before the members in braces and `membersBuilder` is used to list the node’s members.
///
/// For example, you can construct
///
/// ```swift
/// struct Point {
/// var x: Int
/// var y: Int
/// }
/// ```
///
/// using this call
///
/// ```swift
/// try StructDeclSyntax("struct Point") {
/// DeclSyntax("var x: Int")
/// DeclSyntax("var y: Int")
/// }
/// ```
///
/// Throws an error if `header` defines a different node type than the type the initializer is called on. E.g. if calling `try StructDeclSyntax("class MyClass") {}`
init(
_ header: SyntaxNodeString,
@MemberBlockItemListBuilder membersBuilder: () throws -> MemberBlockItemListSyntax
) throws
}
extension HasTrailingMemberDeclBlock where Self: DeclSyntaxProtocol {
public init(
_ header: SyntaxNodeString,
@MemberBlockItemListBuilder membersBuilder: () throws -> MemberBlockItemListSyntax
) throws {
let decl = DeclSyntax("\(header) {}")
guard let castedDecl = decl.as(Self.self) else {
throw SyntaxStringInterpolationInvalidNodeTypeError(expectedType: Self.self, actualNode: decl)
}
self = castedDecl
self.memberBlock = try MemberBlockSyntax(members: membersBuilder())
}
}
extension ActorDeclSyntax: HasTrailingMemberDeclBlock {}
extension ClassDeclSyntax: HasTrailingMemberDeclBlock {}
extension EnumDeclSyntax: HasTrailingMemberDeclBlock {}
extension ExtensionDeclSyntax: HasTrailingMemberDeclBlock {}
extension ProtocolDeclSyntax: HasTrailingMemberDeclBlock {}
extension StructDeclSyntax: HasTrailingMemberDeclBlock {}
// MARK: - IfExprSyntax
// IfExprSyntax is a special scenario as we also have the `else` body or an if-else
// So we cannot conform to `HasTrailingCodeBlock`
extension IfExprSyntax {
/// Constructs an `if` expression with an optional `else` block.
///
/// `header` specifies the part of the `if` expression before the body’s first brace.
///
/// For example, the following `if` expression has the header `if sunny`
///
/// ```swift
/// if sunny {
/// sunbath()
/// }
/// ```
///
/// If `elseBuilder` is not `nil`, an `else` keyword will automatically be inserted.
///
/// This function takes care of inserting the braces as well.
///
/// Throws an error if `header` does not start an `if` expression. E.g. if calling `try IfExprSyntax("while true") {}`
public init(
_ header: SyntaxNodeString,
@CodeBlockItemListBuilder bodyBuilder: () throws -> CodeBlockItemListSyntax,
@CodeBlockItemListBuilder `else` elseBuilder: () throws -> CodeBlockItemListSyntax? = { nil }
) throws {
let expr = ExprSyntax("\(header) {}")
guard let ifExpr = expr.as(Self.self) else {
throw SyntaxStringInterpolationInvalidNodeTypeError(expectedType: Self.self, actualNode: expr)
}
self = ifExpr
self.body = try CodeBlockSyntax(statements: bodyBuilder())
self.elseBody = try elseBuilder().map { .codeBlock(CodeBlockSyntax(statements: $0)) }
self.elseKeyword = elseBody != nil ? .keyword(.else) : nil
}
/// Constructs an `if` expression with a following `else if` clause.
/// This can be used to create longer chains of `if`, `else if` expressions.
///
/// For example, you can construct
///
/// ```swift
/// if x == 1 {
/// return "one
/// } else if x == 2 {
/// return "two
/// } else {
/// return "many
/// }
/// ```
///
/// using this call
///
/// ```swift
/// try IfExprSyntax(
/// "if x == 1",
/// bodyBuilder: {
/// StmtSyntax(#"return "one""#)
/// }, elseIf: IfExprSyntax(
/// "if x == 2",
/// bodyBuilder: {
/// StmtSyntax(#"return "two""#)
/// }, else: {
/// StmtSyntax(#"return "many""#)
/// }
/// )
/// )
/// ```
///
/// Throws an error if `header` does not start an `if` expression. E.g. if calling `try IfExprSyntax("while true", bodyBuilder: {}, elseIf: {})`
public init(
_ header: SyntaxNodeString,
@CodeBlockItemListBuilder bodyBuilder: () throws -> CodeBlockItemListSyntax,
elseIf: IfExprSyntax
) throws {
let expr = ExprSyntax("\(header) {}")
guard let ifExpr = expr.as(Self.self) else {
throw SyntaxStringInterpolationInvalidNodeTypeError(expectedType: Self.self, actualNode: expr)
}
self = ifExpr
self.body = CodeBlockSyntax(statements: try bodyBuilder())
self.elseBody = .ifExpr(elseIf)
self.elseKeyword = elseBody != nil ? .keyword(.else) : nil
}
}
// MARK: - SwitchCase
extension SwitchCaseSyntax {
/// Constructs a case item where `header` includes the text between the `case` keyword and the `:` (both inclusive) and `statementsBuilder` can be used to build the statements inside the case item.
///
/// For example, you can construct
///
/// ```swift
/// default:
/// return nil
/// ```
///
/// using this call
///
/// ```swift
/// try SwitchCaseSyntax("default:") {
/// StmtSyntax("return")
/// }
/// ```
///
/// Throws an error if `header` does not start a switch case item. E.g. if calling `try SwitchCaseSyntax("func foo") {}`
public init(
_ header: SyntaxNodeString,
@CodeBlockItemListBuilder statementsBuilder: () throws -> CodeBlockItemListSyntax
) rethrows {
self = SwitchCaseSyntax("\(header)")
self.statements = try statementsBuilder()
}
}
// MARK: - SwitchExprSyntax
// SwitchExprSyntax is a special scenario as it don't have body or members
// So we cannot conform to `HasTrailingCodeBlock` or `HasTrailingMemberDeclBlock`
extension SwitchExprSyntax {
/// Constructs a `switch` expression where `header` builds the text before the opening `{` and `casesBuilder` can be used to build the case items.
///
/// For example, to construct
///
/// ```swift
/// switch direction {
/// case .up:
/// goUp()
/// case .down:
/// goDown()
/// }
/// ```
///
/// using this call
///
/// ```swift
/// try SwitchExprSyntax("switch direction") {
/// SwitchCaseSyntax("case .up: goUp()")
/// SwitchCaseSyntax("case .down: goDown()")
/// }
/// ```
///
/// Throws an error if `header` does not start a switch expression. E.g. if calling `try SwitchExprSyntax("if x < 42") {}`
public init(
_ header: SyntaxNodeString,
@SwitchCaseListBuilder casesBuilder: () throws -> SwitchCaseListSyntax = { SwitchCaseListSyntax([]) }
) throws {
let expr = ExprSyntax("\(header) {}")
guard let switchExpr = expr.as(Self.self) else {
throw SyntaxStringInterpolationInvalidNodeTypeError(expectedType: Self.self, actualNode: expr)
}
self = switchExpr
self.cases = try casesBuilder()
}
}
// MARK: - VariableDeclSyntax
// VariableDeclSyntax is a special scenario as it don't have body or members
// So we cannot conform to `HasTrailingCodeBlock` or `HasTrailingMemberDeclBlock`
extension VariableDeclSyntax {
/// Construct a variable with a single `get` accessor where `header` builds the text before the opening `{` and `accessor` builds the accessor body.
///
/// For example, to construct
///
/// ```swift
/// var x: Int {
/// return origin.x
/// }
/// ```
///
/// using this call
///
/// ```swift
/// try VariableDeclSyntax("var x: Int") {
/// StmtSyntax("return origin.x")
/// }
/// ```
///
/// Throws an error if `header` does not start a variable declaration. E.g. if calling `try VariableDeclSyntax("func foo") {}`
public init(
_ header: SyntaxNodeString,
@CodeBlockItemListBuilder accessor: () throws -> CodeBlockItemListSyntax
) throws {
let decl = DeclSyntax("\(header) {}")
guard let castedDecl = decl.as(Self.self) else {
throw SyntaxStringInterpolationInvalidNodeTypeError(expectedType: Self.self, actualNode: decl)
}
self = castedDecl
precondition(self.bindings.count == 1)
var binding: PatternBindingSyntax? = self.bindings.last
binding?.accessorBlock = AccessorBlockSyntax(accessors: .getter(try accessor()))
bindings = PatternBindingListSyntax([binding].compactMap { $0 })
}
}
|