Skip to content

Commit 6d45d60

Browse files
committed
Generate SwiftSyntaxBuilder @resultBuilders with SwiftSyntaxBuilder
1 parent ead746b commit 6d45d60

36 files changed

+5446
-193
lines changed

Package.swift

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,22 @@ 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+
"Classification.swift.gyb",
108+
"CommonNodes.swift.gyb",
109+
"DeclNodes.swift.gyb",
110+
"ExprNodes.swift.gyb",
111+
"GenericNodes.swift.gyb",
112+
"Kinds.swift.gyb",
113+
"NodeSerializationCodes.swift.gyb",
114+
"PatternNodes.swift.gyb",
115+
"StmtNodes.swift.gyb",
116+
"Tokens.swift.gyb",
117+
"Traits.swift.gyb",
118+
"Trivia.swift.gyb",
119+
"TypeNodes.swift.gyb"
105120
]
106121
),
107122
.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: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
/// A child of a node, that may be declared optional or a token with a
14+
/// restricted subset of acceptable kinds or texts.
15+
class Child {
16+
let name: String
17+
let syntaxKind: String
18+
let description: String?
19+
let collectionElementName: String?
20+
let forceClassification: Bool
21+
let isIndented: Bool
22+
let requiresLeadingNewline: Bool
23+
let isOptional: Bool
24+
let textChoices: [String]
25+
let nodeChoices: [Child]
26+
let classification: SyntaxClassification?
27+
/// A restricted set of token kinds that will be accepted for this child.
28+
let tokenChoices: [Token]
29+
30+
var swiftName: String {
31+
return lowercaseFirstWord(name: name)
32+
}
33+
34+
var swiftSyntaxKind: String {
35+
return lowercaseFirstWord(name: syntaxKind)
36+
}
37+
38+
var typeName: String {
39+
return kindToType(kind: syntaxKind)
40+
}
41+
42+
/// 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.
43+
var tokenKind: String? {
44+
if syntaxKind.hasSuffix("Token") {
45+
return syntaxKind
46+
} else {
47+
return nil
48+
}
49+
}
50+
51+
/// Returns `true` if this child has a token kind.
52+
var isToken: Bool {
53+
return tokenKind != nil
54+
}
55+
56+
var token: Token? {
57+
guard let tokenKind = tokenKind else { return nil }
58+
return SYNTAX_TOKEN_MAP[tokenKind]
59+
}
60+
61+
/// Returns the first choice from the `tokenChoices` if there are any, otherwise returns `nil`.
62+
var mainToken: Token? {
63+
return tokenChoices.first
64+
}
65+
66+
/// If a classification is passed, it specifies the color identifiers in
67+
/// that subtree should inherit for syntax coloring. Must be a member of
68+
/// SyntaxClassification in SyntaxClassifier.h.gyb
69+
/// If force_classification is also set to true, all child nodes (not only
70+
/// identifiers) inherit the syntax classification.
71+
init(name: String,
72+
kind: String,
73+
description: String? = nil,
74+
isOptional: Bool = false,
75+
tokenChoices: [String] = [],
76+
textChoices: [String] = [],
77+
nodeChoices: [Child] = [],
78+
collectionElementName: String? = nil,
79+
classification: String? = nil,
80+
forceClassification: Bool = false,
81+
isIndented: Bool = false,
82+
requiresLeadingNewline: Bool = false) {
83+
self.name = name
84+
self.syntaxKind = kind
85+
self.description = description
86+
self.collectionElementName = collectionElementName
87+
self.classification = classificationByName(classification)
88+
self.forceClassification = forceClassification
89+
self.isIndented = isIndented
90+
self.requiresLeadingNewline = requiresLeadingNewline
91+
self.isOptional = isOptional
92+
93+
var mappedTokenChoices = [Token]()
94+
95+
if syntaxKind.hasSuffix("Token"), let token = SYNTAX_TOKEN_MAP[syntaxKind] {
96+
mappedTokenChoices.append(token)
97+
}
98+
99+
mappedTokenChoices.append(contentsOf: tokenChoices.compactMap { SYNTAX_TOKEN_MAP[$0] })
100+
self.tokenChoices = mappedTokenChoices
101+
102+
// A list of valid text for tokens, if specified.
103+
// This will force validation logic to check the text passed into the
104+
// token against the choices.
105+
self.textChoices = textChoices
106+
107+
// A list of valid choices for a child
108+
self.nodeChoices = nodeChoices
109+
110+
// Check the choices are either empty or multiple
111+
assert(nodeChoices.count != 1)
112+
113+
// Check node choices are well-formed
114+
for choice in self.nodeChoices {
115+
assert(!choice.isOptional, "node choice \(choice.name) cannot be optional")
116+
assert(choice.nodeChoices.isEmpty, "node choice \(choice.name) cannot have further choices")
117+
}
118+
}
119+
}
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+
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+
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+
]
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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!
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+
% quotedSyntaxBaseKinds = map(lambda x: '"%s"' % x, SYNTAX_BASE_KINDS)
22+
let SyntaxBaseKinds: [String] = [
23+
${",\n ".join(quotedSyntaxBaseKinds)}
24+
]

0 commit comments

Comments
 (0)