Skip to content

Commit d7b1493

Browse files
authored
Rename eof to endOfFile (#1842)
Update Codegeneration to use 'endOfFile' instead of 'eof', and include EOF tokens as part of the normal SYNTAX_TOKENS
1 parent 36ebbef commit d7b1493

File tree

50 files changed

+148
-203
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+148
-203
lines changed

CodeGeneration/Sources/SyntaxSupport/DeclNodes.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2036,7 +2036,7 @@ public let DECL_NODES: [Node] = [
20362036
]
20372037
),
20382038

2039-
// source-file = code-block-item-list eof
2039+
// source-file = code-block-item-list endOfFile
20402040
Node(
20412041
kind: .sourceFile,
20422042
base: .syntax,
@@ -2051,7 +2051,7 @@ public let DECL_NODES: [Node] = [
20512051
Child(
20522052
name: "EndOfFileToken",
20532053
deprecatedName: "EOFToken",
2054-
kind: .token(choices: [.token(tokenKind: "EOFToken")])
2054+
kind: .token(choices: [.token(tokenKind: "EndOfFileToken")])
20552055
),
20562056
]
20572057
),

CodeGeneration/Sources/SyntaxSupport/TokenSpec.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ public let SYNTAX_TOKENS: [TokenSpec] = [
183183
PunctuatorSpec(name: "Comma", kind: "comma", text: ",", requiresTrailingSpace: true),
184184
MiscSpec(name: "DollarIdentifier", kind: "dollarident", nameForDiagnostics: "dollar identifier", classification: "DollarIdentifier"),
185185
PunctuatorSpec(name: "Ellipsis", kind: "ellipsis", text: "..."),
186+
MiscSpec(name: "EndOfFile", kind: "eof", nameForDiagnostics: "end of file", text: ""),
186187
PunctuatorSpec(name: "Equal", kind: "equal", text: "=", requiresLeadingSpace: true, requiresTrailingSpace: true),
187188
PunctuatorSpec(name: "ExclamationMark", kind: "exclaim_postfix", text: "!"),
188189
MiscSpec(name: "ExtendedRegexDelimiter", kind: "extended_regex_delimiter", nameForDiagnostics: "extended delimiter", classification: "RegexLiteral"),
@@ -224,8 +225,6 @@ public let SYNTAX_TOKENS: [TokenSpec] = [
224225
MiscSpec(name: "Wildcard", kind: "_", nameForDiagnostics: "wildcard", 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: 0 additions & 35 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

@@ -86,9 +80,6 @@ let tokenKindFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
8680
}
8781
}
8882

89-
SwitchCaseSyntax("case .eof:") {
90-
StmtSyntax(#"return """#)
91-
}
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: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,8 @@ class ValidateSyntaxNodes: XCTestCase {
252252
message: "child 'ClosingPounds' has a token as its only token choice and should thus be named 'ExtendedRegexDelimiter'"
253253
// There are the opening and closing ExtendedRegexDelimiter in the node
254254
),
255+
// We should explicitly mention token here because it’s not obvious that the end of a file is represented by a token
256+
ValidationFailure(node: .sourceFile, message: "child 'EndOfFileToken' has a token as its only token choice and should thus be named 'EndOfFile'"),
255257
ValidationFailure(
256258
node: .stringLiteralExpr,
257259
message: "child 'OpenDelimiter' has a token as its only token choice and should thus be named 'RawStringDelimiter'"
@@ -298,11 +300,6 @@ class ValidateSyntaxNodes: XCTestCase {
298300
node: .importPathComponent,
299301
message: "child 'TrailingPeriod' has a token as its only token choice and should thus be named 'Period'"
300302
),
301-
// We should explicitly mention token here because it’s not obvious that the end of a file is represented by a token
302-
ValidationFailure(
303-
node: .sourceFile,
304-
message: "child 'EndOfFileToken' has a token as its only token choice and should thus be named 'EOF'"
305-
),
306303
// `~` is the only operator that’s allowed here
307304
ValidationFailure(
308305
node: .suppressedType,

Sources/SwiftBasicFormat/BasicFormat.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ open class BasicFormat: SyntaxRewriter {
208208
(.backslash, _),
209209
(.backtick, _),
210210
(.dollarIdentifier, .period), // a.b
211-
(.eof, _),
211+
(.endOfFile, _),
212212
(.exclamationMark, .leftParen), // myOptionalClosure!()
213213
(.exclamationMark, .period), // myOptionalBar!.foo()
214214
(.extendedRegexDelimiter, .leftParen), // opening extended regex delimiter should never be separate by a space
@@ -254,7 +254,7 @@ open class BasicFormat: SyntaxRewriter {
254254
(.stringSegment, _),
255255
(_, .comma),
256256
(_, .ellipsis),
257-
(_, .eof),
257+
(_, .endOfFile),
258258
(_, .exclamationMark),
259259
(_, .postfixOperator),
260260
(_, .postfixQuestionMark),

Sources/SwiftIDEUtils/generated/SyntaxClassification.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ extension RawTokenKind {
122122
return .dollarIdentifier
123123
case .ellipsis:
124124
return .none
125+
case .endOfFile:
126+
return .none
125127
case .equal:
126128
return .none
127129
case .exclamationMark:
@@ -200,8 +202,6 @@ extension RawTokenKind {
200202
return .none
201203
case .wildcard:
202204
return .none
203-
case .eof:
204-
return .none
205205
}
206206
}
207207
}

Sources/SwiftParser/Attributes.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ extension Parser {
287287
// The contents of the @_effects attribute are parsed in SIL, we just
288288
// represent the contents as a list of tokens in SwiftSyntax.
289289
var tokens: [RawTokenSyntax] = []
290-
while !parser.at(.rightParen, .eof) {
290+
while !parser.at(.rightParen, .endOfFile) {
291291
tokens.append(parser.consumeAnyToken())
292292
}
293293
return .effectsArguments(RawEffectsArgumentsSyntax(elements: tokens, arena: parser.arena))
@@ -453,7 +453,7 @@ extension Parser {
453453

454454
var elements = [RawDifferentiabilityParamSyntax]()
455455
var loopProgress = LoopProgressCondition()
456-
while !self.at(.eof, .rightParen) && loopProgress.evaluate(currentToken) {
456+
while !self.at(.endOfFile, .rightParen) && loopProgress.evaluate(currentToken) {
457457
guard let param = self.parseDifferentiabilityParameter() else {
458458
break
459459
}
@@ -676,7 +676,7 @@ extension Parser {
676676
var elements = [RawSpecializeAttributeSpecListSyntax.Element]()
677677
// Parse optional "exported" and "kind" labeled parameters.
678678
var loopProgress = LoopProgressCondition()
679-
while !self.at(.eof, .rightParen, .keyword(.where)) && loopProgress.evaluate(currentToken) {
679+
while !self.at(.endOfFile, .rightParen, .keyword(.where)) && loopProgress.evaluate(currentToken) {
680680
switch self.at(anyIn: SpecializeParameter.self) {
681681
case (.target, let handle)?:
682682
let ident = self.eat(handle)

Sources/SwiftParser/Declarations.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ extension TokenConsumer {
8585
}
8686

8787
if hasAttribute {
88-
if subparser.at(.rightBrace) || subparser.at(.eof) || subparser.at(.poundEndifKeyword) {
88+
if subparser.at(.rightBrace) || subparser.at(.endOfFile) || subparser.at(.poundEndifKeyword) {
8989
return true
9090
}
9191
}
@@ -776,7 +776,7 @@ extension Parser {
776776
let (unexpectedBeforeLBrace, lbrace) = self.expect(.leftBrace)
777777
do {
778778
var loopProgress = LoopProgressCondition()
779-
while !self.at(.eof, .rightBrace) && loopProgress.evaluate(currentToken) {
779+
while !self.at(.endOfFile, .rightBrace) && loopProgress.evaluate(currentToken) {
780780
let newItemAtStartOfLine = self.currentToken.isAtStartOfLine
781781
guard let newElement = self.parseMemberDeclListItem() else {
782782
break
@@ -1539,7 +1539,7 @@ extension Parser {
15391539
var elements = [RawAccessorDeclSyntax]()
15401540
do {
15411541
var loopProgress = LoopProgressCondition()
1542-
while !self.at(.eof, .rightBrace) && loopProgress.evaluate(currentToken) {
1542+
while !self.at(.endOfFile, .rightBrace) && loopProgress.evaluate(currentToken) {
15431543
guard let introducer = self.parseAccessorIntroducer() else {
15441544
// There can only be an implicit getter if no other accessors were
15451545
// seen before this one.
@@ -1758,7 +1758,7 @@ extension Parser {
17581758
var loopProgress = LoopProgressCondition()
17591759
while (identifiersAfterOperatorName.last ?? name).trailingTriviaByteLength == 0,
17601760
self.currentToken.leadingTriviaByteLength == 0,
1761-
!self.at(.colon, .leftBrace, .eof),
1761+
!self.at(.colon, .leftBrace, .endOfFile),
17621762
loopProgress.evaluate(self.currentToken)
17631763
{
17641764
identifiersAfterOperatorName.append(consumeAnyToken())
@@ -1906,7 +1906,7 @@ extension Parser {
19061906
var elements = [RawPrecedenceGroupAttributeListSyntax.Element]()
19071907
do {
19081908
var attributesProgress = LoopProgressCondition()
1909-
LOOP: while !self.at(.eof, .rightBrace) && attributesProgress.evaluate(currentToken) {
1909+
LOOP: while !self.at(.endOfFile, .rightBrace) && attributesProgress.evaluate(currentToken) {
19101910
switch self.at(anyIn: LabelText.self) {
19111911
case (.associativity, let handle)?:
19121912
let associativity = self.eat(handle)

Sources/SwiftParser/Directives.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ extension Parser {
205205
) -> [Element] {
206206
var elements = [Element]()
207207
var elementsProgress = LoopProgressCondition()
208-
while !self.at(.eof)
208+
while !self.at(.endOfFile)
209209
&& !self.at(.poundElseKeyword, .poundElseifKeyword, .poundEndifKeyword)
210210
&& !self.atElifTypo()
211211
&& elementsProgress.evaluate(currentToken)

0 commit comments

Comments
 (0)