dt-sql-parser 4.0.3-beta.0 → 4.0.3-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.
Files changed (74) hide show
  1. package/dist/index.d.ts +1 -1
  2. package/dist/lib/flink/FlinkSqlParser.d.ts +168 -171
  3. package/dist/lib/flink/FlinkSqlParser.js +2341 -2426
  4. package/dist/lib/flink/FlinkSqlParserListener.d.ts +0 -11
  5. package/dist/lib/flink/FlinkSqlParserVisitor.d.ts +0 -7
  6. package/dist/lib/hive/HiveSqlParser.d.ts +5 -1
  7. package/dist/lib/hive/HiveSqlParser.js +145 -130
  8. package/dist/lib/impala/ImpalaSqlParser.d.ts +121 -105
  9. package/dist/lib/impala/ImpalaSqlParser.js +3303 -3225
  10. package/dist/lib/impala/ImpalaSqlParserListener.d.ts +22 -0
  11. package/dist/lib/impala/ImpalaSqlParserVisitor.d.ts +14 -0
  12. package/dist/lib/mysql/MySqlParser.d.ts +4 -0
  13. package/dist/lib/mysql/MySqlParser.js +4 -4
  14. package/dist/lib/postgresql/PostgreSqlParser.d.ts +3 -0
  15. package/dist/lib/postgresql/PostgreSqlParser.js +1500 -1492
  16. package/dist/lib/spark/SparkSqlParser.d.ts +16 -13
  17. package/dist/lib/spark/SparkSqlParser.js +49 -49
  18. package/dist/lib/spark/SparkSqlParserListener.d.ts +10 -10
  19. package/dist/lib/spark/SparkSqlParserVisitor.d.ts +6 -6
  20. package/dist/lib/trino/TrinoSqlParser.d.ts +13 -5
  21. package/dist/lib/trino/TrinoSqlParser.js +23 -23
  22. package/dist/locale/locale.d.ts +41 -0
  23. package/dist/locale/locale.js +43 -0
  24. package/dist/parser/common/basicSQL.d.ts +7 -2
  25. package/dist/parser/common/basicSQL.js +5 -5
  26. package/dist/parser/common/entityCollector.d.ts +58 -11
  27. package/dist/parser/common/entityCollector.js +140 -17
  28. package/dist/parser/common/parseErrorListener.d.ts +6 -3
  29. package/dist/parser/common/parseErrorListener.js +83 -2
  30. package/dist/parser/common/textAndWord.d.ts +6 -9
  31. package/dist/parser/common/textAndWord.js +9 -20
  32. package/dist/parser/common/transform.d.ts +8 -0
  33. package/dist/parser/common/transform.js +11 -0
  34. package/dist/parser/common/types.d.ts +1 -0
  35. package/dist/parser/flink/flinkEntityCollector.js +40 -6
  36. package/dist/parser/flink/flinkErrorListener.d.ts +9 -0
  37. package/dist/parser/flink/flinkErrorListener.js +67 -0
  38. package/dist/parser/flink/index.d.ts +3 -0
  39. package/dist/parser/flink/index.js +4 -0
  40. package/dist/parser/hive/hiveEntityCollector.js +40 -6
  41. package/dist/parser/hive/hiveErrorListener.d.ts +9 -0
  42. package/dist/parser/hive/hiveErrorListener.js +65 -0
  43. package/dist/parser/hive/index.d.ts +3 -0
  44. package/dist/parser/hive/index.js +4 -0
  45. package/dist/parser/impala/ImpalaErrorListener.d.ts +9 -0
  46. package/dist/parser/impala/ImpalaErrorListener.js +63 -0
  47. package/dist/parser/impala/impalaEntityCollector.js +51 -6
  48. package/dist/parser/impala/index.d.ts +3 -0
  49. package/dist/parser/impala/index.js +4 -0
  50. package/dist/parser/mysql/index.d.ts +3 -0
  51. package/dist/parser/mysql/index.js +4 -0
  52. package/dist/parser/mysql/mysqlEntityCollector.d.ts +1 -1
  53. package/dist/parser/mysql/mysqlEntityCollector.js +34 -5
  54. package/dist/parser/mysql/mysqlErrorListener.d.ts +9 -0
  55. package/dist/parser/mysql/mysqlErrorListener.js +63 -0
  56. package/dist/parser/postgresql/index.d.ts +3 -0
  57. package/dist/parser/postgresql/index.js +4 -0
  58. package/dist/parser/postgresql/postgreEntityCollector.d.ts +3 -1
  59. package/dist/parser/postgresql/postgreEntityCollector.js +38 -4
  60. package/dist/parser/postgresql/postgreErrorListener.d.ts +9 -0
  61. package/dist/parser/postgresql/postgreErrorListener.js +71 -0
  62. package/dist/parser/spark/index.d.ts +3 -0
  63. package/dist/parser/spark/index.js +4 -0
  64. package/dist/parser/spark/sparkEntityCollector.d.ts +1 -1
  65. package/dist/parser/spark/sparkEntityCollector.js +43 -6
  66. package/dist/parser/spark/sparkErrorListener.d.ts +9 -0
  67. package/dist/parser/spark/sparkErrorListener.js +63 -0
  68. package/dist/parser/trino/index.d.ts +3 -0
  69. package/dist/parser/trino/index.js +4 -0
  70. package/dist/parser/trino/trinoEntityCollector.d.ts +1 -1
  71. package/dist/parser/trino/trinoEntityCollector.js +34 -5
  72. package/dist/parser/trino/trinoErrorListener.d.ts +9 -0
  73. package/dist/parser/trino/trinoErrorListener.js +65 -0
  74. package/package.json +5 -3
@@ -590,7 +590,7 @@ export class TrinoSqlParser extends SQLParserBase {
590
590
  this.state = 425;
591
591
  this.match(TrinoSqlParser.KW_COMMENT);
592
592
  this.state = 426;
593
- this.string_();
593
+ localContext._comment = this.string_();
594
594
  }
595
595
  }
596
596
  this.state = 431;
@@ -714,7 +714,7 @@ export class TrinoSqlParser extends SQLParserBase {
714
714
  this.state = 470;
715
715
  this.match(TrinoSqlParser.KW_COMMENT);
716
716
  this.state = 471;
717
- this.string_();
717
+ localContext._comment = this.string_();
718
718
  }
719
719
  break;
720
720
  }
@@ -1336,7 +1336,7 @@ export class TrinoSqlParser extends SQLParserBase {
1336
1336
  this.state = 679;
1337
1337
  this.match(TrinoSqlParser.KW_COMMENT);
1338
1338
  this.state = 680;
1339
- this.string_();
1339
+ localContext._comment = this.string_();
1340
1340
  }
1341
1341
  }
1342
1342
  this.state = 685;
@@ -1385,7 +1385,7 @@ export class TrinoSqlParser extends SQLParserBase {
1385
1385
  this.state = 697;
1386
1386
  this.match(TrinoSqlParser.KW_COMMENT);
1387
1387
  this.state = 698;
1388
- this.string_();
1388
+ localContext._comment = this.string_();
1389
1389
  }
1390
1390
  }
1391
1391
  this.state = 703;
@@ -3507,7 +3507,7 @@ export class TrinoSqlParser extends SQLParserBase {
3507
3507
  this.state = 1270;
3508
3508
  this.columnNameCreate();
3509
3509
  this.state = 1271;
3510
- this.type_(0);
3510
+ localContext._colType = this.type_(0);
3511
3511
  this.state = 1274;
3512
3512
  this.errorHandler.sync(this);
3513
3513
  _la = this.tokenStream.LA(1);
@@ -3527,7 +3527,7 @@ export class TrinoSqlParser extends SQLParserBase {
3527
3527
  this.state = 1276;
3528
3528
  this.match(TrinoSqlParser.KW_COMMENT);
3529
3529
  this.state = 1277;
3530
- this.string_();
3530
+ localContext._comment = this.string_();
3531
3531
  }
3532
3532
  break;
3533
3533
  }
@@ -4998,7 +4998,7 @@ export class TrinoSqlParser extends SQLParserBase {
4998
4998
  }
4999
4999
  }
5000
5000
  this.state = 1573;
5001
- this.identifier();
5001
+ localContext._alias = this.identifier();
5002
5002
  }
5003
5003
  break;
5004
5004
  }
@@ -6112,7 +6112,7 @@ export class TrinoSqlParser extends SQLParserBase {
6112
6112
  }
6113
6113
  }
6114
6114
  this.state = 1825;
6115
- this.identifier();
6115
+ localContext._alias = this.identifier();
6116
6116
  this.state = 1827;
6117
6117
  this.errorHandler.sync(this);
6118
6118
  switch (this.interpreter.adaptivePredict(this.tokenStream, 223, this.context)) {
@@ -20391,15 +20391,15 @@ export class CreateTableContext extends StatementContext {
20391
20391
  KW_COMMENT() {
20392
20392
  return this.getToken(TrinoSqlParser.KW_COMMENT, 0);
20393
20393
  }
20394
- string() {
20395
- return this.getRuleContext(0, StringContext);
20396
- }
20397
20394
  KW_WITH() {
20398
20395
  return this.getToken(TrinoSqlParser.KW_WITH, 0);
20399
20396
  }
20400
20397
  properties() {
20401
20398
  return this.getRuleContext(0, PropertiesContext);
20402
20399
  }
20400
+ string() {
20401
+ return this.getRuleContext(0, StringContext);
20402
+ }
20403
20403
  enterRule(listener) {
20404
20404
  if (listener.enterCreateTable) {
20405
20405
  listener.enterCreateTable(this);
@@ -20558,9 +20558,6 @@ export class CreateTableAsSelectContext extends StatementContext {
20558
20558
  KW_COMMENT() {
20559
20559
  return this.getToken(TrinoSqlParser.KW_COMMENT, 0);
20560
20560
  }
20561
- string() {
20562
- return this.getRuleContext(0, StringContext);
20563
- }
20564
20561
  KW_WITH(i) {
20565
20562
  if (i === undefined) {
20566
20563
  return this.getTokens(TrinoSqlParser.KW_WITH);
@@ -20575,6 +20572,9 @@ export class CreateTableAsSelectContext extends StatementContext {
20575
20572
  KW_DATA() {
20576
20573
  return this.getToken(TrinoSqlParser.KW_DATA, 0);
20577
20574
  }
20575
+ string() {
20576
+ return this.getRuleContext(0, StringContext);
20577
+ }
20578
20578
  KW_NO() {
20579
20579
  return this.getToken(TrinoSqlParser.KW_NO, 0);
20580
20580
  }
@@ -21228,15 +21228,15 @@ export class CreateMaterializedViewContext extends StatementContext {
21228
21228
  KW_COMMENT() {
21229
21229
  return this.getToken(TrinoSqlParser.KW_COMMENT, 0);
21230
21230
  }
21231
- string() {
21232
- return this.getRuleContext(0, StringContext);
21233
- }
21234
21231
  KW_WITH() {
21235
21232
  return this.getToken(TrinoSqlParser.KW_WITH, 0);
21236
21233
  }
21237
21234
  properties() {
21238
21235
  return this.getRuleContext(0, PropertiesContext);
21239
21236
  }
21237
+ string() {
21238
+ return this.getRuleContext(0, StringContext);
21239
+ }
21240
21240
  enterRule(listener) {
21241
21241
  if (listener.enterCreateMaterializedView) {
21242
21242
  listener.enterCreateMaterializedView(this);
@@ -21324,9 +21324,6 @@ export class CreateViewContext extends StatementContext {
21324
21324
  KW_COMMENT() {
21325
21325
  return this.getToken(TrinoSqlParser.KW_COMMENT, 0);
21326
21326
  }
21327
- string() {
21328
- return this.getRuleContext(0, StringContext);
21329
- }
21330
21327
  KW_SECURITY() {
21331
21328
  return this.getToken(TrinoSqlParser.KW_SECURITY, 0);
21332
21329
  }
@@ -21336,6 +21333,9 @@ export class CreateViewContext extends StatementContext {
21336
21333
  properties() {
21337
21334
  return this.getRuleContext(0, PropertiesContext);
21338
21335
  }
21336
+ string() {
21337
+ return this.getRuleContext(0, StringContext);
21338
+ }
21339
21339
  KW_DEFINER() {
21340
21340
  return this.getToken(TrinoSqlParser.KW_DEFINER, 0);
21341
21341
  }
@@ -22302,15 +22302,15 @@ export class ColumnDefinitionContext extends antlr.ParserRuleContext {
22302
22302
  KW_COMMENT() {
22303
22303
  return this.getToken(TrinoSqlParser.KW_COMMENT, 0);
22304
22304
  }
22305
- string() {
22306
- return this.getRuleContext(0, StringContext);
22307
- }
22308
22305
  KW_WITH() {
22309
22306
  return this.getToken(TrinoSqlParser.KW_WITH, 0);
22310
22307
  }
22311
22308
  properties() {
22312
22309
  return this.getRuleContext(0, PropertiesContext);
22313
22310
  }
22311
+ string() {
22312
+ return this.getRuleContext(0, StringContext);
22313
+ }
22314
22314
  get ruleIndex() {
22315
22315
  return TrinoSqlParser.RULE_columnDefinition;
22316
22316
  }
@@ -0,0 +1,41 @@
1
+ declare const i18n: {
2
+ zh_CN: {
3
+ stmtInComplete: string;
4
+ noValidPosition: string;
5
+ expecting: string;
6
+ unfinishedMultilineComment: string;
7
+ unfinishedDoubleQuoted: string;
8
+ unfinishedSingleQuoted: string;
9
+ unfinishedTickQuoted: string;
10
+ noValidInput: string;
11
+ newObj: string;
12
+ existingObj: string;
13
+ new: string;
14
+ existing: string;
15
+ orKeyword: string;
16
+ keyword: string;
17
+ missing: string;
18
+ at: string;
19
+ or: string;
20
+ };
21
+ en_US: {
22
+ stmtInComplete: string;
23
+ noValidPosition: string;
24
+ expecting: string;
25
+ unfinishedMultilineComment: string;
26
+ unfinishedDoubleQuoted: string;
27
+ unfinishedSingleQuoted: string;
28
+ unfinishedTickQuoted: string;
29
+ noValidInput: string;
30
+ newObj: string;
31
+ existingObj: string;
32
+ new: string;
33
+ existing: string;
34
+ orKeyword: string;
35
+ keyword: string;
36
+ missing: string;
37
+ at: string;
38
+ or: string;
39
+ };
40
+ };
41
+ export { i18n };
@@ -0,0 +1,43 @@
1
+ const zh_CN = {
2
+ stmtInComplete: '语句不完整',
3
+ noValidPosition: '在此位置无效',
4
+ expecting: ',期望',
5
+ unfinishedMultilineComment: '未完成的多行注释',
6
+ unfinishedDoubleQuoted: '未完成的双引号字符串字变量',
7
+ unfinishedSingleQuoted: '未完成的单引号字符串字变量',
8
+ unfinishedTickQuoted: '未完成的反引号引用字符串字变量',
9
+ noValidInput: '没有有效的输入',
10
+ newObj: '一个新的对象',
11
+ existingObj: '一个存在的对象',
12
+ new: '一个新的',
13
+ existing: '一个存在的',
14
+ orKeyword: '或者一个关键字',
15
+ keyword: '一个关键字',
16
+ missing: '缺少',
17
+ at: '在',
18
+ or: '或者',
19
+ };
20
+ const en_US = {
21
+ stmtInComplete: 'Statement is incomplete',
22
+ noValidPosition: 'is not valid at this position',
23
+ expecting: ', expecting ',
24
+ unfinishedMultilineComment: 'Unfinished multiline comment',
25
+ unfinishedDoubleQuoted: 'Unfinished double quoted string literal',
26
+ unfinishedSingleQuoted: 'Unfinished single quoted string literal',
27
+ unfinishedTickQuoted: 'Unfinished back tick quoted string literal',
28
+ noValidInput: 'is no valid input at all',
29
+ newObj: 'a new object',
30
+ existingObj: 'an existing object',
31
+ new: 'a new ',
32
+ existing: 'an existing ',
33
+ orKeyword: ' or a keyword',
34
+ keyword: 'a keyword',
35
+ missing: 'missing ',
36
+ at: ' at ',
37
+ or: ' or ',
38
+ };
39
+ const i18n = {
40
+ zh_CN,
41
+ en_US,
42
+ };
43
+ export { i18n };
@@ -1,8 +1,8 @@
1
- import { Lexer, Token, CommonTokenStream, CharStream, ParserRuleContext, ParseTreeListener } from 'antlr4ng';
1
+ import { Lexer, Token, CommonTokenStream, CharStream, ParserRuleContext, ParseTreeListener, ANTLRErrorListener } from 'antlr4ng';
2
2
  import { CandidatesCollection } from 'antlr4-c3';
3
3
  import { SQLParserBase } from '../../lib/SQLParserBase';
4
4
  import { TextSlice } from './textAndWord';
5
- import { CaretPosition, Suggestions } from './types';
5
+ import { CaretPosition, LOCALE_TYPE, Suggestions } from './types';
6
6
  import { ParseError, ErrorListener } from './parseErrorListener';
7
7
  import type { SplitListener } from './splitListener';
8
8
  import type { EntityCollector } from './entityCollector';
@@ -47,10 +47,15 @@ export declare abstract class BasicSQL<L extends Lexer = Lexer, PRC extends Pars
47
47
  * Get a new splitListener instance.
48
48
  */
49
49
  protected abstract get splitListener(): SplitListener<ParserRuleContext>;
50
+ /**
51
+ * Get a new errorListener instance.
52
+ */
53
+ protected abstract createErrorListener(errorListener: ErrorListener): ANTLRErrorListener;
50
54
  /**
51
55
  * Get a new entityCollector instance.
52
56
  */
53
57
  protected abstract createEntityCollector(input: string, caretTokenIndex?: number): EntityCollector;
58
+ locale: LOCALE_TYPE;
54
59
  /**
55
60
  * Create an antlr4 lexer from input.
56
61
  * @param input string
@@ -2,7 +2,6 @@ import { CharStreams, CommonTokenStream, ParseTreeWalker, PredictionMode, } from
2
2
  import { CodeCompletionCore } from 'antlr4-c3';
3
3
  import { findCaretTokenIndex } from './findCaretTokenIndex';
4
4
  import { ctxToText, tokenToWord } from './textAndWord';
5
- import { ParseErrorListener } from './parseErrorListener';
6
5
  import { ErrorStrategy } from './errorStrategy';
7
6
  /**
8
7
  * Basic SQL class, every sql needs extends it.
@@ -14,6 +13,7 @@ export class BasicSQL {
14
13
  this._errorListener = (error) => {
15
14
  this._parseErrors.push(error);
16
15
  };
16
+ this.locale = 'en_US';
17
17
  }
18
18
  /**
19
19
  * Create an antlr4 lexer from input.
@@ -24,7 +24,7 @@ export class BasicSQL {
24
24
  const lexer = this.createLexerFromCharStream(charStreams);
25
25
  if (errorListener) {
26
26
  lexer.removeErrorListeners();
27
- lexer.addErrorListener(new ParseErrorListener(errorListener));
27
+ lexer.addErrorListener(this.createErrorListener(errorListener));
28
28
  }
29
29
  return lexer;
30
30
  }
@@ -39,7 +39,7 @@ export class BasicSQL {
39
39
  parser.interpreter.predictionMode = PredictionMode.SLL;
40
40
  if (errorListener) {
41
41
  parser.removeErrorListeners();
42
- parser.addErrorListener(new ParseErrorListener(errorListener));
42
+ parser.addErrorListener(this.createErrorListener(errorListener));
43
43
  }
44
44
  return parser;
45
45
  }
@@ -65,7 +65,7 @@ export class BasicSQL {
65
65
  this._charStreams = CharStreams.fromString(input);
66
66
  this._lexer = this.createLexerFromCharStream(this._charStreams);
67
67
  this._lexer.removeErrorListeners();
68
- this._lexer.addErrorListener(new ParseErrorListener(this._errorListener));
68
+ this._lexer.addErrorListener(this.createErrorListener(this._errorListener));
69
69
  this._tokenStream = new CommonTokenStream(this._lexer);
70
70
  /**
71
71
  * All tokens are generated in advance.
@@ -96,7 +96,7 @@ export class BasicSQL {
96
96
  const parser = this.createParserWithCache(input);
97
97
  this._parsedInput = input;
98
98
  parser.removeErrorListeners();
99
- parser.addErrorListener(new ParseErrorListener(this._errorListener));
99
+ parser.addErrorListener(this.createErrorListener(this._errorListener));
100
100
  this._parseTree = parser.program();
101
101
  return this._parseTree;
102
102
  }
@@ -1,6 +1,6 @@
1
- import { ParserRuleContext } from 'antlr4ng';
1
+ import { ParserRuleContext, Token } from 'antlr4ng';
2
+ import { TextPosition, WordPosition, WordRange } from './textAndWord';
2
3
  import { EntityContextType } from './types';
3
- import { WordPosition, TextPosition } from './textAndWord';
4
4
  /**
5
5
  * TODO: more stmt type should be supported.
6
6
  */
@@ -24,20 +24,65 @@ export interface StmtContext {
24
24
  readonly isContainCaret?: boolean;
25
25
  }
26
26
  export declare function toStmtContext(ctx: ParserRuleContext, type: StmtContextType, input: string, rootStmt: StmtContext | null, parentStmt: StmtContext | null, isContainCaret?: boolean): StmtContext | null;
27
- export interface BaseAliasContext {
28
- readonly isAlias: boolean;
29
- alias?: string | EntityContext | null;
30
- origin?: string | EntityContext | StmtContext | null;
27
+ /**
28
+ * entity's attribute
29
+ * @key comment: entity's comment attribute
30
+ * @key colType: column entity's type attribute
31
+ * @key alias: entity's alias attribute
32
+ * */
33
+ export declare enum AttrName {
34
+ comment = "_comment",
35
+ colType = "_colType",
36
+ alias = "_alias"
31
37
  }
32
- export interface EntityContext extends BaseAliasContext {
38
+ /**
39
+ * ParserRuleContext with custom attributes
40
+ * */
41
+ type ParserRuleContextWithAttr = ParserRuleContext & {
42
+ [K in AttrName]?: Token;
43
+ };
44
+ /**
45
+ * Function's arguments
46
+ * */
47
+ export interface Argument {
48
+ argMode?: WordRange;
49
+ argName?: WordRange;
50
+ argType: WordRange;
51
+ }
52
+ export interface BaseEntityContext {
33
53
  readonly entityContextType: EntityContextType;
34
54
  readonly text: string;
35
55
  readonly position: WordPosition;
36
56
  readonly belongStmt: StmtContext;
37
- relatedEntities: EntityContext[] | null;
38
- columns: EntityContext[] | null;
57
+ reference?: string | EntityContext;
58
+ [AttrName.comment]: WordRange | null;
59
+ [AttrName.alias]?: WordRange | null;
60
+ }
61
+ export interface CommonEntityContext extends BaseEntityContext {
62
+ relatedEntities: CommonEntityContext[] | null;
63
+ columns?: ColumnEntityContext[];
64
+ }
65
+ export interface ColumnEntityContext extends BaseEntityContext {
66
+ [AttrName.colType]: WordRange | null;
67
+ }
68
+ export interface FuncEntityContext extends BaseEntityContext {
69
+ relatedEntities: CommonEntityContext[] | null;
70
+ arguments: Argument[] | null;
71
+ returns?: Argument;
72
+ }
73
+ export type EntityContext = CommonEntityContext | FuncEntityContext | ColumnEntityContext;
74
+ export declare function isCommonEntityContext(entity: EntityContext): entity is CommonEntityContext;
75
+ export declare function isFuncEntityContext(entity: EntityContext): entity is FuncEntityContext;
76
+ export declare function isColumnEntityContext(entity: EntityContext): entity is ColumnEntityContext;
77
+ /**
78
+ * what we need when collect attribute information
79
+ * */
80
+ interface AttrInfo {
81
+ attrName: AttrName;
82
+ endContextList: string[];
39
83
  }
40
- export declare function toEntityContext(ctx: ParserRuleContext, type: EntityContextType, input: string, belongStmt: StmtContext, alias?: BaseAliasContext): EntityContext | null;
84
+ export declare function toEntityContext(ctx: ParserRuleContext, type: EntityContextType, input: string, belongStmt: StmtContext, attrInfo?: AttrInfo[]): EntityContext | null;
85
+ export declare function findAttribute(ctx: ParserRuleContextWithAttr | null, keyName: AttrName, endContextNameList: string[]): Token | null;
41
86
  /**
42
87
  * @todo: Handle alias, includes column alias, table alias, query as alias and so on.
43
88
  * @todo: [may be need] Combine the entities in each clause.
@@ -60,11 +105,12 @@ export declare abstract class EntityCollector {
60
105
  visitErrorNode(): void;
61
106
  enterEveryRule(): void;
62
107
  exitEveryRule(): void;
108
+ getRootStmt(): StmtContext | null;
63
109
  getEntities(): EntityContext[];
64
110
  enterProgram(): void;
65
111
  protected pushStmt(ctx: ParserRuleContext, type: StmtContextType): StmtContext | null;
66
112
  protected popStmt(): StmtContext;
67
- protected pushEntity(ctx: ParserRuleContext, type: EntityContextType, alias?: BaseAliasContext): EntityContext | null;
113
+ protected pushEntity(ctx: ParserRuleContext, type: EntityContextType, attrInfo?: AttrInfo[]): EntityContext | null;
68
114
  /**
69
115
  * Combine entities that inside a single statement.
70
116
  * e.g. combine tableName and column if they are inside a same createTableStatement.
@@ -77,3 +123,4 @@ export declare abstract class EntityCollector {
77
123
  protected combineRootStmtEntities(stmtContext: StmtContext, entitiesInsideStmt: EntityContext[]): EntityContext[];
78
124
  protected combineCreateTableOrViewStmtEntities(stmtContext: StmtContext, entitiesInsideStmt: EntityContext[]): EntityContext[];
79
125
  }
126
+ export {};
@@ -9,9 +9,10 @@ var __rest = (this && this.__rest) || function (s, e) {
9
9
  }
10
10
  return t;
11
11
  };
12
- import { EntityContextType } from './types';
13
- import { ctxToText, ctxToWord } from './textAndWord';
12
+ import { isToken } from 'antlr4ng';
14
13
  import { SimpleStack } from './simpleStack';
14
+ import { ctxToText, isWordRange, tokenToWord, } from './textAndWord';
15
+ import { EntityContextType } from './types';
15
16
  /**
16
17
  * TODO: more stmt type should be supported.
17
18
  */
@@ -41,20 +42,131 @@ export function toStmtContext(ctx, type, input, rootStmt, parentStmt, isContainC
41
42
  isContainCaret,
42
43
  };
43
44
  }
44
- const baseAlias = {
45
- isAlias: false,
46
- origin: null,
47
- alias: null,
48
- };
49
- export function toEntityContext(ctx, type, input, belongStmt, alias) {
50
- const word = ctxToWord(ctx, input);
45
+ /**
46
+ * entity's attribute
47
+ * @key comment: entity's comment attribute
48
+ * @key colType: column entity's type attribute
49
+ * @key alias: entity's alias attribute
50
+ * */
51
+ export var AttrName;
52
+ (function (AttrName) {
53
+ AttrName["comment"] = "_comment";
54
+ AttrName["colType"] = "_colType";
55
+ AttrName["alias"] = "_alias";
56
+ })(AttrName || (AttrName = {}));
57
+ export function isCommonEntityContext(entity) {
58
+ if (!entity)
59
+ return false;
60
+ return 'relatedEntities' in entity && !('arguments' in entity);
61
+ }
62
+ export function isFuncEntityContext(entity) {
63
+ if (!entity)
64
+ return false;
65
+ return 'arguments' in entity;
66
+ }
67
+ export function isColumnEntityContext(entity) {
68
+ if (!entity)
69
+ return false;
70
+ return AttrName.colType in entity;
71
+ }
72
+ export function toEntityContext(ctx, type, input, belongStmt, attrInfo) {
73
+ var _a, _b;
74
+ const word = ctxToText(ctx, input);
51
75
  if (!word)
52
76
  return null;
53
- const { text } = word, position = __rest(word, ["text"]);
54
- const finalAlias = Object.assign({}, baseAlias, alias !== null && alias !== void 0 ? alias : {});
55
- return Object.assign({ entityContextType: type, text,
77
+ const { text, startLine, endLine } = word, rest = __rest(word, ["text", "startLine", "endLine"]);
78
+ const position = Object.assign(Object.assign({}, rest), { line: startLine });
79
+ let entityInfo = {
80
+ entityContextType: type,
81
+ text,
56
82
  position,
57
- belongStmt, relatedEntities: null, columns: null }, finalAlias);
83
+ belongStmt,
84
+ [AttrName.comment]: null,
85
+ };
86
+ switch (entityInfo.entityContextType) {
87
+ case EntityContextType.FUNCTION:
88
+ case EntityContextType.FUNCTION_CREATE: {
89
+ entityInfo.relatedEntities = null;
90
+ entityInfo.arguments = null;
91
+ break;
92
+ }
93
+ case EntityContextType.COLUMN:
94
+ case EntityContextType.COLUMN_CREATE:
95
+ entityInfo[AttrName.colType] = null;
96
+ break;
97
+ default:
98
+ entityInfo.relatedEntities = null;
99
+ break;
100
+ }
101
+ if (attrInfo) {
102
+ for (let k = 0; k < attrInfo.length; k++) {
103
+ const attributeName = (_a = attrInfo[k]) === null || _a === void 0 ? void 0 : _a.attrName;
104
+ const attrToken = findAttribute(ctx, attributeName, (_b = attrInfo[k]) === null || _b === void 0 ? void 0 : _b.endContextList);
105
+ if (attrToken) {
106
+ const attrVal = isToken(attrToken)
107
+ ? tokenToWord(attrToken, input)
108
+ : ctxToText(attrToken, input);
109
+ if (attrVal) {
110
+ if (isWordRange(attrVal)) {
111
+ entityInfo = Object.assign(entityInfo, {
112
+ [attributeName]: attrVal,
113
+ });
114
+ }
115
+ else {
116
+ const { startIndex, endIndex, startColumn, endColumn, text } = attrVal, rest = __rest(attrVal, ["startIndex", "endIndex", "startColumn", "endColumn", "text"]);
117
+ entityInfo = Object.assign(entityInfo, {
118
+ [attributeName]: {
119
+ startIndex,
120
+ endIndex,
121
+ startColumn,
122
+ endColumn,
123
+ text,
124
+ line: rest === null || rest === void 0 ? void 0 : rest.startLine,
125
+ },
126
+ });
127
+ }
128
+ }
129
+ }
130
+ }
131
+ }
132
+ return entityInfo;
133
+ }
134
+ export function findAttribute(ctx, keyName, endContextNameList) {
135
+ var _a, _b;
136
+ const parent = (ctx === null || ctx === void 0 ? void 0 : ctx.parent) || null;
137
+ let attrVal = null;
138
+ if (parent === null || parent === void 0 ? void 0 : parent[keyName]) {
139
+ attrVal = (parent === null || parent === void 0 ? void 0 : parent[keyName]) || null;
140
+ return attrVal;
141
+ }
142
+ else {
143
+ if (((_a = parent === null || parent === void 0 ? void 0 : parent.constructor) === null || _a === void 0 ? void 0 : _a.name) && !endContextNameList.includes((_b = parent === null || parent === void 0 ? void 0 : parent.constructor) === null || _b === void 0 ? void 0 : _b.name)) {
144
+ attrVal = findAttribute(parent, keyName, endContextNameList);
145
+ }
146
+ if (!attrVal) {
147
+ if (parent === null || parent === void 0 ? void 0 : parent.children) {
148
+ attrVal = findAttributeChildren(parent, keyName);
149
+ }
150
+ }
151
+ }
152
+ return attrVal;
153
+ }
154
+ function findAttributeChildren(ctx, keyName) {
155
+ const visitChildren = (ctx === null || ctx === void 0 ? void 0 : ctx.children) || [];
156
+ let attrVal = null;
157
+ if (visitChildren.length) {
158
+ for (let i = 0; i < visitChildren.length; i++) {
159
+ const child = visitChildren[i] || null;
160
+ if (child === null || child === void 0 ? void 0 : child[keyName]) {
161
+ attrVal = (child === null || child === void 0 ? void 0 : child[keyName]) || null;
162
+ return attrVal;
163
+ }
164
+ else {
165
+ attrVal = findAttributeChildren(child, keyName);
166
+ }
167
+ }
168
+ }
169
+ return attrVal;
58
170
  }
59
171
  /**
60
172
  * @todo: Handle alias, includes column alias, table alias, query as alias and so on.
@@ -73,6 +185,9 @@ export class EntityCollector {
73
185
  visitErrorNode() { }
74
186
  enterEveryRule() { }
75
187
  exitEveryRule() { }
188
+ getRootStmt() {
189
+ return this._rootStmt;
190
+ }
76
191
  getEntities() {
77
192
  return Array.from(this._entitiesSet);
78
193
  }
@@ -112,8 +227,8 @@ export class EntityCollector {
112
227
  }
113
228
  return stmtContext;
114
229
  }
115
- pushEntity(ctx, type, alias) {
116
- const entityContext = toEntityContext(ctx, type, this._input, this._stmtStack.peek(), alias);
230
+ pushEntity(ctx, type, attrInfo) {
231
+ const entityContext = toEntityContext(ctx, type, this._input, this._stmtStack.peek(), attrInfo);
117
232
  if (entityContext) {
118
233
  if (this._stmtStack.isEmpty()) {
119
234
  this._entitiesSet.add(entityContext);
@@ -182,10 +297,18 @@ export class EntityCollector {
182
297
  return result;
183
298
  }, []);
184
299
  if (mainEntity && columns.length) {
185
- mainEntity.columns = columns;
300
+ if (isCommonEntityContext(mainEntity)) {
301
+ mainEntity = Object.assign(mainEntity, {
302
+ columns,
303
+ });
304
+ }
186
305
  }
187
306
  if (mainEntity && relatedEntities.length) {
188
- mainEntity.relatedEntities = relatedEntities;
307
+ if (isCommonEntityContext(mainEntity) || isFuncEntityContext(mainEntity)) {
308
+ mainEntity = Object.assign(mainEntity, {
309
+ relatedEntities,
310
+ });
311
+ }
189
312
  }
190
313
  return finalEntities;
191
314
  }
@@ -1,4 +1,5 @@
1
- import { Token, Recognizer, ANTLRErrorListener, RecognitionException, ATNSimulator } from 'antlr4ng';
1
+ import { Token, Recognizer, ANTLRErrorListener, RecognitionException, ATNSimulator, Parser } from 'antlr4ng';
2
+ import { LOCALE_TYPE } from './types';
2
3
  /**
3
4
  * Converted from {@link SyntaxError}.
4
5
  */
@@ -29,11 +30,13 @@ export interface SyntaxError {
29
30
  * Includes lexical errors and parsing errors.
30
31
  */
31
32
  export type ErrorListener = (parseError: ParseError, originalError: SyntaxError) => void;
32
- export declare class ParseErrorListener implements ANTLRErrorListener {
33
+ export declare abstract class ParseErrorListener implements ANTLRErrorListener {
33
34
  private _errorListener;
34
- constructor(errorListener: ErrorListener);
35
+ private locale;
36
+ constructor(errorListener: ErrorListener, locale?: LOCALE_TYPE);
35
37
  reportAmbiguity(): void;
36
38
  reportAttemptingFullContext(): void;
37
39
  reportContextSensitivity(): void;
40
+ protected abstract getExpectedText(parser: Parser, token: Token): string;
38
41
  syntaxError(recognizer: Recognizer<ATNSimulator>, offendingSymbol: Token | null, line: number, charPositionInLine: number, msg: string, e: RecognitionException): void;
39
42
  }