Skip to content

Move SyntaxTraits from gyb to codegen #1187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ struct GenerateSwiftSyntax: ParsableCommand {
TemplateSpec(sourceFile: syntaxBaseNodesFile, module: swiftSyntaxDir, filename: "SyntaxBaseNodes.swift"),
TemplateSpec(sourceFile: syntaxEnumFile, module: swiftSyntaxDir, filename: "SyntaxEnum.swift"),
TemplateSpec(sourceFile: syntaxKindFile, module: swiftSyntaxDir, filename: "SyntaxKind.swift"),
TemplateSpec(sourceFile: syntaxTraitsFile, module: swiftSyntaxDir, filename: "SyntaxTraits.swift"),
TemplateSpec(sourceFile: buildableCollectionNodesFile, module: swiftSyntaxBuilderDir, filename: "BuildableCollectionNodes.swift"),
TemplateSpec(sourceFile: buildableNodesFile, module: swiftSyntaxBuilderDir, filename: "BuildableNodes.swift"),
TemplateSpec(sourceFile: resultBuildersFile, module: swiftSyntaxBuilderDir, filename: "ResultBuilders.swift"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import SwiftSyntax
import SwiftSyntaxBuilder
import SyntaxSupport
import Utils

let syntaxTraitsFile = SourceFileSyntax {
for trait in TRAITS {
ProtocolDeclSyntax("""
// MARK: - \(trait.traitName)Syntax

public protocol \(trait.traitName)Syntax: SyntaxProtocol
""") {

for child in trait.children {
VariableDeclSyntax("var \(raw: child.swiftName): \(raw: child.typeName)\(raw: child.isOptional ? "?" : "") { get }")
FunctionDeclSyntax("func with\(raw: child.name)(_ newChild: \(raw: child.typeName)?) -> Self")
}
}

ExtensionDeclSyntax("public extension SyntaxProtocol") {
FunctionDeclSyntax("""
/// Check whether the non-type erased version of this syntax node conforms to
/// `\(raw: trait.traitName)Syntax`.
/// Note that this will incur an existential conversion.
func isProtocol(_: \(raw: trait.traitName)Syntax.Protocol) -> Bool {
return self.asProtocol(\(raw: trait.traitName)Syntax.self) != nil
}
""")

FunctionDeclSyntax("""
/// Return the non-type erased version of this syntax node if it conforms to
/// `\(raw: trait.traitName)Syntax`. Otherwise return `nil`.
/// Note that this will incur an existential conversion.
func asProtocol(_: \(raw: trait.traitName)Syntax.Protocol) -> \(raw: trait.traitName)Syntax? {
return Syntax(self).asProtocol(SyntaxProtocol.self) as? \(raw: trait.traitName)Syntax
}
""")
}
}

for node in SYNTAX_NODES where !node.isBase && !node.traits.isEmpty {
ExtensionDeclSyntax("extension \(raw: node.name): \(raw: node.traits.map { $0 + "Syntax" }.joined(separator: ", ")) {}")
}
}
1 change: 0 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ let package = Package(
"SyntaxNodes.swift.gyb.template",
"SyntaxRewriter.swift.gyb",
"SyntaxTransform.swift.gyb",
"SyntaxTraits.swift.gyb",
"SyntaxVisitor.swift.gyb",
"TokenKind.swift.gyb",
"Tokens.swift.gyb",
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftSyntax/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ add_swift_host_library(SwiftSyntax
gyb_generated/SyntaxFactory.swift
generated/SyntaxKind.swift
gyb_generated/SyntaxRewriter.swift
gyb_generated/SyntaxTraits.swift
generated/SyntaxTraits.swift
gyb_generated/SyntaxTransform.swift
gyb_generated/SyntaxVisitor.swift
gyb_generated/TokenKind.swift
Expand Down
64 changes: 0 additions & 64 deletions Sources/SwiftSyntax/SyntaxTraits.swift.gyb

This file was deleted.

Loading