-
Notifications
You must be signed in to change notification settings - Fork 441
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
Changes from 4 commits
9b2ea0d
2ab4021
79c1b1a
d852fb9
1b18fdb
8e6ce86
fb134e9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -242,6 +242,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`. | ||||||
|
@@ -402,6 +405,15 @@ extension Lexer.Cursor { | |||||
} else { | ||||||
newlineInLeadingTrivia = .absent | ||||||
} | ||||||
|
||||||
var flags: Lexer.Lexeme.Flags = [] | ||||||
if newlineInLeadingTrivia == .present { | ||||||
flags.insert(.isAtStartOfLine) | ||||||
} | ||||||
if let previousLexemeTrailingNewlinePresence, previousLexemeTrailingNewlinePresence == .present { | ||||||
flags.insert(.isAtStartOfLine) | ||||||
} | ||||||
self.previousLexemeTrailingNewlinePresence = nil | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Resetting to If you do that, I think you can also move this entire code block below the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As you said, it seems like it should have a non-nil value when lexing the previous token. The code above makes it look like I need to put How about assigning
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Additionally, if we move this block of code below the token text lexing section, the value assigned in the token text lexing logic will affect the flag value of the current token, so it would be difficult to move it 🤔 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, now that I think about it, inside the lexing logic, it makes more sense to include the flag in the lexer's result and return it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, that’s just what I was just about to suggest There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar to |
||||||
|
||||||
// Token text. | ||||||
let textStart = self | ||||||
|
@@ -433,28 +445,24 @@ extension Lexer.Cursor { | |||||
if let stateTransition = result.stateTransition { | ||||||
self.stateStack.perform(stateTransition: stateTransition, stateAllocator: stateAllocator) | ||||||
} | ||||||
|
||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oops, I missed that! |
||||||
// Trailing trivia. | ||||||
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( | ||||||
tokenKind: result.tokenKind, | ||||||
flags: flags, | ||||||
flags: result.flags.union(flags), | ||||||
diagnostic: diagnostic, | ||||||
start: leadingTriviaStart.pointer, | ||||||
leadingTriviaLength: leadingTriviaStart.distance(to: textStart), | ||||||
|
@@ -1889,6 +1897,7 @@ extension Lexer.Cursor { | |||||
if character == UInt8(ascii: "\r") { | ||||||
_ = self.advance(matching: "\n") | ||||||
} | ||||||
self.previousLexemeTrailingNewlinePresence = .present | ||||||
return Lexer.Result(.stringSegment, error: error) | ||||||
} else { | ||||||
// Single line literals cannot span multiple lines. | ||||||
|
Uh oh!
There was an error while loading. Please reload this page.