From e6f8ac20916f8953212e3ce378d15480d1adaeab Mon Sep 17 00:00:00 2001 From: Seren Kwok Date: Mon, 11 Mar 2024 11:49:16 +0000 Subject: [PATCH] how much is left --- resources/edit_grammars.mjs | 116 ++++++++++++++++++------------------ 1 file changed, 58 insertions(+), 58 deletions(-) diff --git a/resources/edit_grammars.mjs b/resources/edit_grammars.mjs index 6afd9d5f6..419a9d5f7 100644 --- a/resources/edit_grammars.mjs +++ b/resources/edit_grammars.mjs @@ -1,5 +1,5 @@ -import { exec } from "child_process"; -import fs from "fs/promises"; +import { exec } from 'child_process'; +import fs from 'fs/promises'; /////////////////////////////////////////////////// // Scripting helpers @@ -29,10 +29,10 @@ function cleanExit() { } process.exit(); } -process.on("SIGINT", cleanExit); // catch ctrl-c -process.on("SIGTERM", cleanExit); // catch kill -process.on("uncaughtException", (err) => { - console.error("Uncaught Exception:", err); +process.on('SIGINT', cleanExit); // catch ctrl-c +process.on('SIGTERM', cleanExit); // catch kill +process.on('uncaughtException', (err) => { + console.error('Uncaught Exception:', err); cleanExit(); }); @@ -40,22 +40,22 @@ process.on("uncaughtException", (err) => { // Constants /////////////////////////////////////////////////// const allLanguages = [ - "c-sharp", - "css", - "go", - "hcl", - "html", - "java", - "javascript", - "json", - "markdown", - "python", - "ruby", - "rust", - "solidity", - "sql", - "typescript", - "yaml", + 'c-sharp', + 'css', + 'go', + 'hcl', + 'html', + 'java', + 'javascript', + 'json', + 'markdown', + 'python', + 'ruby', + 'rust', + 'solidity', + 'sql', + 'typescript', + 'yaml', ]; // For these languages, copyMvGrammar is optional @@ -63,11 +63,11 @@ const allLanguages = [ // I'd rather not special-case languages in individual steps // like this and instead have them define their own build // steps, but makes the code smaller for now -const languagesWithoutMetaVariables = ["ruby", "html", "hcl"]; +const languagesWithoutMetaVariables = ['ruby', 'html', 'hcl']; // assumes that this script is run from marzano/resources directory const METAVARIABLE_GRAMMARS = `../metavariable-grammars`; -const LANGUAGE_METAVARIABLES = "language-metavariables"; +const LANGUAGE_METAVARIABLES = 'language-metavariables'; /////////////////////////////////////////////////// // Build steps @@ -93,10 +93,10 @@ const copyMvScanner = async (lang, dest) => ); const treeSitterGenerate = async (dir, buildWasm = true) => { - const andMaybeBuildWasm = buildWasm ? "&& npx tree-sitter build-wasm " : ""; + const andMaybeBuildWasm = buildWasm ? '&& npx tree-sitter build-wasm ' : ''; await execPromise( - `cd tree-sitter-${dir} && npx tree-sitter generate ${andMaybeBuildWasm} && echo "Generated grammar for ${dir}"` - ).catch((e) => console.log("swallowed error, ", e)); + `cd tree-sitter-${dir} && npx tree-sitter generate ${andMaybeBuildWasm} && echo 'Generated grammar for ${dir}'` + ).catch((e) => console.log('swallowed error, ', e)); }; const copyNodeTypes = async (lang, dest) => @@ -107,14 +107,14 @@ const copyNodeTypes = async (lang, dest) => const copyWasmParser = async (lang, prefix) => fs.rename( - `${prefix ?? "tree-sitter-"}${lang}/tree-sitter-${lang}.wasm`, + `${prefix ?? 'tree-sitter-'}${lang}/tree-sitter-${lang}.wasm`, `../../crates/wasm-bindings/wasm_parsers/tree-sitter-${lang}.wasm` ); async function rsyncGrammars(language) { //If a language is given, only sync that language //Otherwise, rm -rf the entire language-metavariables dir and sync it from scratch - const treeSitterLang = language ? `tree-sitter-${language}` : "."; + const treeSitterLang = language ? `tree-sitter-${language}` : '.'; const mvDir = language ? `${LANGUAGE_METAVARIABLES}/${treeSitterLang}` : LANGUAGE_METAVARIABLES; @@ -128,16 +128,16 @@ async function rsyncGrammars(language) { const submodulesDir = language ? `language-submodules/${treeSitterLang}` - : "language-submodules/."; + : 'language-submodules/.'; const blobsToExclude = [ - ".git*", - "tree-sitter-*/example", - "tree-sitter-*/test", - "tree-sitter-*/corpus", + '.git*', + 'tree-sitter-*/example', + 'tree-sitter-*/test', + 'tree-sitter-*/corpus', ]; await execPromise( `rsync -r -l ${submodulesDir} language-metavariables --exclude={${blobsToExclude.join( - "," + ',' )}}` ); } @@ -168,28 +168,28 @@ async function buildLanguage(language) { const tsLangDir = `tree-sitter-${language}`; //Force cargo.toml to use the correct version of tree-sitter await execPromise(`for cargo in ${tsLangDir}/[Cc]argo.toml; do - if [ -f "$cargo" ]; then - sed -i '' -e 's/tree-sitter = ".*"/tree-sitter = "~0.20"/g' "$cargo"; + if [ -f '$cargo' ]; then + sed -i '' -e 's/tree-sitter = '.*'/tree-sitter = '~0.20'/g' '$cargo'; fi; done`); - if (language === "c-sharp") { + if (language === 'c-sharp') { //skip generating c-sharp, tree-sitter hangs on it - await copyNodeTypes("c-sharp", "csharp"); - } else if (language === "markdown") { + await copyNodeTypes('c-sharp', 'csharp'); + } else if (language === 'markdown') { //markdown has sub-grammars await Promise.all([ - copyMvGrammar("markdown-common", "markdown/common"), - copyMvGrammar("markdown-block", "markdown/tree-sitter-markdown"), - copyMvGrammar("markdown-inline", "markdown/tree-sitter-markdown-inline"), + copyMvGrammar('markdown-common', 'markdown/common'), + copyMvGrammar('markdown-block', 'markdown/tree-sitter-markdown'), + copyMvGrammar('markdown-inline', 'markdown/tree-sitter-markdown-inline'), ]); await Promise.all([ - treeSitterGenerate("markdown/tree-sitter-markdown"), - treeSitterGenerate("markdown/tree-sitter-markdown-inline"), + treeSitterGenerate('markdown/tree-sitter-markdown'), + treeSitterGenerate('markdown/tree-sitter-markdown-inline'), ]); await Promise.all([ - copyNodeTypes("markdown/tree-sitter-markdown", "markdown-block"), - copyNodeTypes("markdown/tree-sitter-markdown-inline", `markdown-inline`), + copyNodeTypes('markdown/tree-sitter-markdown', 'markdown-block'), + copyNodeTypes('markdown/tree-sitter-markdown-inline', `markdown-inline`), ]); await fs.rename( @@ -201,7 +201,7 @@ async function buildLanguage(language) { `tree-sitter-markdown/tree-sitter-markdown-inline/tree-sitter-markdown_inline.wasm`, `../../crates/wasm-bindings/wasm_parsers/tree-sitter-markdown_inline.wasm` ); - } else if (language === "typescript") { + } else if (language === 'typescript') { // typescript is special // we edit its package.json to point to our local version of the js grammar log(`Copying files`); @@ -217,33 +217,33 @@ async function buildLanguage(language) { ); await execPromise( - `cd ${tsLangDir} && yarn && yarn build && echo "Generated grammar for ${language}"` + `cd ${tsLangDir} && yarn && yarn build && echo 'Generated grammar for ${language}'` ); await Promise.all([ execPromise(`cd ${tsLangDir}/tsx && npx tree-sitter build-wasm`), execPromise(`cd ${tsLangDir}/typescript && npx tree-sitter build-wasm`), ]); - await copyNodeTypes("typescript/typescript", "typescript"); - await copyNodeTypes("typescript/tsx", "tsx"); + await copyNodeTypes('typescript/typescript', 'typescript'); + await copyNodeTypes('typescript/tsx', 'tsx'); - await copyWasmParser("typescript", "tree-sitter-typescript/"); - await copyWasmParser("tsx", "tree-sitter-typescript/"); - } else if (language === "yaml") { + await copyWasmParser('typescript', 'tree-sitter-typescript/'); + await copyWasmParser('tsx', 'tree-sitter-typescript/'); + } else if (language === 'yaml') { await copyMvScanner(language); await buildSimpleLanguage(log, language); await fs.copyFile( `${METAVARIABLE_GRAMMARS}/cc_build.rs`, `${tsLangDir}/bindings/rust/build.rs` ); - } else if (language === "hcl") { + } else if (language === 'hcl') { //HCL's mv grammar goes into `make_grammar.js`, not `grammar.js` await fs.copyFile( `${METAVARIABLE_GRAMMARS}/${language}-metavariable-grammar.js`, `tree-sitter-${language}/make_grammar.js` ); await buildSimpleLanguage(log, language); - } else if (language === "sql") { + } else if (language === 'sql') { await copyMvGrammar(language); //SQL's wasm build hangs so we skip it await treeSitterGenerate(language, false); @@ -264,7 +264,7 @@ async function run() { const buildAll = args.length == 0; const languagesTobuild = buildAll ? allLanguages : args; - console.log("Syncing upstream grammars"); + console.log('Syncing upstream grammars'); if (buildAll) { await rsyncGrammars(); } else { @@ -273,7 +273,7 @@ async function run() { process.chdir(LANGUAGE_METAVARIABLES); await Promise.all(languagesTobuild.map(buildLanguage)); await execPromise( - `find . -name "build.rs" -exec sed -i '' -e 's/Wno-unused-parameter/w/g' {} \\;` + `find . -name 'build.rs' -exec sed -i '' -e 's/Wno-unused-parameter/w/g' {} \\;` ); }