Skip to content

Commit f492984

Browse files
committed
Move SyntaxEnum from gyb to codegen
1 parent 724b5d3 commit f492984

File tree

6 files changed

+1048
-784
lines changed

6 files changed

+1048
-784
lines changed

CodeGeneration/Sources/generate-swiftsyntax/GenerateSwiftSyntax.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ struct GenerateSwiftSyntax: ParsableCommand {
2727
try generateTemplates(
2828
templates: [
2929
(miscFile, "Misc.swift"),
30+
(syntaxEnumFile, "SyntaxEnum.swift"),
3031
],
3132
destination: URL(fileURLWithPath: generatedPath),
3233
verbose: verbose
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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 syntaxEnumFile = SourceFile(leadingTrivia: .docLineComment(generateCopyrightHeader(for: "generate-swiftsyntax"))) {
19+
EnumDecl("""
20+
/// Enum to exhaustively switch over all different syntax nodes.
21+
@frozen // FIXME: Not actually stable, works around a miscompile
22+
public enum SyntaxEnum
23+
""") {
24+
EnumCaseDecl("case token(TokenSyntax)")
25+
for node in NON_BASE_SYNTAX_NODES {
26+
EnumCaseDecl("case \(raw: node.swiftSyntaxKind)(\(raw: node.name))")
27+
}
28+
}
29+
30+
ExtensionDecl("""
31+
public extension Syntax
32+
""") {
33+
FunctionDecl("""
34+
/// Get an enum that can be used to exhaustively switch over all syntax nodes.
35+
func `as`(_: SyntaxEnum.Type) -> SyntaxEnum
36+
""") {
37+
SwitchStmt(expression: Expr("raw.kind")) {
38+
SwitchCase("case .token:") {
39+
ReturnStmt("return .token(TokenSyntax(self)!)")
40+
}
41+
42+
for node in NON_BASE_SYNTAX_NODES {
43+
SwitchCase("case .\(raw: node.swiftSyntaxKind):") {
44+
ReturnStmt("return .\(raw: node.swiftSyntaxKind)(\(raw: node.name)(self)!)")
45+
}
46+
}
47+
}
48+
}
49+
}
50+
}

Package.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ let package = Package(
6868
"SyntaxAnyVisitor.swift.gyb",
6969
"SyntaxBaseNodes.swift.gyb",
7070
"SyntaxCollections.swift.gyb",
71-
"SyntaxEnum.swift.gyb",
7271
"SyntaxFactory.swift.gyb",
7372
"SyntaxKind.swift.gyb",
7473
"SyntaxNodes.swift.gyb.template",

Sources/SwiftSyntax/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ add_swift_host_library(SwiftSyntax
3535
gyb_generated/SyntaxAnyVisitor.swift
3636
gyb_generated/SyntaxBaseNodes.swift
3737
gyb_generated/SyntaxCollections.swift
38-
gyb_generated/SyntaxEnum.swift
38+
generated/SyntaxEnum.swift
3939
gyb_generated/SyntaxFactory.swift
4040
gyb_generated/SyntaxKind.swift
4141
gyb_generated/SyntaxRewriter.swift

Sources/SwiftSyntax/SyntaxEnum.swift.gyb

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

0 commit comments

Comments
 (0)