sqruff_lib_dialects/
sqlite.rs

1use sqruff_lib_core::dialects::Dialect;
2use sqruff_lib_core::dialects::init::DialectKind;
3use sqruff_lib_core::dialects::syntax::SyntaxKind;
4use sqruff_lib_core::helpers::{Config, ToMatchable};
5use sqruff_lib_core::parser::grammar::anyof::{AnyNumberOf, one_of, optionally_bracketed};
6use sqruff_lib_core::parser::grammar::delimited::Delimited;
7use sqruff_lib_core::parser::grammar::sequence::{Bracketed, Sequence};
8use sqruff_lib_core::parser::grammar::{Anything, Nothing, Ref};
9use sqruff_lib_core::parser::lexer::Matcher;
10use sqruff_lib_core::parser::matchable::MatchableTrait;
11use sqruff_lib_core::parser::node_matcher::NodeMatcher;
12use sqruff_lib_core::parser::parsers::TypedParser;
13use sqruff_lib_core::parser::segments::meta::MetaSegment;
14use sqruff_lib_core::parser::types::ParseMode;
15
16use crate::sqlite_keywords::{RESERVED_KEYWORDS, UNRESERVED_KEYWORDS};
17
18pub fn dialect() -> Dialect {
19    raw_dialect().config(|dialect| dialect.expand())
20}
21
22pub fn raw_dialect() -> Dialect {
23    let sqlite_dialect = super::ansi::raw_dialect();
24    let mut sqlite_dialect = sqlite_dialect;
25    sqlite_dialect.name = DialectKind::Sqlite;
26
27    // Add lexer matchers for SQLite blob literals (X'...' or x'...')
28    // These must be inserted before single_quote to take precedence
29    sqlite_dialect.insert_lexer_matchers(
30        vec![Matcher::regex(
31            "bytes_single_quote",
32            r"[xX]'([^'\\]|\\.)*'",
33            SyntaxKind::BytesSingleQuote,
34        )],
35        "single_quote",
36    );
37
38    sqlite_dialect.sets_mut("reserved_keywords").clear();
39    sqlite_dialect
40        .sets_mut("reserved_keywords")
41        .extend(RESERVED_KEYWORDS);
42    sqlite_dialect.sets_mut("unreserved_keywords").clear();
43    sqlite_dialect
44        .sets_mut("unreserved_keywords")
45        .extend(UNRESERVED_KEYWORDS);
46
47    sqlite_dialect.add([
48        // SQLite blob literal segment (X'...' or x'...')
49        (
50            "BytesQuotedLiteralSegment".into(),
51            TypedParser::new(SyntaxKind::BytesSingleQuote, SyntaxKind::BytesQuotedLiteral)
52                .to_matchable()
53                .into(),
54        ),
55        // Extend LiteralGrammar to include blob literals
56        (
57            "LiteralGrammar".into(),
58            sqlite_dialect
59                .grammar("LiteralGrammar")
60                .copy(
61                    Some(vec![Ref::new("BytesQuotedLiteralSegment").to_matchable()]),
62                    None,
63                    None,
64                    None,
65                    Vec::new(),
66                    false,
67                )
68                .into(),
69        ),
70        (
71            "ColumnConstraintDefaultGrammar".into(),
72            Ref::new("ExpressionSegment").to_matchable().into(),
73        ),
74        (
75            "BooleanBinaryOperatorGrammar".into(),
76            one_of(vec![
77                Ref::new("AndOperatorGrammar").to_matchable(),
78                Ref::new("OrOperatorGrammar").to_matchable(),
79                Ref::keyword("REGEXP").to_matchable(),
80            ])
81            .to_matchable()
82            .into(),
83        ),
84        (
85            "PrimaryKeyGrammar".into(),
86            Sequence::new(vec![
87                Ref::keyword("PRIMARY").to_matchable(),
88                Ref::keyword("KEY").to_matchable(),
89                Sequence::new(vec![Ref::keyword("AUTOINCREMENT").to_matchable()])
90                    .config(|config| {
91                        config.optional();
92                    })
93                    .to_matchable(),
94            ])
95            .to_matchable()
96            .into(),
97        ),
98        (
99            "TemporaryTransientGrammar".into(),
100            Ref::new("TemporaryGrammar").to_matchable().into(),
101        ),
102        (
103            "DateTimeLiteralGrammar".into(),
104            Sequence::new(vec![
105                one_of(vec![
106                    Ref::keyword("DATE").to_matchable(),
107                    Ref::keyword("DATETIME").to_matchable(),
108                ])
109                .to_matchable(),
110                TypedParser::new(SyntaxKind::SingleQuote, SyntaxKind::DateConstructorLiteral)
111                    .to_matchable(),
112            ])
113            .to_matchable()
114            .into(),
115        ),
116        (
117            "BaseExpressionElementGrammar".into(),
118            one_of(vec![
119                Ref::new("LiteralGrammar").to_matchable(),
120                Ref::new("BareFunctionSegment").to_matchable(),
121                Ref::new("FunctionSegment").to_matchable(),
122                Ref::new("ColumnReferenceSegment").to_matchable(),
123                Ref::new("ExpressionSegment").to_matchable(),
124                Sequence::new(vec![
125                    Ref::new("DatatypeSegment").to_matchable(),
126                    Ref::new("LiteralGrammar").to_matchable(),
127                ])
128                .to_matchable(),
129            ])
130            .to_matchable()
131            .into(),
132        ),
133        (
134            "AutoIncrementGrammar".into(),
135            Nothing::new().to_matchable().into(),
136        ),
137        (
138            "CommentClauseSegment".into(),
139            Nothing::new().to_matchable().into(),
140        ),
141        (
142            "IntervalExpressionSegment".into(),
143            Nothing::new().to_matchable().into(),
144        ),
145        (
146            "TimeZoneGrammar".into(),
147            Nothing::new().to_matchable().into(),
148        ),
149        (
150            "FetchClauseSegment".into(),
151            Nothing::new().to_matchable().into(),
152        ),
153        (
154            "TrimParametersGrammar".into(),
155            Nothing::new().to_matchable().into(),
156        ),
157        (
158            "LikeGrammar".into(),
159            Sequence::new(vec![Ref::keyword("LIKE").to_matchable()])
160                .to_matchable()
161                .into(),
162        ),
163        (
164            "OverlapsClauseSegment".into(),
165            Nothing::new().to_matchable().into(),
166        ),
167        (
168            "MLTableExpressionSegment".into(),
169            Nothing::new().to_matchable().into(),
170        ),
171        (
172            "MergeIntoLiteralGrammar".into(),
173            Nothing::new().to_matchable().into(),
174        ),
175        (
176            "SamplingExpressionSegment".into(),
177            Nothing::new().to_matchable().into(),
178        ),
179        (
180            "OrderByClauseTerminators".into(),
181            one_of(vec![
182                Ref::keyword("LIMIT").to_matchable(),
183                Ref::keyword("WINDOW").to_matchable(),
184                Ref::new("FrameClauseUnitGrammar").to_matchable(),
185            ])
186            .to_matchable()
187            .into(),
188        ),
189        (
190            "WhereClauseTerminatorGrammar".into(),
191            one_of(vec![
192                Ref::keyword("LIMIT").to_matchable(),
193                Sequence::new(vec![
194                    Ref::keyword("GROUP").to_matchable(),
195                    Ref::keyword("BY").to_matchable(),
196                ])
197                .to_matchable(),
198                Sequence::new(vec![
199                    Ref::keyword("ORDER").to_matchable(),
200                    Ref::keyword("BY").to_matchable(),
201                ])
202                .to_matchable(),
203                Ref::keyword("WINDOW").to_matchable(),
204            ])
205            .to_matchable()
206            .into(),
207        ),
208        (
209            "FromClauseTerminatorGrammar".into(),
210            one_of(vec![
211                Ref::keyword("WHERE").to_matchable(),
212                Ref::keyword("LIMIT").to_matchable(),
213                Sequence::new(vec![
214                    Ref::keyword("GROUP").to_matchable(),
215                    Ref::keyword("BY").to_matchable(),
216                ])
217                .to_matchable(),
218                Sequence::new(vec![
219                    Ref::keyword("ORDER").to_matchable(),
220                    Ref::keyword("BY").to_matchable(),
221                ])
222                .to_matchable(),
223                Ref::keyword("WINDOW").to_matchable(),
224                Ref::new("SetOperatorSegment").to_matchable(),
225                Ref::new("WithNoSchemaBindingClauseSegment").to_matchable(),
226                Ref::new("WithDataClauseSegment").to_matchable(),
227            ])
228            .to_matchable()
229            .into(),
230        ),
231        (
232            "GroupByClauseTerminatorGrammar".into(),
233            one_of(vec![
234                Sequence::new(vec![
235                    Ref::keyword("ORDER").to_matchable(),
236                    Ref::keyword("BY").to_matchable(),
237                ])
238                .to_matchable(),
239                Ref::keyword("LIMIT").to_matchable(),
240                Ref::keyword("HAVING").to_matchable(),
241                Ref::keyword("WINDOW").to_matchable(),
242            ])
243            .to_matchable()
244            .into(),
245        ),
246        (
247            "PostFunctionGrammar".into(),
248            Sequence::new(vec![
249                Ref::new("FilterClauseGrammar").optional().to_matchable(),
250                Ref::new("OverClauseSegment").to_matchable(),
251            ])
252            .to_matchable()
253            .into(),
254        ),
255        (
256            "IgnoreRespectNullsGrammar".into(),
257            Nothing::new().to_matchable().into(),
258        ),
259        (
260            "SelectClauseTerminatorGrammar".into(),
261            one_of(vec![
262                Ref::keyword("FROM").to_matchable(),
263                Ref::keyword("WHERE").to_matchable(),
264                Sequence::new(vec![
265                    Ref::keyword("ORDER").to_matchable(),
266                    Ref::keyword("BY").to_matchable(),
267                ])
268                .to_matchable(),
269                Ref::keyword("LIMIT").to_matchable(),
270                Ref::new("SetOperatorSegment").to_matchable(),
271            ])
272            .to_matchable()
273            .into(),
274        ),
275        (
276            "FunctionContentsGrammar".into(),
277            AnyNumberOf::new(vec![
278                Ref::new("ExpressionSegment").to_matchable(),
279                Sequence::new(vec![
280                    Ref::new("ExpressionSegment").to_matchable(),
281                    Ref::keyword("AS").to_matchable(),
282                    Ref::new("DatatypeSegment").to_matchable(),
283                ])
284                .to_matchable(),
285                Sequence::new(vec![
286                    Ref::new("TrimParametersGrammar").to_matchable(),
287                    Ref::new("ExpressionSegment")
288                        .optional()
289                        .exclude(Ref::keyword("FROM"))
290                        .config(|config| {
291                            config.exclude = Ref::keyword("FROM").to_matchable().into();
292                        })
293                        .to_matchable(),
294                    Ref::keyword("FROM").to_matchable(),
295                    Ref::new("ExpressionSegment").to_matchable(),
296                ])
297                .to_matchable(),
298                Sequence::new(vec![
299                    one_of(vec![
300                        Ref::new("DatetimeUnitSegment").to_matchable(),
301                        Ref::new("ExpressionSegment").to_matchable(),
302                    ])
303                    .to_matchable(),
304                    Ref::keyword("FROM").to_matchable(),
305                    Ref::new("ExpressionSegment").to_matchable(),
306                ])
307                .to_matchable(),
308                Sequence::new(vec![
309                    Ref::keyword("DISTINCT").optional().to_matchable(),
310                    one_of(vec![
311                        Ref::new("StarSegment").to_matchable(),
312                        Delimited::new(vec![
313                            Ref::new("FunctionContentsExpressionGrammar").to_matchable(),
314                        ])
315                        .to_matchable(),
316                    ])
317                    .to_matchable(),
318                ])
319                .to_matchable(),
320                Ref::new("OrderByClauseSegment").to_matchable(),
321                Sequence::new(vec![
322                    one_of(vec![
323                        Ref::new("QuotedLiteralSegment").to_matchable(),
324                        Ref::new("SingleIdentifierGrammar").to_matchable(),
325                        Ref::new("ColumnReferenceSegment").to_matchable(),
326                    ])
327                    .to_matchable(),
328                    Ref::keyword("IN").to_matchable(),
329                    one_of(vec![
330                        Ref::new("QuotedLiteralSegment").to_matchable(),
331                        Ref::new("SingleIdentifierGrammar").to_matchable(),
332                        Ref::new("ColumnReferenceSegment").to_matchable(),
333                    ])
334                    .to_matchable(),
335                ])
336                .to_matchable(),
337                Ref::new("IndexColumnDefinitionSegment").to_matchable(),
338            ])
339            .to_matchable()
340            .into(),
341        ),
342        (
343            "Expression_A_Unary_Operator_Grammar".into(),
344            one_of(vec![
345                Ref::new("SignedSegmentGrammar")
346                    .exclude(Sequence::new(vec![
347                        Ref::new("QualifiedNumericLiteralSegment").to_matchable(),
348                    ]))
349                    .config(|config| {
350                        config.exclude = Sequence::new(vec![
351                            Ref::new("QualifiedNumericLiteralSegment").to_matchable(),
352                        ])
353                        .to_matchable()
354                        .into();
355                    })
356                    .to_matchable(),
357                Ref::new("TildeSegment").to_matchable(),
358                Ref::new("NotOperatorGrammar").to_matchable(),
359            ])
360            .to_matchable()
361            .into(),
362        ),
363        (
364            "IsClauseGrammar".into(),
365            one_of(vec![
366                Ref::keyword("NULL").to_matchable(),
367                Ref::new("BooleanLiteralGrammar").to_matchable(),
368            ])
369            .to_matchable()
370            .into(),
371        ),
372    ]);
373    sqlite_dialect.add([(
374        "SetOperatorSegment".into(),
375        NodeMatcher::new(SyntaxKind::SetOperator, |_| {
376            one_of(vec![
377                Sequence::new(vec![
378                    Ref::keyword("UNION").to_matchable(),
379                    one_of(vec![
380                        Ref::keyword("DISTINCT").to_matchable(),
381                        Ref::keyword("ALL").to_matchable(),
382                    ])
383                    .config(|config| {
384                        config.optional();
385                    })
386                    .to_matchable(),
387                ])
388                .to_matchable(),
389                Sequence::new(vec![
390                    one_of(vec![
391                        Ref::keyword("INTERSECT").to_matchable(),
392                        Ref::keyword("EXCEPT").to_matchable(),
393                    ])
394                    .to_matchable(),
395                    Ref::keyword("ALL").optional().to_matchable(),
396                ])
397                .to_matchable(),
398            ])
399            .config(|config| {
400                config.exclude = Sequence::new(vec![
401                    Ref::keyword("EXCEPT").to_matchable(),
402                    Bracketed::new(vec![Anything::new().to_matchable()]).to_matchable(),
403                ])
404                .to_matchable()
405                .into();
406            })
407            .to_matchable()
408        })
409        .to_matchable()
410        .into(),
411    )]);
412
413    sqlite_dialect.replace_grammar(
414        "DatatypeSegment",
415        one_of(vec![
416            Sequence::new(vec![
417                Ref::keyword("DOUBLE").to_matchable(),
418                Ref::keyword("PRECISION").to_matchable(),
419            ])
420            .to_matchable(),
421            Sequence::new(vec![
422                Ref::keyword("UNSIGNED").to_matchable(),
423                Ref::keyword("BIG").to_matchable(),
424                Ref::keyword("INT").to_matchable(),
425            ])
426            .to_matchable(),
427            Sequence::new(vec![
428                one_of(vec![
429                    Sequence::new(vec![
430                        one_of(vec![
431                            Ref::keyword("VARYING").to_matchable(),
432                            Ref::keyword("NATIVE").to_matchable(),
433                        ])
434                        .to_matchable(),
435                        one_of(vec![Ref::keyword("CHARACTER").to_matchable()]).to_matchable(),
436                    ])
437                    .to_matchable(),
438                    Ref::new("DatatypeIdentifierSegment").to_matchable(),
439                ])
440                .to_matchable(),
441                Ref::new("BracketedArguments").optional().to_matchable(),
442            ])
443            .to_matchable(),
444        ])
445        .to_matchable(),
446    );
447    sqlite_dialect.add([(
448        "TableEndClauseSegment".into(),
449        NodeMatcher::new(SyntaxKind::TableEndClauseSegment, |_| {
450            Delimited::new(vec![
451                Sequence::new(vec![
452                    Ref::keyword("WITHOUT").to_matchable(),
453                    Ref::keyword("ROWID").to_matchable(),
454                ])
455                .to_matchable(),
456                Ref::keyword("STRICT").to_matchable(),
457            ])
458            .to_matchable()
459        })
460        .to_matchable()
461        .into(),
462    )]);
463
464    sqlite_dialect.replace_grammar(
465        "ValuesClauseSegment",
466        Sequence::new(vec![
467            Ref::keyword("VALUES").to_matchable(),
468            Delimited::new(vec![
469                Sequence::new(vec![
470                    Bracketed::new(vec![
471                        Delimited::new(vec![
472                            Ref::keyword("DEFAULT").to_matchable(),
473                            Ref::new("ExpressionSegment").to_matchable(),
474                        ])
475                        .to_matchable(),
476                    ])
477                    .config(|config| {
478                        config.parse_mode(ParseMode::Greedy);
479                    })
480                    .to_matchable(),
481                ])
482                .to_matchable(),
483            ])
484            .to_matchable(),
485        ])
486        .to_matchable(),
487    );
488    sqlite_dialect.add([
489        (
490            "IndexColumnDefinitionSegment".into(),
491            NodeMatcher::new(SyntaxKind::IndexColumnDefinition, |_| {
492                Sequence::new(vec![
493                    one_of(vec![
494                        Ref::new("SingleIdentifierGrammar").to_matchable(),
495                        Ref::new("ExpressionSegment").to_matchable(),
496                    ])
497                    .to_matchable(),
498                    one_of(vec![
499                        Ref::keyword("ASC").to_matchable(),
500                        Ref::keyword("DESC").to_matchable(),
501                    ])
502                    .config(|config| {
503                        config.optional();
504                    })
505                    .to_matchable(),
506                ])
507                .to_matchable()
508            })
509            .to_matchable()
510            .into(),
511        ),
512        (
513            "InsertStatementSegment".into(),
514            NodeMatcher::new(SyntaxKind::InsertStatement, |_| {
515                Sequence::new(vec![
516                    one_of(vec![
517                        Sequence::new(vec![
518                            Ref::keyword("INSERT").to_matchable(),
519                            Sequence::new(vec![
520                                Ref::keyword("OR").to_matchable(),
521                                one_of(vec![
522                                    Ref::keyword("ABORT").to_matchable(),
523                                    Ref::keyword("FAIL").to_matchable(),
524                                    Ref::keyword("IGNORE").to_matchable(),
525                                    Ref::keyword("REPLACE").to_matchable(),
526                                    Ref::keyword("ROLLBACK").to_matchable(),
527                                ])
528                                .to_matchable(),
529                            ])
530                            .config(|config| {
531                                config.optional();
532                            })
533                            .to_matchable(),
534                        ])
535                        .to_matchable(),
536                        Ref::keyword("REPLACE").to_matchable(),
537                    ])
538                    .to_matchable(),
539                    Ref::keyword("INTO").to_matchable(),
540                    Ref::new("TableReferenceSegment").to_matchable(),
541                    Ref::new("BracketedColumnReferenceListGrammar")
542                        .optional()
543                        .to_matchable(),
544                    one_of(vec![
545                        Ref::new("ValuesClauseSegment").to_matchable(),
546                        optionally_bracketed(vec![Ref::new("SelectableGrammar").to_matchable()])
547                            .to_matchable(),
548                        Ref::new("DefaultValuesGrammar").to_matchable(),
549                    ])
550                    .to_matchable(),
551                    Ref::new("ReturningClauseSegment").optional().to_matchable(),
552                ])
553                .to_matchable()
554            })
555            .to_matchable()
556            .into(),
557        ),
558        (
559            "DeleteStatementSegment".into(),
560            NodeMatcher::new(SyntaxKind::DeleteStatement, |_| {
561                Sequence::new(vec![
562                    Ref::keyword("DELETE").to_matchable(),
563                    Ref::new("FromClauseSegment").to_matchable(),
564                    Ref::new("WhereClauseSegment").optional().to_matchable(),
565                    Ref::new("ReturningClauseSegment").optional().to_matchable(),
566                ])
567                .to_matchable()
568            })
569            .to_matchable()
570            .into(),
571        ),
572        (
573            "UpdateStatementSegment".into(),
574            NodeMatcher::new(SyntaxKind::UpdateStatement, |_| {
575                Sequence::new(vec![
576                    Ref::keyword("UPDATE").to_matchable(),
577                    Ref::new("TableReferenceSegment").to_matchable(),
578                    Ref::new("AliasExpressionSegment")
579                        .exclude(Ref::keyword("SET"))
580                        .optional()
581                        .to_matchable(),
582                    Ref::new("SetClauseListSegment").to_matchable(),
583                    Ref::new("FromClauseSegment").optional().to_matchable(),
584                    Ref::new("WhereClauseSegment").optional().to_matchable(),
585                    Ref::new("ReturningClauseSegment").optional().to_matchable(),
586                ])
587                .to_matchable()
588            })
589            .to_matchable()
590            .into(),
591        ),
592    ]);
593
594    let column_constraint = sqlite_dialect
595        .grammar("ColumnConstraintSegment")
596        .match_grammar(&sqlite_dialect)
597        .unwrap()
598        .copy(
599            Some(vec![
600                one_of(vec![
601                    Ref::keyword("DEFERRABLE").to_matchable(),
602                    Sequence::new(vec![
603                        Ref::keyword("NOT").to_matchable(),
604                        Ref::keyword("DEFERRABLE").to_matchable(),
605                    ])
606                    .to_matchable(),
607                ])
608                .config(|config| {
609                    config.optional();
610                })
611                .to_matchable(),
612                one_of(vec![
613                    Sequence::new(vec![
614                        Ref::keyword("INITIALLY").to_matchable(),
615                        Ref::keyword("DEFERRED").to_matchable(),
616                    ])
617                    .to_matchable(),
618                    Sequence::new(vec![
619                        Ref::keyword("INITIALLY").to_matchable(),
620                        Ref::keyword("IMMEDIATE").to_matchable(),
621                    ])
622                    .to_matchable(),
623                ])
624                .config(|config| {
625                    config.optional();
626                })
627                .to_matchable(),
628            ]),
629            None,
630            None,
631            None,
632            Vec::new(),
633            false,
634        );
635    sqlite_dialect.replace_grammar("ColumnConstraintSegment", column_constraint);
636
637    sqlite_dialect.replace_grammar(
638        "TableConstraintSegment",
639        Sequence::new(vec![
640            Sequence::new(vec![
641                Ref::keyword("CONSTRAINT").to_matchable(),
642                Ref::new("ObjectReferenceSegment").to_matchable(),
643            ])
644            .config(|config| {
645                config.optional();
646            })
647            .to_matchable(),
648            one_of(vec![
649                Sequence::new(vec![
650                    Ref::keyword("CHECK").to_matchable(),
651                    Bracketed::new(vec![Ref::new("ExpressionSegment").to_matchable()])
652                        .to_matchable(),
653                ])
654                .to_matchable(),
655                Sequence::new(vec![
656                    Ref::keyword("UNIQUE").to_matchable(),
657                    Ref::new("BracketedColumnReferenceListGrammar").to_matchable(),
658                ])
659                .to_matchable(),
660                Sequence::new(vec![
661                    Ref::new("PrimaryKeyGrammar").to_matchable(),
662                    Ref::new("BracketedColumnReferenceListGrammar").to_matchable(),
663                ])
664                .to_matchable(),
665                Sequence::new(vec![
666                    Ref::new("ForeignKeyGrammar").to_matchable(),
667                    Ref::new("BracketedColumnReferenceListGrammar").to_matchable(),
668                    Ref::new("ReferenceDefinitionGrammar").to_matchable(),
669                ])
670                .to_matchable(),
671            ])
672            .to_matchable(),
673            one_of(vec![
674                Ref::keyword("DEFERRABLE").to_matchable(),
675                Sequence::new(vec![
676                    Ref::keyword("NOT").to_matchable(),
677                    Ref::keyword("DEFERRABLE").to_matchable(),
678                ])
679                .to_matchable(),
680            ])
681            .config(|config| {
682                config.optional();
683            })
684            .to_matchable(),
685            one_of(vec![
686                Sequence::new(vec![
687                    Ref::keyword("INITIALLY").to_matchable(),
688                    Ref::keyword("DEFERRED").to_matchable(),
689                ])
690                .to_matchable(),
691                Sequence::new(vec![
692                    Ref::keyword("INITIALLY").to_matchable(),
693                    Ref::keyword("IMMEDIATE").to_matchable(),
694                ])
695                .to_matchable(),
696            ])
697            .config(|config| {
698                config.optional();
699            })
700            .to_matchable(),
701        ])
702        .to_matchable(),
703    );
704
705    sqlite_dialect.replace_grammar(
706        "TransactionStatementSegment",
707        Sequence::new(vec![
708            one_of(vec![
709                Ref::keyword("BEGIN").to_matchable(),
710                Ref::keyword("COMMIT").to_matchable(),
711                Ref::keyword("ROLLBACK").to_matchable(),
712                Ref::keyword("END").to_matchable(),
713            ])
714            .to_matchable(),
715            one_of(vec![Ref::keyword("TRANSACTION").to_matchable()])
716                .config(|config| {
717                    config.optional();
718                })
719                .to_matchable(),
720            Sequence::new(vec![
721                Ref::keyword("TO").to_matchable(),
722                Ref::keyword("SAVEPOINT").to_matchable(),
723                Ref::new("ObjectReferenceSegment").to_matchable(),
724            ])
725            .config(|config| {
726                config.optional();
727            })
728            .to_matchable(),
729        ])
730        .to_matchable(),
731    );
732
733    sqlite_dialect.add([(
734        "PragmaReferenceSegment".into(),
735        NodeMatcher::new(SyntaxKind::PragmaReference, |sqlite_dialect| {
736            sqlite_dialect
737                .grammar("ObjectReferenceSegment")
738                .match_grammar(sqlite_dialect)
739                .unwrap()
740        })
741        .to_matchable()
742        .into(),
743    )]);
744
745    sqlite_dialect.add([(
746        "PragmaStatementSegment".into(),
747        NodeMatcher::new(SyntaxKind::PragmaStatement, |_| {
748            let pragma_value = one_of(vec![
749                Ref::new("LiteralGrammar").to_matchable(),
750                Ref::new("BooleanLiteralGrammar").to_matchable(),
751                Ref::keyword("YES").to_matchable(),
752                Ref::keyword("NO").to_matchable(),
753                Ref::keyword("ON").to_matchable(),
754                Ref::keyword("OFF").to_matchable(),
755                Ref::keyword("NONE").to_matchable(),
756                Ref::keyword("FULL").to_matchable(),
757                Ref::keyword("INCREMENTAL").to_matchable(),
758                Ref::keyword("DELETE").to_matchable(),
759                Ref::keyword("TRUNCATE").to_matchable(),
760                Ref::keyword("PERSIST").to_matchable(),
761                Ref::keyword("MEMORY").to_matchable(),
762                Ref::keyword("WAL").to_matchable(),
763                Ref::keyword("NORMAL").to_matchable(),
764                Ref::keyword("EXCLUSIVE").to_matchable(),
765                Ref::keyword("FAST").to_matchable(),
766                Ref::keyword("EXTRA").to_matchable(),
767                Ref::keyword("DEFAULT").to_matchable(),
768                Ref::keyword("FILE").to_matchable(),
769                Ref::keyword("PASSIVE").to_matchable(),
770                Ref::keyword("RESTART").to_matchable(),
771                Ref::keyword("RESET").to_matchable(),
772            ]);
773
774            Sequence::new(vec![
775                Ref::keyword("PRAGMA").to_matchable(),
776                Ref::new("PragmaReferenceSegment").to_matchable(),
777                Bracketed::new(vec![pragma_value.clone().to_matchable()])
778                    .config(|config| {
779                        config.optional();
780                    })
781                    .to_matchable(),
782                Sequence::new(vec![
783                    Ref::new("EqualsSegment").to_matchable(),
784                    optionally_bracketed(vec![pragma_value.to_matchable()]).to_matchable(),
785                ])
786                .config(|config| {
787                    config.optional();
788                })
789                .to_matchable(),
790            ])
791            .to_matchable()
792        })
793        .to_matchable()
794        .into(),
795    )]);
796
797    sqlite_dialect.replace_grammar(
798        "CreateTriggerStatementSegment",
799        Sequence::new(vec![
800            Ref::keyword("CREATE").to_matchable(),
801            Ref::new("TemporaryGrammar").optional().to_matchable(),
802            Ref::keyword("TRIGGER").to_matchable(),
803            Ref::new("IfNotExistsGrammar").optional().to_matchable(),
804            Ref::new("TriggerReferenceSegment").to_matchable(),
805            one_of(vec![
806                Ref::keyword("BEFORE").to_matchable(),
807                Ref::keyword("AFTER").to_matchable(),
808                Sequence::new(vec![
809                    Ref::keyword("INSTEAD").to_matchable(),
810                    Ref::keyword("OF").to_matchable(),
811                ])
812                .to_matchable(),
813            ])
814            .config(|config| {
815                config.optional();
816            })
817            .to_matchable(),
818            one_of(vec![
819                Ref::keyword("DELETE").to_matchable(),
820                Ref::keyword("INSERT").to_matchable(),
821                Sequence::new(vec![
822                    Ref::keyword("UPDATE").to_matchable(),
823                    Sequence::new(vec![
824                        Ref::keyword("OF").to_matchable(),
825                        Delimited::new(vec![Ref::new("ColumnReferenceSegment").to_matchable()])
826                            .to_matchable(),
827                    ])
828                    .config(|config| {
829                        config.optional();
830                    })
831                    .to_matchable(),
832                ])
833                .to_matchable(),
834            ])
835            .to_matchable(),
836            Ref::keyword("ON").to_matchable(),
837            Ref::new("TableReferenceSegment").to_matchable(),
838            Sequence::new(vec![
839                Ref::keyword("FOR").to_matchable(),
840                Ref::keyword("EACH").to_matchable(),
841                Ref::keyword("ROW").to_matchable(),
842            ])
843            .config(|config| {
844                config.optional();
845            })
846            .to_matchable(),
847            Sequence::new(vec![
848                Ref::keyword("WHEN").to_matchable(),
849                Bracketed::new(vec![Ref::new("ExpressionSegment").to_matchable()]).to_matchable(),
850            ])
851            .config(|config| {
852                config.optional();
853            })
854            .to_matchable(),
855            Ref::keyword("BEGIN").to_matchable(),
856            Delimited::new(vec![
857                Ref::new("UpdateStatementSegment").to_matchable(),
858                Ref::new("InsertStatementSegment").to_matchable(),
859                Ref::new("DeleteStatementSegment").to_matchable(),
860                Ref::new("SelectableGrammar").to_matchable(),
861            ])
862            .config(|config| {
863                config.delimiter(
864                    AnyNumberOf::new(vec![Ref::new("DelimiterGrammar").to_matchable()]).config(
865                        |config| {
866                            config.min_times = 1;
867                        },
868                    ),
869                );
870                config.allow_trailing = true;
871            })
872            .to_matchable(),
873            Ref::keyword("END").to_matchable(),
874        ])
875        .to_matchable(),
876    );
877    sqlite_dialect.add([(
878        "UnorderedSelectStatementSegment".into(),
879        NodeMatcher::new(SyntaxKind::SelectStatement, |_| {
880            Sequence::new(vec![
881                Ref::new("SelectClauseSegment").to_matchable(),
882                MetaSegment::dedent().to_matchable(),
883                Ref::new("FromClauseSegment").optional().to_matchable(),
884                Ref::new("WhereClauseSegment").optional().to_matchable(),
885                Ref::new("GroupByClauseSegment").optional().to_matchable(),
886                Ref::new("HavingClauseSegment").optional().to_matchable(),
887                Ref::new("OverlapsClauseSegment").optional().to_matchable(),
888                Ref::new("NamedWindowSegment").optional().to_matchable(),
889            ])
890            .to_matchable()
891        })
892        .to_matchable()
893        .into(),
894    )]);
895
896    sqlite_dialect.add([(
897        "SelectStatementSegment".into(),
898        NodeMatcher::new(SyntaxKind::SelectStatement, |sqlite_dialect| {
899            sqlite_dialect
900                .grammar("UnorderedSelectStatementSegment")
901                .match_grammar(sqlite_dialect)
902                .unwrap()
903                .copy(
904                    Some(vec![
905                        Ref::new("OrderByClauseSegment").optional().to_matchable(),
906                        Ref::new("FetchClauseSegment").optional().to_matchable(),
907                        Ref::new("LimitClauseSegment").optional().to_matchable(),
908                        Ref::new("NamedWindowSegment").optional().to_matchable(),
909                    ]),
910                    None,
911                    None,
912                    None,
913                    Vec::new(),
914                    false,
915                )
916        })
917        .to_matchable()
918        .into(),
919    )]);
920
921    sqlite_dialect.replace_grammar(
922        "CreateIndexStatementSegment",
923        Sequence::new(vec![
924            Ref::keyword("CREATE").to_matchable(),
925            Ref::keyword("UNIQUE").optional().to_matchable(),
926            Ref::keyword("INDEX").to_matchable(),
927            Ref::new("IfNotExistsGrammar").optional().to_matchable(),
928            Ref::new("IndexReferenceSegment").to_matchable(),
929            Ref::keyword("ON").to_matchable(),
930            Ref::new("TableReferenceSegment").to_matchable(),
931            Sequence::new(vec![
932                Bracketed::new(vec![
933                    Delimited::new(vec![
934                        Ref::new("IndexColumnDefinitionSegment").to_matchable(),
935                    ])
936                    .to_matchable(),
937                ])
938                .to_matchable(),
939            ])
940            .to_matchable(),
941            Ref::new("WhereClauseSegment").optional().to_matchable(),
942        ])
943        .to_matchable(),
944    );
945
946    sqlite_dialect.replace_grammar(
947        "StatementSegment",
948        one_of(vec![
949            Ref::new("AlterTableStatementSegment").to_matchable(),
950            Ref::new("CreateIndexStatementSegment").to_matchable(),
951            Ref::new("CreateTableStatementSegment").to_matchable(),
952            Ref::new("CreateTriggerStatementSegment").to_matchable(),
953            Ref::new("CreateViewStatementSegment").to_matchable(),
954            Ref::new("DeleteStatementSegment").to_matchable(),
955            Ref::new("DropIndexStatementSegment").to_matchable(),
956            Ref::new("DropTableStatementSegment").to_matchable(),
957            Ref::new("DropTriggerStatementSegment").to_matchable(),
958            Ref::new("DropViewStatementSegment").to_matchable(),
959            Ref::new("ExplainStatementSegment").to_matchable(),
960            Ref::new("InsertStatementSegment").to_matchable(),
961            Ref::new("PragmaStatementSegment").to_matchable(),
962            Ref::new("SelectableGrammar").to_matchable(),
963            Ref::new("TransactionStatementSegment").to_matchable(),
964            Ref::new("UpdateStatementSegment").to_matchable(),
965            Bracketed::new(vec![Ref::new("StatementSegment").to_matchable()]).to_matchable(),
966        ])
967        .to_matchable(),
968    );
969
970    sqlite_dialect.replace_grammar(
971        "AlterTableStatementSegment",
972        Sequence::new(vec![
973            Ref::keyword("ALTER").to_matchable(),
974            Ref::keyword("TABLE").to_matchable(),
975            Ref::new("TableReferenceSegment").to_matchable(),
976            one_of(vec![
977                Sequence::new(vec![
978                    Ref::keyword("RENAME").to_matchable(),
979                    Ref::keyword("TO").to_matchable(),
980                    one_of(vec![
981                        one_of(vec![
982                            Ref::new("ParameterNameSegment").to_matchable(),
983                            Ref::new("QuotedIdentifierSegment").to_matchable(),
984                        ])
985                        .to_matchable(),
986                    ])
987                    .to_matchable(),
988                ])
989                .to_matchable(),
990                Sequence::new(vec![
991                    Ref::keyword("RENAME").to_matchable(),
992                    Ref::keyword("COLUMN").optional().to_matchable(),
993                    Ref::new("ColumnReferenceSegment").to_matchable(),
994                    Ref::keyword("TO").to_matchable(),
995                    Ref::new("ColumnReferenceSegment").to_matchable(),
996                ])
997                .to_matchable(),
998                Sequence::new(vec![
999                    Ref::keyword("ADD").to_matchable(),
1000                    Ref::keyword("COLUMN").optional().to_matchable(),
1001                    Ref::new("ColumnDefinitionSegment").to_matchable(),
1002                ])
1003                .to_matchable(),
1004                Sequence::new(vec![
1005                    Ref::keyword("DROP").to_matchable(),
1006                    Ref::keyword("COLUMN").optional().to_matchable(),
1007                    Ref::new("ColumnReferenceSegment").to_matchable(),
1008                ])
1009                .to_matchable(),
1010            ])
1011            .to_matchable(),
1012        ])
1013        .to_matchable(),
1014    );
1015
1016    sqlite_dialect.add([(
1017        "ReturningClauseSegment".into(),
1018        Sequence::new(vec![
1019            Ref::keyword("RETURNING").to_matchable(),
1020            MetaSegment::indent().to_matchable(),
1021            one_of(vec![
1022                Ref::new("StarSegment").to_matchable(),
1023                Delimited::new(vec![
1024                    Sequence::new(vec![
1025                        Ref::new("ExpressionSegment").to_matchable(),
1026                        Ref::new("AsAliasExpressionSegment")
1027                            .optional()
1028                            .to_matchable(),
1029                    ])
1030                    .to_matchable(),
1031                ])
1032                .to_matchable(),
1033            ])
1034            .to_matchable(),
1035            MetaSegment::dedent().to_matchable(),
1036        ])
1037        .to_matchable()
1038        .into(),
1039    )]);
1040
1041    sqlite_dialect.add([(
1042        "AsAliasExpressionSegment".into(),
1043        NodeMatcher::new(SyntaxKind::AliasExpression, |_| {
1044            Sequence::new(vec![
1045                MetaSegment::indent().to_matchable(),
1046                Ref::keyword("AS").to_matchable(),
1047                Ref::new("SingleIdentifierGrammar").to_matchable(),
1048                MetaSegment::dedent().to_matchable(),
1049            ])
1050            .to_matchable()
1051        })
1052        .to_matchable()
1053        .into(),
1054    )]);
1055
1056    sqlite_dialect
1057}