rawsql-ts 0.1.0-beta.3 → 0.1.0-beta.5

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 (135) hide show
  1. package/README.md +143 -199
  2. package/dist/index.js +32 -0
  3. package/dist/index.js.map +1 -0
  4. package/dist/models/BinarySelectQuery.js +140 -0
  5. package/dist/models/BinarySelectQuery.js.map +1 -0
  6. package/dist/models/Clause.js +318 -0
  7. package/dist/models/Clause.js.map +1 -0
  8. package/dist/models/KeywordTrie.js +52 -0
  9. package/dist/models/KeywordTrie.js.map +1 -0
  10. package/dist/models/Lexeme.js +21 -0
  11. package/dist/models/Lexeme.js.map +1 -0
  12. package/dist/models/SelectQuery.js +10 -0
  13. package/dist/models/SelectQuery.js.map +1 -0
  14. package/dist/models/SimpleSelectQuery.js +290 -0
  15. package/dist/models/SimpleSelectQuery.js.map +1 -0
  16. package/dist/models/SqlComponent.js +27 -0
  17. package/dist/models/SqlComponent.js.map +1 -0
  18. package/dist/models/ValueComponent.js +250 -0
  19. package/dist/models/ValueComponent.js.map +1 -0
  20. package/dist/models/ValuesQuery.js +16 -0
  21. package/dist/models/ValuesQuery.js.map +1 -0
  22. package/dist/parsers/CommandExpressionParser.js +124 -0
  23. package/dist/parsers/CommandExpressionParser.js.map +1 -0
  24. package/dist/parsers/CommonTableParser.js +60 -0
  25. package/dist/parsers/CommonTableParser.js.map +1 -0
  26. package/dist/parsers/ForClauseParser.js +56 -0
  27. package/dist/parsers/ForClauseParser.js.map +1 -0
  28. package/dist/parsers/FromClauseParser.js +45 -0
  29. package/dist/parsers/FromClauseParser.js.map +1 -0
  30. package/dist/parsers/FunctionExpressionParser.js +178 -0
  31. package/dist/parsers/FunctionExpressionParser.js.map +1 -0
  32. package/dist/parsers/GroupByParser.js +56 -0
  33. package/dist/parsers/GroupByParser.js.map +1 -0
  34. package/dist/parsers/HavingParser.js +34 -0
  35. package/dist/parsers/HavingParser.js.map +1 -0
  36. package/dist/parsers/IdentifierParser.js +39 -0
  37. package/dist/parsers/IdentifierParser.js.map +1 -0
  38. package/dist/parsers/JoinClauseParser.js +105 -0
  39. package/dist/parsers/JoinClauseParser.js.map +1 -0
  40. package/dist/parsers/KeywordParser.js +91 -0
  41. package/dist/parsers/KeywordParser.js.map +1 -0
  42. package/dist/parsers/LimitClauseParser.js +48 -0
  43. package/dist/parsers/LimitClauseParser.js.map +1 -0
  44. package/dist/parsers/LiteralParser.js +38 -0
  45. package/dist/parsers/LiteralParser.js.map +1 -0
  46. package/dist/parsers/OrderByClauseParser.js +75 -0
  47. package/dist/parsers/OrderByClauseParser.js.map +1 -0
  48. package/dist/parsers/OverExpressionParser.js +44 -0
  49. package/dist/parsers/OverExpressionParser.js.map +1 -0
  50. package/dist/parsers/ParameterExpressionParser.js +15 -0
  51. package/dist/parsers/ParameterExpressionParser.js.map +1 -0
  52. package/dist/parsers/ParenExpressionParser.js +33 -0
  53. package/dist/parsers/ParenExpressionParser.js.map +1 -0
  54. package/dist/parsers/PartitionByParser.js +51 -0
  55. package/dist/parsers/PartitionByParser.js.map +1 -0
  56. package/dist/parsers/SelectClauseParser.js +82 -0
  57. package/dist/parsers/SelectClauseParser.js.map +1 -0
  58. package/dist/parsers/SelectQueryParser.js +151 -0
  59. package/dist/parsers/SelectQueryParser.js.map +1 -0
  60. package/dist/parsers/SourceAliasExpressionParser.js +48 -0
  61. package/dist/parsers/SourceAliasExpressionParser.js.map +1 -0
  62. package/dist/parsers/SourceExpressionParser.js +34 -0
  63. package/dist/parsers/SourceExpressionParser.js.map +1 -0
  64. package/dist/parsers/SourceParser.js +116 -0
  65. package/dist/parsers/SourceParser.js.map +1 -0
  66. package/dist/parsers/SqlTokenizer.js +174 -0
  67. package/dist/parsers/SqlTokenizer.js.map +1 -0
  68. package/dist/parsers/StringSpecifierExpressionParser.js +22 -0
  69. package/dist/parsers/StringSpecifierExpressionParser.js.map +1 -0
  70. package/dist/parsers/UnaryExpressionParser.js +30 -0
  71. package/dist/parsers/UnaryExpressionParser.js.map +1 -0
  72. package/dist/parsers/ValueParser.js +134 -0
  73. package/dist/parsers/ValueParser.js.map +1 -0
  74. package/dist/parsers/ValuesQueryParser.js +86 -0
  75. package/dist/parsers/ValuesQueryParser.js.map +1 -0
  76. package/dist/parsers/WhereClauseParser.js +34 -0
  77. package/dist/parsers/WhereClauseParser.js.map +1 -0
  78. package/dist/parsers/WindowClauseParser.js +43 -0
  79. package/dist/parsers/WindowClauseParser.js.map +1 -0
  80. package/dist/parsers/WindowExpressionParser.js +151 -0
  81. package/dist/parsers/WindowExpressionParser.js.map +1 -0
  82. package/dist/parsers/WithClauseParser.js +55 -0
  83. package/dist/parsers/WithClauseParser.js.map +1 -0
  84. package/dist/tokenReaders/BaseTokenReader.js +82 -0
  85. package/dist/tokenReaders/BaseTokenReader.js.map +1 -0
  86. package/dist/tokenReaders/CommandTokenReader.js +145 -0
  87. package/dist/tokenReaders/CommandTokenReader.js.map +1 -0
  88. package/dist/tokenReaders/FunctionTokenReader.js +45 -0
  89. package/dist/tokenReaders/FunctionTokenReader.js.map +1 -0
  90. package/dist/tokenReaders/IdentifierTokenReader.js +70 -0
  91. package/dist/tokenReaders/IdentifierTokenReader.js.map +1 -0
  92. package/dist/tokenReaders/LiteralTokenReader.js +189 -0
  93. package/dist/tokenReaders/LiteralTokenReader.js.map +1 -0
  94. package/dist/tokenReaders/OperatorTokenReader.js +98 -0
  95. package/dist/tokenReaders/OperatorTokenReader.js.map +1 -0
  96. package/dist/tokenReaders/ParameterTokenReader.js +44 -0
  97. package/dist/tokenReaders/ParameterTokenReader.js.map +1 -0
  98. package/dist/tokenReaders/StringSpecifierTokenReader.js +31 -0
  99. package/dist/tokenReaders/StringSpecifierTokenReader.js.map +1 -0
  100. package/dist/tokenReaders/SymbolTokenReader.js +35 -0
  101. package/dist/tokenReaders/SymbolTokenReader.js.map +1 -0
  102. package/dist/tokenReaders/TokenReaderManager.js +110 -0
  103. package/dist/tokenReaders/TokenReaderManager.js.map +1 -0
  104. package/dist/tokenReaders/TypeTokenReader.js +59 -0
  105. package/dist/tokenReaders/TypeTokenReader.js.map +1 -0
  106. package/dist/transformers/CTEBuilder.js +188 -0
  107. package/dist/transformers/CTEBuilder.js.map +1 -0
  108. package/dist/transformers/CTECollector.js +384 -0
  109. package/dist/transformers/CTECollector.js.map +1 -0
  110. package/dist/transformers/CTEDisabler.js +325 -0
  111. package/dist/transformers/CTEDisabler.js.map +1 -0
  112. package/dist/transformers/CTEInjector.js +83 -0
  113. package/dist/transformers/CTEInjector.js.map +1 -0
  114. package/dist/transformers/CTENormalizer.js +42 -0
  115. package/dist/transformers/CTENormalizer.js.map +1 -0
  116. package/dist/transformers/Formatter.js +452 -0
  117. package/dist/transformers/Formatter.js.map +1 -0
  118. package/dist/transformers/QueryNormalizer.js +114 -0
  119. package/dist/transformers/QueryNormalizer.js.map +1 -0
  120. package/dist/transformers/SelectValueCollector.js +249 -0
  121. package/dist/transformers/SelectValueCollector.js.map +1 -0
  122. package/dist/transformers/SelectableColumnCollector.js +308 -0
  123. package/dist/transformers/SelectableColumnCollector.js.map +1 -0
  124. package/dist/transformers/TableSourceCollector.js +384 -0
  125. package/dist/transformers/TableSourceCollector.js.map +1 -0
  126. package/dist/transformers/UpstreamSelectQueryFinder.js +129 -0
  127. package/dist/transformers/UpstreamSelectQueryFinder.js.map +1 -0
  128. package/dist/utils/charLookupTable.js +73 -0
  129. package/dist/utils/charLookupTable.js.map +1 -0
  130. package/dist/utils/stringUtils.js +168 -0
  131. package/dist/utils/stringUtils.js.map +1 -0
  132. package/package.json +2 -2
  133. package/dist/tsconfig.tsbuildinfo +0 -1
  134. package/dist/vitest.config.js +0 -15
  135. package/dist/vitest.config.js.map +0 -1
@@ -0,0 +1,82 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SelectClauseParser = void 0;
4
+ const Clause_1 = require("../models/Clause");
5
+ const Lexeme_1 = require("../models/Lexeme");
6
+ const SqlTokenizer_1 = require("./SqlTokenizer");
7
+ const ValueParser_1 = require("./ValueParser");
8
+ class SelectClauseParser {
9
+ static parseFromText(query) {
10
+ const tokenizer = new SqlTokenizer_1.SqlTokenizer(query); // Initialize tokenizer
11
+ const lexemes = tokenizer.readLexmes(); // Get tokens
12
+ // Parse
13
+ const result = this.parse(lexemes, 0);
14
+ // Error if there are remaining tokens
15
+ if (result.newIndex < lexemes.length) {
16
+ throw new Error(`Syntax error: Unexpected token "${lexemes[result.newIndex].value}" at position ${result.newIndex}. The SELECT clause is complete but there are additional tokens.`);
17
+ }
18
+ return result.value;
19
+ }
20
+ static parse(lexemes, index) {
21
+ let idx = index;
22
+ let distinct = null;
23
+ if (lexemes[idx].value !== 'select') {
24
+ throw new Error(`Syntax error at position ${idx}: Expected 'SELECT' keyword but found "${lexemes[idx].value}". SELECT clauses must start with the SELECT keyword.`);
25
+ }
26
+ idx++;
27
+ if (idx < lexemes.length && lexemes[idx].value === 'distinct') {
28
+ idx++;
29
+ distinct = new Clause_1.Distinct();
30
+ }
31
+ else if (idx < lexemes.length && lexemes[idx].value === 'distinct on') {
32
+ idx++;
33
+ const argument = ValueParser_1.ValueParser.parseArgument(Lexeme_1.TokenType.OpenParen, Lexeme_1.TokenType.CloseParen, lexemes, idx);
34
+ distinct = new Clause_1.DistinctOn(argument.value);
35
+ idx = argument.newIndex;
36
+ }
37
+ const items = [];
38
+ const item = this.parseItem(lexemes, idx);
39
+ items.push(item.value);
40
+ idx = item.newIndex;
41
+ while (idx < lexemes.length && lexemes[idx].type === Lexeme_1.TokenType.Comma) {
42
+ idx++;
43
+ const item = this.parseItem(lexemes, idx);
44
+ items.push(item.value);
45
+ idx = item.newIndex;
46
+ }
47
+ if (items.length === 0) {
48
+ throw new Error(`Syntax error at position ${index}: No select items found. The SELECT clause requires at least one expression to select.`);
49
+ }
50
+ else {
51
+ const clause = new Clause_1.SelectClause(items, distinct);
52
+ return { value: clause, newIndex: idx };
53
+ }
54
+ }
55
+ static parseItem(lexemes, index) {
56
+ let idx = index;
57
+ const parsedValue = ValueParser_1.ValueParser.parse(lexemes, idx);
58
+ const value = parsedValue.value;
59
+ idx = parsedValue.newIndex;
60
+ if (idx < lexemes.length && lexemes[idx].value === 'as') {
61
+ // Skip 'AS' keyword
62
+ idx++;
63
+ }
64
+ if (idx < lexemes.length && lexemes[idx].type === Lexeme_1.TokenType.Identifier) {
65
+ const alias = lexemes[idx].value;
66
+ idx++;
67
+ return {
68
+ value: new Clause_1.SelectItem(value, alias),
69
+ newIndex: idx,
70
+ };
71
+ }
72
+ else {
73
+ // alias nameless
74
+ return {
75
+ value,
76
+ newIndex: idx,
77
+ };
78
+ }
79
+ }
80
+ }
81
+ exports.SelectClauseParser = SelectClauseParser;
82
+ //# sourceMappingURL=SelectClauseParser.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SelectClauseParser.js","sourceRoot":"","sources":["../../src/parsers/SelectClauseParser.ts"],"names":[],"mappings":";;;AAAA,6CAAsH;AACtH,6CAAqD;AACrD,iDAA8C;AAC9C,+CAA4C;AAE5C,MAAa,kBAAkB;IACpB,MAAM,CAAC,aAAa,CAAC,KAAa;QACrC,MAAM,SAAS,GAAG,IAAI,2BAAY,CAAC,KAAK,CAAC,CAAC,CAAC,uBAAuB;QAClE,MAAM,OAAO,GAAG,SAAS,CAAC,UAAU,EAAE,CAAC,CAAC,aAAa;QAErD,QAAQ;QACR,MAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC;QAEtC,sCAAsC;QACtC,IAAI,MAAM,CAAC,QAAQ,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;YACnC,MAAM,IAAI,KAAK,CAAC,mCAAmC,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,KAAK,iBAAiB,MAAM,CAAC,QAAQ,kEAAkE,CAAC,CAAC;QACzL,CAAC;QAED,OAAO,MAAM,CAAC,KAAK,CAAC;IACxB,CAAC;IAEM,M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AC,KAAK,EAAE,KAAK,CAAC;gBACnC,QAAQ,EAAE,GAAG;aAChB,CAAC;QACN,CAAC;aAAM,CAAC;YACJ,iBAAiB;YACjB,OAAO;gBACH,KAAK;gBACL,QAAQ,EAAE,GAAG;aAChB,CAAC;QACN,CAAC;IACL,CAAC;CACJ;AAlFD,gDAkFC"}
@@ -0,0 +1,151 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SelectQueryParser = void 0;
4
+ const SelectQuery_1 = require("../models/SelectQuery");
5
+ const SelectClauseParser_1 = require("./SelectClauseParser");
6
+ const FromClauseParser_1 = require("./FromClauseParser");
7
+ const WhereClauseParser_1 = require("./WhereClauseParser");
8
+ const GroupByParser_1 = require("./GroupByParser");
9
+ const HavingParser_1 = require("./HavingParser");
10
+ const OrderByClauseParser_1 = require("./OrderByClauseParser");
11
+ const WindowClauseParser_1 = require("./WindowClauseParser");
12
+ const LimitClauseParser_1 = require("./LimitClauseParser");
13
+ const ForClauseParser_1 = require("./ForClauseParser");
14
+ const SqlTokenizer_1 = require("./SqlTokenizer");
15
+ const WithClauseParser_1 = require("./WithClauseParser");
16
+ const ValuesQueryParser_1 = require("./ValuesQueryParser");
17
+ class SelectQueryParser {
18
+ static parseFromText(query) {
19
+ const tokenizer = new SqlTokenizer_1.SqlTokenizer(query);
20
+ const lexemes = tokenizer.readLexmes();
21
+ // Parse
22
+ const result = this.parse(lexemes, 0);
23
+ // Error if there are remaining tokens
24
+ if (result.newIndex < lexemes.length) {
25
+ throw new Error(`Syntax error: Unexpected token "${lexemes[result.newIndex].value}" at position ${result.newIndex}. The SELECT query is complete but there are additional tokens.`);
26
+ }
27
+ return result.value;
28
+ }
29
+ static parse(lexemes, index) {
30
+ let idx = index;
31
+ if (idx >= lexemes.length) {
32
+ throw new Error(`Syntax error: Unexpected end of input at position ${index}.`);
33
+ }
34
+ // Check if the first token is a SELECT keyword or VALUES
35
+ const firstToken = lexemes[idx].value;
36
+ if (!this.selectCommandSet.has(firstToken) && firstToken !== 'values') {
37
+ throw new Error(`Syntax error at position ${idx}: Expected 'SELECT' or 'VALUES' keyword but found "${lexemes[idx].value}".`);
38
+ }
39
+ let firstResult = this.selectCommandSet.has(firstToken)
40
+ ? this.parseSimpleSelectQuery(lexemes, idx)
41
+ : this.parseValuesQuery(lexemes, idx);
42
+ let query = firstResult.value;
43
+ idx = firstResult.newIndex;
44
+ // check 'union'
45
+ while (idx < lexemes.length && this.unionCommandSet.has(lexemes[idx].value.toLowerCase())) {
46
+ const operator = lexemes[idx].value.toLowerCase();
47
+ idx++;
48
+ if (idx >= lexemes.length) {
49
+ throw new Error(`Syntax error at position ${idx}: Expected a query after '${operator.toUpperCase()}' but found end of input.`);
50
+ }
51
+ const nextToken = lexemes[idx].value.toLowerCase();
52
+ if (this.selectCommandSet.has(nextToken)) {
53
+ const result = this.parseSimpleSelectQuery(lexemes, idx);
54
+ query = new SelectQuery_1.BinarySelectQuery(query, operator, result.value);
55
+ idx = result.newIndex;
56
+ }
57
+ else if (nextToken === 'values') {
58
+ const result = this.parseValuesQuery(lexemes, idx);
59
+ query = new SelectQuery_1.BinarySelectQuery(query, operator, result.value);
60
+ idx = result.newIndex;
61
+ }
62
+ else {
63
+ throw new Error(`Syntax error at position ${idx}: Expected 'SELECT' or 'VALUES' after '${operator.toUpperCase()}' but found "${lexemes[idx].value}".`);
64
+ }
65
+ }
66
+ return { value: query, newIndex: idx };
67
+ }
68
+ static parseSimpleSelectQuery(lexemes, index) {
69
+ let idx = index;
70
+ let withClauseResult = null;
71
+ // Parse optional WITH clause
72
+ if (idx < lexemes.length && lexemes[idx].value.toLowerCase() === 'with') {
73
+ withClauseResult = WithClauseParser_1.WithClauseParser.parse(lexemes, idx);
74
+ idx = withClauseResult.newIndex;
75
+ }
76
+ // Parse SELECT clause (required)
77
+ if (idx >= lexemes.length || lexemes[idx].value.toLowerCase() !== 'select') {
78
+ throw new Error(`Syntax error at position ${idx}: Expected 'SELECT' keyword but found "${idx < lexemes.length ? lexemes[idx].value : 'end of input'}". SELECT queries must start with the SELECT keyword.`);
79
+ }
80
+ const selectClauseResult = SelectClauseParser_1.SelectClauseParser.parse(lexemes, idx);
81
+ idx = selectClauseResult.newIndex;
82
+ // Parse FROM clause (optional)
83
+ let fromClauseResult = null;
84
+ if (idx < lexemes.length && lexemes[idx].value.toLowerCase() === 'from') {
85
+ fromClauseResult = FromClauseParser_1.FromClauseParser.parse(lexemes, idx);
86
+ idx = fromClauseResult.newIndex;
87
+ }
88
+ // Parse WHERE clause (optional)
89
+ let whereClauseResult = null;
90
+ if (idx < lexemes.length && lexemes[idx].value.toLowerCase() === 'where') {
91
+ whereClauseResult = WhereClauseParser_1.WhereClauseParser.parse(lexemes, idx);
92
+ idx = whereClauseResult.newIndex;
93
+ }
94
+ // Parse GROUP BY clause (optional)
95
+ let groupByClauseResult = null;
96
+ if (idx < lexemes.length && lexemes[idx].value.toLowerCase() === 'group by') {
97
+ groupByClauseResult = GroupByParser_1.GroupByClauseParser.parse(lexemes, idx);
98
+ idx = groupByClauseResult.newIndex;
99
+ }
100
+ // Parse HAVING clause (optional)
101
+ let havingClauseResult = null;
102
+ if (idx < lexemes.length && lexemes[idx].value.toLowerCase() === 'having') {
103
+ havingClauseResult = HavingParser_1.HavingClauseParser.parse(lexemes, idx);
104
+ idx = havingClauseResult.newIndex;
105
+ }
106
+ // Parse WINDOW clause (optional)
107
+ let windowFrameClauseResult = null;
108
+ if (idx < lexemes.length && lexemes[idx].value.toLowerCase() === 'window') {
109
+ windowFrameClauseResult = WindowClauseParser_1.WindowClauseParser.parse(lexemes, idx);
110
+ idx = windowFrameClauseResult.newIndex;
111
+ }
112
+ // Parse ORDER BY clause (optional)
113
+ let orderByClauseResult = null;
114
+ if (idx < lexemes.length && lexemes[idx].value.toLowerCase() === 'order by') {
115
+ orderByClauseResult = OrderByClauseParser_1.OrderByClauseParser.parse(lexemes, idx);
116
+ idx = orderByClauseResult.newIndex;
117
+ }
118
+ // Parse LIMIT clause (optional)
119
+ let limitClauseResult = null;
120
+ if (idx < lexemes.length && lexemes[idx].value.toLowerCase() === 'limit') {
121
+ limitClauseResult = LimitClauseParser_1.LimitClauseParser.parse(lexemes, idx);
122
+ idx = limitClauseResult.newIndex;
123
+ }
124
+ // Parse FOR clause (optional)
125
+ let forClauseResult = null;
126
+ if (idx < lexemes.length && lexemes[idx].value.toLowerCase() === 'for') {
127
+ forClauseResult = ForClauseParser_1.ForClauseParser.parse(lexemes, idx);
128
+ idx = forClauseResult.newIndex;
129
+ }
130
+ // Create and return the SelectQuery object
131
+ const selectQuery = new SelectQuery_1.SimpleSelectQuery(withClauseResult ? withClauseResult.value : null, selectClauseResult.value, fromClauseResult ? fromClauseResult.value : null, whereClauseResult ? whereClauseResult.value : null, groupByClauseResult ? groupByClauseResult.value : null, havingClauseResult ? havingClauseResult.value : null, orderByClauseResult ? orderByClauseResult.value : null, windowFrameClauseResult ? windowFrameClauseResult.value : null, limitClauseResult ? limitClauseResult.value : null, forClauseResult ? forClauseResult.value : null);
132
+ return { value: selectQuery, newIndex: idx };
133
+ }
134
+ static parseValuesQuery(lexemes, index) {
135
+ // Use ValuesQueryParser to parse VALUES clause
136
+ const result = ValuesQueryParser_1.ValuesQueryParser.parse(lexemes, index);
137
+ // Return the result from ValuesQueryParser directly
138
+ return { value: result.value, newIndex: result.newIndex };
139
+ }
140
+ }
141
+ exports.SelectQueryParser = SelectQueryParser;
142
+ SelectQueryParser.unionCommandSet = new Set([
143
+ "union",
144
+ "union all",
145
+ "intersect",
146
+ "intersect all",
147
+ "except",
148
+ "except all",
149
+ ]);
150
+ SelectQueryParser.selectCommandSet = new Set(["with", "select"]);
151
+ //# sourceMappingURL=SelectQueryParser.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SelectQueryParser.js","sourceRoot":"","sources":["../../src/parsers/SelectQueryParser.ts"],"names":[],"mappings":";;;AACA,uDAA0F;AAC1F,6DAA0D;AAC1D,yDAAsD;AACtD,2DAAwD;A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,EAAE,CAAC;IAC9D,CAAC;;AAxKL,8CAyKC;AAzJkB,iCAAe,GAAG,IAAI,GAAG,CAAS;IAC7C,OAAO;IACP,WAAW;IACX,WAAW;IACX,eAAe;IACf,QAAQ;IACR,YAAY;CACf,CAAC,CAAC;AACY,kCAAgB,GAAG,IAAI,GAAG,CAAS,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC,CAAC"}
@@ -0,0 +1,48 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SourceAliasExpressionParser = void 0;
4
+ const Clause_1 = require("../models/Clause");
5
+ const Lexeme_1 = require("../models/Lexeme");
6
+ class SourceAliasExpressionParser {
7
+ static parse(lexemes, index) {
8
+ var _a;
9
+ let idx = index;
10
+ // If there is a column alias, it may be detected as a function, so functions are also processed.
11
+ if (idx < lexemes.length && (lexemes[idx].type === Lexeme_1.TokenType.Identifier || lexemes[idx].type === Lexeme_1.TokenType.Function)) {
12
+ // Check for alias
13
+ const table = lexemes[idx].value;
14
+ idx++;
15
+ if (idx < lexemes.length && lexemes[idx].type === Lexeme_1.TokenType.OpenParen) {
16
+ // Check for column alias
17
+ const columns = [];
18
+ // Skip the open parenthesis
19
+ idx++;
20
+ while (idx < lexemes.length && lexemes[idx].type === Lexeme_1.TokenType.Identifier) {
21
+ columns.push(lexemes[idx].value);
22
+ idx++;
23
+ if (idx < lexemes.length && lexemes[idx].type === Lexeme_1.TokenType.Comma) {
24
+ idx++;
25
+ }
26
+ else {
27
+ break; // Exit loop if not a comma
28
+ }
29
+ }
30
+ if (lexemes[idx].type === Lexeme_1.TokenType.CloseParen) {
31
+ // Skip the closing parenthesis
32
+ idx++;
33
+ }
34
+ else {
35
+ throw new Error(`Syntax error at position ${idx}: Missing closing parenthesis ')' for column alias list. Each opening parenthesis must have a matching closing parenthesis.`);
36
+ }
37
+ if (columns.length === 0) {
38
+ throw new Error(`Syntax error at position ${index}: No column aliases found. Column alias declarations must contain at least one column name.`);
39
+ }
40
+ return { value: new Clause_1.SourceAliasExpression(table, columns), newIndex: idx };
41
+ }
42
+ return { value: new Clause_1.SourceAliasExpression(table, null), newIndex: idx };
43
+ }
44
+ throw new Error(`Syntax error at position ${index}: Expected an identifier for table alias but found "${((_a = lexemes[index]) === null || _a === void 0 ? void 0 : _a.value) || 'end of input'}".`);
45
+ }
46
+ }
47
+ exports.SourceAliasExpressionParser = SourceAliasExpressionParser;
48
+ //# sourceMappingURL=SourceAliasExpressionParser.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SourceAliasExpressionParser.js","sourceRoot":"","sources":["../../src/parsers/SourceAliasExpressionParser.ts"],"names":[],"mappings":";;;AAAA,6CAAyD;AACzD,6CAAqD;AAGrD,MAAa,2BAA2B;IAE7B,MAAM,CAAC,KAAK,CAAC,OAAiB,EAAE,KAAa;;QAChD,IAAI,GAAG,GAAG,KAAK,CAAC;QAEhB,iGAAiG;QACjG,IAAI,GAAG,GAAG,OAAO,CAAC,MAAM,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,IAAI,KAAK,kBAAS,CAAC,UAAU,IAAI,OAAO,CAAC,GAAG,CAAC,CAAC,IAAI,KAAK,kBAAS,CAAC,QAAQ,CAAC,EAAE,CAAC;YACnH,kBAAkB;YAClB,MAAM,KAAK,GAAG,OAAO,CAAC,GAAG,CAAC,CAAC,KAAK,CAAC;YACjC,GAAG,EAAE,CAAC;YAEN,IAAI,GAAG,GAAG,OAAO,CAAC,MAAM,IAAI,OAAO,CAAC,GAAG,CAAC,CAAC,IAAI,KAAK,kBAAS,CAAC,SAAS,EAAE,CAAC;gBACpE,yBAAyB;gBACzB,MAAM,OAAO,GAAa,EAAE,CAAC;gBAE7B,4BAA4B;gBAC5B,GAAG,EAAE,CAAC;gBAEN,OAAO,GAAG,GAAG,OAAO,CAAC,MAAM,IAAI,OAAO,CAAC,GAAG,CAAC,CAAC,IAAI,KAAK,kBAAS,CAAC,UAAU,EAAE,CAAC;oBACxE,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC;oBACjC,GAAG,EAAE,CAAC;oBACN,IAAI,GAAG,GAAG,OAAO,CAAC,MAAM,IAAI,OAAO,CAAC,GAAG,CAAC,CAAC,IAAI,KAAK,kBAAS,CAAC,KAAK,EAAE,CAAC;wBAChE,GAAG,EAAE,CAAC;oBACV,CAAC;yBAAM,CAAC;wBACJ,MAAM,CAAC,2BAA2B;oBACtC,CAAC;gBACL,CAAC;gBAED,IAAI,OAAO,CAAC,GAAG,CAAC,CAAC,IAAI,KAAK,kBAAS,CAAC,UAAU,EAAE,CAAC;oBAC7C,+BAA+B;oBAC/B,GAAG,EAAE,CAAC;gBACV,CAAC;qBAAM,CAAC;oBACJ,MAAM,IAAI,KAAK,CAAC,4BAA4B,GAAG,6HAA6H,CAAC,CAAC;gBAClL,CAAC;gBACD,IAAI,OAAO,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;oBACvB,MAAM,IAAI,KAAK,CAAC,4BAA4B,KAAK,6FAA6F,CAAC,CAAC;gBACpJ,CAAC;gBAED,OAAO,EAAE,KAAK,EAAE,IAAI,8BAAqB,CAAC,KAAK,EAAE,OAAO,CAAC,EAAE,QAAQ,EAAE,GAAG,EAAE,CAAC;YAC/E,CAAC;YAED,OAAO,EAAE,KAAK,EAAE,IAAI,8BAAqB,CAAC,KAAK,EAAE,IAAI,CAAC,EAAE,QAAQ,EAAE,GAAG,EAAE,CAAC;QAC5E,CAAC;QAED,MAAM,IAAI,KAAK,CAAC,4BAA4B,KAAK,uDAAuD,CAAA,MAAA,OAAO,CAAC,KAAK,CAAC,0CAAE,KAAK,KAAI,cAAc,IAAI,CAAC,CAAC;IACzJ,CAAC;CACJ;AA9CD,kEA8CC"}
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SourceExpressionParser = void 0;
4
+ const Clause_1 = require("../models/Clause");
5
+ const Lexeme_1 = require("../models/Lexeme");
6
+ const SourceParser_1 = require("./SourceParser");
7
+ const SourceAliasExpressionParser_1 = require("./SourceAliasExpressionParser");
8
+ class SourceExpressionParser {
9
+ static parse(lexemes, index) {
10
+ let idx = index;
11
+ const sourceResult = SourceParser_1.SourceParser.parse(lexemes, idx);
12
+ idx = sourceResult.newIndex;
13
+ if (idx < lexemes.length) {
14
+ if (lexemes[idx].value === "as") {
15
+ idx++;
16
+ const aliasResult = SourceAliasExpressionParser_1.SourceAliasExpressionParser.parse(lexemes, idx);
17
+ idx = aliasResult.newIndex;
18
+ const sourceExpr = new Clause_1.SourceExpression(sourceResult.value, aliasResult.value);
19
+ return { value: sourceExpr, newIndex: idx };
20
+ }
21
+ if (lexemes[idx].type === Lexeme_1.TokenType.Identifier) {
22
+ const aliasResult = SourceAliasExpressionParser_1.SourceAliasExpressionParser.parse(lexemes, idx);
23
+ idx = aliasResult.newIndex;
24
+ const sourceExpr = new Clause_1.SourceExpression(sourceResult.value, aliasResult.value);
25
+ return { value: sourceExpr, newIndex: idx };
26
+ }
27
+ }
28
+ // no alias
29
+ const expr = new Clause_1.SourceExpression(sourceResult.value, null);
30
+ return { value: expr, newIndex: idx };
31
+ }
32
+ }
33
+ exports.SourceExpressionParser = SourceExpressionParser;
34
+ //# sourceMappingURL=SourceExpressionParser.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SourceExpressionParser.js","sourceRoot":"","sources":["../../src/parsers/SourceExpressionParser.ts"],"names":[],"mappings":";;;AAAA,6CAAwF;AACxF,6CAAqD;AACrD,iDAA8C;AAC9C,+EAA4E;AAE5E,MAAa,sBAAsB;IACxB,MAAM,CAAC,KAAK,CAAC,OAAiB,EAAE,KAAa;QAChD,IAAI,GAAG,GAAG,KAAK,CAAC;QAEhB,MAAM,YAAY,GAAG,2BAAY,CAAC,KAAK,CAAC,OAAO,EAAE,GAAG,CAAC,CAAC;QACtD,GAAG,GAAG,YAAY,CAAC,QAAQ,CAAC;QAE5B,IAAI,GAAG,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;YACvB,IAAI,OAAO,CAAC,GAAG,CAAC,CAAC,KAAK,KAAK,IAAI,EAAE,CAAC;gBAC9B,GAAG,EAAE,CAAC;gBACN,MAAM,WAAW,GAAG,yDAA2B,CAAC,KAAK,CAAC,OAAO,EAAE,GAAG,CAAC,CAAC;gBACpE,GAAG,GAAG,WAAW,CAAC,QAAQ,CAAC;gBAC3B,MAAM,UAAU,GAAG,IAAI,yBAAgB,CAAC,YAAY,CAAC,KAAK,EAAE,WAAW,CAAC,KAAK,CAAC,CAAC;gBAC/E,OAAO,EAAE,KAAK,EAAE,UAAU,EAAE,QAAQ,EAAE,GAAG,EAAE,CAAC;YAChD,CAAC;YAED,IAAI,OAAO,CAAC,GAAG,CAAC,CAAC,IAAI,KAAK,kBAAS,CAAC,UAAU,EAAE,CAAC;gBAC7C,MAAM,WAAW,GAAG,yDAA2B,CAAC,KAAK,CAAC,OAAO,EAAE,GAAG,CAAC,CAAC;gBACpE,GAAG,GAAG,WAAW,CAAC,QAAQ,CAAC;gBAC3B,MAAM,UAAU,GAAG,IAAI,yBAAgB,CAAC,YAAY,CAAC,KAAK,EAAE,WAAW,CAAC,KAAK,CAAC,CAAC;gBAC/E,OAAO,EAAE,KAAK,EAAE,UAAU,EAAE,QAAQ,EAAE,GAAG,EAAE,CAAC;YAChD,CAAC;QACL,CAAC;QAED,WAAW;QACX,MAAM,IAAI,GAAG,IAAI,yBAAgB,CAAC,YAAY,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;QAC5D,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,QAAQ,EAAE,GAAG,EAAE,CAAC;IAC1C,CAAC;CACJ;AA5BD,wDA4BC"}
@@ -0,0 +1,116 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SourceParser = void 0;
4
+ const Clause_1 = require("../models/Clause");
5
+ const Lexeme_1 = require("../models/Lexeme");
6
+ const SelectQueryParser_1 = require("./SelectQueryParser");
7
+ const SqlTokenizer_1 = require("./SqlTokenizer");
8
+ const ValueParser_1 = require("./ValueParser");
9
+ class SourceParser {
10
+ static parseFromText(query) {
11
+ const tokenizer = new SqlTokenizer_1.SqlTokenizer(query); // Initialize tokenizer
12
+ const lexemes = tokenizer.readLexmes(); // Get tokens
13
+ // Parse
14
+ const result = this.parse(lexemes, 0);
15
+ // Error if there are remaining tokens
16
+ if (result.newIndex < lexemes.length) {
17
+ throw new Error(`Syntax error: Unexpected token "${lexemes[result.newIndex].value}" at position ${result.newIndex}. The source component is complete but there are additional tokens.`);
18
+ }
19
+ return result.value;
20
+ }
21
+ static parse(lexemes, index) {
22
+ const idx = index;
23
+ // Handle subquery
24
+ if (idx < lexemes.length && lexemes[idx].type === Lexeme_1.TokenType.OpenParen) {
25
+ return this.parseParenSource(lexemes, idx);
26
+ }
27
+ // Handle function-based source
28
+ if (idx < lexemes.length && lexemes[idx].type === Lexeme_1.TokenType.Function) {
29
+ return this.parseFunctionSource(lexemes, idx);
30
+ }
31
+ // Handle table source (regular table, potentially schema-qualified)
32
+ return this.parseTableSource(lexemes, idx);
33
+ }
34
+ static parseTableSource(lexemes, index) {
35
+ // Check for column reference pattern ([identifier dot] * n + identifier)
36
+ let idx = index;
37
+ const identifiers = [];
38
+ // Add the first identifier
39
+ identifiers.push(lexemes[idx].value);
40
+ idx++;
41
+ // Look for dot and identifier pattern
42
+ while (idx < lexemes.length &&
43
+ idx + 1 < lexemes.length &&
44
+ lexemes[idx].type === Lexeme_1.TokenType.Dot &&
45
+ lexemes[idx + 1].type === Lexeme_1.TokenType.Identifier) {
46
+ // Skip the dot and add the next identifier
47
+ idx++;
48
+ identifiers.push(lexemes[idx].value);
49
+ idx++;
50
+ }
51
+ if (identifiers.length > 1) {
52
+ // If there are multiple identifiers, treat it as a column reference
53
+ const lastIdentifier = identifiers.pop() || '';
54
+ const value = new Clause_1.TableSource(identifiers, lastIdentifier);
55
+ return { value, newIndex: idx };
56
+ }
57
+ else {
58
+ // If there is a single identifier, treat it as a simple identifier
59
+ const value = new Clause_1.TableSource(null, identifiers[0]);
60
+ return { value, newIndex: idx };
61
+ }
62
+ }
63
+ static parseFunctionSource(lexemes, index) {
64
+ let idx = index;
65
+ const functionName = lexemes[idx].value;
66
+ idx++;
67
+ const argument = ValueParser_1.ValueParser.parseArgument(Lexeme_1.TokenType.OpenParen, Lexeme_1.TokenType.CloseParen, lexemes, idx);
68
+ idx = argument.newIndex;
69
+ const result = new Clause_1.FunctionSource(functionName, argument.value);
70
+ return { value: result, newIndex: idx };
71
+ }
72
+ static parseParenSource(lexemes, index) {
73
+ let idx = index;
74
+ // skip the open parenthesis
75
+ idx++;
76
+ if (idx >= lexemes.length) {
77
+ throw new Error(`Syntax error: Unexpected end of input at position ${idx}. Expected a subquery or nested expression after opening parenthesis.`);
78
+ }
79
+ // Support both SELECT and VALUES in subqueries
80
+ const keyword = lexemes[idx].value;
81
+ if (keyword === "select" || keyword === "values" || keyword === "with") {
82
+ const result = this.parseSubQuerySource(lexemes, idx);
83
+ idx = result.newIndex;
84
+ if (idx < lexemes.length && lexemes[idx].type == Lexeme_1.TokenType.CloseParen) {
85
+ // skip the closing parenthesis
86
+ idx++;
87
+ }
88
+ else {
89
+ throw new Error(`Syntax error at position ${idx}: Missing closing parenthesis. Each opening parenthesis must have a matching closing parenthesis.`);
90
+ }
91
+ return { value: result.value, newIndex: idx };
92
+ }
93
+ else if (lexemes[idx].type == Lexeme_1.TokenType.OpenParen) {
94
+ const result = this.parseParenSource(lexemes, idx);
95
+ idx = result.newIndex;
96
+ if (idx < lexemes.length && lexemes[idx].type == Lexeme_1.TokenType.CloseParen) {
97
+ // skip the closing parenthesis
98
+ idx++;
99
+ }
100
+ else {
101
+ throw new Error(`Syntax error at position ${idx}: Missing closing parenthesis. Each opening parenthesis must have a matching closing parenthesis.`);
102
+ }
103
+ return { value: result.value, newIndex: idx };
104
+ }
105
+ throw new Error(`Syntax error at position ${idx}: Expected 'SELECT' keyword, 'VALUES' keyword, or opening parenthesis '(' but found "${lexemes[idx].value}".`);
106
+ }
107
+ static parseSubQuerySource(lexemes, index) {
108
+ let idx = index;
109
+ const selectQuery = SelectQueryParser_1.SelectQueryParser.parse(lexemes, idx);
110
+ idx = selectQuery.newIndex;
111
+ const subQuerySource = new Clause_1.SubQuerySource(selectQuery.value);
112
+ return { value: subQuerySource, newIndex: idx };
113
+ }
114
+ }
115
+ exports.SourceParser = SourceParser;
116
+ //# sourceMappingURL=SourceParser.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SourceParser.js","sourceRoot":"","sources":["../../src/parsers/SourceParser.ts"],"names":[],"mappings":";;;AAAA,6CAAgG;AAChG,6CAAqD;AACrD,2DAAwD;AACxD,iDAA8C;AAC9C,+CAA4C;AAE5C,MAAa,YAAY;IAEd,MAAM,CAAC,aAAa,CAAC,KAAa;QACrC,MAAM,SAAS,GAAG,IAAI,2BAAY,CAAC,KAAK,CAAC,CAAC,CAAC,uBAAuB;QAClE,MAAM,OAAO,GAAG,SAAS,CAAC,UAAU,EAAE,CAAC,CAAC,aAAa;QAErD,QAAQ;QACR,MAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC;QAEtC,sCAAsC;QACtC,IAAI,MAAM,CAAC,QAAQ,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;YACnC,MAAM,IAAI,KAAK,CAAC,mCAAmC,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,KAAK,iBAAiB,MAAM,CAAC,QAAQ,qEAAqE,CAAC,CAAC;QAC5L,CAAC;QAED,OAAO,MAAM,CAAC,KAAK,CAAC;IACxB,CAAC;IAEM,MAAM,CAAC,KAAK,CAAC,OAAiB,EAAE,KAAa;QAChD,MAAM,GAAG,GAAG,KAAK,CAAC;QAElB,kBAAkB;QAClB,IAAI,GAAG,GAAG,OAAO,CAAC,MAAM,IAAI,OAAO,CAAC,GAAG,CAAC,C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CAAC,KAAK,IAAI,CAAC,CAAC;IACnK,CAAC;IAEO,MAAM,CAAC,mBAAmB,CAAC,OAAiB,EAAE,KAAa;QAC/D,IAAI,GAAG,GAAG,KAAK,CAAC;QAEhB,MAAM,WAAW,GAAG,qCAAiB,CAAC,KAAK,CAAC,OAAO,EAAE,GAAG,CAAC,CAAC;QAC1D,GAAG,GAAG,WAAW,CAAC,QAAQ,CAAC;QAE3B,MAAM,cAAc,GAAG,IAAI,uBAAc,CAAC,WAAW,CAAC,KAAK,CAAC,CAAC;QAC7D,OAAO,EAAE,KAAK,EAAE,cAAc,EAAE,QAAQ,EAAE,GAAG,EAAE,CAAC;IACpD,CAAC;CACJ;AA5HD,oCA4HC"}
@@ -0,0 +1,174 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SqlTokenizer = void 0;
4
+ const Lexeme_1 = require("../models/Lexeme");
5
+ const IdentifierTokenReader_1 = require("../tokenReaders/IdentifierTokenReader");
6
+ const LiteralTokenReader_1 = require("../tokenReaders/LiteralTokenReader");
7
+ const ParameterTokenReader_1 = require("../tokenReaders/ParameterTokenReader");
8
+ const SymbolTokenReader_1 = require("../tokenReaders/SymbolTokenReader");
9
+ const TokenReaderManager_1 = require("../tokenReaders/TokenReaderManager");
10
+ const OperatorTokenReader_1 = require("../tokenReaders/OperatorTokenReader");
11
+ const stringUtils_1 = require("../utils/stringUtils");
12
+ const CommandTokenReader_1 = require("../tokenReaders/CommandTokenReader");
13
+ const StringSpecifierTokenReader_1 = require("../tokenReaders/StringSpecifierTokenReader");
14
+ const FunctionTokenReader_1 = require("../tokenReaders/FunctionTokenReader");
15
+ const TypeTokenReader_1 = require("../tokenReaders/TypeTokenReader");
16
+ /**
17
+ * Class responsible for tokenizing SQL input.
18
+ */
19
+ class SqlTokenizer {
20
+ /**
21
+ * Initializes a new instance of the SqlTokenizer.
22
+ */
23
+ constructor(input) {
24
+ this.input = input;
25
+ this.position = 0;
26
+ // Initialize the token reader manager and register all readers
27
+ this.readerManager = new TokenReaderManager_1.TokenReaderManager(input)
28
+ .register(new ParameterTokenReader_1.ParameterTokenReader(input))
29
+ .register(new StringSpecifierTokenReader_1.StringSpecifierTokenReader(input))
30
+ // LiteralTokenReader should be registered before SpecialSymbolTokenReader and OperatorTokenReader
31
+ // Reason: To prevent numeric literals starting with a dot or sign from being misrecognized as operators
32
+ // e.g. `1.0` is a literal, not an operator
33
+ .register(new LiteralTokenReader_1.LiteralTokenReader(input))
34
+ .register(new SymbolTokenReader_1.SpecialSymbolTokenReader(input))
35
+ .register(new CommandTokenReader_1.CommandTokenReader(input))
36
+ .register(new OperatorTokenReader_1.OperatorTokenReader(input))
37
+ // TypeTokenReader should be registered before FunctionTokenReader
38
+ // Reason: To prevent types containing parentheses from being misrecognized as functions
39
+ // e.g. `numeric(10, 2)` is a type, not a function
40
+ .register(new TypeTokenReader_1.TypeTokenReader(input))
41
+ .register(new FunctionTokenReader_1.FunctionTokenReader(input))
42
+ .register(new IdentifierTokenReader_1.IdentifierTokenReader(input)) // IdentifierTokenReader should be registered last
43
+ ;
44
+ }
45
+ /**
46
+ * Checks if the end of input is reached.
47
+ *
48
+ * @param shift - The shift to consider beyond the current position.
49
+ * @returns True if the end of input is reached; otherwise, false.
50
+ */
51
+ isEndOfInput(shift = 0) {
52
+ return this.position + shift >= this.input.length;
53
+ }
54
+ /**
55
+ * Checks if more input can be read.
56
+ *
57
+ * @param shift - The shift to consider beyond the current position.
58
+ * @returns True if more input can be read; otherwise, false.
59
+ */
60
+ canRead(shift = 0) {
61
+ return !this.isEndOfInput(shift);
62
+ }
63
+ /**
64
+ * Reads the lexemes from the input string.
65
+ *
66
+ * @returns An array of lexemes extracted from the input string.
67
+ * @throws Error if an unexpected character is encountered.
68
+ */
69
+ readLexmes() {
70
+ // Pre-allocate array with estimated capacity for better performance
71
+ const estimatedTokens = Math.ceil(this.input.length / 8); // Assuming average token length of 8 chars
72
+ const lexemes = new Array(estimatedTokens);
73
+ let lexemeCount = 0;
74
+ // Read initial prefix comments
75
+ const comment = this.readComment();
76
+ let pendingComments = comment.lines;
77
+ this.position = comment.position;
78
+ // Track the previous token
79
+ let previous = null;
80
+ // Read tokens until the end of input is reached
81
+ while (this.canRead()) {
82
+ // Semicolon is a delimiter, so stop reading
83
+ if (this.input[this.position] === ';') {
84
+ break;
85
+ }
86
+ // Read using the token reader manager
87
+ const lexeme = this.readerManager.tryRead(this.position, previous);
88
+ if (lexeme === null) {
89
+ throw new Error(`Unexpected character. actual: ${this.input[this.position]}, position: ${this.position}\n${this.getDebugPositionInfo(this.position)}`);
90
+ }
91
+ // Update position
92
+ this.position = this.readerManager.getMaxPosition();
93
+ // Read suffix comments
94
+ const currentComment = this.readComment();
95
+ this.position = currentComment.position;
96
+ if (lexeme.type === Lexeme_1.TokenType.Comma || lexeme.type === Lexeme_1.TokenType.Operator) {
97
+ // Carry over comments after commas or operators
98
+ if (currentComment.lines.length > 0) {
99
+ pendingComments.push(...currentComment.lines);
100
+ }
101
+ }
102
+ else {
103
+ // Add comments to the current token if any
104
+ const hasComments = pendingComments.length > 0 || currentComment.lines.length > 0;
105
+ if (hasComments) {
106
+ this.addCommentsToToken(lexeme, pendingComments, currentComment.lines);
107
+ }
108
+ pendingComments = []; // Clear as they are processed
109
+ }
110
+ lexemes[lexemeCount++] = lexeme;
111
+ previous = lexeme;
112
+ }
113
+ // Add any pending comments to the last token
114
+ if (pendingComments.length > 0 && lexemeCount > 0) {
115
+ const lastToken = lexemes[lexemeCount - 1];
116
+ if (lastToken.comments === null) {
117
+ lastToken.comments = [];
118
+ }
119
+ lastToken.comments.push(...pendingComments);
120
+ }
121
+ // Trim the array to actual size used
122
+ return lexemeCount === estimatedTokens ? lexemes : lexemes.slice(0, lexemeCount);
123
+ }
124
+ /**
125
+ * Adds pending comments to the last token.
126
+ */
127
+ addPendingCommentsToLastToken(lexemes, pendingComments) {
128
+ if (pendingComments.length > 0 && lexemes.length > 0) {
129
+ const lastToken = lexemes[lexemes.length - 1];
130
+ if (lastToken.comments === null) {
131
+ lastToken.comments = [];
132
+ }
133
+ lastToken.comments.push(...pendingComments);
134
+ }
135
+ }
136
+ /**
137
+ * Adds comments to the token.
138
+ */
139
+ addCommentsToToken(lexeme, prefixComments, suffixComments) {
140
+ const hasComments = prefixComments.length > 0 || suffixComments.length > 0;
141
+ if (hasComments) {
142
+ if (lexeme.comments === null) {
143
+ lexeme.comments = [];
144
+ }
145
+ // Add prefix comments to the beginning.
146
+ if (prefixComments.length > 0) {
147
+ lexeme.comments.unshift(...prefixComments);
148
+ }
149
+ // Add suffix comments to the end.
150
+ if (suffixComments.length > 0) {
151
+ lexeme.comments.push(...suffixComments);
152
+ }
153
+ }
154
+ }
155
+ /**
156
+ * Skips whitespace characters and SQL comments in the input.
157
+ *
158
+ * @remarks This method updates the position pointer.
159
+ */
160
+ readComment() {
161
+ return stringUtils_1.StringUtils.readWhiteSpaceAndComment(this.input, this.position);
162
+ }
163
+ /**
164
+ * Gets debug information for error reporting.
165
+ *
166
+ * @param errPosition - The position where the error occurred.
167
+ * @returns A string containing the debug position information.
168
+ */
169
+ getDebugPositionInfo(errPosition) {
170
+ return stringUtils_1.StringUtils.getDebugPositionInfo(this.input, errPosition);
171
+ }
172
+ }
173
+ exports.SqlTokenizer = SqlTokenizer;
174
+ //# sourceMappingURL=SqlTokenizer.js.map