2023-06-09 11:22:53 +08:00
|
|
|
import { Token } from 'antlr4ts';
|
|
|
|
import { CandidatesCollection } from 'antlr4-c3';
|
2023-05-30 14:44:03 +08:00
|
|
|
import { TrinoSqlLexer } from '../lib/trinosql/TrinoSqlLexer';
|
2023-10-24 14:37:27 +08:00
|
|
|
import { TrinoSqlParser, ProgramContext, StatementContext } from '../lib/trinosql/TrinoSqlParser';
|
|
|
|
import { TrinoSqlListener } from '../lib/trinosql/TrinoSqlListener';
|
2023-05-24 15:07:02 +08:00
|
|
|
import BasicParser from './common/basicParser';
|
2023-10-24 14:37:27 +08:00
|
|
|
import { Suggestions, SyntaxContextType, SyntaxSuggestion } from './common/basic-parser-types';
|
2023-06-09 11:22:53 +08:00
|
|
|
|
|
|
|
export default class TrinoSQL extends BasicParser<TrinoSqlLexer, ProgramContext, TrinoSqlParser> {
|
2023-06-16 16:14:53 +08:00
|
|
|
protected createLexerFormCharStream(charStreams) {
|
2023-06-09 11:22:53 +08:00
|
|
|
const lexer = new TrinoSqlLexer(charStreams);
|
2023-05-24 15:07:02 +08:00
|
|
|
return lexer;
|
|
|
|
}
|
2023-06-09 11:22:53 +08:00
|
|
|
|
2023-06-16 16:14:53 +08:00
|
|
|
protected createParserFromTokenStream(tokenStream) {
|
2023-06-09 11:22:53 +08:00
|
|
|
const parser = new TrinoSqlParser(tokenStream);
|
2023-05-24 15:07:02 +08:00
|
|
|
return parser;
|
|
|
|
}
|
2023-06-09 11:22:53 +08:00
|
|
|
|
2023-10-13 11:16:36 +08:00
|
|
|
protected get splitListener() {
|
2023-10-24 14:37:27 +08:00
|
|
|
return new TrinoSqlSplitListener();
|
2023-06-09 11:22:53 +08:00
|
|
|
}
|
|
|
|
|
2023-10-24 14:37:27 +08:00
|
|
|
protected preferredRules: Set<number> = new Set([
|
|
|
|
TrinoSqlParser.RULE_catalogName,
|
|
|
|
TrinoSqlParser.RULE_catalogNameCreate,
|
|
|
|
TrinoSqlParser.RULE_schemaName,
|
|
|
|
TrinoSqlParser.RULE_schemaNameCreate,
|
|
|
|
TrinoSqlParser.RULE_tableName,
|
|
|
|
TrinoSqlParser.RULE_tableNameCreate,
|
|
|
|
TrinoSqlParser.RULE_viewName,
|
|
|
|
TrinoSqlParser.RULE_viewNameCreate,
|
|
|
|
TrinoSqlParser.RULE_functionName,
|
2023-11-28 21:18:44 +08:00
|
|
|
TrinoSqlParser.RULE_columnName,
|
|
|
|
TrinoSqlParser.RULE_columnNameCreate,
|
2023-10-24 14:37:27 +08:00
|
|
|
]);
|
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-10-24 14:37:27 +08:00
|
|
|
caretTokenIndex: number,
|
|
|
|
tokenIndexOffset: number
|
2023-06-09 11:22:53 +08:00
|
|
|
): Suggestions<Token> {
|
2023-10-24 14:37:27 +08:00
|
|
|
const originalSyntaxSuggestions: SyntaxSuggestion<Token>[] = [];
|
|
|
|
const keywords: string[] = [];
|
|
|
|
|
|
|
|
for (let 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 TrinoSqlParser.RULE_catalogName: {
|
|
|
|
syntaxContextType = SyntaxContextType.CATALOG;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case TrinoSqlParser.RULE_schemaName: {
|
|
|
|
syntaxContextType = SyntaxContextType.DATABASE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case TrinoSqlParser.RULE_schemaNameCreate: {
|
|
|
|
syntaxContextType = SyntaxContextType.DATABASE_CREATE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case TrinoSqlParser.RULE_tableName: {
|
|
|
|
syntaxContextType = SyntaxContextType.TABLE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case TrinoSqlParser.RULE_tableNameCreate: {
|
|
|
|
syntaxContextType = SyntaxContextType.TABLE_CREATE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case TrinoSqlParser.RULE_viewName: {
|
|
|
|
syntaxContextType = SyntaxContextType.VIEW;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case TrinoSqlParser.RULE_viewNameCreate: {
|
|
|
|
syntaxContextType = SyntaxContextType.VIEW_CREATE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case TrinoSqlParser.RULE_functionName: {
|
|
|
|
syntaxContextType = SyntaxContextType.FUNCTION;
|
|
|
|
break;
|
|
|
|
}
|
2023-11-28 21:18:44 +08:00
|
|
|
case TrinoSqlParser.RULE_columnNameCreate: {
|
|
|
|
syntaxContextType = SyntaxContextType.COLUMN_CREATE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case TrinoSqlParser.RULE_columnName: {
|
|
|
|
syntaxContextType = SyntaxContextType.COLUMN;
|
|
|
|
break;
|
|
|
|
}
|
2023-10-24 14:37:27 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (syntaxContextType) {
|
|
|
|
originalSyntaxSuggestions.push({
|
|
|
|
syntaxContextType,
|
|
|
|
wordRanges: tokenRanges,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let 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-10-24 14:37:27 +08:00
|
|
|
syntax: originalSyntaxSuggestions,
|
|
|
|
keywords,
|
2023-10-13 11:16:36 +08:00
|
|
|
};
|
2023-06-09 11:22:53 +08:00
|
|
|
}
|
2023-05-24 15:07:02 +08:00
|
|
|
}
|
2023-10-24 14:37:27 +08:00
|
|
|
|
|
|
|
export class TrinoSqlSplitListener implements TrinoSqlListener {
|
|
|
|
private _statementsContext: StatementContext[] = [];
|
|
|
|
|
|
|
|
exitStatement = (ctx: StatementContext) => {
|
|
|
|
this._statementsContext.push(ctx);
|
|
|
|
};
|
|
|
|
|
|
|
|
get statementsContext() {
|
|
|
|
return this._statementsContext;
|
|
|
|
}
|
|
|
|
}
|