Skip to content

Commit b80bf1c

Browse files
committed
Differentiate Shebang from Garbage
1 parent 950dba9 commit b80bf1c

File tree

4 files changed

+31
-2
lines changed

4 files changed

+31
-2
lines changed

Sources/SwiftSyntax/SourceLocation.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,8 @@ fileprivate extension TriviaPiece {
445445
body(carriageReturnLineLength)
446446
}
447447
lineLength = .zero
448-
case let .lineComment(text),
448+
case let .shebang(text),
449+
let .lineComment(text),
449450
let .docLineComment(text):
450451
// Line comments are not supposed to contain newlines.
451452
assert(!text.containsSwiftNewline(), "line comment created that contained a new-line character")

Sources/SwiftSyntax/gyb_generated/Trivia.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ public enum TriviaPiece {
4747
case docBlockComment(String)
4848
/// Any skipped garbage text.
4949
case garbageText(String)
50+
/// A script command, starting with '#!'.
51+
case shebang(String)
5052
}
5153

5254
extension TriviaPiece: TextOutputStreamable {
@@ -83,6 +85,8 @@ extension TriviaPiece: TextOutputStreamable {
8385
target.write(text)
8486
case let .garbageText(text):
8587
target.write(text)
88+
case let .shebang(text):
89+
target.write(text)
8690
}
8791
}
8892
}
@@ -115,6 +119,8 @@ extension TriviaPiece: CustomDebugStringConvertible {
115119
return "docBlockComment(\(name))"
116120
case .garbageText(let name):
117121
return "garbageText(\(name))"
122+
case .shebang(let name):
123+
return "shebang(\(name))"
118124
}
119125
}
120126
}
@@ -249,6 +255,11 @@ public struct Trivia {
249255
public static func garbageText(_ text: String) -> Trivia {
250256
return [.garbageText(text)]
251257
}
258+
259+
/// Returns a piece of trivia for Shebang.
260+
public static func shebang(_ text: String) -> Trivia {
261+
return [.shebang(text)]
262+
}
252263
}
253264

254265
extension Trivia: Equatable {}
@@ -314,6 +325,8 @@ extension TriviaPiece {
314325
return SourceLength(of: text)
315326
case let .garbageText(text):
316327
return SourceLength(of: text)
328+
case let .shebang(text):
329+
return SourceLength(of: text)
317330
}
318331
}
319332
}
@@ -346,6 +359,8 @@ extension TriviaPiece {
346359
return .docBlockComment(.fromBuffer(textBuffer))
347360
case 12:
348361
return .garbageText(.fromBuffer(textBuffer))
362+
case 13:
363+
return .shebang(.fromBuffer(textBuffer))
349364
default:
350365
fatalError("unexpected trivia piece kind \(piece.kind)")
351366
}
@@ -377,6 +392,8 @@ extension TriviaPiece {
377392
return true
378393
case 12:
379394
return true
395+
case 13:
396+
return true
380397
default:
381398
fatalError("unexpected trivia piece kind \(kind)")
382399
}
@@ -409,6 +426,8 @@ internal enum TriviaPieceKind: CTriviaKind {
409426
case docBlockComment = 11
410427
/// Any skipped garbage text.
411428
case garbageText = 12
429+
/// A script command, starting with '#!'.
430+
case shebang = 13
412431

413432
static func fromRawValue(_ rawValue: CTriviaKind) -> TriviaPieceKind {
414433
return TriviaPieceKind(rawValue: rawValue)!
@@ -471,6 +490,12 @@ extension TriviaPiece {
471490
return text.utf8.withContiguousStorageIfAvailable({ (buf: UnsafeBufferPointer<UInt8>) in
472491
return body(.init(kind: .garbageText, length: length, customText: buf))
473492
})!
493+
case var .shebang(text):
494+
text.makeContiguousUTF8()
495+
let length = text.utf8.count
496+
return text.utf8.withContiguousStorageIfAvailable({ (buf: UnsafeBufferPointer<UInt8>) in
497+
return body(.init(kind: .shebang, length: length, customText: buf))
498+
})!
474499
}
475500
}
476501
}

Sources/SwiftSyntaxBuilderGeneration/gyb_generated/Trivia.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,4 +138,7 @@ let TRIVIAS: [Trivia] = [
138138
Trivia(name: "GarbageText",
139139
comment: #"Any skipped garbage text."#,
140140
serializationCode: 12),
141+
Trivia(name: "Shebang",
142+
comment: #"A script command, starting with '#!'."#,
143+
serializationCode: 13),
141144
]

Sources/SwiftSyntaxParser/gyb_generated/NodeDeclarationHash.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@
1717
extension SyntaxParser {
1818
static func verifyNodeDeclarationHash() -> Bool {
1919
return String(cString: swiftparse_syntax_structure_versioning_identifier()!) ==
20-
"de72b46d41b170b5827e54c6b89a67250599521e"
20+
"137bb8e5ef6ced69ca3b6f2873c513d57412931f"
2121
}
2222
}

0 commit comments

Comments
 (0)