Skip to content

Commit 01bb752

Browse files
committed
Rename swift-parser-test to swift-parser-cli
swift-parser-test was easily confusable with the SwiftParserTest test target for newcomers. Also, rename the `print-tree` subcommand to `tree` and `dump-diags` to `diags`. That’s a little shorter to type and you don’t need to remember which of the two commands was `dump` and which one was `print`.
1 parent 4f2e153 commit 01bb752

File tree

4 files changed

+45
-20
lines changed

4 files changed

+45
-20
lines changed

Package.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ let package = Package(
142142
dependencies: ["SwiftSyntax", "SwiftSyntaxParser"]
143143
),
144144
.executableTarget(
145-
name: "swift-parser-test",
145+
name: "swift-parser-cli",
146146
dependencies: ["SwiftDiagnostics", "SwiftSyntax", "SwiftParser",
147147
"SwiftOperators",
148148
.product(name: "ArgumentParser", package: "swift-argument-parser")]

Sources/SwiftParser/SwiftParser.docc/FilingBugReports.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
Guide to provide steps for filing actionable bug reports for parser failures.
44

5-
Reducing a test case requires the `swift-parser-test` utility that you can build by checking out `swift-syntax` and running `swift build --product swift-parser-test` or building the `swift-parser-test` target in Xcode.
5+
Reducing a test case requires the `swift-parser-cli` utility that you can build by checking out `swift-syntax` and running `swift build --product swift-parser-cli` or building the `swift-parser-cli` target in Xcode.
66

77
## Round-Trip Failure or Parser Crash
88

99
If you encounter a case where printing the parsed syntax tree does not reproduce the original source file, that’s a round-tripping failure and is considered a serious bug in SwiftSyntax – preserving the original source is a core principle of this library. To reproduce and reduce the failure, perform the following steps
1010

1111
1. Reduce the failure by running
1212
```
13-
swift-parser-test reduce /pth/to/file/that/does/not/roundtrip.swift
13+
swift-parser-cli reduce /pth/to/file/that/does/not/roundtrip.swift
1414
```
1515
2. File a bug report on <https://github.com/apple/swift-syntax/issues/new/choose> with the reduced source code or fix the issue yourself by following the steps in <doc:FixingBugs>.
1616
@@ -20,7 +20,7 @@ If you have source code that is parsed correctly by the current Swift compiler (
2020
2121
1. Run the following command to see the diagnostics produced by the parser
2222
```
23-
swift-parser-test print-diags /path/to/file.swift
23+
swift-parser-cli print-diags /path/to/file.swift
2424
```
2525
2. Remove as much code as possible from your test file and check if it still produces the same diagnostic.
2626
3. File a bug report on <https://github.com/apple/swift-syntax/issues/new/choose> with the reduced source code or fix the issue yourself by following the steps in <doc:FixingBugs>.
@@ -31,7 +31,7 @@ If you have valid source code that produced a syntax tree which doesn’t repres
3131
3232
1. Run the following command to print the parsed syntax tree
3333
```
34-
swift-parser-test dump-tree /path/to/file.swift
34+
swift-parser-cli print-tree /path/to/file.swift
3535
```
3636
2. Remove as much code as possible from your test file and check if it still produces the same invalid tree
3737
3. File a bug report on <https://github.com/apple/swift-syntax/issues/new/choose> with the reduced source code or fix the issue yourself by following the steps in <doc:FixingBugs>.

Sources/SwiftParser/SwiftParser.docc/FixingBugs.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ Unhelpful diagnostics can result from two reasons:
4444

4545
To distinguish these cases run the following command and look at the dumped syntax tree. Use your own judgment to decide whether this models the intended meaning of the source code reasonably well.
4646
```
47-
swift-parser-test print-tree /path/to/file/with/unhelpful/diagnostic.swift
47+
swift-parser-cli print-tree /path/to/file/with/unhelpful/diagnostic.swift
4848
```
4949
5050
Fixing the first case where the parser does not recover according to the user’s intent is similar to [Parse of Valid Source Code Produced an Invalid Syntax Tree](#Parse-of-Valid-Source-Code-Produced-an-Invalid-Syntax-Tree). See <doc:SwiftParser/ParserRecovery> for documentation how parser recovery works and determine how to recover better from the invalid source code.

Sources/swift-parser-test/swift-parser-test.swift Sources/swift-parser-cli/swift-parser-cli.swift

+39-14
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//===------------ main.swift - Entry point for swift-parser-test ----------===//
1+
//===--- swift-parser-cli.swift - Entry point for swift-parser-cli --------===//
22
//
33
// This source file is part of the Swift.org open source project
44
//
@@ -28,7 +28,7 @@ func printerr(_ message: String, terminator: String = "\n") {
2828

2929
private func withTemporaryFile<T>(contents: [UInt8], body: (URL) throws -> T) throws -> T {
3030
var tempFileURL = FileManager.default.temporaryDirectory
31-
tempFileURL.appendPathComponent("swift-parser-test-\(UUID().uuidString).swift")
31+
tempFileURL.appendPathComponent("swift-parser-cli-\(UUID().uuidString).swift")
3232
try Data(contents).write(to: tempFileURL)
3333
defer {
3434
try? FileManager.default.removeItem(at: tempFileURL)
@@ -47,16 +47,6 @@ private func getContentsOfSourceFile(at path: String?) throws -> [UInt8] {
4747
return [UInt8](source)
4848
}
4949

50-
@main
51-
class SwiftParserTest: ParsableCommand {
52-
required init() {}
53-
54-
static var configuration = CommandConfiguration(
55-
abstract: "Utility to test SwiftSyntax syntax tree creation.",
56-
subcommands: [VerifyRoundTrip.self, DumpTree.self, PrintDiags.self, Reduce.self]
57-
)
58-
}
59-
6050
/// Fold all of the sequences in the given source file.
6151
func foldAllSequences(_ tree: SourceFileSyntax) -> (Syntax, [Diagnostic]) {
6252
var diags: [Diagnostic] = []
@@ -70,9 +60,29 @@ func foldAllSequences(_ tree: SourceFileSyntax) -> (Syntax, [Diagnostic]) {
7060
return (resultTree, diags)
7161
}
7262

63+
@main
64+
class SwiftParserCli: ParsableCommand {
65+
required init() {}
66+
67+
static var configuration = CommandConfiguration(
68+
abstract: "Utility to test SwiftSyntax syntax tree creation.",
69+
subcommands: [
70+
PrintDiags.self,
71+
PrintTree.self,
72+
Reduce.self,
73+
VerifyRoundTrip.self,
74+
]
75+
)
76+
}
77+
7378
class VerifyRoundTrip: ParsableCommand {
7479
required init() {}
7580

81+
static var configuration = CommandConfiguration(
82+
commandName: "verify-round-trip",
83+
abstract: "Verify that printing the parsed syntax tree produces the original source"
84+
)
85+
7686
init(sourceFile: String?, swiftVersion: String?, enableBareSlashRegex: Bool?) {
7787
self.sourceFile = sourceFile
7888
self.swiftVersion = swiftVersion
@@ -139,6 +149,11 @@ class VerifyRoundTrip: ParsableCommand {
139149
}
140150

141151
class PrintDiags: ParsableCommand {
152+
static var configuration = CommandConfiguration(
153+
commandName: "print-diags",
154+
abstract: "Print the diagnostics produced by parsing a soruce file"
155+
)
156+
142157
required init() {}
143158

144159
@Argument(help: "The source file that should be parsed; if omitted, use stdin")
@@ -178,7 +193,12 @@ class PrintDiags: ParsableCommand {
178193
}
179194
}
180195

181-
class DumpTree: ParsableCommand {
196+
class PrintTree: ParsableCommand {
197+
static var configuration = CommandConfiguration(
198+
commandName: "print-tree",
199+
abstract: "Print the syntax tree produced by parsing a source file"
200+
)
201+
182202
required init() {}
183203

184204
@Argument(help: "The source file that should be parsed; if omitted, use stdin")
@@ -217,6 +237,11 @@ class DumpTree: ParsableCommand {
217237
}
218238

219239
class Reduce: ParsableCommand {
240+
static var configuration = CommandConfiguration(
241+
commandName: "reduce",
242+
abstract: "Reduce a test case that crashes the parser or fails to round-trip to a smaller test case that still reproduces the issue"
243+
)
244+
220245
required init() {}
221246

222247
@Argument(help: "The test case that should be reduced; if omitted, use stdin")
@@ -257,7 +282,7 @@ class Reduce: ParsableCommand {
257282
case potentialCrash
258283
}
259284

260-
/// Invoke `swift-parser-test verify-round-trip` with the same arguments as this `reduce` subcommand.
285+
/// Invoke `swift-parser-cli verify-round-trip` with the same arguments as this `reduce` subcommand.
261286
/// Returns the exit code of the invocation.
262287
private func runVerifyRoundTripInSeparateProcess(source: [UInt8]) throws -> ProcessExit {
263288
return try withTemporaryFile(contents: source) { tempFileURL in

0 commit comments

Comments
 (0)