Skip to content

Commit ed13eef

Browse files
committed
Move SyntaxTraits from gyb to codegen
1 parent 0cea8fb commit ed13eef

File tree

6 files changed

+294
-181
lines changed

6 files changed

+294
-181
lines changed

CodeGeneration/Sources/generate-swiftsyntax/GenerateSwiftSyntax.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ struct GenerateSwiftSyntax: ParsableCommand {
5959
TemplateSpec(sourceFile: syntaxBaseNodesFile, module: swiftSyntaxDir, filename: "SyntaxBaseNodes.swift"),
6060
TemplateSpec(sourceFile: syntaxEnumFile, module: swiftSyntaxDir, filename: "SyntaxEnum.swift"),
6161
TemplateSpec(sourceFile: syntaxKindFile, module: swiftSyntaxDir, filename: "SyntaxKind.swift"),
62+
TemplateSpec(sourceFile: syntaxTraitsFile, module: swiftSyntaxDir, filename: "SyntaxTraits.swift"),
6263
TemplateSpec(sourceFile: buildableCollectionNodesFile, module: swiftSyntaxBuilderDir, filename: "BuildableCollectionNodes.swift"),
6364
TemplateSpec(sourceFile: buildableNodesFile, module: swiftSyntaxBuilderDir, filename: "BuildableNodes.swift"),
6465
TemplateSpec(sourceFile: resultBuildersFile, module: swiftSyntaxBuilderDir, filename: "ResultBuilders.swift"),
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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 syntaxTraitsFile = SourceFileSyntax {
19+
for trait in TRAITS {
20+
ProtocolDeclSyntax("""
21+
// MARK: - \(trait.traitName)Syntax
22+
23+
public protocol \(trait.traitName)Syntax: SyntaxProtocol
24+
""") {
25+
26+
for child in trait.children {
27+
VariableDeclSyntax("var \(raw: child.swiftName): \(raw: child.typeName)\(raw: child.isOptional ? "?" : "") { get }")
28+
FunctionDeclSyntax("func with\(raw: child.name)(_ newChild: \(raw: child.typeName)?) -> Self")
29+
}
30+
}
31+
32+
ExtensionDeclSyntax("public extension SyntaxProtocol") {
33+
FunctionDeclSyntax("""
34+
/// Check whether the non-type erased version of this syntax node conforms to
35+
/// `\(raw: trait.traitName)Syntax`.
36+
/// Note that this will incur an existential conversion.
37+
func isProtocol(_: \(raw: trait.traitName)Syntax.Protocol) -> Bool {
38+
return self.asProtocol(\(raw: trait.traitName)Syntax.self) != nil
39+
}
40+
""")
41+
42+
FunctionDeclSyntax("""
43+
/// Return the non-type erased version of this syntax node if it conforms to
44+
/// `\(raw: trait.traitName)Syntax`. Otherwise return `nil`.
45+
/// Note that this will incur an existential conversion.
46+
func asProtocol(_: \(raw: trait.traitName)Syntax.Protocol) -> \(raw: trait.traitName)Syntax? {
47+
return Syntax(self).asProtocol(SyntaxProtocol.self) as? \(raw: trait.traitName)Syntax
48+
}
49+
""")
50+
}
51+
}
52+
53+
for node in SYNTAX_NODES where !node.isBase && !node.traits.isEmpty {
54+
ExtensionDeclSyntax("extension \(raw: node.name): \(raw: node.traits.map { $0 + "Syntax" }.joined(separator: ", ")) {}")
55+
}
56+
}

Package.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ let package = Package(
7070
"SyntaxNodes.swift.gyb.template",
7171
"SyntaxRewriter.swift.gyb",
7272
"SyntaxTransform.swift.gyb",
73-
"SyntaxTraits.swift.gyb",
7473
"SyntaxVisitor.swift.gyb",
7574
"TokenKind.swift.gyb",
7675
"Tokens.swift.gyb",

Sources/SwiftSyntax/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ add_swift_host_library(SwiftSyntax
3939
gyb_generated/SyntaxFactory.swift
4040
generated/SyntaxKind.swift
4141
gyb_generated/SyntaxRewriter.swift
42-
gyb_generated/SyntaxTraits.swift
42+
generated/SyntaxTraits.swift
4343
gyb_generated/SyntaxTransform.swift
4444
gyb_generated/SyntaxVisitor.swift
4545
gyb_generated/TokenKind.swift

Sources/SwiftSyntax/SyntaxTraits.swift.gyb

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

0 commit comments

Comments
 (0)