1
- import { type Token , TokenType , isLogicalOperator } from 'src/lexer/token' ;
2
- import { AstNode , BetweenPredicate , NodeType , Parenthesis } from 'src/parser/ast' ;
1
+ import { TokenType , isLogicalOperator } from 'src/lexer/token' ;
2
+ import {
3
+ AstNode ,
4
+ BetweenPredicateNode ,
5
+ KeywordNode ,
6
+ NodeType ,
7
+ ParenthesisNode ,
8
+ } from 'src/parser/ast' ;
3
9
4
10
/**
5
11
* Bookkeeper for inline blocks.
@@ -15,11 +21,11 @@ export default class InlineBlock {
15
21
* Check if this should be an inline parentheses block
16
22
* Examples are "NOW()", "COUNT(*)", "int(10)", key(`somecolumn`), DECIMAL(7,2)
17
23
*/
18
- public isInlineBlock ( parenthesis : Parenthesis ) : boolean {
24
+ public isInlineBlock ( parenthesis : ParenthesisNode ) : boolean {
19
25
return this . inlineParenthesisWidth ( parenthesis ) <= this . expressionWidth ;
20
26
}
21
27
22
- private inlineParenthesisWidth ( parenthesis : Parenthesis ) : number {
28
+ private inlineParenthesisWidth ( parenthesis : ParenthesisNode ) : number {
23
29
// +2 for the two parenthesis
24
30
return this . inlineWidth ( parenthesis . children ) + 2 ;
25
31
}
@@ -30,10 +36,10 @@ export default class InlineBlock {
30
36
for ( const node of nodes ) {
31
37
switch ( node . type ) {
32
38
case NodeType . function_call :
33
- length += node . nameToken . text . length + this . inlineParenthesisWidth ( node . parenthesis ) ;
39
+ length += node . name . text . length + this . inlineParenthesisWidth ( node . parenthesis ) ;
34
40
break ;
35
41
case NodeType . array_subscript :
36
- length += node . arrayToken . text . length + this . inlineParenthesisWidth ( node . parenthesis ) ;
42
+ length += node . array . text . length + this . inlineParenthesisWidth ( node . parenthesis ) ;
37
43
break ;
38
44
case NodeType . parenthesis :
39
45
length += this . inlineParenthesisWidth ( node ) ;
@@ -44,13 +50,22 @@ export default class InlineBlock {
44
50
case NodeType . clause :
45
51
case NodeType . limit_clause :
46
52
case NodeType . set_operation :
53
+ case NodeType . line_comment :
54
+ case NodeType . block_comment :
47
55
return Infinity ;
48
56
case NodeType . all_columns_asterisk :
57
+ case NodeType . comma :
49
58
length += 1 ;
50
59
break ;
51
- case NodeType . token :
52
- length += node . token . text . length ;
53
- if ( this . isForbiddenToken ( node . token ) ) {
60
+ case NodeType . literal :
61
+ case NodeType . identifier :
62
+ case NodeType . parameter :
63
+ case NodeType . operator :
64
+ length += node . text . length ;
65
+ break ;
66
+ case NodeType . keyword :
67
+ length += node . text . length ;
68
+ if ( this . isForbiddenKeyword ( node ) ) {
54
69
return Infinity ;
55
70
}
56
71
break ;
@@ -64,23 +79,17 @@ export default class InlineBlock {
64
79
return length ;
65
80
}
66
81
67
- private betweenWidth ( node : BetweenPredicate ) : number {
82
+ private betweenWidth ( node : BetweenPredicateNode ) : number {
68
83
return (
69
- node . betweenToken . text . length +
84
+ node . between . text . length +
70
85
this . inlineWidth ( node . expr1 ) +
71
- node . andToken . text . length +
86
+ node . and . text . length +
72
87
this . inlineWidth ( node . expr2 )
73
88
) ;
74
89
}
75
90
76
- // Reserved words that cause newlines, comments and semicolons
77
- // are not allowed inside inline parentheses block
78
- private isForbiddenToken ( token : Token ) {
79
- return (
80
- isLogicalOperator ( token ) ||
81
- token . type === TokenType . LINE_COMMENT ||
82
- token . type === TokenType . BLOCK_COMMENT ||
83
- token . type === TokenType . CASE // CASE cannot have inline blocks
84
- ) ;
91
+ // Reserved words that cause newlines are not allowed inside inline parentheses block
92
+ private isForbiddenKeyword ( node : KeywordNode ) {
93
+ return isLogicalOperator ( node . tokenType ) || node . tokenType === TokenType . CASE ;
85
94
}
86
95
}
0 commit comments