Skip to content

Commit e5745e2

Browse files
committed
Flatten TokenSpec class hierarchy in CodeGeneration
1 parent 4cfb370 commit e5745e2

File tree

2 files changed

+74
-115
lines changed

2 files changed

+74
-115
lines changed

CodeGeneration/Sources/SyntaxSupport/TokenSpec.swift

Lines changed: 72 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -11,154 +11,113 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
/// Represents the specification for a Token in the TokenSyntax file.
14-
public class TokenSpec {
14+
public struct TokenSpec {
1515
public let name: String
1616
public let nameForDiagnostics: String
1717
public let text: String?
18+
public let isPunctuation: Bool
1819
public let associatedValueClass: String?
1920

2021
public var swiftKind: String {
2122
return lowercaseFirstWord(name: self.name)
2223
}
2324

24-
init(
25+
private init(
2526
name: String,
2627
nameForDiagnostics: String,
2728
text: String? = nil,
29+
isPunctuation: Bool,
2830
associatedValueClass: String? = nil
2931
) {
3032
self.name = name
3133
self.nameForDiagnostics = nameForDiagnostics
3234
self.text = text
35+
self.isPunctuation = isPunctuation
3336
self.associatedValueClass = associatedValueClass
3437
}
35-
}
36-
37-
public class PoundSpec: TokenSpec {
38-
init(
39-
name: String,
40-
nameForDiagnostics: String? = nil,
41-
text: String
42-
) {
43-
super.init(
44-
name: name,
45-
nameForDiagnostics: nameForDiagnostics ?? text,
46-
text: text
47-
)
48-
}
49-
}
5038

51-
public class PoundObjectLiteralSpec: PoundSpec {
52-
let `protocol`: String
53-
54-
init(
55-
name: String,
56-
text: String,
57-
nameForDiagnostics: String,
58-
`protocol`: String
59-
) {
60-
self.`protocol` = `protocol`
61-
super.init(
39+
static func punctuator(name: String, text: String) -> TokenSpec {
40+
return TokenSpec(
6241
name: name,
63-
nameForDiagnostics: nameForDiagnostics,
64-
text: text
42+
nameForDiagnostics: text,
43+
text: text,
44+
isPunctuation: true,
45+
associatedValueClass: nil
6546
)
6647
}
67-
}
68-
69-
public class PoundConfigSpec: PoundSpec {}
7048

71-
public class PoundDirectiveSpec: PoundSpec {
72-
init(
73-
name: String,
74-
text: String
75-
) {
76-
super.init(
49+
static func poundKeyword(name: String, text: String) -> TokenSpec {
50+
return TokenSpec(
7751
name: name,
78-
text: text
52+
nameForDiagnostics: text,
53+
text: text,
54+
isPunctuation: false,
55+
associatedValueClass: nil
7956
)
8057
}
81-
}
8258

83-
public class PoundConditionalDirectiveSpec: PoundDirectiveSpec {
84-
override init(
85-
name: String,
86-
text: String
87-
) {
88-
super.init(
59+
static func other(name: String, nameForDiagnostics: String, text: String? = nil, associatedValueClass: String? = nil) -> TokenSpec {
60+
TokenSpec(
8961
name: name,
90-
text: text
62+
nameForDiagnostics: nameForDiagnostics,
63+
text: text,
64+
isPunctuation: false,
65+
associatedValueClass: associatedValueClass
9166
)
92-
}
93-
}
9467

95-
public class PunctuatorSpec: TokenSpec {
96-
init(
97-
name: String,
98-
text: String
99-
) {
100-
super.init(
101-
name: name,
102-
nameForDiagnostics: text,
103-
text: text
104-
)
10568
}
10669
}
10770

108-
public class LiteralSpec: TokenSpec {}
109-
110-
public class MiscSpec: TokenSpec {}
111-
11271
public let SYNTAX_TOKENS: [TokenSpec] = [
113-
PunctuatorSpec(name: "Arrow", text: "->"),
114-
PunctuatorSpec(name: "AtSign", text: "@"),
115-
PunctuatorSpec(name: "Backslash", text: "\\"),
116-
PunctuatorSpec(name: "Backtick", text: "`"),
117-
MiscSpec(name: "BinaryOperator", nameForDiagnostics: "binary operator"),
118-
PunctuatorSpec(name: "Colon", text: ":"),
119-
PunctuatorSpec(name: "Comma", text: ","),
120-
MiscSpec(name: "DollarIdentifier", nameForDiagnostics: "dollar identifier"),
121-
PunctuatorSpec(name: "Ellipsis", text: "..."),
122-
MiscSpec(name: "EndOfFile", nameForDiagnostics: "end of file", text: ""),
123-
PunctuatorSpec(name: "Equal", text: "="),
124-
PunctuatorSpec(name: "ExclamationMark", text: "!"),
125-
MiscSpec(name: "ExtendedRegexDelimiter", nameForDiagnostics: "extended delimiter"),
126-
LiteralSpec(name: "FloatingLiteral", nameForDiagnostics: "floating literal"),
127-
MiscSpec(name: "Identifier", nameForDiagnostics: "identifier"),
128-
PunctuatorSpec(name: "InfixQuestionMark", text: "?"),
129-
LiteralSpec(name: "IntegerLiteral", nameForDiagnostics: "integer literal"),
130-
MiscSpec(name: "Keyword", nameForDiagnostics: "keyword", associatedValueClass: "Keyword"),
131-
PunctuatorSpec(name: "LeftAngle", text: "<"),
132-
PunctuatorSpec(name: "LeftBrace", text: "{"),
133-
PunctuatorSpec(name: "LeftParen", text: "("),
134-
PunctuatorSpec(name: "LeftSquare", text: "["),
135-
PunctuatorSpec(name: "MultilineStringQuote", text: "\"\"\""),
136-
PunctuatorSpec(name: "Period", text: "."),
137-
MiscSpec(name: "PostfixOperator", nameForDiagnostics: "postfix operator"),
138-
PunctuatorSpec(name: "PostfixQuestionMark", text: "?"),
139-
PunctuatorSpec(name: "Pound", text: "#"),
140-
PoundConfigSpec(name: "PoundAvailable", text: "#available"),
141-
PoundConditionalDirectiveSpec(name: "PoundElse", text: "#else"),
142-
PoundConditionalDirectiveSpec(name: "PoundElseif", text: "#elseif"),
143-
PoundConditionalDirectiveSpec(name: "PoundEndif", text: "#endif"),
144-
PoundConditionalDirectiveSpec(name: "PoundIf", text: "#if"),
145-
PoundDirectiveSpec(name: "PoundSourceLocation", text: "#sourceLocation"),
146-
PoundConfigSpec(name: "PoundUnavailable", text: "#unavailable"),
147-
PunctuatorSpec(name: "PrefixAmpersand", text: "&"),
148-
MiscSpec(name: "PrefixOperator", nameForDiagnostics: "prefix operator"),
149-
MiscSpec(name: "RawStringDelimiter", nameForDiagnostics: "raw string delimiter"),
150-
MiscSpec(name: "RegexLiteralPattern", nameForDiagnostics: "regex pattern"),
151-
PunctuatorSpec(name: "RegexSlash", text: "/"),
152-
PunctuatorSpec(name: "RightAngle", text: ">"),
153-
PunctuatorSpec(name: "RightBrace", text: "}"),
154-
PunctuatorSpec(name: "RightParen", text: ")"),
155-
PunctuatorSpec(name: "RightSquare", text: "]"),
156-
PunctuatorSpec(name: "Semicolon", text: ";"),
157-
PunctuatorSpec(name: "SingleQuote", text: "\'"),
158-
PunctuatorSpec(name: "StringQuote", text: "\""),
159-
MiscSpec(name: "StringSegment", nameForDiagnostics: "string segment"),
160-
MiscSpec(name: "Unknown", nameForDiagnostics: "token"),
161-
MiscSpec(name: "Wildcard", nameForDiagnostics: "wildcard", text: "_"),
72+
.punctuator(name: "Arrow", text: "->"),
73+
.punctuator(name: "AtSign", text: "@"),
74+
.punctuator(name: "Backslash", text: "\\"),
75+
.punctuator(name: "Backtick", text: "`"),
76+
.other(name: "BinaryOperator", nameForDiagnostics: "binary operator"),
77+
.punctuator(name: "Colon", text: ":"),
78+
.punctuator(name: "Comma", text: ","),
79+
.other(name: "DollarIdentifier", nameForDiagnostics: "dollar identifier"),
80+
.punctuator(name: "Ellipsis", text: "..."),
81+
.other(name: "EndOfFile", nameForDiagnostics: "end of file", text: ""),
82+
.punctuator(name: "Equal", text: "="),
83+
.punctuator(name: "ExclamationMark", text: "!"),
84+
.other(name: "ExtendedRegexDelimiter", nameForDiagnostics: "extended delimiter"),
85+
.other(name: "FloatingLiteral", nameForDiagnostics: "floating literal"),
86+
.other(name: "Identifier", nameForDiagnostics: "identifier"),
87+
.punctuator(name: "InfixQuestionMark", text: "?"),
88+
.other(name: "IntegerLiteral", nameForDiagnostics: "integer literal"),
89+
.other(name: "Keyword", nameForDiagnostics: "keyword", associatedValueClass: "Keyword"),
90+
.punctuator(name: "LeftAngle", text: "<"),
91+
.punctuator(name: "LeftBrace", text: "{"),
92+
.punctuator(name: "LeftParen", text: "("),
93+
.punctuator(name: "LeftSquare", text: "["),
94+
.punctuator(name: "MultilineStringQuote", text: "\"\"\""),
95+
.punctuator(name: "Period", text: "."),
96+
.other(name: "PostfixOperator", nameForDiagnostics: "postfix operator"),
97+
.punctuator(name: "PostfixQuestionMark", text: "?"),
98+
.punctuator(name: "Pound", text: "#"),
99+
.poundKeyword(name: "PoundAvailable", text: "#available"),
100+
.poundKeyword(name: "PoundElse", text: "#else"),
101+
.poundKeyword(name: "PoundElseif", text: "#elseif"),
102+
.poundKeyword(name: "PoundEndif", text: "#endif"),
103+
.poundKeyword(name: "PoundIf", text: "#if"),
104+
.poundKeyword(name: "PoundSourceLocation", text: "#sourceLocation"),
105+
.poundKeyword(name: "PoundUnavailable", text: "#unavailable"),
106+
.punctuator(name: "PrefixAmpersand", text: "&"),
107+
.other(name: "PrefixOperator", nameForDiagnostics: "prefix operator"),
108+
.other(name: "RawStringDelimiter", nameForDiagnostics: "raw string delimiter"),
109+
.other(name: "RegexLiteralPattern", nameForDiagnostics: "regex pattern"),
110+
.punctuator(name: "RegexSlash", text: "/"),
111+
.punctuator(name: "RightAngle", text: ">"),
112+
.punctuator(name: "RightBrace", text: "}"),
113+
.punctuator(name: "RightParen", text: ")"),
114+
.punctuator(name: "RightSquare", text: "]"),
115+
.punctuator(name: "Semicolon", text: ";"),
116+
.punctuator(name: "SingleQuote", text: "\'"),
117+
.punctuator(name: "StringQuote", text: "\""),
118+
.other(name: "StringSegment", nameForDiagnostics: "string segment"),
119+
.other(name: "Unknown", nameForDiagnostics: "token"),
120+
.other(name: "Wildcard", nameForDiagnostics: "wildcard", text: "_"),
162121
]
163122

164123
public let SYNTAX_TOKEN_MAP = Dictionary(

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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: type(of: token) == PunctuatorSpec.self)")
102+
StmtSyntax("return \(raw: token.isPunctuation)")
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: type(of: token) == PunctuatorSpec.self)")
180+
StmtSyntax("return \(raw: token.isPunctuation)")
181181
}
182182
}
183183
}

0 commit comments

Comments
 (0)