Skip to content

[5.3] Introduce a syntaxNodeType property for all types conforming to SyntaxProtocol #215

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
May 1, 2020
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
7 changes: 7 additions & 0 deletions Sources/SwiftSyntax/Syntax.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ extension Syntax {
public func `as`<S: SyntaxProtocol>(_ syntaxType: S.Type) -> S? {
return S.init(self)
}

public var syntaxNodeType: SyntaxProtocol.Type {
return Swift.type(of: self.asProtocol(SyntaxProtocol.self))
}
}

extension Syntax: CustomReflectable {
Expand Down Expand Up @@ -101,6 +105,9 @@ public protocol SyntaxProtocol: CustomStringConvertible,
/// integrity after it has been rewritten by the syntax rewriter.
/// Results in an assertion failure if the layout is invalid.
func _validateLayout()

/// Returns the underlying syntax node type.
var syntaxNodeType: SyntaxProtocol.Type { get }
}

internal extension SyntaxProtocol {
Expand Down
4 changes: 4 additions & 0 deletions Sources/SwiftSyntax/SyntaxBaseNodes.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ public struct ${node.name}: ${node.name}Protocol, SyntaxHashable {
self._syntaxNode = Syntax(data)
}

public var syntaxNodeType: SyntaxProtocol.Type {
return _syntaxNode.syntaxNodeType
}

public func _validateLayout() {
// Check the layout of the concrete type
return Syntax(self)._validateLayout()
Expand Down
4 changes: 4 additions & 0 deletions Sources/SwiftSyntax/SyntaxCollections.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ public struct ${node.name}: SyntaxCollection, SyntaxHashable {
self._syntaxNode = Syntax(data)
}

public var syntaxNodeType: SyntaxProtocol.Type {
return Swift.type(of: self)
}

/// The number of elements, `present` or `missing`, in this collection.
public var count: Int { return raw.numberOfChildren }

Expand Down
4 changes: 4 additions & 0 deletions Sources/SwiftSyntax/SyntaxNodes.swift.gyb.template
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ public struct ${node.name}: ${base_type}Protocol, SyntaxHashable {
assert(data.raw.kind == .${node.swift_syntax_kind})
self._syntaxNode = Syntax(data)
}

public var syntaxNodeType: SyntaxProtocol.Type {
return Swift.type(of: self)
}
% for child in node.children:
% child_node = NODE_MAP.get(child.syntax_kind)
%
Expand Down
8 changes: 8 additions & 0 deletions Sources/SwiftSyntax/SyntaxOtherNodes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ public struct UnknownSyntax: SyntaxProtocol, SyntaxHashable {
self._syntaxNode = syntax
}

public var syntaxNodeType: SyntaxProtocol.Type {
return Swift.type(of: self)
}

public func _validateLayout() {
// We are verifying an unknown node. Since we don’t know anything about it
// we need to assume it’s valid.
Expand Down Expand Up @@ -66,6 +70,10 @@ public struct TokenSyntax: SyntaxProtocol, SyntaxHashable {
self._syntaxNode = Syntax(data)
}

public var syntaxNodeType: SyntaxProtocol.Type {
return Swift.type(of: self)
}

public func _validateLayout() {
/// A token is always valid as it has no children. Nothing to do here.
}
Expand Down
20 changes: 20 additions & 0 deletions Sources/SwiftSyntax/gyb_generated/SyntaxBaseNodes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ public struct DeclSyntax: DeclSyntaxProtocol, SyntaxHashable {
self._syntaxNode = Syntax(data)
}

public var syntaxNodeType: SyntaxProtocol.Type {
return _syntaxNode.syntaxNodeType
}

public func _validateLayout() {
// Check the layout of the concrete type
return Syntax(self)._validateLayout()
Expand Down Expand Up @@ -184,6 +188,10 @@ public struct ExprSyntax: ExprSyntaxProtocol, SyntaxHashable {
self._syntaxNode = Syntax(data)
}

public var syntaxNodeType: SyntaxProtocol.Type {
return _syntaxNode.syntaxNodeType
}

public func _validateLayout() {
// Check the layout of the concrete type
return Syntax(self)._validateLayout()
Expand Down Expand Up @@ -288,6 +296,10 @@ public struct StmtSyntax: StmtSyntaxProtocol, SyntaxHashable {
self._syntaxNode = Syntax(data)
}

public var syntaxNodeType: SyntaxProtocol.Type {
return _syntaxNode.syntaxNodeType
}

public func _validateLayout() {
// Check the layout of the concrete type
return Syntax(self)._validateLayout()
Expand Down Expand Up @@ -392,6 +404,10 @@ public struct TypeSyntax: TypeSyntaxProtocol, SyntaxHashable {
self._syntaxNode = Syntax(data)
}

public var syntaxNodeType: SyntaxProtocol.Type {
return _syntaxNode.syntaxNodeType
}

public func _validateLayout() {
// Check the layout of the concrete type
return Syntax(self)._validateLayout()
Expand Down Expand Up @@ -496,6 +512,10 @@ public struct PatternSyntax: PatternSyntaxProtocol, SyntaxHashable {
self._syntaxNode = Syntax(data)
}

public var syntaxNodeType: SyntaxProtocol.Type {
return _syntaxNode.syntaxNodeType
}

public func _validateLayout() {
// Check the layout of the concrete type
return Syntax(self)._validateLayout()
Expand Down
Loading