-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy pathindex.ts
142 lines (130 loc) · 5.62 KB
/
index.ts
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
import { CharStream, CommonTokenStream, Token } from 'antlr4ng';
import { CandidatesCollection } from 'antlr4-c3';
import { TrinoSqlLexer } from '../../lib/trino/TrinoSqlLexer';
import { TrinoSqlParser, ProgramContext } from '../../lib/trino/TrinoSqlParser';
import { BasicSQL } from '../common/basicSQL';
import { Suggestions, EntityContextType, SyntaxSuggestion } from '../common/types';
import { StmtContextType } from '../common/entityCollector';
import { TrinoSqlSplitListener } from './trinoSplitListener';
import { TrinoEntityCollector } from './trinoEntityCollector';
import { ErrorListener } from '../common/parseErrorListener';
import { TrinoErrorListener } from './trinoErrorListener';
export { TrinoSqlSplitListener, TrinoEntityCollector };
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) {
return new TrinoErrorListener(_errorListener, this.preferredRules, this.locale);
}
protected createEntityCollector(input: string, caretTokenIndex?: number) {
return new TrinoEntityCollector(input, caretTokenIndex);
}
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_columnNameCreate,
]);
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 startTokenIndex = candidateRule.startTokenIndex;
const tokenRanges = allTokens.slice(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;
}
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 (displayName && symbolicName && symbolicName.startsWith('KW_')) {
const keyword =
displayName.startsWith("'") && displayName.endsWith("'")
? displayName.slice(1, -1)
: displayName;
keywords.push(keyword);
}
}
return {
syntax: originalSyntaxSuggestions,
keywords,
};
}
}