Skip to content

Commit cf04988

Browse files
committed
setting up argument parser
1 parent 18fe915 commit cf04988

File tree

4 files changed

+75
-55
lines changed

4 files changed

+75
-55
lines changed

Package.resolved

Lines changed: 10 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ let package = Package(
2525
],
2626
dependencies: [
2727
.package(url: "https://github.com/apple/swift-syntax.git", from: "601.0.1"),
28-
.package(url: "https://github.com/swiftlang/swift-docc-plugin", from: "1.4.0")
28+
.package(url: "https://github.com/swiftlang/swift-docc-plugin", from: "1.4.0"),
29+
.package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.5.1")
2930
],
3031
targets: [
3132
.target(
@@ -38,7 +39,10 @@ let package = Package(
3839
),
3940
.executableTarget(
4041
name: "skit",
41-
dependencies: ["SyntaxKit"]
42+
dependencies: [
43+
"SyntaxKit",
44+
.product(name: "ArgumentParser", package: "swift-argument-parser")
45+
]
4246
),
4347
.testTarget(
4448
name: "SyntaxKitTests",

Sources/skit/Skit.swift

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
//
2+
// Skit.swift
3+
// SyntaxKit
4+
//
5+
// Created by Leo Dion on 6/26/25.
6+
//
7+
8+
import ArgumentParser
9+
import Foundation
10+
import SyntaxKit
11+
12+
@main
13+
struct Skit:ParsableCommand {
14+
static let configuration: CommandConfiguration = .init(
15+
subcommands: [
16+
Parse.self,
17+
Generate.self
18+
],
19+
defaultSubcommand: Generate.self
20+
)
21+
}
22+
23+
struct Parse: ParsableCommand {
24+
func run() throws {
25+
// Read Swift code from stdin
26+
let code =
27+
String(data: FileHandle.standardInput.readDataToEndOfFile(), encoding: .utf8) ?? ""
28+
29+
do {
30+
// Parse the code using SyntaxKit
31+
let response = try SyntaxParser.parse(code: code, options: ["fold"])
32+
33+
// Output the JSON to stdout
34+
print(response.syntaxJSON)
35+
} catch {
36+
// If there's an error, output it as JSON
37+
let errorResponse = ["error": error.localizedDescription]
38+
if let jsonData = try? JSONSerialization.data(withJSONObject: errorResponse),
39+
let jsonString = String(data: jsonData, encoding: .utf8)
40+
{
41+
print(jsonString)
42+
}
43+
}
44+
45+
}
46+
}
47+
48+
49+
struct Generate: ParsableCommand {
50+
func run() throws {
51+
let dsl =
52+
String(data: FileHandle.standardInput.readDataToEndOfFile(), encoding: .utf8) ?? ""
53+
54+
let code = try SyntaxKitGenerator.generateCode(from: dsl)
55+
56+
print(code)
57+
58+
}
59+
}

Sources/skit/main.swift

Lines changed: 0 additions & 52 deletions
This file was deleted.

0 commit comments

Comments
 (0)