Skip to content

Commit fe193a9

Browse files
committed
Adopt the Swift Parser
1 parent 7216a18 commit fe193a9

File tree

13 files changed

+36
-37
lines changed

13 files changed

+36
-37
lines changed

Package.swift

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ let package = Package(
4646
"SwiftFormatRules",
4747
"SwiftFormatWhitespaceLinter",
4848
.product(name: "SwiftSyntax", package: "swift-syntax"),
49-
.product(name: "SwiftSyntaxParser", package: "swift-syntax"),
49+
.product(name: "SwiftParser", package: "swift-syntax"),
5050
]
5151
),
5252
.target(
@@ -89,7 +89,7 @@ let package = Package(
8989
"SwiftFormatCore",
9090
"SwiftFormatRules",
9191
.product(name: "SwiftSyntax", package: "swift-syntax"),
92-
.product(name: "SwiftSyntaxParser", package: "swift-syntax"),
92+
.product(name: "SwiftParser", package: "swift-syntax"),
9393
]
9494
),
9595
.executableTarget(
@@ -100,6 +100,7 @@ let package = Package(
100100
"SwiftFormatCore",
101101
.product(name: "ArgumentParser", package: "swift-argument-parser"),
102102
.product(name: "SwiftSyntax", package: "swift-syntax"),
103+
.product(name: "SwiftParser", package: "swift-syntax"),
103104
.product(name: "TSCBasic", package: "swift-tools-support-core"),
104105
]
105106
),
@@ -109,7 +110,7 @@ let package = Package(
109110
dependencies: [
110111
"SwiftFormat",
111112
.product(name: "SwiftSyntax", package: "swift-syntax"),
112-
.product(name: "SwiftSyntaxParser", package: "swift-syntax"),
113+
.product(name: "SwiftParser", package: "swift-syntax"),
113114
]
114115
),
115116
.testTarget(
@@ -122,7 +123,7 @@ let package = Package(
122123
"SwiftFormatConfiguration",
123124
"SwiftFormatCore",
124125
.product(name: "SwiftSyntax", package: "swift-syntax"),
125-
.product(name: "SwiftSyntaxParser", package: "swift-syntax"),
126+
.product(name: "SwiftParser", package: "swift-syntax"),
126127
]
127128
),
128129
.testTarget(
@@ -131,7 +132,7 @@ let package = Package(
131132
"SwiftFormatTestSupport",
132133
"SwiftFormatWhitespaceLinter",
133134
.product(name: "SwiftSyntax", package: "swift-syntax"),
134-
.product(name: "SwiftSyntaxParser", package: "swift-syntax"),
135+
.product(name: "SwiftParser", package: "swift-syntax"),
135136
]
136137
),
137138
.testTarget(
@@ -143,7 +144,7 @@ let package = Package(
143144
"SwiftFormatRules",
144145
"SwiftFormatTestSupport",
145146
.product(name: "SwiftSyntax", package: "swift-syntax"),
146-
.product(name: "SwiftSyntaxParser", package: "swift-syntax"),
147+
.product(name: "SwiftParser", package: "swift-syntax"),
147148
]
148149
),
149150
.testTarget(
@@ -155,7 +156,7 @@ let package = Package(
155156
"SwiftFormatRules",
156157
"SwiftFormatTestSupport",
157158
.product(name: "SwiftSyntax", package: "swift-syntax"),
158-
.product(name: "SwiftSyntaxParser", package: "swift-syntax"),
159+
.product(name: "SwiftParser", package: "swift-syntax"),
159160
]
160161
),
161162
.testTarget(
@@ -166,7 +167,7 @@ let package = Package(
166167
"SwiftFormatTestSupport",
167168
"SwiftFormatWhitespaceLinter",
168169
.product(name: "SwiftSyntax", package: "swift-syntax"),
169-
.product(name: "SwiftSyntaxParser", package: "swift-syntax"),
170+
.product(name: "SwiftParser", package: "swift-syntax"),
170171
]
171172
),
172173
]
@@ -182,7 +183,7 @@ if ProcessInfo.processInfo.environment["SWIFTCI_USE_LOCAL_DEPS"] == nil {
182183
branch: "main"
183184
),
184185
.package(
185-
url: "https://github.com/apple/swift-syntax",
186+
url: "https://github.com/apple/swift-syntax.git",
186187
branch: "main"
187188
),
188189
.package(

Sources/SwiftFormat/SwiftFormatter.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import SwiftFormatCore
1616
import SwiftFormatPrettyPrint
1717
import SwiftFormatRules
1818
import SwiftSyntax
19-
import SwiftSyntaxParser
19+
import SwiftParser
2020

2121
/// Formats Swift source code or syntax trees according to the Swift style guidelines.
2222
public final class SwiftFormatter {
@@ -64,8 +64,8 @@ public final class SwiftFormatter {
6464
if FileManager.default.fileExists(atPath: url.path, isDirectory: &isDir), isDir.boolValue {
6565
throw SwiftFormatError.isDirectory
6666
}
67-
let sourceFile = try SyntaxParser.parse(url, diagnosticHandler: parsingDiagnosticHandler)
6867
let source = try String(contentsOf: url, encoding: .utf8)
68+
let sourceFile = try Parser.parse(source: source)
6969
try format(syntax: sourceFile, assumingFileURL: url, source: source, to: &outputStream)
7070
}
7171

@@ -87,8 +87,7 @@ public final class SwiftFormatter {
8787
to outputStream: inout Output,
8888
parsingDiagnosticHandler: ((Diagnostic) -> Void)? = nil
8989
) throws {
90-
let sourceFile =
91-
try SyntaxParser.parse(source: source, diagnosticHandler: parsingDiagnosticHandler)
90+
let sourceFile = try Parser.parse(source: source)
9291
try format(syntax: sourceFile, assumingFileURL: url, source: source, to: &outputStream)
9392
}
9493

Sources/SwiftFormat/SwiftLinter.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import SwiftFormatPrettyPrint
1717
import SwiftFormatRules
1818
import SwiftFormatWhitespaceLinter
1919
import SwiftSyntax
20-
import SwiftSyntaxParser
20+
import SwiftParser
2121

2222
/// Diagnoses and reports problems in Swift source code or syntax trees according to the Swift style
2323
/// guidelines.
@@ -62,8 +62,8 @@ public final class SwiftLinter {
6262
if FileManager.default.fileExists(atPath: url.path, isDirectory: &isDir), isDir.boolValue {
6363
throw SwiftFormatError.isDirectory
6464
}
65-
let sourceFile = try SyntaxParser.parse(url, diagnosticHandler: parsingDiagnosticHandler)
6665
let source = try String(contentsOf: url, encoding: .utf8)
66+
let sourceFile = try Parser.parse(source: source)
6767
try lint(syntax: sourceFile, assumingFileURL: url, source: source)
6868
}
6969

@@ -80,8 +80,7 @@ public final class SwiftLinter {
8080
assumingFileURL url: URL,
8181
parsingDiagnosticHandler: ((Diagnostic) -> Void)? = nil
8282
) throws {
83-
let sourceFile =
84-
try SyntaxParser.parse(source: source, diagnosticHandler: parsingDiagnosticHandler)
83+
let sourceFile = try Parser.parse(source: source)
8584
try lint(syntax: sourceFile, assumingFileURL: url, source: source)
8685
}
8786

Sources/generate-pipeline/RuleCollector.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import Foundation
1414
import SwiftFormatCore
1515
import SwiftSyntax
16-
import SwiftSyntaxParser
16+
import SwiftParser
1717

1818
/// Collects information about rules in the formatter code base.
1919
final class RuleCollector {
@@ -57,7 +57,8 @@ final class RuleCollector {
5757
guard let baseName = baseName as? String, baseName.hasSuffix(".swift") else { continue }
5858

5959
let fileURL = url.appendingPathComponent(baseName)
60-
let sourceFile = try SyntaxParser.parse(fileURL)
60+
let fileInput = try String(contentsOf: fileURL)
61+
let sourceFile = try Parser.parse(source: fileInput)
6162

6263
for statement in sourceFile.statements {
6364
guard let detectedRule = self.detectedRule(at: statement) else { continue }

Sources/swift-format/Frontend/Frontend.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import Foundation
1414
import SwiftFormat
1515
import SwiftFormatConfiguration
1616
import SwiftSyntax
17-
import SwiftSyntaxParser
17+
import SwiftParser
1818

1919
class Frontend {
2020
/// Represents a file to be processed by the frontend and any file-specific options associated

Sources/swift-format/Utilities/UnifiedDiagnosticsEngine.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
import SwiftFormatCore
1414
import SwiftSyntax
15-
import SwiftSyntaxParser
1615
import TSCBasic
1716

1817
/// Diagnostic data that retains the separation of a finding category (if present) from the rest of
@@ -46,7 +45,7 @@ final class UnifiedDiagnosticsEngine {
4645
/// Represents a location from either the linter or the syntax parser and supports converting it
4746
/// to a string representation for printing.
4847
private enum UnifiedLocation: DiagnosticLocation {
49-
/// A location received from the syntax parser.
48+
/// A location received from the swift parser.
5049
case parserLocation(SourceLocation)
5150

5251
/// A location received from the linter.

Tests/SwiftFormatCoreTests/RuleMaskTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import SwiftFormatCore
22
import SwiftSyntax
3-
import SwiftSyntaxParser
3+
import SwiftParser
44
import XCTest
55

66
final class RuleMaskTests: XCTestCase {
@@ -12,7 +12,7 @@ final class RuleMaskTests: XCTestCase {
1212
private func createMask(sourceText: String) -> RuleMask {
1313
let fileURL = URL(fileURLWithPath: "/tmp/test.swift")
1414
converter = SourceLocationConverter(file: fileURL.path, source: sourceText)
15-
let syntax = try! SyntaxParser.parse(source: sourceText)
15+
let syntax = try! Parser.parse(source: sourceText)
1616
return RuleMask(syntaxNode: Syntax(syntax), sourceLocationConverter: converter)
1717
}
1818

Tests/SwiftFormatPerformanceTests/WhitespaceLinterPerformanceTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import SwiftFormatTestSupport
22
import SwiftFormatWhitespaceLinter
33
import SwiftSyntax
4-
import SwiftSyntaxParser
4+
import SwiftParser
55
import XCTest
66

77
final class WhitespaceLinterPerformanceTests: DiagnosingTestCase {
@@ -58,7 +58,7 @@ final class WhitespaceLinterPerformanceTests: DiagnosingTestCase {
5858
private func performWhitespaceLint(input: String, expected: String) {
5959
let sourceFileSyntax: SourceFileSyntax
6060
do {
61-
sourceFileSyntax = try SyntaxParser.parse(source: input)
61+
sourceFileSyntax = try Parser.parse(source: input)
6262
} catch {
6363
XCTFail("Parsing failed with error: \(error)")
6464
return

Tests/SwiftFormatPrettyPrintTests/PrettyPrintTestCase.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import SwiftFormatCore
33
import SwiftFormatPrettyPrint
44
import SwiftFormatTestSupport
55
import SwiftSyntax
6-
import SwiftSyntaxParser
6+
import SwiftParser
77
import XCTest
88

99
class PrettyPrintTestCase: DiagnosingTestCase {
@@ -66,7 +66,7 @@ class PrettyPrintTestCase: DiagnosingTestCase {
6666
) -> String? {
6767
let sourceFileSyntax: SourceFileSyntax
6868
do {
69-
sourceFileSyntax = try SyntaxParser.parse(source: source)
69+
sourceFileSyntax = try Parser.parse(source: source)
7070
} catch {
7171
XCTFail("Parsing failed with error: \(error)")
7272
return nil

Tests/SwiftFormatPrettyPrintTests/SequenceExprFoldingTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import SwiftFormatPrettyPrint
22
import SwiftSyntax
3-
import SwiftSyntaxParser
3+
import SwiftParser
44
import XCTest
55

66
final class SequenceExprFoldingTests: XCTestCase {
@@ -248,7 +248,7 @@ final class SequenceExprFoldingTests: XCTestCase {
248248
/// - Precondition: The first code block of `source` is a statement containing
249249
/// a `SequenceExprSyntax`. All subsequent code blocks are ignored.
250250
private func sequenceExpr(_ source: String) -> SequenceExprSyntax {
251-
let sourceFileSyntax = try! SyntaxParser.parse(source: source)
251+
let sourceFileSyntax = try! Parser.parse(source: source)
252252
return sourceFileSyntax.statements.first!.item.as(SequenceExprSyntax.self)!
253253
}
254254
}

0 commit comments

Comments
 (0)