@@ -19,7 +19,7 @@ import {Bench} from 'tinybench';
1919import { Grammar } from 'ohm-js' ;
2020import { compileGrammars } from '../src/api.ts' ;
2121import { unparse } from '../test/_helpers.js' ;
22- import { createReader } from '../../runtime/src/cstReader.ts' ;
22+ import { createReader , CstNodeType } from '../../runtime/src/cstReader.ts' ;
2323
2424const __dirname = dirname ( fileURLToPath ( import . meta. url ) ) ;
2525const datadir = join ( __dirname , '../test/data' ) ;
@@ -36,7 +36,6 @@ const positionalArgs = process.argv.slice(2).filter(a => !a.startsWith('--'));
3636const smallSize = flags . has ( '--small-size' ) ;
3737const includeUnparse = flags . has ( '--include-unparse' ) ;
3838const useCstReader = flags . has ( '--cst-reader' ) ;
39- const useCstReaderPacked = flags . has ( '--cst-reader-packed' ) ;
4039
4140// Get pattern from command line arguments
4241const pattern = positionalArgs [ 0 ] ;
@@ -105,34 +104,12 @@ const pattern = positionalArgs[0];
105104 opts
106105 ) ;
107106
108- // Walk CST using CstReader (raw handles) , collecting terminal text.
109- function unparseCstReaderRaw ( matchResult ) {
107+ // Walk CST using CstReader, collecting terminal text.
108+ function unparseCstReader ( matchResult ) {
110109 const reader = createReader ( matchResult ) ;
111- const inp = reader . input ;
112- let ans = '' ;
113- function walk ( handle , startIdx ) {
114- if ( reader . isTerminal ( handle ) ) {
115- ans += inp . slice ( startIdx , startIdx + reader . matchLength ( handle ) ) ;
116- return ;
117- }
118- reader . forEachChild (
119- handle ,
120- ( child , _leadingSpaces , offset ) => {
121- walk ( child , startIdx + offset ) ;
122- } ,
123- startIdx
124- ) ;
125- }
126- walk ( reader . rootHandle , reader . rootStartIdx ) ;
127- return ans ;
128- }
129-
130- // Walk CST using CstReader (handles with startIdx), collecting terminal text.
131- function unparseCstReaderPacked ( matchResult ) {
132- const reader = createReader ( matchResult , { packStartIdx : true } ) ;
133110 let ans = '' ;
134111 function walk ( handle ) {
135- if ( reader . isTerminal ( handle ) ) {
112+ if ( reader . type ( handle ) === CstNodeType . TERMINAL ) {
136113 ans += reader . sourceString ( handle ) ;
137114 return ;
138115 }
@@ -146,11 +123,7 @@ const pattern = positionalArgs[0];
146123
147124 const wasmLabel = includeUnparse ? 'Wasm parse+unparse' : 'Wasm parse' ;
148125 bench . add (
149- useCstReaderPacked
150- ? `${ wasmLabel } (CstReader packed)`
151- : useCstReader
152- ? `${ wasmLabel } (CstReader)`
153- : wasmLabel ,
126+ useCstReader ? `${ wasmLabel } (CstReader)` : wasmLabel ,
154127 ( ) => {
155128 let overriddenDuration = 0 ;
156129 for ( const { input} of files ) {
@@ -167,11 +140,7 @@ const pattern = positionalArgs[0];
167140 peakWasmMemoryBytes ,
168141 exports . memory . buffer . byteLength
169142 ) ;
170- return useCstReaderPacked
171- ? unparseCstReaderPacked ( m )
172- : useCstReader
173- ? unparseCstReaderRaw ( m )
174- : unparse ( g ) ;
143+ return useCstReader ? unparseCstReader ( m ) : unparse ( g ) ;
175144 } ) ;
176145 if ( includeUnparse ) overriddenDuration += bench . now ( ) - start ;
177146 }
0 commit comments