Skip to content

Commit

Permalink
Support unary operators
Browse files Browse the repository at this point in the history
  • Loading branch information
lemonadern committed Aug 22, 2024
1 parent f0a289a commit ed145a9
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 1 deletion.
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().as_str() == "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 ("+", "-", "!!", "~", "@", "|/", "||/", "NOT")
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))
}
}
39 changes: 39 additions & 0 deletions crates/uroborosql-fmt/testfiles/dst/select/unary.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
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
@-5 as absolute_value
;
select
@-5 as absolute_value
;
select
|/25 as square_root
;
select
|/25 as square_root
;
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 @-5 as absolute_value
select @ -5 as absolute_value

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

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

0 comments on commit ed145a9

Please sign in to comment.