Skip to content

Commit 35e0876

Browse files
authored
parser: clean & set transaction historical syntax (#491)
Various renames to prepare for the codegen'd stuff. AST node names need to match their SyntaxKind names. Also added a test case and fixed parsing historical syntax for set transaction that I found when looking through the pg gram.y Also delete a dead test helper
1 parent 50f1437 commit 35e0876

File tree

196 files changed

+336
-572
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

196 files changed

+336
-572
lines changed

crates/squawk_parser/src/grammar.rs

Lines changed: 40 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1654,10 +1654,10 @@ fn json_key_value(p: &mut Parser<'_>, lhs: CompletedMarker) -> CompletedMarker {
16541654
}
16551655

16561656
fn named_arg(p: &mut Parser<'_>, lhs: CompletedMarker) -> CompletedMarker {
1657-
assert!(p.at(FAT_ARROW) || p.at(COLONEQ));
1657+
assert!(p.at(FAT_ARROW) || p.at(COLON_EQ));
16581658
let m = lhs.precede(p);
1659-
if p.at(COLONEQ) {
1660-
p.bump(COLONEQ);
1659+
if p.at(COLON_EQ) {
1660+
p.bump(COLON_EQ);
16611661
} else {
16621662
p.bump(FAT_ARROW);
16631663
}
@@ -1668,9 +1668,9 @@ fn named_arg(p: &mut Parser<'_>, lhs: CompletedMarker) -> CompletedMarker {
16681668
}
16691669

16701670
fn cast_expr(p: &mut Parser<'_>, lhs: CompletedMarker) -> CompletedMarker {
1671-
assert!(p.at(COLON2));
1671+
assert!(p.at(COLON_COLON));
16721672
let m = lhs.precede(p);
1673-
p.bump(COLON2);
1673+
p.bump(COLON_COLON);
16741674
type_name(p);
16751675
m.complete(p, CAST_EXPR)
16761676
}
@@ -2089,9 +2089,9 @@ fn current_op(p: &Parser<'_>, r: &Restrictions) -> (u8, SyntaxKind, Associativit
20892089
MINUS if p.next_not_joined_op(0) => (8, MINUS, Left), // symbol
20902090
// Later on we return a NAMED_ARG for this instead of BIN_EXPR
20912091
// :=
2092-
COLON if p.at(COLONEQ) => (5, COLONEQ, Right), // symbol
2092+
COLON if p.at(COLON_EQ) => (5, COLON_EQ, Right), // symbol
20932093
// ::
2094-
COLON if p.at(COLON2) => (15, COLON2, Left), // symbol
2094+
COLON if p.at(COLON_COLON) => (15, COLON_COLON, Left), // symbol
20952095
// Only used in json_object, like json_object('a' value 1) instead of json_object('a': 1)
20962096
// value
20972097
VALUE_KW if r.json_field_arg_allowed => (7, VALUE_KW, Right),
@@ -2159,11 +2159,11 @@ fn expr_bp(p: &mut Parser<'_>, bp: u8, r: &Restrictions) -> Option<CompletedMark
21592159
break;
21602160
}
21612161
match op {
2162-
COLON2 => {
2162+
COLON_COLON => {
21632163
lhs = cast_expr(p, lhs);
21642164
continue;
21652165
}
2166-
FAT_ARROW | COLONEQ => {
2166+
FAT_ARROW | COLON_EQ => {
21672167
lhs = named_arg(p, lhs);
21682168
continue;
21692169
}
@@ -2740,15 +2740,15 @@ fn data_source(p: &mut Parser<'_>) {
27402740

27412741
// USING data_source ON join_condition
27422742
fn merge_using_clause(p: &mut Parser<'_>) {
2743-
let m1 = p.start();
2743+
let m = p.start();
27442744
p.expect(USING_KW);
27452745
data_source(p);
27462746
p.expect(ON_KW);
27472747
// join_condition
27482748
if expr(p).is_none() {
27492749
p.error("expected an expression");
27502750
}
2751-
m1.complete(p, USING_CLAUSE);
2751+
m.complete(p, USING_CLAUSE);
27522752
}
27532753

27542754
// where from_item can be one of:
@@ -3119,8 +3119,7 @@ fn opt_with_params(p: &mut Parser<'_>) -> Option<CompletedMarker> {
31193119
// [ INCLUDE ( column_name [, ... ] ) ]
31203120
// [ WITH ( storage_parameter [= value] [, ... ] ) ]
31213121
// [ USING INDEX TABLESPACE tablespace_name ]
3122-
#[must_use]
3123-
fn opt_index_parameters(p: &mut Parser<'_>) -> bool {
3122+
fn opt_index_parameters(p: &mut Parser<'_>) {
31243123
opt_include_columns(p);
31253124
opt_with_params(p);
31263125
if p.at(USING_KW) {
@@ -3131,7 +3130,6 @@ fn opt_index_parameters(p: &mut Parser<'_>) -> bool {
31313130
name_ref(p);
31323131
m.complete(p, CONSTRAINT_INDEX_TABLESPACE);
31333132
}
3134-
true
31353133
}
31363134

31373135
// referential_action in a FOREIGN KEY/REFERENCES constraint is:
@@ -3293,18 +3291,14 @@ fn opt_constraint_inner(p: &mut Parser<'_>) -> Option<SyntaxKind> {
32933291
p.eat(NOT_KW);
32943292
p.expect(DISTINCT_KW);
32953293
}
3296-
if !opt_index_parameters(p) {
3297-
p.error("expected index parameters");
3298-
}
3294+
opt_index_parameters(p);
32993295
UNIQUE_CONSTRAINT
33003296
}
33013297
// PRIMARY KEY index_parameters
33023298
PRIMARY_KW => {
33033299
p.bump(PRIMARY_KW);
33043300
p.expect(KEY_KW);
3305-
if !opt_index_parameters(p) {
3306-
p.error("expected index parameters");
3307-
}
3301+
opt_index_parameters(p);
33083302
PRIMARY_KEY_CONSTRAINT
33093303
}
33103304
// REFERENCES reftable [ ( refcolumn ) ] [ MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ]
@@ -3506,9 +3500,7 @@ fn table_constraint(p: &mut Parser<'_>) -> CompletedMarker {
35063500
p.eat(DISTINCT_KW);
35073501
}
35083502
column_list(p);
3509-
if !opt_index_parameters(p) {
3510-
p.error("expected index parameters");
3511-
}
3503+
opt_index_parameters(p);
35123504
}
35133505
UNIQUE_CONSTRAINT
35143506
}
@@ -3523,9 +3515,7 @@ fn table_constraint(p: &mut Parser<'_>) -> CompletedMarker {
35233515
// ( column_name [, ... ] ) index_parameters
35243516
} else {
35253517
column_list(p);
3526-
if !opt_index_parameters(p) {
3527-
p.error("expected index parameters");
3528-
}
3518+
opt_index_parameters(p);
35293519
}
35303520
PRIMARY_KEY_CONSTRAINT
35313521
}
@@ -3553,9 +3543,7 @@ fn table_constraint(p: &mut Parser<'_>) -> CompletedMarker {
35533543
}
35543544
p.expect(R_PAREN);
35553545
m.complete(p, CONSTRAINT_EXCLUSIONS);
3556-
if !opt_index_parameters(p) {
3557-
p.error("expected index parameters");
3558-
}
3546+
opt_index_parameters(p);
35593547
if p.at(WHERE_KW) {
35603548
let m = p.start();
35613549
p.bump(WHERE_KW);
@@ -3637,7 +3625,7 @@ fn opt_initally_constraint_option(p: &mut Parser<'_>) -> Option<CompletedMarker>
36373625
(INITIALLY_KW, DEFERRED_KW) => {
36383626
p.bump(INITIALLY_KW);
36393627
p.bump(DEFERRED_KW);
3640-
INITALLY_DEFERRED_CONSTRAINT_OPTION
3628+
INITIALLY_DEFERRED_CONSTRAINT_OPTION
36413629
}
36423630
(INITIALLY_KW, IMMEDIATE_KW) => {
36433631
p.bump(INITIALLY_KW);
@@ -3665,7 +3653,7 @@ fn opt_constraint_options(p: &mut Parser<'_>) {
36653653
}
36663654
(Some(deferrable), Some(initially)) => {
36673655
if deferrable.kind() == NOT_DEFERRABLE_CONSTRAINT_OPTION
3668-
&& initially.kind() == INITALLY_DEFERRED_CONSTRAINT_OPTION
3656+
&& initially.kind() == INITIALLY_DEFERRED_CONSTRAINT_OPTION
36693657
{
36703658
p.error("constraint declared INITIALLY DEFERRED must be DEFERRABLE");
36713659
}
@@ -4176,9 +4164,8 @@ fn opt_target_el(p: &mut Parser) -> Option<CompletedMarker> {
41764164
return None;
41774165
} else if p.at(STAR) && !p.nth_at_ts(1, OPERATOR_FIRST) {
41784166
p.bump(STAR);
4179-
true
41804167
} else if expr(p).is_some() {
4181-
opt_as_col_label(p) || p.at(COMMA)
4168+
opt_as_col_label(p);
41824169
} else {
41834170
m.abandon(p);
41844171
p.error(format!(
@@ -4562,7 +4549,7 @@ fn commit_stmt(p: &mut Parser<'_>) -> CompletedMarker {
45624549
p.expect(CHAIN_KW);
45634550
}
45644551
}
4565-
m.complete(p, COMMIT_STMT)
4552+
m.complete(p, COMMIT)
45664553
}
45674554

45684555
const TRANSACTION_MODE_FIRST: TokenSet =
@@ -4635,7 +4622,7 @@ fn begin_stmt(p: &mut Parser<'_>) -> CompletedMarker {
46354622
p.expect(TRANSACTION_KW);
46364623
opt_transaction_mode_list(p);
46374624
}
4638-
m.complete(p, BEGIN_STMT)
4625+
m.complete(p, BEGIN)
46394626
}
46404627

46414628
// Sconst
@@ -9179,7 +9166,7 @@ fn drop_aggregate_stmt(p: &mut Parser<'_>) -> CompletedMarker {
91799166
aggregate(p);
91809167
}
91819168
opt_cascade_or_restrict(p);
9182-
m.complete(p, DROP_AGGREGATE_STMT)
9169+
m.complete(p, DROP_AGGREGATE)
91839170
}
91849171

91859172
fn source_type_as_target_type(p: &mut Parser<'_>) {
@@ -10554,6 +10541,18 @@ fn set_session_auth_stmt(p: &mut Parser<'_>) -> CompletedMarker {
1055410541
m.complete(p, SET_SESSION_AUTH_STMT)
1055510542
}
1055610543

10544+
fn transaction_mode_list(p: &mut Parser<'_>) {
10545+
// TODO: generalize
10546+
// transaction_mode [, ...]
10547+
while !p.at(EOF) && p.at_ts(TRANSACTION_MODE_FIRST) {
10548+
if !opt_transaction_mode(p) {
10549+
p.error("expected transaction mode");
10550+
}
10551+
// historical pg syntax doesn't require commas
10552+
p.eat(COMMA);
10553+
}
10554+
}
10555+
1055710556
// SET TRANSACTION transaction_mode [, ...]
1055810557
// SET TRANSACTION SNAPSHOT snapshot_id
1055910558
// SET SESSION CHARACTERISTICS AS TRANSACTION transaction_mode [, ...]
@@ -10570,32 +10569,14 @@ fn set_transaction_stmt(p: &mut Parser<'_>) -> CompletedMarker {
1057010569
p.expect(CHARACTERISTICS_KW);
1057110570
p.expect(AS_KW);
1057210571
p.expect(TRANSACTION_KW);
10573-
// TODO: generalize
10574-
// transaction_mode [, ...]
10575-
while !p.at(EOF) {
10576-
if !opt_transaction_mode(p) {
10577-
p.error("expected transaction mode");
10578-
}
10579-
if !p.eat(COMMA) {
10580-
break;
10581-
}
10582-
}
10572+
transaction_mode_list(p);
1058310573
} else {
1058410574
p.expect(TRANSACTION_KW);
1058510575
// [ SNAPSHOT snapshot_id ]
1058610576
if p.eat(SNAPSHOT_KW) {
1058710577
string_literal(p);
1058810578
} else {
10589-
// TODO: generalize
10590-
// transaction_mode [, ...]
10591-
while !p.at(EOF) {
10592-
if !opt_transaction_mode(p) {
10593-
break;
10594-
}
10595-
if !p.eat(COMMA) {
10596-
break;
10597-
}
10598-
}
10579+
transaction_mode_list(p);
1059910580
}
1060010581
}
1060110582
m.complete(p, SET_TRANSACTION_STMT)
@@ -12017,7 +11998,7 @@ fn create_index_stmt(p: &mut Parser<'_>) -> CompletedMarker {
1201711998
}
1201811999
// [ WHERE predicate ]
1201912000
opt_where_clause(p);
12020-
m.complete(p, CREATE_INDEX_STMT)
12001+
m.complete(p, CREATE_INDEX)
1202112002
}
1202212003

1202312004
// (
@@ -12060,7 +12041,7 @@ fn opt_param_mode(p: &mut Parser<'_>) -> Option<CompletedMarker> {
1206012041
IN_KW => {
1206112042
p.bump(IN_KW);
1206212043
if p.eat(OUT_KW) {
12063-
PARAM_INOUT
12044+
PARAM_IN_OUT
1206412045
} else {
1206512046
PARAM_IN
1206612047
}
@@ -12071,7 +12052,7 @@ fn opt_param_mode(p: &mut Parser<'_>) -> Option<CompletedMarker> {
1207112052
}
1207212053
INOUT_KW => {
1207312054
p.bump(INOUT_KW);
12074-
PARAM_INOUT
12055+
PARAM_IN_OUT
1207512056
}
1207612057
_ => {
1207712058
m.abandon(p);

crates/squawk_parser/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,8 @@ impl<'t> Parser<'t> {
210210
return false;
211211
}
212212
let n_raw_tokens = match kind {
213-
SyntaxKind::COLON2
214-
| SyntaxKind::COLONEQ
213+
SyntaxKind::COLON_COLON
214+
| SyntaxKind::COLON_EQ
215215
| SyntaxKind::NEQ
216216
| SyntaxKind::NEQB
217217
| SyntaxKind::LTEQ
@@ -508,14 +508,14 @@ impl<'t> Parser<'t> {
508508
TrivaBetween::NotAllowed,
509509
),
510510
// :=
511-
SyntaxKind::COLONEQ => self.at_composite2(
511+
SyntaxKind::COLON_EQ => self.at_composite2(
512512
n,
513513
SyntaxKind::COLON,
514514
SyntaxKind::EQ,
515515
TrivaBetween::NotAllowed,
516516
),
517517
// ::
518-
SyntaxKind::COLON2 => self.at_composite2(
518+
SyntaxKind::COLON_COLON => self.at_composite2(
519519
n,
520520
SyntaxKind::COLON,
521521
SyntaxKind::COLON,

crates/squawk_parser/src/syntax_kind.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ pub enum SyntaxKind {
5656
/// `:`
5757
COLON,
5858
/// `::`
59-
COLON2,
59+
COLON_COLON,
6060
/// `:=`
61-
COLONEQ,
61+
COLON_EQ,
6262
/// `=`
6363
EQ,
6464
/// `=>`
@@ -1212,7 +1212,7 @@ pub enum SyntaxKind {
12121212
ANALYZE_STMT,
12131213
CLUSTER_STMT,
12141214
COMMENT_STMT,
1215-
COMMIT_STMT,
1215+
COMMIT,
12161216
CREATE_EXTENSION_STMT,
12171217
CREATE_ACCESS_METHOD_STMT,
12181218
CREATE_AGGREGATE_STMT,
@@ -1246,14 +1246,14 @@ pub enum SyntaxKind {
12461246
CREATE_TEXT_SEARCH_PARSER_STMT,
12471247
CREATE_TEXT_SEARCH_TEMPLATE_STMT,
12481248
CREATE_TRANSFORM_STMT,
1249-
CREATE_INDEX_STMT,
1249+
CREATE_INDEX,
12501250
CREATE_TYPE_STMT,
12511251
CREATE_TRIGGER_STMT,
12521252
CREATE_FUNCTION_STMT,
12531253
PARAM,
12541254
PARAM_IN,
12551255
PARAM_OUT,
1256-
PARAM_INOUT,
1256+
PARAM_IN_OUT,
12571257
PARAM_VARIADIC,
12581258
BEGIN_FUNC_OPTION,
12591259
RETURN_FUNC_OPTION,
@@ -1278,7 +1278,7 @@ pub enum SyntaxKind {
12781278
OR_REPLACE,
12791279
DROP_INDEX_STMT,
12801280
DROP_TRIGGER_STMT,
1281-
BEGIN_STMT,
1281+
BEGIN,
12821282
SHOW_STMT,
12831283
SET_STMT,
12841284
PREPARE_TRANSACTION_STMT,
@@ -1339,7 +1339,7 @@ pub enum SyntaxKind {
13391339
DROP_CONVERSION_STMT,
13401340
DROP_COLLATION_STMT,
13411341
DROP_CAST_STMT,
1342-
DROP_AGGREGATE_STMT,
1342+
DROP_AGGREGATE,
13431343
DROP_ACCESS_METHOD_STMT,
13441344
DROP_USER_MAPPING_STMT,
13451345
IMPORT_FOREIGN_SCHEMA,
@@ -1407,7 +1407,7 @@ pub enum SyntaxKind {
14071407
CONSTRAINT_EXCLUSIONS,
14081408
DEFERRABLE_CONSTRAINT_OPTION,
14091409
NOT_DEFERRABLE_CONSTRAINT_OPTION,
1410-
INITALLY_DEFERRED_CONSTRAINT_OPTION,
1410+
INITIALLY_DEFERRED_CONSTRAINT_OPTION,
14111411
INITIALLY_IMMEDIATE_CONSTRAINT_OPTION,
14121412
CONSTRAINT_OPTION_LIST,
14131413
SEQUENCE_OPTION_LIST,

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,8 @@ SET TRANSACTION SNAPSHOT '00000003-0000001B-1';
33

44
SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL READ COMMITTED, read write;
55

6-
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE, READ WRITE, NOT DEFERRABLE
6+
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE, READ WRITE, NOT DEFERRABLE;
77

8+
9+
-- no commas is postgres historical according to gram.y
10+
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE READ WRITE NOT DEFERRABLE;

crates/squawk_parser/tests/snapshots/tests__alter_aggregate_ok.snap

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
---
22
source: crates/squawk_parser/tests/tests.rs
33
input_file: crates/squawk_parser/tests/data/ok/alter_aggregate.sql
4-
snapshot_kind: text
54
---
65
SOURCE_FILE
76
COMMENT "-- star"

crates/squawk_parser/tests/snapshots/tests__alter_collation_ok.snap

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
---
22
source: crates/squawk_parser/tests/tests.rs
33
input_file: crates/squawk_parser/tests/data/ok/alter_collation.sql
4-
snapshot_kind: text
54
---
65
SOURCE_FILE
76
COMMENT "-- refresh"

0 commit comments

Comments
 (0)