Skip to content

Commit 3c34a59

Browse files
authored
compiler: API cleanup (#591)
1 parent 33a4a4f commit 3c34a59

3 files changed

Lines changed: 17 additions & 10 deletions

File tree

packages/compiler/scripts/parseLiquid.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,16 @@ import {fileURLToPath} from 'node:url';
1616
import * as ohm from 'ohm-js-legacy';
1717
import {Bench} from 'tinybench';
1818

19-
import {Compiler} from '../src/Compiler.ts';
20-
import {unparse, legacyGrammarToWasm} from '../test/_helpers.js';
19+
import {Grammar} from 'ohm-js';
20+
import {compileGrammars} from '../src/api.ts';
21+
import {unparse} from '../test/_helpers.js';
2122
import {createReader} from '../../runtime/src/cstReader.ts';
2223

2324
const __dirname = dirname(fileURLToPath(import.meta.url));
2425
const datadir = join(__dirname, '../test/data');
2526

26-
const liquid = ohm.grammars(readFileSync(join(datadir, 'liquid-html.ohm'), 'utf8'));
27+
const grammarSource = readFileSync(join(datadir, 'liquid-html.ohm'), 'utf8');
28+
const liquid = ohm.grammars(grammarSource);
2729

2830
// Parse flags and positional args.
2931
const flags = new Set(process.argv.slice(2).filter(a => a.startsWith('--')));
@@ -79,9 +81,10 @@ const pattern = positionalArgs[0];
7981

8082
// Compile to Wasm.
8183
const compileStart = bench.now();
82-
const modBytes = new Compiler(liquid.LiquidHTML).compile();
84+
const allBytes = compileGrammars(grammarSource);
8385
const compileTime = bench.now() - compileStart;
84-
const g = await legacyGrammarToWasm(liquid.LiquidHTML, {modBytes});
86+
const modBytes = allBytes.LiquidHTML;
87+
const g = await Grammar.instantiate(modBytes);
8588
const {exports} = g._instance;
8689
let peakWasmHeapBytes = 0;
8790
let peakWasmMemoryBytes = 0;

packages/compiler/scripts/parsePipRequirements.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,15 @@ import process from 'node:process';
1111
import {fileURLToPath} from 'node:url';
1212
import * as ohm from 'ohm-js-legacy';
1313

14-
import {unparse, legacyGrammarToWasm} from '../test/_helpers.js';
14+
import {Grammar} from 'ohm-js';
15+
import {compile} from '../src/api.ts';
16+
import {unparse} from '../test/_helpers.js';
1517

1618
const __dirname = dirname(fileURLToPath(import.meta.url));
1719
const datadir = join(__dirname, '../test/data');
1820

19-
const grammar = ohm.grammar(readFileSync(join(datadir, 'pep-508.ohm'), 'utf8'));
21+
const grammarSource = readFileSync(join(datadir, 'pep-508.ohm'), 'utf8');
22+
const grammar = ohm.grammar(grammarSource);
2023
const input = readFileSync(join(datadir, 'requirements_all.txt'), 'utf8');
2124

2225
(async function main() {
@@ -32,7 +35,8 @@ const input = readFileSync(join(datadir, 'requirements_all.txt'), 'utf8');
3235
assert.equal(r.succeeded(), true, `JS parse failed: ${r.shortMessage}`);
3336

3437
// --- Wasm ---
35-
const g = await legacyGrammarToWasm(grammar);
38+
const modBytes = compile(grammarSource);
39+
const g = await Grammar.instantiate(modBytes);
3640
const {exports} = g._instance;
3741

3842
const wasmStart = performance.now();

packages/compiler/test/_helpers.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ async function _compileAndInstantiate(parsedGrammar, compilerOpts) {
5151

5252
// Takes a *legacy* ohm-js Grammar object and compiles it to Wasm.
5353
// Use this only in tests that deliberately compare Wasm vs legacy behavior.
54-
export async function legacyGrammarToWasm(grammar, {modBytes, ...compilerOpts} = {}) {
54+
export async function legacyGrammarToWasm(grammar, compilerOpts = {}) {
5555
const compiler = new Compiler(grammar, compilerOpts);
56-
const bytes = modBytes ?? compiler.compile();
56+
const bytes = compiler.compile();
5757

5858
const wasmGrammar = new Grammar();
5959

0 commit comments

Comments
 (0)