Skip to content

Commit a6321dd

Browse files
committed
Allow specifying simple diagnostic messages in an enum
1 parent d6fa844 commit a6321dd

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

Sources/SwiftParser/Diagnostics/ParseDiagnosticsGenerator.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
4747
diagnostics.append(Diagnostic(node: Syntax(node), message: message))
4848
}
4949

50+
/// Produce a diagnostic.
51+
private func addDiagnostic<T: SyntaxProtocol>(_ node: T, _ message: StaticParserError) {
52+
addDiagnostic(node, message as DiagnosticMessage)
53+
}
54+
5055
/// If a diagnostic is generated that covers multiple syntax nodes, mark them as handles so they don't produce the generic diagnostics anymore.
5156
private func markNodesAsHandled(_ nodes: SyntaxIdentifier...) {
5257
handledNodes.append(contentsOf: nodes)
@@ -95,7 +100,7 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
95100
if let unexpectedCondition = node.body.unexpectedBeforeLeftBrace {
96101
// Detect C-style for loops based on two semicolons which could not be parsed between the 'for' keyword and the '{'
97102
if unexpectedCondition.tokens(withKind: .semicolon).count == 2 {
98-
addDiagnostic(node, CStyleForLoopError())
103+
addDiagnostic(node, .cStyleForLoop)
99104
markNodesAsHandled(node.inKeyword.id, unexpectedCondition.id)
100105
}
101106
}
@@ -108,7 +113,7 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
108113
}
109114
if let output = node.output, let unexpectedBeforeReturnType = output.unexpectedBetweenArrowAndReturnType {
110115
if let throwsInReturnPosition = unexpectedBeforeReturnType.tokens(withKind: .throwsKeyword).first {
111-
addDiagnostic(throwsInReturnPosition, ThrowsInReturnPositionError())
116+
addDiagnostic(throwsInReturnPosition, .throwsInReturnPosition)
112117
markNodesAsHandled(unexpectedBeforeReturnType.id, throwsInReturnPosition.id)
113118
return .visitChildren
114119
}

Sources/SwiftParser/Diagnostics/ParserDiagnosticMessages.swift

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,24 @@ public extension ParserError {
5555
}
5656
}
5757

58-
// MARK: - Diagnostics (please sort alphabetically)
58+
// MARK: - Static diagnostics
59+
60+
/// Please order the cases in this enum alphabetically by case name.
61+
public enum StaticParserError: String, DiagnosticMessage {
62+
case cStyleForLoop = "C-style for statement has been removed in Swift 3"
63+
case throwsInReturnPosition = "'throws' may only occur before '->'"
64+
65+
public var message: String { self.rawValue }
66+
67+
public var diagnosticID: MessageID {
68+
MessageID("\(type(of: self)).\(self)")
69+
}
5970

60-
public struct CStyleForLoopError: ParserError {
61-
public var message = "C-style for statement has been removed in Swift 3"
71+
public var severity: DiagnosticSeverity { .error }
6272
}
6373

74+
// MARK: - Diagnostics (please sort alphabetically)
75+
6476
public struct MissingTokenError: ParserError {
6577
public let missingToken: TokenSyntax
6678

@@ -84,10 +96,6 @@ public struct MissingTokenError: ParserError {
8496
}
8597
}
8698

87-
public struct ThrowsInReturnPositionError: ParserError {
88-
public let message = "'throws' may only occur before '->'"
89-
}
90-
9199
public struct UnexpectedNodesError: ParserError {
92100
public let unexpectedNodes: UnexpectedNodesSyntax
93101

Tests/SwiftParserTest/DiagnosticInfrastructureTests.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,8 @@ public class DiagnosticInfrastructureTests: XCTestCase {
1212
XCTAssertEqual(diag.diagnosticID, MessageID("TestDiagnostic"))
1313
XCTAssertEqual(diag.message, "My test diagnostic")
1414
XCTAssertEqual(diag.severity, .error)
15+
16+
XCTAssertEqual(StaticParserError.throwsInReturnPosition.diagnosticID, MessageID("StaticParserError.throwsInReturnPosition"))
17+
XCTAssertEqual(StaticParserError.throwsInReturnPosition.severity, .error)
1518
}
1619
}

0 commit comments

Comments
 (0)