@@ -193,15 +193,15 @@ extension Lexer.Cursor {
193
193
struct LexingDiagnostic {
194
194
let kind : TokenDiagnostic . Kind
195
195
/// The position in the token at which the diagnostic is.
196
- let position : Lexer . Cursor
196
+ let position : Lexer . Cursor . Position
197
197
198
198
init ( _ kind: TokenDiagnostic . Kind , position: Lexer . Cursor ) {
199
199
self . kind = kind
200
- self . position = position
200
+ self . position = position. position
201
201
}
202
202
203
203
func tokenDiagnostic( tokenStart: Lexer . Cursor ) -> TokenDiagnostic {
204
- return TokenDiagnostic ( kind, byteOffset: tokenStart. distance ( to: position) )
204
+ return TokenDiagnostic ( kind, byteOffset: tokenStart. position . distance ( to: position) )
205
205
}
206
206
}
207
207
}
@@ -215,18 +215,23 @@ extension Lexer {
215
215
/// to reading bytes from an input buffer: all accesses to its input are
216
216
/// bounds-checked.
217
217
struct Cursor {
218
- var input : UnsafeBufferPointer < UInt8 >
219
- var previous : UInt8
218
+ struct Position {
219
+ var input : UnsafeBufferPointer < UInt8 >
220
+ var previous : UInt8
221
+ }
222
+ var position : Position
223
+
220
224
/// If we have already lexed a token, the kind of the previously lexed token
221
225
var previousTokenKind : RawTokenKind ?
222
226
private var stateStack : StateStack = StateStack ( )
223
227
224
228
init ( input: UnsafeBufferPointer < UInt8 > , previous: UInt8 ) {
225
- self . input = input
226
- self . previous = previous
227
- self . stateStack = StateStack ( )
229
+ self . position = Position ( input: input, previous: previous)
228
230
}
229
231
232
+ var input : UnsafeBufferPointer < UInt8 > { position. input }
233
+ var previous : UInt8 { position. previous }
234
+
230
235
var currentState : State {
231
236
stateStack. currentState
232
237
}
@@ -241,18 +246,18 @@ extension Lexer {
241
246
}
242
247
243
248
var pointer : UnsafePointer < UInt8 > {
244
- return self . input . baseAddress!
249
+ self . position . pointer
245
250
}
246
251
func distance( to other: Self ) -> Int {
247
- return self . pointer . distance ( to: other. pointer )
252
+ self . position . distance ( to: other. position )
248
253
}
249
254
250
255
var isAtEndOfFile : Bool {
251
- return self . input . isEmpty
256
+ self . position . isAtEndOfFile
252
257
}
253
258
254
259
var isAtStartOfFile : Bool {
255
- return ! self . input . isEmpty && self . previous == UInt8 ( ascii : " \0 " )
260
+ self . position . isAtStartOfFile
256
261
}
257
262
258
263
/// Debug function to print the remaining source text to be lexed.
@@ -297,6 +302,24 @@ extension Lexer {
297
302
}
298
303
}
299
304
305
+ extension Lexer . Cursor . Position {
306
+ var pointer : UnsafePointer < UInt8 > {
307
+ self . input. baseAddress!
308
+ }
309
+
310
+ func distance( to other: Self ) -> Int {
311
+ self . pointer. distance ( to: other. pointer)
312
+ }
313
+
314
+ var isAtEndOfFile : Bool {
315
+ self . input. isEmpty
316
+ }
317
+
318
+ var isAtStartOfFile : Bool {
319
+ !self . input. isEmpty && self . previous == UInt8 ( ascii: " \0 " )
320
+ }
321
+ }
322
+
300
323
// MARK: - Entry point
301
324
302
325
extension Lexer . Cursor {
@@ -489,7 +512,7 @@ extension Lexer.Cursor {
489
512
490
513
// MARK: - Advancing the cursor
491
514
492
- extension Lexer . Cursor {
515
+ extension Lexer . Cursor . Position {
493
516
/// If there is a character in the input, and return it, advancing the cursor.
494
517
/// If the end of the input is reached, return `nil`.
495
518
mutating func advance( ) -> UInt8 ? {
@@ -501,6 +524,14 @@ extension Lexer.Cursor {
501
524
self . input = UnsafeBufferPointer ( rebasing: input)
502
525
return c
503
526
}
527
+ }
528
+
529
+ extension Lexer . Cursor {
530
+ /// If there is a character in the input, and return it, advancing the cursor.
531
+ /// If the end of the input is reached, return `nil`.
532
+ mutating func advance( ) -> UInt8 ? {
533
+ self . position. advance ( )
534
+ }
504
535
505
536
/// If the current character is `matching`, advance the cursor and return `true`.
506
537
/// Otherwise, this is a no-op and returns `false`.
0 commit comments