Skip to content

Commit 6e0d3e9

Browse files
committed
Generate @resultbuilder with SwiftSyntaxBuilder
1 parent bd98242 commit 6e0d3e9

18 files changed

+3967
-3624
lines changed

Package.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ let package = Package(
8080
"BuildableCollectionNodes.swift.gyb",
8181
"BuildableNodes.swift.gyb",
8282
"ResultBuilders.swift.gyb",
83+
"TokenSyntax.swift.gyb",
8384
]
8485
),
8586
.target(
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
//// Automatically Generated From Kinds.swift.gyb.
2-
//// Do Not Edit Directly!
31
//===----------------------------------------------------------------------===//
42
//
53
// This source file is part of the Swift.org open source project
@@ -12,12 +10,10 @@
1210
//
1311
//===----------------------------------------------------------------------===//
1412

15-
let SyntaxBaseKinds: [String] = [
16-
"Decl",
17-
"Expr",
18-
"Pattern",
19-
"Stmt",
20-
"Syntax",
21-
"SyntaxCollection",
22-
"Type"
23-
]
13+
import SwiftSyntax
14+
15+
extension ArrayElement: ExpressibleAsArrayExpr {
16+
public func createArrayExpr() -> ArrayExpr {
17+
ArrayExpr(elements: ArrayElementList([self]))
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
%{
2-
from gyb_syntax_support import *
3-
from gyb_syntax_support.kinds import SYNTAX_BASE_KINDS
4-
# -*- mode: Swift -*-
5-
# Ignore the following admonition it applies to the resulting .swift file only
6-
}%
7-
//// Automatically Generated From Kinds.swift.gyb.
8-
//// Do Not Edit Directly!
91
//===----------------------------------------------------------------------===//
102
//
113
// This source file is part of the Swift.org open source project
@@ -18,7 +10,10 @@
1810
//
1911
//===----------------------------------------------------------------------===//
2012

21-
% quotedSyntaxBaseKinds = map(lambda x: '"%s"' % x, SYNTAX_BASE_KINDS)
22-
let SyntaxBaseKinds: [String] = [
23-
${",\n ".join(quotedSyntaxBaseKinds)}
24-
]
13+
import SwiftSyntax
14+
15+
extension ArrayElementList: ExpressibleAsArrayExpr {
16+
public func createArrayExpr() -> ArrayExpr {
17+
ArrayExpr(elements: self)
18+
}
19+
}

Sources/SwiftSyntaxBuilder/ResultBuilders.swift.gyb

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

Sources/SwiftSyntaxBuilder/Trivia+Extension.swift

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,36 +21,42 @@ extension Trivia {
2121
func addSpacingAfterNewlinesIfNeeded() -> Trivia {
2222
var updatedTriviaPieces = self.pieces
2323
if updatedTriviaPieces.count > 2,
24-
isNewline(trivia: updatedTriviaPieces[0]),
25-
let toInset = self.makeIndentation(trivia: updatedTriviaPieces[1]) {
26-
27-
for i in (2..<updatedTriviaPieces.count).reversed() where isNewline(trivia: updatedTriviaPieces[i]) {
28-
updatedTriviaPieces.insert(toInset, at: i + 1)
29-
}
30-
}
24+
updatedTriviaPieces[0].isNewline,
25+
let toInset = makeIndentation(trivia: updatedTriviaPieces[1]) {
3126

32-
return Trivia(pieces: updatedTriviaPieces)
27+
for i in (2..<updatedTriviaPieces.count).reversed() where updatedTriviaPieces[i].isNewline {
28+
updatedTriviaPieces.insert(toInset, at: i + 1)
29+
}
3330
}
3431

35-
private func isNewline(trivia: TriviaPiece) -> Bool {
36-
switch trivia {
37-
case .newlines,
38-
.carriageReturns,
39-
.carriageReturnLineFeeds:
40-
return true
41-
default:
42-
return false
43-
}
32+
return Trivia(pieces: updatedTriviaPieces)
33+
}
34+
35+
private func makeIndentation(trivia: TriviaPiece) -> TriviaPiece? {
36+
switch trivia {
37+
case .spaces(let spaces):
38+
return .spaces(spaces)
39+
case .tabs(let tabs):
40+
return .tabs(tabs)
41+
default:
42+
return nil
4443
}
45-
46-
private func makeIndentation(trivia: TriviaPiece) -> TriviaPiece? {
47-
switch trivia {
48-
case .spaces(let spaces):
49-
return .spaces(spaces)
50-
case .tabs(let tabs):
51-
return .tabs(tabs)
52-
default:
53-
return nil
54-
}
44+
}
45+
}
46+
47+
extension TriviaPiece {
48+
/// Returns true, if the ``TriviaPiece`` is a
49+
/// - ``TriviaPiece.newlines``
50+
/// - ``TriviaPiece.carriageReturns``
51+
/// - ``TriviaPiece.carriageReturnLineFeeds``
52+
var isNewline: Bool {
53+
switch self {
54+
case .newlines,
55+
.carriageReturns,
56+
.carriageReturnLineFeeds:
57+
return true
58+
default:
59+
return false
5560
}
61+
}
5662
}

0 commit comments

Comments
 (0)