Skip to content

Commit

Permalink
Cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
max-leuthaeuser committed Oct 25, 2023
1 parent 61e33bf commit 584e960
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 15 deletions.
9 changes: 4 additions & 5 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ let package = Package(
name: "SwiftAstGenLib",
dependencies: [
.product(name: "SwiftSyntax", package: "swift-syntax"),
.product(name: "SwiftOperators", package: "swift-syntax"),
.product(name: "SwiftParser", package: "swift-syntax"),
.product(name: "ArgumentParser", package: "swift-argument-parser"),
"CodeGeneration",
]
),
Expand All @@ -30,13 +28,14 @@ let package = Package(
dependencies: [
.product(name: "SwiftSyntax", package: "swift-syntax"),
.product(name: "SwiftSyntaxBuilder", package: "swift-syntax"),
.product(name: "SwiftOperators", package: "swift-syntax"),
.product(name: "SwiftParser", package: "swift-syntax"),
]
),
.executableTarget(
name: "SwiftAstGen",
dependencies: ["SwiftAstGenLib"],
dependencies: [
.product(name: "ArgumentParser", package: "swift-argument-parser"),
"SwiftAstGenLib",
],
swiftSettings: [
.unsafeFlags(["-cross-module-optimization"], .when(configuration: .release))
]
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ The AST is created by using SwiftSyntax.

```bash
> SwiftAstGen -h
USAGE: swift-ast-gen [--src <src>] [--output <output>] [--prettyPrint]
USAGE: swift-ast-gen [--src <src>] [--output <output>] [--prettyPrint] [--scalaAstOnly]

OPTIONS:
-i, --src <src> Source directory (default: `.`).
-o, --output <output> Output directory for generated AST json files (default: `./ast_out`).
-p, --prettyPrint Pretty print the generated AST json files (default: `false`).
-p, --prettyPrint Pretty print the generated AST json files.
-s, --scalaAstOnly Only print the generated Scala SwiftSyntax AST nodes.
-h, --help Show help information.
```

Expand Down
4 changes: 2 additions & 2 deletions Sources/SwiftAstGen/SwiftAstGen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ struct SwiftAstGen: ParsableCommand {

@Flag(
name: [.customLong("prettyPrint"), .customShort("p")],
help: "Pretty print the generated AST json files (default: `false`).")
help: "Pretty print the generated AST json files.")
var prettyPrint: Bool = false

@Flag(
name: [.customLong("scalaAstOnly"), .customShort("s")],
help: "Only print the generated Scala SwiftSyntax AST nodes (default: `false`).")
help: "Only print the generated Scala SwiftSyntax AST nodes.")
var scalaAstOnly: Bool = false

func validate() throws {
Expand Down
7 changes: 1 addition & 6 deletions Sources/SwiftAstGenLib/ScalaAstGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public class ScalaAstGenerator {
let returnType = isOptional ? "Option[Value]" : "Value"
let returnAccess = isOptional ? "" : ".head"
return
"\tdef \(name): \(returnType) = json(\"children\").arr.toList.find(_(\"name\").str == \"\(name)\")\(returnAccess)"
"\tdef \(name): \(returnType) = json(\"children\").arr.toList.find(_(\"name\").str == \"\(child.varOrCaseName)\")\(returnAccess)"
}.joined(separator: "\n\t")
} else {
childrenString = "\tdef children: Seq[Value] = json(\"children\").arr.toList"
Expand All @@ -101,9 +101,6 @@ public class ScalaAstGenerator {
var containedInDocString = String(describing: node.containedIn)
containedInDocString = containedInDocString.replacingOccurrences(of: "\n", with: "\n\t")

var subTypesDocString = String(describing: node.subtypes)
subTypesDocString = subTypesDocString.replacingOccurrences(of: "\n", with: "\n\t")

return """
\n\t/// ### Documentation
\t///
Expand All @@ -112,8 +109,6 @@ public class ScalaAstGenerator {
\t\(childrenDocString)
\t///
\t\(containedInDocString.isEmpty ? "/// ### Nowhere contained in" : containedInDocString)
\t///
\t\(subTypesDocString.isEmpty ? "/// ### No Subtypes" : subTypesDocString)
\tcase class \(syntaxType)(json: Value) \(inheritsString) {
\(childrenString)
\t}
Expand Down

0 comments on commit 584e960

Please sign in to comment.