@@ -249,175 +249,4 @@ public struct RawTokenSyntax: RawSyntaxNodeProtocol {
249
249
arena: arena
250
250
)
251
251
}
252
-
253
- /// Assuming that text representing `extendedTrivia` preceeds this token,
254
- /// return a token that has its leading trivia prepended by `extendedTrivia`.
255
- /// This can be used to transfer trivia from a preceeding token to this token.
256
- /// The caller is responsible to delete preceeding trivia from the tree to
257
- /// maintain source-fidelity.
258
- public func extendingLeadingTrivia( by extendedTrivia: [ RawTriviaPiece ] , arena: SyntaxArena ) -> RawTokenSyntax {
259
- let extendedTriviaByteLength = extendedTrivia. reduce ( 0 , { $0 + $1. byteLength } )
260
- switch raw. rawData. payload {
261
- case . parsedToken( let dat) :
262
- assert (
263
- String ( syntaxText: SyntaxText ( baseAddress: dat. wholeText. baseAddress? . advanced ( by: - extendedTriviaByteLength) , count: extendedTriviaByteLength) )
264
- == Trivia ( pieces: extendedTrivia. map ( TriviaPiece . init) ) . description
265
- )
266
- let wholeText = SyntaxText ( baseAddress: dat. wholeText. baseAddress? . advanced ( by: - extendedTriviaByteLength) , count: dat. wholeText. count + extendedTriviaByteLength)
267
- let textRange = ( dat. textRange. lowerBound + extendedTriviaByteLength) ..< ( dat. textRange. upperBound + extendedTriviaByteLength)
268
- return RawSyntax . parsedToken (
269
- kind: dat. tokenKind,
270
- wholeText: arena. intern ( wholeText) ,
271
- textRange: textRange,
272
- presence: dat. presence,
273
- lexerError: dat. lexerError,
274
- arena: arena
275
- ) . as ( RawTokenSyntax . self) !
276
- case . materializedToken( let dat) :
277
- let triviaBuffer = arena. allocateRawTriviaPieceBuffer ( count: dat. triviaPieces. count + extendedTrivia. count)
278
- let ( _, extenedTriviaEndIndex) = triviaBuffer. initialize ( from: extendedTrivia)
279
- let ( _, triviaEndIndex) = triviaBuffer [ extenedTriviaEndIndex... ] . initialize ( from: dat. triviaPieces)
280
- assert ( triviaEndIndex == triviaBuffer. endIndex)
281
- return RawSyntax . materializedToken (
282
- kind: dat. tokenKind,
283
- text: dat. tokenText,
284
- triviaPieces: RawTriviaPieceBuffer ( triviaBuffer) ,
285
- numLeadingTrivia: dat. numLeadingTrivia + UInt32( extendedTrivia. count) ,
286
- byteLength: dat. byteLength + UInt32( extendedTriviaByteLength) ,
287
- presence: dat. presence,
288
- lexerError: dat. lexerError,
289
- arena: arena
290
- ) . as ( RawTokenSyntax . self) !
291
- case . layout( _) :
292
- preconditionFailure ( " Should be a token " )
293
- }
294
- }
295
-
296
- /// Assuming that text representing `extendedTrivia` comes after this token,
297
- /// return a token that has its trailing trivia appended by `extendedTrivia`.
298
- /// This can be used to transfer trivia from the next token to this token.
299
- /// The caller is responsible to delete succeeding trivia from the tree to
300
- /// maintain source-fidelity.
301
- public func extendingTrailingTrivia( by extendedTrivia: [ RawTriviaPiece ] , arena: SyntaxArena ) -> RawTokenSyntax {
302
- let extendedTriviaByteLength = extendedTrivia. reduce ( 0 , { $0 + $1. byteLength } )
303
- switch raw. rawData. payload {
304
- case . parsedToken( let dat) :
305
- assert (
306
- String ( syntaxText: SyntaxText ( baseAddress: dat. wholeText. baseAddress? . advanced ( by: dat. wholeText. count) , count: extendedTriviaByteLength) )
307
- == Trivia ( pieces: extendedTrivia. map ( TriviaPiece . init) ) . description
308
- )
309
- let wholeText = SyntaxText ( baseAddress: dat. wholeText. baseAddress, count: dat. wholeText. count + extendedTriviaByteLength)
310
- return RawSyntax . parsedToken (
311
- kind: dat. tokenKind,
312
- wholeText: arena. intern ( wholeText) ,
313
- textRange: dat. textRange,
314
- presence: dat. presence,
315
- lexerError: dat. lexerError,
316
- arena: arena
317
- ) . as ( RawTokenSyntax . self) !
318
- case . materializedToken( let dat) :
319
- let triviaBuffer = arena. allocateRawTriviaPieceBuffer ( count: dat. triviaPieces. count + extendedTrivia. count)
320
- let ( _, existingTriviaEndIndex) = triviaBuffer. initialize ( from: dat. triviaPieces)
321
- let ( _, triviaEndIndex) = triviaBuffer [ existingTriviaEndIndex... ] . initialize ( from: extendedTrivia)
322
- assert ( triviaEndIndex == triviaBuffer. endIndex)
323
- return RawSyntax . materializedToken (
324
- kind: dat. tokenKind,
325
- text: dat. tokenText,
326
- triviaPieces: RawTriviaPieceBuffer ( triviaBuffer) ,
327
- numLeadingTrivia: dat. numLeadingTrivia,
328
- byteLength: dat. byteLength + UInt32( extendedTriviaByteLength) ,
329
- presence: dat. presence,
330
- lexerError: dat. lexerError,
331
- arena: arena
332
- ) . as ( RawTokenSyntax . self) !
333
- case . layout( _) :
334
- preconditionFailure ( " Should be a token " )
335
- }
336
- }
337
-
338
- /// Assuming that the tokens tet starts with text representing `reclassifiedTrivia`,
339
- /// re-classify those characters as no longer being part of the token's text
340
- /// but as part of the token's leading trivia.
341
- /// If `error` is not `nil` and the token currently doesn't have a lexer error,
342
- /// that error will be set as the lexer error.
343
- public func reclassifyAsLeadingTrivia( _ reclassifiedTrivia: [ RawTriviaPiece ] , lexerError: LexerError ? = nil , arena: SyntaxArena ) -> RawTokenSyntax {
344
- let reclassifiedTriviaByteLength = reclassifiedTrivia. reduce ( 0 , { $0 + $1. byteLength } )
345
- assert (
346
- String ( syntaxText: SyntaxText ( rebasing: self . tokenText [ 0 ..< reclassifiedTriviaByteLength] ) )
347
- == Trivia ( pieces: reclassifiedTrivia. map ( TriviaPiece . init) ) . description
348
- )
349
- switch raw. rawData. payload {
350
- case . parsedToken( let dat) :
351
- let textRange = ( dat. textRange. lowerBound + reclassifiedTriviaByteLength) ..< dat. textRange. upperBound
352
- return RawSyntax . parsedToken (
353
- kind: dat. tokenKind,
354
- wholeText: dat. wholeText,
355
- textRange: textRange,
356
- presence: dat. presence,
357
- lexerError: dat. lexerError ?? lexerError,
358
- arena: arena
359
- ) . as ( RawTokenSyntax . self) !
360
- case . materializedToken( let dat) :
361
- let triviaBuffer = arena. allocateRawTriviaPieceBuffer ( count: dat. triviaPieces. count + reclassifiedTrivia. count)
362
- let ( _, existingLeadingTriviaEndIndex) = triviaBuffer. initialize ( from: dat. leadingTrivia)
363
- let ( _, reclassifiedTriviaEndIndex) = triviaBuffer [ existingLeadingTriviaEndIndex... ] . initialize ( from: reclassifiedTrivia)
364
- let ( _, triviaEndIndex) = triviaBuffer [ reclassifiedTriviaEndIndex... ] . initialize ( from: dat. trailingTrivia)
365
- assert ( triviaEndIndex == triviaBuffer. endIndex)
366
- return RawSyntax . materializedToken (
367
- kind: dat. tokenKind,
368
- text: SyntaxText ( rebasing: dat. tokenText [ reclassifiedTriviaByteLength... ] ) ,
369
- triviaPieces: RawTriviaPieceBuffer ( triviaBuffer) ,
370
- numLeadingTrivia: dat. numLeadingTrivia + UInt32( reclassifiedTrivia. count) ,
371
- byteLength: dat. byteLength,
372
- presence: dat. presence,
373
- lexerError: dat. lexerError ?? lexerError,
374
- arena: arena
375
- ) . as ( RawTokenSyntax . self) !
376
- case . layout( _) :
377
- preconditionFailure ( " Should be a token " )
378
- }
379
- }
380
-
381
- /// Assuming that the tokens tet ends with text representing `reclassifiedTrivia`,
382
- /// re-classify those characters as no longer being part of the token's text
383
- /// but as part of the token's trailing trivia.
384
- /// If `error` is not `nil` and the token currently doesn't have a lexer error,
385
- /// that error will be set as the lexer error.
386
- public func reclassifyAsTrailingTrivia( _ reclassifiedTrivia: [ RawTriviaPiece ] , lexerError: LexerError ? = nil , arena: SyntaxArena ) -> RawTokenSyntax {
387
- let reclassifiedTriviaByteLength = reclassifiedTrivia. reduce ( 0 , { $0 + $1. byteLength } )
388
- assert (
389
- String ( syntaxText: SyntaxText ( rebasing: self . tokenText [ ( self . tokenText. count - reclassifiedTriviaByteLength) ... ] ) )
390
- == Trivia ( pieces: reclassifiedTrivia. map ( TriviaPiece . init) ) . description
391
- )
392
- switch raw. rawData. payload {
393
- case . parsedToken( let dat) :
394
- let textRange = dat. textRange. lowerBound..< ( dat. textRange. upperBound - reclassifiedTriviaByteLength)
395
- return RawSyntax . parsedToken (
396
- kind: dat. tokenKind,
397
- wholeText: dat. wholeText,
398
- textRange: textRange,
399
- presence: dat. presence,
400
- lexerError: dat. lexerError ?? lexerError,
401
- arena: arena
402
- ) . as ( RawTokenSyntax . self) !
403
- case . materializedToken( let dat) :
404
- let triviaBuffer = arena. allocateRawTriviaPieceBuffer ( count: dat. triviaPieces. count + reclassifiedTrivia. count)
405
- let ( _, existingLeadingTriviaEndIndex) = triviaBuffer. initialize ( from: dat. leadingTrivia)
406
- let ( _, reclassifiedTriviaEndIndex) = triviaBuffer [ existingLeadingTriviaEndIndex... ] . initialize ( from: reclassifiedTrivia)
407
- let ( _, triviaEndIndex) = triviaBuffer [ reclassifiedTriviaEndIndex... ] . initialize ( from: dat. trailingTrivia)
408
- assert ( triviaEndIndex == triviaBuffer. endIndex)
409
- return RawSyntax . materializedToken (
410
- kind: dat. tokenKind,
411
- text: SyntaxText ( rebasing: dat. tokenText [ 0 ..< ( dat. tokenText. endIndex - reclassifiedTriviaByteLength) ] ) ,
412
- triviaPieces: RawTriviaPieceBuffer ( triviaBuffer) ,
413
- numLeadingTrivia: dat. numLeadingTrivia,
414
- byteLength: dat. byteLength,
415
- presence: dat. presence,
416
- lexerError: dat. lexerError ?? lexerError,
417
- arena: arena
418
- ) . as ( RawTokenSyntax . self) !
419
- case . layout( _) :
420
- preconditionFailure ( " Should be a token " )
421
- }
422
- }
423
252
}
0 commit comments