|
1 |
| -//===-------------------- Syntax.swift - Syntax Protocol ------------------===// |
| 1 | +//===--------------- Syntax.swift - Base Syntax Type eraser --------------===// |
2 | 2 | //
|
3 | 3 | // This source file is part of the Swift.org open source project
|
4 | 4 | //
|
|
13 | 13 | /// A Syntax node represents a tree of nodes with tokens at the leaves.
|
14 | 14 | /// Each node has accessors for its known children, and allows efficient
|
15 | 15 | /// iteration over the children through its `children` property.
|
16 |
| -public struct Syntax: SyntaxProtocol { |
| 16 | +public struct Syntax: SyntaxProtocol, SyntaxHashable { |
17 | 17 | let data: SyntaxData
|
18 | 18 |
|
19 | 19 | public var _syntaxNode: Syntax {
|
@@ -53,15 +53,30 @@ extension Syntax: CustomReflectable {
|
53 | 53 | /// Reconstructs the real syntax type for this type from the node's kind and
|
54 | 54 | /// provides a mirror that reflects this type.
|
55 | 55 | public var customMirror: Mirror {
|
56 |
| - return Mirror(reflecting: self._asConcreteType) |
| 56 | + return Mirror(reflecting: self.as(SyntaxProtocol.self)) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +/// Protocol that provides a common Hashable implementation for all syntax nodes |
| 61 | +public protocol SyntaxHashable: Hashable { |
| 62 | + var _syntaxNode: Syntax { get } |
| 63 | +} |
| 64 | + |
| 65 | +public extension SyntaxHashable { |
| 66 | + func hash(into hasher: inout Hasher) { |
| 67 | + return _syntaxNode.data.nodeId.hash(into: &hasher) |
| 68 | + } |
| 69 | + |
| 70 | + static func ==(lhs: Self, rhs: Self) -> Bool { |
| 71 | + return lhs._syntaxNode.data.nodeId == rhs._syntaxNode.data.nodeId |
57 | 72 | }
|
58 | 73 | }
|
59 | 74 |
|
60 | 75 | /// Provide common functionality for specialized syntax nodes. Extend this
|
61 | 76 | /// protocol to provide common functionality for all syntax nodes.
|
62 | 77 | /// DO NOT CONFORM TO THIS PROTOCOL YOURSELF!
|
63 | 78 | public protocol SyntaxProtocol: CustomStringConvertible,
|
64 |
| - CustomDebugStringConvertible, TextOutputStreamable, Hashable { |
| 79 | + CustomDebugStringConvertible, TextOutputStreamable { |
65 | 80 |
|
66 | 81 | /// Retrieve the generic syntax node that is represented by this node.
|
67 | 82 | /// Do not retrieve this property directly. Use `Syntax(self)` instead.
|
@@ -415,14 +430,6 @@ public extension SyntaxProtocol {
|
415 | 430 | where Target: TextOutputStream {
|
416 | 431 | data.raw.write(to: &target)
|
417 | 432 | }
|
418 |
| - |
419 |
| - func hash(into hasher: inout Hasher) { |
420 |
| - return data.nodeId.hash(into: &hasher) |
421 |
| - } |
422 |
| - |
423 |
| - static func ==(lhs: Self, rhs: Self) -> Bool { |
424 |
| - return lhs.data.nodeId == rhs.data.nodeId |
425 |
| - } |
426 | 433 | }
|
427 | 434 |
|
428 | 435 | /// Sequence of tokens that are part of the provided Syntax node.
|
|
0 commit comments