Skip to content

Commit f17f19f

Browse files
author
zhaoge
committed
feat(merge conflict): merge main solve conflict
2 parents 188d42d + 20f065d commit f17f19f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+11543
-11327
lines changed

.github/workflows/ci.yml

+3
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ jobs:
4141
- name: Run tsc check
4242
run: pnpm check-types
4343

44+
- name: Run antlr4 all sql
45+
run: pnpm antlr4 --all --check
46+
4447
test:
4548
runs-on: ubuntu-latest
4649
strategy:

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
"chalk": "4.1.2",
5656
"commitizen": "^4.3.0",
5757
"console-table-printer": "^2.12.0",
58+
"crypto": "^1.0.1",
5859
"envinfo": "^7.11.1",
5960
"glob": "^10.3.10",
6061
"husky": "^8.0.3",

pnpm-lock.yaml

+12-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/antlr4.js

+70-26
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,69 @@ const fs = require('fs');
44
const argv = require('yargs-parser')(process.argv.slice(2));
55
const inquirer = require('inquirer');
66
const chalk = require('chalk');
7+
const crypto = require('crypto');
78
const { cleanComment } = require('./cleanComment');
89

910
const grammarsPath = path.resolve(__dirname, '../src/grammar');
1011
const outputPath = path.resolve(__dirname, '../src/lib');
1112

12-
const languageEntries = fs.readdirSync(grammarsPath);
13+
const languageEntries = fs.readdirSync(grammarsPath).filter((language) => {
14+
return fs.statSync(path.join(grammarsPath, language)).isDirectory();
15+
});
1316

1417
const baseCmd = 'antlr4ng -Dlanguage=TypeScript -visitor -listener -Xexact-output-dir -o';
1518

16-
function compile(language) {
17-
const cmd = `${baseCmd} ${outputPath}/${language} ${grammarsPath}/${language}/*.g4`;
19+
function getFileHash(filePath) {
20+
if (!fs.existsSync(filePath)) return null;
1821

19-
if (language !== 'plsql' && fs.existsSync(`${outputPath}/${language}`)) {
20-
console.info(chalk.green(`\nRemoving:`, chalk.gray(`${outputPath}/${language}/*`)));
21-
fs.rmSync(`${outputPath}/${language}`, { recursive: true });
22-
}
22+
const fileBuffer = fs.readFileSync(filePath);
23+
const hashSum = crypto.createHash('sha256');
24+
hashSum.update(fileBuffer);
2325

24-
console.info(chalk.green('Executing:'), chalk.gray(cmd));
25-
exec(cmd, (err) => {
26-
if (err) {
27-
console.error(
28-
chalk.redBright(`\n[Antlr4 compile error]:`),
29-
chalk.cyan(language),
30-
chalk.gray(err)
31-
);
32-
} else {
33-
cleanComment(language);
34-
console.info(chalk.greenBright(`Compile ${language} succeeded!`));
26+
return hashSum.digest('hex');
27+
}
28+
29+
function compile(language) {
30+
return new Promise((resolve, reject) => {
31+
const outputDir = `${outputPath}/${language}`;
32+
const grammarFiles = fs
33+
.readdirSync(`${grammarsPath}/${language}`)
34+
.filter((file) => file.endsWith('.g4'));
35+
const previousHashes = grammarFiles.map((file) => ({
36+
file,
37+
hash: getFileHash(path.join(outputDir, file.replace('.g4', '.ts'))),
38+
}));
39+
40+
if (language !== 'plsql' && fs.existsSync(`${outputPath}/${language}`)) {
41+
console.info(chalk.green(`\nRemoving:`, chalk.gray(`${outputPath}/${language}/*`)));
42+
fs.rmSync(`${outputPath}/${language}`, { recursive: true });
3543
}
44+
45+
const cmd = `${baseCmd} ${outputDir} ${grammarsPath}/${language}/*.g4`;
46+
console.info(chalk.green('Executing:'), chalk.gray(cmd));
47+
exec(cmd, (err) => {
48+
if (err) {
49+
console.error(
50+
chalk.redBright(`\n[Antlr4 compile error]:`),
51+
chalk.cyan(language),
52+
chalk.gray(err)
53+
);
54+
} else {
55+
cleanComment(language);
56+
console.info(chalk.greenBright(`Compile ${language} succeeded!`));
57+
58+
const changedFiles = grammarFiles.filter((file) => {
59+
const newHash = getFileHash(path.join(outputDir, file.replace('.g4', '.ts')));
60+
const prevHash = previousHashes.find((h) => h.file === file)?.hash;
61+
return newHash !== prevHash;
62+
});
63+
64+
if (changedFiles.length > 0) {
65+
return reject(`${language} not run antlr4`);
66+
}
67+
resolve();
68+
}
69+
});
3670
});
3771
}
3872

@@ -59,22 +93,32 @@ function prompt() {
5993
});
6094
}
6195

96+
async function antlr4AllSql() {
97+
const errors = [];
98+
99+
const tasks = languageEntries.map((language) =>
100+
compile(language).catch((err) => errors.push(err))
101+
);
102+
103+
await Promise.all(tasks);
104+
105+
if (errors.length > 0 && argv.check) {
106+
errors.forEach((error) => console.error(chalk.red(`- ${error}`)));
107+
process.exit(1); // 非零退出表示错误
108+
}
109+
}
110+
62111
function main() {
63112
if (argv.all) {
64-
// compile all: yarn antlr4 --all
65-
languageEntries.forEach((language) => {
66-
compile(language);
67-
});
68-
} else if (argv.lang) {
113+
antlr4AllSql();
114+
} else if (argv.lang && typeof argv.lang === 'string') {
69115
// compile single: yarn antlr4 --lang=mysql
70116
const supportedLanguage = languageEntries.find((language) =>
71117
language.startsWith(argv.lang)
72118
);
73119

74120
if (argv.lang === 'all') {
75-
languageEntries.forEach((language) => {
76-
compile(language);
77-
});
121+
antlr4AllSql();
78122
} else if (supportedLanguage) {
79123
compile(supportedLanguage);
80124
} else {

src/grammar/postgresql/PostgreSqlParser.g4

+1-3
Original file line numberDiff line numberDiff line change
@@ -526,9 +526,7 @@ reloption_elem
526526
;
527527

528528
partitionboundspec
529-
: KW_FOR KW_VALUES KW_WITH OPEN_PAREN nonreservedword Integral (
530-
COMMA (nonreservedword Integral)
531-
)* CLOSE_PAREN
529+
: KW_FOR KW_VALUES KW_WITH OPEN_PAREN KW_MODULUS Integral COMMA KW_REMAINDER Integral CLOSE_PAREN
532530
| KW_FOR KW_VALUES KW_IN execute_param_clause
533531
| KW_FOR KW_VALUES KW_FROM execute_param_clause KW_TO execute_param_clause
534532
| KW_DEFAULT

src/grammar/trino/TrinoSql.g4

+9-11
Original file line numberDiff line numberDiff line change
@@ -737,17 +737,15 @@ normalForm
737737
;
738738

739739
type
740-
: KW_ROW '(' rowField (',' rowField)* ')' # rowType
741-
| KW_INTERVAL from=intervalField (KW_TO to=intervalField)? # intervalType
742-
| base=KW_TIMESTAMP ('(' precision= typeParameter ')')? (KW_WITHOUT KW_TIME KW_ZONE)? # dateTimeType
743-
| base=KW_TIMESTAMP ('(' precision= typeParameter ')')? KW_WITH KW_TIME KW_ZONE # dateTimeType
744-
| base=KW_TIME ('(' precision= typeParameter ')')? (KW_WITHOUT KW_TIME KW_ZONE)? # dateTimeType
745-
| base=KW_TIME ('(' precision= typeParameter ')')? KW_WITH KW_TIME KW_ZONE # dateTimeType
746-
| KW_DOUBLE KW_PRECISION # doublePrecisionType
747-
| KW_ARRAY '<' type '>' # legacyArrayType
748-
| KW_MAP '<' keyType=type ',' valueType=type '>' # legacyMapType
749-
| type KW_ARRAY ('[' INTEGER_VALUE ']')? # arrayType
750-
| identifier ('(' typeParameter (',' typeParameter)* ')')? # genericType
740+
: KW_ROW '(' rowField (',' rowField)* ')' # rowType
741+
| KW_INTERVAL from=intervalField (KW_TO to=intervalField)? # intervalType
742+
| base=KW_TIMESTAMP ('(' precision= typeParameter ')')? ((KW_WITH | KW_WITHOUT) KW_TIME KW_ZONE)? # dateTimeType
743+
| base=KW_TIME ('(' precision= typeParameter ')')? ((KW_WITH | KW_WITHOUT) KW_TIME KW_ZONE)? # timeType
744+
| KW_DOUBLE KW_PRECISION # doublePrecisionType
745+
| KW_ARRAY '<' type '>' # legacyArrayType
746+
| KW_MAP '<' keyType=type ',' valueType=type '>' # legacyMapType
747+
| type KW_ARRAY ('[' INTEGER_VALUE ']')? # arrayType
748+
| identifier ('(' typeParameter (',' typeParameter)* ')')? # genericType
751749
;
752750

753751
rowField

src/lib/mysql/MySqlLexer.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
// Generated from dt-sql-parser/src/grammar/mysql/MySqlLexer.g4 by ANTLR 4.13.1
2-
3-
// @ts-nocheck
1+
// Generated from /Users/xuxiaoqi/Documents/work/dt-sql-parser/src/grammar/mysql/MySqlLexer.g4 by ANTLR 4.13.1
42

53
import * as antlr from "antlr4ng";
64
import { Token } from "antlr4ng";

src/lib/mysql/MySqlParser.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
// Generated from dt-sql-parser/src/grammar/mysql/MySqlParser.g4 by ANTLR 4.13.1
2-
3-
// @ts-nocheck
1+
// Generated from /Users/xuxiaoqi/Documents/work/dt-sql-parser/src/grammar/mysql/MySqlParser.g4 by ANTLR 4.13.1
42

53
import * as antlr from "antlr4ng";
64
import { Token } from "antlr4ng";

src/lib/mysql/MySqlParserListener.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
// Generated from dt-sql-parser/src/grammar/mysql/MySqlParser.g4 by ANTLR 4.13.1
2-
3-
// @ts-nocheck
1+
// Generated from /Users/xuxiaoqi/Documents/work/dt-sql-parser/src/grammar/mysql/MySqlParser.g4 by ANTLR 4.13.1
42

53
import { ErrorNode, ParseTreeListener, ParserRuleContext, TerminalNode } from "antlr4ng";
64

src/lib/mysql/MySqlParserVisitor.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
// Generated from dt-sql-parser/src/grammar/mysql/MySqlParser.g4 by ANTLR 4.13.1
2-
3-
// @ts-nocheck
1+
// Generated from /Users/xuxiaoqi/Documents/work/dt-sql-parser/src/grammar/mysql/MySqlParser.g4 by ANTLR 4.13.1
42

53
import { AbstractParseTreeVisitor } from "antlr4ng";
64

src/lib/postgresql/PostgreSqlLexer.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
// Generated from dt-sql-parser/src/grammar/postgresql/PostgreSqlLexer.g4 by ANTLR 4.13.1
2-
3-
// @ts-nocheck
1+
// Generated from /Users/xuxiaoqi/Documents/work/dt-sql-parser/src/grammar/postgresql/PostgreSqlLexer.g4 by ANTLR 4.13.1
42

53
import * as antlr from "antlr4ng";
64
import { Token } from "antlr4ng";

src/lib/postgresql/PostgreSqlParser.interp

+1-1
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)