Skip to content

Commit d0cca32

Browse files
committed
Replace offset(of: ) with offsetToStart
1 parent b0f8e70 commit d0cca32

File tree

3 files changed

+5
-23
lines changed

3 files changed

+5
-23
lines changed

Sources/SwiftParser/IncrementalParseTransition.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ extension Parser {
1818
return nil
1919
}
2020

21-
let currentOffset = self.lexemes.getOffsetToStart(self.currentToken)
21+
let currentOffset = self.lexemes.offsetToStart(self.currentToken)
2222
if let node = parseLookup!.lookUp(currentOffset, kind: kind) {
2323
self.lexemes.advance(by: node.byteSize, currentToken: &self.currentToken)
2424
return node
@@ -30,7 +30,7 @@ extension Parser {
3030
mutating func registerNodeForIncrementalParse(node: RawSyntax, startToken: Lexer.Lexeme) {
3131
lookaheadRanges.registerNodeForIncrementalParse(
3232
node: node,
33-
lookaheadLength: lexemes.lookaheadTracker.pointee.furthestOffset - self.lexemes.getOffsetToStart(startToken)
33+
lookaheadLength: lexemes.lookaheadTracker.pointee.furthestOffset - self.lexemes.offsetToStart(startToken)
3434
)
3535
}
3636
}

Sources/SwiftParser/Lexer/LexemeSequence.swift

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ extension Lexer {
3434

3535
/// The offset of the trailing trivia end of `nextToken` relative to the source buffer’s start.
3636
var offsetToNextTokenEnd: Int {
37-
self.getOffsetToStart(self.nextToken) + self.nextToken.byteLength
37+
self.offsetToStart(self.nextToken) + self.nextToken.byteLength
3838
}
3939

4040
/// See doc comments in ``LookaheadTracker``
@@ -70,7 +70,7 @@ extension Lexer {
7070
}
7171

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

@@ -122,20 +122,6 @@ extension Lexer {
122122
return remainingText
123123
}
124124
}
125-
126-
#if SWIFTPARSER_ENABLE_ALTERNATE_TOKEN_INTROSPECTION
127-
/// If `pointer` is in the source buffer of this ``LexemeSequence``, return
128-
/// its offset, otherwise `nil`. Should only be used for the parser's
129-
/// alternate token introspection
130-
func offset(of pointer: UnsafePointer<UInt8>) -> Int? {
131-
let offset = pointer - self.sourceBufferStart.input.baseAddress!
132-
if offset <= self.sourceBufferStart.input.count {
133-
return offset
134-
} else {
135-
return nil
136-
}
137-
}
138-
#endif
139125
}
140126

141127
@_spi(Testing)

Sources/SwiftParser/Parser.swift

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -250,11 +250,7 @@ public struct Parser {
250250
public var alternativeTokenChoices: [Int: [TokenSpec]] = [:]
251251

252252
mutating func recordAlternativeTokenChoice(for lexeme: Lexer.Lexeme, choices: [TokenSpec]) {
253-
guard let lexemeBaseAddress = lexeme.tokenText.baseAddress,
254-
let offset = lexemes.offset(of: lexemeBaseAddress)
255-
else {
256-
return
257-
}
253+
let offset = lexemes.offsetToStart(lexeme)
258254
alternativeTokenChoices[offset, default: []].append(contentsOf: choices)
259255
}
260256
#endif

0 commit comments

Comments
 (0)