Skip to content

[Syntax] Fix TriviaPiece.isNewline #2943

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 48 additions & 100 deletions CodeGeneration/Sources/SyntaxSupport/Trivia.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,38 @@

import SwiftSyntax

public struct TriviaTraits: OptionSet {
public var rawValue: UInt8

public init(rawValue: UInt8) {
self.rawValue = rawValue
}

// Indicates this is a whitespace.
public static var whitespace: Self { .init(rawValue: 1 << 0) }

// Indicates a newline in Swift source code.
public static var newline: Self { .init(rawValue: 1 << 1) }

// Horizontal space.
public static var spaceOrTab: Self { .init(rawValue: 1 << 2) }

// Comment in Swift source code.
public static var comment: Self { .init(rawValue: 1 << 3) }
}

public class Trivia {
/// The name of the trivia.
public let name: TokenSyntax

/// The doc comment describing the trivia.
public let comment: SwiftSyntax.Trivia

/// The list of characters that make up the trivia.
///
/// Useful for multi-character trivias like `\r\n`.
public let characters: [Character]

/// The list of characters as they would appear in Swift code.
///
/// This might differ from `characters` due to Swift's character escape requirements.
public let swiftCharacters: [Character]
/// The characters that make up the trivia.
public let characters: String?

/// Indicates if the trivia represents a comment.
///
/// If `true`, the trivia is some form of a comment in the Swift code.
public let isComment: Bool
/// The traits.
public let traits: TriviaTraits

/// The name of the trivia in lowercase.
public var lowerName: TokenSyntax { .identifier(lowercaseFirstWord(name: name.text)) }
Expand All @@ -52,168 +63,109 @@ public class Trivia {
}
}

/// The length of the `characters` array.
public var charactersLen: Int { characters.count }

/// Indicates if the trivia is a collection of characters.
///
/// If `true`, the trivia is made up of multiple characters.
public var isCollection: Bool { charactersLen > 0 }

/// Indicates if the trivia contains only whitespace characters.
public var isBlank: Bool {
characters.contains { $0.isWhitespace }
}

/// Indicates if the trivia contains newline characters.
public var isNewLine: Bool {
characters.contains { $0.isNewline }
}
public var isCollection: Bool { characters != nil }

/// Initializes a new `Trivia` instance.
///
/// - Parameters:
/// - name: A name of the trivia.
/// - comment: A doc comment describing the trivia.
/// - characters: A list of characters that make up the trivia.
/// - swiftCharacters: A list of characters as they would appear in Swift code.
/// - isComment: Indicates if the trivia represents a comment.
init(
name: TokenSyntax,
comment: SwiftSyntax.Trivia,
characters: [Character] = [],
swiftCharacters: [Character] = [],
isComment: Bool = false
characters: String? = nil,
traits: TriviaTraits = []
) {
self.name = name
self.comment = comment
self.isComment = isComment
self.characters = characters

// Swift sometimes doesn't support escaped characters like \f or \v;
// we should allow specifying alternatives explicitly.
if !swiftCharacters.isEmpty {
self.swiftCharacters = swiftCharacters
} else {
self.swiftCharacters = characters
}
self.traits = traits
}
}

public let TRIVIAS: [Trivia] = [
Trivia(
name: "Backslash",
comment: #"A backslash that is at the end of a line in a multi-line string literal to escape the newline."#,
characters: [
Character("\\")
],
swiftCharacters: [
Character("\\")
]
characters: "\\"
),

Trivia(
name: "BlockComment",
comment: #"A developer block comment, starting with '/*' and ending with '*/'."#,
isComment: true
traits: [.comment]
),

Trivia(
name: "CarriageReturn",
comment: #"A newline '\r' character."#,
characters: [
Character("\r")
],
swiftCharacters: [
Character("\r")
]
characters: "\r",
traits: [.whitespace, .newline]
),

Trivia(
name: "CarriageReturnLineFeed",
comment: #"A newline consists of contiguous '\r' and '\n' characters."#,
characters: [
Character("\r"),
Character("\n"),
],
swiftCharacters: [
Character("\r"),
Character("\n"),
]
characters: "\r\n",
traits: [.whitespace, .newline]
),

Trivia(
name: "DocBlockComment",
comment: #"A documentation block comment, starting with '/**' and ending with '*/'."#,
isComment: true
traits: [.comment]
),

Trivia(
name: "DocLineComment",
comment: #"A documentation line comment, starting with '///' and excluding the trailing newline."#,
isComment: true
traits: [.comment]
),

// Swift don't support form feed '\f' so we use the raw unicode
Trivia(
name: "Formfeed",
comment: #"A form-feed 'f' character."#,
characters: [
Character("\u{c}")
],
swiftCharacters: [
Character("\u{240C}")
]
characters: "\u{000C}",
traits: [.whitespace]
),

Trivia(
name: "LineComment",
comment: #"A developer line comment, starting with '//' and excluding the trailing newline."#,
isComment: true
traits: [.comment]
),

Trivia(
name: "Newline",
comment: #"A newline '\n' character."#,
characters: [
Character("\n")
],
swiftCharacters: [
Character("\n")
]
characters: "\n",
traits: [.whitespace, .newline]
),

Trivia(
name: "Pound",
comment: #"A '#' that is at the end of a line in a multi-line string literal to escape the newline."#,
characters: [
Character("#")
],
swiftCharacters: [
Character("#")
]
characters: "#"
),

Trivia(
name: "Space",
comment: #"A space ' ' character."#,
characters: [
Character(" ")
],
swiftCharacters: [
Character(" ")
]
characters: " ",
traits: [.whitespace, .spaceOrTab]
),

Trivia(
name: "Tab",
comment: #"A tab '\t' character."#,
characters: [
Character("\t")
],
swiftCharacters: [
Character("\t")
]
characters: "\t",
traits: [.whitespace, .spaceOrTab]
),

Trivia(
Expand All @@ -225,11 +177,7 @@ public let TRIVIAS: [Trivia] = [
Trivia(
name: "VerticalTab",
comment: #"A vertical tab '\v' character."#,
characters: [
Character("\u{b}")
],
swiftCharacters: [
Character("\u{2B7F}")
]
characters: "\u{000B}",
traits: [.whitespace]
),
]
Loading