Skip to main content

rigsql_parser/
grammar.rs

1use rigsql_core::{NodeSegment, Segment, SegmentType, Token, TokenKind, TokenSegment};
2
3use crate::context::ParseContext;
4
5/// Grammar provides methods to parse SQL constructs into CST segments.
6///
7/// Each parse method returns `Option<Segment>` — `None` means the
8/// construct was not found at the current position, and the cursor
9/// is left unchanged (backtracking).
10pub struct Grammar;
11
12impl Grammar {
13    // ── Top-level ────────────────────────────────────────────────
14
15    /// Parse a complete SQL file: zero or more statements.
16    pub fn parse_file<'a>(ctx: &mut ParseContext<'a>) -> Segment {
17        let mut children = Vec::new();
18        while !ctx.at_eof() {
19            children.extend(Self::eat_trivia_segments(ctx));
20            if ctx.at_eof() {
21                break;
22            }
23            if let Some(stmt) = Self::parse_statement(ctx) {
24                children.push(stmt);
25            } else {
26                // Consume unparsable token to avoid infinite loop
27                children.extend(Self::eat_trivia_segments(ctx));
28                if !ctx.at_eof() {
29                    if let Some(token) = ctx.advance() {
30                        children.push(Self::unparsable_token(token));
31                    }
32                }
33            }
34        }
35        Segment::Node(NodeSegment::new(SegmentType::File, children))
36    }
37
38    /// Parse a single statement (terminated by `;` or EOF).
39    pub fn parse_statement<'a>(ctx: &mut ParseContext<'a>) -> Option<Segment> {
40        let save = ctx.save();
41        let mut children = Vec::new();
42        children.extend(Self::eat_trivia_segments(ctx));
43
44        let inner = if ctx.peek_keyword("SELECT") || ctx.peek_keyword("WITH") {
45            Self::parse_select_statement(ctx)
46        } else if ctx.peek_keyword("INSERT") {
47            Self::parse_insert_statement(ctx)
48        } else if ctx.peek_keyword("UPDATE") {
49            Self::parse_update_statement(ctx)
50        } else if ctx.peek_keyword("DELETE") {
51            Self::parse_delete_statement(ctx)
52        } else if ctx.peek_keyword("CREATE") {
53            Self::parse_create_statement(ctx)
54        } else if ctx.peek_keyword("DROP") {
55            Self::parse_drop_statement(ctx)
56        } else if ctx.peek_keyword("ALTER") {
57            Self::parse_alter_statement(ctx)
58        } else {
59            None
60        };
61
62        match inner {
63            Some(stmt_seg) => {
64                children.push(stmt_seg);
65                // Optional trailing semicolon
66                children.extend(Self::eat_trivia_segments(ctx));
67                if let Some(semi) = ctx.eat_kind(TokenKind::Semicolon) {
68                    children.push(Self::token_segment(semi, SegmentType::Semicolon));
69                }
70                Some(Segment::Node(NodeSegment::new(
71                    SegmentType::Statement,
72                    children,
73                )))
74            }
75            None => {
76                ctx.restore(save);
77                None
78            }
79        }
80    }
81
82    // ── SELECT ───────────────────────────────────────────────────
83
84    fn parse_select_statement<'a>(ctx: &mut ParseContext<'a>) -> Option<Segment> {
85        let mut children = Vec::new();
86
87        // WITH clause (optional)
88        if ctx.peek_keyword("WITH") {
89            if let Some(with) = Self::parse_with_clause(ctx) {
90                children.push(with);
91                children.extend(Self::eat_trivia_segments(ctx));
92            }
93        }
94
95        // SELECT clause (required)
96        let select = Self::parse_select_clause(ctx)?;
97        children.push(select);
98
99        // FROM clause (optional)
100        children.extend(Self::eat_trivia_segments(ctx));
101        if ctx.peek_keyword("FROM") {
102            if let Some(from) = Self::parse_from_clause(ctx) {
103                children.push(from);
104            }
105        }
106
107        // WHERE clause (optional)
108        children.extend(Self::eat_trivia_segments(ctx));
109        if ctx.peek_keyword("WHERE") {
110            if let Some(wh) = Self::parse_where_clause(ctx) {
111                children.push(wh);
112            }
113        }
114
115        // GROUP BY clause (optional)
116        children.extend(Self::eat_trivia_segments(ctx));
117        if ctx.peek_keywords(&["GROUP", "BY"]) {
118            if let Some(gb) = Self::parse_group_by_clause(ctx) {
119                children.push(gb);
120            }
121        }
122
123        // HAVING clause (optional)
124        children.extend(Self::eat_trivia_segments(ctx));
125        if ctx.peek_keyword("HAVING") {
126            if let Some(hav) = Self::parse_having_clause(ctx) {
127                children.push(hav);
128            }
129        }
130
131        // ORDER BY clause (optional)
132        children.extend(Self::eat_trivia_segments(ctx));
133        if ctx.peek_keywords(&["ORDER", "BY"]) {
134            if let Some(ob) = Self::parse_order_by_clause(ctx) {
135                children.push(ob);
136            }
137        }
138
139        // LIMIT clause (optional)
140        children.extend(Self::eat_trivia_segments(ctx));
141        if ctx.peek_keyword("LIMIT") {
142            if let Some(lim) = Self::parse_limit_clause(ctx) {
143                children.push(lim);
144            }
145        }
146
147        // OFFSET clause (optional)
148        children.extend(Self::eat_trivia_segments(ctx));
149        if ctx.peek_keyword("OFFSET") {
150            if let Some(off) = Self::parse_offset_clause(ctx) {
151                children.push(off);
152            }
153        }
154
155        // UNION / INTERSECT / EXCEPT (optional, recursive)
156        children.extend(Self::eat_trivia_segments(ctx));
157        if ctx.peek_keyword("UNION") || ctx.peek_keyword("INTERSECT") || ctx.peek_keyword("EXCEPT")
158        {
159            if let Some(set_op) = Self::parse_set_operation(ctx) {
160                children.push(set_op);
161            }
162        }
163
164        Some(Segment::Node(NodeSegment::new(
165            SegmentType::SelectStatement,
166            children,
167        )))
168    }
169
170    fn parse_select_clause<'a>(ctx: &mut ParseContext<'a>) -> Option<Segment> {
171        let mut children = Vec::new();
172
173        let kw = ctx.eat_keyword("SELECT")?;
174        children.push(Self::token_segment(kw, SegmentType::Keyword));
175
176        children.extend(Self::eat_trivia_segments(ctx));
177
178        // DISTINCT / ALL (optional)
179        if ctx.peek_keyword("DISTINCT") || ctx.peek_keyword("ALL") {
180            if let Some(token) = ctx.advance() {
181                children.push(Self::token_segment(token, SegmentType::Keyword));
182                children.extend(Self::eat_trivia_segments(ctx));
183            }
184        }
185
186        // TOP (N) / TOP N (TSQL)
187        if ctx.peek_keyword("TOP") {
188            if let Some(top_kw) = ctx.advance() {
189                children.push(Self::token_segment(top_kw, SegmentType::Keyword));
190                children.extend(Self::eat_trivia_segments(ctx));
191                // TOP (expr) or TOP N
192                if let Some(lparen) = ctx.eat_kind(TokenKind::LParen) {
193                    children.push(Self::token_segment(lparen, SegmentType::LParen));
194                    children.extend(Self::eat_trivia_segments(ctx));
195                    if let Some(expr) = Self::parse_expression(ctx) {
196                        children.push(expr);
197                    }
198                    children.extend(Self::eat_trivia_segments(ctx));
199                    if let Some(rparen) = ctx.eat_kind(TokenKind::RParen) {
200                        children.push(Self::token_segment(rparen, SegmentType::RParen));
201                    }
202                } else if let Some(num) = ctx.eat_kind(TokenKind::NumberLiteral) {
203                    children.push(Self::token_segment(num, SegmentType::Literal));
204                }
205                children.extend(Self::eat_trivia_segments(ctx));
206            }
207        }
208
209        // Select targets (comma-separated expressions)
210        if let Some(expr) = Self::parse_select_target(ctx) {
211            children.push(expr);
212        }
213        loop {
214            children.extend(Self::eat_trivia_segments(ctx));
215            if let Some(comma) = ctx.eat_kind(TokenKind::Comma) {
216                children.push(Self::token_segment(comma, SegmentType::Comma));
217                children.extend(Self::eat_trivia_segments(ctx));
218                if let Some(expr) = Self::parse_select_target(ctx) {
219                    children.push(expr);
220                }
221            } else {
222                break;
223            }
224        }
225
226        Some(Segment::Node(NodeSegment::new(
227            SegmentType::SelectClause,
228            children,
229        )))
230    }
231
232    fn parse_select_target<'a>(ctx: &mut ParseContext<'a>) -> Option<Segment> {
233        // Parse an expression, optionally followed by alias (AS alias_name or just alias_name)
234        let expr = Self::parse_expression(ctx)?;
235
236        let save = ctx.save();
237        let trivia = Self::eat_trivia_segments(ctx);
238
239        // Check for alias: AS name, or just a word that's not a keyword
240        if ctx.peek_keyword("AS") {
241            let mut children = vec![expr];
242            children.extend(trivia);
243            let as_kw = ctx.advance().unwrap();
244            children.push(Self::token_segment(as_kw, SegmentType::Keyword));
245            children.extend(Self::eat_trivia_segments(ctx));
246            if let Some(alias) = Self::parse_identifier(ctx) {
247                children.push(alias);
248            }
249            return Some(Segment::Node(NodeSegment::new(
250                SegmentType::AliasExpression,
251                children,
252            )));
253        }
254
255        // Implicit alias: a bare word that's not a clause keyword
256        if let Some(t) = ctx.peek_non_trivia() {
257            if t.kind == TokenKind::Word && !Self::is_clause_keyword(&t.text) {
258                let mut children = vec![expr];
259                children.extend(trivia);
260                if let Some(alias) = Self::parse_identifier(ctx) {
261                    children.push(alias);
262                }
263                return Some(Segment::Node(NodeSegment::new(
264                    SegmentType::AliasExpression,
265                    children,
266                )));
267            }
268        }
269
270        ctx.restore(save);
271        // Re-eat trivia would happen naturally at calling site
272        Some(expr)
273    }
274
275    fn parse_from_clause<'a>(ctx: &mut ParseContext<'a>) -> Option<Segment> {
276        let mut children = Vec::new();
277        let kw = ctx.eat_keyword("FROM")?;
278        children.push(Self::token_segment(kw, SegmentType::Keyword));
279        children.extend(Self::eat_trivia_segments(ctx));
280
281        // Table references (comma-separated)
282        if let Some(tref) = Self::parse_table_reference(ctx) {
283            children.push(tref);
284        }
285        loop {
286            let save = ctx.save();
287            let trivia = Self::eat_trivia_segments(ctx);
288            if let Some(comma) = ctx.eat_kind(TokenKind::Comma) {
289                children.extend(trivia);
290                children.push(Self::token_segment(comma, SegmentType::Comma));
291                children.extend(Self::eat_trivia_segments(ctx));
292                if let Some(tref) = Self::parse_table_reference(ctx) {
293                    children.push(tref);
294                }
295            } else {
296                ctx.restore(save);
297                break;
298            }
299        }
300
301        // JOIN clauses
302        loop {
303            children.extend(Self::eat_trivia_segments(ctx));
304            if Self::peek_join_keyword(ctx) {
305                if let Some(join) = Self::parse_join_clause(ctx) {
306                    children.push(join);
307                } else {
308                    break;
309                }
310            } else {
311                break;
312            }
313        }
314
315        Some(Segment::Node(NodeSegment::new(
316            SegmentType::FromClause,
317            children,
318        )))
319    }
320
321    fn parse_table_reference<'a>(ctx: &mut ParseContext<'a>) -> Option<Segment> {
322        let save = ctx.save();
323
324        // Subquery in parens
325        if ctx.peek_kind() == Some(TokenKind::LParen) {
326            if let Some(subq) = Self::parse_paren_subquery(ctx) {
327                // Optional alias
328                let save2 = ctx.save();
329                let trivia = Self::eat_trivia_segments(ctx);
330                if ctx.peek_keyword("AS")
331                    || ctx.peek_non_trivia().is_some_and(|t| {
332                        t.kind == TokenKind::Word && !Self::is_clause_keyword(&t.text)
333                    })
334                {
335                    let mut children = vec![subq];
336                    children.extend(trivia);
337                    if ctx.peek_keyword("AS") {
338                        let kw = ctx.advance().unwrap();
339                        children.push(Self::token_segment(kw, SegmentType::Keyword));
340                        children.extend(Self::eat_trivia_segments(ctx));
341                    }
342                    if let Some(alias) = Self::parse_identifier(ctx) {
343                        children.push(alias);
344                    }
345                    return Some(Segment::Node(NodeSegment::new(
346                        SegmentType::AliasExpression,
347                        children,
348                    )));
349                }
350                ctx.restore(save2);
351                return Some(subq);
352            }
353        }
354
355        // Table name (possibly qualified: schema.table)
356        let name = Self::parse_qualified_name(ctx);
357        if name.is_none() {
358            ctx.restore(save);
359            return None;
360        }
361        let name = name.unwrap();
362
363        // Optional alias
364        let save2 = ctx.save();
365        let trivia = Self::eat_trivia_segments(ctx);
366        if ctx.peek_keyword("AS") {
367            let mut children = vec![name];
368            children.extend(trivia);
369            let kw = ctx.advance().unwrap();
370            children.push(Self::token_segment(kw, SegmentType::Keyword));
371            children.extend(Self::eat_trivia_segments(ctx));
372            if let Some(alias) = Self::parse_identifier(ctx) {
373                children.push(alias);
374            }
375            return Some(Segment::Node(NodeSegment::new(
376                SegmentType::AliasExpression,
377                children,
378            )));
379        }
380        if let Some(t) = ctx.peek_non_trivia() {
381            if t.kind == TokenKind::Word
382                && !Self::is_clause_keyword(&t.text)
383                && !Self::is_join_keyword(&t.text)
384            {
385                let mut children = vec![name];
386                children.extend(trivia);
387                if let Some(alias) = Self::parse_identifier(ctx) {
388                    children.push(alias);
389                }
390                return Some(Segment::Node(NodeSegment::new(
391                    SegmentType::AliasExpression,
392                    children,
393                )));
394            }
395        }
396        ctx.restore(save2);
397        Some(name)
398    }
399
400    fn parse_where_clause<'a>(ctx: &mut ParseContext<'a>) -> Option<Segment> {
401        let mut children = Vec::new();
402        let kw = ctx.eat_keyword("WHERE")?;
403        children.push(Self::token_segment(kw, SegmentType::Keyword));
404        children.extend(Self::eat_trivia_segments(ctx));
405        if let Some(expr) = Self::parse_expression(ctx) {
406            children.push(expr);
407        }
408        Some(Segment::Node(NodeSegment::new(
409            SegmentType::WhereClause,
410            children,
411        )))
412    }
413
414    fn parse_group_by_clause<'a>(ctx: &mut ParseContext<'a>) -> Option<Segment> {
415        let mut children = Vec::new();
416        let group_kw = ctx.eat_keyword("GROUP")?;
417        children.push(Self::token_segment(group_kw, SegmentType::Keyword));
418        children.extend(Self::eat_trivia_segments(ctx));
419        let by_kw = ctx.eat_keyword("BY")?;
420        children.push(Self::token_segment(by_kw, SegmentType::Keyword));
421        children.extend(Self::eat_trivia_segments(ctx));
422
423        // Comma-separated expressions
424        if let Some(expr) = Self::parse_expression(ctx) {
425            children.push(expr);
426        }
427        loop {
428            let save = ctx.save();
429            let trivia = Self::eat_trivia_segments(ctx);
430            if let Some(comma) = ctx.eat_kind(TokenKind::Comma) {
431                children.extend(trivia);
432                children.push(Self::token_segment(comma, SegmentType::Comma));
433                children.extend(Self::eat_trivia_segments(ctx));
434                if let Some(expr) = Self::parse_expression(ctx) {
435                    children.push(expr);
436                }
437            } else {
438                ctx.restore(save);
439                break;
440            }
441        }
442
443        Some(Segment::Node(NodeSegment::new(
444            SegmentType::GroupByClause,
445            children,
446        )))
447    }
448
449    fn parse_having_clause<'a>(ctx: &mut ParseContext<'a>) -> Option<Segment> {
450        let mut children = Vec::new();
451        let kw = ctx.eat_keyword("HAVING")?;
452        children.push(Self::token_segment(kw, SegmentType::Keyword));
453        children.extend(Self::eat_trivia_segments(ctx));
454        if let Some(expr) = Self::parse_expression(ctx) {
455            children.push(expr);
456        }
457        Some(Segment::Node(NodeSegment::new(
458            SegmentType::HavingClause,
459            children,
460        )))
461    }
462
463    fn parse_order_by_clause<'a>(ctx: &mut ParseContext<'a>) -> Option<Segment> {
464        let mut children = Vec::new();
465        let order_kw = ctx.eat_keyword("ORDER")?;
466        children.push(Self::token_segment(order_kw, SegmentType::Keyword));
467        children.extend(Self::eat_trivia_segments(ctx));
468        let by_kw = ctx.eat_keyword("BY")?;
469        children.push(Self::token_segment(by_kw, SegmentType::Keyword));
470        children.extend(Self::eat_trivia_segments(ctx));
471
472        // Comma-separated order expressions
473        if let Some(expr) = Self::parse_order_expression(ctx) {
474            children.push(expr);
475        }
476        loop {
477            let save = ctx.save();
478            let trivia = Self::eat_trivia_segments(ctx);
479            if let Some(comma) = ctx.eat_kind(TokenKind::Comma) {
480                children.extend(trivia);
481                children.push(Self::token_segment(comma, SegmentType::Comma));
482                children.extend(Self::eat_trivia_segments(ctx));
483                if let Some(expr) = Self::parse_order_expression(ctx) {
484                    children.push(expr);
485                }
486            } else {
487                ctx.restore(save);
488                break;
489            }
490        }
491
492        Some(Segment::Node(NodeSegment::new(
493            SegmentType::OrderByClause,
494            children,
495        )))
496    }
497
498    fn parse_order_expression<'a>(ctx: &mut ParseContext<'a>) -> Option<Segment> {
499        let mut children = Vec::new();
500        let expr = Self::parse_expression(ctx)?;
501        children.push(expr);
502
503        let save = ctx.save();
504        let trivia = Self::eat_trivia_segments(ctx);
505        if ctx.peek_keyword("ASC") || ctx.peek_keyword("DESC") {
506            children.extend(trivia);
507            let kw = ctx.advance().unwrap();
508            children.push(Self::token_segment(kw, SegmentType::Keyword));
509        } else {
510            ctx.restore(save);
511        }
512
513        // NULLS FIRST / NULLS LAST
514        let save = ctx.save();
515        let trivia = Self::eat_trivia_segments(ctx);
516        if ctx.peek_keyword("NULLS") {
517            children.extend(trivia);
518            let kw = ctx.advance().unwrap();
519            children.push(Self::token_segment(kw, SegmentType::Keyword));
520            children.extend(Self::eat_trivia_segments(ctx));
521            if ctx.peek_keyword("FIRST") || ctx.peek_keyword("LAST") {
522                let kw = ctx.advance().unwrap();
523                children.push(Self::token_segment(kw, SegmentType::Keyword));
524            }
525        } else {
526            ctx.restore(save);
527        }
528
529        Some(Segment::Node(NodeSegment::new(
530            SegmentType::OrderByExpression,
531            children,
532        )))
533    }
534
535    fn parse_limit_clause<'a>(ctx: &mut ParseContext<'a>) -> Option<Segment> {
536        let mut children = Vec::new();
537        let kw = ctx.eat_keyword("LIMIT")?;
538        children.push(Self::token_segment(kw, SegmentType::Keyword));
539        children.extend(Self::eat_trivia_segments(ctx));
540        if let Some(expr) = Self::parse_expression(ctx) {
541            children.push(expr);
542        }
543        Some(Segment::Node(NodeSegment::new(
544            SegmentType::LimitClause,
545            children,
546        )))
547    }
548
549    fn parse_offset_clause<'a>(ctx: &mut ParseContext<'a>) -> Option<Segment> {
550        let mut children = Vec::new();
551        let kw = ctx.eat_keyword("OFFSET")?;
552        children.push(Self::token_segment(kw, SegmentType::Keyword));
553        children.extend(Self::eat_trivia_segments(ctx));
554        if let Some(expr) = Self::parse_expression(ctx) {
555            children.push(expr);
556        }
557        Some(Segment::Node(NodeSegment::new(
558            SegmentType::OffsetClause,
559            children,
560        )))
561    }
562
563    fn parse_with_clause<'a>(ctx: &mut ParseContext<'a>) -> Option<Segment> {
564        let mut children = Vec::new();
565        let kw = ctx.eat_keyword("WITH")?;
566        children.push(Self::token_segment(kw, SegmentType::Keyword));
567        children.extend(Self::eat_trivia_segments(ctx));
568
569        // RECURSIVE (optional)
570        if ctx.peek_keyword("RECURSIVE") {
571            let kw = ctx.advance().unwrap();
572            children.push(Self::token_segment(kw, SegmentType::Keyword));
573            children.extend(Self::eat_trivia_segments(ctx));
574        }
575
576        // CTE definitions (comma-separated)
577        if let Some(cte) = Self::parse_cte_definition(ctx) {
578            children.push(cte);
579        }
580        loop {
581            let save = ctx.save();
582            let trivia = Self::eat_trivia_segments(ctx);
583            if let Some(comma) = ctx.eat_kind(TokenKind::Comma) {
584                children.extend(trivia);
585                children.push(Self::token_segment(comma, SegmentType::Comma));
586                children.extend(Self::eat_trivia_segments(ctx));
587                if let Some(cte) = Self::parse_cte_definition(ctx) {
588                    children.push(cte);
589                }
590            } else {
591                ctx.restore(save);
592                break;
593            }
594        }
595
596        Some(Segment::Node(NodeSegment::new(
597            SegmentType::WithClause,
598            children,
599        )))
600    }
601
602    fn parse_cte_definition<'a>(ctx: &mut ParseContext<'a>) -> Option<Segment> {
603        let mut children = Vec::new();
604        let name = Self::parse_identifier(ctx)?;
605        children.push(name);
606        children.extend(Self::eat_trivia_segments(ctx));
607
608        // Optional column list
609        if ctx.peek_kind() == Some(TokenKind::LParen) {
610            if let Some(cols) = Self::parse_paren_list(ctx) {
611                children.push(cols);
612                children.extend(Self::eat_trivia_segments(ctx));
613            }
614        }
615
616        let as_kw = ctx.eat_keyword("AS")?;
617        children.push(Self::token_segment(as_kw, SegmentType::Keyword));
618        children.extend(Self::eat_trivia_segments(ctx));
619
620        // ( subquery )
621        if let Some(subq) = Self::parse_paren_subquery(ctx) {
622            children.push(subq);
623        }
624
625        Some(Segment::Node(NodeSegment::new(
626            SegmentType::CteDefinition,
627            children,
628        )))
629    }
630
631    fn parse_set_operation<'a>(ctx: &mut ParseContext<'a>) -> Option<Segment> {
632        let mut children = Vec::new();
633
634        // UNION / INTERSECT / EXCEPT
635        let kw = ctx.advance()?;
636        children.push(Self::token_segment(kw, SegmentType::Keyword));
637        children.extend(Self::eat_trivia_segments(ctx));
638
639        // ALL / DISTINCT (optional)
640        if ctx.peek_keyword("ALL") || ctx.peek_keyword("DISTINCT") {
641            let kw = ctx.advance().unwrap();
642            children.push(Self::token_segment(kw, SegmentType::Keyword));
643            children.extend(Self::eat_trivia_segments(ctx));
644        }
645
646        // Following select
647        if let Some(sel) = Self::parse_select_statement(ctx) {
648            children.push(sel);
649        }
650
651        Some(Segment::Node(NodeSegment::new(
652            SegmentType::SelectStatement,
653            children,
654        )))
655    }
656
657    // ── JOIN ─────────────────────────────────────────────────────
658
659    fn peek_join_keyword(ctx: &ParseContext) -> bool {
660        if let Some(t) = ctx.peek_non_trivia() {
661            if t.kind == TokenKind::Word {
662                return Self::is_join_keyword(&t.text);
663            }
664        }
665        false
666    }
667
668    fn parse_join_clause<'a>(ctx: &mut ParseContext<'a>) -> Option<Segment> {
669        let mut children = Vec::new();
670
671        // Optional: INNER / LEFT / RIGHT / FULL / CROSS
672        if ctx.peek_keyword("INNER")
673            || ctx.peek_keyword("LEFT")
674            || ctx.peek_keyword("RIGHT")
675            || ctx.peek_keyword("FULL")
676            || ctx.peek_keyword("CROSS")
677        {
678            let kw = ctx.advance().unwrap();
679            children.push(Self::token_segment(kw, SegmentType::Keyword));
680            children.extend(Self::eat_trivia_segments(ctx));
681
682            // Optional: OUTER
683            if ctx.peek_keyword("OUTER") {
684                let kw = ctx.advance().unwrap();
685                children.push(Self::token_segment(kw, SegmentType::Keyword));
686                children.extend(Self::eat_trivia_segments(ctx));
687            }
688        }
689
690        let join_kw = ctx.eat_keyword("JOIN")?;
691        children.push(Self::token_segment(join_kw, SegmentType::Keyword));
692        children.extend(Self::eat_trivia_segments(ctx));
693
694        // Table reference
695        if let Some(tref) = Self::parse_table_reference(ctx) {
696            children.push(tref);
697        }
698
699        // ON or USING
700        children.extend(Self::eat_trivia_segments(ctx));
701        if ctx.peek_keyword("ON") {
702            let kw = ctx.advance().unwrap();
703            let mut on_children = vec![Self::token_segment(kw, SegmentType::Keyword)];
704            on_children.extend(Self::eat_trivia_segments(ctx));
705            if let Some(expr) = Self::parse_expression(ctx) {
706                on_children.push(expr);
707            }
708            children.push(Segment::Node(NodeSegment::new(
709                SegmentType::OnClause,
710                on_children,
711            )));
712        } else if ctx.peek_keyword("USING") {
713            let kw = ctx.advance().unwrap();
714            let mut using_children = vec![Self::token_segment(kw, SegmentType::Keyword)];
715            using_children.extend(Self::eat_trivia_segments(ctx));
716            if let Some(paren) = Self::parse_paren_list(ctx) {
717                using_children.push(paren);
718            }
719            children.push(Segment::Node(NodeSegment::new(
720                SegmentType::UsingClause,
721                using_children,
722            )));
723        }
724
725        Some(Segment::Node(NodeSegment::new(
726            SegmentType::JoinClause,
727            children,
728        )))
729    }
730
731    // ── INSERT ───────────────────────────────────────────────────
732
733    fn parse_insert_statement<'a>(ctx: &mut ParseContext<'a>) -> Option<Segment> {
734        let mut children = Vec::new();
735        let kw = ctx.eat_keyword("INSERT")?;
736        children.push(Self::token_segment(kw, SegmentType::Keyword));
737        children.extend(Self::eat_trivia_segments(ctx));
738
739        let into_kw = ctx.eat_keyword("INTO")?;
740        children.push(Self::token_segment(into_kw, SegmentType::Keyword));
741        children.extend(Self::eat_trivia_segments(ctx));
742
743        // Table name
744        if let Some(name) = Self::parse_qualified_name(ctx) {
745            children.push(name);
746        }
747        children.extend(Self::eat_trivia_segments(ctx));
748
749        // Optional column list
750        if ctx.peek_kind() == Some(TokenKind::LParen) {
751            if let Some(cols) = Self::parse_paren_list(ctx) {
752                children.push(cols);
753                children.extend(Self::eat_trivia_segments(ctx));
754            }
755        }
756
757        // VALUES or SELECT
758        if ctx.peek_keyword("VALUES") {
759            if let Some(vals) = Self::parse_values_clause(ctx) {
760                children.push(vals);
761            }
762        } else if ctx.peek_keyword("SELECT") || ctx.peek_keyword("WITH") {
763            if let Some(sel) = Self::parse_select_statement(ctx) {
764                children.push(sel);
765            }
766        }
767
768        Some(Segment::Node(NodeSegment::new(
769            SegmentType::InsertStatement,
770            children,
771        )))
772    }
773
774    fn parse_values_clause<'a>(ctx: &mut ParseContext<'a>) -> Option<Segment> {
775        let mut children = Vec::new();
776        let kw = ctx.eat_keyword("VALUES")?;
777        children.push(Self::token_segment(kw, SegmentType::Keyword));
778        children.extend(Self::eat_trivia_segments(ctx));
779
780        // Comma-separated (expr, expr, ...)
781        if let Some(row) = Self::parse_paren_list(ctx) {
782            children.push(row);
783        }
784        loop {
785            let save = ctx.save();
786            let trivia = Self::eat_trivia_segments(ctx);
787            if let Some(comma) = ctx.eat_kind(TokenKind::Comma) {
788                children.extend(trivia);
789                children.push(Self::token_segment(comma, SegmentType::Comma));
790                children.extend(Self::eat_trivia_segments(ctx));
791                if let Some(row) = Self::parse_paren_list(ctx) {
792                    children.push(row);
793                }
794            } else {
795                ctx.restore(save);
796                break;
797            }
798        }
799
800        Some(Segment::Node(NodeSegment::new(
801            SegmentType::ValuesClause,
802            children,
803        )))
804    }
805
806    // ── UPDATE ───────────────────────────────────────────────────
807
808    fn parse_update_statement<'a>(ctx: &mut ParseContext<'a>) -> Option<Segment> {
809        let mut children = Vec::new();
810        let kw = ctx.eat_keyword("UPDATE")?;
811        children.push(Self::token_segment(kw, SegmentType::Keyword));
812        children.extend(Self::eat_trivia_segments(ctx));
813
814        // Table name
815        if let Some(name) = Self::parse_table_reference(ctx) {
816            children.push(name);
817        }
818        children.extend(Self::eat_trivia_segments(ctx));
819
820        // SET clause
821        if ctx.peek_keyword("SET") {
822            if let Some(set) = Self::parse_set_clause(ctx) {
823                children.push(set);
824            }
825        }
826
827        // WHERE clause
828        children.extend(Self::eat_trivia_segments(ctx));
829        if ctx.peek_keyword("WHERE") {
830            if let Some(wh) = Self::parse_where_clause(ctx) {
831                children.push(wh);
832            }
833        }
834
835        Some(Segment::Node(NodeSegment::new(
836            SegmentType::UpdateStatement,
837            children,
838        )))
839    }
840
841    fn parse_set_clause<'a>(ctx: &mut ParseContext<'a>) -> Option<Segment> {
842        let mut children = Vec::new();
843        let kw = ctx.eat_keyword("SET")?;
844        children.push(Self::token_segment(kw, SegmentType::Keyword));
845        children.extend(Self::eat_trivia_segments(ctx));
846
847        // col = expr, ...
848        if let Some(assign) = Self::parse_expression(ctx) {
849            children.push(assign);
850        }
851        loop {
852            let save = ctx.save();
853            let trivia = Self::eat_trivia_segments(ctx);
854            if let Some(comma) = ctx.eat_kind(TokenKind::Comma) {
855                children.extend(trivia);
856                children.push(Self::token_segment(comma, SegmentType::Comma));
857                children.extend(Self::eat_trivia_segments(ctx));
858                if let Some(assign) = Self::parse_expression(ctx) {
859                    children.push(assign);
860                }
861            } else {
862                ctx.restore(save);
863                break;
864            }
865        }
866
867        Some(Segment::Node(NodeSegment::new(
868            SegmentType::SetClause,
869            children,
870        )))
871    }
872
873    // ── DELETE ────────────────────────────────────────────────────
874
875    fn parse_delete_statement<'a>(ctx: &mut ParseContext<'a>) -> Option<Segment> {
876        let mut children = Vec::new();
877        let kw = ctx.eat_keyword("DELETE")?;
878        children.push(Self::token_segment(kw, SegmentType::Keyword));
879        children.extend(Self::eat_trivia_segments(ctx));
880
881        // FROM
882        if ctx.peek_keyword("FROM") {
883            let from_kw = ctx.advance().unwrap();
884            children.push(Self::token_segment(from_kw, SegmentType::Keyword));
885            children.extend(Self::eat_trivia_segments(ctx));
886        }
887
888        // Table name
889        if let Some(name) = Self::parse_qualified_name(ctx) {
890            children.push(name);
891        }
892
893        // WHERE clause
894        children.extend(Self::eat_trivia_segments(ctx));
895        if ctx.peek_keyword("WHERE") {
896            if let Some(wh) = Self::parse_where_clause(ctx) {
897                children.push(wh);
898            }
899        }
900
901        Some(Segment::Node(NodeSegment::new(
902            SegmentType::DeleteStatement,
903            children,
904        )))
905    }
906
907    // ── DDL ──────────────────────────────────────────────────────
908
909    fn parse_create_statement<'a>(ctx: &mut ParseContext<'a>) -> Option<Segment> {
910        let mut children = Vec::new();
911        let kw = ctx.eat_keyword("CREATE")?;
912        children.push(Self::token_segment(kw, SegmentType::Keyword));
913        children.extend(Self::eat_trivia_segments(ctx));
914
915        if ctx.peek_keyword("TABLE") {
916            return Self::parse_create_table_body(ctx, children);
917        }
918
919        // For other CREATE statements, consume until semicolon or EOF
920        Self::consume_until_end(ctx, &mut children);
921        Some(Segment::Node(NodeSegment::new(
922            SegmentType::Statement,
923            children,
924        )))
925    }
926
927    fn parse_create_table_body<'a>(
928        ctx: &mut ParseContext<'a>,
929        mut children: Vec<Segment>,
930    ) -> Option<Segment> {
931        let kw = ctx.eat_keyword("TABLE")?;
932        children.push(Self::token_segment(kw, SegmentType::Keyword));
933        children.extend(Self::eat_trivia_segments(ctx));
934
935        // IF NOT EXISTS
936        if ctx.peek_keyword("IF") {
937            let kw = ctx.advance().unwrap();
938            children.push(Self::token_segment(kw, SegmentType::Keyword));
939            children.extend(Self::eat_trivia_segments(ctx));
940            if let Some(kw) = ctx.eat_keyword("NOT") {
941                children.push(Self::token_segment(kw, SegmentType::Keyword));
942                children.extend(Self::eat_trivia_segments(ctx));
943            }
944            if let Some(kw) = ctx.eat_keyword("EXISTS") {
945                children.push(Self::token_segment(kw, SegmentType::Keyword));
946                children.extend(Self::eat_trivia_segments(ctx));
947            }
948        }
949
950        // Table name
951        if let Some(name) = Self::parse_qualified_name(ctx) {
952            children.push(name);
953        }
954        children.extend(Self::eat_trivia_segments(ctx));
955
956        // Column definitions in parens
957        if ctx.peek_kind() == Some(TokenKind::LParen) {
958            if let Some(defs) = Self::parse_paren_block(ctx) {
959                children.push(defs);
960            }
961        }
962
963        Some(Segment::Node(NodeSegment::new(
964            SegmentType::CreateTableStatement,
965            children,
966        )))
967    }
968
969    fn parse_drop_statement<'a>(ctx: &mut ParseContext<'a>) -> Option<Segment> {
970        let mut children = Vec::new();
971        let kw = ctx.eat_keyword("DROP")?;
972        children.push(Self::token_segment(kw, SegmentType::Keyword));
973        children.extend(Self::eat_trivia_segments(ctx));
974
975        // Consume until semicolon
976        Self::consume_until_end(ctx, &mut children);
977        Some(Segment::Node(NodeSegment::new(
978            SegmentType::DropStatement,
979            children,
980        )))
981    }
982
983    fn parse_alter_statement<'a>(ctx: &mut ParseContext<'a>) -> Option<Segment> {
984        let mut children = Vec::new();
985        let kw = ctx.eat_keyword("ALTER")?;
986        children.push(Self::token_segment(kw, SegmentType::Keyword));
987        children.extend(Self::eat_trivia_segments(ctx));
988
989        Self::consume_until_end(ctx, &mut children);
990        Some(Segment::Node(NodeSegment::new(
991            SegmentType::AlterTableStatement,
992            children,
993        )))
994    }
995
996    // ── Expression parsing ───────────────────────────────────────
997
998    /// Parse an expression. This uses a simple precedence climbing approach.
999    pub fn parse_expression<'a>(ctx: &mut ParseContext<'a>) -> Option<Segment> {
1000        Self::parse_or_expression(ctx)
1001    }
1002
1003    fn parse_or_expression<'a>(ctx: &mut ParseContext<'a>) -> Option<Segment> {
1004        let mut left = Self::parse_and_expression(ctx)?;
1005        loop {
1006            let save = ctx.save();
1007            let trivia = Self::eat_trivia_segments(ctx);
1008            if ctx.peek_keyword("OR") {
1009                let op = ctx.advance().unwrap();
1010                let mut children = vec![left];
1011                children.extend(trivia);
1012                children.push(Self::token_segment(op, SegmentType::Keyword));
1013                children.extend(Self::eat_trivia_segments(ctx));
1014                if let Some(right) = Self::parse_and_expression(ctx) {
1015                    children.push(right);
1016                }
1017                left = Segment::Node(NodeSegment::new(SegmentType::BinaryExpression, children));
1018            } else {
1019                ctx.restore(save);
1020                break;
1021            }
1022        }
1023        Some(left)
1024    }
1025
1026    fn parse_and_expression<'a>(ctx: &mut ParseContext<'a>) -> Option<Segment> {
1027        let mut left = Self::parse_not_expression(ctx)?;
1028        loop {
1029            let save = ctx.save();
1030            let trivia = Self::eat_trivia_segments(ctx);
1031            if ctx.peek_keyword("AND") {
1032                let op = ctx.advance().unwrap();
1033                let mut children = vec![left];
1034                children.extend(trivia);
1035                children.push(Self::token_segment(op, SegmentType::Keyword));
1036                children.extend(Self::eat_trivia_segments(ctx));
1037                if let Some(right) = Self::parse_not_expression(ctx) {
1038                    children.push(right);
1039                }
1040                left = Segment::Node(NodeSegment::new(SegmentType::BinaryExpression, children));
1041            } else {
1042                ctx.restore(save);
1043                break;
1044            }
1045        }
1046        Some(left)
1047    }
1048
1049    fn parse_not_expression<'a>(ctx: &mut ParseContext<'a>) -> Option<Segment> {
1050        if ctx.peek_keyword("NOT") {
1051            let mut children = Vec::new();
1052            let kw = ctx.advance().unwrap();
1053            children.push(Self::token_segment(kw, SegmentType::Keyword));
1054            children.extend(Self::eat_trivia_segments(ctx));
1055            if let Some(expr) = Self::parse_not_expression(ctx) {
1056                children.push(expr);
1057            }
1058            return Some(Segment::Node(NodeSegment::new(
1059                SegmentType::UnaryExpression,
1060                children,
1061            )));
1062        }
1063        Self::parse_comparison_expression(ctx)
1064    }
1065
1066    fn parse_comparison_expression<'a>(ctx: &mut ParseContext<'a>) -> Option<Segment> {
1067        let left = Self::parse_addition_expression(ctx)?;
1068
1069        let save = ctx.save();
1070        let trivia = Self::eat_trivia_segments(ctx);
1071
1072        // IS [NOT] NULL
1073        if ctx.peek_keyword("IS") {
1074            let is_kw = ctx.advance().unwrap();
1075            let mut children = vec![left];
1076            children.extend(trivia);
1077            children.push(Self::token_segment(is_kw, SegmentType::Keyword));
1078            children.extend(Self::eat_trivia_segments(ctx));
1079            if ctx.peek_keyword("NOT") {
1080                let not_kw = ctx.advance().unwrap();
1081                children.push(Self::token_segment(not_kw, SegmentType::Keyword));
1082                children.extend(Self::eat_trivia_segments(ctx));
1083            }
1084            if ctx.peek_keyword("NULL") {
1085                let null_kw = ctx.advance().unwrap();
1086                children.push(Self::token_segment(null_kw, SegmentType::Keyword));
1087            }
1088            return Some(Segment::Node(NodeSegment::new(
1089                SegmentType::IsNullExpression,
1090                children,
1091            )));
1092        }
1093
1094        // [NOT] IN (...)
1095        if ctx.peek_keyword("IN") {
1096            let in_kw = ctx.advance().unwrap();
1097            let mut children = vec![left];
1098            children.extend(trivia);
1099            children.push(Self::token_segment(in_kw, SegmentType::Keyword));
1100            children.extend(Self::eat_trivia_segments(ctx));
1101            if ctx.peek_kind() == Some(TokenKind::LParen) {
1102                if let Some(list) = Self::parse_paren_block(ctx) {
1103                    children.push(list);
1104                }
1105            }
1106            return Some(Segment::Node(NodeSegment::new(
1107                SegmentType::InExpression,
1108                children,
1109            )));
1110        }
1111
1112        // NOT IN / NOT BETWEEN / NOT LIKE
1113        if ctx.peek_keyword("NOT") {
1114            let save_not = ctx.save();
1115            let not_kw = ctx.advance().unwrap();
1116            let not_trivia = Self::eat_trivia_segments(ctx);
1117
1118            if ctx.peek_keyword("IN") {
1119                let in_kw = ctx.advance().unwrap();
1120                let mut children = vec![left];
1121                children.extend(trivia);
1122                children.push(Self::token_segment(not_kw, SegmentType::Keyword));
1123                children.extend(not_trivia);
1124                children.push(Self::token_segment(in_kw, SegmentType::Keyword));
1125                children.extend(Self::eat_trivia_segments(ctx));
1126                if ctx.peek_kind() == Some(TokenKind::LParen) {
1127                    if let Some(list) = Self::parse_paren_block(ctx) {
1128                        children.push(list);
1129                    }
1130                }
1131                return Some(Segment::Node(NodeSegment::new(
1132                    SegmentType::InExpression,
1133                    children,
1134                )));
1135            }
1136            if ctx.peek_keyword("BETWEEN") {
1137                let kw = ctx.advance().unwrap();
1138                let mut children = vec![left];
1139                children.extend(trivia);
1140                children.push(Self::token_segment(not_kw, SegmentType::Keyword));
1141                children.extend(not_trivia);
1142                children.push(Self::token_segment(kw, SegmentType::Keyword));
1143                children.extend(Self::eat_trivia_segments(ctx));
1144                if let Some(lo) = Self::parse_addition_expression(ctx) {
1145                    children.push(lo);
1146                }
1147                children.extend(Self::eat_trivia_segments(ctx));
1148                if let Some(and_kw) = ctx.eat_keyword("AND") {
1149                    children.push(Self::token_segment(and_kw, SegmentType::Keyword));
1150                }
1151                children.extend(Self::eat_trivia_segments(ctx));
1152                if let Some(hi) = Self::parse_addition_expression(ctx) {
1153                    children.push(hi);
1154                }
1155                return Some(Segment::Node(NodeSegment::new(
1156                    SegmentType::BetweenExpression,
1157                    children,
1158                )));
1159            }
1160            if ctx.peek_keyword("LIKE") || ctx.peek_keyword("ILIKE") {
1161                let kw = ctx.advance().unwrap();
1162                let mut children = vec![left];
1163                children.extend(trivia);
1164                children.push(Self::token_segment(not_kw, SegmentType::Keyword));
1165                children.extend(not_trivia);
1166                children.push(Self::token_segment(kw, SegmentType::Keyword));
1167                children.extend(Self::eat_trivia_segments(ctx));
1168                if let Some(pattern) = Self::parse_addition_expression(ctx) {
1169                    children.push(pattern);
1170                }
1171                return Some(Segment::Node(NodeSegment::new(
1172                    SegmentType::LikeExpression,
1173                    children,
1174                )));
1175            }
1176
1177            // NOT was consumed but wasn't NOT IN/BETWEEN/LIKE — restore
1178            ctx.restore(save_not);
1179            ctx.restore(save);
1180            return Some(left);
1181        }
1182
1183        // BETWEEN ... AND ...
1184        if ctx.peek_keyword("BETWEEN") {
1185            let kw = ctx.advance().unwrap();
1186            let mut children = vec![left];
1187            children.extend(trivia);
1188            children.push(Self::token_segment(kw, SegmentType::Keyword));
1189            children.extend(Self::eat_trivia_segments(ctx));
1190            if let Some(lo) = Self::parse_addition_expression(ctx) {
1191                children.push(lo);
1192            }
1193            children.extend(Self::eat_trivia_segments(ctx));
1194            if let Some(and_kw) = ctx.eat_keyword("AND") {
1195                children.push(Self::token_segment(and_kw, SegmentType::Keyword));
1196            }
1197            children.extend(Self::eat_trivia_segments(ctx));
1198            if let Some(hi) = Self::parse_addition_expression(ctx) {
1199                children.push(hi);
1200            }
1201            return Some(Segment::Node(NodeSegment::new(
1202                SegmentType::BetweenExpression,
1203                children,
1204            )));
1205        }
1206
1207        // LIKE / ILIKE
1208        if ctx.peek_keyword("LIKE") || ctx.peek_keyword("ILIKE") {
1209            let kw = ctx.advance().unwrap();
1210            let mut children = vec![left];
1211            children.extend(trivia);
1212            children.push(Self::token_segment(kw, SegmentType::Keyword));
1213            children.extend(Self::eat_trivia_segments(ctx));
1214            if let Some(pattern) = Self::parse_addition_expression(ctx) {
1215                children.push(pattern);
1216            }
1217            return Some(Segment::Node(NodeSegment::new(
1218                SegmentType::LikeExpression,
1219                children,
1220            )));
1221        }
1222
1223        // Comparison operators: = <> != < > <= >=
1224        if let Some(kind) = ctx.peek_kind() {
1225            if matches!(
1226                kind,
1227                TokenKind::Eq
1228                    | TokenKind::Neq
1229                    | TokenKind::Lt
1230                    | TokenKind::Gt
1231                    | TokenKind::LtEq
1232                    | TokenKind::GtEq
1233            ) {
1234                let op = ctx.advance().unwrap();
1235                let mut children = vec![left];
1236                children.extend(trivia);
1237                children.push(Self::token_segment(op, SegmentType::ComparisonOperator));
1238                children.extend(Self::eat_trivia_segments(ctx));
1239                if let Some(right) = Self::parse_addition_expression(ctx) {
1240                    children.push(right);
1241                }
1242                return Some(Segment::Node(NodeSegment::new(
1243                    SegmentType::BinaryExpression,
1244                    children,
1245                )));
1246            }
1247        }
1248
1249        ctx.restore(save);
1250        Some(left)
1251    }
1252
1253    fn parse_addition_expression<'a>(ctx: &mut ParseContext<'a>) -> Option<Segment> {
1254        let mut left = Self::parse_multiplication_expression(ctx)?;
1255        loop {
1256            let save = ctx.save();
1257            let trivia = Self::eat_trivia_segments(ctx);
1258            if let Some(kind) = ctx.peek_kind() {
1259                if matches!(kind, TokenKind::Plus | TokenKind::Minus | TokenKind::Concat) {
1260                    let op = ctx.advance().unwrap();
1261                    let mut children = vec![left];
1262                    children.extend(trivia);
1263                    children.push(Self::token_segment(op, SegmentType::ArithmeticOperator));
1264                    children.extend(Self::eat_trivia_segments(ctx));
1265                    if let Some(right) = Self::parse_multiplication_expression(ctx) {
1266                        children.push(right);
1267                    }
1268                    left = Segment::Node(NodeSegment::new(SegmentType::BinaryExpression, children));
1269                    continue;
1270                }
1271            }
1272            ctx.restore(save);
1273            break;
1274        }
1275        Some(left)
1276    }
1277
1278    fn parse_multiplication_expression<'a>(ctx: &mut ParseContext<'a>) -> Option<Segment> {
1279        let mut left = Self::parse_unary_expression(ctx)?;
1280        loop {
1281            let save = ctx.save();
1282            let trivia = Self::eat_trivia_segments(ctx);
1283            if let Some(kind) = ctx.peek_kind() {
1284                if matches!(
1285                    kind,
1286                    TokenKind::Star | TokenKind::Slash | TokenKind::Percent
1287                ) {
1288                    let op = ctx.advance().unwrap();
1289                    let mut children = vec![left];
1290                    children.extend(trivia);
1291                    children.push(Self::token_segment(op, SegmentType::ArithmeticOperator));
1292                    children.extend(Self::eat_trivia_segments(ctx));
1293                    if let Some(right) = Self::parse_unary_expression(ctx) {
1294                        children.push(right);
1295                    }
1296                    left = Segment::Node(NodeSegment::new(SegmentType::BinaryExpression, children));
1297                    continue;
1298                }
1299            }
1300            ctx.restore(save);
1301            break;
1302        }
1303        Some(left)
1304    }
1305
1306    fn parse_unary_expression<'a>(ctx: &mut ParseContext<'a>) -> Option<Segment> {
1307        if let Some(kind) = ctx.peek_kind() {
1308            if matches!(kind, TokenKind::Plus | TokenKind::Minus) {
1309                let op = ctx.advance().unwrap();
1310                let mut children = vec![Self::token_segment(op, SegmentType::ArithmeticOperator)];
1311                children.extend(Self::eat_trivia_segments(ctx));
1312                if let Some(expr) = Self::parse_primary_expression(ctx) {
1313                    children.push(expr);
1314                }
1315                return Some(Segment::Node(NodeSegment::new(
1316                    SegmentType::UnaryExpression,
1317                    children,
1318                )));
1319            }
1320        }
1321        Self::parse_primary_expression(ctx)
1322    }
1323
1324    fn parse_primary_expression<'a>(ctx: &mut ParseContext<'a>) -> Option<Segment> {
1325        match ctx.peek_kind()? {
1326            // Parenthesized expression or subquery
1327            TokenKind::LParen => {
1328                // Check if it's a subquery
1329                let save = ctx.save();
1330                if let Some(subq) = Self::parse_paren_subquery(ctx) {
1331                    return Some(subq);
1332                }
1333                ctx.restore(save);
1334                Self::parse_paren_expression(ctx)
1335            }
1336
1337            // Number literal
1338            TokenKind::NumberLiteral => {
1339                let token = ctx.advance().unwrap();
1340                Some(Self::token_segment(token, SegmentType::NumericLiteral))
1341            }
1342
1343            // String literal
1344            TokenKind::StringLiteral => {
1345                let token = ctx.advance().unwrap();
1346                Some(Self::token_segment(token, SegmentType::StringLiteral))
1347            }
1348
1349            // Star (e.g. SELECT *)
1350            TokenKind::Star => {
1351                let token = ctx.advance().unwrap();
1352                Some(Self::token_segment(token, SegmentType::Star))
1353            }
1354
1355            // Placeholder
1356            TokenKind::Placeholder => {
1357                let token = ctx.advance().unwrap();
1358                Some(Self::token_segment(token, SegmentType::Literal))
1359            }
1360
1361            // Quoted identifier
1362            TokenKind::QuotedIdentifier => {
1363                let token = ctx.advance().unwrap();
1364                Some(Self::token_segment(token, SegmentType::QuotedIdentifier))
1365            }
1366
1367            // Word: keyword, function call, column ref, etc.
1368            TokenKind::Word => {
1369                let text = &ctx.peek().unwrap().text;
1370
1371                if text.eq_ignore_ascii_case("CASE") {
1372                    return Self::parse_case_expression(ctx);
1373                }
1374                if text.eq_ignore_ascii_case("EXISTS") {
1375                    return Self::parse_exists_expression(ctx);
1376                }
1377                if text.eq_ignore_ascii_case("CAST") {
1378                    return Self::parse_cast_expression(ctx);
1379                }
1380                if text.eq_ignore_ascii_case("TRUE") || text.eq_ignore_ascii_case("FALSE") {
1381                    let token = ctx.advance().unwrap();
1382                    return Some(Self::token_segment(token, SegmentType::BooleanLiteral));
1383                }
1384                if text.eq_ignore_ascii_case("NULL") {
1385                    let token = ctx.advance().unwrap();
1386                    return Some(Self::token_segment(token, SegmentType::NullLiteral));
1387                }
1388
1389                Self::parse_name_or_function(ctx)
1390            }
1391
1392            // @ variable (SQL Server)
1393            TokenKind::AtSign => {
1394                let token = ctx.advance().unwrap();
1395                Some(Self::token_segment(token, SegmentType::Identifier))
1396            }
1397
1398            _ => None,
1399        }
1400    }
1401
1402    fn parse_paren_expression<'a>(ctx: &mut ParseContext<'a>) -> Option<Segment> {
1403        let mut children = Vec::new();
1404        let lp = ctx.eat_kind(TokenKind::LParen)?;
1405        children.push(Self::token_segment(lp, SegmentType::LParen));
1406        children.extend(Self::eat_trivia_segments(ctx));
1407
1408        if let Some(expr) = Self::parse_expression(ctx) {
1409            children.push(expr);
1410        }
1411
1412        children.extend(Self::eat_trivia_segments(ctx));
1413        if let Some(rp) = ctx.eat_kind(TokenKind::RParen) {
1414            children.push(Self::token_segment(rp, SegmentType::RParen));
1415        }
1416
1417        Some(Segment::Node(NodeSegment::new(
1418            SegmentType::ParenExpression,
1419            children,
1420        )))
1421    }
1422
1423    fn parse_paren_subquery<'a>(ctx: &mut ParseContext<'a>) -> Option<Segment> {
1424        let save = ctx.save();
1425        let mut children = Vec::new();
1426        let lp = ctx.eat_kind(TokenKind::LParen)?;
1427        children.push(Self::token_segment(lp, SegmentType::LParen));
1428        children.extend(Self::eat_trivia_segments(ctx));
1429
1430        // Check if it's a SELECT or WITH inside
1431        if !ctx.peek_keyword("SELECT") && !ctx.peek_keyword("WITH") {
1432            ctx.restore(save);
1433            return None;
1434        }
1435
1436        if let Some(sel) = Self::parse_select_statement(ctx) {
1437            children.push(sel);
1438        } else {
1439            ctx.restore(save);
1440            return None;
1441        }
1442
1443        children.extend(Self::eat_trivia_segments(ctx));
1444        if let Some(rp) = ctx.eat_kind(TokenKind::RParen) {
1445            children.push(Self::token_segment(rp, SegmentType::RParen));
1446        }
1447
1448        Some(Segment::Node(NodeSegment::new(
1449            SegmentType::Subquery,
1450            children,
1451        )))
1452    }
1453
1454    fn parse_case_expression<'a>(ctx: &mut ParseContext<'a>) -> Option<Segment> {
1455        let mut children = Vec::new();
1456        let case_kw = ctx.eat_keyword("CASE")?;
1457        children.push(Self::token_segment(case_kw, SegmentType::Keyword));
1458        children.extend(Self::eat_trivia_segments(ctx));
1459
1460        // Simple CASE: CASE expr WHEN ...
1461        // Searched CASE: CASE WHEN ...
1462        if !ctx.peek_keyword("WHEN") {
1463            if let Some(expr) = Self::parse_expression(ctx) {
1464                children.push(expr);
1465                children.extend(Self::eat_trivia_segments(ctx));
1466            }
1467        }
1468
1469        // WHEN clauses
1470        while ctx.peek_keyword("WHEN") {
1471            if let Some(when) = Self::parse_when_clause(ctx) {
1472                children.push(when);
1473                children.extend(Self::eat_trivia_segments(ctx));
1474            }
1475        }
1476
1477        // ELSE clause
1478        if ctx.peek_keyword("ELSE") {
1479            let mut else_children = Vec::new();
1480            let kw = ctx.advance().unwrap();
1481            else_children.push(Self::token_segment(kw, SegmentType::Keyword));
1482            else_children.extend(Self::eat_trivia_segments(ctx));
1483            if let Some(expr) = Self::parse_expression(ctx) {
1484                else_children.push(expr);
1485            }
1486            children.push(Segment::Node(NodeSegment::new(
1487                SegmentType::ElseClause,
1488                else_children,
1489            )));
1490            children.extend(Self::eat_trivia_segments(ctx));
1491        }
1492
1493        // END
1494        if let Some(end_kw) = ctx.eat_keyword("END") {
1495            children.push(Self::token_segment(end_kw, SegmentType::Keyword));
1496        }
1497
1498        Some(Segment::Node(NodeSegment::new(
1499            SegmentType::CaseExpression,
1500            children,
1501        )))
1502    }
1503
1504    fn parse_when_clause<'a>(ctx: &mut ParseContext<'a>) -> Option<Segment> {
1505        let mut children = Vec::new();
1506        let kw = ctx.eat_keyword("WHEN")?;
1507        children.push(Self::token_segment(kw, SegmentType::Keyword));
1508        children.extend(Self::eat_trivia_segments(ctx));
1509
1510        if let Some(cond) = Self::parse_expression(ctx) {
1511            children.push(cond);
1512        }
1513        children.extend(Self::eat_trivia_segments(ctx));
1514
1515        if let Some(then_kw) = ctx.eat_keyword("THEN") {
1516            children.push(Self::token_segment(then_kw, SegmentType::Keyword));
1517        }
1518        children.extend(Self::eat_trivia_segments(ctx));
1519
1520        if let Some(result) = Self::parse_expression(ctx) {
1521            children.push(result);
1522        }
1523
1524        Some(Segment::Node(NodeSegment::new(
1525            SegmentType::WhenClause,
1526            children,
1527        )))
1528    }
1529
1530    fn parse_exists_expression<'a>(ctx: &mut ParseContext<'a>) -> Option<Segment> {
1531        let mut children = Vec::new();
1532        let kw = ctx.eat_keyword("EXISTS")?;
1533        children.push(Self::token_segment(kw, SegmentType::Keyword));
1534        children.extend(Self::eat_trivia_segments(ctx));
1535
1536        if let Some(subq) = Self::parse_paren_subquery(ctx) {
1537            children.push(subq);
1538        }
1539
1540        Some(Segment::Node(NodeSegment::new(
1541            SegmentType::ExistsExpression,
1542            children,
1543        )))
1544    }
1545
1546    fn parse_cast_expression<'a>(ctx: &mut ParseContext<'a>) -> Option<Segment> {
1547        let mut children = Vec::new();
1548        let kw = ctx.eat_keyword("CAST")?;
1549        children.push(Self::token_segment(kw, SegmentType::Keyword));
1550        children.extend(Self::eat_trivia_segments(ctx));
1551
1552        let lp = ctx.eat_kind(TokenKind::LParen)?;
1553        children.push(Self::token_segment(lp, SegmentType::LParen));
1554        children.extend(Self::eat_trivia_segments(ctx));
1555
1556        if let Some(expr) = Self::parse_expression(ctx) {
1557            children.push(expr);
1558        }
1559        children.extend(Self::eat_trivia_segments(ctx));
1560
1561        if let Some(as_kw) = ctx.eat_keyword("AS") {
1562            children.push(Self::token_segment(as_kw, SegmentType::Keyword));
1563        }
1564        children.extend(Self::eat_trivia_segments(ctx));
1565
1566        // Data type
1567        if let Some(dt) = Self::parse_data_type(ctx) {
1568            children.push(dt);
1569        }
1570        children.extend(Self::eat_trivia_segments(ctx));
1571
1572        if let Some(rp) = ctx.eat_kind(TokenKind::RParen) {
1573            children.push(Self::token_segment(rp, SegmentType::RParen));
1574        }
1575
1576        Some(Segment::Node(NodeSegment::new(
1577            SegmentType::CastExpression,
1578            children,
1579        )))
1580    }
1581
1582    fn parse_data_type<'a>(ctx: &mut ParseContext<'a>) -> Option<Segment> {
1583        let mut children = Vec::new();
1584
1585        // Type name (may be multi-word like "DOUBLE PRECISION", "CHARACTER VARYING")
1586        let word = ctx.eat_kind(TokenKind::Word)?;
1587        children.push(Self::token_segment(word, SegmentType::Keyword));
1588
1589        // Additional type words
1590        loop {
1591            let save = ctx.save();
1592            let trivia = Self::eat_trivia_segments(ctx);
1593            if let Some(t) = ctx.peek() {
1594                if t.kind == TokenKind::Word && !Self::is_clause_keyword(&t.text) {
1595                    children.extend(trivia);
1596                    let w = ctx.advance().unwrap();
1597                    children.push(Self::token_segment(w, SegmentType::Keyword));
1598                    continue;
1599                }
1600            }
1601            ctx.restore(save);
1602            break;
1603        }
1604
1605        // Optional (precision, scale)
1606        let save = ctx.save();
1607        let trivia = Self::eat_trivia_segments(ctx);
1608        if ctx.peek_kind() == Some(TokenKind::LParen) {
1609            children.extend(trivia);
1610            if let Some(params) = Self::parse_paren_block(ctx) {
1611                children.push(params);
1612            }
1613        } else {
1614            ctx.restore(save);
1615        }
1616
1617        Some(Segment::Node(NodeSegment::new(
1618            SegmentType::DataType,
1619            children,
1620        )))
1621    }
1622
1623    // ── Identifiers & names ──────────────────────────────────────
1624
1625    fn parse_identifier<'a>(ctx: &mut ParseContext<'a>) -> Option<Segment> {
1626        match ctx.peek_kind()? {
1627            TokenKind::Word => {
1628                let token = ctx.advance().unwrap();
1629                Some(Self::token_segment(token, SegmentType::Identifier))
1630            }
1631            TokenKind::QuotedIdentifier => {
1632                let token = ctx.advance().unwrap();
1633                Some(Self::token_segment(token, SegmentType::QuotedIdentifier))
1634            }
1635            _ => None,
1636        }
1637    }
1638
1639    /// Parse a possibly qualified name: a, a.b, a.b.c
1640    fn parse_qualified_name<'a>(ctx: &mut ParseContext<'a>) -> Option<Segment> {
1641        let first = Self::parse_identifier(ctx)?;
1642
1643        let save = ctx.save();
1644        if ctx.peek_kind() == Some(TokenKind::Dot) {
1645            let mut children = vec![first];
1646            while ctx.peek_kind() == Some(TokenKind::Dot) {
1647                let dot = ctx.advance().unwrap();
1648                children.push(Self::token_segment(dot, SegmentType::Dot));
1649                if let Some(part) = Self::parse_identifier(ctx) {
1650                    children.push(part);
1651                } else {
1652                    // Star: schema.table.*
1653                    if ctx.peek_kind() == Some(TokenKind::Star) {
1654                        let star = ctx.advance().unwrap();
1655                        children.push(Self::token_segment(star, SegmentType::Star));
1656                    }
1657                    break;
1658                }
1659            }
1660            Some(Segment::Node(NodeSegment::new(
1661                SegmentType::ColumnRef,
1662                children,
1663            )))
1664        } else {
1665            ctx.restore(save);
1666            Some(first)
1667        }
1668    }
1669
1670    fn parse_name_or_function<'a>(ctx: &mut ParseContext<'a>) -> Option<Segment> {
1671        let name = Self::parse_qualified_name(ctx)?;
1672
1673        // Check for function call: name(...)
1674        if ctx.peek_kind() == Some(TokenKind::LParen) {
1675            let mut children = vec![name];
1676            if let Some(args) = Self::parse_paren_block(ctx) {
1677                children.push(args);
1678            }
1679            let func = Segment::Node(NodeSegment::new(SegmentType::FunctionCall, children));
1680
1681            // Check for OVER clause (window function)
1682            let save = ctx.save();
1683            let trivia = Self::eat_trivia_segments(ctx);
1684            if ctx.peek_keyword("OVER") {
1685                let mut win_children = vec![func];
1686                win_children.extend(trivia);
1687                if let Some(over) = Self::parse_over_clause(ctx) {
1688                    win_children.push(over);
1689                }
1690                return Some(Segment::Node(NodeSegment::new(
1691                    SegmentType::WindowExpression,
1692                    win_children,
1693                )));
1694            }
1695            ctx.restore(save);
1696
1697            return Some(func);
1698        }
1699
1700        Some(name)
1701    }
1702
1703    /// Parse OVER clause: `OVER (PARTITION BY ... ORDER BY ... ROWS/RANGE ...)`
1704    /// or `OVER window_name`
1705    fn parse_over_clause<'a>(ctx: &mut ParseContext<'a>) -> Option<Segment> {
1706        let mut children = Vec::new();
1707        let over_kw = ctx.eat_keyword("OVER")?;
1708        children.push(Self::token_segment(over_kw, SegmentType::Keyword));
1709        children.extend(Self::eat_trivia_segments(ctx));
1710
1711        // OVER window_name (named window reference, no parens)
1712        if ctx.peek_kind() != Some(TokenKind::LParen) {
1713            if let Some(name) = Self::parse_identifier(ctx) {
1714                children.push(name);
1715            }
1716            return Some(Segment::Node(NodeSegment::new(
1717                SegmentType::OverClause,
1718                children,
1719            )));
1720        }
1721
1722        // OVER ( ... )
1723        let lp = ctx.eat_kind(TokenKind::LParen)?;
1724        children.push(Self::token_segment(lp, SegmentType::LParen));
1725        children.extend(Self::eat_trivia_segments(ctx));
1726
1727        // PARTITION BY ...
1728        if ctx.peek_keyword("PARTITION") {
1729            if let Some(pb) = Self::parse_partition_by_clause(ctx) {
1730                children.push(pb);
1731                children.extend(Self::eat_trivia_segments(ctx));
1732            }
1733        }
1734
1735        // ORDER BY ...
1736        if ctx.peek_keywords(&["ORDER", "BY"]) {
1737            if let Some(ob) = Self::parse_window_order_by(ctx) {
1738                children.push(ob);
1739                children.extend(Self::eat_trivia_segments(ctx));
1740            }
1741        }
1742
1743        // Window frame: ROWS / RANGE / GROUPS
1744        if ctx.peek_keyword("ROWS") || ctx.peek_keyword("RANGE") || ctx.peek_keyword("GROUPS") {
1745            if let Some(frame) = Self::parse_window_frame_clause(ctx) {
1746                children.push(frame);
1747                children.extend(Self::eat_trivia_segments(ctx));
1748            }
1749        }
1750
1751        if let Some(rp) = ctx.eat_kind(TokenKind::RParen) {
1752            children.push(Self::token_segment(rp, SegmentType::RParen));
1753        }
1754
1755        Some(Segment::Node(NodeSegment::new(
1756            SegmentType::OverClause,
1757            children,
1758        )))
1759    }
1760
1761    /// Parse PARTITION BY expr, expr, ...
1762    fn parse_partition_by_clause<'a>(ctx: &mut ParseContext<'a>) -> Option<Segment> {
1763        let mut children = Vec::new();
1764        let part_kw = ctx.eat_keyword("PARTITION")?;
1765        children.push(Self::token_segment(part_kw, SegmentType::Keyword));
1766        children.extend(Self::eat_trivia_segments(ctx));
1767
1768        if let Some(by_kw) = ctx.eat_keyword("BY") {
1769            children.push(Self::token_segment(by_kw, SegmentType::Keyword));
1770        }
1771        children.extend(Self::eat_trivia_segments(ctx));
1772
1773        // Comma-separated expressions
1774        if let Some(expr) = Self::parse_expression(ctx) {
1775            children.push(expr);
1776        }
1777        loop {
1778            let save = ctx.save();
1779            let trivia = Self::eat_trivia_segments(ctx);
1780            if let Some(comma) = ctx.eat_kind(TokenKind::Comma) {
1781                children.extend(trivia);
1782                children.push(Self::token_segment(comma, SegmentType::Comma));
1783                children.extend(Self::eat_trivia_segments(ctx));
1784                if let Some(expr) = Self::parse_expression(ctx) {
1785                    children.push(expr);
1786                }
1787            } else {
1788                ctx.restore(save);
1789                break;
1790            }
1791        }
1792
1793        Some(Segment::Node(NodeSegment::new(
1794            SegmentType::PartitionByClause,
1795            children,
1796        )))
1797    }
1798
1799    /// Parse ORDER BY inside a window spec (reuses expression parsing).
1800    fn parse_window_order_by<'a>(ctx: &mut ParseContext<'a>) -> Option<Segment> {
1801        // Delegate to the existing ORDER BY parser
1802        Self::parse_order_by_clause(ctx)
1803    }
1804
1805    /// Parse window frame: ROWS/RANGE/GROUPS frame_spec
1806    fn parse_window_frame_clause<'a>(ctx: &mut ParseContext<'a>) -> Option<Segment> {
1807        let mut children = Vec::new();
1808
1809        // ROWS | RANGE | GROUPS
1810        let frame_kw = ctx.advance()?;
1811        children.push(Self::token_segment(frame_kw, SegmentType::Keyword));
1812        children.extend(Self::eat_trivia_segments(ctx));
1813
1814        // BETWEEN ... AND ... or single bound
1815        if ctx.peek_keyword("BETWEEN") {
1816            let bw_kw = ctx.advance().unwrap();
1817            children.push(Self::token_segment(bw_kw, SegmentType::Keyword));
1818            children.extend(Self::eat_trivia_segments(ctx));
1819
1820            // start bound
1821            Self::eat_frame_bound(ctx, &mut children);
1822            children.extend(Self::eat_trivia_segments(ctx));
1823
1824            // AND
1825            if let Some(and_kw) = ctx.eat_keyword("AND") {
1826                children.push(Self::token_segment(and_kw, SegmentType::Keyword));
1827                children.extend(Self::eat_trivia_segments(ctx));
1828            }
1829
1830            // end bound
1831            Self::eat_frame_bound(ctx, &mut children);
1832        } else {
1833            // Single bound (e.g. ROWS UNBOUNDED PRECEDING)
1834            Self::eat_frame_bound(ctx, &mut children);
1835        }
1836
1837        Some(Segment::Node(NodeSegment::new(
1838            SegmentType::WindowFrameClause,
1839            children,
1840        )))
1841    }
1842
1843    /// Consume a frame bound: UNBOUNDED PRECEDING/FOLLOWING, CURRENT ROW, N PRECEDING/FOLLOWING
1844    fn eat_frame_bound(ctx: &mut ParseContext, children: &mut Vec<Segment>) {
1845        // CURRENT ROW
1846        if ctx.peek_keyword("CURRENT") {
1847            let kw = ctx.advance().unwrap();
1848            children.push(Self::token_segment(kw, SegmentType::Keyword));
1849            children.extend(Self::eat_trivia_segments(ctx));
1850            if ctx.peek_keyword("ROW") {
1851                let row_kw = ctx.advance().unwrap();
1852                children.push(Self::token_segment(row_kw, SegmentType::Keyword));
1853            }
1854            return;
1855        }
1856
1857        // UNBOUNDED PRECEDING/FOLLOWING
1858        if ctx.peek_keyword("UNBOUNDED") {
1859            let kw = ctx.advance().unwrap();
1860            children.push(Self::token_segment(kw, SegmentType::Keyword));
1861            children.extend(Self::eat_trivia_segments(ctx));
1862            if ctx.peek_keyword("PRECEDING") || ctx.peek_keyword("FOLLOWING") {
1863                let dir = ctx.advance().unwrap();
1864                children.push(Self::token_segment(dir, SegmentType::Keyword));
1865            }
1866            return;
1867        }
1868
1869        // N PRECEDING/FOLLOWING
1870        if ctx.peek_kind() == Some(TokenKind::NumberLiteral) {
1871            let num = ctx.advance().unwrap();
1872            children.push(Self::token_segment(num, SegmentType::NumericLiteral));
1873            children.extend(Self::eat_trivia_segments(ctx));
1874            if ctx.peek_keyword("PRECEDING") || ctx.peek_keyword("FOLLOWING") {
1875                let dir = ctx.advance().unwrap();
1876                children.push(Self::token_segment(dir, SegmentType::Keyword));
1877            }
1878        }
1879    }
1880
1881    // ── Utility parsing ──────────────────────────────────────────
1882
1883    /// Parse parenthesized content as a simple block.
1884    fn parse_paren_block<'a>(ctx: &mut ParseContext<'a>) -> Option<Segment> {
1885        let mut children = Vec::new();
1886        let lp = ctx.eat_kind(TokenKind::LParen)?;
1887        children.push(Self::token_segment(lp, SegmentType::LParen));
1888
1889        let mut depth = 1u32;
1890        while depth > 0 && !ctx.at_eof() {
1891            match ctx.peek_kind() {
1892                Some(TokenKind::LParen) => {
1893                    depth += 1;
1894                    let token = ctx.advance().unwrap();
1895                    children.push(Self::any_token_segment(token));
1896                }
1897                Some(TokenKind::RParen) => {
1898                    depth -= 1;
1899                    let token = ctx.advance().unwrap();
1900                    if depth == 0 {
1901                        children.push(Self::token_segment(token, SegmentType::RParen));
1902                    } else {
1903                        children.push(Self::any_token_segment(token));
1904                    }
1905                }
1906                _ => {
1907                    let token = ctx.advance().unwrap();
1908                    children.push(Self::any_token_segment(token));
1909                }
1910            }
1911        }
1912
1913        Some(Segment::Node(NodeSegment::new(
1914            SegmentType::ParenExpression,
1915            children,
1916        )))
1917    }
1918
1919    /// Parse parenthesized comma-separated list of identifiers/expressions.
1920    fn parse_paren_list<'a>(ctx: &mut ParseContext<'a>) -> Option<Segment> {
1921        Self::parse_paren_block(ctx)
1922    }
1923
1924    fn consume_until_end(ctx: &mut ParseContext, children: &mut Vec<Segment>) {
1925        while !ctx.at_eof() {
1926            if ctx.peek_kind() == Some(TokenKind::Semicolon) {
1927                break;
1928            }
1929            let token = ctx.advance().unwrap();
1930            children.push(Self::any_token_segment(token));
1931        }
1932    }
1933
1934    // ── Segment constructors ─────────────────────────────────────
1935
1936    fn token_segment(token: &Token, segment_type: SegmentType) -> Segment {
1937        Segment::Token(TokenSegment {
1938            token: token.clone(),
1939            segment_type,
1940        })
1941    }
1942
1943    fn any_token_segment(token: &Token) -> Segment {
1944        let st = match token.kind {
1945            TokenKind::Whitespace => SegmentType::Whitespace,
1946            TokenKind::Newline => SegmentType::Newline,
1947            TokenKind::LineComment => SegmentType::LineComment,
1948            TokenKind::BlockComment => SegmentType::BlockComment,
1949            TokenKind::Comma => SegmentType::Comma,
1950            TokenKind::Dot => SegmentType::Dot,
1951            TokenKind::Semicolon => SegmentType::Semicolon,
1952            TokenKind::Star => SegmentType::Star,
1953            TokenKind::LParen => SegmentType::LParen,
1954            TokenKind::RParen => SegmentType::RParen,
1955            TokenKind::NumberLiteral => SegmentType::NumericLiteral,
1956            TokenKind::StringLiteral => SegmentType::StringLiteral,
1957            TokenKind::Word => SegmentType::Keyword,
1958            TokenKind::QuotedIdentifier => SegmentType::QuotedIdentifier,
1959            _ => SegmentType::Operator,
1960        };
1961        Self::token_segment(token, st)
1962    }
1963
1964    fn unparsable_token(token: &Token) -> Segment {
1965        Segment::Token(TokenSegment {
1966            token: token.clone(),
1967            segment_type: SegmentType::Unparsable,
1968        })
1969    }
1970
1971    fn eat_trivia_segments(ctx: &mut ParseContext) -> Vec<Segment> {
1972        ctx.eat_trivia()
1973            .into_iter()
1974            .map(Self::any_token_segment)
1975            .collect()
1976    }
1977
1978    /// Sorted list of keywords that must NOT be consumed as implicit aliases.
1979    const CLAUSE_KEYWORDS: &[&str] = &[
1980        "ALTER",
1981        "AND",
1982        "AS",
1983        "BEGIN",
1984        "BETWEEN",
1985        "BREAK",
1986        "CASE",
1987        "CATCH",
1988        "CLOSE",
1989        "COMMIT",
1990        "CONTINUE",
1991        "CREATE",
1992        "CROSS",
1993        "CURSOR",
1994        "DEALLOCATE",
1995        "DECLARE",
1996        "DELETE",
1997        "DROP",
1998        "ELSE",
1999        "END",
2000        "EXCEPT",
2001        "EXEC",
2002        "EXECUTE",
2003        "EXISTS",
2004        "FETCH",
2005        "FOR",
2006        "FROM",
2007        "FULL",
2008        "GO",
2009        "GOTO",
2010        "GROUP",
2011        "HAVING",
2012        "IF",
2013        "IN",
2014        "INNER",
2015        "INSERT",
2016        "INTERSECT",
2017        "INTO",
2018        "IS",
2019        "JOIN",
2020        "LEFT",
2021        "LIKE",
2022        "LIMIT",
2023        "MERGE",
2024        "NEXT",
2025        "NOT",
2026        "OFFSET",
2027        "ON",
2028        "OPEN",
2029        "OR",
2030        "ORDER",
2031        "OUTPUT",
2032        "OVER",
2033        "PARTITION",
2034        "PRINT",
2035        "RAISERROR",
2036        "RETURN",
2037        "RETURNING",
2038        "RIGHT",
2039        "ROLLBACK",
2040        "SELECT",
2041        "SET",
2042        "TABLE",
2043        "THEN",
2044        "THROW",
2045        "TRUNCATE",
2046        "TRY",
2047        "UNION",
2048        "UPDATE",
2049        "USING",
2050        "VALUES",
2051        "WHEN",
2052        "WHERE",
2053        "WHILE",
2054        "WITH",
2055    ];
2056
2057    fn is_clause_keyword(word: &str) -> bool {
2058        let upper = word.to_ascii_uppercase();
2059        Self::CLAUSE_KEYWORDS.binary_search(&upper.as_str()).is_ok()
2060    }
2061
2062    fn is_join_keyword(word: &str) -> bool {
2063        word.eq_ignore_ascii_case("JOIN")
2064            || word.eq_ignore_ascii_case("INNER")
2065            || word.eq_ignore_ascii_case("LEFT")
2066            || word.eq_ignore_ascii_case("RIGHT")
2067            || word.eq_ignore_ascii_case("FULL")
2068            || word.eq_ignore_ascii_case("CROSS")
2069    }
2070}