FIX: Fixes stringify with parentheses
This commit is contained in:
committed by
Nicolás Hatcher Andrés
parent
f2cb05d7bf
commit
429615ae85
@@ -2,7 +2,7 @@ use super::{super::utils::quote_name, Node, Reference};
|
|||||||
use crate::constants::{LAST_COLUMN, LAST_ROW};
|
use crate::constants::{LAST_COLUMN, LAST_ROW};
|
||||||
use crate::expressions::parser::move_formula::to_string_array_node;
|
use crate::expressions::parser::move_formula::to_string_array_node;
|
||||||
use crate::expressions::parser::static_analysis::add_implicit_intersection;
|
use crate::expressions::parser::static_analysis::add_implicit_intersection;
|
||||||
use crate::expressions::token::OpUnary;
|
use crate::expressions::token::{OpSum, OpUnary};
|
||||||
use crate::{expressions::types::CellReferenceRC, number_format::to_excel_precision_str};
|
use crate::{expressions::types::CellReferenceRC, number_format::to_excel_precision_str};
|
||||||
|
|
||||||
pub enum DisplaceData {
|
pub enum DisplaceData {
|
||||||
@@ -483,34 +483,32 @@ fn stringify(
|
|||||||
kind,
|
kind,
|
||||||
stringify(right, context, displace_data, export_to_excel)
|
stringify(right, context, displace_data, export_to_excel)
|
||||||
),
|
),
|
||||||
OpSumKind { kind, left, right } => format!(
|
OpSumKind { kind, left, right } => {
|
||||||
"{}{}{}",
|
let left_str = stringify(left, context, displace_data, export_to_excel);
|
||||||
stringify(left, context, displace_data, export_to_excel),
|
// if kind is minus then we need parentheses in the right side if they are OpSumKind or CompareKind
|
||||||
kind,
|
let right_str = if (matches!(kind, OpSum::Minus) && matches!(**right, OpSumKind { .. }))
|
||||||
stringify(right, context, displace_data, export_to_excel)
|
| matches!(**right, CompareKind { .. })
|
||||||
),
|
{
|
||||||
|
format!(
|
||||||
|
"({})",
|
||||||
|
stringify(right, context, displace_data, export_to_excel)
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
stringify(right, context, displace_data, export_to_excel)
|
||||||
|
};
|
||||||
|
|
||||||
|
format!("{left_str}{kind}{right_str}")
|
||||||
|
}
|
||||||
OpProductKind { kind, left, right } => {
|
OpProductKind { kind, left, right } => {
|
||||||
let x = match **left {
|
let x = match **left {
|
||||||
OpSumKind { .. } => format!(
|
OpSumKind { .. } | CompareKind { .. } => format!(
|
||||||
"({})",
|
|
||||||
stringify(left, context, displace_data, export_to_excel)
|
|
||||||
),
|
|
||||||
CompareKind { .. } => format!(
|
|
||||||
"({})",
|
"({})",
|
||||||
stringify(left, context, displace_data, export_to_excel)
|
stringify(left, context, displace_data, export_to_excel)
|
||||||
),
|
),
|
||||||
_ => stringify(left, context, displace_data, export_to_excel),
|
_ => stringify(left, context, displace_data, export_to_excel),
|
||||||
};
|
};
|
||||||
let y = match **right {
|
let y = match **right {
|
||||||
OpSumKind { .. } => format!(
|
OpSumKind { .. } | CompareKind { .. } | OpProductKind { .. } => format!(
|
||||||
"({})",
|
|
||||||
stringify(right, context, displace_data, export_to_excel)
|
|
||||||
),
|
|
||||||
CompareKind { .. } => format!(
|
|
||||||
"({})",
|
|
||||||
stringify(right, context, displace_data, export_to_excel)
|
|
||||||
),
|
|
||||||
OpProductKind { .. } => format!(
|
|
||||||
"({})",
|
"({})",
|
||||||
stringify(right, context, displace_data, export_to_excel)
|
stringify(right, context, displace_data, export_to_excel)
|
||||||
),
|
),
|
||||||
@@ -621,10 +619,43 @@ fn stringify(
|
|||||||
WrongVariableKind(name) => name.to_string(),
|
WrongVariableKind(name) => name.to_string(),
|
||||||
UnaryKind { kind, right } => match kind {
|
UnaryKind { kind, right } => match kind {
|
||||||
OpUnary::Minus => {
|
OpUnary::Minus => {
|
||||||
format!(
|
let needs_parentheses = match **right {
|
||||||
"-{}",
|
BooleanKind(_)
|
||||||
stringify(right, context, displace_data, export_to_excel)
|
| NumberKind(_)
|
||||||
)
|
| StringKind(_)
|
||||||
|
| ReferenceKind { .. }
|
||||||
|
| RangeKind { .. }
|
||||||
|
| WrongReferenceKind { .. }
|
||||||
|
| WrongRangeKind { .. }
|
||||||
|
| OpRangeKind { .. }
|
||||||
|
| OpConcatenateKind { .. }
|
||||||
|
| OpProductKind { .. }
|
||||||
|
| OpPowerKind { .. }
|
||||||
|
| FunctionKind { .. }
|
||||||
|
| InvalidFunctionKind { .. }
|
||||||
|
| ArrayKind(_)
|
||||||
|
| DefinedNameKind(_)
|
||||||
|
| TableNameKind(_)
|
||||||
|
| WrongVariableKind(_)
|
||||||
|
| ImplicitIntersection { .. }
|
||||||
|
| CompareKind { .. }
|
||||||
|
| ErrorKind(_)
|
||||||
|
| ParseErrorKind { .. }
|
||||||
|
| EmptyArgKind => false,
|
||||||
|
|
||||||
|
OpSumKind { .. } | UnaryKind { .. } => true,
|
||||||
|
};
|
||||||
|
if needs_parentheses {
|
||||||
|
format!(
|
||||||
|
"-({})",
|
||||||
|
stringify(right, context, displace_data, export_to_excel)
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
format!(
|
||||||
|
"-{}",
|
||||||
|
stringify(right, context, displace_data, export_to_excel)
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
OpUnary::Percentage => {
|
OpUnary::Percentage => {
|
||||||
format!(
|
format!(
|
||||||
|
|||||||
@@ -32,3 +32,36 @@ fn exp_order() {
|
|||||||
let t = parser.parse("(5)^(4)", &cell_reference);
|
let t = parser.parse("(5)^(4)", &cell_reference);
|
||||||
assert_eq!(to_string(&t, &cell_reference), "5^4");
|
assert_eq!(to_string(&t, &cell_reference), "5^4");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn correct_parenthesis() {
|
||||||
|
let worksheets = vec!["Sheet1".to_string()];
|
||||||
|
let mut parser = Parser::new(worksheets, vec![], HashMap::new());
|
||||||
|
|
||||||
|
let cell_reference = CellReferenceRC {
|
||||||
|
sheet: "Sheet1".to_string(),
|
||||||
|
row: 1,
|
||||||
|
column: 1,
|
||||||
|
};
|
||||||
|
|
||||||
|
let t = parser.parse("-(1 + 1)", &cell_reference);
|
||||||
|
assert_eq!(to_string(&t, &cell_reference), "-(1+1)");
|
||||||
|
|
||||||
|
let t = parser.parse("1 - (3 + 4)", &cell_reference);
|
||||||
|
assert_eq!(to_string(&t, &cell_reference), "1-(3+4)");
|
||||||
|
|
||||||
|
let t = parser.parse("-(1.05*(0.0284 + 0.0046) - 0.0284)", &cell_reference);
|
||||||
|
assert_eq!(to_string(&t, &cell_reference), "-(1.05*(0.0284+0.0046)-0.0284)");
|
||||||
|
|
||||||
|
let t = parser.parse("1 + (3+5)", &cell_reference);
|
||||||
|
assert_eq!(to_string(&t, &cell_reference), "1+3+5");
|
||||||
|
|
||||||
|
let t = parser.parse("1 - (3+5)", &cell_reference);
|
||||||
|
assert_eq!(to_string(&t, &cell_reference), "1-(3+5)");
|
||||||
|
|
||||||
|
let t = parser.parse("(1 - 3) - (3+5)", &cell_reference);
|
||||||
|
assert_eq!(to_string(&t, &cell_reference), "1-3-(3+5)");
|
||||||
|
|
||||||
|
let t = parser.parse("1 + (3<5)", &cell_reference);
|
||||||
|
assert_eq!(to_string(&t, &cell_reference), "1+(3<5)");
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user