lava-oushudb-dt-sql-parser/scripts/antlr4.js

93 lines
2.8 KiB
JavaScript
Raw Normal View History

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');
2023-10-09 17:43:41 +08:00
const grammarsPath = path.resolve(__dirname, '../src/grammar');
const outputPath = path.resolve(__dirname, '../src/lib');
2023-10-09 17:43:41 +08:00
const languageEntries = fs.readdirSync(grammarsPath);
2023-10-09 17:43:41 +08:00
const baseCmd = 'antlr4ng -Dlanguage=TypeScript -visitor -listener -Xexact-output-dir -o';
2023-10-09 17:43:41 +08:00
function compile(language) {
const cmd = `${baseCmd} ${outputPath}/${language} ${grammarsPath}/${language}/*.g4`;
2023-11-29 10:38:03 +08:00
if (language !== 'plsql' && fs.existsSync(`${outputPath}/${language}`)) {
2023-11-16 20:35:44 +08:00
console.info(chalk.green(`\nRemoving:`, chalk.gray(`${outputPath}/${language}/*`)));
fs.rmSync(`${outputPath}/${language}`, { recursive: true });
}
2023-10-09 17:43:41 +08:00
console.info(chalk.green('Executing:'), chalk.gray(cmd));
2023-10-09 17:43:41 +08:00
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!`));
2023-10-09 17:43:41 +08:00
}
});
}
function prompt() {
inquirer
.prompt([
{
type: 'list',
name: 'language',
message: 'Which language you want compile (or all languages)',
choices: ['All Languages', ...languageEntries],
2023-10-09 17:43:41 +08:00
loop: true,
},
])
.then((result) => {
const language = result.language;
if (language === 'All Languages') {
2023-10-09 17:43:41 +08:00
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);
2023-10-09 17:43:41 +08:00
} else {
console.error(
chalk.bold.red('\n[Invalid language]:'),
2023-10-09 17:43:41 +08:00
chalk.white.underline(`${argv.lang}\n`)
);
prompt();
}
} else {
prompt();
}
}
main();