qusql-parse 0.4.0

Parser for sql
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Parser for the `GRANT` statement.
//!
//! Covers both the privilege-grant form and the role-membership form as documented at
//! <https://www.postgresql.org/docs/current/sql-grant.html>.

use crate::{
    DataType, Identifier, QualifiedName, Span, Spanned,
    create_function::FunctionParamDirection,
    data_type::{DataTypeContext, parse_data_type},
    expression::{Expression, PRIORITY_MAX, parse_expression_unreserved},
    keywords::Keyword,
    lexer::Token,
    parser::{ParseError, Parser},
    qualified_name::parse_qualified_name_unreserved,
    span::OptSpanned,
};
use alloc::vec::Vec;

/// A single privilege keyword in a GRANT statement.
#[derive(Clone, Debug)]
pub enum GrantPrivilege {
    Select(Span),
    Insert(Span),
    Update(Span),
    Delete(Span),
    Truncate(Span),
    References(Span),
    Trigger(Span),
    Maintain(Span),
    Usage(Span),
    Create(Span),
    Connect(Span),
    /// TEMPORARY or TEMP
    Temporary(Span),
    Execute(Span),
    Set(Span),
    /// ALTER SYSTEM - span covers both keywords
    AlterSystem(Span),
    /// ALL [ PRIVILEGES ] - second span is the optional PRIVILEGES keyword
    All(Span, Option<Span>),
}

impl Spanned for GrantPrivilege {
    fn span(&self) -> Span {
        match self {
            GrantPrivilege::Select(s)
            | GrantPrivilege::Insert(s)
            | GrantPrivilege::Update(s)
            | GrantPrivilege::Delete(s)
            | GrantPrivilege::Truncate(s)
            | GrantPrivilege::References(s)
            | GrantPrivilege::Trigger(s)
            | GrantPrivilege::Maintain(s)
            | GrantPrivilege::Usage(s)
            | GrantPrivilege::Create(s)
            | GrantPrivilege::Connect(s)
            | GrantPrivilege::Temporary(s)
            | GrantPrivilege::Execute(s)
            | GrantPrivilege::Set(s)
            | GrantPrivilege::AlterSystem(s) => s.clone(),
            GrantPrivilege::All(s, p) => s.join_span(p),
        }
    }
}

/// One entry in the privilege list: a privilege with an optional column list.
///
/// Column lists are valid for SELECT, INSERT, UPDATE, REFERENCES on tables.
#[derive(Clone, Debug)]
pub struct PrivilegeItem<'a> {
    pub privilege: GrantPrivilege,
    /// Empty unless the privilege was qualified with `( col [, ...] )`.
    pub columns: Vec<Identifier<'a>>,
}

impl<'a> Spanned for PrivilegeItem<'a> {
    fn span(&self) -> Span {
        self.privilege.join_span(&self.columns)
    }
}

/// How a routine was named in the GRANT ON { FUNCTION | PROCEDURE | ROUTINE } clause.
#[derive(Clone, Debug)]
pub enum RoutineKind {
    Function(Span),
    Procedure(Span),
    Routine(Span),
}

impl Spanned for RoutineKind {
    fn span(&self) -> Span {
        match self {
            RoutineKind::Function(s) | RoutineKind::Procedure(s) | RoutineKind::Routine(s) => {
                s.clone()
            }
        }
    }
}

/// ALL { FUNCTIONS | PROCEDURES | ROUTINES } - which plural form was used.
#[derive(Clone, Debug)]
pub enum AllRoutineKind {
    Functions(Span),
    Procedures(Span),
    Routines(Span),
}

impl Spanned for AllRoutineKind {
    fn span(&self) -> Span {
        match self {
            AllRoutineKind::Functions(s)
            | AllRoutineKind::Procedures(s)
            | AllRoutineKind::Routines(s) => s.clone(),
        }
    }
}

/// An argument type entry inside a routine reference's optional `( args )` list.
#[derive(Clone, Debug)]
pub struct RoutineArgType<'a> {
    /// Optional IN / OUT / INOUT modifier.
    pub mode: Option<FunctionParamDirection>,
    /// Optional parameter name (present when followed by a type token).
    pub name: Option<Identifier<'a>>,
    /// The argument data type.
    pub type_: DataType<'a>,
}

impl<'a> Spanned for RoutineArgType<'a> {
    fn span(&self) -> Span {
        self.type_.join_span(&self.mode).join_span(&self.name)
    }
}

/// A routine name together with an optional argument-type list.
///
/// The argument list is used only for disambiguation when overloaded routines exist.
#[derive(Clone, Debug)]
pub struct RoutineName<'a> {
    pub name: QualifiedName<'a>,
    /// `None` means no parentheses were written; `Some([])` means `name()`.
    pub args: Option<Vec<RoutineArgType<'a>>>,
}

impl<'a> Spanned for RoutineName<'a> {
    fn span(&self) -> Span {
        self.name.join_span(&self.args)
    }
}

/// The object (or set of objects) to which privileges are being granted.
#[derive(Clone, Debug)]
pub enum GrantObject<'a> {
    /// `[ TABLE ] table_name [, ...]`
    Tables {
        table_kw: Option<Span>,
        names: Vec<QualifiedName<'a>>,
    },
    /// `ALL TABLES IN SCHEMA schema_name [, ...]`
    AllTablesInSchema {
        /// Span covering `ALL TABLES IN SCHEMA`
        span: Span,
        schemas: Vec<QualifiedName<'a>>,
    },
    /// `SEQUENCE sequence_name [, ...]`
    Sequences {
        sequence_kw: Span,
        names: Vec<QualifiedName<'a>>,
    },
    /// `ALL SEQUENCES IN SCHEMA schema_name [, ...]`
    AllSequencesInSchema {
        /// Span covering `ALL SEQUENCES IN SCHEMA`
        span: Span,
        schemas: Vec<QualifiedName<'a>>,
    },
    /// `DATABASE database_name [, ...]`
    Databases {
        database_kw: Span,
        names: Vec<QualifiedName<'a>>,
    },
    /// `DOMAIN domain_name [, ...]`
    Domains {
        domain_kw: Span,
        names: Vec<QualifiedName<'a>>,
    },
    /// `FOREIGN DATA WRAPPER fdw_name [, ...]`
    ForeignDataWrappers {
        /// Span covering `FOREIGN DATA WRAPPER`
        span: Span,
        names: Vec<QualifiedName<'a>>,
    },
    /// `FOREIGN SERVER server_name [, ...]`
    ForeignServers {
        /// Span covering `FOREIGN SERVER`
        span: Span,
        names: Vec<QualifiedName<'a>>,
    },
    /// `{ FUNCTION | PROCEDURE | ROUTINE } name[(args)] [, ...]`
    Routines {
        kind: RoutineKind,
        names: Vec<RoutineName<'a>>,
    },
    /// `ALL { FUNCTIONS | PROCEDURES | ROUTINES } IN SCHEMA schema_name [, ...]`
    AllRoutinesInSchema {
        all_span: Span,
        kind: AllRoutineKind,
        in_schema_span: Span,
        schemas: Vec<QualifiedName<'a>>,
    },
    /// `LANGUAGE lang_name [, ...]`
    Languages {
        language_kw: Span,
        names: Vec<QualifiedName<'a>>,
    },
    /// `LARGE OBJECT loid [, ...]`
    LargeObjects {
        /// Span covering `LARGE OBJECT`
        span: Span,
        oids: Vec<Expression<'a>>,
    },
    /// `PARAMETER configuration_parameter [, ...]`
    Parameters {
        parameter_kw: Span,
        names: Vec<QualifiedName<'a>>,
    },
    /// `SCHEMA schema_name [, ...]`
    Schemas {
        schema_kw: Span,
        names: Vec<QualifiedName<'a>>,
    },
    /// `TABLESPACE tablespace_name [, ...]`
    Tablespaces {
        tablespace_kw: Span,
        names: Vec<QualifiedName<'a>>,
    },
    /// `TYPE type_name [, ...]`
    Types {
        type_kw: Span,
        names: Vec<QualifiedName<'a>>,
    },
}

impl<'a> Spanned for GrantObject<'a> {
    fn span(&self) -> Span {
        match self {
            GrantObject::Tables { table_kw, names } => {
                names.opt_span().unwrap().join_span(table_kw)
            }
            GrantObject::AllTablesInSchema { span, schemas } => span.join_span(schemas),
            GrantObject::Sequences { sequence_kw, names } => sequence_kw.join_span(names),
            GrantObject::AllSequencesInSchema { span, schemas } => span.join_span(schemas),
            GrantObject::Databases { database_kw, names } => database_kw.join_span(names),
            GrantObject::Domains { domain_kw, names } => domain_kw.join_span(names),
            GrantObject::ForeignDataWrappers { span, names } => span.join_span(names),
            GrantObject::ForeignServers { span, names } => span.join_span(names),
            GrantObject::Routines { kind, names } => kind.join_span(names),
            GrantObject::AllRoutinesInSchema {
                all_span, schemas, ..
            } => all_span.join_span(schemas),
            GrantObject::Languages { language_kw, names } => language_kw.join_span(names),
            GrantObject::LargeObjects { span, oids } => span.join_span(oids),
            GrantObject::Parameters {
                parameter_kw,
                names,
            } => parameter_kw.join_span(names),
            GrantObject::Schemas { schema_kw, names } => schema_kw.join_span(names),
            GrantObject::Tablespaces {
                tablespace_kw,
                names,
            } => tablespace_kw.join_span(names),
            GrantObject::Types { type_kw, names } => type_kw.join_span(names),
        }
    }
}

/// A grantee or grantor role specification.
#[derive(Clone, Debug)]
pub enum RoleSpec<'a> {
    /// `[ GROUP ] role_name`
    Named {
        group_kw: Option<Span>,
        name: Identifier<'a>,
    },
    /// `PUBLIC`
    Public(Span),
    /// `CURRENT_ROLE`
    CurrentRole(Span),
    /// `CURRENT_USER`
    CurrentUser(Span),
    /// `SESSION_USER`
    SessionUser(Span),
}

impl<'a> Spanned for RoleSpec<'a> {
    fn span(&self) -> Span {
        match self {
            RoleSpec::Named { group_kw, name } => name.span().join_span(group_kw),
            RoleSpec::Public(s)
            | RoleSpec::CurrentRole(s)
            | RoleSpec::CurrentUser(s)
            | RoleSpec::SessionUser(s) => s.clone(),
        }
    }
}

/// Which membership option is being set in `WITH { ADMIN | INHERIT | SET } ...`.
#[derive(Clone, Debug)]
pub enum MembershipOptionKind {
    Admin(Span),
    Inherit(Span),
    Set(Span),
}

impl Spanned for MembershipOptionKind {
    fn span(&self) -> Span {
        match self {
            MembershipOptionKind::Admin(s)
            | MembershipOptionKind::Inherit(s)
            | MembershipOptionKind::Set(s) => s.clone(),
        }
    }
}

/// The value part of a membership option: `OPTION` (= TRUE), `TRUE`, or `FALSE`.
#[derive(Clone, Debug)]
pub enum MembershipOptionValue {
    /// The `OPTION` noise word — equivalent to `TRUE`.
    Option(Span),
    True(Span),
    False(Span),
}

impl Spanned for MembershipOptionValue {
    fn span(&self) -> Span {
        match self {
            MembershipOptionValue::Option(s)
            | MembershipOptionValue::True(s)
            | MembershipOptionValue::False(s) => s.clone(),
        }
    }
}

/// A single `WITH { ADMIN | INHERIT | SET } { OPTION | TRUE | FALSE }` clause.
#[derive(Clone, Debug)]
pub struct MembershipOption {
    pub with_span: Span,
    pub kind: MembershipOptionKind,
    pub value: MembershipOptionValue,
}

impl Spanned for MembershipOption {
    fn span(&self) -> Span {
        self.with_span.join_span(&self.kind).join_span(&self.value)
    }
}

/// The two top-level forms that GRANT can take.
#[derive(Clone, Debug)]
pub enum GrantKind<'a> {
    /// `GRANT privileges ON object TO grantees [WITH GRANT OPTION] [GRANTED BY role]`
    Privilege {
        privileges: Vec<PrivilegeItem<'a>>,
        on_span: Span,
        object: GrantObject<'a>,
        to_span: Span,
        grantees: Vec<RoleSpec<'a>>,
        /// Span covering `WITH GRANT OPTION` if present.
        with_grant_option: Option<Span>,
        /// The `GRANTED BY role_spec` clause if present.
        granted_by: Option<(Span, RoleSpec<'a>)>,
    },
    /// `GRANT role_name [, ...] TO role_spec [, ...] [WITH opt] [GRANTED BY role]`
    Role {
        roles: Vec<QualifiedName<'a>>,
        to_span: Span,
        grantees: Vec<RoleSpec<'a>>,
        with_option: Option<MembershipOption>,
        granted_by: Option<(Span, RoleSpec<'a>)>,
    },
}

impl<'a> Spanned for GrantKind<'a> {
    fn span(&self) -> Span {
        match self {
            GrantKind::Privilege {
                privileges,
                on_span,
                object,
                grantees,
                granted_by,
                ..
            } => on_span
                .join_span(privileges)
                .join_span(object)
                .join_span(grantees)
                .join_span(granted_by),
            GrantKind::Role {
                roles,
                to_span,
                grantees,
                with_option,
                granted_by,
                ..
            } => to_span
                .join_span(roles)
                .join_span(grantees)
                .join_span(with_option)
                .join_span(granted_by),
        }
    }
}

/// A parsed `GRANT` statement.
#[derive(Clone, Debug)]
pub struct Grant<'a> {
    pub grant_span: Span,
    pub kind: GrantKind<'a>,
}

impl<'a> Spanned for Grant<'a> {
    fn span(&self) -> Span {
        self.grant_span.join_span(&self.kind)
    }
}

fn is_privilege_keyword(token: &Token) -> bool {
    matches!(
        token,
        Token::Ident(
            _,
            Keyword::SELECT
                | Keyword::INSERT
                | Keyword::UPDATE
                | Keyword::DELETE
                | Keyword::TRUNCATE
                | Keyword::REFERENCES
                | Keyword::TRIGGER
                | Keyword::MAINTAIN
                | Keyword::USAGE
                | Keyword::CREATE
                | Keyword::CONNECT
                | Keyword::TEMPORARY
                | Keyword::TEMP
                | Keyword::EXECUTE
                | Keyword::SET
                | Keyword::ALTER
                | Keyword::ALL
        )
    )
}

fn parse_privilege<'a>(parser: &mut Parser<'a, '_>) -> Result<GrantPrivilege, ParseError> {
    match &parser.token {
        Token::Ident(_, Keyword::SELECT) => Ok(GrantPrivilege::Select(
            parser.consume_keyword(Keyword::SELECT)?,
        )),
        Token::Ident(_, Keyword::INSERT) => Ok(GrantPrivilege::Insert(
            parser.consume_keyword(Keyword::INSERT)?,
        )),
        Token::Ident(_, Keyword::UPDATE) => Ok(GrantPrivilege::Update(
            parser.consume_keyword(Keyword::UPDATE)?,
        )),
        Token::Ident(_, Keyword::DELETE) => Ok(GrantPrivilege::Delete(
            parser.consume_keyword(Keyword::DELETE)?,
        )),
        Token::Ident(_, Keyword::TRUNCATE) => Ok(GrantPrivilege::Truncate(
            parser.consume_keyword(Keyword::TRUNCATE)?,
        )),
        Token::Ident(_, Keyword::REFERENCES) => Ok(GrantPrivilege::References(
            parser.consume_keyword(Keyword::REFERENCES)?,
        )),
        Token::Ident(_, Keyword::TRIGGER) => Ok(GrantPrivilege::Trigger(
            parser.consume_keyword(Keyword::TRIGGER)?,
        )),
        Token::Ident(_, Keyword::MAINTAIN) => Ok(GrantPrivilege::Maintain(
            parser.consume_keyword(Keyword::MAINTAIN)?,
        )),
        Token::Ident(_, Keyword::USAGE) => Ok(GrantPrivilege::Usage(
            parser.consume_keyword(Keyword::USAGE)?,
        )),
        Token::Ident(_, Keyword::CREATE) => Ok(GrantPrivilege::Create(
            parser.consume_keyword(Keyword::CREATE)?,
        )),
        Token::Ident(_, Keyword::CONNECT) => Ok(GrantPrivilege::Connect(
            parser.consume_keyword(Keyword::CONNECT)?,
        )),
        Token::Ident(_, Keyword::TEMPORARY) => Ok(GrantPrivilege::Temporary(
            parser.consume_keyword(Keyword::TEMPORARY)?,
        )),
        Token::Ident(_, Keyword::TEMP) => Ok(GrantPrivilege::Temporary(
            parser.consume_keyword(Keyword::TEMP)?,
        )),
        Token::Ident(_, Keyword::EXECUTE) => Ok(GrantPrivilege::Execute(
            parser.consume_keyword(Keyword::EXECUTE)?,
        )),
        Token::Ident(_, Keyword::SET) => {
            Ok(GrantPrivilege::Set(parser.consume_keyword(Keyword::SET)?))
        }
        Token::Ident(_, Keyword::ALTER) => {
            let alter = parser.consume_keyword(Keyword::ALTER)?;
            let sys = parser.consume_keyword(Keyword::SYSTEM)?;
            Ok(GrantPrivilege::AlterSystem(alter.join_span(&sys)))
        }
        Token::Ident(_, Keyword::ALL) => {
            let all = parser.consume_keyword(Keyword::ALL)?;
            let privileges = parser.skip_keyword(Keyword::PRIVILEGES);
            Ok(GrantPrivilege::All(all, privileges))
        }
        _ => parser.expected_failure("privilege keyword"),
    }
}

fn parse_privilege_list<'a>(
    parser: &mut Parser<'a, '_>,
) -> Result<Vec<PrivilegeItem<'a>>, ParseError> {
    let mut items = Vec::new();
    loop {
        let privilege = parse_privilege(parser)?;
        let mut columns = Vec::new();
        if matches!(parser.token, Token::LParen) {
            parser.consume_token(Token::LParen)?;
            parser.recovered("')'", &|t| t == &Token::RParen, |parser| {
                loop {
                    columns.push(parser.consume_plain_identifier_unreserved()?);
                    if parser.skip_token(Token::Comma).is_none() {
                        break;
                    }
                }
                Ok(())
            })?;
            parser.consume_token(Token::RParen)?;
        }
        items.push(PrivilegeItem { privilege, columns });
        if parser.skip_token(Token::Comma).is_none() {
            break;
        }
        // If after the comma we don't see a privilege keyword, stop - could be ON
        if !is_privilege_keyword(&parser.token) {
            break;
        }
    }
    Ok(items)
}

fn parse_role_spec<'a>(parser: &mut Parser<'a, '_>) -> Result<RoleSpec<'a>, ParseError> {
    match &parser.token {
        Token::Ident(_, Keyword::PUBLIC) => {
            Ok(RoleSpec::Public(parser.consume_keyword(Keyword::PUBLIC)?))
        }
        Token::Ident(_, Keyword::CURRENT_ROLE) => Ok(RoleSpec::CurrentRole(
            parser.consume_keyword(Keyword::CURRENT_ROLE)?,
        )),
        Token::Ident(_, Keyword::CURRENT_USER) => Ok(RoleSpec::CurrentUser(
            parser.consume_keyword(Keyword::CURRENT_USER)?,
        )),
        Token::Ident(_, Keyword::SESSION_USER) => Ok(RoleSpec::SessionUser(
            parser.consume_keyword(Keyword::SESSION_USER)?,
        )),
        Token::Ident(_, Keyword::GROUP) => {
            let group_kw = Some(parser.consume_keyword(Keyword::GROUP)?);
            let name = parser.consume_plain_identifier_unreserved()?;
            Ok(RoleSpec::Named { group_kw, name })
        }
        _ => {
            let name = parser.consume_plain_identifier_unreserved()?;
            Ok(RoleSpec::Named {
                group_kw: None,
                name,
            })
        }
    }
}

fn parse_role_spec_list<'a>(parser: &mut Parser<'a, '_>) -> Result<Vec<RoleSpec<'a>>, ParseError> {
    let mut list = Vec::new();
    loop {
        list.push(parse_role_spec(parser)?);
        if parser.skip_token(Token::Comma).is_none() {
            break;
        }
    }
    Ok(list)
}

fn parse_granted_by<'a>(
    parser: &mut Parser<'a, '_>,
) -> Result<Option<(Span, RoleSpec<'a>)>, ParseError> {
    if let Some(granted_span) = parser.skip_keyword(Keyword::GRANTED) {
        let by_span = parser.consume_keyword(Keyword::BY)?;
        let role = parse_role_spec(parser)?;
        Ok(Some((granted_span.join_span(&by_span), role)))
    } else {
        Ok(None)
    }
}

fn parse_routine_arg_list<'a>(
    parser: &mut Parser<'a, '_>,
) -> Result<Vec<RoutineArgType<'a>>, ParseError> {
    let mut args = Vec::new();
    parser.recovered("')'", &|t| t == &Token::RParen, |parser| {
        loop {
            if matches!(parser.token, Token::RParen) {
                break;
            }
            let mode = match &parser.token {
                Token::Ident(_, Keyword::IN) => {
                    let in_ = parser.consume_keyword(Keyword::IN)?;
                    if let Some(out) = parser.skip_keyword(Keyword::OUT) {
                        Some(FunctionParamDirection::InOut(in_.join_span(&out)))
                    } else {
                        Some(FunctionParamDirection::In(in_))
                    }
                }
                Token::Ident(_, Keyword::OUT) => Some(FunctionParamDirection::Out(
                    parser.consume_keyword(Keyword::OUT)?,
                )),
                Token::Ident(_, Keyword::INOUT) => Some(FunctionParamDirection::InOut(
                    parser.consume_keyword(Keyword::INOUT)?,
                )),
                _ => None,
            };
            // Peek: if next token after this one looks like a type start, this is a name
            let name = {
                let is_unnamed = matches!(
                    parser.peek(),
                    Token::Comma | Token::RParen | Token::LBracket
                );
                if is_unnamed {
                    None
                } else {
                    Some(parser.consume_plain_identifier_unreserved()?)
                }
            };
            let type_ = parse_data_type(parser, DataTypeContext::FunctionParam)?;
            args.push(RoutineArgType { mode, name, type_ });
            if parser.skip_token(Token::Comma).is_none() {
                break;
            }
        }
        Ok(())
    })?;
    Ok(args)
}

fn parse_routine_name_list<'a>(
    parser: &mut Parser<'a, '_>,
) -> Result<Vec<RoutineName<'a>>, ParseError> {
    let mut names = Vec::new();
    loop {
        let name = parse_qualified_name_unreserved(parser)?;
        let args = if matches!(parser.token, Token::LParen) {
            parser.consume_token(Token::LParen)?;
            let args = parse_routine_arg_list(parser)?;
            parser.consume_token(Token::RParen)?;
            Some(args)
        } else {
            None
        };
        names.push(RoutineName { name, args });
        if parser.skip_token(Token::Comma).is_none() {
            break;
        }
    }
    Ok(names)
}

fn parse_qualified_name_list<'a>(
    parser: &mut Parser<'a, '_>,
) -> Result<Vec<QualifiedName<'a>>, ParseError> {
    let mut names = Vec::new();
    loop {
        names.push(parse_qualified_name_unreserved(parser)?);
        if parser.skip_token(Token::Comma).is_none() {
            break;
        }
    }
    Ok(names)
}

fn parse_grant_object<'a>(parser: &mut Parser<'a, '_>) -> Result<GrantObject<'a>, ParseError> {
    match &parser.token {
        Token::Ident(_, Keyword::TABLE) => {
            let table_kw = Some(parser.consume_keyword(Keyword::TABLE)?);
            let names = parse_qualified_name_list(parser)?;
            Ok(GrantObject::Tables { table_kw, names })
        }
        Token::Ident(_, Keyword::ALL) => {
            let all_span = parser.consume_keyword(Keyword::ALL)?;
            match &parser.token {
                Token::Ident(_, Keyword::TABLES) => {
                    let table_in_schema_span = parser.consume_keywords(&[
                        Keyword::TABLES,
                        Keyword::IN,
                        Keyword::SCHEMA,
                    ])?;
                    let schemas = parse_qualified_name_list(parser)?;
                    Ok(GrantObject::AllTablesInSchema {
                        span: all_span.join_span(&table_in_schema_span),
                        schemas,
                    })
                }
                Token::Ident(_, Keyword::SEQUENCES) => {
                    let in_sequences_schema_span = parser.consume_keywords(&[
                        Keyword::SEQUENCES,
                        Keyword::IN,
                        Keyword::SCHEMA,
                    ])?;
                    let schemas = parse_qualified_name_list(parser)?;
                    Ok(GrantObject::AllSequencesInSchema {
                        span: all_span.join_span(&in_sequences_schema_span),
                        schemas,
                    })
                }
                Token::Ident(_, Keyword::FUNCTIONS) => {
                    let kind_span = parser.consume_keyword(Keyword::FUNCTIONS)?;
                    let in_schema_span =
                        parser.consume_keywords(&[Keyword::IN, Keyword::SCHEMA])?;
                    let schemas = parse_qualified_name_list(parser)?;
                    Ok(GrantObject::AllRoutinesInSchema {
                        all_span,
                        kind: AllRoutineKind::Functions(kind_span),
                        in_schema_span,
                        schemas,
                    })
                }
                Token::Ident(_, Keyword::PROCEDURES) => {
                    let kind_span = parser.consume_keyword(Keyword::PROCEDURES)?;
                    let in_schema_span =
                        parser.consume_keywords(&[Keyword::IN, Keyword::SCHEMA])?;
                    let schemas = parse_qualified_name_list(parser)?;
                    Ok(GrantObject::AllRoutinesInSchema {
                        all_span,
                        kind: AllRoutineKind::Procedures(kind_span),
                        in_schema_span,
                        schemas,
                    })
                }
                Token::Ident(_, Keyword::ROUTINES) => {
                    let kind_span = parser.consume_keyword(Keyword::ROUTINES)?;
                    let in_schema_span =
                        parser.consume_keywords(&[Keyword::IN, Keyword::SCHEMA])?;
                    let schemas = parse_qualified_name_list(parser)?;
                    Ok(GrantObject::AllRoutinesInSchema {
                        all_span,
                        kind: AllRoutineKind::Routines(kind_span),
                        in_schema_span,
                        schemas,
                    })
                }
                _ => parser.expected_failure(
                    "TABLES, SEQUENCES, FUNCTIONS, PROCEDURES, or ROUTINES after ALL",
                ),
            }
        }
        Token::Ident(_, Keyword::SEQUENCE) => {
            let sequence_kw = parser.consume_keyword(Keyword::SEQUENCE)?;
            let names = parse_qualified_name_list(parser)?;
            Ok(GrantObject::Sequences { sequence_kw, names })
        }
        Token::Ident(_, Keyword::DATABASE) => {
            let database_kw = parser.consume_keyword(Keyword::DATABASE)?;
            let names = parse_qualified_name_list(parser)?;
            Ok(GrantObject::Databases { database_kw, names })
        }
        Token::Ident(_, Keyword::DOMAIN) => {
            let domain_kw = parser.consume_keyword(Keyword::DOMAIN)?;
            let names = parse_qualified_name_list(parser)?;
            Ok(GrantObject::Domains { domain_kw, names })
        }
        Token::Ident(_, Keyword::FOREIGN) => {
            let foreign_span = parser.consume_keyword(Keyword::FOREIGN)?;
            match &parser.token {
                Token::Ident(_, Keyword::DATA) => {
                    let data_span = parser.consume_keyword(Keyword::DATA)?;
                    let wrapper_span = parser.consume_keyword(Keyword::WRAPPER)?;
                    let names = parse_qualified_name_list(parser)?;
                    Ok(GrantObject::ForeignDataWrappers {
                        span: foreign_span.join_span(&data_span).join_span(&wrapper_span),
                        names,
                    })
                }
                Token::Ident(_, Keyword::SERVER) => {
                    let server_span = parser.consume_keyword(Keyword::SERVER)?;
                    let names = parse_qualified_name_list(parser)?;
                    Ok(GrantObject::ForeignServers {
                        span: foreign_span.join_span(&server_span),
                        names,
                    })
                }
                _ => parser.expected_failure("DATA WRAPPER or SERVER after FOREIGN"),
            }
        }
        Token::Ident(_, Keyword::FUNCTION) => {
            let kw = parser.consume_keyword(Keyword::FUNCTION)?;
            let names = parse_routine_name_list(parser)?;
            Ok(GrantObject::Routines {
                kind: RoutineKind::Function(kw),
                names,
            })
        }
        Token::Ident(_, Keyword::PROCEDURE) => {
            let kw = parser.consume_keyword(Keyword::PROCEDURE)?;
            let names = parse_routine_name_list(parser)?;
            Ok(GrantObject::Routines {
                kind: RoutineKind::Procedure(kw),
                names,
            })
        }
        Token::Ident(_, Keyword::ROUTINE) => {
            let kw = parser.consume_keyword(Keyword::ROUTINE)?;
            let names = parse_routine_name_list(parser)?;
            Ok(GrantObject::Routines {
                kind: RoutineKind::Routine(kw),
                names,
            })
        }
        Token::Ident(_, Keyword::LANGUAGE) => {
            let language_kw = parser.consume_keyword(Keyword::LANGUAGE)?;
            let names = parse_qualified_name_list(parser)?;
            Ok(GrantObject::Languages { language_kw, names })
        }
        Token::Ident(_, Keyword::LARGE) => {
            let large_span = parser.consume_keyword(Keyword::LARGE)?;
            let object_span = parser.consume_keyword(Keyword::OBJECT)?;
            let mut oids = Vec::new();
            loop {
                oids.push(parse_expression_unreserved(parser, PRIORITY_MAX)?);
                if parser.skip_token(Token::Comma).is_none() {
                    break;
                }
            }
            Ok(GrantObject::LargeObjects {
                span: large_span.join_span(&object_span),
                oids,
            })
        }
        Token::Ident(_, Keyword::PARAMETER) => {
            let parameter_kw = parser.consume_keyword(Keyword::PARAMETER)?;
            let names = parse_qualified_name_list(parser)?;
            Ok(GrantObject::Parameters {
                parameter_kw,
                names,
            })
        }
        Token::Ident(_, Keyword::SCHEMA) => {
            let schema_kw = parser.consume_keyword(Keyword::SCHEMA)?;
            let names = parse_qualified_name_list(parser)?;
            Ok(GrantObject::Schemas { schema_kw, names })
        }
        Token::Ident(_, Keyword::TABLESPACE) => {
            let tablespace_kw = parser.consume_keyword(Keyword::TABLESPACE)?;
            let names = parse_qualified_name_list(parser)?;
            Ok(GrantObject::Tablespaces {
                tablespace_kw,
                names,
            })
        }
        Token::Ident(_, Keyword::TYPE) => {
            let type_kw = parser.consume_keyword(Keyword::TYPE)?;
            let names = parse_qualified_name_list(parser)?;
            Ok(GrantObject::Types { type_kw, names })
        }
        _ => {
            // Bare table name without TABLE keyword
            let names = parse_qualified_name_list(parser)?;
            Ok(GrantObject::Tables {
                table_kw: None,
                names,
            })
        }
    }
}

pub(crate) fn parse_grant<'a>(parser: &mut Parser<'a, '_>) -> Result<Grant<'a>, ParseError> {
    let grant_span = parser.consume_keyword(Keyword::GRANT)?;

    // Disambiguate: if the first token is a privilege keyword, this is a privilege grant.
    // Otherwise it is a role-membership grant.
    let kind = if is_privilege_keyword(&parser.token) {
        let privileges = parse_privilege_list(parser)?;
        let on_span = parser.consume_keyword(Keyword::ON)?;
        let object = parse_grant_object(parser)?;
        let to_span = parser.consume_keyword(Keyword::TO)?;
        let grantees = parse_role_spec_list(parser)?;

        // WITH GRANT OPTION
        let with_grant_option = if let Some(with_span) = parser.skip_keyword(Keyword::WITH) {
            let grant_span = parser.consume_keyword(Keyword::GRANT)?;
            let option_span = parser.consume_keyword(Keyword::OPTION)?;
            Some(with_span.join_span(&grant_span).join_span(&option_span))
        } else {
            None
        };

        let granted_by = parse_granted_by(parser)?;

        GrantKind::Privilege {
            privileges,
            on_span,
            object,
            to_span,
            grantees,
            with_grant_option,
            granted_by,
        }
    } else {
        let roles = parse_qualified_name_list(parser)?;
        let to_span = parser.consume_keyword(Keyword::TO)?;
        let grantees = parse_role_spec_list(parser)?;

        // WITH { ADMIN | INHERIT | SET } { OPTION | TRUE | FALSE }
        let with_option = if let Some(with_span) = parser.skip_keyword(Keyword::WITH) {
            let kind = match &parser.token {
                Token::Ident(_, Keyword::ADMIN) => {
                    MembershipOptionKind::Admin(parser.consume_keyword(Keyword::ADMIN)?)
                }
                Token::Ident(_, Keyword::INHERIT) => {
                    MembershipOptionKind::Inherit(parser.consume_keyword(Keyword::INHERIT)?)
                }
                Token::Ident(_, Keyword::SET) => {
                    MembershipOptionKind::Set(parser.consume_keyword(Keyword::SET)?)
                }
                _ => parser.expected_failure("ADMIN, INHERIT, or SET after WITH")?,
            };
            let value = match &parser.token {
                Token::Ident(_, Keyword::OPTION) => {
                    MembershipOptionValue::Option(parser.consume_keyword(Keyword::OPTION)?)
                }
                Token::Ident(_, Keyword::TRUE) => {
                    MembershipOptionValue::True(parser.consume_keyword(Keyword::TRUE)?)
                }
                Token::Ident(_, Keyword::FALSE) => {
                    MembershipOptionValue::False(parser.consume_keyword(Keyword::FALSE)?)
                }
                _ => parser.expected_failure("OPTION, TRUE, or FALSE")?,
            };
            Some(MembershipOption {
                with_span,
                kind,
                value,
            })
        } else {
            None
        };

        let granted_by = parse_granted_by(parser)?;

        GrantKind::Role {
            roles,
            to_span,
            grantees,
            with_option,
            granted_by,
        }
    };

    Ok(Grant { grant_span, kind })
}