Skip to content

Commit aac93ed

Browse files
committed
Address code review comments
1 parent 9bd340c commit aac93ed

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

Sources/SwiftSyntax/SyntaxParser.swift

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,16 @@ public enum SyntaxParser {
6464
/// - Parameters:
6565
/// - source: The source string to parse.
6666
/// - 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
6770
/// - Returns: A top-level Syntax node representing the contents of the tree,
6871
/// if the parse was successful.
6972
/// - Throws: `ParserError`
7073
public static func parse(
7174
source: String,
72-
file: String = "",
7375
parseTransition: IncrementalParseTransition? = nil,
76+
filenameForDiagnostics: String = "",
7477
diagnosticEngine: DiagnosticEngine? = nil
7578
) throws -> SourceFileSyntax {
7679
guard nodeHashVerifyResult else {
@@ -82,7 +85,8 @@ public enum SyntaxParser {
8285
var utf8Source = source
8386
utf8Source.makeNativeUTF8IfNeeded()
8487

85-
let rawSyntax = parseRaw(file, utf8Source, parseTransition, diagnosticEngine)
88+
let rawSyntax = parseRaw(utf8Source, parseTransition, filenameForDiagnostics,
89+
diagnosticEngine)
8690

8791
guard let file = makeSyntax(.forRoot(rawSyntax)) as? SourceFileSyntax else {
8892
throw ParserError.invalidSyntaxData
@@ -94,6 +98,8 @@ public enum SyntaxParser {
9498
///
9599
/// - Parameters:
96100
/// - url: The file URL to parse.
101+
/// - diagnosticEngine: Optional diagnotic engine to where the parser will
102+
/// emit diagnostics
97103
/// - Returns: A top-level Syntax node representing the contents of the tree,
98104
/// if the parse was successful.
99105
/// - Throws: `ParserError`
@@ -105,14 +111,14 @@ public enum SyntaxParser {
105111
let source = fileData.withUnsafeBytes { (ptr: UnsafePointer<CChar>) in
106112
return String(cString: ptr)
107113
}
108-
return try parse(source: source, file: url.absoluteString,
114+
return try parse(source: source, filenameForDiagnostics: url.absoluteString,
109115
diagnosticEngine: diagnosticEngine)
110116
}
111117

112118
private static func parseRaw(
113-
_ file: String,
114119
_ source: String,
115120
_ parseTransition: IncrementalParseTransition?,
121+
_ filenameForDiagnostics: String,
116122
_ diagnosticEngine: DiagnosticEngine?
117123
) -> RawSyntax {
118124
assert(source.isNativeUTF8)
@@ -159,7 +165,7 @@ public enum SyntaxParser {
159165
// If requested, we should set up a source location converter to calculate
160166
// line and column.
161167
let converter = diagnosticEngine.needsLineColumn ?
162-
SourceLocationConverter(file: file, source: source) : nil
168+
SourceLocationConverter(file: filenameForDiagnostics, source: source) : nil
163169
let diagHandler = { (diag: CDiagnostic!) in
164170
// If the coming diagnostic is a note, we cache the pending note
165171
if swiftparse_diagnostic_get_severity(diag) ==

0 commit comments

Comments
 (0)