dt-sql-parser 4.1.0-beta.0 → 4.1.0-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -40,9 +40,8 @@ export declare abstract class BasicSQL<L extends Lexer = Lexer, PRC extends Pars
40
40
  * @param candidates candidate list
41
41
  * @param allTokens all tokens from input
42
42
  * @param caretTokenIndex tokenIndex of caretPosition
43
- * @param tokenIndexOffset offset of the tokenIndex in the candidates compared to the tokenIndex in allTokens
44
43
  */
45
- protected abstract processCandidates(candidates: CandidatesCollection, allTokens: Token[], caretTokenIndex: number, tokenIndexOffset: number): Suggestions<Token>;
44
+ protected abstract processCandidates(candidates: CandidatesCollection, allTokens: Token[], caretTokenIndex: number): Suggestions<Token>;
46
45
  /**
47
46
  * Get a new splitListener instance.
48
47
  */
@@ -111,6 +110,13 @@ export declare abstract class BasicSQL<L extends Lexer = Lexer, PRC extends Pars
111
110
  * @param input source string
112
111
  */
113
112
  splitSQLByStatement(input: string): TextSlice[] | null;
113
+ /**
114
+ * Get the smaller range of input
115
+ * @param input string
116
+ * @param caretTokenIndex tokenIndex of caretPosition
117
+ * @returns inputSlice: string, caretTokenIndex: number, allTokens: Token[]
118
+ */
119
+ private splitInputBySeparator;
114
120
  /**
115
121
  * Get suggestions of syntax and token at caretPosition
116
122
  * @param input source string
@@ -3,6 +3,7 @@ import { CodeCompletionCore } from 'antlr4-c3';
3
3
  import { findCaretTokenIndex } from './findCaretTokenIndex';
4
4
  import { ctxToText, tokenToWord } from './textAndWord';
5
5
  import { ErrorStrategy } from './errorStrategy';
6
+ const SEPARATOR = ';';
6
7
  /**
7
8
  * Basic SQL class, every sql needs extends it.
8
9
  */
@@ -148,6 +149,58 @@ export class BasicSQL {
148
149
  .filter(Boolean);
149
150
  return res;
150
151
  }
152
+ /**
153
+ * Get the smaller range of input
154
+ * @param input string
155
+ * @param caretTokenIndex tokenIndex of caretPosition
156
+ * @returns inputSlice: string, caretTokenIndex: number, allTokens: Token[]
157
+ */
158
+ splitInputBySeparator(input, caretTokenIndex) {
159
+ var _a, _b, _c;
160
+ const allTokens = this.getAllTokens(input);
161
+ /**
162
+ * set startToken
163
+ */
164
+ let startToken = null;
165
+ for (let tokenIndex = caretTokenIndex; tokenIndex >= 0; tokenIndex--) {
166
+ const token = allTokens[tokenIndex];
167
+ if ((token === null || token === void 0 ? void 0 : token.text) === SEPARATOR) {
168
+ startToken = allTokens[tokenIndex + 1];
169
+ break;
170
+ }
171
+ }
172
+ if (startToken === null) {
173
+ startToken = allTokens[0];
174
+ }
175
+ /**
176
+ * set stopToken
177
+ */
178
+ let stopToken = null;
179
+ for (let tokenIndex = caretTokenIndex; tokenIndex < allTokens.length; tokenIndex++) {
180
+ const token = allTokens[tokenIndex];
181
+ if ((token === null || token === void 0 ? void 0 : token.text) === SEPARATOR) {
182
+ stopToken = token;
183
+ break;
184
+ }
185
+ }
186
+ if (stopToken === null) {
187
+ stopToken = allTokens[allTokens.length - 1];
188
+ }
189
+ let startIndex = (_a = startToken === null || startToken === void 0 ? void 0 : startToken.start) !== null && _a !== void 0 ? _a : 0;
190
+ let stopIndex = (_b = (stopToken === null || stopToken === void 0 ? void 0 : stopToken.stop) + 1) !== null && _b !== void 0 ? _b : input.length;
191
+ /**
192
+ * Save offset of the tokenIndex in the range of input
193
+ * compared to the tokenIndex in the whole input
194
+ */
195
+ const tokenIndexOffset = (_c = startToken === null || startToken === void 0 ? void 0 : startToken.tokenIndex) !== null && _c !== void 0 ? _c : 0;
196
+ const _caretTokenIndex = caretTokenIndex - tokenIndexOffset;
197
+ /**
198
+ * Get the smaller range of _input
199
+ */
200
+ const _input = input.slice(startIndex, stopIndex);
201
+ const _allTokens = this.getAllTokens(_input);
202
+ return { inputSlice: _input, caretTokenIndex: _caretTokenIndex, allTokens: _allTokens };
203
+ }
151
204
  /**
152
205
  * Get suggestions of syntax and token at caretPosition
153
206
  * @param input source string
@@ -157,11 +210,12 @@ export class BasicSQL {
157
210
  getSuggestionAtCaretPosition(input, caretPosition) {
158
211
  var _a, _b, _c, _d, _e, _f, _g, _h, _j;
159
212
  const splitListener = this.splitListener;
160
- this.parseWithCache(input);
213
+ let inputSlice = input;
214
+ this.parseWithCache(inputSlice);
161
215
  if (!this._parseTree)
162
216
  return null;
163
217
  let sqlParserIns = this._parser;
164
- const allTokens = this.getAllTokens(input);
218
+ let allTokens = this.getAllTokens(inputSlice);
165
219
  let caretTokenIndex = findCaretTokenIndex(caretPosition, allTokens);
166
220
  let c3Context = this._parseTree;
167
221
  let tokenIndexOffset = 0;
@@ -206,19 +260,29 @@ export class BasicSQL {
206
260
  }
207
261
  }
208
262
  // A boundary consisting of the index of the input.
209
- const startIndex = (_e = (_d = startStatement === null || startStatement === void 0 ? void 0 : startStatement.start) === null || _d === void 0 ? void 0 : _d.start) !== null && _e !== void 0 ? _e : 0;
210
- const stopIndex = (_g = (_f = stopStatement === null || stopStatement === void 0 ? void 0 : stopStatement.stop) === null || _f === void 0 ? void 0 : _f.stop) !== null && _g !== void 0 ? _g : input.length - 1;
263
+ let startIndex = (_e = (_d = startStatement === null || startStatement === void 0 ? void 0 : startStatement.start) === null || _d === void 0 ? void 0 : _d.start) !== null && _e !== void 0 ? _e : 0;
264
+ let stopIndex = (_g = (_f = stopStatement === null || stopStatement === void 0 ? void 0 : stopStatement.stop) === null || _f === void 0 ? void 0 : _f.stop) !== null && _g !== void 0 ? _g : inputSlice.length - 1;
211
265
  /**
212
266
  * Save offset of the tokenIndex in the range of input
213
267
  * compared to the tokenIndex in the whole input
214
268
  */
215
269
  tokenIndexOffset = (_j = (_h = startStatement === null || startStatement === void 0 ? void 0 : startStatement.start) === null || _h === void 0 ? void 0 : _h.tokenIndex) !== null && _j !== void 0 ? _j : 0;
216
270
  caretTokenIndex = caretTokenIndex - tokenIndexOffset;
217
- /**
218
- * Reparse the input fragment,
219
- * and c3 will collect candidates in the newly generated parseTree.
220
- */
221
- const inputSlice = input.slice(startIndex, stopIndex);
271
+ inputSlice = inputSlice.slice(startIndex, stopIndex);
272
+ }
273
+ /**
274
+ * Split the inputSlice by separator to get the smaller range of inputSlice.
275
+ */
276
+ if (inputSlice.includes(SEPARATOR)) {
277
+ const { inputSlice: _input, caretTokenIndex: _caretTokenIndex, allTokens: _allTokens, } = this.splitInputBySeparator(inputSlice, caretTokenIndex);
278
+ caretTokenIndex = _caretTokenIndex;
279
+ inputSlice = _input;
280
+ allTokens = _allTokens;
281
+ }
282
+ /**
283
+ * Reparse the input fragment, and c3 will collect candidates in the newly generated parseTree when input changed.
284
+ */
285
+ if (inputSlice !== input) {
222
286
  const lexer = this.createLexer(inputSlice);
223
287
  lexer.removeErrorListeners();
224
288
  const tokenStream = new CommonTokenStream(lexer);
@@ -234,7 +298,7 @@ export class BasicSQL {
234
298
  const core = new CodeCompletionCore(sqlParserIns);
235
299
  core.preferredRules = this.preferredRules;
236
300
  const candidates = core.collectCandidates(caretTokenIndex, c3Context);
237
- const originalSuggestions = this.processCandidates(candidates, allTokens, caretTokenIndex, tokenIndexOffset);
301
+ const originalSuggestions = this.processCandidates(candidates, allTokens, caretTokenIndex);
238
302
  const syntaxSuggestions = originalSuggestions.syntax.map((syntaxCtx) => {
239
303
  const wordRanges = syntaxCtx.wordRanges.map((token) => {
240
304
  return tokenToWord(token, this._parsedInput);
@@ -16,5 +16,5 @@ export declare class FlinkSQL extends BasicSQL<FlinkSqlLexer, ProgramContext, Fl
16
16
  protected get splitListener(): FlinkSqlSplitListener;
17
17
  protected createErrorListener(_errorListener: ErrorListener): FlinkErrorListener;
18
18
  protected createEntityCollector(input: string, caretTokenIndex?: number): FlinkEntityCollector;
19
- protected processCandidates(candidates: CandidatesCollection, allTokens: Token[], caretTokenIndex: number, tokenIndexOffset: number): Suggestions<Token>;
19
+ protected processCandidates(candidates: CandidatesCollection, allTokens: Token[], caretTokenIndex: number): Suggestions<Token>;
20
20
  }
@@ -38,13 +38,13 @@ export class FlinkSQL extends BasicSQL {
38
38
  createEntityCollector(input, caretTokenIndex) {
39
39
  return new FlinkEntityCollector(input, caretTokenIndex);
40
40
  }
41
- processCandidates(candidates, allTokens, caretTokenIndex, tokenIndexOffset) {
41
+ processCandidates(candidates, allTokens, caretTokenIndex) {
42
42
  const originalSyntaxSuggestions = [];
43
43
  const keywords = [];
44
44
  for (let candidate of candidates.rules) {
45
45
  const [ruleType, candidateRule] = candidate;
46
- const startTokenIndex = candidateRule.startTokenIndex + tokenIndexOffset;
47
- const tokenRanges = allTokens.slice(startTokenIndex, caretTokenIndex + tokenIndexOffset + 1);
46
+ const startTokenIndex = candidateRule.startTokenIndex;
47
+ const tokenRanges = allTokens.slice(startTokenIndex, caretTokenIndex + 1);
48
48
  let syntaxContextType = void 0;
49
49
  switch (ruleType) {
50
50
  case FlinkSqlParser.RULE_catalogPath: {
@@ -16,5 +16,5 @@ export declare class HiveSQL extends BasicSQL<HiveSqlLexer, ProgramContext, Hive
16
16
  protected get splitListener(): HiveSqlSplitListener;
17
17
  protected createErrorListener(_errorListener: ErrorListener): HiveErrorListener;
18
18
  protected createEntityCollector(input: string, caretTokenIndex?: number): HiveEntityCollector;
19
- protected processCandidates(candidates: CandidatesCollection, allTokens: Token[], caretTokenIndex: number, tokenIndexOffset: number): Suggestions<Token>;
19
+ protected processCandidates(candidates: CandidatesCollection, allTokens: Token[], caretTokenIndex: number): Suggestions<Token>;
20
20
  }
@@ -38,13 +38,13 @@ export class HiveSQL extends BasicSQL {
38
38
  createEntityCollector(input, caretTokenIndex) {
39
39
  return new HiveEntityCollector(input, caretTokenIndex);
40
40
  }
41
- processCandidates(candidates, allTokens, caretTokenIndex, tokenIndexOffset) {
41
+ processCandidates(candidates, allTokens, caretTokenIndex) {
42
42
  const originalSyntaxSuggestions = [];
43
43
  const keywords = [];
44
44
  for (let candidate of candidates.rules) {
45
45
  const [ruleType, candidateRule] = candidate;
46
- const startTokenIndex = candidateRule.startTokenIndex + tokenIndexOffset;
47
- const tokenRanges = allTokens.slice(startTokenIndex, caretTokenIndex + tokenIndexOffset + 1);
46
+ const startTokenIndex = candidateRule.startTokenIndex;
47
+ const tokenRanges = allTokens.slice(startTokenIndex, caretTokenIndex + 1);
48
48
  let syntaxContextType = void 0;
49
49
  switch (ruleType) {
50
50
  case HiveSqlParser.RULE_dbSchemaName: {
@@ -16,5 +16,5 @@ export declare class ImpalaSQL extends BasicSQL<ImpalaSqlLexer, ProgramContext,
16
16
  protected get splitListener(): ImpalaSqlSplitListener;
17
17
  protected createErrorListener(_errorListener: ErrorListener): ImpalaErrorListener;
18
18
  protected createEntityCollector(input: string, caretTokenIndex?: number): ImpalaEntityCollector;
19
- protected processCandidates(candidates: CandidatesCollection, allTokens: Token[], caretTokenIndex: number, tokenIndexOffset: number): Suggestions<Token>;
19
+ protected processCandidates(candidates: CandidatesCollection, allTokens: Token[], caretTokenIndex: number): Suggestions<Token>;
20
20
  }
@@ -37,13 +37,13 @@ export class ImpalaSQL extends BasicSQL {
37
37
  createEntityCollector(input, caretTokenIndex) {
38
38
  return new ImpalaEntityCollector(input, caretTokenIndex);
39
39
  }
40
- processCandidates(candidates, allTokens, caretTokenIndex, tokenIndexOffset) {
40
+ processCandidates(candidates, allTokens, caretTokenIndex) {
41
41
  const originalSyntaxSuggestions = [];
42
42
  const keywords = [];
43
43
  for (let candidate of candidates.rules) {
44
44
  const [ruleType, candidateRule] = candidate;
45
- const startTokenIndex = candidateRule.startTokenIndex + tokenIndexOffset;
46
- const tokenRanges = allTokens.slice(startTokenIndex, caretTokenIndex + tokenIndexOffset + 1);
45
+ const startTokenIndex = candidateRule.startTokenIndex;
46
+ const tokenRanges = allTokens.slice(startTokenIndex, caretTokenIndex + 1);
47
47
  let syntaxContextType = void 0;
48
48
  switch (ruleType) {
49
49
  case ImpalaSqlParser.RULE_functionNameCreate: {
@@ -16,5 +16,5 @@ export declare class MySQL extends BasicSQL<MySqlLexer, ProgramContext, MySqlPar
16
16
  protected get splitListener(): MysqlSplitListener;
17
17
  protected createErrorListener(_errorListener: ErrorListener): MysqlErrorListener;
18
18
  protected createEntityCollector(input: string, caretTokenIndex?: number): MySqlEntityCollector;
19
- protected processCandidates(candidates: CandidatesCollection, allTokens: Token[], caretTokenIndex: number, tokenIndexOffset: number): Suggestions<Token>;
19
+ protected processCandidates(candidates: CandidatesCollection, allTokens: Token[], caretTokenIndex: number): Suggestions<Token>;
20
20
  }
@@ -37,13 +37,13 @@ export class MySQL extends BasicSQL {
37
37
  createEntityCollector(input, caretTokenIndex) {
38
38
  return new MySqlEntityCollector(input, caretTokenIndex);
39
39
  }
40
- processCandidates(candidates, allTokens, caretTokenIndex, tokenIndexOffset) {
40
+ processCandidates(candidates, allTokens, caretTokenIndex) {
41
41
  const originalSyntaxSuggestions = [];
42
42
  const keywords = [];
43
43
  for (const candidate of candidates.rules) {
44
44
  const [ruleType, candidateRule] = candidate;
45
- const startTokenIndex = candidateRule.startTokenIndex + tokenIndexOffset;
46
- const tokenRanges = allTokens.slice(startTokenIndex, caretTokenIndex + tokenIndexOffset + 1);
45
+ const startTokenIndex = candidateRule.startTokenIndex;
46
+ const tokenRanges = allTokens.slice(startTokenIndex, caretTokenIndex + 1);
47
47
  let syntaxContextType = void 0;
48
48
  switch (ruleType) {
49
49
  case MySqlParser.RULE_databaseName: {
@@ -16,5 +16,5 @@ export declare class PostgreSQL extends BasicSQL<PostgreSqlLexer, ProgramContext
16
16
  protected get splitListener(): PostgreSqlSplitListener;
17
17
  protected createErrorListener(_errorListener: ErrorListener): PostgreSqlErrorListener;
18
18
  protected createEntityCollector(input: string, caretTokenIndex?: number): PostgreSqlEntityCollector;
19
- protected processCandidates(candidates: CandidatesCollection, allTokens: Token[], caretTokenIndex: number, tokenIndexOffset: number): Suggestions<Token>;
19
+ protected processCandidates(candidates: CandidatesCollection, allTokens: Token[], caretTokenIndex: number): Suggestions<Token>;
20
20
  }
@@ -41,13 +41,13 @@ export class PostgreSQL extends BasicSQL {
41
41
  createEntityCollector(input, caretTokenIndex) {
42
42
  return new PostgreSqlEntityCollector(input, caretTokenIndex);
43
43
  }
44
- processCandidates(candidates, allTokens, caretTokenIndex, tokenIndexOffset) {
44
+ processCandidates(candidates, allTokens, caretTokenIndex) {
45
45
  const originalSyntaxSuggestions = [];
46
46
  const keywords = [];
47
47
  for (let candidate of candidates.rules) {
48
48
  const [ruleType, candidateRule] = candidate;
49
- const startTokenIndex = candidateRule.startTokenIndex + tokenIndexOffset;
50
- const tokenRanges = allTokens.slice(startTokenIndex, caretTokenIndex + tokenIndexOffset + 1);
49
+ const startTokenIndex = candidateRule.startTokenIndex;
50
+ const tokenRanges = allTokens.slice(startTokenIndex, caretTokenIndex + 1);
51
51
  let syntaxContextType = void 0;
52
52
  switch (ruleType) {
53
53
  case PostgreSqlParser.RULE_table_name_create: {
@@ -16,5 +16,5 @@ export declare class SparkSQL extends BasicSQL<SparkSqlLexer, ProgramContext, Sp
16
16
  protected get splitListener(): SparkSqlSplitListener;
17
17
  protected createErrorListener(_errorListener: ErrorListener): SparkErrorListener;
18
18
  protected createEntityCollector(input: string, caretTokenIndex?: number): SparkEntityCollector;
19
- protected processCandidates(candidates: CandidatesCollection, allTokens: Token[], caretTokenIndex: number, tokenIndexOffset: number): Suggestions<Token>;
19
+ protected processCandidates(candidates: CandidatesCollection, allTokens: Token[], caretTokenIndex: number): Suggestions<Token>;
20
20
  }
@@ -37,13 +37,13 @@ export class SparkSQL extends BasicSQL {
37
37
  createEntityCollector(input, caretTokenIndex) {
38
38
  return new SparkEntityCollector(input, caretTokenIndex);
39
39
  }
40
- processCandidates(candidates, allTokens, caretTokenIndex, tokenIndexOffset) {
40
+ processCandidates(candidates, allTokens, caretTokenIndex) {
41
41
  const originalSyntaxSuggestions = [];
42
42
  const keywords = [];
43
43
  for (const candidate of candidates.rules) {
44
44
  const [ruleType, candidateRule] = candidate;
45
- const startTokenIndex = candidateRule.startTokenIndex + tokenIndexOffset;
46
- const tokenRanges = allTokens.slice(startTokenIndex, caretTokenIndex + tokenIndexOffset + 1);
45
+ const startTokenIndex = candidateRule.startTokenIndex;
46
+ const tokenRanges = allTokens.slice(startTokenIndex, caretTokenIndex + 1);
47
47
  let syntaxContextType = void 0;
48
48
  switch (ruleType) {
49
49
  case SparkSqlParser.RULE_namespaceName: {
@@ -16,5 +16,5 @@ export declare class TrinoSQL extends BasicSQL<TrinoSqlLexer, ProgramContext, Tr
16
16
  protected createErrorListener(_errorListener: ErrorListener): TrinoErrorListener;
17
17
  protected createEntityCollector(input: string, caretTokenIndex?: number): TrinoEntityCollector;
18
18
  protected preferredRules: Set<number>;
19
- protected processCandidates(candidates: CandidatesCollection, allTokens: Token[], caretTokenIndex: number, tokenIndexOffset: number): Suggestions<Token>;
19
+ protected processCandidates(candidates: CandidatesCollection, allTokens: Token[], caretTokenIndex: number): Suggestions<Token>;
20
20
  }
@@ -39,13 +39,13 @@ export class TrinoSQL extends BasicSQL {
39
39
  createEntityCollector(input, caretTokenIndex) {
40
40
  return new TrinoEntityCollector(input, caretTokenIndex);
41
41
  }
42
- processCandidates(candidates, allTokens, caretTokenIndex, tokenIndexOffset) {
42
+ processCandidates(candidates, allTokens, caretTokenIndex) {
43
43
  const originalSyntaxSuggestions = [];
44
44
  const keywords = [];
45
45
  for (let candidate of candidates.rules) {
46
46
  const [ruleType, candidateRule] = candidate;
47
- const startTokenIndex = candidateRule.startTokenIndex + tokenIndexOffset;
48
- const tokenRanges = allTokens.slice(startTokenIndex, caretTokenIndex + tokenIndexOffset + 1);
47
+ const startTokenIndex = candidateRule.startTokenIndex;
48
+ const tokenRanges = allTokens.slice(startTokenIndex, caretTokenIndex + 1);
49
49
  let syntaxContextType = void 0;
50
50
  switch (ruleType) {
51
51
  case TrinoSqlParser.RULE_catalogRef: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dt-sql-parser",
3
- "version": "4.1.0-beta.0",
3
+ "version": "4.1.0-beta.2",
4
4
  "authors": "DTStack Corporation",
5
5
  "description": "SQL Parsers for BigData, built with antlr4",
6
6
  "keywords": [