Skip to content

Commit 274ba82

Browse files
authored
parser: fix last of pg regression suite, strings (#543)
1 parent cdd89b1 commit 274ba82

File tree

11 files changed

+315
-68
lines changed

11 files changed

+315
-68
lines changed

crates/squawk_parser/src/generated/syntax_kind.rs

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/squawk_parser/src/grammar.rs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,15 @@ fn literal(p: &mut Parser<'_>) -> Option<CompletedMarker> {
2727
return None;
2828
}
2929
let m = p.start();
30+
if p.eat(BYTE_STRING) {
31+
if p.eat(UESCAPE_KW) {
32+
p.eat(STRING);
33+
}
34+
}
3035
// E021-03 string continuation syntax
3136
// If two string literals are next to each other, and don't have a comment
3237
// between them, then they are automatically combined.
33-
if p.eat(STRING) {
38+
else if p.eat(STRING) {
3439
while !p.at(EOF) && p.eat(STRING) {}
3540
} else {
3641
p.bump_any();
@@ -362,10 +367,6 @@ fn substring_fn(p: &mut Parser<'_>) -> CompletedMarker {
362367
if expr(p).is_none() {
363368
p.error("expected an expression");
364369
}
365-
p.expect(ESCAPE_KW);
366-
if expr(p).is_none() {
367-
p.error("expected an expression");
368-
}
369370
}
370371
_ if p.eat(COMMA) => {
371372
// normal function call
@@ -1462,7 +1463,13 @@ fn opt_name(p: &mut Parser<'_>) -> Option<CompletedMarker> {
14621463
return None;
14631464
}
14641465
let m = p.start();
1465-
p.bump_any();
1466+
if p.eat(IDENT) {
1467+
if p.eat(UESCAPE_KW) {
1468+
p.expect(STRING);
1469+
}
1470+
} else {
1471+
p.bump_any();
1472+
}
14661473
Some(m.complete(p, NAME))
14671474
}
14681475

@@ -2156,10 +2163,14 @@ fn current_op(p: &Parser<'_>, r: &Restrictions) -> (u8, SyntaxKind, Associativit
21562163
PLUS if p.next_not_joined_op(0) => (8, PLUS, Left), // symbol
21572164
// overlaps
21582165
OVERLAPS_KW => (7, OVERLAPS_KW, Left),
2166+
// escape
2167+
ESCAPE_KW => (7, ESCAPE_KW, Left),
21592168
// like
21602169
LIKE_KW => (6, LIKE_KW, Left),
21612170
// ilike
21622171
ILIKE_KW => (6, ILIKE_KW, Left),
2172+
// not similar to
2173+
NOT_KW if !r.not_disabled && p.at(NOT_SIMILAR_TO) => (6, NOT_SIMILAR_TO, Left),
21632174
// not like
21642175
NOT_KW if !r.not_disabled && p.at(NOT_LIKE) => (6, NOT_LIKE, Left),
21652176
// not ilike
@@ -13727,7 +13738,13 @@ fn alter_table_action(p: &mut Parser<'_>) -> Option<SyntaxKind> {
1372713738
fn opt_col_label(p: &mut Parser<'_>) -> bool {
1372813739
if p.at_ts(COL_LABEL_FIRST) {
1372913740
let m = p.start();
13730-
p.bump_any();
13741+
if p.eat(IDENT) {
13742+
if p.eat(UESCAPE_KW) {
13743+
p.expect(STRING);
13744+
}
13745+
} else {
13746+
p.bump_any();
13747+
}
1373113748
m.complete(p, NAME);
1373213749
true
1373313750
} else {

crates/squawk_parser/src/lib.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,14 @@ impl<'t> Parser<'t> {
343343
m.complete(self, SyntaxKind::IS_JSON_SCALAR);
344344
return true;
345345
}
346+
SyntaxKind::NOT_SIMILAR_TO => {
347+
let m = self.start();
348+
self.bump(SyntaxKind::NOT_KW);
349+
self.bump(SyntaxKind::SIMILAR_KW);
350+
self.bump(SyntaxKind::TO_KW);
351+
m.complete(self, SyntaxKind::NOT_SIMILAR_TO);
352+
return true;
353+
}
346354
SyntaxKind::IS_NOT_DISTINCT_FROM => {
347355
let m = self.start();
348356
self.bump(SyntaxKind::IS_KW);
@@ -767,6 +775,12 @@ impl<'t> Parser<'t> {
767775
}
768776
return false;
769777
}
778+
SyntaxKind::NOT_SIMILAR_TO => self.at_composite3(
779+
n,
780+
SyntaxKind::NOT_KW,
781+
SyntaxKind::SIMILAR_KW,
782+
SyntaxKind::TO_KW,
783+
),
770784
// similar to
771785
SyntaxKind::SIMILAR_TO => self.at_composite2(
772786
n,

crates/squawk_parser/tests/data/ok/select_operators.sql

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,17 @@ select U&'\0061\0308bc' is not nfd normalized;
180180
-- pattern_matching
181181
-- like
182182
select 'foo' like 'bar';
183-
183+
select 'foo' like 'bar' escape '#';
184184
-- not like
185185
select 'foo' not like 'bar';
186+
select 'foo' not like 'bar' escape '#';
187+
188+
-- ilike
189+
select 'foo' ilike 'bar';
190+
select 'foo' ilike 'bar' escape '#';
191+
-- not ilike
192+
select 'foo' not ilike 'bar';
193+
select 'foo' not ilike 'bar' escape '#';
186194

187195
-- ~~
188196
select 'a' ~~ 'b';
@@ -192,6 +200,10 @@ select 'a' !~~ 'b';
192200

193201
-- similar to
194202
select 'abc' similar to 'abc';
203+
select 'abc' similar to 'abc' escape '#';
204+
205+
select 'abc' not similar to 'abc';
206+
select 'abc' not similar to 'abc' escape '#';
195207

196208
-- posix regex
197209
-- string matches regex case sensitive

crates/squawk_parser/tests/data/regression_suite/strings.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ SELECT 'tricky' AS U&"\" UESCAPE '!';
2828
2929
SELECT U&'wrong: \061';
3030
SELECT U&'wrong: \+0061';
31-
SELECT U&'wrong: +0061' UESCAPE +;
31+
-- SELECT U&'wrong: +0061' UESCAPE +;
3232
SELECT U&'wrong: +0061' UESCAPE '+';
3333
3434
SELECT U&'wrong: \db99';

crates/squawk_parser/tests/snapshots/tests__regression_strings.snap

Lines changed: 1 addition & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2,52 +2,4 @@
22
source: crates/squawk_parser/tests/tests.rs
33
input_file: crates/squawk_parser/tests/data/regression_suite/strings.sql
44
---
5-
ERROR@536: missing comma
6-
ERROR@563: missing comma
7-
ERROR@625: missing comma
8-
ERROR@667: missing comma
9-
ERROR@763: missing comma
10-
ERROR@765: expected an expression, found SEMICOLON
11-
ERROR@798: missing comma
12-
ERROR@1460: missing comma
13-
ERROR@1487: missing comma
14-
ERROR@1523: missing comma
15-
ERROR@1565: missing comma
16-
ERROR@1661: missing comma
17-
ERROR@6313: missing comma
18-
ERROR@6369: missing comma
19-
ERROR@6621: missing comma
20-
ERROR@6717: missing comma
21-
ERROR@6775: missing comma
22-
ERROR@17083: missing comma
23-
ERROR@17136: missing comma
24-
ERROR@17188: missing comma
25-
ERROR@17242: missing comma
26-
ERROR@17354: missing comma
27-
ERROR@17403: missing comma
28-
ERROR@17455: missing comma
29-
ERROR@17510: missing comma
30-
ERROR@17562: missing comma
31-
ERROR@17617: missing comma
32-
ERROR@17675: missing comma
33-
ERROR@17735: missing comma
34-
ERROR@17787: missing comma
35-
ERROR@17841: missing comma
36-
ERROR@17894: missing comma
37-
ERROR@17949: missing comma
38-
ERROR@18003: missing comma
39-
ERROR@18060: missing comma
40-
ERROR@18112: missing comma
41-
ERROR@18167: missing comma
42-
ERROR@18230: missing comma
43-
ERROR@18302: missing comma
44-
ERROR@18406: missing comma
45-
ERROR@18459: missing comma
46-
ERROR@18511: missing comma
47-
ERROR@18565: missing comma
48-
ERROR@18616: missing comma
49-
ERROR@18669: missing comma
50-
ERROR@18721: missing comma
51-
ERROR@18775: missing comma
52-
ERROR@18827: missing comma
53-
ERROR@18882: missing comma
5+

crates/squawk_parser/tests/snapshots/tests__regression_suite_errors.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
source: crates/squawk_parser/tests/tests.rs
33
expression: "out.join(\"\\n\")"
44
---
5-
tests/snapshots/tests__regression_strings.snap:49
5+

crates/squawk_parser/tests/snapshots/tests__select_funcs_ok.snap

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -730,13 +730,14 @@ SOURCE_FILE
730730
WHITESPACE " "
731731
SIMILAR_KW "similar"
732732
WHITESPACE " "
733-
NAME_REF
734-
IDENT "b"
735-
WHITESPACE " "
736-
ESCAPE_KW "escape"
737-
WHITESPACE " "
738-
NAME_REF
739-
IDENT "c"
733+
BIN_EXPR
734+
NAME_REF
735+
IDENT "b"
736+
WHITESPACE " "
737+
ESCAPE_KW "escape"
738+
WHITESPACE " "
739+
NAME_REF
740+
IDENT "c"
740741
R_PAREN ")"
741742
SEMICOLON ";"
742743
WHITESPACE "\n\n"

0 commit comments

Comments
 (0)