Skip to content

Commit 06c7670

Browse files
authored
parser: fix parsing grant / revoke with schema specified names (#469)
previously ```sql GRANT ALL ON SEQUENCE public.s TO u; ``` wouldn't parse managed to de-dupe some logic between revoke & grant rel: #466
1 parent 6316385 commit 06c7670

File tree

6 files changed

+542
-272
lines changed

6 files changed

+542
-272
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ cargo xtask new-rule 'prefer big serial'
291291
2. Run `s/update-version`
292292
293293
```bash
294-
# update version in cli/Cargo.toml, package.json, flake.nix to 4.5.3
294+
# update version in squawk/Cargo.toml, package.json, flake.nix to 4.5.3
295295
s/update-version 4.5.3
296296
```
297297

crates/squawk_parser/src/grammar.rs

Lines changed: 100 additions & 169 deletions
Original file line numberDiff line numberDiff line change
@@ -6560,7 +6560,7 @@ fn alter_default_privileges_stmt(p: &mut Parser<'_>) -> CompletedMarker {
65606560
p.bump(GRANT_KW);
65616561
privileges(p);
65626562
p.expect(ON_KW);
6563-
privilege_target(p);
6563+
privilege_target_type(p);
65646564
p.expect(TO_KW);
65656565
role(p);
65666566
while !p.at(EOF) && p.eat(COMMA) {
@@ -6579,7 +6579,7 @@ fn alter_default_privileges_stmt(p: &mut Parser<'_>) -> CompletedMarker {
65796579
}
65806580
privileges(p);
65816581
p.expect(ON_KW);
6582-
privilege_target(p);
6582+
privilege_target_type(p);
65836583
p.expect(FROM_KW);
65846584
role(p);
65856585
while !p.at(EOF) && p.eat(COMMA) {
@@ -6594,7 +6594,7 @@ fn alter_default_privileges_stmt(p: &mut Parser<'_>) -> CompletedMarker {
65946594
m.complete(p, ALTER_DEFAULT_PRIVILEGES_STMT)
65956595
}
65966596

6597-
fn privilege_target(p: &mut Parser<'_>) {
6597+
fn privilege_target_type(p: &mut Parser<'_>) {
65986598
match p.current() {
65996599
TABLES_KW | FUNCTIONS_KW | ROUTINES_KW | SEQUENCES_KW | TYPES_KW | SCHEMAS_KW => {
66006600
p.bump_any();
@@ -10052,90 +10052,7 @@ fn grant_stmt(p: &mut Parser<'_>) -> CompletedMarker {
1005210052
// | ALL { FUNCTIONS | PROCEDURES | ROUTINES } IN SCHEMA schema_name [, ...] }
1005310053
// ON PARAMETER configuration_parameter [, ...]
1005410054
if p.eat(ON_KW) {
10055-
if p.eat(ALL_KW) {
10056-
match p.current() {
10057-
TABLES_KW | SEQUENCES_KW | FUNCTIONS_KW | PROCEDURES_KW | ROUTINES_KW => {
10058-
p.bump_any();
10059-
p.expect(IN_KW);
10060-
p.expect(SCHEMA_KW);
10061-
// schema_name [, ...]
10062-
name_ref(p);
10063-
while !p.at(EOF) && p.eat(COMMA) {
10064-
name_ref(p);
10065-
}
10066-
}
10067-
_ => p.error("expected TABLE"),
10068-
}
10069-
} else {
10070-
match p.current() {
10071-
PARAMETER_KW => {
10072-
p.bump(PARAMETER_KW);
10073-
name_ref(p);
10074-
while !p.at(EOF) && p.eat(COMMA) {
10075-
name_ref(p);
10076-
}
10077-
}
10078-
FUNCTION_KW | PROCEDURE_KW | ROUTINE_KW => {
10079-
p.bump_any();
10080-
// function_name [ ( [ [ argmode ] [ arg_name ] arg_type [, ...] ] ) ] [, ...]
10081-
path_name_ref(p);
10082-
opt_param_list(p);
10083-
while !p.at(EOF) && p.eat(COMMA) {
10084-
// function_name [ ( [ [ argmode ] [ arg_name ] arg_type [, ...] ] ) ] [, ...]
10085-
path_name_ref(p);
10086-
opt_param_list(p);
10087-
}
10088-
}
10089-
// TYPE type_name [, ...]
10090-
TYPE_KW => {
10091-
p.bump(TYPE_KW);
10092-
type_name(p);
10093-
while !p.at(EOF) && p.eat(COMMA) {
10094-
type_name(p);
10095-
}
10096-
}
10097-
TABLE_KW | SEQUENCE_KW | DATABASE_KW | TABLESPACE_KW | SCHEMA_KW | LANGUAGE_KW
10098-
| DOMAIN_KW => {
10099-
p.bump_any();
10100-
name_ref(p);
10101-
while !p.at(EOF) && p.eat(COMMA) {
10102-
name_ref(p);
10103-
}
10104-
}
10105-
FOREIGN_KW => {
10106-
p.bump(FOREIGN_KW);
10107-
if p.eat(DATA_KW) {
10108-
p.expect(WRAPPER_KW);
10109-
} else {
10110-
p.expect(SERVER_KW);
10111-
}
10112-
name_ref(p);
10113-
while !p.at(EOF) && p.eat(COMMA) {
10114-
name_ref(p);
10115-
}
10116-
}
10117-
LARGE_KW => {
10118-
p.bump(LARGE_KW);
10119-
p.expect(OBJECT_KW);
10120-
if opt_numeric_literal(p).is_none() {
10121-
p.error("expected large_object_oid")
10122-
}
10123-
while !p.at(EOF) && p.eat(COMMA) {
10124-
if opt_numeric_literal(p).is_none() {
10125-
p.error("expected large_object_oid")
10126-
}
10127-
}
10128-
}
10129-
// table_name [, ...]
10130-
_ if p.at_ts(COL_LABEL_FIRST) => {
10131-
name_ref(p);
10132-
while !p.at(EOF) && p.eat(COMMA) {
10133-
name_ref(p);
10134-
}
10135-
}
10136-
_ => (),
10137-
}
10138-
}
10055+
privilege_target(p);
1013910056
}
1014010057
// TO role_specification [, ...]
1014110058
p.expect(TO_KW);
@@ -10165,6 +10082,101 @@ fn grant_stmt(p: &mut Parser<'_>) -> CompletedMarker {
1016510082
m.complete(p, GRANT_STMT)
1016610083
}
1016710084

10085+
fn privilege_target(p: &mut Parser<'_>) {
10086+
if p.eat(ALL_KW) {
10087+
match p.current() {
10088+
TABLES_KW | SEQUENCES_KW | FUNCTIONS_KW | PROCEDURES_KW | ROUTINES_KW => {
10089+
p.bump_any();
10090+
p.expect(IN_KW);
10091+
p.expect(SCHEMA_KW);
10092+
// schema_name [, ...]
10093+
name_ref(p);
10094+
while !p.at(EOF) && p.eat(COMMA) {
10095+
name_ref(p);
10096+
}
10097+
}
10098+
_ => p.error("expected TABLE"),
10099+
}
10100+
} else {
10101+
match p.current() {
10102+
PARAMETER_KW => {
10103+
p.bump(PARAMETER_KW);
10104+
path_name_ref(p);
10105+
while !p.at(EOF) && p.eat(COMMA) {
10106+
path_name_ref(p);
10107+
}
10108+
}
10109+
FUNCTION_KW | PROCEDURE_KW | ROUTINE_KW => {
10110+
p.bump_any();
10111+
// function_name [ ( [ [ argmode ] [ arg_name ] arg_type [, ...] ] ) ] [, ...]
10112+
path_name_ref(p);
10113+
opt_param_list(p);
10114+
while !p.at(EOF) && p.eat(COMMA) {
10115+
// function_name [ ( [ [ argmode ] [ arg_name ] arg_type [, ...] ] ) ] [, ...]
10116+
path_name_ref(p);
10117+
opt_param_list(p);
10118+
}
10119+
}
10120+
// TYPE type_name [, ...]
10121+
TYPE_KW => {
10122+
p.bump(TYPE_KW);
10123+
type_name(p);
10124+
while !p.at(EOF) && p.eat(COMMA) {
10125+
type_name(p);
10126+
}
10127+
}
10128+
// no schema allowed for the name
10129+
DATABASE_KW | TABLESPACE_KW | SCHEMA_KW | LANGUAGE_KW => {
10130+
p.bump_any();
10131+
name_ref(p);
10132+
while !p.at(EOF) && p.eat(COMMA) {
10133+
name_ref(p);
10134+
}
10135+
}
10136+
// these allow schema
10137+
TABLE_KW | SEQUENCE_KW | DOMAIN_KW => {
10138+
p.bump_any();
10139+
path_name_ref(p);
10140+
while !p.at(EOF) && p.eat(COMMA) {
10141+
path_name_ref(p);
10142+
}
10143+
}
10144+
FOREIGN_KW => {
10145+
p.bump(FOREIGN_KW);
10146+
if p.eat(DATA_KW) {
10147+
p.expect(WRAPPER_KW);
10148+
} else {
10149+
p.expect(SERVER_KW);
10150+
}
10151+
name_ref(p);
10152+
while !p.at(EOF) && p.eat(COMMA) {
10153+
name_ref(p);
10154+
}
10155+
}
10156+
LARGE_KW => {
10157+
p.bump(LARGE_KW);
10158+
p.expect(OBJECT_KW);
10159+
if opt_numeric_literal(p).is_none() {
10160+
p.error("expected large_object_oid")
10161+
}
10162+
while !p.at(EOF) && p.eat(COMMA) {
10163+
if opt_numeric_literal(p).is_none() {
10164+
p.error("expected large_object_oid")
10165+
}
10166+
}
10167+
}
10168+
// table_name [, ...]
10169+
_ if p.at_ts(COL_LABEL_FIRST) => {
10170+
path_name_ref(p);
10171+
while !p.at(EOF) && p.eat(COMMA) {
10172+
path_name_ref(p);
10173+
}
10174+
}
10175+
_ => (),
10176+
}
10177+
}
10178+
}
10179+
1016810180
// [ GRANTED BY role_specification ]
1016910181
fn opt_granted_by(p: &mut Parser<'_>) {
1017010182
if p.eat(GRANTED_KW) {
@@ -10214,88 +10226,7 @@ fn revoke_stmt(p: &mut Parser<'_>) -> CompletedMarker {
1021410226
// | ALL { FUNCTIONS | PROCEDURES | ROUTINES } IN SCHEMA schema_name [, ...] }
1021510227
// ON PARAMETER configuration_parameter [, ...]
1021610228
if p.eat(ON_KW) {
10217-
if p.eat(ALL_KW) {
10218-
match p.current() {
10219-
TABLES_KW | SEQUENCES_KW | FUNCTIONS_KW | PROCEDURES_KW | ROUTINES_KW => {
10220-
p.bump_any();
10221-
p.expect(IN_KW);
10222-
p.expect(SCHEMA_KW);
10223-
// schema_name [, ...]
10224-
name_ref(p);
10225-
while !p.at(EOF) && p.eat(COMMA) {
10226-
name_ref(p);
10227-
}
10228-
}
10229-
_ => p.error("expected TABLE"),
10230-
}
10231-
} else {
10232-
match p.current() {
10233-
PARAMETER_KW => {
10234-
p.bump(PARAMETER_KW);
10235-
name_ref(p);
10236-
while !p.at(EOF) && p.eat(COMMA) {
10237-
name_ref(p);
10238-
}
10239-
}
10240-
FUNCTION_KW | PROCEDURE_KW | ROUTINE_KW => {
10241-
p.bump_any();
10242-
path_name_ref(p);
10243-
opt_param_list(p);
10244-
while !p.at(EOF) && p.eat(COMMA) {
10245-
path_name_ref(p);
10246-
opt_param_list(p);
10247-
}
10248-
}
10249-
// TYPE type_name [, ...]
10250-
TYPE_KW => {
10251-
p.bump(TYPE_KW);
10252-
type_name(p);
10253-
while !p.at(EOF) && p.eat(COMMA) {
10254-
type_name(p);
10255-
}
10256-
}
10257-
TABLE_KW | SEQUENCE_KW | DATABASE_KW | TABLESPACE_KW | SCHEMA_KW | LANGUAGE_KW
10258-
| DOMAIN_KW => {
10259-
p.bump_any();
10260-
name_ref(p);
10261-
while !p.at(EOF) && p.eat(COMMA) {
10262-
name_ref(p);
10263-
}
10264-
}
10265-
FOREIGN_KW => {
10266-
p.bump(FOREIGN_KW);
10267-
if p.eat(DATA_KW) {
10268-
p.expect(WRAPPER_KW);
10269-
} else {
10270-
p.expect(SERVER_KW);
10271-
}
10272-
name_ref(p);
10273-
while !p.at(EOF) && p.eat(COMMA) {
10274-
name_ref(p);
10275-
}
10276-
}
10277-
LARGE_KW => {
10278-
p.bump(LARGE_KW);
10279-
p.expect(OBJECT_KW);
10280-
if opt_numeric_literal(p).is_none() {
10281-
p.error("expected large_object_oid")
10282-
}
10283-
while !p.at(EOF) && p.eat(COMMA) {
10284-
if opt_numeric_literal(p).is_none() {
10285-
p.error("expected large_object_oid")
10286-
}
10287-
}
10288-
}
10289-
// table_name [, ...]
10290-
_ if p.at_ts(COL_LABEL_FIRST) => {
10291-
name_ref(p);
10292-
while !p.at(EOF) && p.eat(COMMA) {
10293-
name_ref(p);
10294-
}
10295-
}
10296-
_ => (),
10297-
}
10298-
}
10229+
privilege_target(p);
1029910230
}
1030010231
// FROM role_specification [, ...]
1030110232
p.expect(FROM_KW);

0 commit comments

Comments
 (0)