Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions crates/lib-core/src/dialects/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ pub enum SyntaxKind {
WithCompoundStatement,
CommonTableExpression,
CTEColumnList,
ReferencedColumnList,
TriggerReference,
TableConstraint,
JoinOnCondition,
Expand Down Expand Up @@ -133,6 +134,7 @@ pub enum SyntaxKind {
IntervalExpression,
ArrayType,
SizedArrayType,
ArrayTypeSuffix,
SelectStatement,
OverlapsClause,
SelectClause,
Expand Down
289 changes: 179 additions & 110 deletions crates/lib-dialects/src/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,22 +277,125 @@ fn build_datatype_segment_grammar(pgvector: bool) -> Matchable {
Ref::new("WellKnownTextGeometrySegment").to_matchable(),
Ref::new("DateTimeTypeIdentifier").to_matchable(),
Sequence::new(vec![one_of(known_types).to_matchable()]).to_matchable(),
Ref::new("DatatypeIdentifierSegment").to_matchable(),
Sequence::new(vec![
Ref::new("DatatypeIdentifierSegment").to_matchable(),
Ref::new("BracketedArguments").optional().to_matchable(),
])
.to_matchable(),
])
.to_matchable(),
one_of(vec![
AnyNumberOf::new(vec![
Ref::new("ArrayTypeSuffixSegment").to_matchable(),
Ref::new("ArrayTypeSegment").to_matchable(),
Ref::new("SizedArrayTypeSegment").to_matchable(),
])
.config(|this| this.optional())
.to_matchable(),
])
.to_matchable()
}

fn table_constraint_body_grammar() -> Matchable {
Sequence::new(vec![
one_of(vec![
Sequence::new(vec![
Ref::keyword("CHECK").to_matchable(),
Bracketed::new(vec![Ref::new("ExpressionSegment").to_matchable()]).to_matchable(),
Sequence::new(vec![
Ref::keyword("NO").to_matchable(),
Ref::keyword("INHERIT").to_matchable(),
])
.config(|this| this.optional())
.to_matchable(),
])
.to_matchable(),
Sequence::new(vec![
Ref::keyword("UNIQUE").to_matchable(),
Sequence::new(vec![
Ref::keyword("NULLS").to_matchable(),
Ref::keyword("NOT").optional().to_matchable(),
Ref::keyword("DISTINCT").to_matchable(),
])
.config(|this| this.optional())
.to_matchable(),
Ref::new("BracketedColumnReferenceListGrammar").to_matchable(),
Ref::new("IndexParametersSegment").optional().to_matchable(),
])
.to_matchable(),
Sequence::new(vec![
Ref::new("PrimaryKeyGrammar").to_matchable(),
Ref::new("BracketedColumnReferenceListGrammar").to_matchable(),
Ref::new("IndexParametersSegment").optional().to_matchable(),
])
.to_matchable(),
Sequence::new(vec![
Ref::keyword("EXCLUDE").to_matchable(),
Sequence::new(vec![
Ref::keyword("USING").to_matchable(),
Ref::new("IndexAccessMethodSegment").to_matchable(),
])
.config(|this| this.optional())
.to_matchable(),
Bracketed::new(vec![
Ref::new("ExpressionSegment").optional().to_matchable(),
Delimited::new(vec![
Ref::new("ExclusionConstraintElementSegment").to_matchable(),
])
.to_matchable(),
])
.config(|this| this.bracket_type("square"))
.to_matchable(),
Ref::new("IndexParametersSegment").optional().to_matchable(),
Sequence::new(vec![
Ref::keyword("WHERE").to_matchable(),
Bracketed::new(vec![Ref::new("ExpressionSegment").to_matchable()])
.to_matchable(),
])
.config(|this| this.optional())
.to_matchable(),
])
.to_matchable(),
Ref::new("ArrayTypeSegment").to_matchable(),
Ref::new("SizedArrayTypeSegment").to_matchable(),
Sequence::new(vec![
Ref::keyword("FOREIGN").to_matchable(),
Ref::keyword("KEY").to_matchable(),
Ref::new("BracketedColumnReferenceListGrammar").to_matchable(),
Ref::new("ReferenceDefinitionGrammar").to_matchable(),
])
.to_matchable(),
])
.to_matchable(),
AnyNumberOf::new(vec![
one_of(vec![
Ref::keyword("DEFERRABLE").to_matchable(),
Sequence::new(vec![
Ref::keyword("NOT").to_matchable(),
Ref::keyword("DEFERRABLE").to_matchable(),
])
.to_matchable(),
])
.to_matchable(),
one_of(vec![
Sequence::new(vec![
Ref::keyword("INITIALLY").to_matchable(),
Ref::keyword("DEFERRED").to_matchable(),
])
.to_matchable(),
Sequence::new(vec![
Ref::keyword("INITIALLY").to_matchable(),
Ref::keyword("IMMEDIATE").to_matchable(),
])
.to_matchable(),
])
.to_matchable(),
Sequence::new(vec![
Ref::keyword("NOT").to_matchable(),
Ref::keyword("VALID").to_matchable(),
])
.to_matchable(),
Sequence::new(vec![
Ref::keyword("NO").to_matchable(),
Ref::keyword("INHERIT").to_matchable(),
])
.to_matchable(),
])
.config(|this| this.optional())
.to_matchable(),
])
.to_matchable()
Expand Down Expand Up @@ -1162,10 +1265,74 @@ pub fn raw_dialect() -> Dialect {
.into(),
)]);

postgres.add([(
"ArrayTypeSuffixSegment".into(),
NodeMatcher::new(SyntaxKind::ArrayTypeSuffix, |_| {
AnyNumberOf::new(vec![
Bracketed::new(vec![
Ref::new("ExpressionSegment").optional().to_matchable(),
])
.config(|this| this.bracket_type("square"))
.to_matchable(),
])
.config(|this| this.min_times(1))
.to_matchable()
})
.to_matchable()
.into(),
)]);

postgres.replace_grammar("DatatypeSegment", build_datatype_segment_grammar(false));

postgres.replace_grammar("ArrayTypeSegment", Ref::keyword("ARRAY").to_matchable());

postgres.add([(
"ReferencedColumnListGrammar".into(),
NodeMatcher::new(SyntaxKind::ReferencedColumnList, |_| {
Ref::new("BracketedColumnReferenceListGrammar").to_matchable()
})
.to_matchable()
.into(),
)]);

postgres.replace_grammar(
"ReferenceDefinitionGrammar",
Sequence::new(vec![
Ref::keyword("REFERENCES").to_matchable(),
Ref::new("TableReferenceSegment").to_matchable(),
Ref::new("ReferencedColumnListGrammar")
.optional()
.to_matchable(),
Sequence::new(vec![
Ref::keyword("MATCH").to_matchable(),
one_of(vec![
Ref::keyword("FULL").to_matchable(),
Ref::keyword("PARTIAL").to_matchable(),
Ref::keyword("SIMPLE").to_matchable(),
])
.to_matchable(),
])
.config(|this| this.optional())
.to_matchable(),
AnyNumberOf::new(vec![
Sequence::new(vec![
Ref::keyword("ON").to_matchable(),
Ref::keyword("DELETE").to_matchable(),
Ref::new("ReferentialActionGrammar").to_matchable(),
])
.to_matchable(),
Sequence::new(vec![
Ref::keyword("ON").to_matchable(),
Ref::keyword("UPDATE").to_matchable(),
Ref::new("ReferentialActionGrammar").to_matchable(),
])
.to_matchable(),
])
.to_matchable(),
])
.to_matchable(),
);

postgres.add([(
"IndexAccessMethodSegment".into(),
NodeMatcher::new(SyntaxKind::IndexAccessMethod, |_| {
Expand Down Expand Up @@ -4881,114 +5048,16 @@ pub fn raw_dialect() -> Dialect {
postgres.add([(
"TableConstraintSegment".into(),
NodeMatcher::new(SyntaxKind::TableConstraint, |_| {
Sequence::new(vec![
one_of(vec![
Sequence::new(vec![
Ref::keyword("CONSTRAINT").to_matchable(),
Ref::new("ObjectReferenceSegment").to_matchable(),
])
.config(|this| this.optional())
.to_matchable(),
one_of(vec![
Sequence::new(vec![
Ref::keyword("CHECK").to_matchable(),
Bracketed::new(vec![Ref::new("ExpressionSegment").to_matchable()])
.to_matchable(),
Sequence::new(vec![
Ref::keyword("NO").to_matchable(),
Ref::keyword("INHERIT").to_matchable(),
])
.config(|this| this.optional())
.to_matchable(),
])
.to_matchable(),
Sequence::new(vec![
Ref::keyword("UNIQUE").to_matchable(),
Sequence::new(vec![
Ref::keyword("NULLS").to_matchable(),
Ref::keyword("NOT").optional().to_matchable(),
Ref::keyword("DISTINCT").to_matchable(),
])
.config(|this| this.optional())
.to_matchable(),
Ref::new("BracketedColumnReferenceListGrammar").to_matchable(),
Ref::new("IndexParametersSegment").optional().to_matchable(),
])
.to_matchable(),
Sequence::new(vec![
Ref::new("PrimaryKeyGrammar").to_matchable(),
Ref::new("BracketedColumnReferenceListGrammar").to_matchable(),
Ref::new("IndexParametersSegment").optional().to_matchable(),
])
.to_matchable(),
Sequence::new(vec![
Ref::keyword("EXCLUDE").to_matchable(),
Sequence::new(vec![
Ref::keyword("USING").to_matchable(),
Ref::new("IndexAccessMethodSegment").to_matchable(),
])
.config(|this| this.optional())
.to_matchable(),
Bracketed::new(vec![
Delimited::new(vec![
Ref::new("ExclusionConstraintElementSegment").to_matchable(),
])
.to_matchable(),
])
.to_matchable(),
Ref::new("IndexParametersSegment").optional().to_matchable(),
Sequence::new(vec![
Ref::keyword("WHERE").to_matchable(),
Bracketed::new(vec![Ref::new("ExpressionSegment").to_matchable()])
.to_matchable(),
])
.config(|this| this.optional())
.to_matchable(),
])
.to_matchable(),
Sequence::new(vec![
Ref::keyword("FOREIGN").to_matchable(),
Ref::keyword("KEY").to_matchable(),
Ref::new("BracketedColumnReferenceListGrammar").to_matchable(),
Ref::new("ReferenceDefinitionGrammar").to_matchable(),
])
.to_matchable(),
])
.to_matchable(),
AnyNumberOf::new(vec![
one_of(vec![
Ref::keyword("DEFERRABLE").to_matchable(),
Sequence::new(vec![
Ref::keyword("NOT").to_matchable(),
Ref::keyword("DEFERRABLE").to_matchable(),
])
.to_matchable(),
])
.to_matchable(),
one_of(vec![
Sequence::new(vec![
Ref::keyword("INITIALLY").to_matchable(),
Ref::keyword("DEFERRED").to_matchable(),
])
.to_matchable(),
Sequence::new(vec![
Ref::keyword("INITIALLY").to_matchable(),
Ref::keyword("IMMEDIATE").to_matchable(),
])
.to_matchable(),
])
.to_matchable(),
Sequence::new(vec![
Ref::keyword("NOT").to_matchable(),
Ref::keyword("VALID").to_matchable(),
])
.to_matchable(),
Sequence::new(vec![
Ref::keyword("NO").to_matchable(),
Ref::keyword("INHERIT").to_matchable(),
])
.to_matchable(),
MetaSegment::indent().to_matchable(),
table_constraint_body_grammar(),
MetaSegment::dedent().to_matchable(),
])
.to_matchable(),
table_constraint_body_grammar(),
])
.to_matchable()
})
Expand Down
Loading