Skip to content

Commit 90fcf63

Browse files
committed
Move Tokens from gyb to codegen
1 parent 9140aa3 commit 90fcf63

File tree

7 files changed

+1688
-1558
lines changed

7 files changed

+1688
-1558
lines changed

CodeGeneration/Sources/generate-swiftsyntax/GenerateSwiftSyntax.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ struct GenerateSwiftSyntax: ParsableCommand {
4545

4646
@Flag(help: "Enable verbose output")
4747
var verbose: Bool = false
48-
48+
4949
func run() throws {
5050
let templates: [TemplateSpec] = [
5151
TemplateSpec(sourceFile: basicFormatFile, module: swiftBasicFormatDir, filename: "BasicFormat.swift"),
@@ -62,6 +62,7 @@ struct GenerateSwiftSyntax: ParsableCommand {
6262
TemplateSpec(sourceFile: syntaxTraitsFile, module: swiftSyntaxDir, filename: "SyntaxTraits.swift"),
6363
TemplateSpec(sourceFile: syntaxTransformFile, module: swiftSyntaxDir, filename: "SyntaxTransform.swift"),
6464
TemplateSpec(sourceFile: syntaxVisitorFile, module: swiftSyntaxDir, filename: "SyntaxVisitor.swift"),
65+
TemplateSpec(sourceFile: tokensFile, module: swiftSyntaxDir, filename: "Tokens.swift"),
6566
TemplateSpec(sourceFile: buildableCollectionNodesFile, module: swiftSyntaxBuilderDir, filename: "BuildableCollectionNodes.swift"),
6667
TemplateSpec(sourceFile: buildableNodesFile, module: swiftSyntaxBuilderDir, filename: "BuildableNodes.swift"),
6768
TemplateSpec(sourceFile: resultBuildersFile, module: swiftSyntaxBuilderDir, filename: "ResultBuilders.swift"),
@@ -91,7 +92,7 @@ struct GenerateSwiftSyntax: ParsableCommand {
9192
throw firstError
9293
}
9394
}
94-
95+
9596
private func generateTemplate(
9697
sourceFile: SourceFileSyntax,
9798
module: String,
@@ -103,12 +104,12 @@ struct GenerateSwiftSyntax: ParsableCommand {
103104
withIntermediateDirectories: true,
104105
attributes: nil
105106
)
106-
107+
107108
let fileURL = destination
108109
.appendingPathComponent(module)
109110
.appendingPathComponent("generated")
110111
.appendingPathComponent(filename)
111-
112+
112113
if verbose {
113114
print("Generating \(fileURL.path)...")
114115
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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 tokensFile = SourceFile(
19+
leadingTrivia: .docLineComment(generateCopyrightHeader(for: "generate-swiftsyntax"))) {
20+
FunctionDecl("""
21+
fileprivate func defaultTrivia(presence: SourcePresence, trivia: Trivia) -> Trivia {
22+
switch presence {
23+
case .present:
24+
return trivia
25+
case .missing:
26+
return []
27+
}
28+
}
29+
""")
30+
31+
ExtensionDecl("extension TokenSyntax") {
32+
for token in SYNTAX_TOKENS {
33+
if token.isKeyword {
34+
FunctionDecl("""
35+
public static func \(raw: token.swiftKind)(
36+
leadingTrivia: Trivia = [],
37+
trailingTrivia: Trivia = [],
38+
presence: SourcePresence = .present
39+
) -> TokenSyntax {
40+
return TokenSyntax(
41+
.\(raw: token.swiftKind),
42+
leadingTrivia: leadingTrivia,
43+
trailingTrivia: trailingTrivia,
44+
presence: presence
45+
)
46+
}
47+
""")
48+
} else if let text = token.text {
49+
FunctionDecl("""
50+
public static func \(raw: token.swiftKind)Token(
51+
leadingTrivia: Trivia = [],
52+
trailingTrivia: Trivia = [],
53+
presence: SourcePresence = .present
54+
) -> TokenSyntax {
55+
return TokenSyntax(
56+
.\(raw: token.swiftKind),
57+
leadingTrivia: leadingTrivia,
58+
trailingTrivia: trailingTrivia,
59+
presence: presence
60+
)
61+
}
62+
""")
63+
} else {
64+
FunctionDecl("""
65+
public static func \(raw: token.swiftKind)(
66+
_ text: String,
67+
leadingTrivia: Trivia = [],
68+
trailingTrivia: Trivia = [],
69+
presence: SourcePresence = .present
70+
) -> TokenSyntax {
71+
return TokenSyntax(
72+
.\(raw: token.swiftKind)(text),
73+
leadingTrivia: leadingTrivia,
74+
trailingTrivia: trailingTrivia,
75+
presence: presence
76+
)
77+
}
78+
""")
79+
}
80+
}
81+
82+
FunctionDecl("""
83+
public static func eof(
84+
leadingTrivia: Trivia = [],
85+
presence: SourcePresence = .present
86+
) -> TokenSyntax {
87+
return TokenSyntax(
88+
.eof,
89+
leadingTrivia: leadingTrivia,
90+
trailingTrivia: [],
91+
presence: presence
92+
)
93+
}
94+
""")
95+
}
96+
}

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
"TokenKind.swift.gyb",
73-
"Tokens.swift.gyb",
7473
"Trivia.swift.gyb",
7574
],
7675
swiftSettings: swiftSyntaxSwiftSettings

Sources/SwiftSyntax/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ add_swift_host_library(SwiftSyntax
4343
generated/SyntaxTransform.swift
4444
generated/SyntaxVisitor.swift
4545
gyb_generated/TokenKind.swift
46-
gyb_generated/Tokens.swift
46+
generated/Tokens.swift
4747
gyb_generated/Trivia.swift
4848

4949
gyb_generated/syntax_nodes/SyntaxDeclNodes.swift

Sources/SwiftSyntax/Tokens.swift.gyb

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

0 commit comments

Comments
 (0)