@@ -187,15 +187,15 @@ extension Lexer.Cursor {
187187 struct LexingDiagnostic {
188188 let kind : TokenDiagnostic . Kind
189189 /// The position in the token at which the diagnostic is.
190- let position : Lexer . Cursor
190+ let position : Lexer . Cursor . Position
191191
192192 init ( _ kind: TokenDiagnostic . Kind , position: Lexer . Cursor ) {
193193 self . kind = kind
194- self . position = position
194+ self . position = position. position
195195 }
196196
197197 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) )
199199 }
200200 }
201201}
@@ -209,18 +209,23 @@ extension Lexer {
209209 /// to reading bytes from an input buffer: all accesses to its input are
210210 /// bounds-checked.
211211 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+
214218 /// If we have already lexed a token, the kind of the previously lexed token
215219 var previousTokenKind : RawTokenKind ?
216220 private var stateStack : StateStack = StateStack ( )
217221
218222 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)
222224 }
223225
226+ var input : UnsafeBufferPointer < UInt8 > { position. input }
227+ var previous : UInt8 { position. previous }
228+
224229 var currentState : State {
225230 stateStack. currentState
226231 }
@@ -235,18 +240,18 @@ extension Lexer {
235240 }
236241
237242 var pointer : UnsafePointer < UInt8 > {
238- return self . input . baseAddress!
243+ self . position . pointer
239244 }
240245 func distance( to other: Self ) -> Int {
241- return self . pointer . distance ( to: other. pointer )
246+ self . position . distance ( to: other. position )
242247 }
243248
244249 var isAtEndOfFile : Bool {
245- return self . input . isEmpty
250+ self . position . isAtEndOfFile
246251 }
247252
248253 var isAtStartOfFile : Bool {
249- return ! self . input . isEmpty && self . previous == UInt8 ( ascii : " \0 " )
254+ self . position . isAtStartOfFile
250255 }
251256
252257 /// Debug function to print the remaining source text to be lexed.
@@ -291,6 +296,24 @@ extension Lexer {
291296 }
292297}
293298
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+
294317// MARK: - Entry point
295318
296319extension Lexer . Cursor {
@@ -481,7 +504,7 @@ extension Lexer.Cursor {
481504
482505// MARK: - Advancing the cursor
483506
484- extension Lexer . Cursor {
507+ extension Lexer . Cursor . Position {
485508 /// If there is a character in the input, and return it, advancing the cursor.
486509 /// If the end of the input is reached, return `nil`.
487510 mutating func advance( ) -> UInt8 ? {
@@ -493,6 +516,14 @@ extension Lexer.Cursor {
493516 self . input = UnsafeBufferPointer ( rebasing: input)
494517 return c
495518 }
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+ }
496527
497528 /// If the current character is `matching`, advance the cursor and return `true`.
498529 /// Otherwise, this is a no-op and returns `false`.
0 commit comments