File: TokenSequence.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 (129 lines) | stat: -rw-r--r-- 4,541 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
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

/// Sequence of tokens that are part of the provided Syntax node.
public struct TokenSequence: Sequence, Sendable {
  /// Iterates over a ``TokenSequence``.
  public struct Iterator: IteratorProtocol {
    var nextToken: TokenSyntax?
    /// The last token to iterate (inclusive).
    let endToken: TokenSyntax?
    let viewMode: SyntaxTreeViewMode

    init(_ token: TokenSyntax?, endToken: TokenSyntax?, viewMode: SyntaxTreeViewMode) {
      self.nextToken = token
      self.endToken = endToken
      self.viewMode = viewMode
    }

    /// Return the next element in a ``TokenSequence``.
    public mutating func next() -> TokenSyntax? {
      guard let token = self.nextToken else { return nil }
      if nextToken == endToken {
        self.nextToken = nil
      } else {
        self.nextToken = token.nextToken(viewMode: viewMode)
      }
      return token
    }
  }

  let node: Syntax
  let viewMode: SyntaxTreeViewMode

  /// Construct a ``TokenSequence`` that walks all tokens in `node` in source order,
  /// recursively walking into child nodes.
  ///
  /// All nodes that are not visible in the given `viewMode` are skipped.
  public init(_ node: Syntax, viewMode: SyntaxTreeViewMode) {
    self.node = node
    self.viewMode = viewMode
  }

  /// Create an iterator that iterates over all the tokens in the sequence.
  public func makeIterator() -> Iterator {
    return Iterator(
      node.firstToken(viewMode: viewMode),
      endToken: node.lastToken(viewMode: viewMode),
      viewMode: viewMode
    )
  }

  /// Iterate the tokens in reverse order.
  public func reversed() -> ReversedTokenSequence {
    return ReversedTokenSequence(node, viewMode: viewMode)
  }
}

extension TokenSequence: CustomReflectable {
  /// A custom mirror for ``TokenSequence`` that shows all elements in the sequence.
  public var customMirror: Mirror {
    return Mirror(self, unlabeledChildren: self.map { $0 })
  }
}

/// Reverse sequence of tokens that are part of the provided Syntax node.
public struct ReversedTokenSequence: Sequence, Sendable {
  /// Iterates over a ``ReversedTokenSequence``.
  public struct Iterator: IteratorProtocol {
    var nextToken: TokenSyntax?
    let startPosition: AbsolutePosition
    let viewMode: SyntaxTreeViewMode

    init(_ token: TokenSyntax?, startPosition: AbsolutePosition, viewMode: SyntaxTreeViewMode) {
      self.nextToken = token
      self.startPosition = startPosition
      self.viewMode = viewMode
    }

    /// Returns the next element in a ``ReversedTokenSequence``, i.e. the one
    /// that occurred before the current token in source order.
    public mutating func next() -> TokenSyntax? {
      guard let token = self.nextToken else { return nil }
      self.nextToken = token.previousToken(viewMode: viewMode)
      // Make sure we stop once we went beyond the start of the containing node.
      if let nextTok = self.nextToken, nextTok.position < self.startPosition {
        self.nextToken = nil
      }
      return token
    }
  }

  let node: Syntax
  let viewMode: SyntaxTreeViewMode

  /// Construct a ``TokenSequence`` that walks all tokens in `node` in reverse
  /// source order, recursively walking into child nodes.
  ///
  /// All nodes that are not visible in the given `viewMode` are skipped.
  public init(_ node: Syntax, viewMode: SyntaxTreeViewMode) {
    self.node = node
    self.viewMode = viewMode
  }

  /// Create an iterator that iterates over all the tokens in the sequence.
  public func makeIterator() -> Iterator {
    return Iterator(node.lastToken(viewMode: viewMode), startPosition: node.position, viewMode: viewMode)
  }

  /// Iterate over the tokens in source order.
  public func reversed() -> TokenSequence {
    return TokenSequence(node, viewMode: viewMode)
  }
}

extension ReversedTokenSequence: CustomReflectable {
  /// A custom mirror for ``ReversedTokenSequence`` that shows all elements in the sequence.
  public var customMirror: Mirror {
    return Mirror(self, unlabeledChildren: self.map { $0 })
  }
}