Skip to content

Commit 1b297c2

Browse files
committed
Move syntax nodes from gyb to code gen
1 parent db2d7ce commit 1b297c2

24 files changed

+18301
-14753
lines changed

CodeGeneration/Sources/SyntaxSupport/Child.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,22 @@ public enum ChildKind {
4343
return false
4444
}
4545
}
46+
47+
public var isNodeChoicesEmpty: Bool {
48+
if case .nodeChoices(let nodeChoices) = self {
49+
return nodeChoices.isEmpty
50+
} else {
51+
return true
52+
}
53+
}
4654
}
4755

4856
/// A child of a node, that may be declared optional or a token with a
4957
/// restricted subset of acceptable kinds or texts.
5058
public class Child {
5159
public let name: String
5260
public let kind: ChildKind
61+
public let nameForDiagnostics: String?
5362
public let description: String?
5463
public let forceClassification: Bool
5564
public let isIndented: Bool
@@ -117,6 +126,25 @@ public class Child {
117126
syntaxKind == "UnexpectedNodes"
118127
}
119128

129+
/// Returns `true` if this child's type is one of the base syntax kinds and
130+
/// it has no node choices.
131+
public var hasBaseType: Bool {
132+
switch kind {
133+
case .nodeChoices(let choices):
134+
return choices.isEmpty && SYNTAX_BASE_KINDS.contains(syntaxKind)
135+
case .node,
136+
.collection,
137+
.token:
138+
return SYNTAX_BASE_KINDS.contains(syntaxKind)
139+
}
140+
}
141+
142+
/// Returns `true` if this child's type is one of the base syntax kinds and
143+
/// it's optional.
144+
public var hasOptionalBaseType: Bool {
145+
return hasBaseType && isOptional
146+
}
147+
120148
/// If a classification is passed, it specifies the color identifiers in
121149
/// that subtree should inherit for syntax coloring. Must be a member of
122150
/// SyntaxClassification in SyntaxClassifier.h.gyb
@@ -125,6 +153,7 @@ public class Child {
125153
init(
126154
name: String,
127155
kind: ChildKind,
156+
nameForDiagnostics: String? = nil,
128157
description: String? = nil,
129158
isOptional: Bool = false,
130159
classification: String? = nil,
@@ -134,6 +163,7 @@ public class Child {
134163
) {
135164
self.name = name
136165
self.kind = kind
166+
self.nameForDiagnostics = nameForDiagnostics
137167
self.description = description
138168
self.classification = classificationByName(classification)
139169
self.forceClassification = forceClassification

CodeGeneration/Sources/SyntaxSupport/Node.swift

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,32 +33,32 @@ public class Node {
3333
public let elementsSeparatedByNewline: Bool
3434
public let collectionElement: String
3535

36-
/// Returns `True` if this node declares one of the base syntax kinds.
36+
/// Returns `true` if this node declares one of the base syntax kinds.
3737
public var isBase: Bool {
3838
return SYNTAX_BASE_KINDS.contains(syntaxKind)
3939
}
4040

41-
/// Returns `True` if this node is a subclass of SyntaxCollection.
41+
/// Returns `true` if this node is a subclass of SyntaxCollection.
4242
public var isSyntaxCollection: Bool {
4343
return baseKind == "SyntaxCollection"
4444
}
4545

46-
/// Returns `True` if this node should have a `validate` method associated.
46+
/// Returns `true` if this node should have a `validate` method associated.
4747
public var requiresValidation: Bool {
4848
return isBuildable
4949
}
5050

51-
/// Returns `True` if this node is an `Unknown` syntax subclass.
51+
/// Returns `true` if this node is an `Unknown` syntax subclass.
5252
public var isUnknown: Bool {
5353
return syntaxKind.contains("Unknown")
5454
}
5555

56-
/// Returns `True` if this node is an `Unknown` syntax subclass.
56+
/// Returns `true` if this node is an `Unknown` syntax subclass.
5757
public var isMissing: Bool {
5858
return syntaxKind.contains("Missing")
5959
}
6060

61-
/// Returns `True` if this node should have a builder associated.
61+
/// Returns `true` if this node should have a builder associated.
6262
public var isBuildable: Bool {
6363
return !isBase && !isUnknown && !isMissing && !isSyntaxCollection
6464
}
@@ -77,6 +77,16 @@ public class Node {
7777
return !isBase
7878
}
7979

80+
public var hasOptionalBaseTypeChild: Bool {
81+
return children.contains { child in
82+
if child.hasOptionalBaseType {
83+
return true
84+
} else {
85+
return false
86+
}
87+
}
88+
}
89+
8090
init(
8191
name: String,
8292
nameForDiagnostics: String?,
@@ -102,7 +112,7 @@ public class Node {
102112

103113
if kind == "SyntaxCollection" {
104114
self.children = children
105-
} else {
115+
} else if children.count > 0 {
106116
// Add implicitly generated UnexpectedNodes children between
107117
// any two defined children
108118
self.children =
@@ -130,6 +140,10 @@ public class Node {
130140
isOptional: true
131141
)
132142
] : [])
143+
} else {
144+
self.children = [
145+
Child(name: "Unexpected", kind: .collection(kind: "UnexpectedNodes", collectionElementName: "Unexpected"), isOptional: true)
146+
]
133147
}
134148

135149
self.nonUnexpectedChildren = children.filter { !$0.isUnexpectedNodes }

CodeGeneration/Sources/SyntaxSupport/gyb_generated/AttributeNodes.swift

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public let ATTRIBUTE_NODES: [Node] = [
2424
description: "The `@` sign."),
2525
Child(name: "AttributeName",
2626
kind: .node(kind: "Type"),
27+
nameForDiagnostics: "name",
2728
description: "The name of the attribute.",
2829
classification: "Attribute"),
2930
Child(name: "LeftParen",
@@ -104,6 +105,7 @@ public let ATTRIBUTE_NODES: [Node] = [
104105
children: [
105106
Child(name: "Label",
106107
kind: .token(choices: [.keyword(text: "availability")]),
108+
nameForDiagnostics: "label",
107109
description: "The label of the argument"),
108110
Child(name: "Colon",
109111
kind: .token(choices: [.token(tokenKind: "ColonToken")]),
@@ -124,12 +126,14 @@ public let ATTRIBUTE_NODES: [Node] = [
124126
children: [
125127
Child(name: "Label",
126128
kind: .token(choices: [.token(tokenKind: "IdentifierToken"), .keyword(text: "available"), .keyword(text: "exported"), .keyword(text: "kind"), .keyword(text: "spi"), .keyword(text: "spiModule")]),
129+
nameForDiagnostics: "label",
127130
description: "The label of the argument"),
128131
Child(name: "Colon",
129132
kind: .token(choices: [.token(tokenKind: "ColonToken")]),
130133
description: "The colon separating the label and the value"),
131134
Child(name: "Value",
132135
kind: .node(kind: "Token"),
136+
nameForDiagnostics: "value",
133137
description: "The value for this argument"),
134138
Child(name: "TrailingComma",
135139
kind: .token(choices: [.token(tokenKind: "CommaToken")]),
@@ -147,12 +151,14 @@ public let ATTRIBUTE_NODES: [Node] = [
147151
children: [
148152
Child(name: "Label",
149153
kind: .token(choices: [.keyword(text: "target")]),
154+
nameForDiagnostics: "label",
150155
description: "The label of the argument"),
151156
Child(name: "Colon",
152157
kind: .token(choices: [.token(tokenKind: "ColonToken")]),
153158
description: "The colon separating the label and the value"),
154159
Child(name: "Declname",
155160
kind: .node(kind: "DeclName"),
161+
nameForDiagnostics: "declaration name",
156162
description: "The value for this argument"),
157163
Child(name: "TrailingComma",
158164
kind: .token(choices: [.token(tokenKind: "CommaToken")]),
@@ -166,9 +172,11 @@ public let ATTRIBUTE_NODES: [Node] = [
166172
children: [
167173
Child(name: "DeclBaseName",
168174
kind: .token(choices: [.token(tokenKind: "IdentifierToken"), .token(tokenKind: "PrefixOperatorToken"), .keyword(text: "init")]),
175+
nameForDiagnostics: "base name",
169176
description: "The base name of the protocol's requirement."),
170177
Child(name: "DeclNameArguments",
171178
kind: .node(kind: "DeclNameArguments"),
179+
nameForDiagnostics: "arguments",
172180
description: "The argument labels of the protocol's requirement if it is a function requirement.",
173181
isOptional: true)
174182
]),
@@ -180,15 +188,18 @@ public let ATTRIBUTE_NODES: [Node] = [
180188
children: [
181189
Child(name: "Type",
182190
kind: .node(kind: "Type"),
191+
nameForDiagnostics: "type",
183192
description: "The type for which the method with this attribute implements a requirement."),
184193
Child(name: "Comma",
185194
kind: .token(choices: [.token(tokenKind: "CommaToken")]),
186195
description: "The comma separating the type and method name"),
187196
Child(name: "DeclBaseName",
188197
kind: .node(kind: "Token"),
198+
nameForDiagnostics: "declaration base name",
189199
description: "The base name of the protocol's requirement."),
190200
Child(name: "DeclNameArguments",
191201
kind: .node(kind: "DeclNameArguments"),
202+
nameForDiagnostics: "declaration name arguments",
192203
description: "The argument labels of the protocol's requirement if it is a function requirement.",
193204
isOptional: true)
194205
]),
@@ -200,6 +211,7 @@ public let ATTRIBUTE_NODES: [Node] = [
200211
children: [
201212
Child(name: "Name",
202213
kind: .token(choices: [.token(tokenKind: "IdentifierToken")]),
214+
nameForDiagnostics: "name",
203215
isOptional: true),
204216
Child(name: "Colon",
205217
kind: .token(choices: [.token(tokenKind: "ColonToken")]),
@@ -252,7 +264,8 @@ public let ATTRIBUTE_NODES: [Node] = [
252264
kind: .node(kind: "DifferentiabilityParam")),
253265
Child(name: "ParameterList",
254266
kind: .node(kind: "DifferentiabilityParams"))
255-
]))
267+
]),
268+
nameForDiagnostics: "parameters")
256269
]),
257270

258271
Node(name: "DifferentiabilityParams",
@@ -326,16 +339,19 @@ public let ATTRIBUTE_NODES: [Node] = [
326339
children: [
327340
Child(name: "BaseType",
328341
kind: .node(kind: "Type"),
342+
nameForDiagnostics: "base type",
329343
description: "The base type of the qualified name, optionally specified.",
330344
isOptional: true),
331345
Child(name: "Dot",
332346
kind: .token(choices: [.token(tokenKind: "PeriodToken")]),
333347
isOptional: true),
334348
Child(name: "Name",
335349
kind: .token(choices: [.token(tokenKind: "IdentifierToken"), .token(tokenKind: "BinaryOperatorToken"), .token(tokenKind: "PrefixOperatorToken"), .token(tokenKind: "PostfixOperatorToken")]),
350+
nameForDiagnostics: "base name",
336351
description: "The base name of the referenced function."),
337352
Child(name: "Arguments",
338353
kind: .node(kind: "DeclNameArguments"),
354+
nameForDiagnostics: "arguments",
339355
description: "The argument labels of the referenced function, optionally specified.",
340356
isOptional: true)
341357
]),
@@ -510,7 +526,8 @@ public let ATTRIBUTE_NODES: [Node] = [
510526
],
511527
children: [
512528
Child(name: "Label",
513-
kind: .token(choices: [.keyword(text: "visibility"), .keyword(text: "metadata")])),
529+
kind: .token(choices: [.keyword(text: "visibility"), .keyword(text: "metadata")]),
530+
nameForDiagnostics: "label"),
514531
Child(name: "Colon",
515532
kind: .token(choices: [.token(tokenKind: "ColonToken")])),
516533
Child(name: "Value",

CodeGeneration/Sources/SyntaxSupport/gyb_generated/AvailabilityNodes.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public let AVAILABILITY_NODES: [Node] = [
4646
children: [
4747
Child(name: "Label",
4848
kind: .token(choices: [.keyword(text: "message"), .keyword(text: "renamed"), .keyword(text: "introduced"), .keyword(text: "obsoleted"), .keyword(text: "deprecated")]),
49+
nameForDiagnostics: "label",
4950
description: "The label of the argument"),
5051
Child(name: "Colon",
5152
kind: .token(choices: [.token(tokenKind: "ColonToken")]),
@@ -57,6 +58,7 @@ public let AVAILABILITY_NODES: [Node] = [
5758
Child(name: "Version",
5859
kind: .node(kind: "VersionTuple"))
5960
]),
61+
nameForDiagnostics: "value",
6062
description: "The value of this labeled argument")
6163
]),
6264

@@ -67,10 +69,12 @@ public let AVAILABILITY_NODES: [Node] = [
6769
children: [
6870
Child(name: "Platform",
6971
kind: .token(choices: [.token(tokenKind: "IdentifierToken")]),
72+
nameForDiagnostics: "platform",
7073
description: "The name of the OS on which the availability should be restricted or 'swift' if the availability should be restricted based on a Swift version.",
7174
classification: "Keyword"),
7275
Child(name: "Version",
7376
kind: .node(kind: "VersionTuple"),
77+
nameForDiagnostics: "version",
7478
isOptional: true)
7579
]),
7680

CodeGeneration/Sources/SyntaxSupport/gyb_generated/CommonNodes.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ public let COMMON_NODES: [Node] = [
113113
kind: .token(choices: [.token(tokenKind: "LeftBraceToken")])),
114114
Child(name: "Statements",
115115
kind: .collection(kind: "CodeBlockItemList", collectionElementName: "Statement"),
116+
nameForDiagnostics: "statements",
116117
isIndented: true),
117118
Child(name: "RightBrace",
118119
kind: .token(choices: [.token(tokenKind: "RightBraceToken")]),

0 commit comments

Comments
 (0)