Skip to content

Commit 88cec89

Browse files
authored
Merge pull request #1151 from kimdv/kimdv/move-SyntaxEnum-to-codegen
2 parents 77aad96 + dedd6c1 commit 88cec89

File tree

11 files changed

+1727
-1129
lines changed

11 files changed

+1727
-1129
lines changed

CodeGeneration/Sources/SyntaxSupport/SyntaxBaseKinds.swift.gyb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
//
1919
//===----------------------------------------------------------------------===//
2020

21-
public let SYNTAX_BASE_KINDS: Set<String> = [
21+
public let SYNTAX_BASE_KINDS: [String] = [
2222
% for name in SYNTAX_BASE_KINDS:
2323
"${name}",
2424
% end

CodeGeneration/Sources/SyntaxSupport/gyb_generated/SyntaxBaseKinds.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15-
public let SYNTAX_BASE_KINDS: Set<String> = [
15+
public let SYNTAX_BASE_KINDS: [String] = [
1616
"Decl",
1717
"Expr",
1818
"Pattern",

CodeGeneration/Sources/generate-swiftsyntax/GenerateSwiftSyntax.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ struct GenerateSwiftSyntax: ParsableCommand {
2929
(miscFile, "Misc.swift"),
3030
(syntaxAnyVisitorFile, "SyntaxAnyVisitor.swift"),
3131
(syntaxBaseNodesFile, "SyntaxBaseNodes.swift"),
32+
(syntaxEnumFile, "SyntaxEnum.swift"),
33+
(syntaxKindFile, "SyntaxKind.swift")
3234
],
3335
destination: URL(fileURLWithPath: generatedPath),
3436
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+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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 syntaxKindFile = SourceFile(leadingTrivia: .docLineComment(generateCopyrightHeader(for: "generate-swiftsyntax"))) {
19+
EnumDecl("""
20+
/// Enumerates the known kinds of Syntax represented in the Syntax tree.
21+
@frozen // FIXME: Not actually stable, works around a miscompile
22+
public enum SyntaxKind
23+
""") {
24+
EnumCaseDecl("case token")
25+
for node in NON_BASE_SYNTAX_NODES {
26+
EnumCaseDecl("case \(raw: node.swiftSyntaxKind)")
27+
}
28+
29+
VariableDecl(
30+
modifiers: [DeclModifier(name: .public)],
31+
name: IdentifierPattern("isSyntaxCollection"),
32+
type: TypeAnnotation(
33+
colon: .colon,
34+
type: SimpleTypeIdentifier("Bool")
35+
)
36+
) {
37+
SwitchStmt(expression: Expr("self")) {
38+
for node in SYNTAX_NODES where node.baseKind == "SyntaxCollection"{
39+
SwitchCase("case .\(raw: node.swiftSyntaxKind):") {
40+
ReturnStmt("return true")
41+
}
42+
}
43+
44+
SwitchCase("default:") {
45+
ReturnStmt("return false")
46+
}
47+
}
48+
}
49+
50+
VariableDecl(
51+
modifiers: [DeclModifier(name: .public)],
52+
name: IdentifierPattern("isMissing"),
53+
type: TypeAnnotation(
54+
colon: .colon,
55+
type: SimpleTypeIdentifier("Bool")
56+
)
57+
) {
58+
SwitchStmt(expression: Expr("self")) {
59+
for name in SYNTAX_BASE_KINDS where !["Syntax", "SyntaxCollection"].contains(name) {
60+
SwitchCase("case .missing\(raw: name):") {
61+
ReturnStmt("return true")
62+
}
63+
}
64+
65+
SwitchCase("default:") {
66+
ReturnStmt("return false")
67+
}
68+
}
69+
}
70+
}
71+
}

Package.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,7 @@ let package = Package(
6666
"Raw/RawSyntaxNodes.swift.gyb",
6767
"Raw/RawSyntaxValidation.swift.gyb",
6868
"SyntaxCollections.swift.gyb",
69-
"SyntaxEnum.swift.gyb",
7069
"SyntaxFactory.swift.gyb",
71-
"SyntaxKind.swift.gyb",
7270
"SyntaxNodes.swift.gyb.template",
7371
"SyntaxRewriter.swift.gyb",
7472
"SyntaxTransform.swift.gyb",

Sources/SwiftSyntax/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ add_swift_host_library(SwiftSyntax
3535
generated/SyntaxAnyVisitor.swift
3636
generated/SyntaxBaseNodes.swift
3737
gyb_generated/SyntaxCollections.swift
38-
gyb_generated/SyntaxEnum.swift
38+
generated/SyntaxEnum.swift
3939
gyb_generated/SyntaxFactory.swift
40-
gyb_generated/SyntaxKind.swift
40+
generated/SyntaxKind.swift
4141
gyb_generated/SyntaxRewriter.swift
4242
gyb_generated/SyntaxTraits.swift
4343
gyb_generated/SyntaxTransform.swift

Sources/SwiftSyntax/SyntaxEnum.swift.gyb

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

Sources/SwiftSyntax/SyntaxKind.swift.gyb

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

0 commit comments

Comments
 (0)