Skip to main content

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