forked from DTStack/dt-sql-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
179 lines (165 loc) · 7.24 KB
/
index.ts
File metadata and controls
179 lines (165 loc) · 7.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import { CandidatesCollection } from 'antlr4-c3';
import { CharStream, CommonTokenStream, Token } from 'antlr4ng';
import { processTokenCandidates } from '../common/tokenUtils';
import { TrinoSqlLexer } from '../../lib/trino/TrinoSqlLexer';
import { ProgramContext, TrinoSqlParser } from '../../lib/trino/TrinoSqlParser';
import { BasicSQL } from '../common/basicSQL';
import {
Suggestions,
EntityContextType,
SyntaxSuggestion,
CaretPosition,
SemanticCollectOptions,
} from '../common/types';
import { StmtContextType } from '../common/entityCollector';
import { ErrorListener } from '../common/parseErrorListener';
import { TrinoEntityCollector } from './trinoEntityCollector';
import { TrinoErrorListener } from './trinoErrorListener';
import { TrinoSqlSplitListener } from './trinoSplitListener';
import { TrinoSemanticContextCollector } from './trinoSemanticContextCollector';
export { TrinoEntityCollector, TrinoSqlSplitListener };
export class TrinoSQL extends BasicSQL<TrinoSqlLexer, ProgramContext, TrinoSqlParser> {
protected createLexerFromCharStream(charStreams: CharStream) {
return new TrinoSqlLexer(charStreams);
}
protected createParserFromTokenStream(tokenStream: CommonTokenStream) {
return new TrinoSqlParser(tokenStream);
}
protected get splitListener() {
return new TrinoSqlSplitListener();
}
protected createErrorListener(_errorListener: ErrorListener): TrinoErrorListener {
const parserContext = this;
return new TrinoErrorListener(_errorListener, parserContext, this.preferredRules);
}
protected createEntityCollector(input: string, allTokens?: Token[], caretTokenIndex?: number) {
return new TrinoEntityCollector(input, allTokens, caretTokenIndex);
}
protected createSemanticContextCollector(
input: string,
caretPosition: CaretPosition,
allTokens: Token[],
options?: SemanticCollectOptions
) {
return new TrinoSemanticContextCollector(input, caretPosition, allTokens, options);
}
/**
* The rules that keywords you don't want to be suggested.
*/
protected excludeKeywordRules = new Set([TrinoSqlParser.RULE_nonReserved]);
protected preferredRules: Set<number> = new Set([
TrinoSqlParser.RULE_catalogRef,
TrinoSqlParser.RULE_catalogNameCreate,
TrinoSqlParser.RULE_schemaRef,
TrinoSqlParser.RULE_schemaNameCreate,
TrinoSqlParser.RULE_tableRef,
TrinoSqlParser.RULE_tableNameCreate,
TrinoSqlParser.RULE_viewRef,
TrinoSqlParser.RULE_viewNameCreate,
TrinoSqlParser.RULE_functionName,
TrinoSqlParser.RULE_functionNameCreate,
TrinoSqlParser.RULE_columnRef,
TrinoSqlParser.RULE_columnName,
TrinoSqlParser.RULE_columnNameCreate,
...this.excludeKeywordRules,
]);
protected processCandidates(
candidates: CandidatesCollection,
allTokens: Token[],
caretTokenIndex: number
): Suggestions<Token> {
const originalSyntaxSuggestions: SyntaxSuggestion<Token>[] = [];
const keywords: string[] = [];
for (let candidate of candidates.rules) {
const [ruleType, candidateRule] = candidate;
const tokenRanges = allTokens.slice(candidateRule.startTokenIndex, caretTokenIndex + 1);
let syntaxContextType: EntityContextType | StmtContextType | undefined = void 0;
switch (ruleType) {
case TrinoSqlParser.RULE_catalogRef: {
syntaxContextType = EntityContextType.CATALOG;
break;
}
case TrinoSqlParser.RULE_catalogNameCreate: {
syntaxContextType = EntityContextType.CATALOG_CREATE;
break;
}
case TrinoSqlParser.RULE_schemaRef: {
syntaxContextType = EntityContextType.DATABASE;
break;
}
case TrinoSqlParser.RULE_schemaNameCreate: {
syntaxContextType = EntityContextType.DATABASE_CREATE;
break;
}
case TrinoSqlParser.RULE_tableRef: {
syntaxContextType = EntityContextType.TABLE;
break;
}
case TrinoSqlParser.RULE_tableNameCreate: {
syntaxContextType = EntityContextType.TABLE_CREATE;
break;
}
case TrinoSqlParser.RULE_viewRef: {
syntaxContextType = EntityContextType.VIEW;
break;
}
case TrinoSqlParser.RULE_viewNameCreate: {
syntaxContextType = EntityContextType.VIEW_CREATE;
break;
}
case TrinoSqlParser.RULE_functionName: {
syntaxContextType = EntityContextType.FUNCTION;
break;
}
case TrinoSqlParser.RULE_functionNameCreate: {
syntaxContextType = EntityContextType.FUNCTION_CREATE;
break;
}
case TrinoSqlParser.RULE_columnNameCreate: {
syntaxContextType = EntityContextType.COLUMN_CREATE;
break;
}
case TrinoSqlParser.RULE_columnRef: {
syntaxContextType = EntityContextType.COLUMN;
break;
}
case TrinoSqlParser.RULE_columnName: {
if (
candidateRule.ruleList.includes(TrinoSqlParser.RULE_selectItem) ||
candidateRule.ruleList.includes(TrinoSqlParser.RULE_groupBy) ||
candidateRule.ruleList.includes(TrinoSqlParser.RULE_sortItem) ||
candidateRule.ruleList.includes(TrinoSqlParser.RULE_whereClause) ||
candidateRule.ruleList.includes(TrinoSqlParser.RULE_havingClause) ||
candidateRule.ruleList.includes(TrinoSqlParser.RULE_partitionBy) ||
candidateRule.ruleList.includes(TrinoSqlParser.RULE_whenClause) ||
candidateRule.ruleList.includes(TrinoSqlParser.RULE_relation)
) {
syntaxContextType = EntityContextType.COLUMN;
}
}
default:
break;
}
if (
syntaxContextType &&
!originalSyntaxSuggestions.some(
(syn) =>
syn.syntaxContextType === syntaxContextType &&
syn.wordRanges.map((wordRange: Token) => wordRange.text)?.join(',') ===
tokenRanges.map((tokenRange: Token) => tokenRange.text)?.join(',')
)
) {
originalSyntaxSuggestions.push({
syntaxContextType,
wordRanges: tokenRanges,
});
}
}
const processedKeywords = processTokenCandidates(this._parser, candidates.tokens);
keywords.push(...processedKeywords);
return {
syntax: originalSyntaxSuggestions,
keywords,
};
}
}