Skip to content

Commit 2b64f5b

Browse files
committed
Add Buildable gyb
1 parent 4cb05fc commit 2b64f5b

13 files changed

+7038
-504
lines changed
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
%{
2+
from gyb_syntax_support import *
3+
from gyb_syntax_support.kinds import lowercase_first_word
4+
from gyb_syntax_support.kinds import SYNTAX_BASE_KINDS
5+
# -*- mode: Swift -*-
6+
# Ignore the following admonition it applies to the resulting .swift file only
7+
}%
8+
//// Automatically Generated From DeclBuildables.swift.gyb.
9+
//===----------------------------------------------------------------------===//
10+
//
11+
// This source file is part of the Swift.org open source project
12+
//
13+
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
14+
// Licensed under Apache License v2.0 with Runtime Library Exception
15+
//
16+
// See https://swift.org/LICENSE.txt for license information
17+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
18+
//
19+
//===----------------------------------------------------------------------===//
20+
21+
import SwiftSyntax
22+
23+
// MARK: Protocols
24+
25+
% for kind in SYNTAX_BASE_KINDS:
26+
% if kind != 'SyntaxCollection':
27+
% build_kind = 'Syntax' if kind == 'Syntax' else kind + 'Syntax'
28+
% if kind == 'Syntax':
29+
public protocol ${kind}ListBuildable {
30+
% else:
31+
public protocol ${kind}ListBuildable: SyntaxListBuildable {
32+
% end
33+
func build${kind}List(format: Format, leadingTrivia: Trivia) -> [${build_kind}]
34+
}
35+
36+
% if kind == 'Syntax':
37+
public protocol ${kind}Buildable: ${kind}ListBuildable {
38+
% else:
39+
public protocol ${kind}Buildable: SyntaxBuildable, ${kind}ListBuildable {
40+
% end
41+
func build${kind}(format: Format, leadingTrivia: Trivia) -> ${build_kind}
42+
}
43+
44+
extension ${kind}Buildable {
45+
public func buildSyntax(format: Format, leadingTrivia: Trivia) -> Syntax {
46+
Syntax(build${kind}(format: format, leadingTrivia: leadingTrivia))
47+
}
48+
49+
public func build${kind}List(format: Format, leadingTrivia: Trivia) -> [${build_kind}] {
50+
[build${kind}(format: format, leadingTrivia: leadingTrivia)]
51+
}
52+
}
53+
54+
% end
55+
% end
56+
57+
// MARK: - Buildables
58+
59+
% for node in SYNTAX_NODES:
60+
% if node.is_buildable():
61+
% return_type = 'Syntax'
62+
% if node.base_kind != 'Syntax':
63+
% return_type = node.base_kind + 'Syntax'
64+
% end
65+
public struct ${node.syntax_kind}: ${node.base_kind}Buildable {
66+
% child_params = []
67+
% for child in node.children:
68+
% param_type = child.type_name
69+
% if child.syntax_kind in SYNTAX_BASE_KINDS:
70+
% param_type = child.syntax_kind + 'Buildable'
71+
% elif not child.is_token():
72+
% param_type = child.syntax_kind
73+
% end
74+
% if child.is_optional:
75+
% param_type = param_type + "?"
76+
% end
77+
% child_params.append("%s: %s" % (child.swift_name, param_type))
78+
% end
79+
% for child_param in child_params:
80+
let ${child_param}
81+
% end
82+
83+
public init(
84+
${',\n '.join(child_params)}
85+
) {
86+
% for child in node.children:
87+
self.${child.swift_name} = ${child.swift_name}
88+
% end
89+
}
90+
91+
func build${node.syntax_kind}(format: Format, leadingTrivia: Trivia) -> ${node.syntax_kind}Syntax {
92+
% child_params = []
93+
% for child in node.children:
94+
% param_name = child.swift_name
95+
% if child.is_token() and child.requires_leading_newline:
96+
% param_name = param_name + '.withLeadingTrivia(.newlines(1) + format._makeIndent())'
97+
% elif child.syntax_kind in SYNTAX_BASE_KINDS or not child.is_token():
98+
% if child.is_optional:
99+
% param_name = param_name + "?"
100+
% end
101+
% format = 'format'
102+
% if child.is_indented:
103+
% format += '._indented()'
104+
% end
105+
% param_name = param_name + ".build" + child.syntax_kind + "(format: " + format + ", leadingTrivia: .zero)"
106+
% end
107+
% child_params.append("%s: %s" % (child.swift_name, param_name))
108+
% end
109+
SyntaxFactory.make${node.syntax_kind}(
110+
${',\n '.join(child_params)}
111+
).withLeadingTrivia(leadingTrivia)
112+
}
113+
114+
public func build${node.base_kind}(format: Format, leadingTrivia: Trivia) -> ${return_type} {
115+
let ${node.swift_syntax_kind} = build${node.syntax_kind}(format: format, leadingTrivia: leadingTrivia)
116+
return ${return_type}(${node.swift_syntax_kind})
117+
}
118+
}
119+
120+
% elif node.is_syntax_collection():
121+
// MARK: - Syntax collection
122+
public struct ${node.syntax_kind}: SyntaxBuildable {
123+
// ${node.collection_element}
124+
% if node.collection_element.endswith("Token"):
125+
% element_type = 'TokenSyntax'
126+
% elif node.collection_element == 'Syntax':
127+
% element_type = 'SyntaxBuildable'
128+
% elif node.collection_element in SYNTAX_BASE_KINDS:
129+
% element_type = node.collection_element + 'Buildable'
130+
% else:
131+
% element_type = node.collection_element
132+
% end
133+
let elements: [${element_type}]
134+
135+
public init(_ elements: [${element_type}]) {
136+
self.elements = elements
137+
}
138+
139+
public func build${node.syntax_kind}(format: Format, leadingTrivia: Trivia) -> ${node.syntax_kind}Syntax {
140+
% if not node.is_token():
141+
% leading_trivia = '.zero'
142+
% if node.elements_separated_by_newline:
143+
% leading_trivia = '.newlines(1) + format._makeIndent()'
144+
% end
145+
let collection = SyntaxFactory.make${node.syntax_kind}(elements.map {
146+
$0.build${node.collection_element}(format: format, leadingTrivia: ${leading_trivia})
147+
})
148+
% else:
149+
let collection = SyntaxFactory.make${node.syntax_kind}(elements)
150+
% end
151+
return collection
152+
}
153+
154+
public func buildSyntax(format: Format, leadingTrivia: Trivia) -> Syntax {
155+
let ${node.swift_syntax_kind} = build${node.syntax_kind}(format: format, leadingTrivia: leadingTrivia)
156+
return Syntax(${node.swift_syntax_kind}).withLeadingTrivia(leadingTrivia)
157+
}
158+
}
159+
160+
% end
161+
% end

Sources/SwiftSyntaxBuilder/DeclBuildables.swift

Lines changed: 0 additions & 187 deletions
This file was deleted.

0 commit comments

Comments
 (0)