Skip to content

Commit ba9ad40

Browse files
committed
Move TypeAttribute to SwiftSyntaxBuilder
1 parent a820410 commit ba9ad40

25 files changed

+1339
-1221
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: [

CodeGeneration/Sources/SyntaxSupport/AttributeKinds.swift

Lines changed: 941 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
public func generateCopyrightHeader(for generator: String) -> String {
14+
return """
15+
//// Automatically Generated by \(generator)
16+
//// Do Not Edit Directly!
17+
//===----------------------------------------------------------------------===//
18+
//
19+
// This source file is part of the Swift.org open source project
20+
//
21+
// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
22+
// Licensed under Apache License v2.0 with Runtime Library Exception
23+
//
24+
// See https://swift.org/LICENSE.txt for license information
25+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
26+
//
27+
//===----------------------------------------------------------------------===//
28+
29+
30+
"""
31+
}

CodeGeneration/Sources/generate-swiftbasicformat/BasicFormatFile.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import Utils
1818

1919
let basicFormatFile = SourceFile {
2020
ImportDecl(
21-
leadingTrivia: .docLineComment(copyrightHeader),
21+
leadingTrivia: .docLineComment(generateCopyrightHeader(for: "generate-swiftbasicformat")),
2222
path: [AccessPathComponent(name: "SwiftSyntax")]
2323
)
2424

CodeGeneration/Sources/generate-swiftbasicformat/SyntaxUtilities.swift

Lines changed: 0 additions & 29 deletions
This file was deleted.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
(declarationModifierFile, "DeclarationModifier.swift"),
31+
(declarationAttributeFile, "DeclarationAttribute.swift"),
32+
],
33+
destination: URL(fileURLWithPath: generatedPath),
34+
verbose: verbose
35+
)
36+
}
37+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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 declarationAttributeFile = SourceFile {
20+
ImportDecl(
21+
"""
22+
\(generateCopyrightHeader(for: "generate-swiftparser"))
23+
@_spi(RawSyntax) import SwiftSyntax
24+
25+
"""
26+
)
27+
28+
ExtensionDecl("extension Parser") {
29+
EnumDecl("enum DeclarationAttribute: SyntaxText, ContextualKeywords") {
30+
for attribute in DECL_ATTR_KINDS {
31+
EnumCaseDecl("case \(attribute.swiftName) = \"\(attribute.name)\"")
32+
}
33+
EnumCaseDecl(#"case _spi_available = "_spi_available""#)
34+
}
35+
}
36+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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 declarationModifierFile = SourceFile {
20+
ImportDecl(
21+
"""
22+
\(generateCopyrightHeader(for: "generate-swiftparser"))
23+
@_spi(RawSyntax) import SwiftSyntax
24+
25+
"""
26+
)
27+
28+
EnumDecl("enum DeclarationModifier: SyntaxText, ContextualKeywords, RawTokenKindSubset") {
29+
for attribute in DECL_MODIFIER_KINDS {
30+
EnumCaseDecl("case \(attribute.swiftName) = \"\(attribute.name)\"")
31+
}
32+
InitializerDecl("init?(lexeme: Lexer.Lexeme)") {
33+
SwitchStmt(switchKeyword: .switch, expression: Expr("lexeme.tokenKind")) {
34+
// % for attr in DECL_MODIFIER_KINDS:
35+
// % if attr.swift_name.endswith('Keyword'):
36+
// case .${attr.swift_name}: self = .${attr.swift_name}
37+
// % end
38+
// % end
39+
// case .identifier: self.init(rawValue: lexeme.tokenText)
40+
// default: return nil
41+
}
42+
}
43+
44+
VariableDecl(
45+
name: IdentifierPattern("rawTokenKind"),
46+
type: TypeAnnotation(
47+
colon: .colon,
48+
type: SimpleTypeIdentifier("RawTokenKind")
49+
)
50+
) {
51+
SwitchStmt(switchKeyword: .switch, expression: Expr("self")) {
52+
for attribute in DECL_MODIFIER_KINDS {
53+
SwitchCase("case .\(attribute.swiftName):") {
54+
if attribute.swiftName.hasSuffix("Keyword") {
55+
ReturnStmt("return .\(attribute.swiftName)")
56+
} else {
57+
ReturnStmt("return .identifier")
58+
}
59+
60+
}
61+
}
62+
}
63+
}
64+
65+
66+
VariableDecl(
67+
name: IdentifierPattern("contextualKeyword"),
68+
type: TypeAnnotation(
69+
colon: .colon,
70+
type: OptionalType("SyntaxText?")
71+
)
72+
) {
73+
SwitchStmt(switchKeyword: .switch, expression: Expr("self")) {
74+
for attribute in DECL_MODIFIER_KINDS where !attribute.swiftName.hasSuffix("Keyword") {
75+
SwitchCase("case .\(attribute.swiftName):") {
76+
// ReturnStmt("return \(attribute.name)")
77+
}
78+
}
79+
}
80+
}
81+
82+
// var contextualKeyword: SyntaxText? {
83+
// switch self {
84+
// % for attr in DECL_MODIFIER_KINDS:
85+
// % if not attr.swift_name.endswith('Keyword'):
86+
// case .${attr.swift_name}: return "${attr.name}"
87+
// % end
88+
// % end
89+
// default: return nil
90+
// }
91+
// }
92+
}
93+
}
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
%{
2-
from gyb_syntax_support import *
3-
from gyb_syntax_support.AttributeKinds import *
4-
# -*- mode: Swift -*-
5-
# Ignore the following admonition it applies to the resulting .swift file only
6-
}%
7-
//// Automatically Generated From TypeAttribute.swift.gyb.
8-
//// Do Not Edit Directly!
91
//===----------------------------------------------------------------------===//
102
//
113
// This source file is part of the Swift.org open source project
@@ -18,12 +10,26 @@
1810
//
1911
//===----------------------------------------------------------------------===//
2012

21-
@_spi(RawSyntax) import SwiftSyntax
13+
import Foundation
14+
import SwiftSyntax
15+
import SwiftSyntaxBuilder
16+
import SyntaxSupport
17+
import Utils
2218

23-
extension Parser {
24-
enum TypeAttribute: SyntaxText, ContextualKeywords {
25-
% for attr in TYPE_ATTR_KINDS:
26-
case ${attr.swift_name} = "${attr.name}"
27-
% end
19+
let typeAttributeFile = SourceFile {
20+
ImportDecl(
21+
"""
22+
\(generateCopyrightHeader(for: "generate-swiftparser"))
23+
@_spi(RawSyntax) import SwiftSyntax
24+
25+
"""
26+
)
27+
28+
ExtensionDecl("extension Parser") {
29+
EnumDecl("enum TypeAttribute: SyntaxText, ContextualKeywords") {
30+
for attribute in TYPE_ATTR_KINDS {
31+
EnumCaseDecl("case \(attribute.name) = \"\(attribute.name)\"")
32+
}
33+
}
2834
}
2935
}

CodeGeneration/Sources/generate-swiftsyntaxbuilder/BuildableCollectionNodesFile.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import SwiftBasicFormat
1919

2020
let buildableCollectionNodesFile = SourceFile {
2121
ImportDecl(
22-
leadingTrivia: .docLineComment(copyrightHeader),
22+
leadingTrivia: .docLineComment(generateCopyrightHeader(for: "generate-swiftsyntaxbuilder")),
2323
path: [AccessPathComponent(name: "SwiftSyntax")]
2424
)
2525

CodeGeneration/Sources/generate-swiftsyntaxbuilder/BuildableNodesFile.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import Utils
1818

1919
let buildableNodesFile = SourceFile {
2020
ImportDecl(
21-
leadingTrivia: .docLineComment(copyrightHeader),
21+
leadingTrivia: .docLineComment(generateCopyrightHeader(for: "generate-swiftsyntaxbuilder")),
2222
path: [AccessPathComponent(name: "SwiftSyntax")]
2323
)
2424

CodeGeneration/Sources/generate-swiftsyntaxbuilder/CopyrightHeader.swift

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

CodeGeneration/Sources/generate-swiftsyntaxbuilder/ResultBuildersFile.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import Utils
1818

1919
let resultBuildersFile = SourceFile {
2020
ImportDecl(
21-
leadingTrivia: .docLineComment(copyrightHeader),
21+
leadingTrivia: .docLineComment(generateCopyrightHeader(for: "generate-swiftsyntaxbuilder")),
2222
path: [AccessPathComponent(name: "SwiftSyntax")]
2323
)
2424
for node in SYNTAX_NODES where node.isSyntaxCollection {

CodeGeneration/Sources/generate-swiftsyntaxbuilder/TokenFile.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import Utils
1818

1919
let tokenFile = SourceFile {
2020
ImportDecl(
21-
leadingTrivia: .docLineComment(copyrightHeader),
21+
leadingTrivia: .docLineComment(generateCopyrightHeader(for: "generate-swiftsyntaxbuilder")),
2222
path: [AccessPathComponent(name: "SwiftSyntax")]
2323
)
2424

CodeGeneration/Sources/generate-swiftsyntaxbuilder/TypealiasesFile.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import SwiftBasicFormat
1919

2020
let typealiasesFile = SourceFile {
2121
ImportDecl(
22-
leadingTrivia: .docLineComment(copyrightHeader),
22+
leadingTrivia: .docLineComment(generateCopyrightHeader(for: "generate-swiftsyntaxbuilder")),
2323
path: [AccessPathComponent(name: "SwiftSyntax")]
2424
)
2525

Package.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@ let package = Package(
113113
exclude: [
114114
"CMakeLists.txt",
115115
"README.md",
116-
"TypeAttribute.swift.gyb",
117116
"DeclarationModifier.swift.gyb",
118117
"DeclarationAttribute.swift.gyb",
119118
]

0 commit comments

Comments
 (0)