@@ -193,15 +193,15 @@ extension Lexer.Cursor {
193193 struct LexingDiagnostic {
194194 let kind : TokenDiagnostic . Kind
195195 /// The position in the token at which the diagnostic is.
196- let position : Lexer . Cursor
196+ let position : Lexer . Cursor . Position
197197
198198 init ( _ kind: TokenDiagnostic . Kind , position: Lexer . Cursor ) {
199199 self . kind = kind
200- self . position = position
200+ self . position = position. position
201201 }
202202
203203 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) )
205205 }
206206 }
207207}
@@ -215,18 +215,23 @@ extension Lexer {
215215 /// to reading bytes from an input buffer: all accesses to its input are
216216 /// bounds-checked.
217217 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+
220224 /// If we have already lexed a token, the kind of the previously lexed token
221225 var previousTokenKind : RawTokenKind ?
222226 private var stateStack : StateStack = StateStack ( )
223227
224228 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)
228230 }
229231
232+ var input : UnsafeBufferPointer < UInt8 > { position. input }
233+ var previous : UInt8 { position. previous }
234+
230235 var currentState : State {
231236 stateStack. currentState
232237 }
@@ -241,18 +246,18 @@ extension Lexer {
241246 }
242247
243248 var pointer : UnsafePointer < UInt8 > {
244- return self . input . baseAddress!
249+ self . position . pointer
245250 }
246251 func distance( to other: Self ) -> Int {
247- return self . pointer . distance ( to: other. pointer )
252+ self . position . distance ( to: other. position )
248253 }
249254
250255 var isAtEndOfFile : Bool {
251- return self . input . isEmpty
256+ self . position . isAtEndOfFile
252257 }
253258
254259 var isAtStartOfFile : Bool {
255- return ! self . input . isEmpty && self . previous == UInt8 ( ascii : " \0 " )
260+ self . position . isAtStartOfFile
256261 }
257262
258263 /// Debug function to print the remaining source text to be lexed.
@@ -297,6 +302,24 @@ extension Lexer {
297302 }
298303}
299304
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+
300323// MARK: - Entry point
301324
302325extension Lexer . Cursor {
@@ -489,7 +512,7 @@ extension Lexer.Cursor {
489512
490513// MARK: - Advancing the cursor
491514
492- extension Lexer . Cursor {
515+ extension Lexer . Cursor . Position {
493516 /// If there is a character in the input, and return it, advancing the cursor.
494517 /// If the end of the input is reached, return `nil`.
495518 mutating func advance( ) -> UInt8 ? {
@@ -501,6 +524,14 @@ extension Lexer.Cursor {
501524 self . input = UnsafeBufferPointer ( rebasing: input)
502525 return c
503526 }
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+ }
504535
505536 /// If the current character is `matching`, advance the cursor and return `true`.
506537 /// Otherwise, this is a no-op and returns `false`.
0 commit comments