Skip to content

Commit 60b8322

Browse files
committed
Migrate isPossibleDeclStart to RawTokenKindSubset
1 parent 2973ece commit 60b8322

File tree

3 files changed

+77
-42
lines changed

3 files changed

+77
-42
lines changed

Sources/SwiftParser/Lookahead.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ extension Parser.Lookahead {
221221
]
222222

223223
func isStartOfDeclaration() -> Bool {
224-
guard self.currentToken.isKeywordPossibleDeclStart else {
224+
guard self.at(anyIn: CanBeDeclaratinStart.self) != nil else {
225225
// If this is obviously not the start of a decl, then we're done.
226226
return false
227227
}
@@ -347,7 +347,7 @@ extension Parser.Lookahead {
347347
}
348348

349349
// If the next token is obviously not the start of a decl, bail early.
350-
guard tok2.isKeywordPossibleDeclStart else {
350+
guard CanBeDeclaratinStart(tok2) != nil else {
351351
return false
352352
}
353353

Sources/SwiftParser/Names.swift

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -198,46 +198,6 @@ extension Lexer.Lexeme {
198198
self.tokenKind.isKeyword
199199
}
200200

201-
var isKeywordPossibleDeclStart: Bool {
202-
switch self.tokenKind {
203-
case .atSign,
204-
.associatedtypeKeyword,
205-
.caseKeyword,
206-
.classKeyword,
207-
.deinitKeyword,
208-
.enumKeyword,
209-
.extensionKeyword,
210-
.fileprivateKeyword,
211-
.funcKeyword,
212-
.importKeyword,
213-
.initKeyword,
214-
.internalKeyword,
215-
.letKeyword,
216-
.operatorKeyword,
217-
.precedencegroupKeyword,
218-
.privateKeyword,
219-
.protocolKeyword,
220-
.publicKeyword,
221-
.staticKeyword,
222-
.structKeyword,
223-
.subscriptKeyword,
224-
.typealiasKeyword,
225-
.varKeyword,
226-
.poundIfKeyword,
227-
.poundWarningKeyword,
228-
.poundErrorKeyword,
229-
.identifier,
230-
.poundSourceLocationKeyword:
231-
return true
232-
case .poundLineKeyword:
233-
// #line at the start of the line is a directive, but it's deprecated.
234-
// #line within a line is an expression.
235-
return self.isAtStartOfLine
236-
default:
237-
return false
238-
}
239-
}
240-
241201
func starts(with symbol: SyntaxText) -> Bool {
242202
guard Operator(self) != nil || self.tokenKind.isPunctuation else {
243203
return false

Sources/SwiftParser/RawTokenKindSubset.swift

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,81 @@ enum BinaryOperator: RawTokenKindSubset {
8484
}
8585
}
8686

87+
enum CanBeDeclaratinStart: RawTokenKindSubset {
88+
case associatedtypeKeyword
89+
case atSign
90+
case caseKeyword
91+
case classKeyword
92+
case deinitKeyword
93+
case enumKeyword
94+
case extensionKeyword
95+
case fileprivateKeyword
96+
case funcKeyword
97+
case identifier
98+
case importKeyword
99+
case initKeyword
100+
case internalKeyword
101+
case letKeyword
102+
case operatorKeyword
103+
case poundErrorKeyword
104+
case poundIfKeyword
105+
case poundLineKeyword
106+
case poundSourceLocationKeyword
107+
case poundWarningKeyword
108+
case precedencegroupKeyword
109+
case privateKeyword
110+
case protocolKeyword
111+
case publicKeyword
112+
case staticKeyword
113+
case structKeyword
114+
case subscriptKeyword
115+
case typealiasKeyword
116+
case varKeyword
117+
118+
var rawTokenKind: RawTokenKind {
119+
switch self {
120+
case .associatedtypeKeyword: return .associatedtypeKeyword
121+
case .atSign: return .atSign
122+
case .caseKeyword: return .caseKeyword
123+
case .classKeyword: return .classKeyword
124+
case .deinitKeyword: return .deinitKeyword
125+
case .enumKeyword: return .enumKeyword
126+
case .extensionKeyword: return .extensionKeyword
127+
case .fileprivateKeyword: return .fileprivateKeyword
128+
case .funcKeyword: return .funcKeyword
129+
case .identifier: return .identifier
130+
case .importKeyword: return .importKeyword
131+
case .initKeyword: return .initKeyword
132+
case .internalKeyword: return .internalKeyword
133+
case .letKeyword: return .letKeyword
134+
case .operatorKeyword: return .operatorKeyword
135+
case .poundErrorKeyword: return .poundErrorKeyword
136+
case .poundIfKeyword: return .poundIfKeyword
137+
case .poundLineKeyword: return .poundLineKeyword
138+
case .poundSourceLocationKeyword: return .poundSourceLocationKeyword
139+
case .poundWarningKeyword: return .poundWarningKeyword
140+
case .precedencegroupKeyword: return .precedencegroupKeyword
141+
case .privateKeyword: return .privateKeyword
142+
case .protocolKeyword: return .protocolKeyword
143+
case .publicKeyword: return .publicKeyword
144+
case .staticKeyword: return .staticKeyword
145+
case .structKeyword: return .structKeyword
146+
case .subscriptKeyword: return .subscriptKeyword
147+
case .typealiasKeyword: return .typealiasKeyword
148+
case .varKeyword: return .varKeyword
149+
}
150+
}
151+
152+
func accepts(lexeme: Lexer.Lexeme) -> Bool {
153+
switch self {
154+
case .poundLineKeyword:
155+
return lexeme.isAtStartOfLine
156+
default:
157+
return true
158+
}
159+
}
160+
}
161+
87162
enum ContextualDeclKeyword: SyntaxText, ContextualKeywords {
88163
case __consuming = "__consuming"
89164
case _compilerInitialized = "_compilerInitialized"

0 commit comments

Comments
 (0)