Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions packages/compiler/scripts/parseLiquid.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {Bench} from 'tinybench';
import {Grammar} from 'ohm-js';
import {compileGrammars} from '../src/api.ts';
import {unparse} from '../test/_helpers.js';
import {createReader, CstNodeType} from '../../runtime/src/cstReader.ts';
import {CstNodeType} from '../../runtime/src/cstView.ts';

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

// Get pattern from command line arguments
const pattern = positionalArgs[0];
Expand Down Expand Up @@ -104,26 +104,26 @@ const pattern = positionalArgs[0];
opts
);

// Walk CST using CstReader, collecting terminal text.
function unparseCstReader(matchResult) {
const reader = createReader(matchResult);
// Walk CST using CstView, collecting terminal text.
function unparseCstView(matchResult) {
const cst = matchResult.cstView();
let ans = '';
function walk(handle) {
if (reader.type(handle) === CstNodeType.TERMINAL) {
ans += reader.sourceString(handle);
if (cst.type(handle) === CstNodeType.TERMINAL) {
ans += cst.sourceString(handle);
return;
}
reader.forEachChild(handle, (child, _leadingSpaces) => {
cst.forEachChild(handle, (child, _leadingSpaces) => {
walk(child);
});
}
walk(reader.root);
walk(cst.root);
return ans;
}

const wasmLabel = includeUnparse ? 'Wasm parse+unparse' : 'Wasm parse';
bench.add(
useCstReader ? `${wasmLabel} (CstReader)` : wasmLabel,
useCstView ? `${wasmLabel} (CstView)` : wasmLabel,
() => {
let overriddenDuration = 0;
for (const {input} of files) {
Expand All @@ -140,7 +140,7 @@ const pattern = positionalArgs[0];
peakWasmMemoryBytes,
exports.memory.buffer.byteLength
);
return useCstReader ? unparseCstReader(m) : unparse(g);
return useCstView ? unparseCstView(m) : unparse(g);
});
if (includeUnparse) overriddenDuration += bench.now() - start;
}
Expand Down
3 changes: 1 addition & 2 deletions packages/compiler/src/parseGrammars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// building and validation.

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

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

Expand Down Expand Up @@ -45,7 +44,7 @@ export function grammars(source: string): Record<string, any> {
if (result.failed()) {
throw new Error(`Failed to parse grammar:\n${result.message}`);
}
buildGrammars(result.getCstRoot() as CstNode, ns, source);
buildGrammars(result.cstView().rootNode(), ns, source);
});
return ns;
}
4 changes: 2 additions & 2 deletions packages/compiler/test/_test-v24.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ test('nested matching with `using`', async t => {
{
using outer = g.match('abc');
t.assert(outer.succeeded());
const outerCst = outer.getCstRoot();
const outerCst = outer.cstView().rootNode();

{
using inner = g.match('1234');
t.assert(inner.succeeded());
t.is(inner.getCstRoot().sourceString, '1234');
t.is(inner.cstView().rootNode().sourceString, '1234');
}

// Outer CST is still valid after inner is disposed.
Expand Down
4 changes: 2 additions & 2 deletions packages/compiler/test/test-cst-compat.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ test.failing('compat: arithmetic', async t => {
for (const input of ['1', '10 + 20', '1+276*(3+4)', '(10+ 999)- 1 +222']) {
matchWithInput(wasmG, input);
const wasmShape = serializeCst(wasmG._getCstRoot());
const v18Shape = serializeCst(v18G.match(input).getCstRoot());
const v18Shape = serializeCst(v18G.match(input).cstView().rootNode());
t.deepEqual(v18Shape, wasmShape);
}
});
Expand All @@ -76,7 +76,7 @@ test.failing('compat: liquid-html', async t => {
for (const input of inputs) {
matchWithInput(wasmG, input);
const wasmShape = serializeCst(wasmG._getCstRoot());
const v18Shape = serializeCst(v18G.match(input).getCstRoot());
const v18Shape = serializeCst(v18G.match(input).cstView().rootNode());
t.deepEqual(v18Shape, wasmShape);
}
});
Loading
Loading