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 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
|
//// Automatically generated by generate-swift-syntax
//// Do not edit directly!
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
/// A contiguous stretch of a single kind of trivia. The constituent part of
/// a ``Trivia`` collection.
///
/// For example, four spaces would be represented by
/// `.spaces(4)`
///
/// In general, you should deal with the actual Trivia collection instead
/// of individual pieces whenever possible.
public enum TriviaPiece: Sendable {
/// A backslash that is at the end of a line in a multi-line string literal to escape the newline.
case backslashes(Int)
/// A developer block comment, starting with '/*' and ending with '*/'.
case blockComment(String)
/// A newline '\r' character.
case carriageReturns(Int)
/// A newline consists of contiguous '\r' and '\n' characters.
case carriageReturnLineFeeds(Int)
/// A documentation block comment, starting with '/**' and ending with '*/'.
case docBlockComment(String)
/// A documentation line comment, starting with '///'.
case docLineComment(String)
/// A form-feed 'f' character.
case formfeeds(Int)
/// A developer line comment, starting with '//'
case lineComment(String)
/// A newline '\n' character.
case newlines(Int)
/// A '#' that is at the end of a line in a multi-line string literal to escape the newline.
case pounds(Int)
/// A space ' ' character.
case spaces(Int)
/// A tab '\t' character.
case tabs(Int)
/// Any skipped unexpected text.
case unexpectedText(String)
/// A vertical tab '\v' character.
case verticalTabs(Int)
}
extension TriviaPiece: TextOutputStreamable {
/// Prints the provided trivia as they would be written in a source file.
///
/// - Parameter stream: The stream to which to print the trivia.
public func write(to target: inout some TextOutputStream) {
func printRepeated(_ character: String, count: Int) {
for _ in 0 ..< count {
target.write(character)
}
}
switch self {
case let .backslashes(count):
printRepeated(#"\"#, count: count)
case let .blockComment(text):
target.write(text)
case let .carriageReturns(count):
printRepeated("\r", count: count)
case let .carriageReturnLineFeeds(count):
printRepeated("\r\n", count: count)
case let .docBlockComment(text):
target.write(text)
case let .docLineComment(text):
target.write(text)
case let .formfeeds(count):
printRepeated("\u{c}", count: count)
case let .lineComment(text):
target.write(text)
case let .newlines(count):
printRepeated("\n", count: count)
case let .pounds(count):
printRepeated("#", count: count)
case let .spaces(count):
printRepeated(" ", count: count)
case let .tabs(count):
printRepeated("\t", count: count)
case let .unexpectedText(text):
target.write(text)
case let .verticalTabs(count):
printRepeated("\u{b}", count: count)
}
}
}
extension TriviaPiece: CustomDebugStringConvertible {
/// Returns a description used by dump.
public var debugDescription: String {
switch self {
case .backslashes(let data):
return "backslashes(\(data))"
case .blockComment(let name):
return "blockComment(\(name.debugDescription))"
case .carriageReturns(let data):
return "carriageReturns(\(data))"
case .carriageReturnLineFeeds(let data):
return "carriageReturnLineFeeds(\(data))"
case .docBlockComment(let name):
return "docBlockComment(\(name.debugDescription))"
case .docLineComment(let name):
return "docLineComment(\(name.debugDescription))"
case .formfeeds(let data):
return "formfeeds(\(data))"
case .lineComment(let name):
return "lineComment(\(name.debugDescription))"
case .newlines(let data):
return "newlines(\(data))"
case .pounds(let data):
return "pounds(\(data))"
case .spaces(let data):
return "spaces(\(data))"
case .tabs(let data):
return "tabs(\(data))"
case .unexpectedText(let name):
return "unexpectedText(\(name.debugDescription))"
case .verticalTabs(let data):
return "verticalTabs(\(data))"
}
}
}
extension Trivia {
/// Returns a piece of trivia for some number of #"\"# characters.
public static func backslashes(_ count: Int) -> Trivia {
return [.backslashes(count)]
}
/// Gets a piece of trivia for #"\"# characters.
public static var backslash: Trivia {
return .backslashes(1)
}
/// Returns a piece of trivia for BlockComment.
public static func blockComment(_ text: String) -> Trivia {
return [.blockComment(text)]
}
/// Returns a piece of trivia for some number of "\r" characters.
public static func carriageReturns(_ count: Int) -> Trivia {
return [.carriageReturns(count)]
}
/// Gets a piece of trivia for "\r" characters.
public static var carriageReturn: Trivia {
return .carriageReturns(1)
}
/// Returns a piece of trivia for some number of "\r\n" characters.
public static func carriageReturnLineFeeds(_ count: Int) -> Trivia {
return [.carriageReturnLineFeeds(count)]
}
/// Gets a piece of trivia for "\r\n" characters.
public static var carriageReturnLineFeed: Trivia {
return .carriageReturnLineFeeds(1)
}
/// Returns a piece of trivia for DocBlockComment.
public static func docBlockComment(_ text: String) -> Trivia {
return [.docBlockComment(text)]
}
/// Returns a piece of trivia for DocLineComment.
public static func docLineComment(_ text: String) -> Trivia {
return [.docLineComment(text)]
}
/// Returns a piece of trivia for some number of "\u{c}" characters.
public static func formfeeds(_ count: Int) -> Trivia {
return [.formfeeds(count)]
}
/// Gets a piece of trivia for "\u{c}" characters.
public static var formfeed: Trivia {
return .formfeeds(1)
}
/// Returns a piece of trivia for LineComment.
public static func lineComment(_ text: String) -> Trivia {
return [.lineComment(text)]
}
/// Returns a piece of trivia for some number of "\n" characters.
public static func newlines(_ count: Int) -> Trivia {
return [.newlines(count)]
}
/// Gets a piece of trivia for "\n" characters.
public static var newline: Trivia {
return .newlines(1)
}
/// Returns a piece of trivia for some number of "#" characters.
public static func pounds(_ count: Int) -> Trivia {
return [.pounds(count)]
}
/// Gets a piece of trivia for "#" characters.
public static var pound: Trivia {
return .pounds(1)
}
/// Returns a piece of trivia for some number of " " characters.
public static func spaces(_ count: Int) -> Trivia {
return [.spaces(count)]
}
/// Gets a piece of trivia for " " characters.
public static var space: Trivia {
return .spaces(1)
}
/// Returns a piece of trivia for some number of "\t" characters.
public static func tabs(_ count: Int) -> Trivia {
return [.tabs(count)]
}
/// Gets a piece of trivia for "\t" characters.
public static var tab: Trivia {
return .tabs(1)
}
/// Returns a piece of trivia for UnexpectedText.
public static func unexpectedText(_ text: String) -> Trivia {
return [.unexpectedText(text)]
}
/// Returns a piece of trivia for some number of "\u{b}" characters.
public static func verticalTabs(_ count: Int) -> Trivia {
return [.verticalTabs(count)]
}
/// Gets a piece of trivia for "\u{b}" characters.
public static var verticalTab: Trivia {
return .verticalTabs(1)
}
}
extension TriviaPiece: Equatable {}
extension TriviaPiece {
public var sourceLength: SourceLength {
switch self {
case let .backslashes(count):
return SourceLength(utf8Length: count)
case let .blockComment(text):
return SourceLength(of: text)
case let .carriageReturns(count):
return SourceLength(utf8Length: count)
case let .carriageReturnLineFeeds(count):
return SourceLength(utf8Length: count * 2)
case let .docBlockComment(text):
return SourceLength(of: text)
case let .docLineComment(text):
return SourceLength(of: text)
case let .formfeeds(count):
return SourceLength(utf8Length: count)
case let .lineComment(text):
return SourceLength(of: text)
case let .newlines(count):
return SourceLength(utf8Length: count)
case let .pounds(count):
return SourceLength(utf8Length: count)
case let .spaces(count):
return SourceLength(utf8Length: count)
case let .tabs(count):
return SourceLength(utf8Length: count)
case let .unexpectedText(text):
return SourceLength(of: text)
case let .verticalTabs(count):
return SourceLength(utf8Length: count)
}
}
}
/// Trivia piece for token RawSyntax.
///
/// In contrast to ``TriviaPiece``, a ``RawTriviaPiece`` does not own the source
/// text of the trivia.
@_spi(RawSyntax)
public enum RawTriviaPiece: Equatable, Sendable {
case backslashes(Int)
case blockComment(SyntaxText)
case carriageReturns(Int)
case carriageReturnLineFeeds(Int)
case docBlockComment(SyntaxText)
case docLineComment(SyntaxText)
case formfeeds(Int)
case lineComment(SyntaxText)
case newlines(Int)
case pounds(Int)
case spaces(Int)
case tabs(Int)
case unexpectedText(SyntaxText)
case verticalTabs(Int)
static func make(_ piece: TriviaPiece, arena: SyntaxArena) -> RawTriviaPiece {
switch piece {
case let .backslashes(count):
return .backslashes(count)
case let .blockComment(text):
return .blockComment(arena.intern(text))
case let .carriageReturns(count):
return .carriageReturns(count)
case let .carriageReturnLineFeeds(count):
return .carriageReturnLineFeeds(count)
case let .docBlockComment(text):
return .docBlockComment(arena.intern(text))
case let .docLineComment(text):
return .docLineComment(arena.intern(text))
case let .formfeeds(count):
return .formfeeds(count)
case let .lineComment(text):
return .lineComment(arena.intern(text))
case let .newlines(count):
return .newlines(count)
case let .pounds(count):
return .pounds(count)
case let .spaces(count):
return .spaces(count)
case let .tabs(count):
return .tabs(count)
case let .unexpectedText(text):
return .unexpectedText(arena.intern(text))
case let .verticalTabs(count):
return .verticalTabs(count)
}
}
}
extension TriviaPiece {
@_spi(RawSyntax) public init(raw: RawTriviaPiece) {
switch raw {
case let .backslashes(count):
self = .backslashes(count)
case let .blockComment(text):
self = .blockComment(String(syntaxText: text))
case let .carriageReturns(count):
self = .carriageReturns(count)
case let .carriageReturnLineFeeds(count):
self = .carriageReturnLineFeeds(count)
case let .docBlockComment(text):
self = .docBlockComment(String(syntaxText: text))
case let .docLineComment(text):
self = .docLineComment(String(syntaxText: text))
case let .formfeeds(count):
self = .formfeeds(count)
case let .lineComment(text):
self = .lineComment(String(syntaxText: text))
case let .newlines(count):
self = .newlines(count)
case let .pounds(count):
self = .pounds(count)
case let .spaces(count):
self = .spaces(count)
case let .tabs(count):
self = .tabs(count)
case let .unexpectedText(text):
self = .unexpectedText(String(syntaxText: text))
case let .verticalTabs(count):
self = .verticalTabs(count)
}
}
}
extension RawTriviaPiece {
public var byteLength: Int {
switch self {
case let .backslashes(count):
return count
case let .blockComment(text):
return text.count
case let .carriageReturns(count):
return count
case let .carriageReturnLineFeeds(count):
return count * 2
case let .docBlockComment(text):
return text.count
case let .docLineComment(text):
return text.count
case let .formfeeds(count):
return count
case let .lineComment(text):
return text.count
case let .newlines(count):
return count
case let .pounds(count):
return count
case let .spaces(count):
return count
case let .tabs(count):
return count
case let .unexpectedText(text):
return text.count
case let .verticalTabs(count):
return count
}
}
var storedText: SyntaxText? {
switch self {
case .backslashes(_):
return nil
case .blockComment(let text):
return text
case .carriageReturns(_):
return nil
case .carriageReturnLineFeeds(_):
return nil
case .docBlockComment(let text):
return text
case .docLineComment(let text):
return text
case .formfeeds(_):
return nil
case .lineComment(let text):
return text
case .newlines(_):
return nil
case .pounds(_):
return nil
case .spaces(_):
return nil
case .tabs(_):
return nil
case .unexpectedText(let text):
return text
case .verticalTabs(_):
return nil
}
}
}
extension TriviaPiece {
/// Returns `true` if this piece is a newline, space or tab.
public var isWhitespace: Bool {
return isSpaceOrTab || isNewline
}
public var isNewline: Bool {
switch self {
case .carriageReturns:
return true
case .carriageReturnLineFeeds:
return true
case .formfeeds:
return true
case .newlines:
return true
case .verticalTabs:
return true
default:
return false
}
}
public var isSpaceOrTab: Bool {
switch self {
case .spaces:
return true
case .tabs:
return true
default:
return false
}
}
/// Returns `true` if this piece is a comment.
public var isComment: Bool {
switch self {
case .lineComment, .blockComment, .docLineComment, .docBlockComment:
return true
default:
return false
}
}
}
extension RawTriviaPiece {
/// Returns `true` if this piece is a newline, space or tab.
public var isWhitespace: Bool {
return isSpaceOrTab || isNewline
}
public var isNewline: Bool {
switch self {
case .carriageReturns:
return true
case .carriageReturnLineFeeds:
return true
case .formfeeds:
return true
case .newlines:
return true
case .verticalTabs:
return true
default:
return false
}
}
public var isSpaceOrTab: Bool {
switch self {
case .spaces:
return true
case .tabs:
return true
default:
return false
}
}
/// Returns `true` if this piece is a comment.
public var isComment: Bool {
switch self {
case .lineComment, .blockComment, .docLineComment, .docBlockComment:
return true
default:
return false
}
}
}
|