Skip to content

Commit 55b040e

Browse files
authored
parser: fix crash with trailing comma (#551)
fix crash with trailing comma: ```sql select a, from t; ``` would get stuck at the comma, now we report an error: ``` error[syntax-error]: unexpected trailing comma --> stdin:1:9 | 1 | select a, from t; | ^ | ```
1 parent 0226555 commit 55b040e

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

crates/squawk_parser/src/grammar.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4510,6 +4510,8 @@ const TARGET_FOLLOW: TokenSet = TokenSet::new(&[
45104510
R_PAREN,
45114511
R_BRACK,
45124512
RETURNING_KW,
4513+
SEMICOLON,
4514+
EOF,
45134515
])
45144516
.union(COMPOUND_SELECT_FIRST);
45154517

@@ -4567,7 +4569,7 @@ fn opt_target_list(p: &mut Parser) -> Option<CompletedMarker> {
45674569
let m = p.start();
45684570
while !p.at(EOF) && !p.at(SEMICOLON) {
45694571
if opt_target_el(p).is_some() {
4570-
if p.at(COMMA) && matches!(p.nth(1), SEMICOLON | EOF) {
4572+
if p.at(COMMA) && p.nth_at_ts(1, TARGET_FOLLOW) {
45714573
p.err_and_bump("unexpected trailing comma");
45724574
break;
45734575
}
@@ -4584,6 +4586,8 @@ fn opt_target_list(p: &mut Parser) -> Option<CompletedMarker> {
45844586
break;
45854587
}
45864588
}
4589+
} else {
4590+
break;
45874591
}
45884592
}
45894593
Some(m.complete(p, TARGET_LIST))

crates/squawk_parser/tests/data/err/select.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,8 @@ select numeric 1234;
2929
-- warns about a missing semicolon
3030
select select;
3131

32+
-- extra comma
33+
select a, from t;
34+
3235
-- trailing comma at EOF
3336
select 1,

crates/squawk_parser/tests/snapshots/tests__select_err.snap

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,27 @@ SOURCE_FILE
223223
SELECT_KW "select"
224224
SEMICOLON ";"
225225
WHITESPACE "\n\n"
226+
COMMENT "-- extra comma"
227+
WHITESPACE "\n"
228+
SELECT
229+
SELECT_CLAUSE
230+
SELECT_KW "select"
231+
WHITESPACE " "
232+
TARGET_LIST
233+
TARGET
234+
NAME_REF
235+
IDENT "a"
236+
ERROR
237+
COMMA ","
238+
WHITESPACE " "
239+
FROM_CLAUSE
240+
FROM_KW "from"
241+
WHITESPACE " "
242+
FROM_ITEM
243+
NAME_REF
244+
IDENT "t"
245+
SEMICOLON ";"
246+
WHITESPACE "\n\n"
226247
COMMENT "-- trailing comma at EOF"
227248
WHITESPACE "\n"
228249
SELECT
@@ -248,4 +269,5 @@ ERROR@396: expected expression
248269
ERROR@397: expected expression
249270
ERROR@520: missing comma
250271
ERROR@646: expected SEMICOLON
251-
ERROR@689: unexpected trailing comma
272+
ERROR@679: unexpected trailing comma
273+
ERROR@723: unexpected trailing comma

0 commit comments

Comments
 (0)