Skip to content

Update for MissingDeclSyntax Gaining Attributes #578

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Sources/SwiftSyntax/gyb_generated/SyntaxFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,26 @@ public enum SyntaxFactory {
], length: .zero, presence: presence))
return MissingSyntax(data)
}
public static func makeMissingDecl(_ garbageBeforeAttributes: GarbageNodesSyntax? = nil, attributes: AttributeListSyntax?, _ garbageBetweenAttributesAndModifiers: GarbageNodesSyntax? = nil, modifiers: ModifierListSyntax?) -> MissingDeclSyntax {
let layout: [RawSyntax?] = [
garbageBeforeAttributes?.raw,
attributes?.raw,
garbageBetweenAttributesAndModifiers?.raw,
modifiers?.raw,
]
let raw = RawSyntax.createAndCalcLength(kind: SyntaxKind.missingDecl,
layout: layout, presence: SourcePresence.present)
let data = SyntaxData.forRoot(raw)
return MissingDeclSyntax(data)
}

public static func makeBlankMissingDecl(presence: SourcePresence = .missing) -> MissingDeclSyntax {
let data = SyntaxData.forRoot(RawSyntax.create(kind: .missingDecl,
layout: [
nil,
nil,
nil,
nil,
], length: .zero, presence: presence))
return MissingDeclSyntax(data)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ extension UnknownDeclSyntax: CustomReflectable {
// MARK: - MissingDeclSyntax

public struct MissingDeclSyntax: DeclSyntaxProtocol, SyntaxHashable {
enum Cursor: Int {
case garbageBeforeAttributes
case attributes
case garbageBetweenAttributesAndModifiers
case modifiers
}

public let _syntaxNode: Syntax

Expand All @@ -77,16 +83,178 @@ public struct MissingDeclSyntax: DeclSyntaxProtocol, SyntaxHashable {
return Swift.type(of: self)
}

public var garbageBeforeAttributes: GarbageNodesSyntax? {
get {
let childData = data.child(at: Cursor.garbageBeforeAttributes,
parent: Syntax(self))
if childData == nil { return nil }
return GarbageNodesSyntax(childData!)
}
set(value) {
self = withGarbageBeforeAttributes(value)
}
}

/// Returns a copy of the receiver with its `garbageBeforeAttributes` replaced.
/// - param newChild: The new `garbageBeforeAttributes` to replace the node's
/// current `garbageBeforeAttributes`, if present.
public func withGarbageBeforeAttributes(
_ newChild: GarbageNodesSyntax?) -> MissingDeclSyntax {
let raw = newChild?.raw
let newData = data.replacingChild(raw, at: Cursor.garbageBeforeAttributes)
return MissingDeclSyntax(newData)
}

public var attributes: AttributeListSyntax? {
get {
let childData = data.child(at: Cursor.attributes,
parent: Syntax(self))
if childData == nil { return nil }
return AttributeListSyntax(childData!)
}
set(value) {
self = withAttributes(value)
}
}

/// Adds the provided `Attribute` to the node's `attributes`
/// collection.
/// - param element: The new `Attribute` to add to the node's
/// `attributes` collection.
/// - returns: A copy of the receiver with the provided `Attribute`
/// appended to its `attributes` collection.
public func addAttribute(_ element: Syntax) -> MissingDeclSyntax {
var collection: RawSyntax
if let col = raw[Cursor.attributes] {
collection = col.appending(element.raw)
} else {
collection = RawSyntax.create(kind: SyntaxKind.attributeList,
layout: [element.raw], length: element.raw.totalLength, presence: .present)
}
let newData = data.replacingChild(collection,
at: Cursor.attributes)
return MissingDeclSyntax(newData)
}

/// Returns a copy of the receiver with its `attributes` replaced.
/// - param newChild: The new `attributes` to replace the node's
/// current `attributes`, if present.
public func withAttributes(
_ newChild: AttributeListSyntax?) -> MissingDeclSyntax {
let raw = newChild?.raw
let newData = data.replacingChild(raw, at: Cursor.attributes)
return MissingDeclSyntax(newData)
}

public var garbageBetweenAttributesAndModifiers: GarbageNodesSyntax? {
get {
let childData = data.child(at: Cursor.garbageBetweenAttributesAndModifiers,
parent: Syntax(self))
if childData == nil { return nil }
return GarbageNodesSyntax(childData!)
}
set(value) {
self = withGarbageBetweenAttributesAndModifiers(value)
}
}

/// Returns a copy of the receiver with its `garbageBetweenAttributesAndModifiers` replaced.
/// - param newChild: The new `garbageBetweenAttributesAndModifiers` to replace the node's
/// current `garbageBetweenAttributesAndModifiers`, if present.
public func withGarbageBetweenAttributesAndModifiers(
_ newChild: GarbageNodesSyntax?) -> MissingDeclSyntax {
let raw = newChild?.raw
let newData = data.replacingChild(raw, at: Cursor.garbageBetweenAttributesAndModifiers)
return MissingDeclSyntax(newData)
}

public var modifiers: ModifierListSyntax? {
get {
let childData = data.child(at: Cursor.modifiers,
parent: Syntax(self))
if childData == nil { return nil }
return ModifierListSyntax(childData!)
}
set(value) {
self = withModifiers(value)
}
}

/// Adds the provided `Modifier` to the node's `modifiers`
/// collection.
/// - param element: The new `Modifier` to add to the node's
/// `modifiers` collection.
/// - returns: A copy of the receiver with the provided `Modifier`
/// appended to its `modifiers` collection.
public func addModifier(_ element: DeclModifierSyntax) -> MissingDeclSyntax {
var collection: RawSyntax
if let col = raw[Cursor.modifiers] {
collection = col.appending(element.raw)
} else {
collection = RawSyntax.create(kind: SyntaxKind.modifierList,
layout: [element.raw], length: element.raw.totalLength, presence: .present)
}
let newData = data.replacingChild(collection,
at: Cursor.modifiers)
return MissingDeclSyntax(newData)
}

/// Returns a copy of the receiver with its `modifiers` replaced.
/// - param newChild: The new `modifiers` to replace the node's
/// current `modifiers`, if present.
public func withModifiers(
_ newChild: ModifierListSyntax?) -> MissingDeclSyntax {
let raw = newChild?.raw
let newData = data.replacingChild(raw, at: Cursor.modifiers)
return MissingDeclSyntax(newData)
}


public func _validateLayout() {
let rawChildren = Array(RawSyntaxChildren(Syntax(self)))
assert(rawChildren.count == 0)
assert(rawChildren.count == 4)
// Check child #0 child is GarbageNodesSyntax or missing
if let raw = rawChildren[0].raw {
let info = rawChildren[0].syntaxInfo
let absoluteRaw = AbsoluteRawSyntax(raw: raw, info: info)
let syntaxData = SyntaxData(absoluteRaw, parent: Syntax(self))
let syntaxChild = Syntax(syntaxData)
assert(syntaxChild.is(GarbageNodesSyntax.self))
}
// Check child #1 child is AttributeListSyntax or missing
if let raw = rawChildren[1].raw {
let info = rawChildren[1].syntaxInfo
let absoluteRaw = AbsoluteRawSyntax(raw: raw, info: info)
let syntaxData = SyntaxData(absoluteRaw, parent: Syntax(self))
let syntaxChild = Syntax(syntaxData)
assert(syntaxChild.is(AttributeListSyntax.self))
}
// Check child #2 child is GarbageNodesSyntax or missing
if let raw = rawChildren[2].raw {
let info = rawChildren[2].syntaxInfo
let absoluteRaw = AbsoluteRawSyntax(raw: raw, info: info)
let syntaxData = SyntaxData(absoluteRaw, parent: Syntax(self))
let syntaxChild = Syntax(syntaxData)
assert(syntaxChild.is(GarbageNodesSyntax.self))
}
// Check child #3 child is ModifierListSyntax or missing
if let raw = rawChildren[3].raw {
let info = rawChildren[3].syntaxInfo
let absoluteRaw = AbsoluteRawSyntax(raw: raw, info: info)
let syntaxData = SyntaxData(absoluteRaw, parent: Syntax(self))
let syntaxChild = Syntax(syntaxData)
assert(syntaxChild.is(ModifierListSyntax.self))
}
}
}

extension MissingDeclSyntax: CustomReflectable {
public var customMirror: Mirror {
return Mirror(self, children: [
"garbageBeforeAttributes": garbageBeforeAttributes.map(Syntax.init)?.asProtocol(SyntaxProtocol.self) as Any,
"attributes": attributes.map(Syntax.init)?.asProtocol(SyntaxProtocol.self) as Any,
"garbageBetweenAttributesAndModifiers": garbageBetweenAttributesAndModifiers.map(Syntax.init)?.asProtocol(SyntaxProtocol.self) as Any,
"modifiers": modifiers.map(Syntax.init)?.asProtocol(SyntaxProtocol.self) as Any,
])
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,17 @@ let COMMON_NODES: [Node] = [
kind: "Syntax"),

Node(name: "MissingDecl",
kind: "Decl"),
kind: "Decl",
children: [
Child(name: "Attributes",
kind: "AttributeList",
isOptional: true,
collectionElementName: "Attribute"),
Child(name: "Modifiers",
kind: "ModifierList",
isOptional: true,
collectionElementName: "Modifier")
]),

Node(name: "MissingExpr",
kind: "Expr"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@
extension SyntaxParser {
static func verifyNodeDeclarationHash() -> Bool {
return String(cString: swiftparse_syntax_structure_versioning_identifier()!) ==
"d88a6458605388d56b70604413c05f40ff4ffb0c"
"cae571f89efe64c25c27e8c2c299f2e19b8593dd"
}
}