Skip to main content
Module

x/ohm_js/scripts/generate-types.mjs

A library and language for building parsers, interpreters, compilers, etc.
Go to Latest
File
import fs from 'fs';import ohm from 'ohm-js';import prettier from 'prettier';import {fileURLToPath, URL} from 'url';
import {getActionDecls} from '@ohm-js/cli/src/helpers/generateTypes.js';
/* This script uses the internals of ohm-typescript-codegen to generate portions of Ohm's main TypeScript declarations (index.d.ts). */
const templatePath = new URL('./data/index.d.ts.template', import.meta.url);const doNotEditBanner = `// DO NOT EDIT! This file is autogenerated from scripts/data/index.d.ts.template.\n`;
// Renders a very simple Mustache-style template, with variables like// `{{varName}}` and comments like `{{! This is a comment. }}`const render = (template, vars) => template.replace(/{{([^{}]*)}}/g, (_, name) => { if (name.trim().startsWith('!')) return ''; // Comment return vars[name]; // Variable substitution. });
(async function main() { // Get the BuiltInRules grammar and generate the types. const BuiltInRules = ohm.ohmGrammar.superGrammar; const builtInRuleActions = ['', ...getActionDecls(BuiltInRules)].join('\n ');
// Render index.d.ts from the template, overwriting the existing file contents. const template = await fs.promises.readFile(templatePath, 'utf-8'); const output = render(template, { builtInRuleActions, doNotEditBanner }); const options = await prettier.resolveConfig(fileURLToPath(templatePath)); const formattedOutput = prettier.format(output, { ...options, parser: 'typescript' }); await fs.promises.writeFile( new URL('../index.d.ts', import.meta.url), formattedOutput, 'utf-8' );})();