@@ -23,17 +23,20 @@ export class LocationCalculator {
2323 private ltOffsets : number [ ]
2424 private baseOffset : number
2525 private baseIndexOfGap : number
26+ private shiftOffset : number
2627
2728 /**
2829 * Initialize this calculator.
2930 * @param gapOffsets The list of the offset of removed characters in tokenization phase.
3031 * @param ltOffsets The list of the offset of line terminators.
3132 * @param baseOffset The base offset to calculate locations.
33+ * @param shiftOffset The shift offset to calculate locations.
3234 */
3335 public constructor (
3436 gapOffsets : number [ ] ,
3537 ltOffsets : number [ ] ,
3638 baseOffset ?: number ,
39+ shiftOffset : number = 0 ,
3740 ) {
3841 this . gapOffsets = gapOffsets
3942 this . ltOffsets = ltOffsets
@@ -42,6 +45,7 @@ export class LocationCalculator {
4245 this . baseOffset === 0
4346 ? 0
4447 : sortedLastIndex ( gapOffsets , this . baseOffset )
48+ this . shiftOffset = shiftOffset
4549 }
4650
4751 /**
@@ -54,6 +58,21 @@ export class LocationCalculator {
5458 this . gapOffsets ,
5559 this . ltOffsets ,
5660 this . baseOffset + offset ,
61+ this . shiftOffset ,
62+ )
63+ }
64+
65+ /**
66+ * Get sub calculator that shifts the given offset.
67+ * @param offset The shift of new sub calculator.
68+ * @returns Sub calculator.
69+ */
70+ public getSubCalculatorShift ( offset : number ) : LocationCalculator {
71+ return new LocationCalculator (
72+ this . gapOffsets ,
73+ this . ltOffsets ,
74+ this . baseOffset ,
75+ this . shiftOffset + offset ,
5776 )
5877 }
5978
@@ -91,7 +110,7 @@ export class LocationCalculator {
91110 * @returns The location of the index.
92111 */
93112 public getLocation ( index : number ) : Location {
94- return this . _getLocation ( this . baseOffset + index )
113+ return this . _getLocation ( this . baseOffset + index + this . shiftOffset )
95114 }
96115
97116 /**
@@ -100,20 +119,27 @@ export class LocationCalculator {
100119 * @returns The offset of the index.
101120 */
102121 public getOffsetWithGap ( index : number ) : number {
103- return this . baseOffset + index + this . _getGap ( index )
122+ const shiftOffset = this . shiftOffset
123+ return (
124+ this . baseOffset +
125+ index +
126+ shiftOffset +
127+ this . _getGap ( index + shiftOffset )
128+ )
104129 }
105130
106131 /**
107132 * Modify the location information of the given node with using the base offset and gaps of this calculator.
108133 * @param node The node to modify their location.
109134 */
110135 public fixLocation < T extends HasLocation > ( node : T ) : T {
136+ const shiftOffset = this . shiftOffset
111137 const range = node . range
112138 const loc = node . loc
113- const gap0 = this . _getGap ( range [ 0 ] )
114- const gap1 = this . _getGap ( range [ 1 ] )
115- const d0 = this . baseOffset + Math . max ( 0 , gap0 )
116- const d1 = this . baseOffset + Math . max ( 0 , gap1 )
139+ const gap0 = this . _getGap ( range [ 0 ] + shiftOffset )
140+ const gap1 = this . _getGap ( range [ 1 ] + shiftOffset )
141+ const d0 = this . baseOffset + Math . max ( 0 , gap0 ) + shiftOffset
142+ const d1 = this . baseOffset + Math . max ( 0 , gap1 ) + shiftOffset
117143
118144 if ( d0 !== 0 ) {
119145 range [ 0 ] += d0
@@ -138,8 +164,9 @@ export class LocationCalculator {
138164 * @param error The error to modify their location.
139165 */
140166 public fixErrorLocation ( error : ParseError ) {
141- const gap = this . _getGap ( error . index )
142- const diff = this . baseOffset + Math . max ( 0 , gap )
167+ const shiftOffset = this . shiftOffset
168+ const gap = this . _getGap ( error . index + shiftOffset )
169+ const diff = this . baseOffset + Math . max ( 0 , gap ) + shiftOffset
143170
144171 error . index += diff
145172
0 commit comments