Skip to content

Commit f7c948c

Browse files
committed
Introduce ListBuilder protocol
1 parent 3ec17e0 commit f7c948c

File tree

1 file changed

+108
-0
lines changed

1 file changed

+108
-0
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import SwiftSyntax
14+
15+
public protocol ListBuilder {
16+
/// The type of the final returned result, which defaults to Component if
17+
/// buildFinalResult() is not provided.
18+
associatedtype FinalResult: SyntaxCollection
19+
20+
/// The type of individual statement expressions in the transformed function,
21+
/// which defaults to Component if buildExpression() is not provided.
22+
typealias Expression = FinalResult.Element
23+
24+
/// The type of a partial result, which will be carried through all of the
25+
/// build methods.
26+
typealias Component = [Expression]
27+
28+
/// Required by every result builder to build combined results from
29+
/// statement blocks.
30+
static func buildBlock(_ components: Component...) -> Component
31+
32+
/// Provides contextual type information for statement
33+
/// expressions to translate them into partial results.
34+
static func buildExpression(_ expression: Expression) -> Component
35+
36+
/// Add all the elements of `expression` to this result builder, effectively flattening them.
37+
///
38+
/// - Note: This overload is disfavored to resolve an ambiguity when both the final result and
39+
/// the elements are expressible by string interpolation. In that case we favor creating a
40+
/// single element from the string literal.
41+
@_disfavoredOverload
42+
static func buildExpression(_ expression: FinalResult) -> Component
43+
44+
/// Enables support for `if` statements that do not have an `else`.
45+
static func buildOptional(_ component: Component?) -> Component
46+
47+
/// With buildEither(second:), enables support for 'if-else' and 'switch'
48+
/// statements by folding conditional results into a single result.
49+
static func buildEither(first component: Component) -> Component
50+
51+
/// With buildEither(first:), enables support for 'if-else' and 'switch'
52+
/// statements by folding conditional results into a single result.
53+
static func buildEither(second component: Component) -> Component
54+
55+
/// Enables support for 'for..in' loops by combining the
56+
/// results of all iterations into a single result.
57+
static func buildArray(_ components: [Component]) -> Component
58+
59+
/// This will be called on the partial result of an 'if'
60+
/// #available' block to allow the result builder to erase type
61+
/// information.
62+
static func buildLimitedAvailability(_ component: Component) -> Component
63+
64+
/// This will be called on the partial result from the outermost
65+
/// block statement to produce the final returned result.
66+
static func buildFinalResult(_ component: Component) -> FinalResult
67+
}
68+
69+
public extension ListBuilder {
70+
static func buildBlock(_ components: Component...) -> Component {
71+
components.flatMap { $0 }
72+
}
73+
static func buildExpression(_ expression: Expression) -> Component {
74+
[expression]
75+
}
76+
@_disfavoredOverload
77+
static func buildExpression(_ expression: FinalResult) -> Component {
78+
expression.map { $0 }
79+
}
80+
static func buildOptional(_ component: Component?) -> Component {
81+
component ?? []
82+
}
83+
static func buildEither(first component: Component) -> Component {
84+
component
85+
}
86+
static func buildEither(second component: Component) -> Component {
87+
component
88+
}
89+
static func buildArray(_ components: [Component]) -> Component {
90+
components.flatMap { $0 }
91+
}
92+
static func buildLimitedAvailability(_ component: Component) -> Component {
93+
component
94+
}
95+
static func buildFinalResult(_ component: Component) -> FinalResult {
96+
.init(component)
97+
}
98+
}
99+
100+
public extension ListBuilder where Expression: WithTrailingCommaSyntax {
101+
static func buildFinalResult(_ component: Component) -> FinalResult {
102+
.init(
103+
component.enumerated().map { index, expression in
104+
index < component.endIndex - 1 ? expression.ensuringTrailingComma() : expression
105+
}
106+
)
107+
}
108+
}

0 commit comments

Comments
 (0)