Skip to content

Commit 43c345b

Browse files
authored
wasm: rename CstReader -> CstView (#614)
1 parent d545459 commit 43c345b

27 files changed

Lines changed: 702 additions & 600 deletions

packages/compiler/scripts/parseLiquid.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {Bench} from 'tinybench';
1919
import {Grammar} from 'ohm-js';
2020
import {compileGrammars} from '../src/api.ts';
2121
import {unparse} from '../test/_helpers.js';
22-
import {createReader, CstNodeType} from '../../runtime/src/cstReader.ts';
22+
import {CstNodeType} from '../../runtime/src/cstView.ts';
2323

2424
const __dirname = dirname(fileURLToPath(import.meta.url));
2525
const datadir = join(__dirname, '../test/data');
@@ -35,7 +35,7 @@ const positionalArgs = process.argv.slice(2).filter(a => !a.startsWith('--'));
3535
// https://matklad.github.io/2024/03/22/basic-things.html
3636
const smallSize = flags.has('--small-size');
3737
const includeUnparse = flags.has('--include-unparse');
38-
const useCstReader = flags.has('--cst-reader');
38+
const useCstView = flags.has('--cst-view');
3939

4040
// Get pattern from command line arguments
4141
const pattern = positionalArgs[0];
@@ -104,26 +104,26 @@ const pattern = positionalArgs[0];
104104
opts
105105
);
106106

107-
// Walk CST using CstReader, collecting terminal text.
108-
function unparseCstReader(matchResult) {
109-
const reader = createReader(matchResult);
107+
// Walk CST using CstView, collecting terminal text.
108+
function unparseCstView(matchResult) {
109+
const cst = matchResult.cstView();
110110
let ans = '';
111111
function walk(handle) {
112-
if (reader.type(handle) === CstNodeType.TERMINAL) {
113-
ans += reader.sourceString(handle);
112+
if (cst.type(handle) === CstNodeType.TERMINAL) {
113+
ans += cst.sourceString(handle);
114114
return;
115115
}
116-
reader.forEachChild(handle, (child, _leadingSpaces) => {
116+
cst.forEachChild(handle, (child, _leadingSpaces) => {
117117
walk(child);
118118
});
119119
}
120-
walk(reader.root);
120+
walk(cst.root);
121121
return ans;
122122
}
123123

124124
const wasmLabel = includeUnparse ? 'Wasm parse+unparse' : 'Wasm parse';
125125
bench.add(
126-
useCstReader ? `${wasmLabel} (CstReader)` : wasmLabel,
126+
useCstView ? `${wasmLabel} (CstView)` : wasmLabel,
127127
() => {
128128
let overriddenDuration = 0;
129129
for (const {input} of files) {
@@ -140,7 +140,7 @@ const pattern = positionalArgs[0];
140140
peakWasmMemoryBytes,
141141
exports.memory.buffer.byteLength
142142
);
143-
return useCstReader ? unparseCstReader(m) : unparse(g);
143+
return useCstView ? unparseCstView(m) : unparse(g);
144144
});
145145
if (includeUnparse) overriddenDuration += bench.now() - start;
146146
}

packages/compiler/src/parseGrammars.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// building and validation.
44

55
import {Grammar} from 'ohm-js';
6-
import type {CstNode} from 'ohm-js';
76

87
import {Grammar as ParsedGrammar} from 'ohm-js-legacy/src/Grammar.js';
98

@@ -45,7 +44,7 @@ export function grammars(source: string): Record<string, any> {
4544
if (result.failed()) {
4645
throw new Error(`Failed to parse grammar:\n${result.message}`);
4746
}
48-
buildGrammars(result.getCstRoot() as CstNode, ns, source);
47+
buildGrammars(result.cstView().rootNode(), ns, source);
4948
});
5049
return ns;
5150
}

packages/compiler/test/_test-v24.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ test('nested matching with `using`', async t => {
88
{
99
using outer = g.match('abc');
1010
t.assert(outer.succeeded());
11-
const outerCst = outer.getCstRoot();
11+
const outerCst = outer.cstView().rootNode();
1212

1313
{
1414
using inner = g.match('1234');
1515
t.assert(inner.succeeded());
16-
t.is(inner.getCstRoot().sourceString, '1234');
16+
t.is(inner.cstView().rootNode().sourceString, '1234');
1717
}
1818

1919
// Outer CST is still valid after inner is disposed.

packages/compiler/test/test-cst-compat.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ test.failing('compat: arithmetic', async t => {
5757
for (const input of ['1', '10 + 20', '1+276*(3+4)', '(10+ 999)- 1 +222']) {
5858
matchWithInput(wasmG, input);
5959
const wasmShape = serializeCst(wasmG._getCstRoot());
60-
const v18Shape = serializeCst(v18G.match(input).getCstRoot());
60+
const v18Shape = serializeCst(v18G.match(input).cstView().rootNode());
6161
t.deepEqual(v18Shape, wasmShape);
6262
}
6363
});
@@ -76,7 +76,7 @@ test.failing('compat: liquid-html', async t => {
7676
for (const input of inputs) {
7777
matchWithInput(wasmG, input);
7878
const wasmShape = serializeCst(wasmG._getCstRoot());
79-
const v18Shape = serializeCst(v18G.match(input).getCstRoot());
79+
const v18Shape = serializeCst(v18G.match(input).cstView().rootNode());
8080
t.deepEqual(v18Shape, wasmShape);
8181
}
8282
});

0 commit comments

Comments
 (0)