2023-10-24 10:34:14 +08:00
|
|
|
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');
|
2023-11-28 21:28:20 +08:00
|
|
|
const { cleanComment } = require('./cleanComment');
|
2023-10-09 17:43:41 +08:00
|
|
|
|
2023-10-24 10:34:14 +08:00
|
|
|
const grammarsPath = path.resolve(__dirname, '../src/grammar');
|
|
|
|
const outputPath = path.resolve(__dirname, '../src/lib');
|
2023-10-09 17:43:41 +08:00
|
|
|
|
2023-11-28 21:11:07 +08:00
|
|
|
const languageEntries = fs.readdirSync(grammarsPath);
|
2023-10-09 17:43:41 +08:00
|
|
|
|
2024-02-26 20:25:09 +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
|
|
|
|
2023-10-24 10:34:14 +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 {
|
2023-11-28 21:28:20 +08:00
|
|
|
cleanComment(language);
|
2024-02-26 20:25:09 +08:00
|
|
|
console.info(chalk.greenBright(`Compile ${language} succeeded!`));
|
2023-10-09 17:43:41 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function prompt() {
|
|
|
|
inquirer
|
|
|
|
.prompt([
|
|
|
|
{
|
2023-10-24 10:34:14 +08:00
|
|
|
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;
|
2023-10-24 10:34:14 +08:00
|
|
|
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) {
|
2023-11-27 15:25:40 +08:00
|
|
|
// compile single: yarn antlr4 --lang=mysql
|
2023-10-24 10:34:14 +08:00
|
|
|
const supportedLanguage = languageEntries.some((language) => language === argv.lang);
|
2023-10-09 17:43:41 +08:00
|
|
|
if (supportedLanguage) {
|
|
|
|
compile(argv.lang);
|
|
|
|
} else {
|
|
|
|
console.error(
|
2023-10-24 10:34:14 +08:00
|
|
|
chalk.bold.red('\n[Invalid language]:'),
|
2023-10-09 17:43:41 +08:00
|
|
|
chalk.white.underline(`${argv.lang}\n`)
|
|
|
|
);
|
|
|
|
prompt();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
prompt();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
main();
|