Skip to content

Commit 272b510

Browse files
committed
Generate SwiftSyntaxBuilder @resultBuilders with SwiftSyntaxBuilder
1 parent eec37f3 commit 272b510

37 files changed

+4739
-1
lines changed

Package.swift

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,24 @@ let package = Package(
101101
name: "SwiftSyntaxBuilderGeneration",
102102
dependencies: ["SwiftSyntaxBuilder"],
103103
exclude: [
104-
"Tokens.swift.gyb"
104+
"gyb_helpers",
105+
"AttributeNodes.swift.gyb",
106+
"AvailabilityNodes.swift.gyb",
107+
"Child.swift.gyb",
108+
"Classification.swift.gyb",
109+
"CommonNodes.swift.gyb",
110+
"DeclNodes.swift.gyb",
111+
"ExprNodes.swift.gyb",
112+
"GenericNodes.swift.gyb",
113+
"Kinds.swift.gyb",
114+
"Node.swift.gyb",
115+
"NodeSerializationCodes.swift.gyb",
116+
"PatternNodes.swift.gyb",
117+
"StmtNodes.swift.gyb",
118+
"Tokens.swift.gyb",
119+
"Traits.swift.gyb",
120+
"Trivia.swift.gyb",
121+
"TypeNodes.swift.gyb"
105122
]
106123
),
107124
.testTarget(
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
%{
2+
from gyb_syntax_support import *
3+
from gyb_helpers import make_swift_child, make_swift_node
4+
# -*- mode: Swift -*-
5+
# Ignore the following admonition it applies to the resulting .swift file only
6+
}%
7+
//// Automatically Generated From AttributeNodes.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 - 2022 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+
let ATTRIBUTE_NODES: [Node] = [
22+
% for node in ATTRIBUTE_NODES:
23+
${make_swift_node(node)},
24+
25+
% end
26+
]
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
%{
2+
from gyb_syntax_support import *
3+
from gyb_helpers import make_swift_child, make_swift_node
4+
# -*- mode: Swift -*-
5+
# Ignore the following admonition it applies to the resulting .swift file only
6+
}%
7+
//// Automatically Generated From AvailabilityNodes.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 - 2022 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+
let AVAILABILITY_NODES: [Node] = [
22+
% for node in AVAILABILITY_NODES:
23+
${make_swift_node(node)},
24+
25+
% end
26+
]
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
%{
2+
from gyb_syntax_support import *
3+
from gyb_syntax_support.Token import *
4+
from gyb_syntax_support.kinds import lowercase_first_word
5+
# -*- mode: Swift -*-
6+
# Ignore the following admonition it applies to the resulting .swift file only
7+
}%
8+
//// Automatically Generated From Child.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 - 2022 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+
/// A child of a node, that may be declared optional or a token with a
23+
/// restricted subset of acceptable kinds or texts.
24+
public class Child {
25+
public let name: String
26+
public let syntaxKind: String
27+
public let description: String?
28+
public let collectionElementName: String?
29+
public let forceClassification: Bool
30+
public let isIndented: Bool
31+
public let requiresLeadingNewline: Bool
32+
public let isOptional: Bool
33+
public let textChoices: [String]
34+
public let nodeChoices: [Child]
35+
public let classification: SyntaxClassification?
36+
37+
public var swiftName: String {
38+
return lowercaseFirstWord(name: name)
39+
}
40+
41+
public var swiftSyntaxKind: String {
42+
return lowercaseFirstWord(name: syntaxKind)
43+
}
44+
45+
public var typeName: String {
46+
return kindToType(kind: syntaxKind)
47+
}
48+
49+
/// A restricted set of token kinds that will be accepted for this child.
50+
public var tokenChoices: [Token] {
51+
var result = [Token]()
52+
53+
if let token = token {
54+
result.append(token)
55+
}
56+
57+
return result
58+
59+
}
60+
61+
/// If the child ends with "token" in the kind, it's considered a token node. Grab the existing reference to that token from the global list.
62+
public var tokenKind: String? {
63+
if self.syntaxKind.hasSuffix("Token") {
64+
return syntaxKind
65+
} else {
66+
return nil
67+
}
68+
}
69+
70+
/// Returns `true` if this child has a token kind.
71+
public var isToken: Bool {
72+
return tokenKind != nil
73+
}
74+
75+
public var token: Token? {
76+
guard let tokenKind = tokenKind else { return nil }
77+
return SYNTAX_TOKEN_MAP[tokenKind]
78+
}
79+
80+
// def main_token(self):
81+
// """
82+
// Returns the first choice from the token_choices if there are any,
83+
// otherwise returns None.
84+
// """
85+
// if self.token_choices:
86+
// return self.token_choices[0]
87+
// return None
88+
89+
/// If a classification is passed, it specifies the color identifiers in
90+
/// that subtree should inherit for syntax coloring. Must be a member of
91+
/// SyntaxClassification in SyntaxClassifier.h.gyb
92+
/// If force_classification is also set to true, all child nodes (not only
93+
/// identifiers) inherit the syntax classification.
94+
public init(name: String,
95+
kind: String,
96+
description: String? = nil,
97+
isOptional: Bool = false,
98+
tokenChoices: [String]? = nil,
99+
textChoices: [String]? = nil,
100+
nodeChoices: [Child]? = nil,
101+
collectionElementName: String? = nil,
102+
classification: String? = nil,
103+
forceClassification: Bool = false,
104+
isIndented: Bool = false,
105+
requiresLeadingNewline: Bool = false) {
106+
self.name = name
107+
self.syntaxKind = kind
108+
self.description = description
109+
self.collectionElementName = collectionElementName
110+
self.classification = classificationByName(classification)
111+
self.forceClassification = forceClassification
112+
self.isIndented = isIndented
113+
self.requiresLeadingNewline = requiresLeadingNewline
114+
self.isOptional = isOptional
115+
116+
// for choice in token_choices or []:
117+
// token = SYNTAX_TOKEN_MAP[choice]
118+
// self.token_choices.append(token)
119+
120+
// A list of valid text for tokens, if specified.
121+
// This will force validation logic to check the text passed into the
122+
// token against the choices.
123+
self.textChoices = textChoices ?? []
124+
125+
// A list of valid choices for a child
126+
self.nodeChoices = nodeChoices ?? []
127+
128+
// Check the choices are either empty or multiple
129+
assert(nodeChoices?.count != 1)
130+
131+
// Check node choices are well-formed
132+
for choice in self.nodeChoices {
133+
assert(!choice.isOptional, "node choice \(choice.name) cannot be optional")
134+
assert(!choice.nodeChoices.isEmpty, "node choice \(choice.name) cannot have further choices")
135+
}
136+
}
137+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
%{
2+
from gyb_syntax_support import *
3+
from gyb_syntax_support.Token import *
4+
from gyb_syntax_support.kinds import lowercase_first_word
5+
# -*- mode: Swift -*-
6+
# Ignore the following admonition it applies to the resulting .swift file only
7+
}%
8+
//// Automatically Generated From Classification.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 - 2022 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+
/// Represents a classification a token can receive for syntax highlighting.
23+
public class SyntaxClassification {
24+
public let name: String
25+
public let description: String
26+
27+
public var swiftName: String {
28+
lowercaseFirstWord(name: name)
29+
}
30+
31+
public init(name: String, description: String) {
32+
self.name = name
33+
self.description = description
34+
}
35+
}
36+
37+
let SYNTAX_CLASSIFICATIONS: [SyntaxClassification] = [
38+
% for syntaxClassification in SYNTAX_CLASSIFICATIONS:
39+
SyntaxClassification(name: "${syntaxClassification.name}", description: "${syntaxClassification.description.strip()}"),
40+
% end
41+
]
42+
43+
public func classificationByName(_ name: String?) -> SyntaxClassification? {
44+
guard let name = name else { return nil }
45+
for classification in SYNTAX_CLASSIFICATIONS where classification.name == name {
46+
return classification
47+
}
48+
49+
fatalError("Unknown syntax classification '\(name)'")
50+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
%{
2+
from gyb_syntax_support import *
3+
from gyb_helpers import make_swift_child, make_swift_node
4+
# -*- mode: Swift -*-
5+
# Ignore the following admonition it applies to the resulting .swift file only
6+
}%
7+
//// Automatically Generated From CommonNodes.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 - 2022 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+
let COMMON_NODES: [Node] = [
22+
% for node in COMMON_NODES:
23+
${make_swift_node(node)},
24+
25+
% end
26+
]
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
%{
2+
from gyb_syntax_support import *
3+
from gyb_helpers import make_swift_child, make_swift_node
4+
# -*- mode: Swift -*-
5+
# Ignore the following admonition it applies to the resulting .swift file only
6+
}%
7+
//// Automatically Generated From DeclNodes.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 - 2022 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+
let DECL_NODES: [Node] = [
22+
% for node in DECL_NODES:
23+
${make_swift_node(node)},
24+
25+
% end
26+
]
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
%{
2+
from gyb_syntax_support import *
3+
from gyb_helpers import make_swift_child, make_swift_node
4+
# -*- mode: Swift -*-
5+
# Ignore the following admonition it applies to the resulting .swift file only
6+
}%
7+
//// Automatically Generated From ExprNodes.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 - 2022 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+
let EXPR_NODES: [Node] = [
22+
% for node in EXPR_NODES:
23+
${make_swift_node(node)},
24+
25+
% end
26+
]
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
%{
2+
from gyb_syntax_support import *
3+
from gyb_helpers import make_swift_child, make_swift_node
4+
# -*- mode: Swift -*-
5+
# Ignore the following admonition it applies to the resulting .swift file only
6+
}%
7+
//// Automatically Generated From GenericNodes.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 - 2022 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+
let GENERIC_NODES: [Node] = [
22+
% for node in GENERIC_NODES:
23+
${make_swift_node(node)},
24+
25+
% end
26+
]

0 commit comments

Comments
 (0)