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

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,7 @@ 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
+ private splitInputBySeparator;
114
114
  /**
115
115
  * Get suggestions of syntax and token at caretPosition
116
116
  * @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,39 @@ export class BasicSQL {
148
149
  .filter(Boolean);
149
150
  return res;
150
151
  }
152
+ // 通过分隔符进一步切分语句
153
+ splitInputBySeparator(inputSlice, caretTokenIndex) {
154
+ const allTokens = this.getAllTokens(inputSlice);
155
+ /**
156
+ * set startToken
157
+ */
158
+ let startToken = null;
159
+ for (let tokenIndex = caretTokenIndex; tokenIndex >= 0; tokenIndex--) {
160
+ const token = allTokens[tokenIndex];
161
+ if ((token === null || token === void 0 ? void 0 : token.text) === SEPARATOR) {
162
+ startToken = allTokens[tokenIndex + 1];
163
+ break;
164
+ }
165
+ }
166
+ if (startToken === null) {
167
+ startToken = allTokens[0];
168
+ }
169
+ /**
170
+ * set stopToken
171
+ */
172
+ let stopToken = null;
173
+ for (let tokenIndex = caretTokenIndex; tokenIndex < allTokens.length; tokenIndex++) {
174
+ const token = allTokens[tokenIndex];
175
+ if ((token === null || token === void 0 ? void 0 : token.text) === SEPARATOR) {
176
+ stopToken = token;
177
+ break;
178
+ }
179
+ }
180
+ if (stopToken === null) {
181
+ stopToken = allTokens[allTokens.length - 1];
182
+ }
183
+ return { startToken, stopToken };
184
+ }
151
185
  /**
152
186
  * Get suggestions of syntax and token at caretPosition
153
187
  * @param input source string
@@ -155,13 +189,13 @@ export class BasicSQL {
155
189
  * @returns suggestion
156
190
  */
157
191
  getSuggestionAtCaretPosition(input, caretPosition) {
158
- var _a, _b, _c, _d, _e, _f, _g, _h, _j;
192
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
159
193
  const splitListener = this.splitListener;
160
194
  this.parseWithCache(input);
161
195
  if (!this._parseTree)
162
196
  return null;
163
197
  let sqlParserIns = this._parser;
164
- const allTokens = this.getAllTokens(input);
198
+ let allTokens = this.getAllTokens(input);
165
199
  let caretTokenIndex = findCaretTokenIndex(caretPosition, allTokens);
166
200
  let c3Context = this._parseTree;
167
201
  let tokenIndexOffset = 0;
@@ -206,8 +240,8 @@ export class BasicSQL {
206
240
  }
207
241
  }
208
242
  // 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;
243
+ 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;
244
+ 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 : input.length - 1;
211
245
  /**
212
246
  * Save offset of the tokenIndex in the range of input
213
247
  * compared to the tokenIndex in the whole input
@@ -218,7 +252,26 @@ export class BasicSQL {
218
252
  * Reparse the input fragment,
219
253
  * and c3 will collect candidates in the newly generated parseTree.
220
254
  */
221
- const inputSlice = input.slice(startIndex, stopIndex);
255
+ let inputSlice = input.slice(startIndex, stopIndex);
256
+ /**
257
+ * Split the inputSlice by separator.
258
+ */
259
+ if (inputSlice.includes(SEPARATOR)) {
260
+ const { startToken, stopToken } = this.splitInputBySeparator(inputSlice, caretTokenIndex);
261
+ startIndex = (_k = startToken === null || startToken === void 0 ? void 0 : startToken.start) !== null && _k !== void 0 ? _k : 0;
262
+ stopIndex = (_l = (stopToken === null || stopToken === void 0 ? void 0 : stopToken.stop) + 1) !== null && _l !== void 0 ? _l : input.length;
263
+ /**
264
+ * Save offset of the tokenIndex in the range of input
265
+ * compared to the tokenIndex in the whole input
266
+ */
267
+ tokenIndexOffset = (_m = startToken === null || startToken === void 0 ? void 0 : startToken.tokenIndex) !== null && _m !== void 0 ? _m : 0;
268
+ caretTokenIndex = caretTokenIndex - tokenIndexOffset;
269
+ /**
270
+ * Get the smaller range of inputSlice
271
+ */
272
+ inputSlice = inputSlice.slice(startIndex, stopIndex);
273
+ allTokens = this.getAllTokens(inputSlice);
274
+ }
222
275
  const lexer = this.createLexer(inputSlice);
223
276
  lexer.removeErrorListeners();
224
277
  const tokenStream = new CommonTokenStream(lexer);
@@ -234,7 +287,7 @@ export class BasicSQL {
234
287
  const core = new CodeCompletionCore(sqlParserIns);
235
288
  core.preferredRules = this.preferredRules;
236
289
  const candidates = core.collectCandidates(caretTokenIndex, c3Context);
237
- const originalSuggestions = this.processCandidates(candidates, allTokens, caretTokenIndex, tokenIndexOffset);
290
+ const originalSuggestions = this.processCandidates(candidates, allTokens, caretTokenIndex);
238
291
  const syntaxSuggestions = originalSuggestions.syntax.map((syntaxCtx) => {
239
292
  const wordRanges = syntaxCtx.wordRanges.map((token) => {
240
293
  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.1",
4
4
  "authors": "DTStack Corporation",
5
5
  "description": "SQL Parsers for BigData, built with antlr4",
6
6
  "keywords": [