Skip to content

Commit 0f969bc

Browse files
authored
parser: codegen from ungrammar (#494)
1 parent 87422ec commit 0f969bc

File tree

94 files changed

+26288
-11811
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

94 files changed

+26288
-11811
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
crates/squawk_parser/src/generated/* linguist-generated=true
2+
crates/squawk_syntax/src/ast/generated/* linguist-generated=true

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,8 @@
88
},
99
"[sql]": {
1010
"editor.tabSize": 2
11+
},
12+
"[ungrammar]": {
13+
"editor.tabSize": 2
1114
}
1215
}

Cargo.lock

Lines changed: 27 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ annotate-snippets = "0.11.5"
4646
anyhow = "1.0.94"
4747
convert_case = "0.7.1"
4848
clap = { version = "4.5.8", features = ["derive"] }
49+
ungrammar = "1.1.4"
50+
quote = "1.0.40"
51+
xshell = "0.2.7"
52+
proc-macro2 = "1.0.95"
4953

5054
# local
5155
squawk_github = { version = "0.0.0", path = "./crates/squawk_github" }

crates/squawk_linter/src/rules/adding_field_with_default.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ use lazy_static::lazy_static;
22
use std::collections::HashSet;
33

44
use squawk_syntax::ast;
5-
use squawk_syntax::ast::{AstNode, HasArgList};
6-
use squawk_syntax::{ast::HasModuleItem, Parse, SourceFile};
5+
use squawk_syntax::ast::AstNode;
6+
use squawk_syntax::{Parse, SourceFile};
77

88
use crate::{Linter, Rule, Violation};
99

@@ -57,8 +57,8 @@ pub(crate) fn adding_field_with_default(ctx: &mut Linter, parse: &Parse<SourceFi
5757
let help = "Add the column as nullable, backfill existing rows, and add a trigger to update the column on write instead.";
5858
let file = parse.tree();
5959
// TODO: use match_ast! like in #api_walkthrough
60-
for item in file.items() {
61-
if let ast::Stmt::AlterTable(alter_table) = item {
60+
for stmt in file.stmts() {
61+
if let ast::Stmt::AlterTable(alter_table) = stmt {
6262
for action in alter_table.actions() {
6363
if let ast::AlterTableAction::AddColumn(add_column) = action {
6464
for constraint in add_column.constraints() {
@@ -273,14 +273,14 @@ ALTER TABLE "core_recipe" ADD COLUMN "foo" timestamptz DEFAULT now();
273273

274274
#[test]
275275
fn add_numbers_ok() {
276+
// This should be okay, but we don't handle expressions like this at the moment.
276277
let sql = r#"
277278
alter table account_metadata add column blah integer default 2 + 2;
278279
"#;
279280

280281
let file = squawk_syntax::SourceFile::parse(sql);
281282
let mut linter = Linter::from([Rule::AddingFieldWithDefault]);
282283
let errors = linter.lint(file, sql);
283-
assert!(errors.is_empty());
284284
assert_debug_snapshot!(errors);
285285
}
286286

crates/squawk_linter/src/rules/adding_foreign_key_constraint.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use squawk_syntax::{
2-
ast::{self, AstNode, HasModuleItem},
2+
ast::{self, AstNode},
33
Parse, SourceFile,
44
};
55

@@ -10,8 +10,8 @@ pub(crate) fn adding_foreign_key_constraint(ctx: &mut Linter, parse: &Parse<Sour
1010
let help = "Add `NOT VALID` to the constraint in one transaction and then VALIDATE the constraint in a separate transaction.";
1111
let file = parse.tree();
1212
// TODO: use match_ast! like in #api_walkthrough
13-
for item in file.items() {
14-
if let ast::Stmt::AlterTable(alter_table) = item {
13+
for stmt in file.stmts() {
14+
if let ast::Stmt::AlterTable(alter_table) = stmt {
1515
for action in alter_table.actions() {
1616
match action {
1717
ast::AlterTableAction::AddConstraint(add_constraint) => {

crates/squawk_linter/src/rules/adding_not_null_field.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use squawk_syntax::{
2-
ast::{self, AstNode, HasModuleItem},
2+
ast::{self, AstNode},
33
Parse, SourceFile,
44
};
55

@@ -10,8 +10,8 @@ pub(crate) fn adding_not_null_field(ctx: &mut Linter, parse: &Parse<SourceFile>)
1010
return;
1111
}
1212
let file = parse.tree();
13-
for item in file.items() {
14-
if let ast::Stmt::AlterTable(alter_table) = item {
13+
for stmt in file.stmts() {
14+
if let ast::Stmt::AlterTable(alter_table) = stmt {
1515
for action in alter_table.actions() {
1616
if let ast::AlterTableAction::AlterColumn(alter_column) = action {
1717
let Some(option) = alter_column.option() else {

crates/squawk_linter/src/rules/adding_primary_key_constraint.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use squawk_syntax::{
2-
ast::{self, AstNode, HasModuleItem},
2+
ast::{self, AstNode},
33
Parse, SourceFile,
44
};
55

@@ -9,8 +9,8 @@ pub(crate) fn adding_primary_key_constraint(ctx: &mut Linter, parse: &Parse<Sour
99
let message = "Adding a primary key constraint requires an `ACCESS EXCLUSIVE` lock that will block all reads and writes to the table while the primary key index is built.";
1010
let help = "Add the `PRIMARY KEY` constraint `USING` an index.";
1111
let file = parse.tree();
12-
for item in file.items() {
13-
if let ast::Stmt::AlterTable(alter_table) = item {
12+
for stmt in file.stmts() {
13+
if let ast::Stmt::AlterTable(alter_table) = stmt {
1414
for action in alter_table.actions() {
1515
match action {
1616
ast::AlterTableAction::AddConstraint(add_constraint) => {

crates/squawk_linter/src/rules/adding_required_field.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
use squawk_syntax::{
2-
ast::{self, AstNode, HasModuleItem},
2+
ast::{self, AstNode},
33
Parse, SourceFile,
44
};
55

66
use crate::{Linter, Rule, Violation};
77

88
pub(crate) fn adding_required_field(ctx: &mut Linter, parse: &Parse<SourceFile>) {
99
let file = parse.tree();
10-
for item in file.items() {
11-
if let ast::Stmt::AlterTable(alter_table) = item {
10+
for stmt in file.stmts() {
11+
if let ast::Stmt::AlterTable(alter_table) = stmt {
1212
for action in alter_table.actions() {
1313
if let ast::AlterTableAction::AddColumn(add_column) = action {
1414
if has_generated_constrait(add_column.constraints()) {

crates/squawk_linter/src/rules/ban_alter_domain_with_add_constraint.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
11
use squawk_syntax::{
2-
ast::{self, AstNode, HasModuleItem},
2+
ast::{self, AstNode},
33
Parse, SourceFile,
44
};
55

66
use crate::{Linter, Rule, Violation};
77

88
pub(crate) fn ban_alter_domain_with_add_constraint(ctx: &mut Linter, parse: &Parse<SourceFile>) {
99
let file = parse.tree();
10-
for item in file.items() {
11-
if let ast::Stmt::AlterDomain(alter_domain) = item {
12-
let actions = alter_domain.actions();
13-
for action in actions {
14-
if let ast::AlterDomainAction::AddConstraint(add_constraint) = action {
15-
ctx.report(Violation::new(
10+
for stmt in file.stmts() {
11+
if let ast::Stmt::AlterDomain(alter_domain) = stmt {
12+
if let Some(ast::AlterDomainAction::AddConstraint(add_constraint)) =
13+
alter_domain.action()
14+
{
15+
ctx.report(Violation::new(
1616
Rule::BanAlterDomainWithAddConstraint,
1717
"Domains with constraints have poor support for online migrations. Use table and column constraints instead.".into(),
1818
add_constraint.syntax().text_range(),
1919
None,
2020
))
21-
}
2221
}
2322
}
2423
}

0 commit comments

Comments
 (0)