@@ -58,16 +58,16 @@ public enum SyntaxParser {
58
58
/// - source: The source string to parse.
59
59
/// - parseTransition: Optional mechanism for incremental re-parsing.
60
60
/// - 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
63
63
/// - Returns: A top-level Syntax node representing the contents of the tree,
64
64
/// if the parse was successful.
65
65
/// - Throws: `ParserError`
66
66
public static func parse(
67
67
source: String ,
68
68
parseTransition: IncrementalParseTransition ? = nil ,
69
69
filenameForDiagnostics: String = " " ,
70
- diagnosticEngine : DiagnosticEngine ? = nil
70
+ diagnosticHandler : ( ( Diagnostic ) -> Void ) ? = nil
71
71
) throws -> SourceFileSyntax {
72
72
guard nodeHashVerifyResult && cnodeLayoutHashVerifyResult else {
73
73
throw ParserError . parserCompatibilityCheckFailed
@@ -79,7 +79,7 @@ public enum SyntaxParser {
79
79
utf8Source. makeContiguousUTF8 ( )
80
80
81
81
let rawSyntax = parseRaw ( utf8Source, parseTransition, filenameForDiagnostics,
82
- diagnosticEngine )
82
+ diagnosticHandler )
83
83
84
84
let base = _SyntaxParserInterop. nodeFromRetainedOpaqueRawSyntax ( rawSyntax)
85
85
guard let file = base. as ( SourceFileSyntax . self) else {
@@ -92,27 +92,27 @@ public enum SyntaxParser {
92
92
///
93
93
/// - Parameters:
94
94
/// - 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
97
97
/// - Returns: A top-level Syntax node representing the contents of the tree,
98
98
/// if the parse was successful.
99
99
/// - Throws: `ParserError`
100
100
public static func parse( _ url: URL ,
101
- diagnosticEngine : DiagnosticEngine ? = nil ) throws -> SourceFileSyntax {
101
+ diagnosticHandler : ( ( Diagnostic ) -> Void ) ? = nil ) throws -> SourceFileSyntax {
102
102
// Avoid using `String(contentsOf:)` because it creates a wrapped NSString.
103
103
let fileData = try Data ( contentsOf: url)
104
104
let source = fileData. withUnsafeBytes { buf in
105
105
return String ( decoding: buf. bindMemory ( to: UInt8 . self) , as: UTF8 . self)
106
106
}
107
107
return try parse ( source: source, filenameForDiagnostics: url. path,
108
- diagnosticEngine : diagnosticEngine )
108
+ diagnosticHandler : diagnosticHandler )
109
109
}
110
110
111
111
private static func parseRaw(
112
112
_ source: String ,
113
113
_ parseTransition: IncrementalParseTransition ? ,
114
114
_ filenameForDiagnostics: String ,
115
- _ diagnosticEngine : DiagnosticEngine ?
115
+ _ diagnosticHandler : ( ( Diagnostic ) -> Void ) ?
116
116
) -> CClientNode {
117
117
precondition ( source. isContiguousUTF8)
118
118
let c_parser = swiftparse_parser_create ( )
@@ -144,18 +144,15 @@ public enum SyntaxParser {
144
144
var pendingNotes : [ Note ] = [ ]
145
145
defer {
146
146
// Consume the pending diagnostic if present
147
- if let diagnosticEngine = diagnosticEngine {
147
+ if let diagnosticHandler = diagnosticHandler {
148
148
if let pendingDiag = pendingDiag {
149
- diagnosticEngine . diagnose ( Diagnostic ( pendingDiag, pendingNotes) )
149
+ diagnosticHandler ( Diagnostic ( pendingDiag, pendingNotes) )
150
150
}
151
151
}
152
152
}
153
153
// 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)
159
156
let diagHandler = { ( diag: CDiagnostic!) in
160
157
// If the coming diagnostic is a note, we cache the pending note
161
158
if swiftparse_diagnostic_get_severity ( diag) ==
@@ -164,7 +161,7 @@ public enum SyntaxParser {
164
161
} else {
165
162
// Cosume the pending diagnostic
166
163
if let pendingDiag = pendingDiag {
167
- diagnosticEngine . diagnose ( Diagnostic ( pendingDiag, pendingNotes) )
164
+ diagnosticHandler ( Diagnostic ( pendingDiag, pendingNotes) )
168
165
// Clear pending notes
169
166
pendingNotes = [ ]
170
167
}
0 commit comments