File: RawSyntaxLayoutView.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (185 lines) | stat: -rw-r--r-- 5,105 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

extension RawSyntax {
  /// A view into the ``RawSyntax`` that exposes functionality that's specific to layout nodes.
  /// The token's payload must be a layout, otherwise this traps.
  @_spi(RawSyntax)
  public var layoutView: RawSyntaxLayoutView? {
    switch raw.payload {
    case .parsedToken, .materializedToken:
      return nil
    case .layout:
      return RawSyntaxLayoutView(raw: self)
    }
  }
}

/// A view into ``RawSyntax`` that exposes functionality that only applies to layout nodes.
@_spi(RawSyntax)
public struct RawSyntaxLayoutView {
  private let raw: RawSyntax

  fileprivate init(raw: RawSyntax) {
    self.raw = raw
    switch raw.payload {
    case .parsedToken, .materializedToken:
      preconditionFailure("RawSyntax must be a layout")
    case .layout(_):
      break
    }
  }

  private var layoutData: RawSyntaxData.Layout {
    switch raw.rawData.payload {
    case .parsedToken(_),
      .materializedToken(_):
      preconditionFailure("RawSyntax must be a layout")
    case .layout(let dat):
      return dat
    }
  }

  var recursiveFlags: RecursiveRawSyntaxFlags {
    return layoutData.recursiveFlags
  }

  /// Creates a new node of the same kind but with children replaced by `elements`.
  ///
  /// The newly created syntax node is allocated in `arena`.
  @_spi(RawSyntax)
  public func replacingLayout(
    with elements: some Collection<RawSyntax?>,
    arena: SyntaxArena
  ) -> RawSyntax {
    return .makeLayout(
      kind: raw.kind,
      uninitializedCount: elements.count,
      arena: arena
    ) { buffer in
      if buffer.isEmpty { return }
      _ = buffer.initialize(from: elements)
    }
  }

  @_spi(RawSyntax)
  public func insertingChild(
    _ newChild: RawSyntax?,
    at index: Int,
    arena: SyntaxArena
  ) -> RawSyntax {
    precondition(children.count >= index)
    return .makeLayout(
      kind: raw.kind,
      uninitializedCount: children.count + 1,
      arena: arena
    ) { buffer in
      var childIterator = children.makeIterator()
      let base = buffer.baseAddress!
      for i in 0..<buffer.count {
        base.advanced(by: i)
          .initialize(to: i == index ? newChild : childIterator.next()!)
      }
    }
  }

  @_spi(RawSyntax)
  public func removingChild(
    at index: Int,
    arena: SyntaxArena
  ) -> RawSyntax {
    precondition(children.count > index)
    let count = children.count - 1
    return .makeLayout(
      kind: raw.kind,
      uninitializedCount: count,
      arena: arena
    ) { buffer in
      if buffer.isEmpty { return }
      let newBase = buffer.baseAddress!
      let oldBase = children.baseAddress!

      // Copy elements up to the index.
      newBase.initialize(from: oldBase, count: index)

      // Copy elements from the index + 1.
      newBase.advanced(by: index)
        .initialize(
          from: oldBase.advanced(by: index + 1),
          count: children.count - index - 1
        )
    }
  }

  @_spi(RawSyntax)
  public func appending(_ newChild: RawSyntax?, arena: SyntaxArena) -> RawSyntax {
    insertingChild(newChild, at: children.count, arena: arena)
  }

  @_spi(RawSyntax)
  public func replacingChildSubrange(
    _ range: Range<Int>,
    with elements: some Collection<RawSyntax?>,
    arena: SyntaxArena
  ) -> RawSyntax {
    precondition(!raw.isToken)
    let newCount = children.count - range.count + elements.count
    return .makeLayout(
      kind: raw.kind,
      uninitializedCount: newCount,
      arena: arena
    ) { buffer in
      if buffer.isEmpty { return }
      var current = buffer.baseAddress!

      // Initialize
      current.initialize(from: children.baseAddress!, count: range.lowerBound)
      current = current.advanced(by: range.lowerBound)
      for elem in elements {
        current.initialize(to: elem)
        current += 1
      }
      current.initialize(
        from: children.baseAddress!.advanced(by: range.upperBound),
        count: children.count - range.upperBound
      )
    }
  }

  @_spi(RawSyntax)
  public func replacingChild(
    at index: Int,
    with newChild: RawSyntax?,
    arena: SyntaxArena
  ) -> RawSyntax {
    precondition(!raw.isToken && children.count > index)
    return .makeLayout(
      kind: raw.kind,
      uninitializedCount: children.count,
      arena: arena
    ) { buffer in
      _ = buffer.initialize(from: children)
      buffer[index] = newChild
    }
  }

  @_spi(RawSyntax)
  public func formLayoutArray() -> [RawSyntax?] {
    Array(children)
  }

  /// Child nodes.
  @_spi(RawSyntax)
  public var children: RawSyntaxBuffer {
    layoutData.layout
  }
}