Skip to content

Commit

Permalink
how much is left
Browse files Browse the repository at this point in the history
  • Loading branch information
seren5240 committed Mar 11, 2024
1 parent ed517e3 commit e6f8ac2
Showing 1 changed file with 58 additions and 58 deletions.
116 changes: 58 additions & 58 deletions resources/edit_grammars.mjs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -29,45 +29,45 @@ 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();
});

///////////////////////////////////////////////////
// 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
// hcl has one but is in a different location than the default
// 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
Expand All @@ -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) =>
Expand All @@ -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;
Expand All @@ -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(
","
','
)}}`
);
}
Expand Down Expand Up @@ -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(
Expand All @@ -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`);
Expand All @@ -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);
Expand All @@ -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 {
Expand All @@ -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' {} \\;`
);
}

Expand Down

0 comments on commit e6f8ac2

Please sign in to comment.