From 17289d480c153200c2944b260aa50b58b904e8be Mon Sep 17 00:00:00 2001 From: Hamish Knight Date: Thu, 23 Mar 2023 13:13:22 +0000 Subject: [PATCH] Remove check for `endOfFile` in `advance` Instead, let the lexer produce `.eof` itself. This will be needed for regex literal lexing, where the lexer may still want to produce an empty regex literal pattern when at the end of the file, before the `.eof` token. --- Sources/SwiftParser/Lexer/Cursor.swift | 2 ++ Sources/SwiftParser/Lexer/LexemeSequence.swift | 15 +-------------- 2 files changed, 3 insertions(+), 14 deletions(-) diff --git a/Sources/SwiftParser/Lexer/Cursor.swift b/Sources/SwiftParser/Lexer/Cursor.swift index 16f5deb09b1..9717ba1a2fc 100644 --- a/Sources/SwiftParser/Lexer/Cursor.swift +++ b/Sources/SwiftParser/Lexer/Cursor.swift @@ -1772,6 +1772,8 @@ extension Lexer.Cursor { } mutating func lexInStringLiteral(stringLiteralKind: StringLiteralKind, delimiterLength: Int) -> Lexer.Result { + if self.isAtEndOfFile { return .init(.eof) } + var error: LexingDiagnostic? = nil while true { diff --git a/Sources/SwiftParser/Lexer/LexemeSequence.swift b/Sources/SwiftParser/Lexer/LexemeSequence.swift index 138e6f74ba3..266cbe4db9c 100644 --- a/Sources/SwiftParser/Lexer/LexemeSequence.swift +++ b/Sources/SwiftParser/Lexer/LexemeSequence.swift @@ -43,20 +43,7 @@ extension Lexer { mutating func advance() -> Lexer.Lexeme { defer { - if self.cursor.isAtEndOfFile { - self.nextToken = Lexeme( - tokenKind: .eof, - flags: [], - diagnostic: nil, - start: self.cursor.pointer, - leadingTriviaLength: 0, - textLength: 0, - trailingTriviaLength: 0, - cursor: self.cursor - ) - } else { - self.nextToken = self.cursor.nextToken(sourceBufferStart: self.sourceBufferStart, stateAllocator: lexerStateAllocator) - } + self.nextToken = self.cursor.nextToken(sourceBufferStart: self.sourceBufferStart, stateAllocator: lexerStateAllocator) } return self.nextToken }