|
| 1 | +//// Automatically Generated From SyntaxNodes.swift.gyb. |
| 2 | +//// Do Not Edit Directly! |
| 3 | +//===------------ SyntaxNodes.swift - Syntax Node definitions -------------===// |
| 4 | +// |
| 5 | +// This source file is part of the Swift.org open source project |
| 6 | +// |
| 7 | +// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors |
| 8 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 9 | +// |
| 10 | +// See https://swift.org/LICENSE.txt for license information |
| 11 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +// MARK: UnknownSyntax |
| 16 | + |
| 17 | +/// A wrapper around a raw Syntax layout. |
| 18 | +public struct UnknownSyntax: SyntaxProtocol { |
| 19 | + public let _syntaxNode: Syntax |
| 20 | + |
| 21 | + /// Convert the given `Syntax` node to an `UnknownSyntax` if possible. Return |
| 22 | + /// `nil` if the conversion is not possible. |
| 23 | + public init?(_ syntax: Syntax) { |
| 24 | + guard syntax.raw.kind == .unknown else { return nil } |
| 25 | + self._syntaxNode = syntax |
| 26 | + } |
| 27 | + |
| 28 | + /// Creates an `UnknownSyntax` node from the given `SyntaxData`. This assumes |
| 29 | + /// that the `SyntaxData` is of the correct kind. If it is not, the behaviour |
| 30 | + /// is undefined. |
| 31 | + internal init(_ data: SyntaxData) { |
| 32 | + assert(data.raw.kind == .unknown) |
| 33 | + self._syntaxNode = Syntax(data) |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +extension UnknownSyntax: CustomReflectable { |
| 38 | + public var customMirror: Mirror { |
| 39 | + return Mirror(self, children: [:]) |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +// MARK: TokenSyntax |
| 44 | + |
| 45 | +/// A Syntax node representing a single token. |
| 46 | +public struct TokenSyntax: SyntaxProtocol { |
| 47 | + public let _syntaxNode: Syntax |
| 48 | + |
| 49 | + /// Converts the given `Syntax` node to a `TokenSyntax` if possible. Returns |
| 50 | + /// `nil` if the conversion is not possible. |
| 51 | + public init?(_ syntax: Syntax) { |
| 52 | + guard syntax.raw.kind == .token else { return nil } |
| 53 | + self._syntaxNode = syntax |
| 54 | + } |
| 55 | + |
| 56 | + /// Creates a Syntax node from the given `SyntaxData`. This assumes |
| 57 | + /// that the `SyntaxData` is of the correct kind. If it is not, the behaviour |
| 58 | + /// is undefined. |
| 59 | + internal init(_ data: SyntaxData) { |
| 60 | + assert(data.raw.kind == .token) |
| 61 | + self._syntaxNode = Syntax(data) |
| 62 | + } |
| 63 | + |
| 64 | + public var presence: SourcePresence { |
| 65 | + return raw.presence |
| 66 | + } |
| 67 | + |
| 68 | + /// The text of the token as written in the source code. |
| 69 | + public var text: String { |
| 70 | + return tokenKind.text |
| 71 | + } |
| 72 | + |
| 73 | + /// Returns a new TokenSyntax with its kind replaced |
| 74 | + /// by the provided token kind. |
| 75 | + public func withKind(_ tokenKind: TokenKind) -> TokenSyntax { |
| 76 | + guard raw.kind == .token else { |
| 77 | + fatalError("TokenSyntax must have token as its raw") |
| 78 | + } |
| 79 | + let newRaw = RawSyntax.createAndCalcLength(kind: tokenKind, |
| 80 | + leadingTrivia: raw.formLeadingTrivia()!, trailingTrivia: raw.formTrailingTrivia()!, |
| 81 | + presence: raw.presence) |
| 82 | + let newData = data.replacingSelf(newRaw) |
| 83 | + return TokenSyntax(newData) |
| 84 | + } |
| 85 | + |
| 86 | + /// Returns a new TokenSyntax with its leading trivia replaced |
| 87 | + /// by the provided trivia. |
| 88 | + public func withLeadingTrivia(_ leadingTrivia: Trivia) -> TokenSyntax { |
| 89 | + guard raw.kind == .token else { |
| 90 | + fatalError("TokenSyntax must have token as its raw") |
| 91 | + } |
| 92 | + return TokenSyntax(data.withLeadingTrivia(leadingTrivia)) |
| 93 | + } |
| 94 | + |
| 95 | + /// Returns a new TokenSyntax with its trailing trivia replaced |
| 96 | + /// by the provided trivia. |
| 97 | + public func withTrailingTrivia(_ trailingTrivia: Trivia) -> TokenSyntax { |
| 98 | + guard raw.kind == .token else { |
| 99 | + fatalError("TokenSyntax must have token as its raw") |
| 100 | + } |
| 101 | + return TokenSyntax(data.withTrailingTrivia(trailingTrivia)) |
| 102 | + } |
| 103 | + |
| 104 | + /// Returns a new TokenSyntax with its leading trivia removed. |
| 105 | + public func withoutLeadingTrivia() -> TokenSyntax { |
| 106 | + return withLeadingTrivia([]) |
| 107 | + } |
| 108 | + |
| 109 | + /// Returns a new TokenSyntax with its trailing trivia removed. |
| 110 | + public func withoutTrailingTrivia() -> TokenSyntax { |
| 111 | + return withTrailingTrivia([]) |
| 112 | + } |
| 113 | + |
| 114 | + /// Returns a new TokenSyntax with all trivia removed. |
| 115 | + public func withoutTrivia() -> TokenSyntax { |
| 116 | + return withoutLeadingTrivia().withoutTrailingTrivia() |
| 117 | + } |
| 118 | + |
| 119 | + /// The leading trivia (spaces, newlines, etc.) associated with this token. |
| 120 | + public var leadingTrivia: Trivia { |
| 121 | + get { |
| 122 | + return raw.formTokenLeadingTrivia()! |
| 123 | + } |
| 124 | + set { |
| 125 | + self = withLeadingTrivia(newValue) |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + /// The trailing trivia (spaces, newlines, etc.) associated with this token. |
| 130 | + public var trailingTrivia: Trivia { |
| 131 | + get { |
| 132 | + return raw.formTokenTrailingTrivia()! |
| 133 | + } |
| 134 | + set { |
| 135 | + self = withTrailingTrivia(newValue) |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + /// The kind of token this node represents. |
| 140 | + public var tokenKind: TokenKind { |
| 141 | + get { |
| 142 | + return raw.formTokenKind()! |
| 143 | + } |
| 144 | + set { |
| 145 | + self = withKind(newValue) |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + /// The length this node takes up spelled out in the source, excluding its |
| 150 | + /// leading or trailing trivia. |
| 151 | + public var contentLength: SourceLength { |
| 152 | + return raw.tokenContentLength |
| 153 | + } |
| 154 | + |
| 155 | + /// The length this node's leading trivia takes up spelled out in source. |
| 156 | + public var leadingTriviaLength: SourceLength { |
| 157 | + return raw.tokenLeadingTriviaLength |
| 158 | + } |
| 159 | + |
| 160 | + /// The length this node's trailing trivia takes up spelled out in source. |
| 161 | + public var trailingTriviaLength: SourceLength { |
| 162 | + return raw.tokenTrailingTriviaLength |
| 163 | + } |
| 164 | + |
| 165 | + /// The length of this node including all of its trivia. |
| 166 | + public var totalLength: SourceLength { |
| 167 | + return raw.totalLength |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +extension TokenSyntax: CustomReflectable { |
| 172 | + public var customMirror: Mirror { |
| 173 | + return Mirror(self, children: [ |
| 174 | + "text": text, |
| 175 | + "leadingTrivia": leadingTrivia, |
| 176 | + "trailingTrivia": trailingTrivia, |
| 177 | + "tokenKind": tokenKind, |
| 178 | + ]) |
| 179 | + } |
| 180 | +} |
0 commit comments