Skip to content

Commit 42a40b3

Browse files
committed
Track the previous keyword in Cursor
We currently track the previous raw token kind, but that is not sufficient to identify keyword kinds. Additionally store the keyword kind.
1 parent 3886e22 commit 42a40b3

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

Sources/SwiftParser/Lexer/Cursor.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,8 @@ extension Lexer {
217217

218218
/// If we have already lexed a token, the kind of the previously lexed token
219219
var previousTokenKind: RawTokenKind?
220+
var previousKeyword: Keyword?
221+
220222
private var stateStack: StateStack = StateStack()
221223

222224
init(input: UnsafeBufferPointer<UInt8>, previous: UInt8) {
@@ -372,10 +374,9 @@ extension Lexer.Cursor {
372374
flags.insert(.isAtStartOfLine)
373375
}
374376

375-
self.previousTokenKind = result.tokenKind
376377
diagnostic = TokenDiagnostic(combining: diagnostic, result.error?.tokenDiagnostic(tokenStart: cursor))
377378

378-
return .init(
379+
let lexeme = Lexer.Lexeme(
379380
tokenKind: result.tokenKind,
380381
flags: flags,
381382
diagnostic: diagnostic,
@@ -385,6 +386,10 @@ extension Lexer.Cursor {
385386
trailingTriviaLength: trailingTriviaStart.distance(to: self),
386387
cursor: cursor
387388
)
389+
self.previousTokenKind = result.tokenKind
390+
self.previousKeyword = result.tokenKind == .keyword ? Keyword(lexeme.tokenText)! : nil
391+
392+
return lexeme
388393
}
389394

390395
}

0 commit comments

Comments
 (0)