Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

式サポート #120

Draft
wants to merge 6 commits into
base: replace-parser-p
Choose a base branch
from
Draft
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
61 changes: 59 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion crates/uroborosql-fmt/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,8 @@ tree-sitter = "~0.20.3"

# git config --global core.longpaths true を管理者権限で実行しないといけない
tree-sitter-sql = { git = "https://github.com/future-architect/tree-sitter-sql" }
postgresql-cst-parser = {git = "https://github.com/future-architect/postgresql-cst-parser"}
postgresql-cst-parser = {git = "https://github.com/future-architect/postgresql-cst-parser" }

[dev-dependencies]
console = "0.15.10"
similar = { version = "2.7.0", features = ["unicode", "inline"] }
12 changes: 12 additions & 0 deletions crates/uroborosql-fmt/src/cst/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,3 +237,15 @@ impl Expr {
}
}
}

impl From<PrimaryExpr> for Expr {
fn from(primary: PrimaryExpr) -> Self {
Expr::Primary(Box::new(primary))
}
}

impl From<AsteriskExpr> for Expr {
fn from(asterisk: AsteriskExpr) -> Self {
Expr::Asterisk(Box::new(asterisk))
}
}
21 changes: 21 additions & 0 deletions crates/uroborosql-fmt/src/cst/expr/primary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,27 @@ impl PrimaryExpr {
PrimaryExpr::new(converted_element, Location::new(node.range()))
}

pub(crate) fn with_pg_node(
node: postgresql_cst_parser::tree_sitter::Node,
expr_kind: PrimaryExprKind,
) -> Result<PrimaryExpr, UroboroSQLFmtError> {
let element = node.text();

// PrimaryExprKindによって適用するルールを変更する
let converted_element = if matches!(expr_kind, PrimaryExprKind::Keyword) {
// キーワードの大文字小文字設定を適用した文字列
convert_keyword_case(element)
} else {
// 文字列リテラルであればそのまま、DBオブジェクトであれば大文字小文字設定を適用した文字列
convert_identifier_case(element)
};

Ok(PrimaryExpr::new(
converted_element,
Location::from(node.range()),
))
}

pub(crate) fn loc(&self) -> Location {
self.loc.clone()
}
Expand Down
1 change: 1 addition & 0 deletions crates/uroborosql-fmt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ pub(crate) fn format_sql_with_config(

pg_validate_format_result(src)?;

load_settings(config);
pg_format_tree(&tree, src)
} else {
// tree-sitter-sql を使用してパースする
Expand Down
64 changes: 60 additions & 4 deletions crates/uroborosql-fmt/src/new_visitor.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
mod clause;
mod expr;
mod pg_expr;
mod statement;

use postgresql_cst_parser::syntax_kind::SyntaxKind;
use tree_sitter::{Node, TreeCursor};
use tree_sitter::TreeCursor;

pub(crate) const COMMENT: &str = "comment";
pub(crate) const COMMA: &str = ",";
Expand Down Expand Up @@ -284,6 +285,33 @@ impl Visitor {
}
}

/// カーソルが指すノードがSQL_IDであれば、clauseに追加する
/// もし (_SQL_ID_が存在していない) && (_SQL_ID_がまだ出現していない) && (_SQL_ID_の補完がオン)
/// の場合は補完する
fn pg_consume_or_complement_sql_id(
&mut self,
cursor: &mut postgresql_cst_parser::tree_sitter::TreeCursor,
clause: &mut Clause,
) {
if cursor.node().kind() == SyntaxKind::C_COMMENT {
let text = cursor.node().text();

if SqlID::is_sql_id(text) {
clause.set_sql_id(SqlID::new(text.to_string()));
cursor.goto_next_sibling();
self.should_complement_sql_id = false;

return;
}
}

// SQL_IDがない、かつSQL補完フラグがtrueの場合、補完する
if self.should_complement_sql_id {
clause.set_sql_id(SqlID::new("/* _SQL_ID_ */".to_string()));
self.should_complement_sql_id = false;
}
}

/// カーソルが指すノードがコメントであれば、コメントを消費してclauseに追加する
fn consume_comment_in_clause(
&mut self,
Expand All @@ -299,6 +327,21 @@ impl Visitor {

Ok(())
}

/// カーソルが指すノードがコメントであれば、コメントを消費してclauseに追加する
fn pg_consume_comments_in_clause(
&mut self,
cursor: &mut postgresql_cst_parser::tree_sitter::TreeCursor,
clause: &mut Clause,
) -> Result<(), UroboroSQLFmtError> {
while cursor.node().is_comment() {
let comment = Comment::pg_new(cursor.node());
clause.add_comment_to_child(comment)?;
cursor.goto_next_sibling();
}

Ok(())
}
}

/// cursorが指定した種類のノードを指しているかどうかをチェックする関数
Expand Down Expand Up @@ -330,7 +373,7 @@ fn pg_ensure_kind<'a>(
"pg_ensure_kind(): excepted node is {}, but actual {}\n{}",
kind,
cursor.node().kind(),
"TODO: annotation".to_string() // error_annotation_from_cursor(cursor, src)
pg_error_annotation_from_cursor(cursor, src)
)))
} else {
Ok(cursor)
Expand All @@ -356,6 +399,20 @@ fn create_alias(lhs: &Expr) -> Option<Expr> {
}
}

fn create_alias_from_expr(lhs: &Expr) -> Option<Expr> {
let loc = lhs.loc();

match lhs {
Expr::Primary(prim) => prim.element().split('.').last().map(|last| {
Expr::Primary(Box::new(PrimaryExpr::new(
convert_identifier_case(last),
loc,
)))
}),
_ => None,
}
}

/// keyword の Clauseを生成する関数。
/// 呼び出し後の cursor はキーワードの最後のノードを指す。
/// cursor のノードがキーワードと異なっていたら UroboroSQLFmtErrorを返す。
Expand Down Expand Up @@ -391,7 +448,6 @@ fn pg_create_clause(
// TODO: 複数の語からなるキーワードについて検討
// とりあえず一つの語からなるキーワードをカバー
let clause = Clause::from_pg_node(cursor.node());
cursor.goto_next_sibling();

Ok(clause)
}
Expand All @@ -416,7 +472,7 @@ fn error_annotation_from_cursor(cursor: &TreeCursor, src: &str) -> String {
}

fn pg_error_annotation_from_cursor(
cursor: &mut postgresql_cst_parser::tree_sitter::TreeCursor,
cursor: &postgresql_cst_parser::tree_sitter::TreeCursor,
src: &str,
) -> String {
let label = format!(r#"Appears as "{}" node on the CST"#, cursor.node().kind());
Expand Down
Loading
Loading