@@ -64,13 +64,16 @@ public enum SyntaxParser {
64
64
/// - Parameters:
65
65
/// - source: The source string to parse.
66
66
/// - parseTransition: Optional mechanism for incremental re-parsing.
67
+ /// - filenameForDiagnostics: Optional file name used for SourceLocation.
68
+ /// - diagnosticEngine: Optional diagnotic engine to where the parser will
69
+ /// emit diagnostics
67
70
/// - Returns: A top-level Syntax node representing the contents of the tree,
68
71
/// if the parse was successful.
69
72
/// - Throws: `ParserError`
70
73
public static func parse(
71
74
source: String ,
72
- file: String = " " ,
73
75
parseTransition: IncrementalParseTransition ? = nil ,
76
+ filenameForDiagnostics: String = " " ,
74
77
diagnosticEngine: DiagnosticEngine ? = nil
75
78
) throws -> SourceFileSyntax {
76
79
guard nodeHashVerifyResult else {
@@ -82,7 +85,8 @@ public enum SyntaxParser {
82
85
var utf8Source = source
83
86
utf8Source. makeNativeUTF8IfNeeded ( )
84
87
85
- let rawSyntax = parseRaw ( file, utf8Source, parseTransition, diagnosticEngine)
88
+ let rawSyntax = parseRaw ( utf8Source, parseTransition, filenameForDiagnostics,
89
+ diagnosticEngine)
86
90
87
91
guard let file = makeSyntax ( . forRoot( rawSyntax) ) as? SourceFileSyntax else {
88
92
throw ParserError . invalidSyntaxData
@@ -94,6 +98,8 @@ public enum SyntaxParser {
94
98
///
95
99
/// - Parameters:
96
100
/// - url: The file URL to parse.
101
+ /// - diagnosticEngine: Optional diagnotic engine to where the parser will
102
+ /// emit diagnostics
97
103
/// - Returns: A top-level Syntax node representing the contents of the tree,
98
104
/// if the parse was successful.
99
105
/// - Throws: `ParserError`
@@ -105,14 +111,14 @@ public enum SyntaxParser {
105
111
let source = fileData. withUnsafeBytes { ( ptr: UnsafePointer < CChar > ) in
106
112
return String ( cString: ptr)
107
113
}
108
- return try parse ( source: source, file : url. absoluteString,
114
+ return try parse ( source: source, filenameForDiagnostics : url. absoluteString,
109
115
diagnosticEngine: diagnosticEngine)
110
116
}
111
117
112
118
private static func parseRaw(
113
- _ file: String ,
114
119
_ source: String ,
115
120
_ parseTransition: IncrementalParseTransition ? ,
121
+ _ filenameForDiagnostics: String ,
116
122
_ diagnosticEngine: DiagnosticEngine ?
117
123
) -> RawSyntax {
118
124
assert ( source. isNativeUTF8)
@@ -159,7 +165,7 @@ public enum SyntaxParser {
159
165
// If requested, we should set up a source location converter to calculate
160
166
// line and column.
161
167
let converter = diagnosticEngine. needsLineColumn ?
162
- SourceLocationConverter ( file: file , source: source) : nil
168
+ SourceLocationConverter ( file: filenameForDiagnostics , source: source) : nil
163
169
let diagHandler = { ( diag: CDiagnostic!) in
164
170
// If the coming diagnostic is a note, we cache the pending note
165
171
if swiftparse_diagnostic_get_severity ( diag) ==
0 commit comments