lava-oushudb-dt-sql-parser/scripts/antlr4.js
Hayden c6615aecac
build: open ts strict check (#279)
* feat: no check lib dir

* feat: open strict check to src dir

* test: update test tsconfig.json

* feat: remove any type

* feat: do not export AbstractParseTreeVisitor

* feat: export StmtContextType as enum

* build: improve antlr4 script
2024-03-27 19:04:16 +08:00

93 lines
2.8 KiB
JavaScript

const path = require('path');
const exec = require('child_process').exec;
const fs = require('fs');
const argv = require('yargs-parser')(process.argv.slice(2));
const inquirer = require('inquirer');
const chalk = require('chalk');
const { cleanComment } = require('./cleanComment');
const grammarsPath = path.resolve(__dirname, '../src/grammar');
const outputPath = path.resolve(__dirname, '../src/lib');
const languageEntries = fs.readdirSync(grammarsPath);
const baseCmd = 'antlr4ng -Dlanguage=TypeScript -visitor -listener -Xexact-output-dir -o';
function compile(language) {
const cmd = `${baseCmd} ${outputPath}/${language} ${grammarsPath}/${language}/*.g4`;
if (language !== 'plsql' && fs.existsSync(`${outputPath}/${language}`)) {
console.info(chalk.green(`\nRemoving:`, chalk.gray(`${outputPath}/${language}/*`)));
fs.rmSync(`${outputPath}/${language}`, { recursive: true });
}
console.info(chalk.green('Executing:'), chalk.gray(cmd));
exec(cmd, (err) => {
if (err) {
console.error(
chalk.redBright(`\n[Antlr4 compile error]:`),
chalk.cyan(language),
chalk.gray(err)
);
} else {
cleanComment(language);
console.info(chalk.greenBright(`Compile ${language} succeeded!`));
}
});
}
function prompt() {
inquirer
.prompt([
{
type: 'list',
name: 'language',
message: 'Which language you want compile (or all languages)',
choices: ['All Languages', ...languageEntries],
loop: true,
},
])
.then((result) => {
const language = result.language;
if (language === 'All Languages') {
languageEntries.forEach((language) => {
compile(language);
});
} else {
compile(result.language);
}
});
}
function main() {
if (argv.all) {
// compile all: yarn antlr4 --all
languageEntries.forEach((language) => {
compile(language);
});
} else if (argv.lang) {
// compile single: yarn antlr4 --lang=mysql
const supportedLanguage = languageEntries.find((language) =>
language.startsWith(argv.lang)
);
if (argv.lang === 'all') {
languageEntries.forEach((language) => {
compile(language);
});
} else if (supportedLanguage) {
compile(supportedLanguage);
} else {
console.error(
chalk.bold.red('\n[Invalid language]:'),
chalk.white.underline(`${argv.lang}\n`)
);
prompt();
}
} else {
prompt();
}
}
main();