Skip to content

Commit 078450b

Browse files
committed
[NFC] Thread experimental features through lexer
This capability is currently unused, but it’ll matter soon.
1 parent 18628b2 commit 078450b

File tree

7 files changed

+41
-16
lines changed

7 files changed

+41
-16
lines changed

Sources/SwiftParser/Lexer/Cursor.swift

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,8 @@ extension Lexer {
255255
}
256256
var position: Position
257257

258+
var experimentalFeatures: Parser.ExperimentalFeatures
259+
258260
/// If we have already lexed a token, the kind of the previously lexed token
259261
var previousTokenKind: RawTokenKind?
260262

@@ -267,8 +269,9 @@ extension Lexer {
267269

268270
private var stateStack: StateStack = StateStack()
269271

270-
init(input: UnsafeBufferPointer<UInt8>, previous: UInt8) {
272+
init(input: UnsafeBufferPointer<UInt8>, previous: UInt8, experimentalFeatures: Parser.ExperimentalFeatures) {
271273
self.position = Position(input: input, previous: previous)
274+
self.experimentalFeatures = experimentalFeatures
272275
}
273276

274277
/// Returns `true` if this cursor is sufficiently different to `other` in a way that indicates that the lexer has
@@ -2460,7 +2463,7 @@ extension Lexer.Cursor {
24602463
return false
24612464
}
24622465

2463-
guard let end = Self.findConflictEnd(start, markerKind: kind) else {
2466+
guard let end = Self.findConflictEnd(start, markerKind: kind, experimentalFeatures: experimentalFeatures) else {
24642467
// No end of conflict marker found.
24652468
return false
24662469
}
@@ -2476,13 +2479,18 @@ extension Lexer.Cursor {
24762479
}
24772480

24782481
/// Find the end of a version control conflict marker.
2479-
static func findConflictEnd(_ curPtr: Lexer.Cursor, markerKind: ConflictMarker) -> Lexer.Cursor? {
2482+
static func findConflictEnd(
2483+
_ curPtr: Lexer.Cursor,
2484+
markerKind: ConflictMarker,
2485+
experimentalFeatures: Parser.ExperimentalFeatures
2486+
) -> Lexer.Cursor? {
24802487
// Get a reference to the rest of the buffer minus the length of the start
24812488
// of the conflict marker.
24822489
let advanced = curPtr.input.baseAddress?.advanced(by: markerKind.introducer.count)
24832490
var restOfBuffer = Lexer.Cursor(
24842491
input: .init(start: advanced, count: curPtr.input.count - markerKind.introducer.count),
2485-
previous: curPtr.input[markerKind.introducer.count - 1]
2492+
previous: curPtr.input[markerKind.introducer.count - 1],
2493+
experimentalFeatures: experimentalFeatures
24862494
)
24872495
let terminator = markerKind.terminator
24882496
let terminatorStart = terminator.first!
@@ -2503,7 +2511,8 @@ extension Lexer.Cursor {
25032511
let advanced = restOfBuffer.input.baseAddress?.advanced(by: terminator.count)
25042512
return Lexer.Cursor(
25052513
input: .init(start: advanced, count: restOfBuffer.input.count - terminator.count),
2506-
previous: restOfBuffer.input[terminator.count - 1]
2514+
previous: restOfBuffer.input[terminator.count - 1],
2515+
experimentalFeatures: experimentalFeatures
25072516
)
25082517
}
25092518
return nil

Sources/SwiftParser/Lexer/LexemeSequence.swift

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,21 @@ extension Lexer {
152152
public static func tokenize(
153153
_ input: UnsafeBufferPointer<UInt8>,
154154
from startIndex: Int = 0,
155-
lookaheadTracker: UnsafeMutablePointer<LookaheadTracker>
155+
lookaheadTracker: UnsafeMutablePointer<LookaheadTracker>,
156+
experimentalFeatures: Parser.ExperimentalFeatures
156157
) -> LexemeSequence {
157158
precondition(input.isEmpty || startIndex < input.endIndex)
158159
let startChar = startIndex == input.startIndex ? UInt8(ascii: "\0") : input[startIndex - 1]
159-
let start = Cursor(input: input, previous: UInt8(ascii: "\0"))
160-
let cursor = Cursor(input: UnsafeBufferPointer(rebasing: input[startIndex...]), previous: startChar)
161-
return LexemeSequence(sourceBufferStart: start, cursor: cursor, lookaheadTracker: lookaheadTracker)
160+
let start = Cursor(input: input, previous: UInt8(ascii: "\0"), experimentalFeatures: experimentalFeatures)
161+
let cursor = Cursor(
162+
input: UnsafeBufferPointer(rebasing: input[startIndex...]),
163+
previous: startChar,
164+
experimentalFeatures: experimentalFeatures
165+
)
166+
return LexemeSequence(
167+
sourceBufferStart: start,
168+
cursor: cursor,
169+
lookaheadTracker: lookaheadTracker
170+
)
162171
}
163172
}

Sources/SwiftParser/Parser.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,11 @@ public struct Parser {
240240
self.experimentalFeatures = experimentalFeatures
241241
self.lookaheadTrackerOwner = LookaheadTrackerOwner()
242242

243-
self.lexemes = Lexer.tokenize(input, lookaheadTracker: lookaheadTrackerOwner.lookaheadTracker)
243+
self.lexemes = Lexer.tokenize(
244+
input,
245+
lookaheadTracker: lookaheadTrackerOwner.lookaheadTracker,
246+
experimentalFeatures: experimentalFeatures
247+
)
244248
self.currentToken = self.lexemes.advance()
245249
if let parseTransition {
246250
self.parseLookup = IncrementalParseLookup(transition: parseTransition)

Sources/SwiftParser/StringLiteralRepresentedLiteralValue.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ extension StringSegmentSyntax {
9090
}
9191

9292
rawText.withBuffer { buffer in
93-
var cursor = Lexer.Cursor(input: buffer, previous: 0)
93+
var cursor = Lexer.Cursor(input: buffer, previous: 0, experimentalFeatures: [])
9494

9595
// Put the cursor in the string literal lexing state. This is just
9696
// defensive as it's currently not used by `lexCharacterInStringLiteral`.

Sources/SwiftParser/TriviaParser.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ public struct TriviaParser {
2525
var pieces: [RawTriviaPiece] = []
2626
var cursor = Lexer.Cursor(
2727
input: UnsafeBufferPointer(start: source.baseAddress, count: source.count),
28-
previous: 0
28+
previous: 0,
29+
// There are currently no experimental features that affect trivia parsing.
30+
experimentalFeatures: []
2931
)
3032

3133
while true {

Tests/SwiftParserTest/Assertions.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ private func assertTokens(
190190
func assertLexemes(
191191
_ markedSource: String,
192192
lexemes expectedLexemes: [LexemeSpec],
193+
experimentalFeatures: Parser.ExperimentalFeatures = [],
193194
file: StaticString = #filePath,
194195
line: UInt = #line
195196
) {
@@ -206,7 +207,7 @@ func assertLexemes(
206207
lookaheadTracker.initialize(to: LookaheadTracker())
207208
source.withUTF8 { buf in
208209
var lexemes = [Lexer.Lexeme]()
209-
for token in Lexer.tokenize(buf, from: 0, lookaheadTracker: lookaheadTracker) {
210+
for token in Lexer.tokenize(buf, from: 0, lookaheadTracker: lookaheadTracker, experimentalFeatures: experimentalFeatures) {
210211
lexemes.append(token)
211212

212213
if token.rawTokenKind == .endOfFile {

Tests/SwiftParserTest/LexerTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
@_spi(RawSyntax) @_spi(Testing) import SwiftParser
14-
@_spi(RawSyntax) import SwiftSyntax
13+
@_spi(RawSyntax) @_spi(Testing) @_spi(ExperimentalLanguageFeatures) import SwiftParser
14+
@_spi(RawSyntax) @_spi(ExperimentalLanguageFeatures) import SwiftSyntax
1515
import XCTest
1616

1717
fileprivate func lex(_ sourceBytes: [UInt8], body: ([Lexer.Lexeme]) throws -> Void) rethrows {
@@ -22,7 +22,7 @@ fileprivate func lex(_ sourceBytes: [UInt8], body: ([Lexer.Lexeme]) throws -> Vo
2222
lookaheadTracker.initialize(to: LookaheadTracker())
2323
try sourceBytes.withUnsafeBufferPointer { (buf) in
2424
var lexemes = [Lexer.Lexeme]()
25-
for token in Lexer.tokenize(buf, from: 0, lookaheadTracker: lookaheadTracker) {
25+
for token in Lexer.tokenize(buf, from: 0, lookaheadTracker: lookaheadTracker, experimentalFeatures: []) {
2626
lexemes.append(token)
2727

2828
if token.rawTokenKind == .endOfFile {

0 commit comments

Comments
 (0)