Skip to content

Commit 5238797

Browse files
committed
Allow specifying how tokens should be remapped in RawTokenKindSubset
We aren’t using this anywhere, but it seems like useful functionality in general.
1 parent 60b8322 commit 5238797

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

Sources/SwiftParser/RawTokenKindSubset.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ protocol RawTokenKindSubset: CaseIterable {
2121
/// Must only return a non-nil value if `rawTokenKind` is `identifier` or `contextualKeyword`.
2222
var contextualKeyword: SyntaxText? { get }
2323

24+
/// If not `nil`, the token's will be remapped to this kind when the handle is eaten.
25+
var remappedKind: RawTokenKind? { get }
26+
2427
/// Allows more flexible rejection of further token kinds based on the token's
2528
/// contents. Useful to e.g. look for contextual keywords.
2629
func accepts(lexeme: Lexer.Lexeme) -> Bool
@@ -31,6 +34,14 @@ extension RawTokenKindSubset {
3134
return nil
3235
}
3336

37+
var remappedKind: RawTokenKind? {
38+
if self.contextualKeyword != nil {
39+
return .contextualKeyword
40+
} else {
41+
return nil
42+
}
43+
}
44+
3445
func accepts(lexeme: Lexer.Lexeme) -> Bool {
3546
return true
3647
}

Sources/SwiftParser/TokenConsumer.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ public protocol TokenConsumer {
4141
struct TokenConsumptionHandle {
4242
/// The kind that is expected to be consumed if the handle is eaten.
4343
var tokenKind: RawTokenKind
44-
/// Whether the token should be remapped to a contextual keyword when eaten
45-
var remapToContextualKeyword: Bool
44+
/// When not `nil`, the token's kind will be remapped to this kind when consumed.
45+
var remappedKind: RawTokenKind?
4646
}
4747

4848
extension TokenConsumer {
@@ -106,7 +106,7 @@ extension TokenConsumer {
106106
if let matchedKind = Subset(self.currentToken) {
107107
return (matchedKind, TokenConsumptionHandle(
108108
tokenKind: matchedKind.rawTokenKind,
109-
remapToContextualKeyword: matchedKind.contextualKeyword != nil
109+
remappedKind: matchedKind.remappedKind
110110
))
111111
}
112112
return nil
@@ -115,8 +115,8 @@ extension TokenConsumer {
115115
/// Eat a token that we know we are currently positioned at, based on `at(anyIn:)`.
116116
mutating func eat(_ handle: TokenConsumptionHandle) -> Token {
117117
assert(self.at(handle.tokenKind))
118-
if handle.remapToContextualKeyword {
119-
return consumeAnyToken(remapping: .contextualKeyword)
118+
if let remappedKind = handle.remappedKind {
119+
return consumeAnyToken(remapping: remappedKind)
120120
} else {
121121
return consumeAnyToken()
122122
}

0 commit comments

Comments
 (0)