1
- use pgt_lexer:: { SyntaxKind , Token , TokenType } ;
1
+ use pgt_lexer:: { SyntaxKind , Token , TokenType , WHITESPACE_TOKENS } ;
2
2
3
3
use super :: {
4
4
Parser ,
@@ -24,6 +24,12 @@ pub fn source(p: &mut Parser) {
24
24
} => {
25
25
p. advance ( ) ;
26
26
}
27
+ Token {
28
+ kind : SyntaxKind :: Ascii92 ,
29
+ ..
30
+ } => {
31
+ plpgsql_command ( p) ;
32
+ }
27
33
_ => {
28
34
statement ( p) ;
29
35
}
@@ -87,6 +93,24 @@ pub(crate) fn parenthesis(p: &mut Parser) {
87
93
}
88
94
}
89
95
96
+ pub ( crate ) fn plpgsql_command ( p : & mut Parser ) {
97
+ p. expect ( SyntaxKind :: Ascii92 ) ;
98
+
99
+ loop {
100
+ match p. current ( ) . kind {
101
+ SyntaxKind :: Newline => {
102
+ p. advance ( ) ;
103
+ break ;
104
+ }
105
+ _ => {
106
+ // advance the parser to the next token without ignoring irrelevant tokens
107
+ // we would skip a newline with `advance()`
108
+ p. current_pos += 1 ;
109
+ }
110
+ }
111
+ }
112
+ }
113
+
90
114
pub ( crate ) fn case ( p : & mut Parser ) {
91
115
p. expect ( SyntaxKind :: Case ) ;
92
116
@@ -125,6 +149,36 @@ pub(crate) fn unknown(p: &mut Parser, exclude: &[SyntaxKind]) {
125
149
} => {
126
150
case ( p) ;
127
151
}
152
+ Token {
153
+ kind : SyntaxKind :: Ascii92 ,
154
+ ..
155
+ } => {
156
+ // pgsql commands e.g.
157
+ //
158
+ // ```
159
+ // \if test
160
+ // ```
161
+ //
162
+ // we wait for "\" and check if the previous token is a newline
163
+
164
+ // newline is a whitespace, but we do not want to ignore it here
165
+ let irrelevant = WHITESPACE_TOKENS
166
+ . iter ( )
167
+ . filter ( |t| * * t != SyntaxKind :: Newline )
168
+ . collect :: < Vec < _ > > ( ) ;
169
+
170
+ // go back from the current position without ignoring irrelevant tokens
171
+ if p. tokens
172
+ . iter ( )
173
+ . take ( p. current_pos )
174
+ . rev ( )
175
+ . find ( |t| !irrelevant. contains ( & & t. kind ) )
176
+ . is_some_and ( |t| t. kind == SyntaxKind :: Newline )
177
+ {
178
+ break ;
179
+ }
180
+ p. advance ( ) ;
181
+ }
128
182
Token {
129
183
kind : SyntaxKind :: Ascii40 ,
130
184
..
0 commit comments