1- import { isTaggedTerminal , MATCH_RECORD_TYPE_MASK , MatchRecordType } from './miniohm.ts' ;
1+ import {
2+ CST_CHILD_COUNT_OFFSET ,
3+ CST_CHILDREN_OFFSET ,
4+ CST_MATCH_LENGTH_OFFSET ,
5+ CST_TYPE_AND_DETAILS_OFFSET ,
6+ isTaggedTerminal ,
7+ MATCH_RECORD_TYPE_MASK ,
8+ MatchRecordType ,
9+ } from './miniohm.ts' ;
210
311import type { MatchContext , SucceededMatchResult } from './miniohm.ts' ;
412
@@ -101,7 +109,7 @@ export class CstReader {
101109 if ( isSpacesHandle ( raw ) ) return false ;
102110 if ( isTaggedTerminal ( raw ) ) return true ;
103111 return (
104- ( ( this . _ctx . view . getInt32 ( raw + 8 , true ) &
112+ ( ( this . _ctx . view . getInt32 ( raw + CST_TYPE_AND_DETAILS_OFFSET , true ) &
105113 MATCH_RECORD_TYPE_MASK ) as MatchRecordType ) === MatchRecordType . TERMINAL
106114 ) ;
107115 }
@@ -111,7 +119,7 @@ export class CstReader {
111119 if ( isSpacesHandle ( raw ) ) return true ;
112120 if ( isTaggedTerminal ( raw ) ) return false ;
113121 return (
114- ( ( this . _ctx . view . getInt32 ( raw + 8 , true ) &
122+ ( ( this . _ctx . view . getInt32 ( raw + CST_TYPE_AND_DETAILS_OFFSET , true ) &
115123 MATCH_RECORD_TYPE_MASK ) as MatchRecordType ) === MatchRecordType . NONTERMINAL
116124 ) ;
117125 }
@@ -120,7 +128,7 @@ export class CstReader {
120128 const raw = handle & MASK ;
121129 if ( isSpacesHandle ( raw ) || isTaggedTerminal ( raw ) ) return false ;
122130 return (
123- ( ( this . _ctx . view . getInt32 ( raw + 8 , true ) &
131+ ( ( this . _ctx . view . getInt32 ( raw + CST_TYPE_AND_DETAILS_OFFSET , true ) &
124132 MATCH_RECORD_TYPE_MASK ) as MatchRecordType ) === MatchRecordType . ITER_FLAG
125133 ) ;
126134 }
@@ -129,7 +137,7 @@ export class CstReader {
129137 const raw = handle & MASK ;
130138 if ( isSpacesHandle ( raw ) || isTaggedTerminal ( raw ) ) return false ;
131139 return (
132- ( ( this . _ctx . view . getInt32 ( raw + 8 , true ) &
140+ ( ( this . _ctx . view . getInt32 ( raw + CST_TYPE_AND_DETAILS_OFFSET , true ) &
133141 MATCH_RECORD_TYPE_MASK ) as MatchRecordType ) === MatchRecordType . OPTIONAL
134142 ) ;
135143 }
@@ -138,15 +146,15 @@ export class CstReader {
138146 childCount ( handle : number ) : number {
139147 const raw = handle & MASK ;
140148 if ( isSpacesHandle ( raw ) || isTaggedTerminal ( raw ) ) return 0 ;
141- return this . _ctx . view . getUint32 ( raw , true ) ;
149+ return this . _ctx . view . getUint32 ( raw + CST_CHILD_COUNT_OFFSET , true ) ;
142150 }
143151
144152 /** Length of matched input (in UTF-16 code units). */
145153 matchLength ( handle : number ) : number {
146154 const raw = handle & MASK ;
147155 if ( isSpacesHandle ( raw ) ) return raw >>> 2 ;
148156 if ( isTaggedTerminal ( raw ) ) return raw >>> 1 ;
149- return this . _ctx . view . getUint32 ( raw + 4 , true ) ;
157+ return this . _ctx . view . getUint32 ( raw + CST_MATCH_LENGTH_OFFSET , true ) ;
150158 }
151159
152160 /**
@@ -157,10 +165,10 @@ export class CstReader {
157165 const raw = handle & MASK ;
158166 if ( isSpacesHandle ( raw ) ) return 'spaces' ;
159167 if ( isTaggedTerminal ( raw ) ) return '_terminal' ;
160- const type = ( this . _ctx . view . getInt32 ( raw + 8 , true ) &
168+ const type = ( this . _ctx . view . getInt32 ( raw + CST_TYPE_AND_DETAILS_OFFSET , true ) &
161169 MATCH_RECORD_TYPE_MASK ) as MatchRecordType ;
162170 if ( type === MatchRecordType . NONTERMINAL ) {
163- const ruleId = this . _ctx . view . getInt32 ( raw + 8 , true ) >>> 2 ;
171+ const ruleId = this . _ctx . view . getInt32 ( raw + CST_TYPE_AND_DETAILS_OFFSET , true ) >>> 2 ;
164172 return this . _ctx . ruleNames [ ruleId ] . split ( '<' ) [ 0 ] ;
165173 }
166174 if ( type === MatchRecordType . TERMINAL ) return '_terminal' ;
@@ -175,13 +183,13 @@ export class CstReader {
175183 details ( handle : number ) : number {
176184 const raw = handle & MASK ;
177185 if ( isSpacesHandle ( raw ) || isTaggedTerminal ( raw ) ) return 0 ;
178- return this . _ctx . view . getInt32 ( raw + 8 , true ) >>> 2 ;
186+ return this . _ctx . view . getInt32 ( raw + CST_TYPE_AND_DETAILS_OFFSET , true ) >>> 2 ;
179187 }
180188
181189 /** Handle (Wasm pointer) of the i-th raw child. */
182190 childAt ( handle : number , i : number ) : number {
183191 const raw = handle & MASK ;
184- return this . _ctx . view . getUint32 ( raw + 16 + i * 4 , true ) ;
192+ return this . _ctx . view . getUint32 ( raw + CST_CHILDREN_OFFSET + i * 4 , true ) ;
185193 }
186194
187195 /** Source string for a node. If startIdx is omitted, it is extracted from the handle. */
@@ -227,7 +235,7 @@ export class CstReader {
227235 /** Check whether a raw child handle has parent-level space skipping. */
228236 private _hasParentSpaces ( rawChild : number ) : boolean {
229237 if ( isTaggedTerminal ( rawChild ) ) return true ;
230- const type = ( this . _ctx . view . getInt32 ( rawChild + 8 , true ) &
238+ const type = ( this . _ctx . view . getInt32 ( rawChild + CST_TYPE_AND_DETAILS_OFFSET , true ) &
231239 MATCH_RECORD_TYPE_MASK ) as MatchRecordType ;
232240 return type === MatchRecordType . NONTERMINAL || type === MatchRecordType . TERMINAL ;
233241 }
@@ -238,11 +246,11 @@ export class CstReader {
238246 fn : ( child : number , leadingSpaces : number , offset : number , index : number ) => void
239247 ) : void {
240248 if ( isTaggedTerminal ( handle ) ) return ;
241- const count = this . _ctx . view . getUint32 ( handle , true ) ;
249+ const count = this . _ctx . view . getUint32 ( handle + CST_CHILD_COUNT_OFFSET , true ) ;
242250 const { getSpacesLenAt} = this . _ctx ;
243251 let offset = 0 ;
244252 for ( let i = 0 ; i < count ; i ++ ) {
245- const child = this . _ctx . view . getUint32 ( handle + 16 + i * 4 , true ) ;
253+ const child = this . _ctx . view . getUint32 ( handle + CST_CHILDREN_OFFSET + i * 4 , true ) ;
246254 const rawSpacesLen =
247255 getSpacesLenAt && this . _hasParentSpaces ( child )
248256 ? Math . max ( 0 , getSpacesLenAt ( parentStartIdx + offset ) )
@@ -251,7 +259,7 @@ export class CstReader {
251259 offset += rawSpacesLen ;
252260 const len = isTaggedTerminal ( child )
253261 ? child >>> 1
254- : this . _ctx . view . getUint32 ( child + 4 , true ) ;
262+ : this . _ctx . view . getUint32 ( child + CST_MATCH_LENGTH_OFFSET , true ) ;
255263 fn ( child , leadingSpaces , offset , i ) ;
256264 offset += len ;
257265 }
@@ -263,11 +271,11 @@ export class CstReader {
263271 ) : void {
264272 const raw = handle & MASK ;
265273 if ( isTaggedTerminal ( raw ) ) return ;
266- const count = this . _ctx . view . getUint32 ( raw , true ) ;
274+ const count = this . _ctx . view . getUint32 ( raw + CST_CHILD_COUNT_OFFSET , true ) ;
267275 let childStart = ( handle - raw ) / SHIFT ;
268276 const { getSpacesLenAt} = this . _ctx ;
269277 for ( let i = 0 ; i < count ; i ++ ) {
270- const rawChild = this . _ctx . view . getUint32 ( raw + 16 + i * 4 , true ) ;
278+ const rawChild = this . _ctx . view . getUint32 ( raw + CST_CHILDREN_OFFSET + i * 4 , true ) ;
271279 const rawSpacesLen =
272280 getSpacesLenAt && this . _hasParentSpaces ( rawChild )
273281 ? Math . max ( 0 , getSpacesLenAt ( childStart ) )
@@ -280,7 +288,7 @@ export class CstReader {
280288 const childHandle = childStart * SHIFT + rawChild ;
281289 const len = isTaggedTerminal ( rawChild )
282290 ? rawChild >>> 1
283- : this . _ctx . view . getUint32 ( rawChild + 4 , true ) ;
291+ : this . _ctx . view . getUint32 ( rawChild + CST_MATCH_LENGTH_OFFSET , true ) ;
284292 fn ( childHandle , leadingSpaces , childStart , i ) ;
285293 childStart += len ;
286294 }
0 commit comments