vibesql-parser 0.2.0

SQL parser for vibesql database engine
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
//! Constraint parsing for CREATE TABLE

use super::super::*;

/// Validate that constraint columns do not contain expressions.
/// SQLite prohibits expressions in PRIMARY KEY and UNIQUE constraints.
fn validate_no_expressions_in_constraint(
    columns: &[vibesql_ast::IndexColumn],
) -> Result<(), ParseError> {
    for col in columns {
        if matches!(col, vibesql_ast::IndexColumn::Expression { .. }) {
            return Err(ParseError {
                message: "expressions prohibited in PRIMARY KEY and UNIQUE constraints".to_string(),
            });
        }
    }
    Ok(())
}

impl Parser {
    /// Parse an optional ON CONFLICT clause for constraints
    /// Returns None if no ON CONFLICT clause is present
    /// Syntax: ON CONFLICT ROLLBACK|ABORT|FAIL|IGNORE|REPLACE
    pub(in crate::parser) fn parse_constraint_conflict_clause(
        &mut self,
    ) -> Result<Option<vibesql_ast::ConflictClause>, ParseError> {
        // Check for ON keyword
        if !self.peek_keyword(Keyword::On) {
            return Ok(None);
        }

        // Peek ahead to see if next is CONFLICT (vs ON DELETE/UPDATE for foreign keys)
        if self.position + 1 < self.tokens.len() {
            if let Token::Keyword { keyword: Keyword::Conflict, .. } =
                &self.tokens[self.position + 1]
            {
                self.advance(); // consume ON
                self.advance(); // consume CONFLICT

                // Parse the conflict resolution algorithm
                if self.peek_keyword(Keyword::Rollback) {
                    self.advance();
                    return Ok(Some(vibesql_ast::ConflictClause::Rollback));
                } else if self.peek_keyword(Keyword::Abort) {
                    self.advance();
                    return Ok(Some(vibesql_ast::ConflictClause::Abort));
                } else if self.peek_keyword(Keyword::Fail) {
                    self.advance();
                    return Ok(Some(vibesql_ast::ConflictClause::Fail));
                } else if self.peek_keyword(Keyword::Ignore) {
                    self.advance();
                    return Ok(Some(vibesql_ast::ConflictClause::Ignore));
                } else if self.peek_keyword(Keyword::Replace) {
                    self.advance();
                    return Ok(Some(vibesql_ast::ConflictClause::Replace));
                } else {
                    return Err(ParseError {
                        message:
                            "Expected ROLLBACK, ABORT, FAIL, IGNORE, or REPLACE after ON CONFLICT"
                                .to_string(),
                    });
                }
            }
        }

        Ok(None)
    }

    /// Parse an index column specification with optional prefix length or expression
    ///
    /// Supports both simple column references and expression indexes:
    /// - Column: `col1`, `col1(10)` (with prefix length)
    /// - Expression: `(lower(name))`, `(a + b)`
    ///
    /// Syntax: column_name [ ( integer ) ] | ( expression )
    fn parse_index_column_spec(&mut self) -> Result<vibesql_ast::IndexColumn, ParseError> {
        // Check if this is an expression index (starts with parenthesis)
        if self.peek() == &Token::LParen {
            self.advance(); // consume LParen

            // Parse the expression
            let expr = self.parse_expression()?;

            self.expect_token(Token::RParen)?;

            // Check for optional ASC/DESC
            let direction = if self.peek_keyword(crate::keywords::Keyword::Asc) {
                self.advance();
                vibesql_ast::OrderDirection::Asc
            } else if self.peek_keyword(crate::keywords::Keyword::Desc) {
                self.advance();
                vibesql_ast::OrderDirection::Desc
            } else {
                vibesql_ast::OrderDirection::Asc
            };

            self.reject_nulls_in_index_position()?;

            return Ok(vibesql_ast::IndexColumn::new_expression(expr, direction));
        }

        // Save position before parsing identifier - we may need to backtrack
        // if this turns out to be an expression like abs(b) rather than a column name
        let saved_position = self.position;

        let column_name = self.parse_identifier()?;

        // Check for optional prefix length: column_name(length)
        // BUT: if the token after ( is not a number, this is likely a function call
        // like abs(b), not a prefix length like name(10). In that case, backtrack
        // and parse as an expression.
        let prefix_length = if self.peek() == &Token::LParen {
            // Peek ahead to check if this is a prefix length (number) or function args
            if matches!(self.peek_at_offset(1), Token::Number(_)) {
                self.advance(); // consume LParen

                // Parse the integer length
                let length = match self.peek() {
                    Token::Number(n) => {
                        let value = n.parse::<i64>().map_err(|_| ParseError {
                            message: "Invalid integer for column prefix length".to_string(),
                        })?;
                        self.advance();

                        // Validate prefix length range
                        if value < 1 {
                            return Err(ParseError {
                                message: "Prefix length must be at least 1".to_string(),
                            });
                        }
                        if value > 10000 {
                            return Err(ParseError {
                                message: "Prefix length must not exceed 10000".to_string(),
                            });
                        }

                        value
                    }
                    _ => {
                        return Err(ParseError {
                            message: "Expected integer for column prefix length".to_string(),
                        })
                    }
                };

                self.expect_token(Token::RParen)?;
                Some(length as u64)
            } else {
                // Not a prefix length - this is a function call like abs(b)
                // Backtrack and parse as an expression
                self.position = saved_position;
                let expr = self.parse_expression()?;

                // Check for optional ASC/DESC
                let direction = if self.peek_keyword(crate::keywords::Keyword::Asc) {
                    self.advance();
                    vibesql_ast::OrderDirection::Asc
                } else if self.peek_keyword(crate::keywords::Keyword::Desc) {
                    self.advance();
                    vibesql_ast::OrderDirection::Desc
                } else {
                    vibesql_ast::OrderDirection::Asc
                };

                self.reject_nulls_in_index_position()?;

                return Ok(vibesql_ast::IndexColumn::new_expression(expr, direction));
            }
        } else {
            None
        };

        // Check for optional ASC/DESC
        let direction = if self.peek_keyword(crate::keywords::Keyword::Asc) {
            self.advance();
            vibesql_ast::OrderDirection::Asc
        } else if self.peek_keyword(crate::keywords::Keyword::Desc) {
            self.advance();
            vibesql_ast::OrderDirection::Desc
        } else {
            vibesql_ast::OrderDirection::Asc // Default for constraints
        };

        // Reject `NULLS FIRST` / `NULLS LAST` here too — same SQLite rule as in
        // CREATE INDEX column specs (see `parse_index_column_list`).
        self.reject_nulls_in_index_position()?;

        Ok(vibesql_ast::IndexColumn::Column { column_name, direction, prefix_length })
    }
    /// Parse ON DELETE/UPDATE referential actions
    fn parse_referential_actions(
        &mut self,
    ) -> Result<
        (Option<vibesql_ast::ReferentialAction>, Option<vibesql_ast::ReferentialAction>),
        ParseError,
    > {
        let mut on_delete = None;
        let mut on_update = None;

        // Parse optional ON DELETE clause
        if self.peek_keyword(Keyword::On) {
            self.advance(); // consume ON
            if self.peek_keyword(Keyword::Delete) {
                self.advance(); // consume DELETE
                on_delete = Some(self.parse_referential_action()?);
            } else if self.peek_keyword(Keyword::Update) {
                self.advance(); // consume UPDATE
                on_update = Some(self.parse_referential_action()?);
            } else {
                return Err(ParseError {
                    message: "Expected DELETE or UPDATE after ON".to_string(),
                });
            }
        }

        // Parse optional second ON clause (if not already parsed above)
        if self.peek_keyword(Keyword::On) {
            self.advance(); // consume ON
            if self.peek_keyword(Keyword::Delete) {
                self.advance(); // consume DELETE
                on_delete = Some(self.parse_referential_action()?);
            } else if self.peek_keyword(Keyword::Update) {
                self.advance(); // consume UPDATE
                on_update = Some(self.parse_referential_action()?);
            } else {
                return Err(ParseError {
                    message: "Expected DELETE or UPDATE after ON".to_string(),
                });
            }
        }

        Ok((on_delete, on_update))
    }

    /// Parse constraint deferral mode: [NOT] DEFERRABLE [INITIALLY {DEFERRED | IMMEDIATE}]
    ///
    /// Returns None if no deferral clause is present.
    fn parse_constraint_deferral(
        &mut self,
    ) -> Result<Option<vibesql_ast::ConstraintDeferral>, ParseError> {
        let is_deferrable = if self.peek_keyword(Keyword::Not) {
            // Check if followed by DEFERRABLE
            if self.position + 1 < self.tokens.len() {
                if let Token::Keyword { keyword: Keyword::Deferrable, .. } =
                    &self.tokens[self.position + 1]
                {
                    self.advance(); // consume NOT
                    self.advance(); // consume DEFERRABLE
                    false
                } else {
                    return Ok(None);
                }
            } else {
                return Ok(None);
            }
        } else if self.peek_keyword(Keyword::Deferrable) {
            self.advance(); // consume DEFERRABLE
            true
        } else {
            return Ok(None);
        };

        // Parse optional INITIALLY clause
        let initially_deferred = if self.peek_keyword(Keyword::Initially) {
            self.advance(); // consume INITIALLY
            if self.peek_keyword(Keyword::Deferred) {
                self.advance(); // consume DEFERRED
                true
            } else if self.peek_keyword(Keyword::Immediate) {
                self.advance(); // consume IMMEDIATE
                false
            } else {
                return Err(ParseError {
                    message: "Expected DEFERRED or IMMEDIATE after INITIALLY".to_string(),
                });
            }
        } else {
            false // Default is INITIALLY IMMEDIATE
        };

        Ok(Some(vibesql_ast::ConstraintDeferral { is_deferrable, initially_deferred }))
    }

    /// Parse a single referential action (NO ACTION, CASCADE, RESTRICT, SET NULL, SET DEFAULT)
    fn parse_referential_action(&mut self) -> Result<vibesql_ast::ReferentialAction, ParseError> {
        if self.peek_keyword(Keyword::No) {
            self.advance(); // consume NO
            self.expect_keyword(Keyword::Action)?;
            Ok(vibesql_ast::ReferentialAction::NoAction)
        } else if self.peek_keyword(Keyword::Cascade) {
            self.advance(); // consume CASCADE
            Ok(vibesql_ast::ReferentialAction::Cascade)
        } else if self.peek_keyword(Keyword::Restrict) {
            self.advance(); // consume RESTRICT
            Ok(vibesql_ast::ReferentialAction::Restrict)
        } else if self.peek_keyword(Keyword::Set) {
            self.advance(); // consume SET
            if self.peek_keyword(Keyword::Null) {
                self.advance(); // consume NULL
                Ok(vibesql_ast::ReferentialAction::SetNull)
            } else if self.peek_keyword(Keyword::Default) {
                self.advance(); // consume DEFAULT
                Ok(vibesql_ast::ReferentialAction::SetDefault)
            } else {
                Err(ParseError { message: "Expected NULL or DEFAULT after SET".to_string() })
            }
        } else {
            Err(ParseError {
                message: "Expected NO ACTION, CASCADE, RESTRICT, SET NULL, or SET DEFAULT"
                    .to_string(),
            })
        }
    }

    /// Parse column-level constraints (PRIMARY KEY, UNIQUE, CHECK, REFERENCES)
    ///
    /// Returns the list of parsed constraints plus an optional `DEFAULT` expression
    /// encountered mid-stream. SQLite grammar allows DEFAULT to appear interleaved
    /// with column constraints in any order (e.g., `idx UNIQUE DEFAULT NULL`), so
    /// callers should treat the returned default as additional output and merge it
    /// with any DEFAULT clause parsed before the constraint list.
    pub(in crate::parser) fn parse_column_constraints(
        &mut self,
    ) -> Result<
        (Vec<vibesql_ast::ColumnConstraint>, Option<Box<vibesql_ast::Expression>>),
        ParseError,
    > {
        let mut constraints = Vec::new();
        let mut default_value: Option<Box<vibesql_ast::Expression>> = None;

        loop {
            // Check for optional CONSTRAINT keyword
            let name = if self.peek_keyword(Keyword::Constraint) {
                self.advance(); // consume CONSTRAINT
                match self.peek() {
                    Token::Identifier(n) => {
                        let constraint_name = n.clone();
                        self.advance();
                        Some(constraint_name)
                    }
                    _ => {
                        return Err(ParseError {
                            message: "Expected constraint name after CONSTRAINT".to_string(),
                        })
                    }
                }
            } else {
                None
            };

            match self.peek() {
                Token::Keyword { keyword: Keyword::Default, .. } => {
                    // DEFAULT clause may appear interleaved with column constraints
                    // (SQLite grammar). Capture the expression and continue parsing.
                    if name.is_some() {
                        return Err(ParseError {
                            message: "DEFAULT cannot follow CONSTRAINT name".to_string(),
                        });
                    }
                    self.advance(); // consume DEFAULT
                    let expr = self.parse_expression()?;
                    if default_value.is_some() {
                        return Err(ParseError {
                            message: "Multiple DEFAULT clauses for the same column".to_string(),
                        });
                    }
                    default_value = Some(Box::new(expr));
                }
                Token::Keyword { keyword: Keyword::Null, .. } => {
                    // MySQL allows standalone NULL keyword to explicitly indicate nullable column
                    // (which is the default anyway), so we just consume it and skip it
                    self.advance(); // consume NULL
                                    // This is a no-op - nullable is the default
                }
                Token::Keyword { keyword: Keyword::Not, .. } => {
                    self.advance(); // consume NOT
                    self.expect_keyword(Keyword::Null)?;
                    constraints.push(vibesql_ast::ColumnConstraint {
                        name,
                        kind: vibesql_ast::ColumnConstraintKind::NotNull,
                    });
                }
                Token::Keyword { keyword: Keyword::Primary, .. } => {
                    self.advance(); // consume PRIMARY
                    self.expect_keyword(Keyword::Key)?;
                    // SQLite allows optional ASC or DESC after PRIMARY KEY
                    if self.peek_keyword(Keyword::Asc) || self.peek_keyword(Keyword::Desc) {
                        self.advance(); // consume ASC or DESC (ignored - SQLite ignores it too)
                    }
                    // Parse optional ON CONFLICT clause
                    let on_conflict = self.parse_constraint_conflict_clause()?;
                    constraints.push(vibesql_ast::ColumnConstraint {
                        name,
                        kind: vibesql_ast::ColumnConstraintKind::PrimaryKey { on_conflict },
                    });
                }
                Token::Keyword { keyword: Keyword::Unique, .. } => {
                    self.advance(); // consume UNIQUE
                                    // MySQL allows optional KEY keyword after UNIQUE
                    if self.peek_keyword(Keyword::Key) {
                        self.advance(); // consume KEY
                    }
                    // Parse optional ON CONFLICT clause
                    let on_conflict = self.parse_constraint_conflict_clause()?;
                    constraints.push(vibesql_ast::ColumnConstraint {
                        name,
                        kind: vibesql_ast::ColumnConstraintKind::Unique { on_conflict },
                    });
                }
                Token::Keyword { keyword: Keyword::Check, .. } => {
                    self.advance(); // consume CHECK
                    self.expect_token(Token::LParen)?;
                    let expr = self.parse_expression()?;
                    self.expect_token(Token::RParen)?;
                    constraints.push(vibesql_ast::ColumnConstraint {
                        name,
                        kind: vibesql_ast::ColumnConstraintKind::Check(Box::new(expr)),
                    });
                }
                Token::Keyword { keyword: Keyword::References, .. } => {
                    self.advance(); // consume REFERENCES
                    // Accept both regular and quoted identifiers (e.g., REFERENCES t1, REFERENCES "t1")
                    let table = self.parse_identifier().map_err(|_| ParseError {
                        message: "Expected table name after REFERENCES".to_string(),
                    })?;

                    // Column specification is optional in SQLite - defaults to PK
                    let column = if self.peek() == &Token::LParen {
                        self.advance(); // consume LParen
                        // Accept both regular and quoted identifiers for column names
                        let col_name = self.parse_identifier().map_err(|_| ParseError {
                            message: "Expected column name in REFERENCES".to_string(),
                        })?;
                        self.expect_token(Token::RParen)?;
                        Some(col_name)
                    } else {
                        None // Column not specified, defaults to PK
                    };

                    let (on_delete, on_update) = self.parse_referential_actions()?;
                    let deferral = self.parse_constraint_deferral()?;

                    constraints.push(vibesql_ast::ColumnConstraint {
                        name,
                        kind: vibesql_ast::ColumnConstraintKind::References {
                            table,
                            column,
                            on_delete,
                            on_update,
                            deferral,
                        },
                    });
                }
                Token::Keyword { keyword: Keyword::AutoIncrement, .. } => {
                    self.advance(); // consume AUTO_INCREMENT or AUTOINCREMENT
                    constraints.push(vibesql_ast::ColumnConstraint {
                        name,
                        kind: vibesql_ast::ColumnConstraintKind::AutoIncrement,
                    });
                }
                Token::Keyword { keyword: Keyword::Key, .. } => {
                    self.advance(); // consume KEY
                    constraints.push(vibesql_ast::ColumnConstraint {
                        name,
                        kind: vibesql_ast::ColumnConstraintKind::Key,
                    });
                }
                Token::Keyword { keyword: Keyword::Collate, .. } => {
                    self.advance(); // consume COLLATE
                    let collation_name = match self.peek() {
                        Token::Identifier(n) | Token::DelimitedIdentifier(n) => {
                            let coll = n.clone();
                            self.advance();
                            coll
                        }
                        Token::Keyword { original, .. } => {
                            // Allow keyword-like collation names (e.g., NOCASE, BINARY)
                            let coll = original.clone();
                            self.advance();
                            coll
                        }
                        _ => {
                            return Err(ParseError {
                                message: "Expected collation name after COLLATE".to_string(),
                            })
                        }
                    };
                    constraints.push(vibesql_ast::ColumnConstraint {
                        name,
                        kind: vibesql_ast::ColumnConstraintKind::Collate(collation_name),
                    });
                }
                _ => {
                    // If we parsed a CONSTRAINT name but no constraint type, error
                    if name.is_some() {
                        return Err(ParseError {
                            message: "Expected constraint type after CONSTRAINT name".to_string(),
                        });
                    }
                    break;
                }
            }
        }

        Ok((constraints, default_value))
    }

    /// Parse table-level constraints (PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK)
    pub(in crate::parser) fn parse_table_constraint(
        &mut self,
    ) -> Result<vibesql_ast::TableConstraint, ParseError> {
        // Check for optional CONSTRAINT keyword
        let name = if self.peek_keyword(Keyword::Constraint) {
            self.advance(); // consume CONSTRAINT
            match self.peek() {
                Token::Identifier(n) => {
                    let constraint_name = n.clone();
                    self.advance();
                    Some(constraint_name)
                }
                _ => {
                    return Err(ParseError {
                        message: "Expected constraint name after CONSTRAINT".to_string(),
                    })
                }
            }
        } else {
            None
        };

        let kind = match self.peek() {
            Token::Keyword { keyword: Keyword::Primary, .. } => {
                self.advance(); // consume PRIMARY
                self.expect_keyword(Keyword::Key)?;
                self.expect_token(Token::LParen)?;

                let mut columns = Vec::new();
                loop {
                    columns.push(self.parse_index_column_spec()?);

                    if matches!(self.peek(), Token::Comma) {
                        self.advance();
                    } else {
                        break;
                    }
                }

                self.expect_token(Token::RParen)?;
                // Validate no expressions in PRIMARY KEY constraint
                validate_no_expressions_in_constraint(&columns)?;
                // Parse optional ON CONFLICT clause
                let on_conflict = self.parse_constraint_conflict_clause()?;
                vibesql_ast::TableConstraintKind::PrimaryKey { columns, on_conflict }
            }
            Token::Keyword { keyword: Keyword::Foreign, .. } => {
                self.advance(); // consume FOREIGN
                self.expect_keyword(Keyword::Key)?;
                self.expect_token(Token::LParen)?;

                let mut columns = Vec::new();
                loop {
                    // Accept both regular and quoted identifiers for column names
                    let col = self.parse_identifier().map_err(|_| ParseError {
                        message: "Expected column name in FOREIGN KEY".to_string(),
                    })?;
                    columns.push(col);

                    if matches!(self.peek(), Token::Comma) {
                        self.advance();
                    } else {
                        break;
                    }
                }

                self.expect_token(Token::RParen)?;
                self.expect_keyword(Keyword::References)?;

                // Accept both regular and quoted identifiers for table names
                let references_table = self.parse_identifier().map_err(|_| ParseError {
                    message: "Expected table name after REFERENCES".to_string(),
                })?;

                // Column specification is optional - if omitted, defaults to PK
                let mut references_columns = Vec::new();
                if self.peek() == &Token::LParen {
                    self.advance(); // consume LParen

                    loop {
                        // Accept both regular and quoted identifiers for column names
                        let col = self.parse_identifier().map_err(|_| ParseError {
                            message: "Expected column name in REFERENCES".to_string(),
                        })?;
                        references_columns.push(col);

                        if matches!(self.peek(), Token::Comma) {
                            self.advance();
                        } else {
                            break;
                        }
                    }

                    self.expect_token(Token::RParen)?;
                }

                let (on_delete, on_update) = self.parse_referential_actions()?;
                let deferral = self.parse_constraint_deferral()?;

                vibesql_ast::TableConstraintKind::ForeignKey {
                    columns,
                    references_table,
                    references_columns,
                    on_delete,
                    on_update,
                    deferral,
                }
            }
            Token::Keyword { keyword: Keyword::Unique, .. } => {
                self.advance(); // consume UNIQUE
                self.expect_token(Token::LParen)?;

                let mut columns = Vec::new();
                loop {
                    columns.push(self.parse_index_column_spec()?);

                    if matches!(self.peek(), Token::Comma) {
                        self.advance();
                    } else {
                        break;
                    }
                }

                self.expect_token(Token::RParen)?;
                // Validate no expressions in UNIQUE constraint
                validate_no_expressions_in_constraint(&columns)?;
                // Parse optional ON CONFLICT clause
                let on_conflict = self.parse_constraint_conflict_clause()?;
                vibesql_ast::TableConstraintKind::Unique { columns, on_conflict }
            }
            Token::Keyword { keyword: Keyword::Check, .. } => {
                self.advance(); // consume CHECK
                self.expect_token(Token::LParen)?;
                let expr = self.parse_expression()?;
                self.expect_token(Token::RParen)?;
                vibesql_ast::TableConstraintKind::Check { expr: Box::new(expr) }
            }
            Token::Keyword { keyword: Keyword::Fulltext, .. } => {
                self.advance(); // consume FULLTEXT

                // Optional INDEX keyword
                if self.peek_keyword(Keyword::Index) {
                    self.advance(); // consume INDEX
                }

                // Optional index name
                let index_name = if matches!(self.peek(), Token::Identifier(_)) {
                    // Look ahead to see if next token is LParen
                    if self.position + 1 < self.tokens.len()
                        && matches!(self.tokens[self.position + 1], Token::LParen)
                    {
                        let name = match self.peek() {
                            Token::Identifier(n) => Some(n.clone()),
                            _ => None,
                        };
                        if name.is_some() {
                            self.advance(); // consume index name
                        }
                        name
                    } else {
                        None
                    }
                } else {
                    None
                };

                self.expect_token(Token::LParen)?;

                let mut columns = Vec::new();
                loop {
                    columns.push(self.parse_index_column_spec()?);

                    if matches!(self.peek(), Token::Comma) {
                        self.advance();
                    } else {
                        break;
                    }
                }

                self.expect_token(Token::RParen)?;
                vibesql_ast::TableConstraintKind::Fulltext { index_name, columns }
            }
            _ => return Err(ParseError {
                message:
                    "Expected table constraint keyword (PRIMARY, FOREIGN, UNIQUE, CHECK, FULLTEXT)"
                        .to_string(),
            }),
        };

        Ok(vibesql_ast::TableConstraint { name, kind })
    }
}