-
Notifications
You must be signed in to change notification settings - Fork 439
Port incremental parsing ability to CodeBlockItem
#1685
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
ahoppen
merged 1 commit into
swiftlang:main
from
StevenWong12:enable_incremental_parsing
Jul 11, 2023
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,24 +32,60 @@ extension Lexer { | |
/// usually less than 0.1% of the memory allocated by the syntax arena. | ||
var lexerStateAllocator = BumpPtrAllocator(slabSize: 256) | ||
|
||
fileprivate init(sourceBufferStart: Lexer.Cursor, cursor: Lexer.Cursor) { | ||
/// The offset of the trailing trivia end of `nextToken` relative to the source buffer’s start. | ||
var offsetToNextTokenEnd: Int { | ||
self.getOffsetToStart(self.nextToken) + self.nextToken.byteLength | ||
} | ||
|
||
/// See doc comments in ``LookaheadTracker`` | ||
StevenWong12 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// | ||
/// This is an `UnsafeMutablePointer` for two reasons | ||
/// - When `LexemeSequence` gets copied (e.g. when a ``Lookahead`` gets created), it should still reference the same ``LookaheadTracker`` so that any lookahead performed in the ``Lookahead`` also affects the original ``Parser``. It thus needs to be a reference type | ||
/// - ``LookaheadTracker`` is not a class to avoid reference counting it. The ``Parser`` that creates the ``LexemeSequence`` will always outlive any ``Lookahead`` created for it. | ||
let lookaheadTracker: UnsafeMutablePointer<LookaheadTracker> | ||
|
||
fileprivate init(sourceBufferStart: Lexer.Cursor, cursor: Lexer.Cursor, lookaheadTracker: UnsafeMutablePointer<LookaheadTracker>) { | ||
self.sourceBufferStart = sourceBufferStart | ||
self.cursor = cursor | ||
self.nextToken = self.cursor.nextToken(sourceBufferStart: self.sourceBufferStart, stateAllocator: lexerStateAllocator) | ||
self.lookaheadTracker = lookaheadTracker | ||
} | ||
|
||
@_spi(Testing) | ||
public mutating func next() -> Lexer.Lexeme? { | ||
return self.advance() | ||
} | ||
|
||
/// Record the offset of the end of `nextToken` as the furthest offset in ``LookaheadTracker`` | ||
private func recordNextTokenInLookaheadTracker() { | ||
self.lookaheadTracker.pointee.recordFurthestOffset(self.offsetToNextTokenEnd) | ||
} | ||
|
||
mutating func advance() -> Lexer.Lexeme { | ||
defer { | ||
self.nextToken = self.cursor.nextToken(sourceBufferStart: self.sourceBufferStart, stateAllocator: lexerStateAllocator) | ||
} | ||
self.recordNextTokenInLookaheadTracker() | ||
return self.nextToken | ||
} | ||
|
||
/// Get the offset of the leading trivia start of `token` relative to `sourceBufferStart`. | ||
func getOffsetToStart(_ token: Lexer.Lexeme) -> Int { | ||
return self.sourceBufferStart.distance(to: token.cursor) | ||
} | ||
Comment on lines
+73
to
+75
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. Not part of this PR: Can we use |
||
|
||
/// Advance the the cursor by `offset` and reset `currentToken` | ||
StevenWong12 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// | ||
/// - Important: This should only be used for incremental parsing. | ||
mutating func advance(by offset: Int, currentToken: inout Lexer.Lexeme) { | ||
self.cursor = currentToken.cursor | ||
self.cursor.position = self.cursor.position.advanced(by: offset) | ||
|
||
self.nextToken = self.cursor.nextToken(sourceBufferStart: self.sourceBufferStart, stateAllocator: lexerStateAllocator) | ||
|
||
currentToken = self.advance() | ||
} | ||
|
||
/// Reset the lexeme sequence to the state we were in when lexing `splitToken` | ||
/// but after we consumed `consumedPrefix` bytes from `splitToken`. | ||
/// - Warning: Do not add more usages of this function. | ||
|
@@ -63,6 +99,7 @@ extension Lexer { | |
} | ||
|
||
func peek() -> Lexer.Lexeme { | ||
self.recordNextTokenInLookaheadTracker() | ||
return self.nextToken | ||
} | ||
|
||
|
@@ -104,12 +141,13 @@ extension Lexer { | |
@_spi(Testing) | ||
public static func tokenize( | ||
_ input: UnsafeBufferPointer<UInt8>, | ||
from startIndex: Int = 0 | ||
from startIndex: Int = 0, | ||
lookaheadTracker: UnsafeMutablePointer<LookaheadTracker> | ||
) -> LexemeSequence { | ||
precondition(input.isEmpty || startIndex < input.endIndex) | ||
let startChar = startIndex == input.startIndex ? UInt8(ascii: "\0") : input[startIndex - 1] | ||
let start = Cursor(input: input, previous: UInt8(ascii: "\0")) | ||
let cursor = Cursor(input: UnsafeBufferPointer(rebasing: input[startIndex...]), previous: startChar) | ||
return LexemeSequence(sourceBufferStart: start, cursor: cursor) | ||
return LexemeSequence(sourceBufferStart: start, cursor: cursor, lookaheadTracker: lookaheadTracker) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Definitely not part of this PR but I think we should consider change
IncrementalParseReusedNodeDelegate
to just be a callback function onIncrementalParseTransition
, i.e. removing this protocol altogether. That’s more swifty than the Objective-C delegate pattern.