Skip to content

Commit

Permalink
feat: improve white-space token logic
Browse files Browse the repository at this point in the history
  • Loading branch information
JackWang032 committed Nov 15, 2024
1 parent 0b2b764 commit b29d058
Showing 1 changed file with 16 additions and 29 deletions.
45 changes: 16 additions & 29 deletions src/parser/common/semanticContextCollector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,14 @@ abstract class SemanticContextCollector {
}
i--;
}
if (tokenIndex === 0 || i === -1) {

// Current token is the first token of tokenStream or the previous token is semicolon
if (
tokenIndex === 0 ||
i === -1 ||
(this._prevTokenIndex &&
this._allTokens[this._prevTokenIndex].text === SQL_SPLIT_SYMBOL_TEXT)
) {
this._isNewStatement = true;
}
}
Expand Down Expand Up @@ -86,7 +93,7 @@ abstract class SemanticContextCollector {

/**
* Caret position is white space, so it will not visited as terminal node or error node.
* We can find the previous no-white-space token,
* We can find the first previous no-white-space token,
* and if previous token is the last leaf node of the statement,
* it can be considered as being in the context of new statement
*/
Expand All @@ -97,34 +104,21 @@ abstract class SemanticContextCollector {
// PostgreSQL whiteSpace not inlcudes '\n' symbol
this._allTokens[this._tokenIndex]?.text === '\n';

const isPrevTokenSplitSymbol =
this._prevTokenIndex &&
this._allTokens[this._prevTokenIndex].text === SQL_SPLIT_SYMBOL_TEXT;

const isPrevTokenEndOfStatement =
this._prevTokenIndex !== undefined &&
ctx.stop?.tokenIndex === this._prevTokenIndex &&
ctx.exception === null;
this._prevTokenIndex && ctx.stop?.tokenIndex === this._prevTokenIndex;

if (isWhiteSpaceToken && (isPrevTokenSplitSymbol || isPrevTokenEndOfStatement)) {
if (!this.previousStatementHasError(ctx)) {
this._isNewStatement = true;
}
if (isWhiteSpaceToken && isPrevTokenEndOfStatement && ctx.exception === null) {
this._isNewStatement = !this.previousStatementHasError(ctx)
? true
: this._isNewStatement;
}
}

/**
* Uncomplete keyword will be error node
*/
visitErrorNode(node: ErrorNode): void {
if (node.symbol.tokenIndex !== this._tokenIndex) return;
if (
this._prevTokenIndex &&
this._allTokens[this._prevTokenIndex].text === SQL_SPLIT_SYMBOL_TEXT
) {
this._isNewStatement = true;
return;
}
if (node.symbol.tokenIndex !== this._tokenIndex || this._isNewStatement) return;

let parent: ParserRuleContext | null = node.parent as ParserRuleContext;
let currentNode: TerminalNode | ParserRuleContext = node;
Expand Down Expand Up @@ -176,14 +170,7 @@ abstract class SemanticContextCollector {
}

visitTerminal(node: TerminalNode): void {
if (node.symbol.tokenIndex !== this._tokenIndex) return;
if (
this._prevTokenIndex &&
this._allTokens[this._prevTokenIndex].text === SQL_SPLIT_SYMBOL_TEXT
) {
this._isNewStatement = true;
return;
}
if (node.symbol.tokenIndex !== this._tokenIndex || this._isNewStatement) return;

let currentNode: TerminalNode | ParserRuleContext = node;
let parent = node.parent as ParserRuleContext | null;
Expand Down

0 comments on commit b29d058

Please sign in to comment.