Skip to content

Commit 2a529d0

Browse files
committed
Add result builder to decl blocks
1 parent 717dbba commit 2a529d0

File tree

7 files changed

+588
-127
lines changed

7 files changed

+588
-127
lines changed

Sources/SwiftSyntaxBuilder/Buildables.swift.gyb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ public protocol ${kind}ListBuildable: SyntaxListBuildable {
4141

4242
% if kind == 'Syntax':
4343
public protocol ${kind}Buildable: ${kind}ListBuildable {
44+
% elif kind == 'Decl':
45+
public protocol ${kind}Buildable: SyntaxBuildable, ${kind}ListBuildable, ExpressibleAsMemberDeclListItem, ExpressibleAsCodeBlockItem {
4446
% else:
4547
public protocol ${kind}Buildable: SyntaxBuildable, ${kind}ListBuildable {
4648
% end
@@ -82,6 +84,20 @@ extension ${kind}Buildable {
8284
[build${kind}(format: format, leadingTrivia: leadingTrivia)]
8385
}
8486
}
87+
% if kind == 'Decl':
88+
89+
extension DeclBuildable {
90+
public func createMemberDeclListItem() -> MemberDeclListItem {
91+
MemberDeclListItem(decl: self)
92+
}
93+
}
94+
95+
extension DeclBuildable {
96+
public func createCodeBlockItem() -> CodeBlockItem {
97+
CodeBlockItem(item: self)
98+
}
99+
}
100+
% end
85101

86102
% end
87103
% end
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
%{
2+
from gyb_syntax_support import *
3+
from gyb_syntax_support.kinds import SYNTAX_BASE_KINDS, lowercase_first_word, syntax_buildable_child_type
4+
NODE_MAP = create_node_map()
5+
# -*- mode: Swift -*-
6+
# Ignore the following admonition it applies to the resulting .swift file only
7+
}%
8+
//// Automatically Generated From ResultBuilders.swift.gyb.
9+
//// Do Not Edit Directly!
10+
//===----------------------------------------------------------------------===//
11+
//
12+
// This source file is part of the Swift.org open source project
13+
//
14+
// Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors
15+
// Licensed under Apache License v2.0 with Runtime Library Exception
16+
//
17+
// See https://swift.org/LICENSE.txt for license information
18+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
19+
//
20+
//===----------------------------------------------------------------------===//
21+
22+
import SwiftSyntax
23+
24+
public protocol ExpressibleAsSyntaxBuildable {
25+
func createSyntaxBuildable() -> SyntaxBuildable
26+
}
27+
28+
public protocol ExpressibleAsTokenSyntax {
29+
func createTokenSyntax() -> TokenSyntax
30+
}
31+
32+
% for node in SYNTAX_NODES:
33+
% if node.is_syntax_collection():
34+
% # child_node = NODE_MAP.get(child.syntax_kind)
35+
% element_type = syntax_buildable_child_type(node.collection_element_type, node.collection_element, node.is_token())
36+
% # `SyntaxBuildable` and `TokenSyntax` are used multiple places
37+
% # so we add them manually af the buttom
38+
% if element_type not in ['SyntaxBuildable', 'TokenSyntax', 'Expr']:
39+
public protocol ExpressibleAs${element_type} {
40+
func create${element_type}() -> ${element_type}
41+
}
42+
% if element_type not in ['ExprBuildable']:
43+
44+
extension ${element_type}: ExpressibleAs${element_type} {
45+
public func create${element_type}() -> ${element_type} {
46+
self
47+
}
48+
}
49+
% end
50+
% end
51+
% end
52+
% end
53+
% for node in SYNTAX_NODES:
54+
% if node.is_buildable() and not node.is_syntax_collection() and node.base_kind not in ['Stmt', 'Type']:
55+
% if node.base_kind not in ['Syntax', 'Pattern', 'Decl', 'Expr']:
56+
57+
extension ${node.syntax_kind}: ExpressibleAs${node.syntax_kind} {
58+
public func create${node.syntax_kind}() -> ${node.syntax_kind} {
59+
${node.syntax_kind}(decl: self)
60+
}
61+
}
62+
% end
63+
% end
64+
% end

Sources/SwiftSyntaxBuilder/ResultBuilders.swift.gyb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ public struct ${node.syntax_kind}Builder {
2929

3030
/// The type of individual statement expressions in the transformed function,
3131
/// which defaults to Component if buildExpression() is not provided.
32-
public typealias Expression = ${element_type}
32+
public typealias Expression = ExpressibleAs${element_type}
3333

3434
/// The type of a partial result, which will be carried through all of the
3535
/// build methods.
36-
public typealias Component = [${element_type}]
36+
public typealias Component = [ExpressibleAs${element_type}]
3737

3838
/// The type of the final returned result, which defaults to Component if
3939
/// buildFinalResult() is not provided.
@@ -84,7 +84,7 @@ public struct ${node.syntax_kind}Builder {
8484
/// If declared, this will be called on the partial result from the outermost
8585
/// block statement to produce the final returned result.
8686
public static func buildFinalResult(_ component: Component) -> FinalResult {
87-
.init(component)
87+
.init(component.map { $0.create${element_type}() })
8888
}
8989
}
9090

Sources/SwiftSyntaxBuilder/gyb_generated/Buildables.swift

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public protocol DeclListBuildable: SyntaxListBuildable {
2424
func buildDeclList(format: Format, leadingTrivia: Trivia?) -> [DeclSyntax]
2525
}
2626

27-
public protocol DeclBuildable: SyntaxBuildable, DeclListBuildable {
27+
public protocol DeclBuildable: SyntaxBuildable, DeclListBuildable, ExpressibleAsMemberDeclListItem, ExpressibleAsCodeBlockItem {
2828
/// Builds a `DeclSyntax`.
2929
/// - Parameter format: The `Format` to use.
3030
/// - Parameter leadingTrivia: Replaces the the last leading trivia if not nil.
@@ -62,6 +62,18 @@ extension DeclBuildable {
6262
}
6363
}
6464

65+
extension DeclBuildable {
66+
public func createMemberDeclListItem() -> MemberDeclListItem {
67+
MemberDeclListItem(decl: self)
68+
}
69+
}
70+
71+
extension DeclBuildable {
72+
public func createCodeBlockItem() -> CodeBlockItem {
73+
CodeBlockItem(item: self)
74+
}
75+
}
76+
6577
public protocol ExprListBuildable: SyntaxListBuildable {
6678
/// Builds list of `ExprSyntax`s.
6779
/// - Parameter format: The `Format` to use.

0 commit comments

Comments
 (0)