Skip to content

Commit 7c81c27

Browse files
committed
Update Codegeneration to use 'endOfFile' instead of 'eof', and include EOF tokens as part of the normal SYNTAX_TOKENS
1 parent 1555299 commit 7c81c27

File tree

11 files changed

+8
-78
lines changed

11 files changed

+8
-78
lines changed

CodeGeneration/Sources/SyntaxSupport/DeclNodes.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2021,7 +2021,7 @@ public let DECL_NODES: [Node] = [
20212021
]
20222022
),
20232023

2024-
// source-file = code-block-item-list eof
2024+
// source-file = code-block-item-list endOfFile
20252025
Node(
20262026
kind: .sourceFile,
20272027
base: .syntax,
@@ -2034,8 +2034,8 @@ public let DECL_NODES: [Node] = [
20342034
kind: .collection(kind: .codeBlockItemList, collectionElementName: "Statement")
20352035
),
20362036
Child(
2037-
name: "EOFToken",
2038-
kind: .token(choices: [.token(tokenKind: "EOFToken")])
2037+
name: "EndOfFileToken",
2038+
kind: .token(choices: [.token(tokenKind: "EndOfFileToken")])
20392039
),
20402040
]
20412041
),

CodeGeneration/Sources/SyntaxSupport/TokenSpec.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,10 +222,9 @@ public let SYNTAX_TOKENS: [TokenSpec] = [
222222
MiscSpec(name: "StringSegment", kind: "string_segment", nameForDiagnostics: "string segment", classification: "StringLiteral"),
223223
MiscSpec(name: "Unknown", kind: "unknown", nameForDiagnostics: "token"),
224224
MiscSpec(name: "Wildcard", kind: "_", nameForDiagnostics: "wildcard", text: "_"),
225+
MiscSpec(name: "EndOfFile", kind: "eof", nameForDiagnostics: "end of file", text: "")
225226
]
226227

227-
// FIXME: Generate the EOF token as part of the normal SYNTAX_TOKENS and remove the special handling for it.
228228
public let SYNTAX_TOKEN_MAP = Dictionary(
229-
uniqueKeysWithValues: (SYNTAX_TOKENS + [MiscSpec(name: "EOF", kind: "eof", nameForDiagnostics: "end of file", text: "")])
230-
.map { ("\($0.name)Token", $0) }
229+
uniqueKeysWithValues: SYNTAX_TOKENS.map { ("\($0.name)Token", $0) }
231230
)

CodeGeneration/Sources/Utils/SyntaxBuildableChild.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,6 @@ public extension Child {
7474
if token.isKeyword {
7575
return InitializerClauseSyntax(value: ExprSyntax(".\(raw: token.swiftKind)()"))
7676
}
77-
if token.name == "EOF" {
78-
return InitializerClauseSyntax(value: ExprSyntax(".eof()"))
79-
}
8077
if token.text != nil {
8178
return InitializerClauseSyntax(value: ExprSyntax(".\(raw: token.swiftKind)Token()"))
8279
}

CodeGeneration/Sources/Utils/SyntaxBuildableType.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,6 @@ public struct SyntaxBuildableType: Hashable {
6868
} else if token.text != nil {
6969
return ExprSyntax(".\(raw: lowercaseFirstWord(name: token.name))Token()")
7070
}
71-
} else if case .token("EOFToken") = kind {
72-
return ExprSyntax(".eof()")
7371
}
7472
return nil
7573
}

CodeGeneration/Sources/generate-swiftsyntax/templates/ideutils/SyntaxClassificationFile.swift

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,10 @@ let syntaxClassificationFile = SourceFileSyntax(leadingTrivia: copyrightHeader)
7777
if let classification = token.classification {
7878
StmtSyntax("return .\(raw: classification.swiftName)")
7979
} else {
80-
StmtSyntax("return .none)")
80+
StmtSyntax("return .none")
8181
}
8282
}
8383
}
84-
SwitchCaseSyntax("case .eof:") {
85-
StmtSyntax("return .none")
86-
}
8784
}
8885
}
8986
}

CodeGeneration/Sources/generate-swiftsyntax/templates/swiftparser/IsLexerClassifiedFile.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,6 @@ let isLexerClassifiedFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
5858
"""
5959
) {
6060
try! SwitchExprSyntax("switch self") {
61-
SwitchCaseSyntax("case .eof:") {
62-
StmtSyntax("return false")
63-
}
64-
6561
for token in SYNTAX_TOKENS where token.isKeyword {
6662
SwitchCaseSyntax("case .\(raw: token.swiftKind):") {
6763
StmtSyntax("return true")

CodeGeneration/Sources/generate-swiftsyntax/templates/swiftparser/TokenSpecStaticMembersFile.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ let tokenSpecStaticMembersFile = SourceFileSyntax(leadingTrivia: copyrightHeader
1919
DeclSyntax("@_spi(RawSyntax) import SwiftSyntax")
2020

2121
try! ExtensionDeclSyntax("extension TokenSpec") {
22-
DeclSyntax("static var eof: TokenSpec { return TokenSpec(.eof) }")
23-
2422
for token in SYNTAX_TOKENS where token.swiftKind != "keyword" {
2523
DeclSyntax("static var \(raw: token.swiftKind): TokenSpec { return TokenSpec(.\(raw: token.swiftKind)) }")
2624
}

CodeGeneration/Sources/generate-swiftsyntax/templates/swiftparserdiagnostics/TokenNameForDiagnosticsFile.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,6 @@ let tokenNameForDiagnosticFile = SourceFileSyntax(leadingTrivia: copyrightHeader
2121
try! ExtensionDeclSyntax("extension TokenKind") {
2222
try! VariableDeclSyntax("var nameForDiagnostics: String") {
2323
try! SwitchExprSyntax("switch self") {
24-
SwitchCaseSyntax("case .eof:") {
25-
StmtSyntax(#"return "end of file""#)
26-
}
27-
2824
for token in SYNTAX_TOKENS where token.swiftKind != "keyword" {
2925
SwitchCaseSyntax("case .\(raw: token.swiftKind):") {
3026
StmtSyntax("return #\"\(raw: token.nameForDiagnostics)\"#")

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

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ let tokenKindFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
2222
public enum TokenKind: Hashable
2323
"""
2424
) {
25-
DeclSyntax("case eof")
26-
2725
for token in SYNTAX_TOKENS {
2826
// Tokens that don't have a set text have an associated value that
2927
// contains their text.
@@ -59,10 +57,6 @@ let tokenKindFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
5957
}
6058
}
6159
}
62-
63-
SwitchCaseSyntax("case .eof:") {
64-
StmtSyntax(#"return """#)
65-
}
6660
}
6761
}
6862

@@ -85,10 +79,7 @@ let tokenKindFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
8579
}
8680
}
8781
}
88-
89-
SwitchCaseSyntax("case .eof:") {
90-
StmtSyntax(#"return """#)
91-
}
82+
9283
SwitchCaseSyntax("default:") {
9384
StmtSyntax(#"return """#)
9485
}
@@ -106,10 +97,6 @@ let tokenKindFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
10697
"""
10798
) {
10899
try SwitchExprSyntax("switch self") {
109-
SwitchCaseSyntax("case .eof:") {
110-
StmtSyntax("return false")
111-
}
112-
113100
for token in SYNTAX_TOKENS {
114101
SwitchCaseSyntax("case .\(raw: token.swiftKind):") {
115102
StmtSyntax("return \(raw: type(of: token) == PunctuatorSpec.self)")
@@ -122,10 +109,6 @@ let tokenKindFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
122109
try! ExtensionDeclSyntax("extension TokenKind: Equatable") {
123110
try FunctionDeclSyntax("public static func ==(lhs: TokenKind, rhs: TokenKind) -> Bool") {
124111
try SwitchExprSyntax("switch (lhs, rhs)") {
125-
SwitchCaseSyntax("case (.eof, .eof):") {
126-
StmtSyntax("return true")
127-
}
128-
129112
for token in SYNTAX_TOKENS {
130113
if token.text != nil {
131114
SwitchCaseSyntax("case (.\(raw: token.swiftKind), .\(raw: token.swiftKind)):") {
@@ -156,8 +139,6 @@ let tokenKindFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
156139
public enum RawTokenKind: UInt8, Equatable, Hashable
157140
"""
158141
) {
159-
DeclSyntax("case eof")
160-
161142
for token in SYNTAX_TOKENS {
162143
DeclSyntax("case \(raw: token.swiftKind)")
163144
}
@@ -169,10 +150,6 @@ let tokenKindFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
169150
"""
170151
) {
171152
try! SwitchExprSyntax("switch self") {
172-
SwitchCaseSyntax("case .eof:") {
173-
StmtSyntax(#"return """#)
174-
}
175-
176153
for token in SYNTAX_TOKENS {
177154
if let text = token.text {
178155
SwitchCaseSyntax("case .\(raw: token.swiftKind):") {
@@ -198,10 +175,6 @@ let tokenKindFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
198175
"""
199176
) {
200177
try! SwitchExprSyntax("switch self") {
201-
SwitchCaseSyntax("case .eof:") {
202-
StmtSyntax("return false")
203-
}
204-
205178
for token in SYNTAX_TOKENS {
206179
SwitchCaseSyntax("case .\(raw: token.swiftKind):") {
207180
StmtSyntax("return \(raw: type(of: token) == PunctuatorSpec.self)")
@@ -220,10 +193,6 @@ let tokenKindFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
220193
"""
221194
) {
222195
try! SwitchExprSyntax("switch rawKind") {
223-
SwitchCaseSyntax("case .eof:") {
224-
StmtSyntax("return .eof")
225-
}
226-
227196
for token in SYNTAX_TOKENS {
228197
if token.swiftKind == "keyword" {
229198
SwitchCaseSyntax("case .\(raw: token.swiftKind):") {
@@ -259,10 +228,6 @@ let tokenKindFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
259228
"""
260229
) {
261230
try! SwitchExprSyntax("switch self") {
262-
SwitchCaseSyntax("case .eof:") {
263-
StmtSyntax("return (.eof, nil)")
264-
}
265-
266231
for token in SYNTAX_TOKENS {
267232
if token.swiftKind == "keyword" {
268233
SwitchCaseSyntax("case .\(raw: token.swiftKind)(let keyword):") {

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

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -90,21 +90,5 @@ let tokensFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
9090
)
9191
}
9292
}
93-
94-
DeclSyntax(
95-
"""
96-
public static func eof(
97-
leadingTrivia: Trivia = [],
98-
presence: SourcePresence = .present
99-
) -> TokenSyntax {
100-
return TokenSyntax(
101-
.eof,
102-
leadingTrivia: leadingTrivia,
103-
trailingTrivia: [],
104-
presence: presence
105-
)
106-
}
107-
"""
108-
)
10993
}
11094
}

CodeGeneration/Tests/ValidateSyntaxNodes/ValidateSyntaxNodes.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ class ValidateSyntaxNodes: XCTestCase {
383383
node: .regexLiteralExpr,
384384
message: "child 'ClosingPounds' has a token as its only token choice and should thus be named 'ExtendedRegexDelimiter'"
385385
),
386-
ValidationFailure(node: .sourceFile, message: "child 'EOFToken' has a token as its only token choice and should thus be named 'EOF'"),
386+
ValidationFailure(node: .sourceFile, message: "child 'EndOfFileToken' has a token as its only token choice and should thus be named 'EndOfFile'"),
387387
ValidationFailure(
388388
node: .stringLiteralExpr,
389389
message: "child 'OpenDelimiter' has a token as its only token choice and should thus be named 'RawStringDelimiter'"

0 commit comments

Comments
 (0)