Skip to content

Commit fbfa766

Browse files
authored
Merge pull request swiftlang#214 from akyrtzi/syntaxnodetype
Introduce a `syntaxNodeType` property for all types conforming to `SyntaxProtocol`
2 parents a510d88 + 66b2523 commit fbfa766

14 files changed

+987
-0
lines changed

Sources/SwiftSyntax/Syntax.swift

+7
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ extension Syntax {
5858
public func `as`<S: SyntaxProtocol>(_ syntaxType: S.Type) -> S? {
5959
return S.init(self)
6060
}
61+
62+
public var syntaxNodeType: SyntaxProtocol.Type {
63+
return Swift.type(of: self.asProtocol(SyntaxProtocol.self))
64+
}
6165
}
6266

6367
extension Syntax: CustomReflectable {
@@ -101,6 +105,9 @@ public protocol SyntaxProtocol: CustomStringConvertible,
101105
/// integrity after it has been rewritten by the syntax rewriter.
102106
/// Results in an assertion failure if the layout is invalid.
103107
func _validateLayout()
108+
109+
/// Returns the underlying syntax node type.
110+
var syntaxNodeType: SyntaxProtocol.Type { get }
104111
}
105112

106113
internal extension SyntaxProtocol {

Sources/SwiftSyntax/SyntaxBaseNodes.swift.gyb

+4
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ public struct ${node.name}: ${node.name}Protocol, SyntaxHashable {
9999
self._syntaxNode = Syntax(data)
100100
}
101101

102+
public var syntaxNodeType: SyntaxProtocol.Type {
103+
return _syntaxNode.syntaxNodeType
104+
}
105+
102106
public func _validateLayout() {
103107
// Check the layout of the concrete type
104108
return Syntax(self)._validateLayout()

Sources/SwiftSyntax/SyntaxCollections.swift.gyb

+4
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ public struct ${node.name}: SyntaxCollection, SyntaxHashable {
5555
self._syntaxNode = Syntax(data)
5656
}
5757

58+
public var syntaxNodeType: SyntaxProtocol.Type {
59+
return Swift.type(of: self)
60+
}
61+
5862
/// The number of elements, `present` or `missing`, in this collection.
5963
public var count: Int { return raw.numberOfChildren }
6064

Sources/SwiftSyntax/SyntaxNodes.swift.gyb.template

+4
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ public struct ${node.name}: ${base_type}Protocol, SyntaxHashable {
7979
assert(data.raw.kind == .${node.swift_syntax_kind})
8080
self._syntaxNode = Syntax(data)
8181
}
82+
83+
public var syntaxNodeType: SyntaxProtocol.Type {
84+
return Swift.type(of: self)
85+
}
8286
% for child in node.children:
8387
% child_node = NODE_MAP.get(child.syntax_kind)
8488
%

Sources/SwiftSyntax/SyntaxOtherNodes.swift

+8
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ public struct UnknownSyntax: SyntaxProtocol, SyntaxHashable {
2525
self._syntaxNode = syntax
2626
}
2727

28+
public var syntaxNodeType: SyntaxProtocol.Type {
29+
return Swift.type(of: self)
30+
}
31+
2832
public func _validateLayout() {
2933
// We are verifying an unknown node. Since we don’t know anything about it
3034
// we need to assume it’s valid.
@@ -66,6 +70,10 @@ public struct TokenSyntax: SyntaxProtocol, SyntaxHashable {
6670
self._syntaxNode = Syntax(data)
6771
}
6872

73+
public var syntaxNodeType: SyntaxProtocol.Type {
74+
return Swift.type(of: self)
75+
}
76+
6977
public func _validateLayout() {
7078
/// A token is always valid as it has no children. Nothing to do here.
7179
}

Sources/SwiftSyntax/gyb_generated/SyntaxBaseNodes.swift

+20
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ public struct DeclSyntax: DeclSyntaxProtocol, SyntaxHashable {
8080
self._syntaxNode = Syntax(data)
8181
}
8282

83+
public var syntaxNodeType: SyntaxProtocol.Type {
84+
return _syntaxNode.syntaxNodeType
85+
}
86+
8387
public func _validateLayout() {
8488
// Check the layout of the concrete type
8589
return Syntax(self)._validateLayout()
@@ -184,6 +188,10 @@ public struct ExprSyntax: ExprSyntaxProtocol, SyntaxHashable {
184188
self._syntaxNode = Syntax(data)
185189
}
186190

191+
public var syntaxNodeType: SyntaxProtocol.Type {
192+
return _syntaxNode.syntaxNodeType
193+
}
194+
187195
public func _validateLayout() {
188196
// Check the layout of the concrete type
189197
return Syntax(self)._validateLayout()
@@ -288,6 +296,10 @@ public struct StmtSyntax: StmtSyntaxProtocol, SyntaxHashable {
288296
self._syntaxNode = Syntax(data)
289297
}
290298

299+
public var syntaxNodeType: SyntaxProtocol.Type {
300+
return _syntaxNode.syntaxNodeType
301+
}
302+
291303
public func _validateLayout() {
292304
// Check the layout of the concrete type
293305
return Syntax(self)._validateLayout()
@@ -392,6 +404,10 @@ public struct TypeSyntax: TypeSyntaxProtocol, SyntaxHashable {
392404
self._syntaxNode = Syntax(data)
393405
}
394406

407+
public var syntaxNodeType: SyntaxProtocol.Type {
408+
return _syntaxNode.syntaxNodeType
409+
}
410+
395411
public func _validateLayout() {
396412
// Check the layout of the concrete type
397413
return Syntax(self)._validateLayout()
@@ -496,6 +512,10 @@ public struct PatternSyntax: PatternSyntaxProtocol, SyntaxHashable {
496512
self._syntaxNode = Syntax(data)
497513
}
498514

515+
public var syntaxNodeType: SyntaxProtocol.Type {
516+
return _syntaxNode.syntaxNodeType
517+
}
518+
499519
public func _validateLayout() {
500520
// Check the layout of the concrete type
501521
return Syntax(self)._validateLayout()

0 commit comments

Comments
 (0)