Skip to content

Replace offset(of: ) with getOffsetToStart #1899

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
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
4 changes: 2 additions & 2 deletions Sources/SwiftParser/IncrementalParseTransition.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ extension Parser {
return nil
}

let currentOffset = self.lexemes.getOffsetToStart(self.currentToken)
let currentOffset = self.lexemes.offsetToStart(self.currentToken)
if let node = parseLookup!.lookUp(currentOffset, kind: kind) {
self.lexemes.advance(by: node.byteSize, currentToken: &self.currentToken)
return node
Expand All @@ -30,7 +30,7 @@ extension Parser {
mutating func registerNodeForIncrementalParse(node: RawSyntax, startToken: Lexer.Lexeme) {
lookaheadRanges.registerNodeForIncrementalParse(
node: node,
lookaheadLength: lexemes.lookaheadTracker.pointee.furthestOffset - self.lexemes.getOffsetToStart(startToken)
lookaheadLength: lexemes.lookaheadTracker.pointee.furthestOffset - self.lexemes.offsetToStart(startToken)
)
}
}
Expand Down
18 changes: 2 additions & 16 deletions Sources/SwiftParser/Lexer/LexemeSequence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ extension Lexer {

/// 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
self.offsetToStart(self.nextToken) + self.nextToken.byteLength
}

/// See doc comments in ``LookaheadTracker``
Expand Down Expand Up @@ -70,7 +70,7 @@ extension Lexer {
}

/// Get the offset of the leading trivia start of `token` relative to `sourceBufferStart`.
func getOffsetToStart(_ token: Lexer.Lexeme) -> Int {
func offsetToStart(_ token: Lexer.Lexeme) -> Int {
return self.sourceBufferStart.distance(to: token.cursor)
}

Expand Down Expand Up @@ -122,20 +122,6 @@ extension Lexer {
return remainingText
}
}

#if SWIFTPARSER_ENABLE_ALTERNATE_TOKEN_INTROSPECTION
/// If `pointer` is in the source buffer of this ``LexemeSequence``, return
/// its offset, otherwise `nil`. Should only be used for the parser's
/// alternate token introspection
func offset(of pointer: UnsafePointer<UInt8>) -> Int? {
let offset = pointer - self.sourceBufferStart.input.baseAddress!
if offset <= self.sourceBufferStart.input.count {
return offset
} else {
return nil
}
}
#endif
}

@_spi(Testing)
Expand Down
6 changes: 1 addition & 5 deletions Sources/SwiftParser/Parser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -250,11 +250,7 @@ public struct Parser {
public var alternativeTokenChoices: [Int: [TokenSpec]] = [:]

mutating func recordAlternativeTokenChoice(for lexeme: Lexer.Lexeme, choices: [TokenSpec]) {
guard let lexemeBaseAddress = lexeme.tokenText.baseAddress,
let offset = lexemes.offset(of: lexemeBaseAddress)
else {
return
}
let offset = lexemes.offsetToStart(lexeme)
alternativeTokenChoices[offset, default: []].append(contentsOf: choices)
}
#endif
Expand Down