@@ -10,9 +10,50 @@ export abstract class SQLParserBase<T = antlr.ParserRuleContext> extends antlr.P
1010
1111 public entityCollecting = false ;
1212
13- public shouldMatchEmpty ( ) {
14- return this . entityCollecting
15- && ( this . tokenStream . LT ( - 1 ) ?. tokenIndex ?? Infinity ) <= this . caretTokenIndex
16- && ( this . tokenStream . LT ( 1 ) ?. tokenIndex ?? - Infinity ) >= this . caretTokenIndex
13+ /**
14+ * Semantic predicate to determine whether to match empty column.
15+ *
16+ * Key design:
17+ * 1. Only match empty column in entityCollecting mode
18+ * 2. Check if caret position is at the empty column position
19+ * 3. In validate mode (entityCollecting=false), this predicate returns false
20+ * and reports an error to ensure incomplete SQL is caught
21+ *
22+ * IMPORTANT: This predicate should be used carefully to avoid affecting
23+ * prediction in non-entity-collecting contexts.
24+ */
25+ public shouldMatchEmpty ( ruleName ?: string ) {
26+ // Only match in entityCollecting mode or when caret position is specified (suggestion mode)
27+ if ( this . entityCollecting || this . caretTokenIndex >= 0 ) {
28+ // If no caret position specified, match all empty columns
29+ if ( this . caretTokenIndex < 0 ) {
30+ return true ;
31+ }
32+
33+ // Check if caret is at the position where empty column would be
34+ const prevTokenIndex = this . tokenStream . LT ( - 1 ) ?. tokenIndex ;
35+ const nextTokenIndex = this . tokenStream . LT ( 1 ) ?. tokenIndex ;
36+
37+ // Match if caret is between previous and next token
38+ if ( prevTokenIndex !== undefined && nextTokenIndex !== undefined ) {
39+ return prevTokenIndex <= this . caretTokenIndex && nextTokenIndex >= this . caretTokenIndex ;
40+ }
41+
42+ // If only previous token exists, match if caret is after it
43+ if ( prevTokenIndex !== undefined ) {
44+ return prevTokenIndex <= this . caretTokenIndex ;
45+ }
46+
47+ // If only next token exists, match if caret is before it
48+ if ( nextTokenIndex !== undefined ) {
49+ return nextTokenIndex >= this . caretTokenIndex ;
50+ }
51+
52+ return false ;
53+ }
54+
55+ // In pure validate mode, don't match empty columns
56+ // This allows ANTLR to report errors naturally
57+ return false ;
1758 }
18- }
59+ }
0 commit comments