Skip to content

Commit f68d3c1

Browse files
authored
Merge pull request #578 from CodaFi/running-on-empty
Update for MissingDeclSyntax Gaining Attributes
2 parents 2ce3e70 + 698e17a commit f68d3c1

File tree

4 files changed

+197
-3
lines changed

4 files changed

+197
-3
lines changed

Sources/SwiftSyntax/gyb_generated/SyntaxFactory.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,27 @@ public enum SyntaxFactory {
9696
], length: .zero, presence: presence))
9797
return MissingSyntax(data)
9898
}
99+
public static func makeMissingDecl(_ garbageBeforeAttributes: GarbageNodesSyntax? = nil, attributes: AttributeListSyntax?, _ garbageBetweenAttributesAndModifiers: GarbageNodesSyntax? = nil, modifiers: ModifierListSyntax?) -> MissingDeclSyntax {
100+
let layout: [RawSyntax?] = [
101+
garbageBeforeAttributes?.raw,
102+
attributes?.raw,
103+
garbageBetweenAttributesAndModifiers?.raw,
104+
modifiers?.raw,
105+
]
106+
let raw = RawSyntax.createAndCalcLength(kind: SyntaxKind.missingDecl,
107+
layout: layout, presence: SourcePresence.present)
108+
let data = SyntaxData.forRoot(raw)
109+
return MissingDeclSyntax(data)
110+
}
99111

100112
@available(*, deprecated, message: "Use initializer on MissingDeclSyntax")
101113
public static func makeBlankMissingDecl(presence: SourcePresence = .missing) -> MissingDeclSyntax {
102114
let data = SyntaxData.forRoot(RawSyntax.create(kind: .missingDecl,
103115
layout: [
116+
nil,
117+
nil,
118+
nil,
119+
nil,
104120
], length: .zero, presence: presence))
105121
return MissingDeclSyntax(data)
106122
}

Sources/SwiftSyntax/gyb_generated/syntax_nodes/SyntaxDeclNodes.swift

Lines changed: 169 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ extension UnknownDeclSyntax: CustomReflectable {
6565
// MARK: - MissingDeclSyntax
6666

6767
public struct MissingDeclSyntax: DeclSyntaxProtocol, SyntaxHashable {
68+
enum Cursor: Int {
69+
case garbageBeforeAttributes
70+
case attributes
71+
case garbageBetweenAttributesAndModifiers
72+
case modifiers
73+
}
6874

6975
public let _syntaxNode: Syntax
7076

@@ -97,16 +103,178 @@ public struct MissingDeclSyntax: DeclSyntaxProtocol, SyntaxHashable {
97103
return Swift.type(of: self)
98104
}
99105

106+
public var garbageBeforeAttributes: GarbageNodesSyntax? {
107+
get {
108+
let childData = data.child(at: Cursor.garbageBeforeAttributes,
109+
parent: Syntax(self))
110+
if childData == nil { return nil }
111+
return GarbageNodesSyntax(childData!)
112+
}
113+
set(value) {
114+
self = withGarbageBeforeAttributes(value)
115+
}
116+
}
117+
118+
/// Returns a copy of the receiver with its `garbageBeforeAttributes` replaced.
119+
/// - param newChild: The new `garbageBeforeAttributes` to replace the node's
120+
/// current `garbageBeforeAttributes`, if present.
121+
public func withGarbageBeforeAttributes(
122+
_ newChild: GarbageNodesSyntax?) -> MissingDeclSyntax {
123+
let raw = newChild?.raw
124+
let newData = data.replacingChild(raw, at: Cursor.garbageBeforeAttributes)
125+
return MissingDeclSyntax(newData)
126+
}
127+
128+
public var attributes: AttributeListSyntax? {
129+
get {
130+
let childData = data.child(at: Cursor.attributes,
131+
parent: Syntax(self))
132+
if childData == nil { return nil }
133+
return AttributeListSyntax(childData!)
134+
}
135+
set(value) {
136+
self = withAttributes(value)
137+
}
138+
}
139+
140+
/// Adds the provided `Attribute` to the node's `attributes`
141+
/// collection.
142+
/// - param element: The new `Attribute` to add to the node's
143+
/// `attributes` collection.
144+
/// - returns: A copy of the receiver with the provided `Attribute`
145+
/// appended to its `attributes` collection.
146+
public func addAttribute(_ element: Syntax) -> MissingDeclSyntax {
147+
var collection: RawSyntax
148+
if let col = raw[Cursor.attributes] {
149+
collection = col.appending(element.raw)
150+
} else {
151+
collection = RawSyntax.create(kind: SyntaxKind.attributeList,
152+
layout: [element.raw], length: element.raw.totalLength, presence: .present)
153+
}
154+
let newData = data.replacingChild(collection,
155+
at: Cursor.attributes)
156+
return MissingDeclSyntax(newData)
157+
}
158+
159+
/// Returns a copy of the receiver with its `attributes` replaced.
160+
/// - param newChild: The new `attributes` to replace the node's
161+
/// current `attributes`, if present.
162+
public func withAttributes(
163+
_ newChild: AttributeListSyntax?) -> MissingDeclSyntax {
164+
let raw = newChild?.raw
165+
let newData = data.replacingChild(raw, at: Cursor.attributes)
166+
return MissingDeclSyntax(newData)
167+
}
168+
169+
public var garbageBetweenAttributesAndModifiers: GarbageNodesSyntax? {
170+
get {
171+
let childData = data.child(at: Cursor.garbageBetweenAttributesAndModifiers,
172+
parent: Syntax(self))
173+
if childData == nil { return nil }
174+
return GarbageNodesSyntax(childData!)
175+
}
176+
set(value) {
177+
self = withGarbageBetweenAttributesAndModifiers(value)
178+
}
179+
}
180+
181+
/// Returns a copy of the receiver with its `garbageBetweenAttributesAndModifiers` replaced.
182+
/// - param newChild: The new `garbageBetweenAttributesAndModifiers` to replace the node's
183+
/// current `garbageBetweenAttributesAndModifiers`, if present.
184+
public func withGarbageBetweenAttributesAndModifiers(
185+
_ newChild: GarbageNodesSyntax?) -> MissingDeclSyntax {
186+
let raw = newChild?.raw
187+
let newData = data.replacingChild(raw, at: Cursor.garbageBetweenAttributesAndModifiers)
188+
return MissingDeclSyntax(newData)
189+
}
190+
191+
public var modifiers: ModifierListSyntax? {
192+
get {
193+
let childData = data.child(at: Cursor.modifiers,
194+
parent: Syntax(self))
195+
if childData == nil { return nil }
196+
return ModifierListSyntax(childData!)
197+
}
198+
set(value) {
199+
self = withModifiers(value)
200+
}
201+
}
202+
203+
/// Adds the provided `Modifier` to the node's `modifiers`
204+
/// collection.
205+
/// - param element: The new `Modifier` to add to the node's
206+
/// `modifiers` collection.
207+
/// - returns: A copy of the receiver with the provided `Modifier`
208+
/// appended to its `modifiers` collection.
209+
public func addModifier(_ element: DeclModifierSyntax) -> MissingDeclSyntax {
210+
var collection: RawSyntax
211+
if let col = raw[Cursor.modifiers] {
212+
collection = col.appending(element.raw)
213+
} else {
214+
collection = RawSyntax.create(kind: SyntaxKind.modifierList,
215+
layout: [element.raw], length: element.raw.totalLength, presence: .present)
216+
}
217+
let newData = data.replacingChild(collection,
218+
at: Cursor.modifiers)
219+
return MissingDeclSyntax(newData)
220+
}
221+
222+
/// Returns a copy of the receiver with its `modifiers` replaced.
223+
/// - param newChild: The new `modifiers` to replace the node's
224+
/// current `modifiers`, if present.
225+
public func withModifiers(
226+
_ newChild: ModifierListSyntax?) -> MissingDeclSyntax {
227+
let raw = newChild?.raw
228+
let newData = data.replacingChild(raw, at: Cursor.modifiers)
229+
return MissingDeclSyntax(newData)
230+
}
231+
100232

101233
public func _validateLayout() {
102234
let rawChildren = Array(RawSyntaxChildren(Syntax(self)))
103-
assert(rawChildren.count == 0)
235+
assert(rawChildren.count == 4)
236+
// Check child #0 child is GarbageNodesSyntax or missing
237+
if let raw = rawChildren[0].raw {
238+
let info = rawChildren[0].syntaxInfo
239+
let absoluteRaw = AbsoluteRawSyntax(raw: raw, info: info)
240+
let syntaxData = SyntaxData(absoluteRaw, parent: Syntax(self))
241+
let syntaxChild = Syntax(syntaxData)
242+
assert(syntaxChild.is(GarbageNodesSyntax.self))
243+
}
244+
// Check child #1 child is AttributeListSyntax or missing
245+
if let raw = rawChildren[1].raw {
246+
let info = rawChildren[1].syntaxInfo
247+
let absoluteRaw = AbsoluteRawSyntax(raw: raw, info: info)
248+
let syntaxData = SyntaxData(absoluteRaw, parent: Syntax(self))
249+
let syntaxChild = Syntax(syntaxData)
250+
assert(syntaxChild.is(AttributeListSyntax.self))
251+
}
252+
// Check child #2 child is GarbageNodesSyntax or missing
253+
if let raw = rawChildren[2].raw {
254+
let info = rawChildren[2].syntaxInfo
255+
let absoluteRaw = AbsoluteRawSyntax(raw: raw, info: info)
256+
let syntaxData = SyntaxData(absoluteRaw, parent: Syntax(self))
257+
let syntaxChild = Syntax(syntaxData)
258+
assert(syntaxChild.is(GarbageNodesSyntax.self))
259+
}
260+
// Check child #3 child is ModifierListSyntax or missing
261+
if let raw = rawChildren[3].raw {
262+
let info = rawChildren[3].syntaxInfo
263+
let absoluteRaw = AbsoluteRawSyntax(raw: raw, info: info)
264+
let syntaxData = SyntaxData(absoluteRaw, parent: Syntax(self))
265+
let syntaxChild = Syntax(syntaxData)
266+
assert(syntaxChild.is(ModifierListSyntax.self))
267+
}
104268
}
105269
}
106270

107271
extension MissingDeclSyntax: CustomReflectable {
108272
public var customMirror: Mirror {
109273
return Mirror(self, children: [
274+
"garbageBeforeAttributes": garbageBeforeAttributes.map(Syntax.init)?.asProtocol(SyntaxProtocol.self) as Any,
275+
"attributes": attributes.map(Syntax.init)?.asProtocol(SyntaxProtocol.self) as Any,
276+
"garbageBetweenAttributesAndModifiers": garbageBetweenAttributesAndModifiers.map(Syntax.init)?.asProtocol(SyntaxProtocol.self) as Any,
277+
"modifiers": modifiers.map(Syntax.init)?.asProtocol(SyntaxProtocol.self) as Any,
110278
])
111279
}
112280
}

Sources/SwiftSyntaxBuilderGeneration/gyb_generated/CommonNodes.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,17 @@ let COMMON_NODES: [Node] = [
4747
kind: "Syntax"),
4848

4949
Node(name: "MissingDecl",
50-
kind: "Decl"),
50+
kind: "Decl",
51+
children: [
52+
Child(name: "Attributes",
53+
kind: "AttributeList",
54+
isOptional: true,
55+
collectionElementName: "Attribute"),
56+
Child(name: "Modifiers",
57+
kind: "ModifierList",
58+
isOptional: true,
59+
collectionElementName: "Modifier")
60+
]),
5161

5262
Node(name: "MissingExpr",
5363
kind: "Expr"),

Sources/SwiftSyntaxParser/gyb_generated/NodeDeclarationHash.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@
1717
extension SyntaxParser {
1818
static func verifyNodeDeclarationHash() -> Bool {
1919
return String(cString: swiftparse_syntax_structure_versioning_identifier()!) ==
20-
"d88a6458605388d56b70604413c05f40ff4ffb0c"
20+
"cae571f89efe64c25c27e8c2c299f2e19b8593dd"
2121
}
2222
}

0 commit comments

Comments
 (0)