Skip to content

Commit e7e7d67

Browse files
committed
Add Buildable gyb
1 parent 4cb05fc commit e7e7d67

16 files changed

+7107
-378
lines changed
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
%{
2+
from gyb_syntax_support import *
3+
from gyb_syntax_support.kinds import lowercase_first_word, kind_to_type
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+
SyntaxFactory.make${node.syntax_kind}(
93+
% child_params = []
94+
% for child in node.children:
95+
% param_name = child.swift_name
96+
% if not child.is_token():
97+
% end
98+
% if child.syntax_kind in SYNTAX_BASE_KINDS or not child.is_token():
99+
% if child.is_optional:
100+
% param_name = param_name + "?"
101+
% end
102+
% if child.syntax_kind in SYNTAX_BASE_KINDS or not child.is_token():
103+
% param_name = param_name + ".build" + child.syntax_kind + "(format: format, leadingTrivia: leadingTrivia)"
104+
% end
105+
% end
106+
% child_params.append("%s: %s" % (child.swift_name, param_name))
107+
% end
108+
${',\n '.join(child_params)}
109+
).withLeadingTrivia(leadingTrivia)
110+
}
111+
112+
public func build${node.base_kind}(format: Format, leadingTrivia: Trivia) -> ${return_type} {
113+
let ${node.swift_syntax_kind} = build${node.syntax_kind}(format: format, leadingTrivia: leadingTrivia)
114+
return ${return_type}(${node.swift_syntax_kind})
115+
}
116+
}
117+
118+
% elif node.is_syntax_collection():
119+
// MARK: - Syntax collection
120+
public struct ${node.syntax_kind}: SyntaxBuildable {
121+
% build = True
122+
// ${node.collection_element}
123+
% if node.collection_element.endswith("Token"):
124+
% element_type = 'TokenSyntax'
125+
% build = False
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 build:
141+
SyntaxFactory.make${node.syntax_kind}(elements.map { $0.build${node.collection_element}(format: format, leadingTrivia: leadingTrivia) })
142+
% else:
143+
SyntaxFactory.make${node.syntax_kind}(elements)
144+
% end
145+
.withLeadingTrivia(leadingTrivia)
146+
}
147+
148+
public func buildSyntax(format: Format, leadingTrivia: Trivia) -> Syntax {
149+
let ${node.swift_syntax_kind} = build${node.syntax_kind}(format: format, leadingTrivia: leadingTrivia)
150+
return Syntax(${node.swift_syntax_kind})
151+
}
152+
}
153+
154+
% end
155+
% end

Sources/SwiftSyntaxBuilder/DeclBuildables.swift

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

0 commit comments

Comments
 (0)