Skip to content

Commit cecf603

Browse files
committed
Address review comments
1 parent 509f005 commit cecf603

File tree

8 files changed

+22
-17
lines changed

8 files changed

+22
-17
lines changed

Sources/SwiftParser/Diagnostics/ParserDiagnosticMessages.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,15 +150,16 @@ public struct MissingNodeError: ParserError {
150150
if let parent = missingNode.parent,
151151
let childName = parent.childNameForDiagnostics(missingNode.index) {
152152
message = "Expected \(childName)"
153-
if let parent = missingNode.parent,
154-
let parentTypeName = parent.nodeTypeNameForDiagnostics(inherit: false) {
153+
if let parentTypeName = parent.nodeTypeNameForDiagnostics(inherit: false) {
155154
message += " of \(parentTypeName)"
156155
hasNamedParent = true
157156
}
158157
} else {
159158
message = "Expected \(missingNode.nodeTypeNameForDiagnostics() ?? "syntax")"
160-
if let lastChild = missingNode.lastToken(viewMode: .fixedUp), lastChild.presence == .present {
161-
message += " after '\(lastChild.text)'"
159+
if let missingDecl = missingNode.as(MissingDeclSyntax.self), let lastModifier = missingDecl.modifiers?.last {
160+
message += " after '\(lastModifier.name.text)' modifier"
161+
} else if let missingDecl = missingNode.as(MissingDeclSyntax.self), missingDecl.attributes != nil {
162+
message += " after attribute"
162163
} else if let previousToken = missingNode.previousToken(viewMode: .fixedUp), previousToken.presence == .present {
163164
message += " after '\(previousToken.text)'"
164165
}

Sources/SwiftParser/Directives.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ extension Parser {
8989
do {
9090
var elementsProgress = LoopProgressCondition()
9191
while !self.at(any: [.eof, .poundElseKeyword, .poundElseifKeyword, .poundEndifKeyword]) && elementsProgress.evaluate(currentToken) {
92-
guard let element = parseElement(&self), element.raw.byteLength > 0 else {
92+
guard let element = parseElement(&self), !element.isEmpty else {
9393
break
9494
}
9595
elements.append(element)

Sources/SwiftParser/Expressions.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1620,7 +1620,7 @@ extension Parser {
16201620
let element = RawArrayElementSyntax(
16211621
expression: el, trailingComma: comma, arena: self.arena
16221622
)
1623-
if element.raw.byteLength == 0 {
1623+
if element.isEmpty {
16241624
break COLLECTION_LOOP
16251625
} else {
16261626
elements.append(RawSyntax(element))
@@ -1634,7 +1634,7 @@ extension Parser {
16341634
trailingComma: comma,
16351635
arena: self.arena
16361636
)
1637-
if element.raw.byteLength == 0 {
1637+
if element.isEmpty {
16381638
break COLLECTION_LOOP
16391639
} else {
16401640
elements.append(RawSyntax(element))

Sources/SwiftParser/TopLevel.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ extension Parser {
119119
let item = self.parseItem(isAtTopLevel: isAtTopLevel)
120120
let semi = self.consume(if: .semicolon)
121121

122-
if item.raw.byteLength == 0 && semi == nil {
122+
if item.isEmpty && semi == nil {
123123
return nil
124124
}
125125
return .init(item: item, semicolon: semi, errorTokens: nil, arena: self.arena)

Sources/SwiftSyntax/Raw/RawSyntaxNodeProtocol.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ public extension RawSyntaxNodeProtocol {
4242
func write<Target>(to target: inout Target) where Target : TextOutputStream {
4343
raw.write(to: &target)
4444
}
45+
46+
var isEmpty: Bool {
47+
return raw.byteLength == 0
48+
}
4549
}
4650

4751
/// `RawSyntax` itself conforms to `RawSyntaxNodeProtocol`.

Tests/SwiftParserTest/Attributes.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ final class AttributeTests: XCTestCase {
1111
}
1212
""",
1313
diagnostics: [
14-
DiagnosticSpec(locationMarker: "DIAG_2", message: "Expected declaration"),
14+
DiagnosticSpec(locationMarker: "DIAG_2", message: "Expected declaration after attribute"),
1515
DiagnosticSpec(locationMarker: "DIAG_1", message: "Expected 'for' in attribute argument"),
1616
DiagnosticSpec(locationMarker: "DIAG_1", message: "Expected ':' in attribute argument"),
1717
DiagnosticSpec(locationMarker: "DIAG_2", message: "Expected ')' to end attribute"),
@@ -42,20 +42,20 @@ final class AttributeTests: XCTestCase {
4242
@_specialize(e#^DIAG^#
4343
""",
4444
diagnostics: [
45-
DiagnosticSpec(message: "Expected declaration"),
45+
DiagnosticSpec(message: "Expected declaration after attribute"),
4646
DiagnosticSpec(message: "Expected ':' in attribute argument"),
4747
DiagnosticSpec(message: "Expected ')' to end attribute"),
4848
]
4949
)
5050
}
51-
51+
5252
func testMultipleInvalidSpecializeParams() {
5353
AssertParse(
5454
"""
5555
@_specialize(e#^DIAG_1^#, exported#^DIAG_2^#)#^DIAG_3^#
5656
""",
5757
diagnostics: [
58-
DiagnosticSpec(locationMarker: "DIAG_3", message: "Expected declaration after ')'"),
58+
DiagnosticSpec(locationMarker: "DIAG_3", message: "Expected declaration after attribute"),
5959
DiagnosticSpec(locationMarker: "DIAG_1", message: "Expected ':' in attribute argument"),
6060
DiagnosticSpec(locationMarker: "DIAG_2", message: "Expected ':' in attribute argument"),
6161
DiagnosticSpec(locationMarker: "DIAG_2", message: "Expected 'false' in attribute argument"),

Tests/SwiftParserTest/Declarations.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ final class DeclarationTests: XCTestCase {
553553
}
554554
""",
555555
diagnostics: [
556-
DiagnosticSpec(message: "Expected declaration after 'public' in struct")
556+
DiagnosticSpec(message: "Expected declaration after 'public' modifier in struct")
557557
]
558558
)
559559
}
@@ -904,7 +904,7 @@ final class DeclarationTests: XCTestCase {
904904
diagnostics: [
905905
DiagnosticSpec(locationMarker: "OPENING_BRACE", message: "Expected '{' to start struct"),
906906
DiagnosticSpec(locationMarker: "AFTER_POUND_IF", message: "Expected condition of conditional compilation clause"),
907-
DiagnosticSpec(locationMarker: "END", message: "Expected declaration after '@' in conditional compilation clause"),
907+
DiagnosticSpec(locationMarker: "END", message: "Expected declaration after attribute in conditional compilation clause"),
908908
DiagnosticSpec(locationMarker: "END", message: "Expected name of attribute"),
909909
DiagnosticSpec(locationMarker: "END", message: "Expected '#endif' in conditional compilation block"),
910910
DiagnosticSpec(locationMarker: "END", message: "Expected '}' to end struct")

Tests/SwiftParserTest/Directives.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,9 @@ final class DirectiveTests: XCTestCase {
148148
}
149149
""",
150150
diagnostics: [
151-
DiagnosticSpec(locationMarker: "DIAG_1", message: "Expected declaration after 'foo' in conditional compilation clause"),
152-
DiagnosticSpec(locationMarker: "DIAG_2", message: "Expected declaration after 'foo' in conditional compilation clause"),
153-
DiagnosticSpec(locationMarker: "DIAG_3", message: "Expected declaration after 'frozen' in conditional compilation clause"),
151+
DiagnosticSpec(locationMarker: "DIAG_1", message: "Expected declaration after attribute in conditional compilation clause"),
152+
DiagnosticSpec(locationMarker: "DIAG_2", message: "Expected declaration after attribute in conditional compilation clause"),
153+
DiagnosticSpec(locationMarker: "DIAG_3", message: "Expected declaration after attribute in conditional compilation clause"),
154154
]
155155
)
156156
}

0 commit comments

Comments
 (0)