lava-oushudb-dt-sql-parser/src/parser/mysql.ts
Hayden 195878da9b
feat: migrate to antlr4ng (#267)
* feat: replace antlr4ts with antlr4ng

* feat: switch caseInsensitive option on

* feat: recompile all g4 file

* feat:  update parser to fit antlr4ng

* test: update test to fit antlr4ng
2024-02-26 20:25:09 +08:00

142 lines
5.0 KiB
TypeScript

import { Token } from 'antlr4ng';
import { CandidatesCollection } from 'antlr4-c3';
import { MySqlLexer } from '../lib/mysql/MySqlLexer';
import { MySqlParser, ProgramContext, SingleStatementContext } from '../lib/mysql/MySqlParser';
import BasicParser from './common/basicParser';
import { Suggestions, SyntaxContextType, SyntaxSuggestion } from './common/basic-parser-types';
import { MySqlParserListener } from '../lib/mysql/MySqlParserListener';
export default class MySQL extends BasicParser<MySqlLexer, ProgramContext, MySqlParser> {
protected createLexerFromCharStream(charStreams): MySqlLexer {
const lexer = new MySqlLexer(charStreams);
return lexer;
}
protected createParserFromTokenStream(tokenStream): MySqlParser {
return new MySqlParser(tokenStream);
}
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,
]);
protected get splitListener() {
return new MysqlSplitListener();
}
protected processCandidates(
candidates: CandidatesCollection,
allTokens: Token[],
caretTokenIndex: number,
tokenIndexOffset: number
): Suggestions<Token> {
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);
}
}
return {
syntax: originalSyntaxSuggestions,
keywords,
};
}
}
export class MysqlSplitListener implements MySqlParserListener {
private _statementsContext: SingleStatementContext[] = [];
exitSingleStatement = (ctx: SingleStatementContext) => {
this._statementsContext.push(ctx);
};
visitTerminal() {}
visitErrorNode() {}
enterEveryRule() {}
exitEveryRule() {}
get statementsContext() {
return this._statementsContext;
}
}