Skip to content

Allow swift-parser-test to take input from stdin. #804

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 16, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions Sources/swift-parser-test/swift-parser-test.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,14 @@ private func withTemporaryFile<T>(contents: [UInt8], body: (URL) throws -> T) th
return try body(tempFileURL)
}

private func getContentsOfSourceFile(at path: String) throws -> [UInt8] {
let sourceURL = URL(fileURLWithPath: path)
let source = try Data(contentsOf: sourceURL)
private func getContentsOfSourceFile(at path: String?) throws -> [UInt8] {
let source: Data
if let path = path {
let sourceURL = URL(fileURLWithPath: path)
source = try Data(contentsOf: sourceURL)
} else {
source = FileHandle.standardInput.readDataToEndOfFile()
}
return [UInt8](source)
}

Expand All @@ -54,14 +59,14 @@ class SwiftParserTest: ParsableCommand {
class VerifyRoundTrip: ParsableCommand {
required init() {}

init(sourceFile: String, swiftVersion: String?, enableBareSlashRegex: Bool?) {
init(sourceFile: String?, swiftVersion: String?, enableBareSlashRegex: Bool?) {
self.sourceFile = sourceFile
self.swiftVersion = swiftVersion
self.enableBareSlashRegex = enableBareSlashRegex
}

@Argument(help: "The source file that should be parsed")
var sourceFile: String
@Argument(help: "The source file that should be parsed; if omitted, use stdin")
var sourceFile: String?

@Option(name: .long, help: "Interpret input according to a specific Swift language version number")
var swiftVersion: String?
Expand Down Expand Up @@ -109,8 +114,8 @@ class VerifyRoundTrip: ParsableCommand {
class PrintDiags: ParsableCommand {
required init() {}

@Argument(help: "The source file that should be parsed")
var sourceFile: String
@Argument(help: "The source file that should be parsed; if omitted, use stdin")
var sourceFile: String?

@Option(name: .long, help: "Interpret input according to a specific Swift language version number")
var swiftVersion: String?
Expand Down Expand Up @@ -141,8 +146,8 @@ class PrintDiags: ParsableCommand {
class DumpTree: ParsableCommand {
required init() {}

@Argument(help: "The source file that should be parsed")
var sourceFile: String
@Argument(help: "The source file that should be parsed; if omitted, use stdin")
var sourceFile: String?

@Option(name: .long, help: "Interpret input according to a specific Swift language version number")
var swiftVersion: String?
Expand All @@ -167,8 +172,8 @@ class DumpTree: ParsableCommand {
class Reduce: ParsableCommand {
required init() {}

@Argument(help: "The test case that should be reduced")
var sourceFile: String
@Argument(help: "The test case that should be reduced; if omitted, use stdin")
var sourceFile: String?

@Option(name: .long, help: "Interpret input according to a specific Swift language version number")
var swiftVersion: String?
Expand Down