Skip to content

Commit e450932

Browse files
committed
Kill TokenSpec.associatedValueClass
1 parent e5745e2 commit e450932

File tree

4 files changed

+25
-25
lines changed

4 files changed

+25
-25
lines changed

CodeGeneration/Sources/SyntaxSupport/TokenSpec.swift

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,37 +12,40 @@
1212

1313
/// Represents the specification for a Token in the TokenSyntax file.
1414
public struct TokenSpec {
15+
public enum Kind {
16+
case punctuation
17+
/// The `keyword` TokenKind that contains the actual keyword as an associated value
18+
case keyword
19+
case other
20+
}
21+
1522
public let name: String
1623
public let nameForDiagnostics: String
1724
public let text: String?
18-
public let isPunctuation: Bool
19-
public let associatedValueClass: String?
25+
public let kind: Kind
2026

2127
public var swiftKind: String {
2228
return lowercaseFirstWord(name: self.name)
2329
}
2430

25-
private init(
31+
fileprivate init(
2632
name: String,
2733
nameForDiagnostics: String,
2834
text: String? = nil,
29-
isPunctuation: Bool,
30-
associatedValueClass: String? = nil
35+
kind: Kind
3136
) {
3237
self.name = name
3338
self.nameForDiagnostics = nameForDiagnostics
3439
self.text = text
35-
self.isPunctuation = isPunctuation
36-
self.associatedValueClass = associatedValueClass
40+
self.kind = kind
3741
}
3842

3943
static func punctuator(name: String, text: String) -> TokenSpec {
4044
return TokenSpec(
4145
name: name,
4246
nameForDiagnostics: text,
4347
text: text,
44-
isPunctuation: true,
45-
associatedValueClass: nil
48+
kind: .punctuation
4649
)
4750
}
4851

@@ -51,20 +54,17 @@ public struct TokenSpec {
5154
name: name,
5255
nameForDiagnostics: text,
5356
text: text,
54-
isPunctuation: false,
55-
associatedValueClass: nil
57+
kind: .other
5658
)
5759
}
5860

59-
static func other(name: String, nameForDiagnostics: String, text: String? = nil, associatedValueClass: String? = nil) -> TokenSpec {
61+
static func other(name: String, nameForDiagnostics: String, text: String? = nil) -> TokenSpec {
6062
TokenSpec(
6163
name: name,
6264
nameForDiagnostics: nameForDiagnostics,
6365
text: text,
64-
isPunctuation: false,
65-
associatedValueClass: associatedValueClass
66+
kind: .other
6667
)
67-
6868
}
6969
}
7070

@@ -86,7 +86,7 @@ public let SYNTAX_TOKENS: [TokenSpec] = [
8686
.other(name: "Identifier", nameForDiagnostics: "identifier"),
8787
.punctuator(name: "InfixQuestionMark", text: "?"),
8888
.other(name: "IntegerLiteral", nameForDiagnostics: "integer literal"),
89-
.other(name: "Keyword", nameForDiagnostics: "keyword", associatedValueClass: "Keyword"),
89+
TokenSpec(name: "Keyword", nameForDiagnostics: "keyword", text: nil, kind: .keyword),
9090
.punctuator(name: "LeftAngle", text: "<"),
9191
.punctuator(name: "LeftBrace", text: "{"),
9292
.punctuator(name: "LeftParen", text: "("),

CodeGeneration/Sources/Utils/SyntaxBuildableChild.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public extension Child {
7171
if token.text != nil {
7272
return ExprSyntax(".\(raw: token.swiftKind)Token()")
7373
}
74-
guard case .token(let choices, _, _) = kind, choices.count == 1, token.associatedValueClass != nil else {
74+
guard case .token(let choices, _, _) = kind, choices.count == 1, token.kind == .keyword else {
7575
return nil
7676
}
7777
var textChoice: String

CodeGeneration/Sources/generate-swiftsyntax/templates/swiftsyntax/TokenKindFile.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ let tokenKindFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
2525
for token in SYNTAX_TOKENS {
2626
// Tokens that don't have a set text have an associated value that
2727
// contains their text.
28-
if let associatedValueClass = token.associatedValueClass {
29-
DeclSyntax("case \(raw: token.swiftKind)(\(raw: associatedValueClass))")
28+
if token.kind == .keyword {
29+
DeclSyntax("case \(raw: token.swiftKind)(Keyword)")
3030
} else if token.text == nil {
3131
DeclSyntax("case \(raw: token.swiftKind)(String)")
3232
} else {
@@ -43,7 +43,7 @@ let tokenKindFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
4343
) {
4444
try SwitchExprSyntax("switch self") {
4545
for token in SYNTAX_TOKENS {
46-
if token.associatedValueClass != nil {
46+
if token.kind == .keyword {
4747
SwitchCaseSyntax("case .\(raw: token.swiftKind)(let assoc):") {
4848
StmtSyntax("return String(syntaxText: assoc.defaultText)")
4949
}
@@ -69,7 +69,7 @@ let tokenKindFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
6969
) {
7070
try SwitchExprSyntax("switch self") {
7171
for token in SYNTAX_TOKENS {
72-
if token.associatedValueClass != nil {
72+
if token.kind == .keyword {
7373
SwitchCaseSyntax("case .\(raw: token.swiftKind)(let assoc):") {
7474
StmtSyntax("return assoc.defaultText")
7575
}
@@ -99,7 +99,7 @@ let tokenKindFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
9999
try SwitchExprSyntax("switch self") {
100100
for token in SYNTAX_TOKENS {
101101
SwitchCaseSyntax("case .\(raw: token.swiftKind):") {
102-
StmtSyntax("return \(raw: token.isPunctuation)")
102+
StmtSyntax("return \(raw: token.kind == .punctuation)")
103103
}
104104
}
105105
}
@@ -177,7 +177,7 @@ let tokenKindFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
177177
try! SwitchExprSyntax("switch self") {
178178
for token in SYNTAX_TOKENS {
179179
SwitchCaseSyntax("case .\(raw: token.swiftKind):") {
180-
StmtSyntax("return \(raw: token.isPunctuation)")
180+
StmtSyntax("return \(raw: token.kind == .punctuation)")
181181
}
182182
}
183183
}

CodeGeneration/Sources/generate-swiftsyntax/templates/swiftsyntax/TokensFile.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ let tokensFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
3535
}
3636
"""
3737
)
38-
} else if let associatedValueClass = token.associatedValueClass {
38+
} else if token.kind == .keyword {
3939
DeclSyntax(
4040
"""
4141
public static func \(raw: token.swiftKind)(
42-
_ value: \(raw: associatedValueClass),
42+
_ value: Keyword,
4343
leadingTrivia: Trivia = [],
4444
trailingTrivia: Trivia = [],
4545
presence: SourcePresence = .present

0 commit comments

Comments
 (0)