2024-02-26 20:25:09 +08:00
|
|
|
import { Token } from 'antlr4ng';
|
2023-06-09 11:22:53 +08:00
|
|
|
import { CandidatesCollection } from 'antlr4-c3';
|
2023-11-27 15:25:40 +08:00
|
|
|
import { MySqlLexer } from '../lib/mysql/MySqlLexer';
|
2023-12-08 18:33:16 +08:00
|
|
|
import { MySqlParser, ProgramContext, SingleStatementContext } from '../lib/mysql/MySqlParser';
|
2021-01-05 15:27:43 +08:00
|
|
|
import BasicParser from './common/basicParser';
|
2023-11-28 21:17:18 +08:00
|
|
|
import { Suggestions, SyntaxContextType, SyntaxSuggestion } from './common/basic-parser-types';
|
2023-12-12 19:37:03 +08:00
|
|
|
import { MySqlParserListener } from '../lib/mysql/MySqlParserListener';
|
2020-09-11 17:39:10 +08:00
|
|
|
|
2023-11-27 15:25:40 +08:00
|
|
|
export default class MySQL extends BasicParser<MySqlLexer, ProgramContext, MySqlParser> {
|
2024-01-04 11:45:41 +08:00
|
|
|
protected createLexerFromCharStream(charStreams): MySqlLexer {
|
2023-11-27 15:25:40 +08:00
|
|
|
const lexer = new MySqlLexer(charStreams);
|
2020-09-11 17:39:10 +08:00
|
|
|
return lexer;
|
|
|
|
}
|
2023-06-09 11:22:53 +08:00
|
|
|
|
2023-11-27 15:25:40 +08:00
|
|
|
protected createParserFromTokenStream(tokenStream): MySqlParser {
|
|
|
|
return new MySqlParser(tokenStream);
|
2020-09-11 17:39:10 +08:00
|
|
|
}
|
2023-06-09 11:22:53 +08:00
|
|
|
|
2023-11-28 21:17:18 +08:00
|
|
|
protected preferredRules: Set<number> = new Set([
|
|
|
|
MySqlParser.RULE_databaseName,
|
|
|
|
MySqlParser.RULE_databaseNameCreate,
|
|
|
|
MySqlParser.RULE_tableName,
|
|
|
|
MySqlParser.RULE_tableNameCreate,
|
|
|
|
MySqlParser.RULE_viewName,
|
|
|
|
MySqlParser.RULE_viewNameCreate,
|
|
|
|
MySqlParser.RULE_functionName,
|
|
|
|
MySqlParser.RULE_functionNameCreate,
|
|
|
|
MySqlParser.RULE_columnName,
|
|
|
|
MySqlParser.RULE_columnNameCreate,
|
|
|
|
]);
|
2023-06-09 11:22:53 +08:00
|
|
|
|
2023-10-13 11:16:36 +08:00
|
|
|
protected get splitListener() {
|
2023-12-08 18:33:16 +08:00
|
|
|
return new MysqlSplitListener();
|
2023-06-09 11:22:53 +08:00
|
|
|
}
|
|
|
|
|
2023-06-16 16:14:53 +08:00
|
|
|
protected processCandidates(
|
2023-10-13 11:16:36 +08:00
|
|
|
candidates: CandidatesCollection,
|
|
|
|
allTokens: Token[],
|
2023-11-28 21:17:18 +08:00
|
|
|
caretTokenIndex: number,
|
|
|
|
tokenIndexOffset: number
|
2023-06-09 11:22:53 +08:00
|
|
|
): Suggestions<Token> {
|
2023-11-28 21:17:18 +08:00
|
|
|
const originalSyntaxSuggestions: SyntaxSuggestion<Token>[] = [];
|
|
|
|
const keywords: string[] = [];
|
|
|
|
|
|
|
|
for (const candidate of candidates.rules) {
|
|
|
|
const [ruleType, candidateRule] = candidate;
|
|
|
|
const startTokenIndex = candidateRule.startTokenIndex + tokenIndexOffset;
|
|
|
|
const tokenRanges = allTokens.slice(
|
|
|
|
startTokenIndex,
|
|
|
|
caretTokenIndex + tokenIndexOffset + 1
|
|
|
|
);
|
|
|
|
|
|
|
|
let syntaxContextType: SyntaxContextType;
|
|
|
|
switch (ruleType) {
|
|
|
|
case MySqlParser.RULE_databaseName: {
|
|
|
|
syntaxContextType = SyntaxContextType.DATABASE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MySqlParser.RULE_databaseNameCreate: {
|
|
|
|
syntaxContextType = SyntaxContextType.DATABASE_CREATE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MySqlParser.RULE_tableName: {
|
|
|
|
syntaxContextType = SyntaxContextType.TABLE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MySqlParser.RULE_tableNameCreate: {
|
|
|
|
syntaxContextType = SyntaxContextType.TABLE_CREATE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MySqlParser.RULE_viewName: {
|
|
|
|
syntaxContextType = SyntaxContextType.VIEW;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MySqlParser.RULE_viewNameCreate: {
|
|
|
|
syntaxContextType = SyntaxContextType.VIEW_CREATE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MySqlParser.RULE_functionName: {
|
|
|
|
syntaxContextType = SyntaxContextType.FUNCTION;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MySqlParser.RULE_functionNameCreate: {
|
|
|
|
syntaxContextType = SyntaxContextType.FUNCTION_CREATE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MySqlParser.RULE_columnName: {
|
|
|
|
syntaxContextType = SyntaxContextType.COLUMN;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MySqlParser.RULE_columnNameCreate: {
|
|
|
|
syntaxContextType = SyntaxContextType.COLUMN_CREATE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (syntaxContextType) {
|
|
|
|
originalSyntaxSuggestions.push({
|
|
|
|
syntaxContextType,
|
|
|
|
wordRanges: tokenRanges,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const candidate of candidates.tokens) {
|
|
|
|
const symbolicName = this._parser.vocabulary.getSymbolicName(candidate[0]);
|
|
|
|
const displayName = this._parser.vocabulary.getDisplayName(candidate[0]);
|
|
|
|
if (symbolicName && symbolicName.startsWith('KW_')) {
|
|
|
|
const keyword =
|
|
|
|
displayName.startsWith("'") && displayName.endsWith("'")
|
|
|
|
? displayName.slice(1, -1)
|
|
|
|
: displayName;
|
|
|
|
keywords.push(keyword);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-09 11:22:53 +08:00
|
|
|
return {
|
2023-11-28 21:17:18 +08:00
|
|
|
syntax: originalSyntaxSuggestions,
|
|
|
|
keywords,
|
2023-10-13 11:16:36 +08:00
|
|
|
};
|
2023-06-09 11:22:53 +08:00
|
|
|
}
|
2020-09-11 17:39:10 +08:00
|
|
|
}
|
2023-11-28 21:17:18 +08:00
|
|
|
|
2023-12-08 18:33:16 +08:00
|
|
|
export class MysqlSplitListener implements MySqlParserListener {
|
|
|
|
private _statementsContext: SingleStatementContext[] = [];
|
2023-11-28 21:17:18 +08:00
|
|
|
|
2023-12-08 18:33:16 +08:00
|
|
|
exitSingleStatement = (ctx: SingleStatementContext) => {
|
2023-11-28 21:17:18 +08:00
|
|
|
this._statementsContext.push(ctx);
|
|
|
|
};
|
|
|
|
|
2024-02-26 20:25:09 +08:00
|
|
|
visitTerminal() {}
|
|
|
|
visitErrorNode() {}
|
|
|
|
enterEveryRule() {}
|
|
|
|
exitEveryRule() {}
|
2023-11-28 21:17:18 +08:00
|
|
|
|
|
|
|
get statementsContext() {
|
|
|
|
return this._statementsContext;
|
|
|
|
}
|
|
|
|
}
|