Skip to content

Commit 72bc211

Browse files
committed
Move TypeAttribute to SwiftSyntaxBuilder
1 parent 6e3dfb3 commit 72bc211

18 files changed

+1343
-1234
lines changed

CodeGeneration/Package.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ let package = Package(
1010
products: [
1111
.executable(name: "generate-swiftbasicformat", targets: ["generate-swiftbasicformat"]),
1212
.executable(name: "generate-swiftideutils", targets: ["generate-swiftideutils"]),
13+
.executable(name: "generate-swiftparser", targets: ["generate-swiftparser"]),
1314
.executable(name: "generate-swiftsyntaxbuilder", targets: ["generate-swiftsyntaxbuilder"]),
1415
],
1516
dependencies: [
@@ -36,6 +37,16 @@ let package = Package(
3637
"SyntaxSupport",
3738
"Utils"
3839
]
40+
),
41+
.executableTarget(
42+
name: "generate-swiftparser",
43+
dependencies: [
44+
.product(name: "SwiftSyntax", package: "swift-syntax"),
45+
.product(name: "SwiftSyntaxBuilder", package: "swift-syntax"),
46+
.product(name: "ArgumentParser", package: "swift-argument-parser"),
47+
"SyntaxSupport",
48+
"Utils"
49+
]
3950
),
4051
.executableTarget(
4152
name: "generate-swiftsyntaxbuilder",

CodeGeneration/Sources/SyntaxSupport/AttributeKinds.swift

Lines changed: 942 additions & 0 deletions
Large diffs are not rendered by default.
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: 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 SwiftSyntax
14+
import SwiftSyntaxBuilder
15+
import SyntaxSupport
16+
import Utils
17+
18+
let declarationAttributeFile = SourceFile {
19+
ImportDecl(
20+
"""
21+
\(generateCopyrightHeader(for: "generate-swiftparser"))
22+
@_spi(RawSyntax) import SwiftSyntax
23+
24+
"""
25+
)
26+
27+
ExtensionDecl("extension Parser") {
28+
EnumDecl("enum DeclarationAttribute: SyntaxText, ContextualKeywords") {
29+
for attribute in DECL_ATTR_KINDS {
30+
EnumCaseDecl("case \(attribute.swiftName) = \"\(attribute.name)\"")
31+
}
32+
EnumCaseDecl(#"case _spi_available = "_spi_available""#)
33+
}
34+
}
35+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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 SwiftSyntax
14+
import SwiftSyntaxBuilder
15+
import SyntaxSupport
16+
import Utils
17+
18+
let declarationModifierFile = SourceFile {
19+
ImportDecl(
20+
"""
21+
\(generateCopyrightHeader(for: "generate-swiftparser"))
22+
@_spi(RawSyntax) import SwiftSyntax
23+
24+
"""
25+
)
26+
27+
EnumDecl("enum DeclarationModifier: SyntaxText, ContextualKeywords, RawTokenKindSubset") {
28+
for attribute in DECL_MODIFIER_KINDS {
29+
EnumCaseDecl("case \(attribute.swiftName) = \"\(attribute.name)\"")
30+
}
31+
InitializerDecl("init?(lexeme: Lexer.Lexeme)") {
32+
SwitchStmt(switchKeyword: .switch, expression: Expr("lexeme.tokenKind")) {
33+
for attribute in DECL_MODIFIER_KINDS where attribute.swiftName.hasSuffix("Keyword") {
34+
SwitchCase("case .\(attribute.swiftName):") {
35+
SequenceExpr("self = .\(attribute.swiftName)")
36+
}
37+
}
38+
SwitchCase("case .identifier:") {
39+
FunctionCallExpr("self.init(rawValue: lexeme.tokenText)")
40+
}
41+
SwitchCase("default:") {
42+
ReturnStmt("return nil")
43+
}
44+
}
45+
}
46+
47+
VariableDecl(
48+
name: IdentifierPattern("rawTokenKind"),
49+
type: TypeAnnotation(
50+
colon: .colon,
51+
type: SimpleTypeIdentifier("RawTokenKind")
52+
)
53+
) {
54+
SwitchStmt(switchKeyword: .switch, expression: Expr("self")) {
55+
for attribute in DECL_MODIFIER_KINDS {
56+
SwitchCase("case .\(attribute.swiftName):") {
57+
if attribute.swiftName.hasSuffix("Keyword") {
58+
ReturnStmt("return .\(attribute.swiftName)")
59+
} else {
60+
ReturnStmt("return .identifier")
61+
}
62+
}
63+
}
64+
}
65+
}
66+
67+
68+
VariableDecl(
69+
name: IdentifierPattern("contextualKeyword"),
70+
type: TypeAnnotation(
71+
colon: .colon,
72+
type: OptionalType("SyntaxText?")
73+
)
74+
) {
75+
SwitchStmt(switchKeyword: .switch, expression: Expr("self")) {
76+
for attribute in DECL_MODIFIER_KINDS where !attribute.swiftName.hasSuffix("Keyword") {
77+
SwitchCase("case .\(attribute.swiftName):") {
78+
ReturnStmt("return \"\(attribute.name)\"")
79+
}
80+
}
81+
82+
SwitchCase("default:") {
83+
ReturnStmt("return nil")
84+
}
85+
}
86+
}
87+
}
88+
}
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,25 @@
1810
//
1911
//===----------------------------------------------------------------------===//
2012

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

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

Package.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,7 @@ let package = Package(
111111
dependencies: ["SwiftDiagnostics", "SwiftSyntax"],
112112
exclude: [
113113
"CMakeLists.txt",
114-
"DeclarationModifier.swift.gyb",
115-
"DeclarationAttribute.swift.gyb",
116-
"Parser+Entry.swift.gyb",
117114
"README.md",
118-
"TypeAttribute.swift.gyb",
119115
]
120116
),
121117
.target(

Sources/SwiftParser/DeclarationAttribute.swift.gyb

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

Sources/SwiftParser/DeclarationModifier.swift.gyb

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

0 commit comments

Comments
 (0)