Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 40 additions & 59 deletions crates/squawk_parser/src/grammar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1654,10 +1654,10 @@ fn json_key_value(p: &mut Parser<'_>, lhs: CompletedMarker) -> CompletedMarker {
}

fn named_arg(p: &mut Parser<'_>, lhs: CompletedMarker) -> CompletedMarker {
assert!(p.at(FAT_ARROW) || p.at(COLONEQ));
assert!(p.at(FAT_ARROW) || p.at(COLON_EQ));
let m = lhs.precede(p);
if p.at(COLONEQ) {
p.bump(COLONEQ);
if p.at(COLON_EQ) {
p.bump(COLON_EQ);
} else {
p.bump(FAT_ARROW);
}
Expand All @@ -1668,9 +1668,9 @@ fn named_arg(p: &mut Parser<'_>, lhs: CompletedMarker) -> CompletedMarker {
}

fn cast_expr(p: &mut Parser<'_>, lhs: CompletedMarker) -> CompletedMarker {
assert!(p.at(COLON2));
assert!(p.at(COLON_COLON));
let m = lhs.precede(p);
p.bump(COLON2);
p.bump(COLON_COLON);
type_name(p);
m.complete(p, CAST_EXPR)
}
Expand Down Expand Up @@ -2089,9 +2089,9 @@ fn current_op(p: &Parser<'_>, r: &Restrictions) -> (u8, SyntaxKind, Associativit
MINUS if p.next_not_joined_op(0) => (8, MINUS, Left), // symbol
// Later on we return a NAMED_ARG for this instead of BIN_EXPR
// :=
COLON if p.at(COLONEQ) => (5, COLONEQ, Right), // symbol
COLON if p.at(COLON_EQ) => (5, COLON_EQ, Right), // symbol
// ::
COLON if p.at(COLON2) => (15, COLON2, Left), // symbol
COLON if p.at(COLON_COLON) => (15, COLON_COLON, Left), // symbol
// Only used in json_object, like json_object('a' value 1) instead of json_object('a': 1)
// value
VALUE_KW if r.json_field_arg_allowed => (7, VALUE_KW, Right),
Expand Down Expand Up @@ -2159,11 +2159,11 @@ fn expr_bp(p: &mut Parser<'_>, bp: u8, r: &Restrictions) -> Option<CompletedMark
break;
}
match op {
COLON2 => {
COLON_COLON => {
lhs = cast_expr(p, lhs);
continue;
}
FAT_ARROW | COLONEQ => {
FAT_ARROW | COLON_EQ => {
lhs = named_arg(p, lhs);
continue;
}
Expand Down Expand Up @@ -2740,15 +2740,15 @@ fn data_source(p: &mut Parser<'_>) {

// USING data_source ON join_condition
fn merge_using_clause(p: &mut Parser<'_>) {
let m1 = p.start();
let m = p.start();
p.expect(USING_KW);
data_source(p);
p.expect(ON_KW);
// join_condition
if expr(p).is_none() {
p.error("expected an expression");
}
m1.complete(p, USING_CLAUSE);
m.complete(p, USING_CLAUSE);
}

// where from_item can be one of:
Expand Down Expand Up @@ -3119,8 +3119,7 @@ fn opt_with_params(p: &mut Parser<'_>) -> Option<CompletedMarker> {
// [ INCLUDE ( column_name [, ... ] ) ]
// [ WITH ( storage_parameter [= value] [, ... ] ) ]
// [ USING INDEX TABLESPACE tablespace_name ]
#[must_use]
fn opt_index_parameters(p: &mut Parser<'_>) -> bool {
fn opt_index_parameters(p: &mut Parser<'_>) {
opt_include_columns(p);
opt_with_params(p);
if p.at(USING_KW) {
Expand All @@ -3131,7 +3130,6 @@ fn opt_index_parameters(p: &mut Parser<'_>) -> bool {
name_ref(p);
m.complete(p, CONSTRAINT_INDEX_TABLESPACE);
}
true
}

// referential_action in a FOREIGN KEY/REFERENCES constraint is:
Expand Down Expand Up @@ -3293,18 +3291,14 @@ fn opt_constraint_inner(p: &mut Parser<'_>) -> Option<SyntaxKind> {
p.eat(NOT_KW);
p.expect(DISTINCT_KW);
}
if !opt_index_parameters(p) {
p.error("expected index parameters");
}
opt_index_parameters(p);
UNIQUE_CONSTRAINT
}
// PRIMARY KEY index_parameters
PRIMARY_KW => {
p.bump(PRIMARY_KW);
p.expect(KEY_KW);
if !opt_index_parameters(p) {
p.error("expected index parameters");
}
opt_index_parameters(p);
PRIMARY_KEY_CONSTRAINT
}
// REFERENCES reftable [ ( refcolumn ) ] [ MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ]
Expand Down Expand Up @@ -3506,9 +3500,7 @@ fn table_constraint(p: &mut Parser<'_>) -> CompletedMarker {
p.eat(DISTINCT_KW);
}
column_list(p);
if !opt_index_parameters(p) {
p.error("expected index parameters");
}
opt_index_parameters(p);
}
UNIQUE_CONSTRAINT
}
Expand All @@ -3523,9 +3515,7 @@ fn table_constraint(p: &mut Parser<'_>) -> CompletedMarker {
// ( column_name [, ... ] ) index_parameters
} else {
column_list(p);
if !opt_index_parameters(p) {
p.error("expected index parameters");
}
opt_index_parameters(p);
}
PRIMARY_KEY_CONSTRAINT
}
Expand Down Expand Up @@ -3553,9 +3543,7 @@ fn table_constraint(p: &mut Parser<'_>) -> CompletedMarker {
}
p.expect(R_PAREN);
m.complete(p, CONSTRAINT_EXCLUSIONS);
if !opt_index_parameters(p) {
p.error("expected index parameters");
}
opt_index_parameters(p);
if p.at(WHERE_KW) {
let m = p.start();
p.bump(WHERE_KW);
Expand Down Expand Up @@ -3637,7 +3625,7 @@ fn opt_initally_constraint_option(p: &mut Parser<'_>) -> Option<CompletedMarker>
(INITIALLY_KW, DEFERRED_KW) => {
p.bump(INITIALLY_KW);
p.bump(DEFERRED_KW);
INITALLY_DEFERRED_CONSTRAINT_OPTION
INITIALLY_DEFERRED_CONSTRAINT_OPTION
}
(INITIALLY_KW, IMMEDIATE_KW) => {
p.bump(INITIALLY_KW);
Expand Down Expand Up @@ -3665,7 +3653,7 @@ fn opt_constraint_options(p: &mut Parser<'_>) {
}
(Some(deferrable), Some(initially)) => {
if deferrable.kind() == NOT_DEFERRABLE_CONSTRAINT_OPTION
&& initially.kind() == INITALLY_DEFERRED_CONSTRAINT_OPTION
&& initially.kind() == INITIALLY_DEFERRED_CONSTRAINT_OPTION
{
p.error("constraint declared INITIALLY DEFERRED must be DEFERRABLE");
}
Expand Down Expand Up @@ -4176,9 +4164,8 @@ fn opt_target_el(p: &mut Parser) -> Option<CompletedMarker> {
return None;
} else if p.at(STAR) && !p.nth_at_ts(1, OPERATOR_FIRST) {
p.bump(STAR);
true
} else if expr(p).is_some() {
opt_as_col_label(p) || p.at(COMMA)
opt_as_col_label(p);
} else {
m.abandon(p);
p.error(format!(
Expand Down Expand Up @@ -4562,7 +4549,7 @@ fn commit_stmt(p: &mut Parser<'_>) -> CompletedMarker {
p.expect(CHAIN_KW);
}
}
m.complete(p, COMMIT_STMT)
m.complete(p, COMMIT)
}

const TRANSACTION_MODE_FIRST: TokenSet =
Expand Down Expand Up @@ -4635,7 +4622,7 @@ fn begin_stmt(p: &mut Parser<'_>) -> CompletedMarker {
p.expect(TRANSACTION_KW);
opt_transaction_mode_list(p);
}
m.complete(p, BEGIN_STMT)
m.complete(p, BEGIN)
}

// Sconst
Expand Down Expand Up @@ -9179,7 +9166,7 @@ fn drop_aggregate_stmt(p: &mut Parser<'_>) -> CompletedMarker {
aggregate(p);
}
opt_cascade_or_restrict(p);
m.complete(p, DROP_AGGREGATE_STMT)
m.complete(p, DROP_AGGREGATE)
}

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

fn transaction_mode_list(p: &mut Parser<'_>) {
// TODO: generalize
// transaction_mode [, ...]
while !p.at(EOF) && p.at_ts(TRANSACTION_MODE_FIRST) {
if !opt_transaction_mode(p) {
p.error("expected transaction mode");
}
// historical pg syntax doesn't require commas
p.eat(COMMA);
}
}

// SET TRANSACTION transaction_mode [, ...]
// SET TRANSACTION SNAPSHOT snapshot_id
// SET SESSION CHARACTERISTICS AS TRANSACTION transaction_mode [, ...]
Expand All @@ -10570,32 +10569,14 @@ fn set_transaction_stmt(p: &mut Parser<'_>) -> CompletedMarker {
p.expect(CHARACTERISTICS_KW);
p.expect(AS_KW);
p.expect(TRANSACTION_KW);
// TODO: generalize
// transaction_mode [, ...]
while !p.at(EOF) {
if !opt_transaction_mode(p) {
p.error("expected transaction mode");
}
if !p.eat(COMMA) {
break;
}
}
transaction_mode_list(p);
} else {
p.expect(TRANSACTION_KW);
// [ SNAPSHOT snapshot_id ]
if p.eat(SNAPSHOT_KW) {
string_literal(p);
} else {
// TODO: generalize
// transaction_mode [, ...]
while !p.at(EOF) {
if !opt_transaction_mode(p) {
break;
}
if !p.eat(COMMA) {
break;
}
}
transaction_mode_list(p);
}
}
m.complete(p, SET_TRANSACTION_STMT)
Expand Down Expand Up @@ -12017,7 +11998,7 @@ fn create_index_stmt(p: &mut Parser<'_>) -> CompletedMarker {
}
// [ WHERE predicate ]
opt_where_clause(p);
m.complete(p, CREATE_INDEX_STMT)
m.complete(p, CREATE_INDEX)
}

// (
Expand Down Expand Up @@ -12060,7 +12041,7 @@ fn opt_param_mode(p: &mut Parser<'_>) -> Option<CompletedMarker> {
IN_KW => {
p.bump(IN_KW);
if p.eat(OUT_KW) {
PARAM_INOUT
PARAM_IN_OUT
} else {
PARAM_IN
}
Expand All @@ -12071,7 +12052,7 @@ fn opt_param_mode(p: &mut Parser<'_>) -> Option<CompletedMarker> {
}
INOUT_KW => {
p.bump(INOUT_KW);
PARAM_INOUT
PARAM_IN_OUT
}
_ => {
m.abandon(p);
Expand Down
8 changes: 4 additions & 4 deletions crates/squawk_parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,8 @@ impl<'t> Parser<'t> {
return false;
}
let n_raw_tokens = match kind {
SyntaxKind::COLON2
| SyntaxKind::COLONEQ
SyntaxKind::COLON_COLON
| SyntaxKind::COLON_EQ
| SyntaxKind::NEQ
| SyntaxKind::NEQB
| SyntaxKind::LTEQ
Expand Down Expand Up @@ -508,14 +508,14 @@ impl<'t> Parser<'t> {
TrivaBetween::NotAllowed,
),
// :=
SyntaxKind::COLONEQ => self.at_composite2(
SyntaxKind::COLON_EQ => self.at_composite2(
n,
SyntaxKind::COLON,
SyntaxKind::EQ,
TrivaBetween::NotAllowed,
),
// ::
SyntaxKind::COLON2 => self.at_composite2(
SyntaxKind::COLON_COLON => self.at_composite2(
n,
SyntaxKind::COLON,
SyntaxKind::COLON,
Expand Down
16 changes: 8 additions & 8 deletions crates/squawk_parser/src/syntax_kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ pub enum SyntaxKind {
/// `:`
COLON,
/// `::`
COLON2,
COLON_COLON,
/// `:=`
COLONEQ,
COLON_EQ,
/// `=`
EQ,
/// `=>`
Expand Down Expand Up @@ -1212,7 +1212,7 @@ pub enum SyntaxKind {
ANALYZE_STMT,
CLUSTER_STMT,
COMMENT_STMT,
COMMIT_STMT,
COMMIT,
CREATE_EXTENSION_STMT,
CREATE_ACCESS_METHOD_STMT,
CREATE_AGGREGATE_STMT,
Expand Down Expand Up @@ -1246,14 +1246,14 @@ pub enum SyntaxKind {
CREATE_TEXT_SEARCH_PARSER_STMT,
CREATE_TEXT_SEARCH_TEMPLATE_STMT,
CREATE_TRANSFORM_STMT,
CREATE_INDEX_STMT,
CREATE_INDEX,
CREATE_TYPE_STMT,
CREATE_TRIGGER_STMT,
CREATE_FUNCTION_STMT,
PARAM,
PARAM_IN,
PARAM_OUT,
PARAM_INOUT,
PARAM_IN_OUT,
PARAM_VARIADIC,
BEGIN_FUNC_OPTION,
RETURN_FUNC_OPTION,
Expand All @@ -1278,7 +1278,7 @@ pub enum SyntaxKind {
OR_REPLACE,
DROP_INDEX_STMT,
DROP_TRIGGER_STMT,
BEGIN_STMT,
BEGIN,
SHOW_STMT,
SET_STMT,
PREPARE_TRANSACTION_STMT,
Expand Down Expand Up @@ -1339,7 +1339,7 @@ pub enum SyntaxKind {
DROP_CONVERSION_STMT,
DROP_COLLATION_STMT,
DROP_CAST_STMT,
DROP_AGGREGATE_STMT,
DROP_AGGREGATE,
DROP_ACCESS_METHOD_STMT,
DROP_USER_MAPPING_STMT,
IMPORT_FOREIGN_SCHEMA,
Expand Down Expand Up @@ -1407,7 +1407,7 @@ pub enum SyntaxKind {
CONSTRAINT_EXCLUSIONS,
DEFERRABLE_CONSTRAINT_OPTION,
NOT_DEFERRABLE_CONSTRAINT_OPTION,
INITALLY_DEFERRED_CONSTRAINT_OPTION,
INITIALLY_DEFERRED_CONSTRAINT_OPTION,
INITIALLY_IMMEDIATE_CONSTRAINT_OPTION,
CONSTRAINT_OPTION_LIST,
SEQUENCE_OPTION_LIST,
Expand Down
5 changes: 4 additions & 1 deletion crates/squawk_parser/tests/data/ok/set_transaction.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@ SET TRANSACTION SNAPSHOT '00000003-0000001B-1';

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

SET TRANSACTION ISOLATION LEVEL SERIALIZABLE, READ WRITE, NOT DEFERRABLE
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE, READ WRITE, NOT DEFERRABLE;


-- no commas is postgres historical according to gram.y
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE READ WRITE NOT DEFERRABLE;
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
source: crates/squawk_parser/tests/tests.rs
input_file: crates/squawk_parser/tests/data/ok/alter_aggregate.sql
snapshot_kind: text
---
SOURCE_FILE
COMMENT "-- star"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
source: crates/squawk_parser/tests/tests.rs
input_file: crates/squawk_parser/tests/data/ok/alter_collation.sql
snapshot_kind: text
---
SOURCE_FILE
COMMENT "-- refresh"
Expand Down
Loading
Loading