Skip to content

Commit b8e4a69

Browse files
authored
Merge pull request #325 from ahoppen/pr/remove-diagnosticengine
Remove `DiagnosticEngine`
2 parents f371d47 + 198582f commit b8e4a69

File tree

10 files changed

+76
-429
lines changed

10 files changed

+76
-429
lines changed

Changelog.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Note: This is in reverse chronological order, so newer entries are added to the
88
* The `SwiftSyntaxParser` type and a few related types now live in their own module (also named `SwiftSyntaxParser`). This allows using `SwiftSyntax` for code generation purposes without having a compatible `_InternalSwiftSyntaxParser.dylib` around.
99

1010
`import SwiftSyntaxParser` where necessary.
11+
* `DiagnosticEngine` has been removed. Instead, `SyntaxParser` takes a closure through which it emits parser diagnostics. Depending on your needs, use the closure to handle the diagnostics or write + hook up your own diagnostics engine.
1112

1213
## Swift 5.3
1314

Sources/SwiftSyntax/DiagnosticConsumer.swift

-33
This file was deleted.

Sources/SwiftSyntax/DiagnosticEngine.swift

-102
This file was deleted.

Sources/SwiftSyntax/JSONDiagnosticConsumer.swift

-79
This file was deleted.

Sources/SwiftSyntax/PrintingDiagnosticConsumer.swift

-59
This file was deleted.

Sources/SwiftSyntax/Diagnostic.swift renamed to Sources/SwiftSyntaxParser/Diagnostic.swift

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
// This file provides the Diagnostic, Note, and FixIt types.
1313
//===----------------------------------------------------------------------===//
1414

15+
import SwiftSyntax
16+
1517
/// A FixIt represents a change to source code in order to "correct" a
1618
/// diagnostic.
1719
public enum FixIt: Codable, CustomDebugStringConvertible {
@@ -117,7 +119,7 @@ public struct Note: Codable {
117119
}
118120

119121
/// Converts this Note to a Diagnostic for serialization.
120-
func asDiagnostic() -> Diagnostic {
122+
public func asDiagnostic() -> Diagnostic {
121123
return Diagnostic(message: message, location: location, notes: [],
122124
highlights: highlights, fixIts: fixIts)
123125
}

Sources/SwiftSyntaxParser/SyntaxParser.swift

+14-17
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,16 @@ public enum SyntaxParser {
5858
/// - source: The source string to parse.
5959
/// - parseTransition: Optional mechanism for incremental re-parsing.
6060
/// - filenameForDiagnostics: Optional file name used for SourceLocation.
61-
/// - diagnosticEngine: Optional diagnotic engine to where the parser will
62-
/// emit diagnostics
61+
/// - diagnosticHandler: Optional callback that will be called for all
62+
/// diagnostics the parser emits
6363
/// - Returns: A top-level Syntax node representing the contents of the tree,
6464
/// if the parse was successful.
6565
/// - Throws: `ParserError`
6666
public static func parse(
6767
source: String,
6868
parseTransition: IncrementalParseTransition? = nil,
6969
filenameForDiagnostics: String = "",
70-
diagnosticEngine: DiagnosticEngine? = nil
70+
diagnosticHandler: ((Diagnostic) -> Void)? = nil
7171
) throws -> SourceFileSyntax {
7272
guard nodeHashVerifyResult && cnodeLayoutHashVerifyResult else {
7373
throw ParserError.parserCompatibilityCheckFailed
@@ -79,7 +79,7 @@ public enum SyntaxParser {
7979
utf8Source.makeContiguousUTF8()
8080

8181
let rawSyntax = parseRaw(utf8Source, parseTransition, filenameForDiagnostics,
82-
diagnosticEngine)
82+
diagnosticHandler)
8383

8484
let base = _SyntaxParserInterop.nodeFromRetainedOpaqueRawSyntax(rawSyntax)
8585
guard let file = base.as(SourceFileSyntax.self) else {
@@ -92,27 +92,27 @@ public enum SyntaxParser {
9292
///
9393
/// - Parameters:
9494
/// - url: The file URL to parse.
95-
/// - diagnosticEngine: Optional diagnotic engine to where the parser will
96-
/// emit diagnostics
95+
/// - diagnosticHandler: Optional callback that will be called for all
96+
/// diagnostics the parser emits
9797
/// - Returns: A top-level Syntax node representing the contents of the tree,
9898
/// if the parse was successful.
9999
/// - Throws: `ParserError`
100100
public static func parse(_ url: URL,
101-
diagnosticEngine: DiagnosticEngine? = nil) throws -> SourceFileSyntax {
101+
diagnosticHandler: ((Diagnostic) -> Void)? = nil) throws -> SourceFileSyntax {
102102
// Avoid using `String(contentsOf:)` because it creates a wrapped NSString.
103103
let fileData = try Data(contentsOf: url)
104104
let source = fileData.withUnsafeBytes { buf in
105105
return String(decoding: buf.bindMemory(to: UInt8.self), as: UTF8.self)
106106
}
107107
return try parse(source: source, filenameForDiagnostics: url.path,
108-
diagnosticEngine: diagnosticEngine)
108+
diagnosticHandler: diagnosticHandler)
109109
}
110110

111111
private static func parseRaw(
112112
_ source: String,
113113
_ parseTransition: IncrementalParseTransition?,
114114
_ filenameForDiagnostics: String,
115-
_ diagnosticEngine: DiagnosticEngine?
115+
_ diagnosticHandler: ((Diagnostic) -> Void)?
116116
) -> CClientNode {
117117
precondition(source.isContiguousUTF8)
118118
let c_parser = swiftparse_parser_create()
@@ -144,18 +144,15 @@ public enum SyntaxParser {
144144
var pendingNotes: [Note] = []
145145
defer {
146146
// Consume the pending diagnostic if present
147-
if let diagnosticEngine = diagnosticEngine {
147+
if let diagnosticHandler = diagnosticHandler {
148148
if let pendingDiag = pendingDiag {
149-
diagnosticEngine.diagnose(Diagnostic(pendingDiag, pendingNotes))
149+
diagnosticHandler(Diagnostic(pendingDiag, pendingNotes))
150150
}
151151
}
152152
}
153153
// Set up diagnostics consumer if requested by the caller.
154-
if let diagnosticEngine = diagnosticEngine {
155-
// If requested, we should set up a source location converter to calculate
156-
// line and column.
157-
let converter = diagnosticEngine.needsLineColumn ?
158-
SourceLocationConverter(file: filenameForDiagnostics, source: source) : nil
154+
if let diagnosticHandler = diagnosticHandler {
155+
let converter = SourceLocationConverter(file: filenameForDiagnostics, source: source)
159156
let diagHandler = { (diag: CDiagnostic!) in
160157
// If the coming diagnostic is a note, we cache the pending note
161158
if swiftparse_diagnostic_get_severity(diag) ==
@@ -164,7 +161,7 @@ public enum SyntaxParser {
164161
} else {
165162
// Cosume the pending diagnostic
166163
if let pendingDiag = pendingDiag {
167-
diagnosticEngine.diagnose(Diagnostic(pendingDiag, pendingNotes))
164+
diagnosticHandler(Diagnostic(pendingDiag, pendingNotes))
168165
// Clear pending notes
169166
pendingNotes = []
170167
}

0 commit comments

Comments
 (0)