1
1
import { ErrorNode , ParserRuleContext , TerminalNode , Token } from 'antlr4ng' ;
2
2
import { findCaretTokenIndex } from '../common/findCaretTokenIndex' ;
3
- import { CaretPosition , SemanticContext } from '../common/types' ;
3
+ import {
4
+ CaretPosition ,
5
+ SemanticCollectOptions ,
6
+ SemanticContext ,
7
+ SqlSplitStrategy ,
8
+ } from '../common/types' ;
4
9
5
10
export const SQL_SPLIT_SYMBOL_TEXT = ';' ;
6
11
7
12
abstract class SemanticContextCollector {
8
- constructor ( _input : string , caretPosition : CaretPosition , allTokens : Token [ ] ) {
13
+ constructor (
14
+ _input : string ,
15
+ caretPosition : CaretPosition ,
16
+ allTokens : Token [ ] ,
17
+ options ?: SemanticCollectOptions
18
+ ) {
9
19
// If caretPosition token is whiteSpace, tokenIndex may be undefined.
10
20
const tokenIndex = findCaretTokenIndex ( caretPosition , allTokens ) ;
11
21
12
22
if ( tokenIndex !== undefined ) {
13
23
this . _tokenIndex = tokenIndex ;
14
24
}
15
25
this . _allTokens = allTokens ;
26
+ this . options = {
27
+ ...this . options ,
28
+ ...options ,
29
+ } ;
16
30
17
31
if ( allTokens ?. length ) {
18
32
let i = tokenIndex ? tokenIndex - 1 : allTokens . length - 1 ;
@@ -50,6 +64,10 @@ abstract class SemanticContextCollector {
50
64
}
51
65
}
52
66
67
+ public readonly options : SemanticCollectOptions = {
68
+ sqlSplitStrategy : SqlSplitStrategy . LOOSE ,
69
+ } ;
70
+
53
71
private _tokenIndex : number ;
54
72
private _allTokens : Token [ ] = [ ] ;
55
73
@@ -117,6 +135,8 @@ abstract class SemanticContextCollector {
117
135
* It should be called in each language's own `enterStatement`.
118
136
*/
119
137
protected visitStatement ( ctx : ParserRuleContext ) {
138
+ if ( this . options . sqlSplitStrategy === SqlSplitStrategy . STRICT ) return ;
139
+
120
140
const isWhiteSpaceToken =
121
141
this . _tokenIndex === undefined ||
122
142
this . _allTokens [ this . _tokenIndex ] ?. type === this . getWhiteSpaceRuleType ( ) ||
@@ -135,7 +155,12 @@ abstract class SemanticContextCollector {
135
155
* Uncomplete keyword will be error node
136
156
*/
137
157
visitErrorNode ( node : ErrorNode ) : void {
138
- if ( node . symbol . tokenIndex !== this . _tokenIndex || this . _isNewStatement ) return ;
158
+ if (
159
+ node . symbol . tokenIndex !== this . _tokenIndex ||
160
+ this . _isNewStatement ||
161
+ this . options . sqlSplitStrategy === SqlSplitStrategy . STRICT
162
+ )
163
+ return ;
139
164
140
165
let parent : ParserRuleContext | null = node . parent as ParserRuleContext ;
141
166
let currentNode : TerminalNode | ParserRuleContext = node ;
@@ -188,7 +213,12 @@ abstract class SemanticContextCollector {
188
213
}
189
214
190
215
visitTerminal ( node : TerminalNode ) : void {
191
- if ( node . symbol . tokenIndex !== this . _tokenIndex || this . _isNewStatement ) return ;
216
+ if (
217
+ node . symbol . tokenIndex !== this . _tokenIndex ||
218
+ this . _isNewStatement ||
219
+ this . options . sqlSplitStrategy === SqlSplitStrategy . STRICT
220
+ )
221
+ return ;
192
222
193
223
let currentNode : TerminalNode | ParserRuleContext = node ;
194
224
let parent = node . parent as ParserRuleContext | null ;
0 commit comments