Skip to content

Move SyntaxEnum from gyb to codegen #1151

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 2 commits into from
Dec 16, 2022
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 @@ -18,7 +18,7 @@
//
//===----------------------------------------------------------------------===//

public let SYNTAX_BASE_KINDS: Set<String> = [
public let SYNTAX_BASE_KINDS: [String] = [
% for name in SYNTAX_BASE_KINDS:
"${name}",
% end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
//
//===----------------------------------------------------------------------===//

public let SYNTAX_BASE_KINDS: Set<String> = [
public let SYNTAX_BASE_KINDS: [String] = [
"Decl",
"Expr",
"Pattern",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ struct GenerateSwiftSyntax: ParsableCommand {
(miscFile, "Misc.swift"),
(syntaxAnyVisitorFile, "SyntaxAnyVisitor.swift"),
(syntaxBaseNodesFile, "SyntaxBaseNodes.swift"),
(syntaxEnumFile, "SyntaxEnum.swift"),
(syntaxKindFile, "SyntaxKind.swift")
],
destination: URL(fileURLWithPath: generatedPath),
verbose: verbose
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//===----------------------------------------------------------------------===//
//
// 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 syntaxEnumFile = SourceFile(leadingTrivia: .docLineComment(generateCopyrightHeader(for: "generate-swiftsyntax"))) {
EnumDecl("""
/// Enum to exhaustively switch over all different syntax nodes.
@frozen // FIXME: Not actually stable, works around a miscompile
public enum SyntaxEnum
""") {
EnumCaseDecl("case token(TokenSyntax)")
for node in NON_BASE_SYNTAX_NODES {
EnumCaseDecl("case \(raw: node.swiftSyntaxKind)(\(raw: node.name))")
}
}

ExtensionDecl("""
public extension Syntax
""") {
FunctionDecl("""
/// Get an enum that can be used to exhaustively switch over all syntax nodes.
func `as`(_: SyntaxEnum.Type) -> SyntaxEnum
""") {
SwitchStmt(expression: Expr("raw.kind")) {
SwitchCase("case .token:") {
ReturnStmt("return .token(TokenSyntax(self)!)")
}

for node in NON_BASE_SYNTAX_NODES {
SwitchCase("case .\(raw: node.swiftSyntaxKind):") {
ReturnStmt("return .\(raw: node.swiftSyntaxKind)(\(raw: node.name)(self)!)")
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
//===----------------------------------------------------------------------===//
//
// 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 syntaxKindFile = SourceFile(leadingTrivia: .docLineComment(generateCopyrightHeader(for: "generate-swiftsyntax"))) {
EnumDecl("""
/// Enumerates the known kinds of Syntax represented in the Syntax tree.
@frozen // FIXME: Not actually stable, works around a miscompile
public enum SyntaxKind
""") {
EnumCaseDecl("case token")
for node in NON_BASE_SYNTAX_NODES {
EnumCaseDecl("case \(raw: node.swiftSyntaxKind)")
}

VariableDecl(
modifiers: [DeclModifier(name: .public)],
name: IdentifierPattern("isSyntaxCollection"),
type: TypeAnnotation(
colon: .colon,
type: SimpleTypeIdentifier("Bool")
)
) {
SwitchStmt(expression: Expr("self")) {
for node in SYNTAX_NODES where node.baseKind == "SyntaxCollection"{
SwitchCase("case .\(raw: node.swiftSyntaxKind):") {
ReturnStmt("return true")
}
}

SwitchCase("default:") {
ReturnStmt("return false")
}
}
}

VariableDecl(
modifiers: [DeclModifier(name: .public)],
name: IdentifierPattern("isMissing"),
type: TypeAnnotation(
colon: .colon,
type: SimpleTypeIdentifier("Bool")
)
) {
SwitchStmt(expression: Expr("self")) {
for name in SYNTAX_BASE_KINDS where !["Syntax", "SyntaxCollection"].contains(name) {
SwitchCase("case .missing\(raw: name):") {
ReturnStmt("return true")
}
}

SwitchCase("default:") {
ReturnStmt("return false")
}
}
}
}
}
2 changes: 0 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,7 @@ let package = Package(
"Raw/RawSyntaxNodes.swift.gyb",
"Raw/RawSyntaxValidation.swift.gyb",
"SyntaxCollections.swift.gyb",
"SyntaxEnum.swift.gyb",
"SyntaxFactory.swift.gyb",
"SyntaxKind.swift.gyb",
"SyntaxNodes.swift.gyb.template",
"SyntaxRewriter.swift.gyb",
"SyntaxTransform.swift.gyb",
Expand Down
4 changes: 2 additions & 2 deletions Sources/SwiftSyntax/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ add_swift_host_library(SwiftSyntax
generated/SyntaxAnyVisitor.swift
generated/SyntaxBaseNodes.swift
gyb_generated/SyntaxCollections.swift
gyb_generated/SyntaxEnum.swift
generated/SyntaxEnum.swift
gyb_generated/SyntaxFactory.swift
gyb_generated/SyntaxKind.swift
generated/SyntaxKind.swift
gyb_generated/SyntaxRewriter.swift
gyb_generated/SyntaxTraits.swift
gyb_generated/SyntaxTransform.swift
Expand Down
51 changes: 0 additions & 51 deletions Sources/SwiftSyntax/SyntaxEnum.swift.gyb

This file was deleted.

51 changes: 0 additions & 51 deletions Sources/SwiftSyntax/SyntaxKind.swift.gyb

This file was deleted.

Loading