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 2 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: 30 additions & 6 deletions Sources/SwiftParser/Lexer/Cursor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,20 @@ extension Lexer.Cursor {
case .inRegexLiteral: return false
}
}

/// Returns whether the lexer is currently parsing the multiline string.
var isParsingMultilineString: Bool {
switch self {
case .normal, .preferRegexOverBinaryOperator: return false
case .afterRawStringDelimiter: return false
case .inStringLiteral(kind: let stringLiteralKind, delimiterLength: _): return stringLiteralKind == .multiLine
case .afterStringLiteral: return false
case .afterClosingStringQuote: return false
case .inStringInterpolationStart: return false
case .inStringInterpolation: return false
case .inRegexLiteral: return false
}
}
}

/// A data structure that holds the state stack entries in the lexer. It is
Expand Down Expand Up @@ -242,6 +256,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, the `NewlinePresence` of the previously lexed token
var previousTokenNewlinePresence: NewlinePresence?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a naming suggestion: I’d like to make it absolutely clear that this is about whether the previous token’s trailing trivia has a newline and avoid any confusion of whether the previous token had a newline inside its leading trivia. So I’d go with

Suggested change
/// If we have already lexed a token, the `NewlinePresence` of the previously lexed token
var previousTokenNewlinePresence: NewlinePresence?
/// If we have already lexed a token, stores whether the previous lexeme‘s trailing trivia contains a newline.
var previousLexemeTrailingTrivaNewlinePresence: NewlinePresence?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great suggestion! I will try to make the modifications along with the changes mentioned above. Thank you.


/// If the `previousTokenKind` is `.keyword`, the keyword kind. Otherwise
/// `nil`.
Expand Down Expand Up @@ -433,23 +450,30 @@ extension Lexer.Cursor {
if let stateTransition = result.stateTransition {
self.stateStack.perform(stateTransition: stateTransition, stateAllocator: stateAllocator)
}


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think swift-format will complain about this whitespace. Could you run format.py? https://github.com/apple/swift-syntax/blob/main/CONTRIBUTING.md#formatting

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, I missed that!
I'll make sure to run it before every commit.
Thanks for pointing that out 🙇

var flags = result.flags
if newlineInLeadingTrivia == .present {
flags.insert(.isAtStartOfLine)
}
if let previousTokenNewlinePresence, previousTokenNewlinePresence == .present,
!currentState.isParsingMultilineString {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need the isParsingMultilineString check? I just tried removing it and it looks like I just need to update three lexer tests, which seems perfectly reasonable to me because the line 1 and line 2 tokens really are on new lines.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In an effort to minimize side effects, I focused on maintaining the existing test cases, which led me to misunderstand the behavior of the lexer. As you mentioned, it is correct that the line1, line2, and whitespace tokens, excluding the """ character, are really on new lines.
I will try to make the modifications quickly within this week. Thank you for your prompt feedback 🙂

flags.insert(.isAtStartOfLine)
}

// Trailing trivia.
let trailingTriviaStart = self
if let trailingTriviaMode = result.trailingTriviaLexingMode ?? currentState.trailingTriviaLexingMode(cursor: self) {
let triviaResult = self.lexTrivia(mode: trailingTriviaMode)
self.previousTokenNewlinePresence = triviaResult.newlinePresence
diagnostic = TokenDiagnostic(combining: diagnostic, triviaResult.error?.tokenDiagnostic(tokenStart: cursor))
} else {
self.previousTokenNewlinePresence = nil
}

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
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
"""
)
}
}