Skip to content

Commit 106af87

Browse files
committed
Split SyntaxData.swift into multiple files
1 parent 3aca872 commit 106af87

File tree

5 files changed

+196
-160
lines changed

5 files changed

+196
-160
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
struct AbsoluteRawSyntax {
14+
let raw: RawSyntax
15+
let info: AbsoluteSyntaxInfo
16+
17+
/// Returns first `present` child.
18+
func firstChild(viewMode: SyntaxTreeViewMode) -> AbsoluteRawSyntax? {
19+
guard let layoutView = raw.layoutView else { return nil }
20+
var curInfo = info.advancedToFirstChild()
21+
for childOpt in layoutView.children {
22+
if let child = childOpt, viewMode.shouldTraverse(node: child) {
23+
return AbsoluteRawSyntax(raw: child, info: curInfo)
24+
}
25+
curInfo = curInfo.advancedBySibling(childOpt)
26+
}
27+
return nil
28+
}
29+
30+
/// Returns next `present` sibling.
31+
func nextSibling(parent: AbsoluteRawSyntax, viewMode: SyntaxTreeViewMode) -> AbsoluteRawSyntax? {
32+
var curInfo = info.advancedBySibling(raw)
33+
for siblingOpt in parent.raw.layoutView!.children.dropFirst(Int(info.indexInParent + 1)) {
34+
if let sibling = siblingOpt, viewMode.shouldTraverse(node: sibling) {
35+
return AbsoluteRawSyntax(raw: sibling, info: curInfo)
36+
}
37+
curInfo = curInfo.advancedBySibling(siblingOpt)
38+
}
39+
return nil
40+
}
41+
42+
func replacingSelf(_ newRaw: RawSyntax, newRootId: UInt) -> AbsoluteRawSyntax {
43+
let nodeId = SyntaxIdentifier(rootId: newRootId, indexInTree: info.nodeId.indexInTree)
44+
let newInfo = AbsoluteSyntaxInfo(position: info.position, nodeId: nodeId)
45+
return .init(raw: newRaw, info: newInfo)
46+
}
47+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
struct AbsoluteSyntaxPosition {
14+
/// The UTF-8 offset of the syntax node in the source file
15+
let offset: UInt32
16+
let indexInParent: UInt32
17+
18+
func advancedBySibling(_ raw: RawSyntax?) -> AbsoluteSyntaxPosition {
19+
let newOffset = self.offset + UInt32(truncatingIfNeeded: raw?.totalLength.utf8Length ?? 0)
20+
let newIndexInParent = self.indexInParent + 1
21+
return .init(offset: newOffset, indexInParent: newIndexInParent)
22+
}
23+
24+
func advancedToFirstChild() -> AbsoluteSyntaxPosition {
25+
return .init(offset: self.offset, indexInParent: 0)
26+
}
27+
28+
static var forRoot: AbsoluteSyntaxPosition {
29+
return .init(offset: 0, indexInParent: 0)
30+
}
31+
}
32+
33+
/// AbsoluteSyntaxInfo represents the information that relates a RawSyntax to a
34+
/// source file tree, like its absolute source offset.
35+
struct AbsoluteSyntaxInfo {
36+
let position: AbsoluteSyntaxPosition
37+
let nodeId: SyntaxIdentifier
38+
39+
/// The UTF-8 offset of the syntax node in the source file
40+
var offset: UInt32 { return position.offset }
41+
var indexInParent: UInt32 { return position.indexInParent }
42+
43+
func advancedBySibling(_ raw: RawSyntax?) -> AbsoluteSyntaxInfo {
44+
let newPosition = position.advancedBySibling(raw)
45+
let newNodeId = nodeId.advancedBySibling(raw)
46+
return .init(position: newPosition, nodeId: newNodeId)
47+
}
48+
49+
func advancedToFirstChild() -> AbsoluteSyntaxInfo {
50+
let newPosition = position.advancedToFirstChild()
51+
let newNodeId = nodeId.advancedToFirstChild()
52+
return .init(position: newPosition, nodeId: newNodeId)
53+
}
54+
55+
static func forRoot(_ raw: RawSyntax) -> AbsoluteSyntaxInfo {
56+
return .init(position: .forRoot, nodeId: .forRoot(raw))
57+
}
58+
}

Sources/SwiftSyntax/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
add_swift_host_library(SwiftSyntax
1010
AbsolutePosition.swift
11+
AbsoluteRawSyntax.swift
12+
AbsoluteSyntaxInfo.swift
1113
Assert.swift
1214
BumpPtrAllocator.swift
1315
CommonAncestor.swift
@@ -24,6 +26,7 @@ add_swift_host_library(SwiftSyntax
2426
SyntaxCollection.swift
2527
SyntaxData.swift
2628
SyntaxHashable.swift
29+
SyntaxIdentifier.swift
2730
SyntaxNodeStructure.swift
2831
SyntaxProtocol.swift
2932
SyntaxText.swift

Sources/SwiftSyntax/SyntaxData.swift

Lines changed: 0 additions & 160 deletions
Original file line numberDiff line numberDiff line change
@@ -10,166 +10,6 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
struct AbsoluteSyntaxPosition {
14-
/// The UTF-8 offset of the syntax node in the source file
15-
let offset: UInt32
16-
let indexInParent: UInt32
17-
18-
func advancedBySibling(_ raw: RawSyntax?) -> AbsoluteSyntaxPosition {
19-
let newOffset = self.offset + UInt32(truncatingIfNeeded: raw?.totalLength.utf8Length ?? 0)
20-
let newIndexInParent = self.indexInParent + 1
21-
return .init(offset: newOffset, indexInParent: newIndexInParent)
22-
}
23-
24-
func advancedToFirstChild() -> AbsoluteSyntaxPosition {
25-
return .init(offset: self.offset, indexInParent: 0)
26-
}
27-
28-
static var forRoot: AbsoluteSyntaxPosition {
29-
return .init(offset: 0, indexInParent: 0)
30-
}
31-
}
32-
33-
/// AbsoluteSyntaxInfo represents the information that relates a RawSyntax to a
34-
/// source file tree, like its absolute source offset.
35-
struct AbsoluteSyntaxInfo {
36-
let position: AbsoluteSyntaxPosition
37-
let nodeId: SyntaxIdentifier
38-
39-
/// The UTF-8 offset of the syntax node in the source file
40-
var offset: UInt32 { return position.offset }
41-
var indexInParent: UInt32 { return position.indexInParent }
42-
43-
func advancedBySibling(_ raw: RawSyntax?) -> AbsoluteSyntaxInfo {
44-
let newPosition = position.advancedBySibling(raw)
45-
let newNodeId = nodeId.advancedBySibling(raw)
46-
return .init(position: newPosition, nodeId: newNodeId)
47-
}
48-
49-
func advancedToFirstChild() -> AbsoluteSyntaxInfo {
50-
let newPosition = position.advancedToFirstChild()
51-
let newNodeId = nodeId.advancedToFirstChild()
52-
return .init(position: newPosition, nodeId: newNodeId)
53-
}
54-
55-
static func forRoot(_ raw: RawSyntax) -> AbsoluteSyntaxInfo {
56-
return .init(position: .forRoot, nodeId: .forRoot(raw))
57-
}
58-
}
59-
60-
/// Represents a unique value for a node within its own tree.
61-
@_spi(RawSyntax)
62-
public struct SyntaxIndexInTree: Comparable, Hashable {
63-
let indexInTree: UInt32
64-
65-
static var zero: SyntaxIndexInTree = SyntaxIndexInTree(indexInTree: 0)
66-
67-
/// Assuming that this index points to the start of ``Raw``, so that it points
68-
/// to the next sibling of ``Raw``.
69-
func advancedBy(_ raw: RawSyntax?) -> SyntaxIndexInTree {
70-
let newIndexInTree = self.indexInTree + UInt32(truncatingIfNeeded: raw?.totalNodes ?? 0)
71-
return .init(indexInTree: newIndexInTree)
72-
}
73-
74-
/// Assuming that this index points to the next sibling of ``Raw``, reverse it
75-
/// so that it points to the start of ``Raw``.
76-
func reversedBy(_ raw: RawSyntax?) -> SyntaxIndexInTree {
77-
let newIndexInTree = self.indexInTree - UInt32(truncatingIfNeeded: raw?.totalNodes ?? 0)
78-
return .init(indexInTree: newIndexInTree)
79-
}
80-
81-
func advancedToFirstChild() -> SyntaxIndexInTree {
82-
let newIndexInTree = self.indexInTree + 1
83-
return .init(indexInTree: newIndexInTree)
84-
}
85-
86-
init(indexInTree: UInt32) {
87-
self.indexInTree = indexInTree
88-
}
89-
90-
/// Returns `true` if `lhs` occurs before `rhs` in the tree.
91-
public static func < (lhs: SyntaxIndexInTree, rhs: SyntaxIndexInTree) -> Bool {
92-
return lhs.indexInTree < rhs.indexInTree
93-
}
94-
}
95-
96-
/// Provides a stable and unique identity for ``Syntax`` nodes.
97-
///
98-
/// Note that two nodes might have the same contents even if their IDs are
99-
/// different. For example two different ``FunctionDeclSyntax`` nodes in the
100-
/// might have the exact same contents but if they occur at a different
101-
/// location in the source file, they have different IDs.
102-
///
103-
/// Also note that the ID of a syntax node changes when it is anchored in a
104-
/// different syntax tree. Modifying any node in the syntax tree a node is
105-
/// contained in generates a copy of that tree and thus changes the IDs of all
106-
/// nodes in the tree, not just the modified node's children.
107-
public struct SyntaxIdentifier: Hashable {
108-
/// Unique value for the root node.
109-
///
110-
/// Multiple trees may have the same 'rootId' if their root RawSyntax is the
111-
/// same instance. This guarantees that the trees with the same 'rootId' have
112-
/// exact the same structure. But, two trees with exactly the same structure
113-
/// might still have different 'rootId's.
114-
let rootId: UInt
115-
/// Unique value for a node within its own tree.
116-
@_spi(RawSyntax)
117-
public let indexInTree: SyntaxIndexInTree
118-
119-
func advancedBySibling(_ raw: RawSyntax?) -> SyntaxIdentifier {
120-
let newIndexInTree = indexInTree.advancedBy(raw)
121-
return .init(rootId: self.rootId, indexInTree: newIndexInTree)
122-
}
123-
124-
func advancedToFirstChild() -> SyntaxIdentifier {
125-
let newIndexInTree = self.indexInTree.advancedToFirstChild()
126-
return .init(rootId: self.rootId, indexInTree: newIndexInTree)
127-
}
128-
129-
static func forRoot(_ raw: RawSyntax) -> SyntaxIdentifier {
130-
return .init(
131-
rootId: UInt(bitPattern: raw.pointer),
132-
indexInTree: .zero
133-
)
134-
}
135-
}
136-
137-
struct AbsoluteRawSyntax {
138-
let raw: RawSyntax
139-
let info: AbsoluteSyntaxInfo
140-
141-
/// Returns first `present` child.
142-
func firstChild(viewMode: SyntaxTreeViewMode) -> AbsoluteRawSyntax? {
143-
guard let layoutView = raw.layoutView else { return nil }
144-
var curInfo = info.advancedToFirstChild()
145-
for childOpt in layoutView.children {
146-
if let child = childOpt, viewMode.shouldTraverse(node: child) {
147-
return AbsoluteRawSyntax(raw: child, info: curInfo)
148-
}
149-
curInfo = curInfo.advancedBySibling(childOpt)
150-
}
151-
return nil
152-
}
153-
154-
/// Returns next `present` sibling.
155-
func nextSibling(parent: AbsoluteRawSyntax, viewMode: SyntaxTreeViewMode) -> AbsoluteRawSyntax? {
156-
var curInfo = info.advancedBySibling(raw)
157-
for siblingOpt in parent.raw.layoutView!.children.dropFirst(Int(info.indexInParent + 1)) {
158-
if let sibling = siblingOpt, viewMode.shouldTraverse(node: sibling) {
159-
return AbsoluteRawSyntax(raw: sibling, info: curInfo)
160-
}
161-
curInfo = curInfo.advancedBySibling(siblingOpt)
162-
}
163-
return nil
164-
}
165-
166-
func replacingSelf(_ newRaw: RawSyntax, newRootId: UInt) -> AbsoluteRawSyntax {
167-
let nodeId = SyntaxIdentifier(rootId: newRootId, indexInTree: info.nodeId.indexInTree)
168-
let newInfo = AbsoluteSyntaxInfo(position: info.position, nodeId: nodeId)
169-
return .init(raw: newRaw, info: newInfo)
170-
}
171-
}
172-
17313
/// SyntaxData is the underlying storage for each Syntax node.
17414
///
17515
/// SyntaxData is an implementation detail, and should not be exposed to clients
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
/// Represents a unique value for a node within its own tree.
14+
@_spi(RawSyntax)
15+
public struct SyntaxIndexInTree: Comparable, Hashable {
16+
let indexInTree: UInt32
17+
18+
static var zero: SyntaxIndexInTree = SyntaxIndexInTree(indexInTree: 0)
19+
20+
/// Assuming that this index points to the start of ``Raw``, so that it points
21+
/// to the next sibling of ``Raw``.
22+
func advancedBy(_ raw: RawSyntax?) -> SyntaxIndexInTree {
23+
let newIndexInTree = self.indexInTree + UInt32(truncatingIfNeeded: raw?.totalNodes ?? 0)
24+
return .init(indexInTree: newIndexInTree)
25+
}
26+
27+
/// Assuming that this index points to the next sibling of ``Raw``, reverse it
28+
/// so that it points to the start of ``Raw``.
29+
func reversedBy(_ raw: RawSyntax?) -> SyntaxIndexInTree {
30+
let newIndexInTree = self.indexInTree - UInt32(truncatingIfNeeded: raw?.totalNodes ?? 0)
31+
return .init(indexInTree: newIndexInTree)
32+
}
33+
34+
func advancedToFirstChild() -> SyntaxIndexInTree {
35+
let newIndexInTree = self.indexInTree + 1
36+
return .init(indexInTree: newIndexInTree)
37+
}
38+
39+
init(indexInTree: UInt32) {
40+
self.indexInTree = indexInTree
41+
}
42+
43+
/// Returns `true` if `lhs` occurs before `rhs` in the tree.
44+
public static func < (lhs: SyntaxIndexInTree, rhs: SyntaxIndexInTree) -> Bool {
45+
return lhs.indexInTree < rhs.indexInTree
46+
}
47+
}
48+
49+
/// Provides a stable and unique identity for ``Syntax`` nodes.
50+
///
51+
/// Note that two nodes might have the same contents even if their IDs are
52+
/// different. For example two different ``FunctionDeclSyntax`` nodes in the
53+
/// might have the exact same contents but if they occur at a different
54+
/// location in the source file, they have different IDs.
55+
///
56+
/// Also note that the ID of a syntax node changes when it is anchored in a
57+
/// different syntax tree. Modifying any node in the syntax tree a node is
58+
/// contained in generates a copy of that tree and thus changes the IDs of all
59+
/// nodes in the tree, not just the modified node's children.
60+
public struct SyntaxIdentifier: Hashable {
61+
/// Unique value for the root node.
62+
///
63+
/// Multiple trees may have the same 'rootId' if their root RawSyntax is the
64+
/// same instance. This guarantees that the trees with the same 'rootId' have
65+
/// exact the same structure. But, two trees with exactly the same structure
66+
/// might still have different 'rootId's.
67+
let rootId: UInt
68+
/// Unique value for a node within its own tree.
69+
@_spi(RawSyntax)
70+
public let indexInTree: SyntaxIndexInTree
71+
72+
func advancedBySibling(_ raw: RawSyntax?) -> SyntaxIdentifier {
73+
let newIndexInTree = indexInTree.advancedBy(raw)
74+
return .init(rootId: self.rootId, indexInTree: newIndexInTree)
75+
}
76+
77+
func advancedToFirstChild() -> SyntaxIdentifier {
78+
let newIndexInTree = self.indexInTree.advancedToFirstChild()
79+
return .init(rootId: self.rootId, indexInTree: newIndexInTree)
80+
}
81+
82+
static func forRoot(_ raw: RawSyntax) -> SyntaxIdentifier {
83+
return .init(
84+
rootId: UInt(bitPattern: raw.pointer),
85+
indexInTree: .zero
86+
)
87+
}
88+
}

0 commit comments

Comments
 (0)