Skip to content

Differentiate Shebang from Garbage #492

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 1 commit into from
Jul 11, 2022
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
3 changes: 2 additions & 1 deletion Sources/SwiftSyntax/SourceLocation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,8 @@ fileprivate extension TriviaPiece {
body(carriageReturnLineLength)
}
lineLength = .zero
case let .lineComment(text),
case let .shebang(text),
let .lineComment(text),
let .docLineComment(text):
// Line comments are not supposed to contain newlines.
assert(!text.containsSwiftNewline(), "line comment created that contained a new-line character")
Expand Down
25 changes: 25 additions & 0 deletions Sources/SwiftSyntax/gyb_generated/Trivia.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public enum TriviaPiece {
case docBlockComment(String)
/// Any skipped garbage text.
case garbageText(String)
/// A script command, starting with '#!'.
case shebang(String)
}

extension TriviaPiece: TextOutputStreamable {
Expand Down Expand Up @@ -83,6 +85,8 @@ extension TriviaPiece: TextOutputStreamable {
target.write(text)
case let .garbageText(text):
target.write(text)
case let .shebang(text):
target.write(text)
}
}
}
Expand Down Expand Up @@ -115,6 +119,8 @@ extension TriviaPiece: CustomDebugStringConvertible {
return "docBlockComment(\(name))"
case .garbageText(let name):
return "garbageText(\(name))"
case .shebang(let name):
return "shebang(\(name))"
}
}
}
Expand Down Expand Up @@ -249,6 +255,11 @@ public struct Trivia {
public static func garbageText(_ text: String) -> Trivia {
return [.garbageText(text)]
}

/// Returns a piece of trivia for Shebang.
public static func shebang(_ text: String) -> Trivia {
return [.shebang(text)]
}
}

extension Trivia: Equatable {}
Expand Down Expand Up @@ -314,6 +325,8 @@ extension TriviaPiece {
return SourceLength(of: text)
case let .garbageText(text):
return SourceLength(of: text)
case let .shebang(text):
return SourceLength(of: text)
}
}
}
Expand Down Expand Up @@ -346,6 +359,8 @@ extension TriviaPiece {
return .docBlockComment(.fromBuffer(textBuffer))
case 12:
return .garbageText(.fromBuffer(textBuffer))
case 13:
return .shebang(.fromBuffer(textBuffer))
default:
fatalError("unexpected trivia piece kind \(piece.kind)")
}
Expand Down Expand Up @@ -377,6 +392,8 @@ extension TriviaPiece {
return true
case 12:
return true
case 13:
return true
default:
fatalError("unexpected trivia piece kind \(kind)")
}
Expand Down Expand Up @@ -409,6 +426,8 @@ internal enum TriviaPieceKind: CTriviaKind {
case docBlockComment = 11
/// Any skipped garbage text.
case garbageText = 12
/// A script command, starting with '#!'.
case shebang = 13

static func fromRawValue(_ rawValue: CTriviaKind) -> TriviaPieceKind {
return TriviaPieceKind(rawValue: rawValue)!
Expand Down Expand Up @@ -471,6 +490,12 @@ extension TriviaPiece {
return text.utf8.withContiguousStorageIfAvailable({ (buf: UnsafeBufferPointer<UInt8>) in
return body(.init(kind: .garbageText, length: length, customText: buf))
})!
case var .shebang(text):
text.makeContiguousUTF8()
let length = text.utf8.count
return text.utf8.withContiguousStorageIfAvailable({ (buf: UnsafeBufferPointer<UInt8>) in
return body(.init(kind: .shebang, length: length, customText: buf))
})!
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,7 @@ let TRIVIAS: [Trivia] = [
Trivia(name: "GarbageText",
comment: #"Any skipped garbage text."#,
serializationCode: 12),
Trivia(name: "Shebang",
comment: #"A script command, starting with '#!'."#,
serializationCode: 13),
]
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@
extension SyntaxParser {
static func verifyNodeDeclarationHash() -> Bool {
return String(cString: swiftparse_syntax_structure_versioning_identifier()!) ==
"de72b46d41b170b5827e54c6b89a67250599521e"
"137bb8e5ef6ced69ca3b6f2873c513d57412931f"
}
}