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

Support Unary operators #81

Merged
merged 4 commits into from
Aug 22, 2024
Merged
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
5 changes: 4 additions & 1 deletion crates/uroborosql-fmt/src/cst/expr/unary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ impl UnaryExpr {
let mut result = String::new();

result.push_str(&self.operator);
result.push('\t');
// `NOT` のときは空白が必要
if self.operator.to_uppercase() == "NOT" {
result.push('\t');
}
result.push_str(&self.operand.render(depth)?);

Ok(result)
Expand Down
5 changes: 5 additions & 0 deletions crates/uroborosql-fmt/src/visitor/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ mod is;
mod paren;
mod subquery;
mod type_cast;
mod unary;

use tree_sitter::TreeCursor;

Expand Down Expand Up @@ -137,6 +138,10 @@ impl Visitor {
"all_some_any_subquery_expression" => {
Expr::Aligned(Box::new(self.visit_all_some_any_subquery(cursor, src)?))
}
"unary_expression" => {
let unary = self.visit_unary_expr(cursor, src)?;
Expr::Unary(Box::new(unary))
}
_ => {
// todo
return Err(UroboroSQLFmtError::Unimplemented(format!(
Expand Down
34 changes: 34 additions & 0 deletions crates/uroborosql-fmt/src/visitor/expr/unary.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use tree_sitter::TreeCursor;

use crate::{
cst::{unary::UnaryExpr, Location},
error::UroboroSQLFmtError,
visitor::{ensure_kind, Visitor},
};

impl Visitor {
pub(crate) fn visit_unary_expr(
&mut self,
cursor: &mut TreeCursor,
src: &str,
) -> Result<UnaryExpr, UroboroSQLFmtError> {
// cursor -> unary_expression

let mut loc = Location::new(cursor.node().range());

cursor.goto_first_child();
// cursor -> op ("+", "-", "!!", "~", "@", "|/", "||/")
let operator = cursor.node().utf8_text(src.as_bytes()).unwrap();

cursor.goto_next_sibling();
// cursor -> _expression

let operand = self.visit_expr(cursor, src)?;
loc.append(operand.loc());

cursor.goto_parent();
ensure_kind(cursor, "unary_expression", src)?;

Ok(UnaryExpr::new(operator, operand, loc))
}
}
26 changes: 26 additions & 0 deletions crates/uroborosql-fmt/testfiles/dst/select/unary.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
select
+5 as positive_value
select
+5 as positive_value
select
-10 as negative_value
select
-10 as negative_value
select
not true as not_true
select
~5 as bitwise_not
select
~5 as bitwise_not
select
|/25 as square_root
select
|/25 as square_root
select
@-5 as absolute_value
select
@-5 as absolute_value
select
||/8 as cube_root
select
||/8 as cube_root
19 changes: 19 additions & 0 deletions crates/uroborosql-fmt/testfiles/src/select/unary.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
select +5 as positive_value
select + 5 as positive_value

select -10 as negative_value
select - 10 as negative_value

select not true as not_true

select ~5 as bitwise_not
select ~ 5 as bitwise_not

select |/25 as square_root
select |/ 25 as square_root

select @-5 as absolute_value
select @ -5 as absolute_value

select ||/8 as cube_root
select ||/ 8 as cube_root
Loading