Skip to content

Commit b8ca9e3

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

File tree

2 files changed

+96
-1
lines changed

2 files changed

+96
-1
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
%{
2+
from gyb_syntax_support import *
3+
from gyb_syntax_support.kinds import lowercase_first_word
4+
# -*- mode: Swift -*-
5+
# Ignore the following admonition it applies to the resulting .swift file only
6+
}%
7+
//// Automatically Generated From Tokens.swift.gyb.
8+
//// Do Not Edit Directly!
9+
//===----------------------------------------------------------------------===//
10+
//
11+
// This source file is part of the Swift.org open source project
12+
//
13+
// Copyright (c) 2014 - 2021 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+
public protocol SyntaxListBuildable {
26+
func buildSyntaxList(format: Format, leadingTrivia: Trivia) -> [Syntax]
27+
}
28+
29+
public protocol SyntaxBuildable: SyntaxListBuildable {
30+
func buildSyntax(format: Format, leadingTrivia: Trivia) -> Syntax
31+
}
32+
33+
extension SyntaxBuildable {
34+
public func buildSyntaxList(format: Format, leadingTrivia: Trivia) -> [Syntax] {
35+
[buildSyntax(format: format, leadingTrivia: leadingTrivia)]
36+
}
37+
}
38+
39+
// MARK: - Function Builder
40+
41+
@_functionBuilder
42+
public struct SyntaxListBuilder {
43+
public static func buildBlock(_ builders: SyntaxListBuildable...) -> SyntaxListBuildable {
44+
SyntaxList(builders: builders)
45+
}
46+
}
47+
48+
// MARK: - List
49+
50+
public struct SyntaxList: SyntaxListBuildable {
51+
let builders: [SyntaxListBuildable]
52+
53+
public func buildSyntaxList(format: Format, leadingTrivia: Trivia) -> [Syntax] {
54+
builders.flatMap {
55+
$0.buildSyntaxList(format: format, leadingTrivia: leadingTrivia)
56+
}
57+
}
58+
}
59+
60+
extension SyntaxList {
61+
public static let empty = SyntaxList(builders: [])
62+
}
63+
64+
// MARK: - Buildables
65+
66+
// MARK: Source File
67+
68+
public struct SourceFile: SyntaxBuildable {
69+
let builder: SyntaxListBuildable
70+
71+
public init(@SyntaxListBuilder makeBuilder: () -> SyntaxListBuildable) {
72+
self.builder = makeBuilder()
73+
}
74+
75+
public func buildSyntax(format: Format, leadingTrivia: Trivia) -> Syntax {
76+
let syntaxList = builder.buildSyntaxList(format: format, leadingTrivia: leadingTrivia)
77+
78+
let sourceFile = SourceFileSyntax {
79+
for (index, syntax) in syntaxList.enumerated() {
80+
let trivia: Trivia =
81+
index == syntaxList.startIndex
82+
? format._makeIndent()
83+
: .newlines(1) + format._makeIndent()
84+
85+
$0.addStatement(CodeBlockItemSyntax {
86+
$0.useItem(syntax)
87+
}.withLeadingTrivia(trivia))
88+
}
89+
}.withLeadingTrivia(leadingTrivia)
90+
91+
return Syntax(sourceFile)
92+
}
93+
}

Sources/SwiftSyntaxBuilder/SyntaxBuildables.swift renamed to Sources/SwiftSyntaxBuilder/gyb_generated/SyntaxBuildables.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
//// Automatically Generated From Tokens.swift.gyb.
2+
//// Do Not Edit Directly!
13
//===----------------------------------------------------------------------===//
24
//
35
// This source file is part of the Swift.org open source project
46
//
5-
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
7+
// Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors
68
// Licensed under Apache License v2.0 with Runtime Library Exception
79
//
810
// See https://swift.org/LICENSE.txt for license information

0 commit comments

Comments
 (0)