Skip to content

Fix newline parsing at trailing trivia #1661

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
May 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 26 additions & 10 deletions Sources/SwiftParser/Lexer/Cursor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,9 @@ extension Lexer {
/// If we have already lexed a token, the kind of the previously lexed token
var previousTokenKind: RawTokenKind?

/// If we have already lexed a token, stores whether the previous lexeme‘s ending contains a newline.
var previousLexemeTrailingNewlinePresence: NewlinePresence?

/// If the `previousTokenKind` is `.keyword`, the keyword kind. Otherwise
/// `nil`.
var previousKeyword: Keyword?
Expand Down Expand Up @@ -317,21 +320,25 @@ extension Lexer {
/// If `tokenKind` is `.keyword`, the kind of keyword produced, otherwise
/// `nil`.
let keywordKind: Keyword?
/// Indicates whether the end of the lexed token text contains a newline.
let trailingNewlinePresence: Lexer.Cursor.NewlinePresence

private init(
_ tokenKind: RawTokenKind,
flags: Lexer.Lexeme.Flags,
error: Cursor.LexingDiagnostic?,
stateTransition: StateTransition?,
trailingTriviaLexingMode: Lexer.Cursor.TriviaLexingMode?,
keywordKind: Keyword?
keywordKind: Keyword?,
trailingNewlinePresence: Lexer.Cursor.NewlinePresence
) {
self.tokenKind = tokenKind
self.flags = flags
self.error = error
self.stateTransition = stateTransition
self.trailingTriviaLexingMode = trailingTriviaLexingMode
self.keywordKind = keywordKind
self.trailingNewlinePresence = trailingNewlinePresence
}

/// Create a lexer result. Note that keywords should use `Result.keyword`
Expand All @@ -341,7 +348,8 @@ extension Lexer {
flags: Lexer.Lexeme.Flags = [],
error: Cursor.LexingDiagnostic? = nil,
stateTransition: StateTransition? = nil,
trailingTriviaLexingMode: Lexer.Cursor.TriviaLexingMode? = nil
trailingTriviaLexingMode: Lexer.Cursor.TriviaLexingMode? = nil,
trailingNewlinePresence: Lexer.Cursor.NewlinePresence = .absent
) {
precondition(tokenKind != .keyword, "Use Result.keyword instead")
self.init(
Expand All @@ -350,7 +358,8 @@ extension Lexer {
error: error,
stateTransition: stateTransition,
trailingTriviaLexingMode: trailingTriviaLexingMode,
keywordKind: nil
keywordKind: nil,
trailingNewlinePresence: trailingNewlinePresence
)
}

Expand All @@ -362,7 +371,8 @@ extension Lexer {
error: nil,
stateTransition: nil,
trailingTriviaLexingMode: nil,
keywordKind: kind
keywordKind: kind,
trailingNewlinePresence: .absent
)
}
}
Expand Down Expand Up @@ -430,6 +440,16 @@ extension Lexer.Cursor {
result = lexInRegexLiteral(lexemes.pointee[index...], existingPtr: lexemes)
}

var flags = result.flags
if newlineInLeadingTrivia == .present {
flags.insert(.isAtStartOfLine)
}
if let previousLexemeTrailingNewlinePresence, previousLexemeTrailingNewlinePresence == .present {
flags.insert(.isAtStartOfLine)
}

self.previousLexemeTrailingNewlinePresence = result.trailingNewlinePresence

if let stateTransition = result.stateTransition {
self.stateStack.perform(stateTransition: stateTransition, stateAllocator: stateAllocator)
}
Expand All @@ -438,18 +458,14 @@ extension Lexer.Cursor {
let trailingTriviaStart = self
if let trailingTriviaMode = result.trailingTriviaLexingMode ?? currentState.trailingTriviaLexingMode(cursor: self) {
let triviaResult = self.lexTrivia(mode: trailingTriviaMode)
self.previousLexemeTrailingNewlinePresence = triviaResult.newlinePresence
diagnostic = TokenDiagnostic(combining: diagnostic, triviaResult.error?.tokenDiagnostic(tokenStart: cursor))
}

if self.currentState.shouldPopStateWhenReachingNewlineInTrailingTrivia && self.is(at: "\r", "\n") {
self.stateStack.perform(stateTransition: .pop, stateAllocator: stateAllocator)
}

var flags = result.flags
if newlineInLeadingTrivia == .present {
flags.insert(.isAtStartOfLine)
}

diagnostic = TokenDiagnostic(combining: diagnostic, result.error?.tokenDiagnostic(tokenStart: cursor))

let lexeme = Lexer.Lexeme(
Expand Down Expand Up @@ -1889,7 +1905,7 @@ extension Lexer.Cursor {
if character == UInt8(ascii: "\r") {
_ = self.advance(matching: "\n")
}
return Lexer.Result(.stringSegment, error: error)
return Lexer.Result(.stringSegment, error: error, trailingNewlinePresence: .present)
} else {
// Single line literals cannot span multiple lines.
// Terminate the string here and go back to normal lexing (instead of `afterStringLiteral`)
Expand Down
12 changes: 6 additions & 6 deletions Tests/SwiftParserTest/LexerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1182,9 +1182,9 @@ public class LexerTests: XCTestCase {
"""#,
lexemes: [
LexemeSpec(.multilineStringQuote, leading: " ", text: #"""""#, trailing: "\n"),
LexemeSpec(.stringSegment, text: " line 1\n"),
LexemeSpec(.stringSegment, text: " line 2\n"),
LexemeSpec(.stringSegment, text: " "),
LexemeSpec(.stringSegment, text: " line 1\n", flags: .isAtStartOfLine),
LexemeSpec(.stringSegment, text: " line 2\n", flags: .isAtStartOfLine),
LexemeSpec(.stringSegment, text: " ", flags: .isAtStartOfLine),
LexemeSpec(.multilineStringQuote, text: #"""""#),
]
)
Expand All @@ -1198,9 +1198,9 @@ public class LexerTests: XCTestCase {
"""#,
lexemes: [
LexemeSpec(.multilineStringQuote, leading: " ", text: #"""""#, trailing: "\n"),
LexemeSpec(.stringSegment, text: " line 1 ", trailing: "\\\n"),
LexemeSpec(.stringSegment, text: " line 2\n"),
LexemeSpec(.stringSegment, text: " "),
LexemeSpec(.stringSegment, text: " line 1 ", trailing: "\\\n", flags: .isAtStartOfLine),
LexemeSpec(.stringSegment, text: " line 2\n", flags: .isAtStartOfLine),
LexemeSpec(.stringSegment, text: " ", flags: .isAtStartOfLine),
LexemeSpec(.multilineStringQuote, text: #"""""#),
]
)
Expand Down
19 changes: 19 additions & 0 deletions Tests/SwiftParserTest/StatementTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -763,4 +763,23 @@ final class StatementTests: XCTestCase {
"""
)
}

func testTrailingTriviaIncludesNewline() {
assertParse(
"""
let a = 2/*
*/let b = 3
"""
)

assertParse(
"""
let a = 2/*



*/let b = 3
"""
)
}
}