Skip to content

Commit 0164cbe

Browse files
committed
Move TypeAttribute to SwiftSyntaxBuilder
1 parent f4f791e commit 0164cbe

File tree

11 files changed

+307
-53
lines changed

11 files changed

+307
-53
lines changed

CodeGeneration/Package.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ let package = Package(
99
],
1010
products: [
1111
.executable(name: "generate-swiftbasicformat", targets: ["generate-swiftbasicformat"]),
12+
.executable(name: "generate-swiftparser", targets: ["generate-swiftparser"]),
1213
.executable(name: "generate-swiftsyntaxbuilder", targets: ["generate-swiftsyntaxbuilder"]),
1314
],
1415
dependencies: [
@@ -26,6 +27,16 @@ let package = Package(
2627
"Utils"
2728
]
2829
),
30+
.executableTarget(
31+
name: "generate-swiftparser",
32+
dependencies: [
33+
.product(name: "SwiftSyntax", package: "swift-syntax"),
34+
.product(name: "SwiftSyntaxBuilder", package: "swift-syntax"),
35+
.product(name: "ArgumentParser", package: "swift-argument-parser"),
36+
"SyntaxSupport",
37+
"Utils"
38+
]
39+
),
2940
.executableTarget(
3041
name: "generate-swiftsyntaxbuilder",
3142
dependencies: [
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
//// Automatically Generated From AttributeNodes.swift.gyb.
2+
//// Do Not Edit Directly!
3+
//===----------------------------------------------------------------------===//
4+
//
5+
// This source file is part of the Swift.org open source project
6+
//
7+
// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
8+
// Licensed under Apache License v2.0 with Runtime Library Exception
9+
//
10+
// See https://swift.org/LICENSE.txt for license information
11+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
public class Attribute {
16+
public let name: String
17+
public let swiftName: String?
18+
19+
public init(name: String, swiftName: String? = nil) {
20+
self.name = name
21+
self.swiftName = swiftName
22+
}
23+
}
24+
25+
public class TypeAttribute: Attribute {
26+
public init(name: String) {
27+
super.init(name: name)
28+
}
29+
}
30+
31+
public class DeclAttribute: Attribute {
32+
public let className: String
33+
public let options: [String]
34+
public let code: Int
35+
36+
public convenience init(name: String, className: String, options: String..., code: Int, swiftName: String? = nil) {
37+
self.init(name: name, className: className, options: options, code: code, swiftName: swiftName)
38+
}
39+
40+
public init(name: String, className: String, options: [String], code: Int, swiftName: String? = nil) {
41+
self.className = className
42+
self.options = options
43+
self.code = code
44+
super.init(name: name, swiftName: swiftName)
45+
}
46+
}
47+
48+
public class SimpleDeclAttribute: DeclAttribute { }
49+
50+
public class ContextualDeclAttribute: DeclAttribute {
51+
public init(name: String, className: String, options: String..., code: Int) {
52+
super.init(name: name, className: className, options: options, code: code)
53+
}
54+
}
55+
56+
public class ContextualSimpleDeclAttribute: SimpleDeclAttribute {
57+
public init(name: String, className: String, options: String..., code: Int) {
58+
super.init(name: name, className: className, options: options, code: code)
59+
}
60+
}
61+
62+
public class DeclAttributeAlias: Attribute {
63+
public let className: String
64+
65+
public init(name: String, className: String, swiftName: String? = nil) {
66+
self.className = className
67+
super.init(name: name, swiftName: swiftName)
68+
69+
}
70+
}
71+
72+
public class ContextualDeclAttributeAlias: DeclAttributeAlias { }
73+
74+
public class BuiltinDeclModifier: Attribute { }
75+
76+
// Type attributes
77+
public let TYPE_ATTR_KINDS = [
78+
TypeAttribute(name: "autoclosure"),
79+
TypeAttribute(name: "convention"),
80+
TypeAttribute(name: "noescape"),
81+
TypeAttribute(name: "escaping"),
82+
TypeAttribute(name: "differentiable"),
83+
TypeAttribute(name: "noDerivative"),
84+
TypeAttribute(name: "async"),
85+
TypeAttribute(name: "Sendable"),
86+
TypeAttribute(name: "unchecked"),
87+
TypeAttribute(name: "_local"),
88+
TypeAttribute(name: "_noMetadata"),
89+
90+
// Generated interface attributes
91+
TypeAttribute(name: "_opaqueReturnTypeOf"),
92+
]
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
//// Automatically Generated From AttributeNodes.swift.gyb.
2+
//// Do Not Edit Directly!
3+
//===----------------------------------------------------------------------===//
4+
//
5+
// This source file is part of the Swift.org open source project
6+
//
7+
// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
8+
// Licensed under Apache License v2.0 with Runtime Library Exception
9+
//
10+
// See https://swift.org/LICENSE.txt for license information
11+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
public class Attribute {
16+
public let name: String
17+
public let swiftName: String?
18+
19+
public init(name: String, swiftName: String? = nil) {
20+
self.name = name
21+
self.swiftName = swiftName
22+
}
23+
}
24+
25+
public class TypeAttribute: Attribute {
26+
public init(name: String) {
27+
super.init(name: name)
28+
}
29+
}
30+
31+
public class DeclAttribute: Attribute {
32+
public let className: String
33+
public let options: [String]
34+
public let code: Int
35+
36+
public convenience init(name: String, className: String, options: String..., code: Int, swiftName: String? = nil) {
37+
self.init(name: name, className: className, options: options, code: code, swiftName: swiftName)
38+
}
39+
40+
public init(name: String, className: String, options: [String], code: Int, swiftName: String? = nil) {
41+
self.className = className
42+
self.options = options
43+
self.code = code
44+
super.init(name: name, swiftName: swiftName)
45+
}
46+
}
47+
48+
public class SimpleDeclAttribute: DeclAttribute { }
49+
50+
public class ContextualDeclAttribute: DeclAttribute {
51+
public init(name: String, className: String, options: String..., code: Int) {
52+
super.init(name: name, className: className, options: options, code: code)
53+
}
54+
}
55+
56+
public class ContextualSimpleDeclAttribute: SimpleDeclAttribute {
57+
public init(name: String, className: String, options: String..., code: Int) {
58+
super.init(name: name, className: className, options: options, code: code)
59+
}
60+
}
61+
62+
public class DeclAttributeAlias: Attribute {
63+
public let className: String
64+
65+
public init(name: String, className: String, swiftName: String? = nil) {
66+
self.className = className
67+
super.init(name: name, swiftName: swiftName)
68+
69+
}
70+
}
71+
72+
public class ContextualDeclAttributeAlias: DeclAttributeAlias { }
73+
74+
public class BuiltinDeclModifier: Attribute { }
75+
76+
// Type attributes
77+
public let TYPE_ATTR_KINDS = [
78+
TypeAttribute(name: "autoclosure"),
79+
TypeAttribute(name: "convention"),
80+
TypeAttribute(name: "noescape"),
81+
TypeAttribute(name: "escaping"),
82+
TypeAttribute(name: "differentiable"),
83+
TypeAttribute(name: "noDerivative"),
84+
TypeAttribute(name: "async"),
85+
TypeAttribute(name: "Sendable"),
86+
TypeAttribute(name: "unchecked"),
87+
TypeAttribute(name: "_local"),
88+
TypeAttribute(name: "_noMetadata"),
89+
90+
// Generated interface attributes
91+
TypeAttribute(name: "_opaqueReturnTypeOf"),
92+
]
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import ArgumentParser
14+
import Foundation
15+
import SwiftSyntaxBuilder
16+
import Utils
17+
18+
@main
19+
struct GenerateSwiftSyntaxBuilder: ParsableCommand {
20+
@Argument(help: "The path to the destination directory where the source files are to be generated")
21+
var generatedPath: String
22+
23+
@Flag(help: "Enable verbose output")
24+
var verbose: Bool = false
25+
26+
func run() throws {
27+
try generateTemplates(
28+
templates: [
29+
(typeAttributeFile, "TypeAttribute.swift"),
30+
],
31+
destination: URL(fileURLWithPath: generatedPath),
32+
verbose: verbose
33+
)
34+
}
35+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
let copyrightHeader = """
14+
//// Automatically Generated by generate-swiftparser
15+
//// Do Not Edit Directly!
16+
//===----------------------------------------------------------------------===//
17+
//
18+
// This source file is part of the Swift.org open source project
19+
//
20+
// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
21+
// Licensed under Apache License v2.0 with Runtime Library Exception
22+
//
23+
// See https://swift.org/LICENSE.txt for license information
24+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
25+
//
26+
//===----------------------------------------------------------------------===//
27+
28+
"""
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import Foundation
14+
import SwiftSyntax
15+
import SwiftSyntaxBuilder
16+
import SyntaxSupport
17+
import Utils
18+
19+
let typeAttributeFile = SourceFile {
20+
ImportDecl(
21+
"""
22+
\(copyrightHeader)
23+
@_spi(RawSyntax) import SwiftSyntax
24+
25+
"""
26+
)
27+
28+
ExtensionDecl("Parser") {
29+
EnumDecl(
30+
identifier: "TypeAttribute",
31+
inheritanceClause: TypeInheritanceClause {
32+
InheritedType(typeName: "SyntaxText")
33+
InheritedType(typeName: "ContextualKeywords")
34+
}) {
35+
for attribute in TYPE_ATTR_KINDS {
36+
EnumCaseDecl("case \(attribute.name) = \"\(attribute.name)\"")
37+
}
38+
}
39+
}
40+
}

Package.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ let package = Package(
129129
exclude: [
130130
"CMakeLists.txt",
131131
"README.md",
132-
"TypeAttribute.swift.gyb",
133132
"DeclarationModifier.swift.gyb",
134133
"DeclarationAttribute.swift.gyb",
135134
]

Sources/SwiftParser/TypeAttribute.swift.gyb

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

Sources/SwiftParser/gyb_generated/TypeAttribute.swift renamed to Sources/SwiftParser/generated/TypeAttribute.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
//// Automatically Generated From TypeAttribute.swift.gyb.
1+
2+
//// Automatically Generated by generate-swiftparser
23
//// Do Not Edit Directly!
34
//===----------------------------------------------------------------------===//
45
//

0 commit comments

Comments
 (0)