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

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,8 +40,9 @@ 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
43
44
  */
44
- protected abstract processCandidates(candidates: CandidatesCollection, allTokens: Token[], caretTokenIndex: number): Suggestions<Token>;
45
+ protected abstract processCandidates(candidates: CandidatesCollection, allTokens: Token[], caretTokenIndex: number, tokenIndexOffset: number): Suggestions<Token>;
45
46
  /**
46
47
  * Get a new splitListener instance.
47
48
  */
@@ -110,13 +111,6 @@ export declare abstract class BasicSQL<L extends Lexer = Lexer, PRC extends Pars
110
111
  * @param input source string
111
112
  */
112
113
  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;
120
114
  /**
121
115
  * Get suggestions of syntax and token at caretPosition
122
116
  * @param input source string
@@ -3,7 +3,6 @@ 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 = ';';
7
6
  /**
8
7
  * Basic SQL class, every sql needs extends it.
9
8
  */
@@ -149,58 +148,6 @@ export class BasicSQL {
149
148
  .filter(Boolean);
150
149
  return res;
151
150
  }
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
- }
204
151
  /**
205
152
  * Get suggestions of syntax and token at caretPosition
206
153
  * @param input source string
@@ -210,12 +157,11 @@ export class BasicSQL {
210
157
  getSuggestionAtCaretPosition(input, caretPosition) {
211
158
  var _a, _b, _c, _d, _e, _f, _g, _h, _j;
212
159
  const splitListener = this.splitListener;
213
- let inputSlice = input;
214
- this.parseWithCache(inputSlice);
160
+ this.parseWithCache(input);
215
161
  if (!this._parseTree)
216
162
  return null;
217
163
  let sqlParserIns = this._parser;
218
- let allTokens = this.getAllTokens(inputSlice);
164
+ const allTokens = this.getAllTokens(input);
219
165
  let caretTokenIndex = findCaretTokenIndex(caretPosition, allTokens);
220
166
  let c3Context = this._parseTree;
221
167
  let tokenIndexOffset = 0;
@@ -260,29 +206,19 @@ export class BasicSQL {
260
206
  }
261
207
  }
262
208
  // A boundary consisting of the index of the input.
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;
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;
265
211
  /**
266
212
  * Save offset of the tokenIndex in the range of input
267
213
  * compared to the tokenIndex in the whole input
268
214
  */
269
215
  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;
270
216
  caretTokenIndex = caretTokenIndex - tokenIndexOffset;
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) {
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);
286
222
  const lexer = this.createLexer(inputSlice);
287
223
  lexer.removeErrorListeners();
288
224
  const tokenStream = new CommonTokenStream(lexer);
@@ -298,7 +234,7 @@ export class BasicSQL {
298
234
  const core = new CodeCompletionCore(sqlParserIns);
299
235
  core.preferredRules = this.preferredRules;
300
236
  const candidates = core.collectCandidates(caretTokenIndex, c3Context);
301
- const originalSuggestions = this.processCandidates(candidates, allTokens, caretTokenIndex);
237
+ const originalSuggestions = this.processCandidates(candidates, allTokens, caretTokenIndex, tokenIndexOffset);
302
238
  const syntaxSuggestions = originalSuggestions.syntax.map((syntaxCtx) => {
303
239
  const wordRanges = syntaxCtx.wordRanges.map((token) => {
304
240
  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): Suggestions<Token>;
19
+ protected processCandidates(candidates: CandidatesCollection, allTokens: Token[], caretTokenIndex: number, tokenIndexOffset: 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) {
41
+ processCandidates(candidates, allTokens, caretTokenIndex, tokenIndexOffset) {
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;
47
- const tokenRanges = allTokens.slice(startTokenIndex, caretTokenIndex + 1);
46
+ const startTokenIndex = candidateRule.startTokenIndex + tokenIndexOffset;
47
+ const tokenRanges = allTokens.slice(startTokenIndex, caretTokenIndex + tokenIndexOffset + 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): Suggestions<Token>;
19
+ protected processCandidates(candidates: CandidatesCollection, allTokens: Token[], caretTokenIndex: number, tokenIndexOffset: 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) {
41
+ processCandidates(candidates, allTokens, caretTokenIndex, tokenIndexOffset) {
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;
47
- const tokenRanges = allTokens.slice(startTokenIndex, caretTokenIndex + 1);
46
+ const startTokenIndex = candidateRule.startTokenIndex + tokenIndexOffset;
47
+ const tokenRanges = allTokens.slice(startTokenIndex, caretTokenIndex + tokenIndexOffset + 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): Suggestions<Token>;
19
+ protected processCandidates(candidates: CandidatesCollection, allTokens: Token[], caretTokenIndex: number, tokenIndexOffset: 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) {
40
+ processCandidates(candidates, allTokens, caretTokenIndex, tokenIndexOffset) {
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;
46
- const tokenRanges = allTokens.slice(startTokenIndex, caretTokenIndex + 1);
45
+ const startTokenIndex = candidateRule.startTokenIndex + tokenIndexOffset;
46
+ const tokenRanges = allTokens.slice(startTokenIndex, caretTokenIndex + tokenIndexOffset + 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): Suggestions<Token>;
19
+ protected processCandidates(candidates: CandidatesCollection, allTokens: Token[], caretTokenIndex: number, tokenIndexOffset: 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) {
40
+ processCandidates(candidates, allTokens, caretTokenIndex, tokenIndexOffset) {
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;
46
- const tokenRanges = allTokens.slice(startTokenIndex, caretTokenIndex + 1);
45
+ const startTokenIndex = candidateRule.startTokenIndex + tokenIndexOffset;
46
+ const tokenRanges = allTokens.slice(startTokenIndex, caretTokenIndex + tokenIndexOffset + 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): Suggestions<Token>;
19
+ protected processCandidates(candidates: CandidatesCollection, allTokens: Token[], caretTokenIndex: number, tokenIndexOffset: 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) {
44
+ processCandidates(candidates, allTokens, caretTokenIndex, tokenIndexOffset) {
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;
50
- const tokenRanges = allTokens.slice(startTokenIndex, caretTokenIndex + 1);
49
+ const startTokenIndex = candidateRule.startTokenIndex + tokenIndexOffset;
50
+ const tokenRanges = allTokens.slice(startTokenIndex, caretTokenIndex + tokenIndexOffset + 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): Suggestions<Token>;
19
+ protected processCandidates(candidates: CandidatesCollection, allTokens: Token[], caretTokenIndex: number, tokenIndexOffset: 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) {
40
+ processCandidates(candidates, allTokens, caretTokenIndex, tokenIndexOffset) {
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;
46
- const tokenRanges = allTokens.slice(startTokenIndex, caretTokenIndex + 1);
45
+ const startTokenIndex = candidateRule.startTokenIndex + tokenIndexOffset;
46
+ const tokenRanges = allTokens.slice(startTokenIndex, caretTokenIndex + tokenIndexOffset + 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): Suggestions<Token>;
19
+ protected processCandidates(candidates: CandidatesCollection, allTokens: Token[], caretTokenIndex: number, tokenIndexOffset: 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) {
42
+ processCandidates(candidates, allTokens, caretTokenIndex, tokenIndexOffset) {
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;
48
- const tokenRanges = allTokens.slice(startTokenIndex, caretTokenIndex + 1);
47
+ const startTokenIndex = candidateRule.startTokenIndex + tokenIndexOffset;
48
+ const tokenRanges = allTokens.slice(startTokenIndex, caretTokenIndex + tokenIndexOffset + 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.2",
3
+ "version": "4.1.0-beta.4",
4
4
  "authors": "DTStack Corporation",
5
5
  "description": "SQL Parsers for BigData, built with antlr4",
6
6
  "keywords": [
@@ -34,7 +34,8 @@
34
34
  "format": "prettier --write .",
35
35
  "format-g4": "antlr-format -c ./antlr.format.json -v ./src/grammar/**/*.g4",
36
36
  "cleanComment": "node ./scripts/cleanCommentCli.js",
37
- "benchmark": "node --no-warnings ./scripts/benchmark.js"
37
+ "benchmark": "NODE_OPTIONS=--max_old_space_size=4096 && node --no-warnings ./scripts/benchmark.js",
38
+ "benchmark:release": "node --no-warnings ./scripts/benchmark.js --release"
38
39
  },
39
40
  "license": "MIT",
40
41
  "dependencies": {