@@ -187,15 +187,15 @@ extension Lexer.Cursor {
187
187
struct LexingDiagnostic {
188
188
let kind : TokenDiagnostic . Kind
189
189
/// The position in the token at which the diagnostic is.
190
- let position : Lexer . Cursor
190
+ let position : Lexer . Cursor . Position
191
191
192
192
init ( _ kind: TokenDiagnostic . Kind , position: Lexer . Cursor ) {
193
193
self . kind = kind
194
- self . position = position
194
+ self . position = position. position
195
195
}
196
196
197
197
func tokenDiagnostic( tokenStart: Lexer . Cursor ) -> TokenDiagnostic {
198
- return TokenDiagnostic ( kind, byteOffset: tokenStart. distance ( to: position) )
198
+ return TokenDiagnostic ( kind, byteOffset: tokenStart. position . distance ( to: position) )
199
199
}
200
200
}
201
201
}
@@ -209,18 +209,23 @@ extension Lexer {
209
209
/// to reading bytes from an input buffer: all accesses to its input are
210
210
/// bounds-checked.
211
211
struct Cursor {
212
- var input : UnsafeBufferPointer < UInt8 >
213
- var previous : UInt8
212
+ struct Position {
213
+ var input : UnsafeBufferPointer < UInt8 >
214
+ var previous : UInt8
215
+ }
216
+ var position : Position
217
+
214
218
/// If we have already lexed a token, the kind of the previously lexed token
215
219
var previousTokenKind : RawTokenKind ?
216
220
private var stateStack : StateStack = StateStack ( )
217
221
218
222
init ( input: UnsafeBufferPointer < UInt8 > , previous: UInt8 ) {
219
- self . input = input
220
- self . previous = previous
221
- self . stateStack = StateStack ( )
223
+ self . position = Position ( input: input, previous: previous)
222
224
}
223
225
226
+ var input : UnsafeBufferPointer < UInt8 > { position. input }
227
+ var previous : UInt8 { position. previous }
228
+
224
229
var currentState : State {
225
230
stateStack. currentState
226
231
}
@@ -235,18 +240,18 @@ extension Lexer {
235
240
}
236
241
237
242
var pointer : UnsafePointer < UInt8 > {
238
- return self . input . baseAddress!
243
+ self . position . pointer
239
244
}
240
245
func distance( to other: Self ) -> Int {
241
- return self . pointer . distance ( to: other. pointer )
246
+ self . position . distance ( to: other. position )
242
247
}
243
248
244
249
var isAtEndOfFile : Bool {
245
- return self . input . isEmpty
250
+ self . position . isAtEndOfFile
246
251
}
247
252
248
253
var isAtStartOfFile : Bool {
249
- return ! self . input . isEmpty && self . previous == UInt8 ( ascii : " \0 " )
254
+ self . position . isAtStartOfFile
250
255
}
251
256
252
257
/// Debug function to print the remaining source text to be lexed.
@@ -291,6 +296,24 @@ extension Lexer {
291
296
}
292
297
}
293
298
299
+ extension Lexer . Cursor . Position {
300
+ var pointer : UnsafePointer < UInt8 > {
301
+ self . input. baseAddress!
302
+ }
303
+
304
+ func distance( to other: Self ) -> Int {
305
+ self . pointer. distance ( to: other. pointer)
306
+ }
307
+
308
+ var isAtEndOfFile : Bool {
309
+ self . input. isEmpty
310
+ }
311
+
312
+ var isAtStartOfFile : Bool {
313
+ !self . input. isEmpty && self . previous == UInt8 ( ascii: " \0 " )
314
+ }
315
+ }
316
+
294
317
// MARK: - Entry point
295
318
296
319
extension Lexer . Cursor {
@@ -481,7 +504,7 @@ extension Lexer.Cursor {
481
504
482
505
// MARK: - Advancing the cursor
483
506
484
- extension Lexer . Cursor {
507
+ extension Lexer . Cursor . Position {
485
508
/// If there is a character in the input, and return it, advancing the cursor.
486
509
/// If the end of the input is reached, return `nil`.
487
510
mutating func advance( ) -> UInt8 ? {
@@ -493,6 +516,14 @@ extension Lexer.Cursor {
493
516
self . input = UnsafeBufferPointer ( rebasing: input)
494
517
return c
495
518
}
519
+ }
520
+
521
+ extension Lexer . Cursor {
522
+ /// If there is a character in the input, and return it, advancing the cursor.
523
+ /// If the end of the input is reached, return `nil`.
524
+ mutating func advance( ) -> UInt8 ? {
525
+ self . position. advance ( )
526
+ }
496
527
497
528
/// If the current character is `matching`, advance the cursor and return `true`.
498
529
/// Otherwise, this is a no-op and returns `false`.
0 commit comments