UPDATE: Adds bindings to update timezone and locale
UPDATE: Update "generate locale" utility FIX: Minor fixes to UI and proper support for locales/timezones UPDATE: Adds "display language" setting to core
This commit is contained in:
@@ -110,6 +110,16 @@ impl Lexer {
|
||||
self.mode = mode;
|
||||
}
|
||||
|
||||
/// Sets the locale
|
||||
pub fn set_locale(&mut self, locale: &Locale) {
|
||||
self.locale = locale.clone();
|
||||
}
|
||||
|
||||
/// Sets the language
|
||||
pub fn set_language(&mut self, language: &Language) {
|
||||
self.language = language.clone();
|
||||
}
|
||||
|
||||
// FIXME: I don't think we should have `is_a1_mode` and `get_formula`.
|
||||
// The caller already knows those two
|
||||
|
||||
@@ -188,6 +198,7 @@ impl Lexer {
|
||||
':' => TokenType::Colon,
|
||||
';' => TokenType::Semicolon,
|
||||
'@' => TokenType::At,
|
||||
'\\' => TokenType::Backslash,
|
||||
',' => {
|
||||
if self.locale.numbers.symbols.decimal == "," {
|
||||
match self.consume_number(',') {
|
||||
|
||||
@@ -32,7 +32,9 @@ use std::collections::HashMap;
|
||||
|
||||
use crate::functions::Function;
|
||||
use crate::language::get_language;
|
||||
use crate::language::Language;
|
||||
use crate::locale::get_locale;
|
||||
use crate::locale::Locale;
|
||||
use crate::types::Table;
|
||||
|
||||
use super::lexer;
|
||||
@@ -208,6 +210,18 @@ pub struct Parser {
|
||||
defined_names: Vec<DefinedNameS>,
|
||||
context: CellReferenceRC,
|
||||
tables: HashMap<String, Table>,
|
||||
locale: Locale,
|
||||
language: Language,
|
||||
}
|
||||
|
||||
pub fn new_parser_english(
|
||||
worksheets: Vec<String>,
|
||||
defined_names: Vec<DefinedNameS>,
|
||||
tables: HashMap<String, Table>,
|
||||
) -> Parser {
|
||||
let locale = Locale::default();
|
||||
let language = Language::default();
|
||||
Parser::new(worksheets, defined_names, tables, &locale, &language)
|
||||
}
|
||||
|
||||
impl Parser {
|
||||
@@ -215,15 +229,10 @@ impl Parser {
|
||||
worksheets: Vec<String>,
|
||||
defined_names: Vec<DefinedNameS>,
|
||||
tables: HashMap<String, Table>,
|
||||
locale: &Locale,
|
||||
language: &Language,
|
||||
) -> Parser {
|
||||
let lexer = lexer::Lexer::new(
|
||||
"",
|
||||
lexer::LexerMode::A1,
|
||||
#[allow(clippy::expect_used)]
|
||||
get_locale("en").expect(""),
|
||||
#[allow(clippy::expect_used)]
|
||||
get_language("en").expect(""),
|
||||
);
|
||||
let lexer = lexer::Lexer::new("", lexer::LexerMode::A1, locale, language);
|
||||
let context = CellReferenceRC {
|
||||
sheet: worksheets.first().map_or("", |v| v).to_string(),
|
||||
column: 1,
|
||||
@@ -235,12 +244,24 @@ impl Parser {
|
||||
defined_names,
|
||||
context,
|
||||
tables,
|
||||
locale: locale.clone(),
|
||||
language: language.clone(),
|
||||
}
|
||||
}
|
||||
pub fn set_lexer_mode(&mut self, mode: lexer::LexerMode) {
|
||||
self.lexer.set_lexer_mode(mode)
|
||||
}
|
||||
|
||||
pub fn set_locale(&mut self, locale: &Locale) {
|
||||
self.locale = locale.clone();
|
||||
self.lexer.set_locale(locale);
|
||||
}
|
||||
|
||||
pub fn set_language(&mut self, language: &Language) {
|
||||
self.language = language.clone();
|
||||
self.lexer.set_language(language);
|
||||
}
|
||||
|
||||
pub fn set_worksheets_and_names(
|
||||
&mut self,
|
||||
worksheets: Vec<String>,
|
||||
@@ -256,6 +277,27 @@ impl Parser {
|
||||
self.parse_expr()
|
||||
}
|
||||
|
||||
// Returns the token used to separate arguments in functions and arrays
|
||||
// If the locale decimal separator is '.', then it is a comma ','
|
||||
// Otherwise, it is a semicolon ';'
|
||||
fn get_argument_separator_token(&self) -> TokenType {
|
||||
if self.locale.numbers.symbols.decimal == "." {
|
||||
TokenType::Comma
|
||||
} else {
|
||||
TokenType::Semicolon
|
||||
}
|
||||
}
|
||||
|
||||
// Returns the token used to separate columns in arrays
|
||||
// If the locale decimal separator is '.', then it is a semicolon ';'
|
||||
fn get_column_separator_token(&self) -> TokenType {
|
||||
if self.locale.numbers.symbols.decimal == "." {
|
||||
TokenType::Semicolon
|
||||
} else {
|
||||
TokenType::Backslash
|
||||
}
|
||||
}
|
||||
|
||||
fn get_sheet_index_by_name(&self, name: &str) -> Option<u32> {
|
||||
let worksheets = &self.worksheets;
|
||||
for (i, sheet) in worksheets.iter().enumerate() {
|
||||
@@ -464,6 +506,7 @@ impl Parser {
|
||||
|
||||
fn parse_array_row(&mut self) -> Result<Vec<ArrayNode>, Node> {
|
||||
let mut row = Vec::new();
|
||||
let column_separator_token = self.get_argument_separator_token();
|
||||
// and array can only have numbers, string or booleans
|
||||
// otherwise it is a syntax error
|
||||
let first_element = match self.parse_expr() {
|
||||
@@ -496,8 +539,7 @@ impl Parser {
|
||||
};
|
||||
row.push(first_element);
|
||||
let mut next_token = self.lexer.peek_token();
|
||||
// FIXME: this is not respecting the locale
|
||||
while next_token == TokenType::Comma {
|
||||
while next_token == column_separator_token {
|
||||
self.lexer.advance_token();
|
||||
let value = match self.parse_expr() {
|
||||
Node::BooleanKind(s) => ArrayNode::Boolean(s),
|
||||
@@ -555,6 +597,7 @@ impl Parser {
|
||||
TokenType::String(s) => Node::StringKind(s),
|
||||
TokenType::LeftBrace => {
|
||||
// It's an array. It's a collection of rows all of the same dimension
|
||||
let column_separator_token = self.get_column_separator_token();
|
||||
|
||||
let first_row = match self.parse_array_row() {
|
||||
Ok(s) => s,
|
||||
@@ -564,9 +607,8 @@ impl Parser {
|
||||
|
||||
let mut matrix = Vec::new();
|
||||
matrix.push(first_row);
|
||||
// FIXME: this is not respecting the locale
|
||||
let mut next_token = self.lexer.peek_token();
|
||||
while next_token == TokenType::Semicolon {
|
||||
while next_token == column_separator_token {
|
||||
self.lexer.advance_token();
|
||||
let row = match self.parse_array_row() {
|
||||
Ok(s) => s,
|
||||
@@ -715,12 +757,7 @@ impl Parser {
|
||||
message: err.message,
|
||||
};
|
||||
}
|
||||
if let Some(function_kind) = Function::get_function(&name) {
|
||||
return Node::FunctionKind {
|
||||
kind: function_kind,
|
||||
args,
|
||||
};
|
||||
}
|
||||
// We should do this *only* importing functions from xlsx
|
||||
if &name == "_xlfn.SINGLE" {
|
||||
if args.len() != 1 {
|
||||
return Node::ParseErrorKind {
|
||||
@@ -735,6 +772,17 @@ impl Parser {
|
||||
child: Box::new(args[0].clone()),
|
||||
};
|
||||
}
|
||||
// We should do this *only* importing functions from xlsx
|
||||
if let Some(function_kind) = self
|
||||
.language
|
||||
.functions
|
||||
.lookup(name.trim_start_matches("_xlfn."))
|
||||
{
|
||||
return Node::FunctionKind {
|
||||
kind: function_kind,
|
||||
args,
|
||||
};
|
||||
}
|
||||
return Node::InvalidFunctionKind { name, args };
|
||||
}
|
||||
let context = &self.context;
|
||||
@@ -849,6 +897,7 @@ impl Parser {
|
||||
| TokenType::RightBracket
|
||||
| TokenType::Colon
|
||||
| TokenType::Semicolon
|
||||
| TokenType::Backslash
|
||||
| TokenType::RightBrace
|
||||
| TokenType::Comma
|
||||
| TokenType::Bang
|
||||
@@ -1048,12 +1097,13 @@ impl Parser {
|
||||
}
|
||||
|
||||
fn parse_function_args(&mut self) -> Result<Vec<Node>, Node> {
|
||||
let arg_separator_token = &self.get_argument_separator_token();
|
||||
let mut args: Vec<Node> = Vec::new();
|
||||
let mut next_token = self.lexer.peek_token();
|
||||
if next_token == TokenType::RightParenthesis {
|
||||
return Ok(args);
|
||||
}
|
||||
if self.lexer.peek_token() == TokenType::Comma {
|
||||
if &self.lexer.peek_token() == arg_separator_token {
|
||||
args.push(Node::EmptyArgKind);
|
||||
} else {
|
||||
let t = self.parse_expr();
|
||||
@@ -1063,11 +1113,11 @@ impl Parser {
|
||||
args.push(t);
|
||||
}
|
||||
next_token = self.lexer.peek_token();
|
||||
while next_token == TokenType::Comma {
|
||||
while &next_token == arg_separator_token {
|
||||
self.lexer.advance_token();
|
||||
if self.lexer.peek_token() == TokenType::Comma {
|
||||
if &self.lexer.peek_token() == arg_separator_token {
|
||||
args.push(Node::EmptyArgKind);
|
||||
next_token = TokenType::Comma;
|
||||
next_token = arg_separator_token.clone();
|
||||
} else if self.lexer.peek_token() == TokenType::RightParenthesis {
|
||||
args.push(Node::EmptyArgKind);
|
||||
return Ok(args);
|
||||
|
||||
@@ -5,6 +5,8 @@ use super::{
|
||||
use crate::{
|
||||
constants::{LAST_COLUMN, LAST_ROW},
|
||||
expressions::token::OpUnary,
|
||||
language::Language,
|
||||
locale::Locale,
|
||||
};
|
||||
use crate::{
|
||||
expressions::types::{Area, CellReferenceRC},
|
||||
@@ -38,38 +40,78 @@ pub(crate) struct MoveContext<'a> {
|
||||
/// We are moving a formula in (row, column) to (row+row_delta, column + column_delta).
|
||||
/// All references that do not point to a cell in area will be left untouched.
|
||||
/// All references that point to a cell in area will be displaced
|
||||
pub(crate) fn move_formula(node: &Node, move_context: &MoveContext) -> String {
|
||||
to_string_moved(node, move_context)
|
||||
pub(crate) fn move_formula(
|
||||
node: &Node,
|
||||
move_context: &MoveContext,
|
||||
locale: &Locale,
|
||||
language: &Language,
|
||||
) -> String {
|
||||
to_string_moved(node, move_context, locale, language)
|
||||
}
|
||||
|
||||
fn move_function(name: &str, args: &Vec<Node>, move_context: &MoveContext) -> String {
|
||||
fn move_function(
|
||||
name: &str,
|
||||
args: &Vec<Node>,
|
||||
move_context: &MoveContext,
|
||||
locale: &Locale,
|
||||
language: &Language,
|
||||
) -> String {
|
||||
let mut first = true;
|
||||
let mut arguments = "".to_string();
|
||||
for el in args {
|
||||
if !first {
|
||||
arguments = format!("{},{}", arguments, to_string_moved(el, move_context));
|
||||
arguments = format!(
|
||||
"{},{}",
|
||||
arguments,
|
||||
to_string_moved(el, move_context, locale, language)
|
||||
);
|
||||
} else {
|
||||
first = false;
|
||||
arguments = to_string_moved(el, move_context);
|
||||
arguments = to_string_moved(el, move_context, locale, language);
|
||||
}
|
||||
}
|
||||
format!("{name}({arguments})")
|
||||
}
|
||||
|
||||
pub(crate) fn to_string_array_node(node: &ArrayNode) -> String {
|
||||
fn format_number_locale(number: f64, locale: &Locale) -> String {
|
||||
let s = to_excel_precision_str(number);
|
||||
let decimal = &locale.numbers.symbols.decimal;
|
||||
if decimal == "." {
|
||||
s
|
||||
} else {
|
||||
s.replace('.', decimal)
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn to_string_array_node(
|
||||
node: &ArrayNode,
|
||||
locale: &Locale,
|
||||
language: &Language,
|
||||
) -> String {
|
||||
match node {
|
||||
ArrayNode::Boolean(value) => format!("{value}").to_ascii_uppercase(),
|
||||
ArrayNode::Number(number) => to_excel_precision_str(*number),
|
||||
ArrayNode::Boolean(value) => {
|
||||
if *value {
|
||||
language.booleans.r#true.to_ascii_uppercase()
|
||||
} else {
|
||||
language.booleans.r#false.to_ascii_uppercase()
|
||||
}
|
||||
}
|
||||
ArrayNode::Number(number) => format_number_locale(*number, locale),
|
||||
ArrayNode::String(value) => format!("\"{value}\""),
|
||||
ArrayNode::Error(kind) => format!("{kind}"),
|
||||
}
|
||||
}
|
||||
|
||||
fn to_string_moved(node: &Node, move_context: &MoveContext) -> String {
|
||||
fn to_string_moved(
|
||||
node: &Node,
|
||||
move_context: &MoveContext,
|
||||
locale: &Locale,
|
||||
language: &Language,
|
||||
) -> String {
|
||||
use self::Node::*;
|
||||
match node {
|
||||
BooleanKind(value) => format!("{value}").to_ascii_uppercase(),
|
||||
NumberKind(number) => to_excel_precision_str(*number),
|
||||
NumberKind(number) => format_number_locale(*number, locale),
|
||||
StringKind(value) => format!("\"{value}\""),
|
||||
ReferenceKind {
|
||||
sheet_name,
|
||||
@@ -329,55 +371,81 @@ fn to_string_moved(node: &Node, move_context: &MoveContext) -> String {
|
||||
}
|
||||
OpRangeKind { left, right } => format!(
|
||||
"{}:{}",
|
||||
to_string_moved(left, move_context),
|
||||
to_string_moved(right, move_context),
|
||||
to_string_moved(left, move_context, locale, language),
|
||||
to_string_moved(right, move_context, locale, language),
|
||||
),
|
||||
OpConcatenateKind { left, right } => format!(
|
||||
"{}&{}",
|
||||
to_string_moved(left, move_context),
|
||||
to_string_moved(right, move_context),
|
||||
to_string_moved(left, move_context, locale, language),
|
||||
to_string_moved(right, move_context, locale, language),
|
||||
),
|
||||
OpSumKind { kind, left, right } => format!(
|
||||
"{}{}{}",
|
||||
to_string_moved(left, move_context),
|
||||
to_string_moved(left, move_context, locale, language),
|
||||
kind,
|
||||
to_string_moved(right, move_context),
|
||||
to_string_moved(right, move_context, locale, language),
|
||||
),
|
||||
OpProductKind { kind, left, right } => {
|
||||
let x = match **left {
|
||||
OpSumKind { .. } => format!("({})", to_string_moved(left, move_context)),
|
||||
CompareKind { .. } => format!("({})", to_string_moved(left, move_context)),
|
||||
_ => to_string_moved(left, move_context),
|
||||
OpSumKind { .. } => format!(
|
||||
"({})",
|
||||
to_string_moved(left, move_context, locale, language)
|
||||
),
|
||||
CompareKind { .. } => format!(
|
||||
"({})",
|
||||
to_string_moved(left, move_context, locale, language)
|
||||
),
|
||||
_ => to_string_moved(left, move_context, locale, language),
|
||||
};
|
||||
let y = match **right {
|
||||
OpSumKind { .. } => format!("({})", to_string_moved(right, move_context)),
|
||||
CompareKind { .. } => format!("({})", to_string_moved(right, move_context)),
|
||||
OpProductKind { .. } => format!("({})", to_string_moved(right, move_context)),
|
||||
OpSumKind { .. } => format!(
|
||||
"({})",
|
||||
to_string_moved(right, move_context, locale, language)
|
||||
),
|
||||
CompareKind { .. } => format!(
|
||||
"({})",
|
||||
to_string_moved(right, move_context, locale, language)
|
||||
),
|
||||
OpProductKind { .. } => format!(
|
||||
"({})",
|
||||
to_string_moved(right, move_context, locale, language)
|
||||
),
|
||||
UnaryKind { .. } => {
|
||||
format!("({})", to_string_moved(right, move_context))
|
||||
format!(
|
||||
"({})",
|
||||
to_string_moved(right, move_context, locale, language)
|
||||
)
|
||||
}
|
||||
_ => to_string_moved(right, move_context),
|
||||
_ => to_string_moved(right, move_context, locale, language),
|
||||
};
|
||||
format!("{x}{kind}{y}")
|
||||
}
|
||||
OpPowerKind { left, right } => format!(
|
||||
"{}^{}",
|
||||
to_string_moved(left, move_context),
|
||||
to_string_moved(right, move_context),
|
||||
to_string_moved(left, move_context, locale, language),
|
||||
to_string_moved(right, move_context, locale, language),
|
||||
),
|
||||
InvalidFunctionKind { name, args } => move_function(name, args, move_context),
|
||||
InvalidFunctionKind { name, args } => {
|
||||
move_function(name, args, move_context, locale, language)
|
||||
}
|
||||
FunctionKind { kind, args } => {
|
||||
let name = &kind.to_string();
|
||||
move_function(name, args, move_context)
|
||||
let name = &kind.to_localized_name(language);
|
||||
move_function(name, args, move_context, locale, language)
|
||||
}
|
||||
ArrayKind(args) => {
|
||||
let mut first_row = true;
|
||||
let mut matrix_string = String::new();
|
||||
|
||||
// Each element in `args` is assumed to be one "row" (itself a `Vec<T>`).
|
||||
let row_separator = if locale.numbers.symbols.decimal == "." {
|
||||
';'
|
||||
} else {
|
||||
'/'
|
||||
};
|
||||
let col_separator = if row_separator == ';' { ',' } else { ';' };
|
||||
for row in args {
|
||||
if !first_row {
|
||||
matrix_string.push(',');
|
||||
matrix_string.push(col_separator);
|
||||
} else {
|
||||
first_row = false;
|
||||
}
|
||||
@@ -387,13 +455,13 @@ fn to_string_moved(node: &Node, move_context: &MoveContext) -> String {
|
||||
let mut row_string = String::new();
|
||||
for el in row {
|
||||
if !first_col {
|
||||
row_string.push(',');
|
||||
row_string.push(row_separator);
|
||||
} else {
|
||||
first_col = false;
|
||||
}
|
||||
|
||||
// Reuse your existing element-stringification function
|
||||
row_string.push_str(&to_string_array_node(el));
|
||||
row_string.push_str(&to_string_array_node(el, locale, language));
|
||||
}
|
||||
|
||||
// Enclose the row in braces
|
||||
@@ -410,13 +478,19 @@ fn to_string_moved(node: &Node, move_context: &MoveContext) -> String {
|
||||
WrongVariableKind(name) => name.to_string(),
|
||||
CompareKind { kind, left, right } => format!(
|
||||
"{}{}{}",
|
||||
to_string_moved(left, move_context),
|
||||
to_string_moved(left, move_context, locale, language),
|
||||
kind,
|
||||
to_string_moved(right, move_context),
|
||||
to_string_moved(right, move_context, locale, language),
|
||||
),
|
||||
UnaryKind { kind, right } => match kind {
|
||||
OpUnary::Minus => format!("-{}", to_string_moved(right, move_context)),
|
||||
OpUnary::Percentage => format!("{}%", to_string_moved(right, move_context)),
|
||||
OpUnary::Minus => format!(
|
||||
"-{}",
|
||||
to_string_moved(right, move_context, locale, language)
|
||||
),
|
||||
OpUnary::Percentage => format!(
|
||||
"{}%",
|
||||
to_string_moved(right, move_context, locale, language)
|
||||
),
|
||||
},
|
||||
ErrorKind(kind) => format!("{kind}"),
|
||||
ParseErrorKind {
|
||||
@@ -429,7 +503,10 @@ fn to_string_moved(node: &Node, move_context: &MoveContext) -> String {
|
||||
automatic: _,
|
||||
child,
|
||||
} => {
|
||||
format!("@{}", to_string_moved(child, move_context))
|
||||
format!(
|
||||
"@{}",
|
||||
to_string_moved(child, move_context, locale, language)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,8 @@ use crate::constants::{LAST_COLUMN, LAST_ROW};
|
||||
use crate::expressions::parser::move_formula::to_string_array_node;
|
||||
use crate::expressions::parser::static_analysis::add_implicit_intersection;
|
||||
use crate::expressions::token::{OpSum, OpUnary};
|
||||
use crate::language::{get_language, Language};
|
||||
use crate::locale::{get_locale, Locale};
|
||||
use crate::{expressions::types::CellReferenceRC, number_format::to_excel_precision_str};
|
||||
|
||||
pub enum DisplaceData {
|
||||
@@ -43,17 +45,44 @@ pub enum DisplaceData {
|
||||
|
||||
/// This is the internal mode in IronCalc
|
||||
pub fn to_rc_format(node: &Node) -> String {
|
||||
stringify(node, None, &DisplaceData::None, false)
|
||||
#[allow(clippy::expect_used)]
|
||||
let locale = get_locale("en").expect("");
|
||||
#[allow(clippy::expect_used)]
|
||||
let language = get_language("en").expect("");
|
||||
stringify(node, None, &DisplaceData::None, false, locale, language)
|
||||
}
|
||||
|
||||
/// This is the mode used to display the formula in the UI
|
||||
pub fn to_string(node: &Node, context: &CellReferenceRC) -> String {
|
||||
stringify(node, Some(context), &DisplaceData::None, false)
|
||||
pub fn to_localized_string(
|
||||
node: &Node,
|
||||
context: &CellReferenceRC,
|
||||
locale: &Locale,
|
||||
language: &Language,
|
||||
) -> String {
|
||||
stringify(
|
||||
node,
|
||||
Some(context),
|
||||
&DisplaceData::None,
|
||||
false,
|
||||
locale,
|
||||
language,
|
||||
)
|
||||
}
|
||||
|
||||
/// This is the mode used to export the formula to Excel
|
||||
pub fn to_excel_string(node: &Node, context: &CellReferenceRC) -> String {
|
||||
stringify(node, Some(context), &DisplaceData::None, true)
|
||||
#[allow(clippy::expect_used)]
|
||||
let locale = get_locale("en").expect("");
|
||||
#[allow(clippy::expect_used)]
|
||||
let language = get_language("en").expect("");
|
||||
stringify(
|
||||
node,
|
||||
Some(context),
|
||||
&DisplaceData::None,
|
||||
true,
|
||||
locale,
|
||||
language,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn to_string_displaced(
|
||||
@@ -61,7 +90,11 @@ pub fn to_string_displaced(
|
||||
context: &CellReferenceRC,
|
||||
displace_data: &DisplaceData,
|
||||
) -> String {
|
||||
stringify(node, Some(context), displace_data, false)
|
||||
#[allow(clippy::expect_used)]
|
||||
let locale = get_locale("en").expect("");
|
||||
#[allow(clippy::expect_used)]
|
||||
let language = get_language("en").expect("");
|
||||
stringify(node, Some(context), displace_data, false, locale, language)
|
||||
}
|
||||
|
||||
/// Converts a local reference to a string applying some displacement if needed.
|
||||
@@ -273,19 +306,41 @@ fn format_function(
|
||||
context: Option<&CellReferenceRC>,
|
||||
displace_data: &DisplaceData,
|
||||
export_to_excel: bool,
|
||||
locale: &Locale,
|
||||
language: &Language,
|
||||
) -> String {
|
||||
let mut first = true;
|
||||
let mut arguments = "".to_string();
|
||||
let arg_separator = if locale.numbers.symbols.decimal == "." {
|
||||
','
|
||||
} else {
|
||||
';'
|
||||
};
|
||||
for el in args {
|
||||
if !first {
|
||||
arguments = format!(
|
||||
"{},{}",
|
||||
"{}{}{}",
|
||||
arguments,
|
||||
stringify(el, context, displace_data, export_to_excel)
|
||||
arg_separator,
|
||||
stringify(
|
||||
el,
|
||||
context,
|
||||
displace_data,
|
||||
export_to_excel,
|
||||
locale,
|
||||
language
|
||||
)
|
||||
);
|
||||
} else {
|
||||
first = false;
|
||||
arguments = stringify(el, context, displace_data, export_to_excel);
|
||||
arguments = stringify(
|
||||
el,
|
||||
context,
|
||||
displace_data,
|
||||
export_to_excel,
|
||||
locale,
|
||||
language,
|
||||
);
|
||||
}
|
||||
}
|
||||
format!("{name}({arguments})")
|
||||
@@ -321,11 +376,26 @@ fn stringify(
|
||||
context: Option<&CellReferenceRC>,
|
||||
displace_data: &DisplaceData,
|
||||
export_to_excel: bool,
|
||||
locale: &Locale,
|
||||
language: &Language,
|
||||
) -> String {
|
||||
use self::Node::*;
|
||||
match node {
|
||||
BooleanKind(value) => format!("{value}").to_ascii_uppercase(),
|
||||
NumberKind(number) => to_excel_precision_str(*number),
|
||||
BooleanKind(value) => {
|
||||
if *value {
|
||||
language.booleans.r#true.to_string()
|
||||
} else {
|
||||
language.booleans.r#false.to_string()
|
||||
}
|
||||
}
|
||||
NumberKind(number) => {
|
||||
let s = to_excel_precision_str(*number);
|
||||
if locale.numbers.symbols.decimal == "." {
|
||||
s
|
||||
} else {
|
||||
s.replace(".", &locale.numbers.symbols.decimal)
|
||||
}
|
||||
}
|
||||
StringKind(value) => format!("\"{value}\""),
|
||||
WrongReferenceKind {
|
||||
sheet_name,
|
||||
@@ -469,32 +539,95 @@ fn stringify(
|
||||
}
|
||||
OpRangeKind { left, right } => format!(
|
||||
"{}:{}",
|
||||
stringify(left, context, displace_data, export_to_excel),
|
||||
stringify(right, context, displace_data, export_to_excel)
|
||||
stringify(
|
||||
left,
|
||||
context,
|
||||
displace_data,
|
||||
export_to_excel,
|
||||
locale,
|
||||
language
|
||||
),
|
||||
stringify(
|
||||
right,
|
||||
context,
|
||||
displace_data,
|
||||
export_to_excel,
|
||||
locale,
|
||||
language
|
||||
)
|
||||
),
|
||||
OpConcatenateKind { left, right } => format!(
|
||||
"{}&{}",
|
||||
stringify(left, context, displace_data, export_to_excel),
|
||||
stringify(right, context, displace_data, export_to_excel)
|
||||
stringify(
|
||||
left,
|
||||
context,
|
||||
displace_data,
|
||||
export_to_excel,
|
||||
locale,
|
||||
language
|
||||
),
|
||||
stringify(
|
||||
right,
|
||||
context,
|
||||
displace_data,
|
||||
export_to_excel,
|
||||
locale,
|
||||
language
|
||||
)
|
||||
),
|
||||
CompareKind { kind, left, right } => format!(
|
||||
"{}{}{}",
|
||||
stringify(left, context, displace_data, export_to_excel),
|
||||
stringify(
|
||||
left,
|
||||
context,
|
||||
displace_data,
|
||||
export_to_excel,
|
||||
locale,
|
||||
language
|
||||
),
|
||||
kind,
|
||||
stringify(right, context, displace_data, export_to_excel)
|
||||
stringify(
|
||||
right,
|
||||
context,
|
||||
displace_data,
|
||||
export_to_excel,
|
||||
locale,
|
||||
language
|
||||
)
|
||||
),
|
||||
OpSumKind { kind, left, right } => {
|
||||
let left_str = stringify(left, context, displace_data, export_to_excel);
|
||||
let left_str = stringify(
|
||||
left,
|
||||
context,
|
||||
displace_data,
|
||||
export_to_excel,
|
||||
locale,
|
||||
language,
|
||||
);
|
||||
// if kind is minus then we need parentheses in the right side if they are OpSumKind or CompareKind
|
||||
let right_str = if (matches!(kind, OpSum::Minus) && matches!(**right, OpSumKind { .. }))
|
||||
| matches!(**right, CompareKind { .. })
|
||||
{
|
||||
format!(
|
||||
"({})",
|
||||
stringify(right, context, displace_data, export_to_excel)
|
||||
stringify(
|
||||
right,
|
||||
context,
|
||||
displace_data,
|
||||
export_to_excel,
|
||||
locale,
|
||||
language
|
||||
)
|
||||
)
|
||||
} else {
|
||||
stringify(right, context, displace_data, export_to_excel)
|
||||
stringify(
|
||||
right,
|
||||
context,
|
||||
displace_data,
|
||||
export_to_excel,
|
||||
locale,
|
||||
language,
|
||||
)
|
||||
};
|
||||
|
||||
format!("{left_str}{kind}{right_str}")
|
||||
@@ -503,16 +636,44 @@ fn stringify(
|
||||
let x = match **left {
|
||||
OpSumKind { .. } | CompareKind { .. } => format!(
|
||||
"({})",
|
||||
stringify(left, context, displace_data, export_to_excel)
|
||||
stringify(
|
||||
left,
|
||||
context,
|
||||
displace_data,
|
||||
export_to_excel,
|
||||
locale,
|
||||
language
|
||||
)
|
||||
),
|
||||
_ => stringify(
|
||||
left,
|
||||
context,
|
||||
displace_data,
|
||||
export_to_excel,
|
||||
locale,
|
||||
language,
|
||||
),
|
||||
_ => stringify(left, context, displace_data, export_to_excel),
|
||||
};
|
||||
let y = match **right {
|
||||
OpSumKind { .. } | CompareKind { .. } | OpProductKind { .. } => format!(
|
||||
"({})",
|
||||
stringify(right, context, displace_data, export_to_excel)
|
||||
stringify(
|
||||
right,
|
||||
context,
|
||||
displace_data,
|
||||
export_to_excel,
|
||||
locale,
|
||||
language
|
||||
)
|
||||
),
|
||||
_ => stringify(
|
||||
right,
|
||||
context,
|
||||
displace_data,
|
||||
export_to_excel,
|
||||
locale,
|
||||
language,
|
||||
),
|
||||
_ => stringify(right, context, displace_data, export_to_excel),
|
||||
};
|
||||
format!("{x}{kind}{y}")
|
||||
}
|
||||
@@ -528,7 +689,14 @@ fn stringify(
|
||||
| DefinedNameKind(_)
|
||||
| TableNameKind(_)
|
||||
| WrongVariableKind(_)
|
||||
| WrongRangeKind { .. } => stringify(left, context, displace_data, export_to_excel),
|
||||
| WrongRangeKind { .. } => stringify(
|
||||
left,
|
||||
context,
|
||||
displace_data,
|
||||
export_to_excel,
|
||||
locale,
|
||||
language,
|
||||
),
|
||||
OpRangeKind { .. }
|
||||
| OpConcatenateKind { .. }
|
||||
| OpProductKind { .. }
|
||||
@@ -543,7 +711,14 @@ fn stringify(
|
||||
| ImplicitIntersection { .. }
|
||||
| EmptyArgKind => format!(
|
||||
"({})",
|
||||
stringify(left, context, displace_data, export_to_excel)
|
||||
stringify(
|
||||
left,
|
||||
context,
|
||||
displace_data,
|
||||
export_to_excel,
|
||||
locale,
|
||||
language
|
||||
)
|
||||
),
|
||||
};
|
||||
let y = match **right {
|
||||
@@ -556,9 +731,14 @@ fn stringify(
|
||||
| DefinedNameKind(_)
|
||||
| TableNameKind(_)
|
||||
| WrongVariableKind(_)
|
||||
| WrongRangeKind { .. } => {
|
||||
stringify(right, context, displace_data, export_to_excel)
|
||||
}
|
||||
| WrongRangeKind { .. } => stringify(
|
||||
right,
|
||||
context,
|
||||
displace_data,
|
||||
export_to_excel,
|
||||
locale,
|
||||
language,
|
||||
),
|
||||
OpRangeKind { .. }
|
||||
| OpConcatenateKind { .. }
|
||||
| OpProductKind { .. }
|
||||
@@ -574,29 +754,56 @@ fn stringify(
|
||||
| ImplicitIntersection { .. }
|
||||
| EmptyArgKind => format!(
|
||||
"({})",
|
||||
stringify(right, context, displace_data, export_to_excel)
|
||||
stringify(
|
||||
right,
|
||||
context,
|
||||
displace_data,
|
||||
export_to_excel,
|
||||
locale,
|
||||
language
|
||||
)
|
||||
),
|
||||
};
|
||||
format!("{x}^{y}")
|
||||
}
|
||||
InvalidFunctionKind { name, args } => {
|
||||
format_function(name, args, context, displace_data, export_to_excel)
|
||||
}
|
||||
InvalidFunctionKind { name, args } => format_function(
|
||||
&name.to_ascii_lowercase(),
|
||||
args,
|
||||
context,
|
||||
displace_data,
|
||||
export_to_excel,
|
||||
locale,
|
||||
language,
|
||||
),
|
||||
FunctionKind { kind, args } => {
|
||||
let name = if export_to_excel {
|
||||
kind.to_xlsx_string()
|
||||
} else {
|
||||
kind.to_string()
|
||||
kind.to_localized_name(language)
|
||||
};
|
||||
format_function(&name, args, context, displace_data, export_to_excel)
|
||||
format_function(
|
||||
&name,
|
||||
args,
|
||||
context,
|
||||
displace_data,
|
||||
export_to_excel,
|
||||
locale,
|
||||
language,
|
||||
)
|
||||
}
|
||||
ArrayKind(args) => {
|
||||
let mut first_row = true;
|
||||
let mut matrix_string = String::new();
|
||||
let row_separator = if locale.numbers.symbols.decimal == "." {
|
||||
';'
|
||||
} else {
|
||||
'/'
|
||||
};
|
||||
let col_separator = if row_separator == ';' { ',' } else { ';' };
|
||||
|
||||
for row in args {
|
||||
if !first_row {
|
||||
matrix_string.push(';');
|
||||
matrix_string.push(row_separator);
|
||||
} else {
|
||||
first_row = false;
|
||||
}
|
||||
@@ -604,11 +811,11 @@ fn stringify(
|
||||
let mut row_string = String::new();
|
||||
for el in row {
|
||||
if !first_column {
|
||||
row_string.push(',');
|
||||
row_string.push(col_separator);
|
||||
} else {
|
||||
first_column = false;
|
||||
}
|
||||
row_string.push_str(&to_string_array_node(el));
|
||||
row_string.push_str(&to_string_array_node(el, locale, language));
|
||||
}
|
||||
matrix_string.push_str(&row_string);
|
||||
}
|
||||
@@ -647,19 +854,40 @@ fn stringify(
|
||||
if needs_parentheses {
|
||||
format!(
|
||||
"-({})",
|
||||
stringify(right, context, displace_data, export_to_excel)
|
||||
stringify(
|
||||
right,
|
||||
context,
|
||||
displace_data,
|
||||
export_to_excel,
|
||||
locale,
|
||||
language
|
||||
)
|
||||
)
|
||||
} else {
|
||||
format!(
|
||||
"-{}",
|
||||
stringify(right, context, displace_data, export_to_excel)
|
||||
stringify(
|
||||
right,
|
||||
context,
|
||||
displace_data,
|
||||
export_to_excel,
|
||||
locale,
|
||||
language
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
OpUnary::Percentage => {
|
||||
format!(
|
||||
"{}%",
|
||||
stringify(right, context, displace_data, export_to_excel)
|
||||
stringify(
|
||||
right,
|
||||
context,
|
||||
displace_data,
|
||||
export_to_excel,
|
||||
locale,
|
||||
language
|
||||
)
|
||||
)
|
||||
}
|
||||
},
|
||||
@@ -680,17 +908,38 @@ fn stringify(
|
||||
|
||||
add_implicit_intersection(&mut new_node, true);
|
||||
if matches!(&new_node, Node::ImplicitIntersection { .. }) {
|
||||
return stringify(child, context, displace_data, export_to_excel);
|
||||
return stringify(
|
||||
child,
|
||||
context,
|
||||
displace_data,
|
||||
export_to_excel,
|
||||
locale,
|
||||
language,
|
||||
);
|
||||
}
|
||||
|
||||
return format!(
|
||||
"_xlfn.SINGLE({})",
|
||||
stringify(child, context, displace_data, export_to_excel)
|
||||
stringify(
|
||||
child,
|
||||
context,
|
||||
displace_data,
|
||||
export_to_excel,
|
||||
locale,
|
||||
language
|
||||
)
|
||||
);
|
||||
}
|
||||
format!(
|
||||
"@{}",
|
||||
stringify(child, context, displace_data, export_to_excel)
|
||||
stringify(
|
||||
child,
|
||||
context,
|
||||
displace_data,
|
||||
export_to_excel,
|
||||
locale,
|
||||
language
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,10 @@ mod test_general;
|
||||
mod test_implicit_intersection;
|
||||
mod test_issue_155;
|
||||
mod test_issue_483;
|
||||
mod test_languages;
|
||||
mod test_locales;
|
||||
mod test_move_formula;
|
||||
mod test_ranges;
|
||||
mod test_stringify;
|
||||
mod test_tables;
|
||||
mod utils;
|
||||
|
||||
@@ -2,8 +2,8 @@ use std::collections::HashMap;
|
||||
|
||||
use crate::expressions::{
|
||||
parser::{
|
||||
stringify::{to_excel_string, to_string},
|
||||
Parser,
|
||||
stringify::to_excel_string,
|
||||
tests::utils::{new_parser, to_english_localized_string},
|
||||
},
|
||||
types::CellReferenceRC,
|
||||
};
|
||||
@@ -13,7 +13,7 @@ use crate::expressions::parser::static_analysis::add_implicit_intersection;
|
||||
#[test]
|
||||
fn simple_test() {
|
||||
let worksheets = vec!["Sheet1".to_string()];
|
||||
let mut parser = Parser::new(worksheets, vec![], HashMap::new());
|
||||
let mut parser = new_parser(worksheets, vec![], HashMap::new());
|
||||
|
||||
// Reference cell is Sheet1!A1
|
||||
let cell_reference = CellReferenceRC {
|
||||
@@ -72,7 +72,7 @@ fn simple_test() {
|
||||
for (formula, expected) in cases {
|
||||
let mut t = parser.parse(formula, &cell_reference);
|
||||
add_implicit_intersection(&mut t, true);
|
||||
let r = to_string(&t, &cell_reference);
|
||||
let r = to_english_localized_string(&t, &cell_reference);
|
||||
assert_eq!(r, expected);
|
||||
let excel_formula = to_excel_string(&t, &cell_reference);
|
||||
assert_eq!(excel_formula, formula);
|
||||
|
||||
@@ -2,14 +2,15 @@
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::expressions::parser::stringify::{to_rc_format, to_string};
|
||||
use crate::expressions::parser::{ArrayNode, Node, Parser};
|
||||
use crate::expressions::parser::stringify::to_rc_format;
|
||||
use crate::expressions::parser::tests::utils::{new_parser, to_english_localized_string};
|
||||
use crate::expressions::parser::{ArrayNode, Node};
|
||||
use crate::expressions::types::CellReferenceRC;
|
||||
|
||||
#[test]
|
||||
fn simple_horizontal() {
|
||||
let worksheets = vec!["Sheet1".to_string()];
|
||||
let mut parser = Parser::new(worksheets, vec![], HashMap::new());
|
||||
let mut parser = new_parser(worksheets, vec![], HashMap::new());
|
||||
|
||||
// Reference cell is Sheet1!A1
|
||||
let cell_reference = CellReferenceRC {
|
||||
@@ -28,13 +29,16 @@ fn simple_horizontal() {
|
||||
);
|
||||
|
||||
assert_eq!(to_rc_format(&horizontal), "{1,2,3}");
|
||||
assert_eq!(to_string(&horizontal, &cell_reference), "{1,2,3}");
|
||||
assert_eq!(
|
||||
to_english_localized_string(&horizontal, &cell_reference),
|
||||
"{1,2,3}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn simple_vertical() {
|
||||
let worksheets = vec!["Sheet1".to_string()];
|
||||
let mut parser = Parser::new(worksheets, vec![], HashMap::new());
|
||||
let mut parser = new_parser(worksheets, vec![], HashMap::new());
|
||||
|
||||
// Reference cell is Sheet1!A1
|
||||
let cell_reference = CellReferenceRC {
|
||||
@@ -52,13 +56,16 @@ fn simple_vertical() {
|
||||
])
|
||||
);
|
||||
assert_eq!(to_rc_format(&vertical), "{1;2;3}");
|
||||
assert_eq!(to_string(&vertical, &cell_reference), "{1;2;3}");
|
||||
assert_eq!(
|
||||
to_english_localized_string(&vertical, &cell_reference),
|
||||
"{1;2;3}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn simple_matrix() {
|
||||
let worksheets = vec!["Sheet1".to_string()];
|
||||
let mut parser = Parser::new(worksheets, vec![], HashMap::new());
|
||||
let mut parser = new_parser(worksheets, vec![], HashMap::new());
|
||||
|
||||
// Reference cell is Sheet1!A1
|
||||
let cell_reference = CellReferenceRC {
|
||||
@@ -88,5 +95,8 @@ fn simple_matrix() {
|
||||
])
|
||||
);
|
||||
assert_eq!(to_rc_format(&matrix), "{1,2,3;4,5,6;7,8,9}");
|
||||
assert_eq!(to_string(&matrix, &cell_reference), "{1,2,3;4,5,6;7,8,9}");
|
||||
assert_eq!(
|
||||
to_english_localized_string(&matrix, &cell_reference),
|
||||
"{1,2,3;4,5,6;7,8,9}"
|
||||
);
|
||||
}
|
||||
|
||||
@@ -3,12 +3,12 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::expressions::lexer::LexerMode;
|
||||
use crate::expressions::parser::stringify::{
|
||||
to_rc_format, to_string, to_string_displaced, DisplaceData,
|
||||
};
|
||||
use crate::expressions::parser::{Node, Parser};
|
||||
use crate::expressions::parser::stringify::{to_rc_format, to_string_displaced, DisplaceData};
|
||||
use crate::expressions::parser::Node;
|
||||
use crate::expressions::types::CellReferenceRC;
|
||||
|
||||
use crate::expressions::parser::tests::utils::{new_parser, to_english_localized_string};
|
||||
|
||||
struct Formula<'a> {
|
||||
initial: &'a str,
|
||||
expected: &'a str,
|
||||
@@ -17,7 +17,7 @@ struct Formula<'a> {
|
||||
#[test]
|
||||
fn test_parser_reference() {
|
||||
let worksheets = vec!["Sheet1".to_string()];
|
||||
let mut parser = Parser::new(worksheets, vec![], HashMap::new());
|
||||
let mut parser = new_parser(worksheets, vec![], HashMap::new());
|
||||
|
||||
// Reference cell is Sheet1!A1
|
||||
let cell_reference = CellReferenceRC {
|
||||
@@ -32,7 +32,7 @@ fn test_parser_reference() {
|
||||
#[test]
|
||||
fn test_parser_absolute_column() {
|
||||
let worksheets = vec!["Sheet1".to_string()];
|
||||
let mut parser = Parser::new(worksheets, vec![], HashMap::new());
|
||||
let mut parser = new_parser(worksheets, vec![], HashMap::new());
|
||||
|
||||
// Reference cell is Sheet1!A1
|
||||
let cell_reference = CellReferenceRC {
|
||||
@@ -47,7 +47,7 @@ fn test_parser_absolute_column() {
|
||||
#[test]
|
||||
fn test_parser_absolute_row_col() {
|
||||
let worksheets = vec!["Sheet1".to_string()];
|
||||
let mut parser = Parser::new(worksheets, vec![], HashMap::new());
|
||||
let mut parser = new_parser(worksheets, vec![], HashMap::new());
|
||||
|
||||
// Reference cell is Sheet1!A1
|
||||
let cell_reference = CellReferenceRC {
|
||||
@@ -62,7 +62,7 @@ fn test_parser_absolute_row_col() {
|
||||
#[test]
|
||||
fn test_parser_absolute_row_col_1() {
|
||||
let worksheets = vec!["Sheet1".to_string()];
|
||||
let mut parser = Parser::new(worksheets, vec![], HashMap::new());
|
||||
let mut parser = new_parser(worksheets, vec![], HashMap::new());
|
||||
|
||||
// Reference cell is Sheet1!A1
|
||||
let cell_reference = CellReferenceRC {
|
||||
@@ -77,7 +77,7 @@ fn test_parser_absolute_row_col_1() {
|
||||
#[test]
|
||||
fn test_parser_simple_formula() {
|
||||
let worksheets = vec!["Sheet1".to_string(), "Sheet2".to_string()];
|
||||
let mut parser = Parser::new(worksheets, vec![], HashMap::new());
|
||||
let mut parser = new_parser(worksheets, vec![], HashMap::new());
|
||||
|
||||
// Reference cell is Sheet1!A1
|
||||
let cell_reference = CellReferenceRC {
|
||||
@@ -93,7 +93,7 @@ fn test_parser_simple_formula() {
|
||||
#[test]
|
||||
fn test_parser_boolean() {
|
||||
let worksheets = vec!["Sheet1".to_string(), "Sheet2".to_string()];
|
||||
let mut parser = Parser::new(worksheets, vec![], HashMap::new());
|
||||
let mut parser = new_parser(worksheets, vec![], HashMap::new());
|
||||
|
||||
// Reference cell is Sheet1!A1
|
||||
let cell_reference = CellReferenceRC {
|
||||
@@ -109,7 +109,7 @@ fn test_parser_boolean() {
|
||||
#[test]
|
||||
fn test_parser_bad_formula() {
|
||||
let worksheets = vec!["Sheet1".to_string(), "Sheet2".to_string()];
|
||||
let mut parser = Parser::new(worksheets, vec![], HashMap::new());
|
||||
let mut parser = new_parser(worksheets, vec![], HashMap::new());
|
||||
|
||||
// Reference cell is Sheet1!A1
|
||||
let cell_reference = CellReferenceRC {
|
||||
@@ -138,7 +138,7 @@ fn test_parser_bad_formula() {
|
||||
#[test]
|
||||
fn test_parser_bad_formula_1() {
|
||||
let worksheets = vec!["Sheet1".to_string(), "Sheet2".to_string()];
|
||||
let mut parser = Parser::new(worksheets, vec![], HashMap::new());
|
||||
let mut parser = new_parser(worksheets, vec![], HashMap::new());
|
||||
|
||||
// Reference cell is Sheet1!A1
|
||||
let cell_reference = CellReferenceRC {
|
||||
@@ -167,7 +167,7 @@ fn test_parser_bad_formula_1() {
|
||||
#[test]
|
||||
fn test_parser_bad_formula_2() {
|
||||
let worksheets = vec!["Sheet1".to_string(), "Sheet2".to_string()];
|
||||
let mut parser = Parser::new(worksheets, vec![], HashMap::new());
|
||||
let mut parser = new_parser(worksheets, vec![], HashMap::new());
|
||||
|
||||
// Reference cell is Sheet1!A1
|
||||
let cell_reference = CellReferenceRC {
|
||||
@@ -196,7 +196,7 @@ fn test_parser_bad_formula_2() {
|
||||
#[test]
|
||||
fn test_parser_bad_formula_3() {
|
||||
let worksheets = vec!["Sheet1".to_string(), "Sheet2".to_string()];
|
||||
let mut parser = Parser::new(worksheets, vec![], HashMap::new());
|
||||
let mut parser = new_parser(worksheets, vec![], HashMap::new());
|
||||
|
||||
// Reference cell is Sheet1!A1
|
||||
let cell_reference = CellReferenceRC {
|
||||
@@ -225,7 +225,7 @@ fn test_parser_bad_formula_3() {
|
||||
#[test]
|
||||
fn test_parser_formulas() {
|
||||
let worksheets = vec!["Sheet1".to_string(), "Sheet2".to_string()];
|
||||
let mut parser = Parser::new(worksheets, vec![], HashMap::new());
|
||||
let mut parser = new_parser(worksheets, vec![], HashMap::new());
|
||||
|
||||
let formulas = vec![
|
||||
Formula {
|
||||
@@ -266,14 +266,17 @@ fn test_parser_formulas() {
|
||||
},
|
||||
);
|
||||
assert_eq!(to_rc_format(&t), formula.expected);
|
||||
assert_eq!(to_string(&t, &cell_reference), formula.initial);
|
||||
assert_eq!(
|
||||
to_english_localized_string(&t, &cell_reference),
|
||||
formula.initial
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parser_r1c1_formulas() {
|
||||
let worksheets = vec!["Sheet1".to_string(), "Sheet2".to_string()];
|
||||
let mut parser = Parser::new(worksheets, vec![], HashMap::new());
|
||||
let mut parser = new_parser(worksheets, vec![], HashMap::new());
|
||||
parser.set_lexer_mode(LexerMode::R1C1);
|
||||
|
||||
let formulas = vec![
|
||||
@@ -330,7 +333,10 @@ fn test_parser_r1c1_formulas() {
|
||||
column: 1,
|
||||
},
|
||||
);
|
||||
assert_eq!(to_string(&t, &cell_reference), formula.expected);
|
||||
assert_eq!(
|
||||
to_english_localized_string(&t, &cell_reference),
|
||||
formula.expected
|
||||
);
|
||||
assert_eq!(to_rc_format(&t), formula.initial);
|
||||
}
|
||||
}
|
||||
@@ -338,7 +344,7 @@ fn test_parser_r1c1_formulas() {
|
||||
#[test]
|
||||
fn test_parser_quotes() {
|
||||
let worksheets = vec!["Sheet1".to_string(), "Second Sheet".to_string()];
|
||||
let mut parser = Parser::new(worksheets, vec![], HashMap::new());
|
||||
let mut parser = new_parser(worksheets, vec![], HashMap::new());
|
||||
|
||||
// Reference cell is Sheet1!A1
|
||||
let cell_reference = CellReferenceRC {
|
||||
@@ -354,7 +360,7 @@ fn test_parser_quotes() {
|
||||
#[test]
|
||||
fn test_parser_escape_quotes() {
|
||||
let worksheets = vec!["Sheet1".to_string(), "Second '2' Sheet".to_string()];
|
||||
let mut parser = Parser::new(worksheets, vec![], HashMap::new());
|
||||
let mut parser = new_parser(worksheets, vec![], HashMap::new());
|
||||
|
||||
// Reference cell is Sheet1!A1
|
||||
let cell_reference = CellReferenceRC {
|
||||
@@ -370,7 +376,7 @@ fn test_parser_escape_quotes() {
|
||||
#[test]
|
||||
fn test_parser_parenthesis() {
|
||||
let worksheets = vec!["Sheet1".to_string(), "Second2".to_string()];
|
||||
let mut parser = Parser::new(worksheets, vec![], HashMap::new());
|
||||
let mut parser = new_parser(worksheets, vec![], HashMap::new());
|
||||
|
||||
// Reference cell is Sheet1!A1
|
||||
let cell_reference = CellReferenceRC {
|
||||
@@ -386,7 +392,7 @@ fn test_parser_parenthesis() {
|
||||
#[test]
|
||||
fn test_parser_excel_xlfn() {
|
||||
let worksheets = vec!["Sheet1".to_string(), "Second2".to_string()];
|
||||
let mut parser = Parser::new(worksheets, vec![], HashMap::new());
|
||||
let mut parser = new_parser(worksheets, vec![], HashMap::new());
|
||||
|
||||
// Reference cell is Sheet1!A1
|
||||
let cell_reference = CellReferenceRC {
|
||||
@@ -407,7 +413,7 @@ fn test_to_string_displaced() {
|
||||
column: 1,
|
||||
};
|
||||
let worksheets = vec!["Sheet1".to_string()];
|
||||
let mut parser = Parser::new(worksheets, vec![], HashMap::new());
|
||||
let mut parser = new_parser(worksheets, vec![], HashMap::new());
|
||||
|
||||
let node = parser.parse("C3", context);
|
||||
let displace_data = DisplaceData::Column {
|
||||
@@ -427,7 +433,7 @@ fn test_to_string_displaced_full_ranges() {
|
||||
column: 1,
|
||||
};
|
||||
let worksheets = vec!["Sheet1".to_string()];
|
||||
let mut parser = Parser::new(worksheets, vec![], HashMap::new());
|
||||
let mut parser = new_parser(worksheets, vec![], HashMap::new());
|
||||
|
||||
let node = parser.parse("SUM(3:3)", context);
|
||||
let displace_data = DisplaceData::Column {
|
||||
@@ -460,7 +466,7 @@ fn test_to_string_displaced_too_low() {
|
||||
column: 1,
|
||||
};
|
||||
let worksheets = vec!["Sheet1".to_string()];
|
||||
let mut parser = Parser::new(worksheets, vec![], HashMap::new());
|
||||
let mut parser = new_parser(worksheets, vec![], HashMap::new());
|
||||
|
||||
let node = parser.parse("C3", context);
|
||||
let displace_data = DisplaceData::Column {
|
||||
@@ -480,7 +486,7 @@ fn test_to_string_displaced_too_high() {
|
||||
column: 1,
|
||||
};
|
||||
let worksheets = vec!["Sheet1".to_string()];
|
||||
let mut parser = Parser::new(worksheets, vec![], HashMap::new());
|
||||
let mut parser = new_parser(worksheets, vec![], HashMap::new());
|
||||
|
||||
let node = parser.parse("C3", context);
|
||||
let displace_data = DisplaceData::Column {
|
||||
|
||||
@@ -1,13 +1,15 @@
|
||||
#![allow(clippy::panic)]
|
||||
|
||||
use crate::expressions::parser::{Node, Parser};
|
||||
use crate::expressions::parser::Node;
|
||||
use crate::expressions::types::CellReferenceRC;
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::expressions::parser::tests::utils::new_parser;
|
||||
|
||||
#[test]
|
||||
fn simple() {
|
||||
let worksheets = vec!["Sheet1".to_string()];
|
||||
let mut parser = Parser::new(worksheets, vec![], HashMap::new());
|
||||
let mut parser = new_parser(worksheets, vec![], HashMap::new());
|
||||
|
||||
// Reference cell is Sheet1!B3
|
||||
let cell_reference = CellReferenceRC {
|
||||
@@ -40,7 +42,7 @@ fn simple() {
|
||||
#[test]
|
||||
fn simple_add() {
|
||||
let worksheets = vec!["Sheet1".to_string()];
|
||||
let mut parser = Parser::new(worksheets, vec![], HashMap::new());
|
||||
let mut parser = new_parser(worksheets, vec![], HashMap::new());
|
||||
|
||||
// Reference cell is Sheet1!B3
|
||||
let cell_reference = CellReferenceRC {
|
||||
|
||||
@@ -2,14 +2,13 @@
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::expressions::parser::stringify::to_string;
|
||||
use crate::expressions::parser::Parser;
|
||||
use crate::expressions::parser::tests::utils::{new_parser, to_english_localized_string};
|
||||
use crate::expressions::types::CellReferenceRC;
|
||||
|
||||
#[test]
|
||||
fn issue_155_parser() {
|
||||
let worksheets = vec!["Sheet1".to_string()];
|
||||
let mut parser = Parser::new(worksheets, vec![], HashMap::new());
|
||||
let mut parser = new_parser(worksheets, vec![], HashMap::new());
|
||||
|
||||
// Reference cell is Sheet1!A1
|
||||
let cell_reference = CellReferenceRC {
|
||||
@@ -18,13 +17,13 @@ fn issue_155_parser() {
|
||||
column: 2,
|
||||
};
|
||||
let t = parser.parse("A$1:A2", &cell_reference);
|
||||
assert_eq!(to_string(&t, &cell_reference), "A$1:A2");
|
||||
assert_eq!(to_english_localized_string(&t, &cell_reference), "A$1:A2");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn issue_155_parser_case_2() {
|
||||
let worksheets = vec!["Sheet1".to_string()];
|
||||
let mut parser = Parser::new(worksheets, vec![], HashMap::new());
|
||||
let mut parser = new_parser(worksheets, vec![], HashMap::new());
|
||||
|
||||
// Reference cell is Sheet1!A1
|
||||
let cell_reference = CellReferenceRC {
|
||||
@@ -33,13 +32,13 @@ fn issue_155_parser_case_2() {
|
||||
column: 20,
|
||||
};
|
||||
let t = parser.parse("C$1:D2", &cell_reference);
|
||||
assert_eq!(to_string(&t, &cell_reference), "C$1:D2");
|
||||
assert_eq!(to_english_localized_string(&t, &cell_reference), "C$1:D2");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn issue_155_parser_only_row() {
|
||||
let worksheets = vec!["Sheet1".to_string()];
|
||||
let mut parser = Parser::new(worksheets, vec![], HashMap::new());
|
||||
let mut parser = new_parser(worksheets, vec![], HashMap::new());
|
||||
|
||||
// Reference cell is Sheet1!A1
|
||||
let cell_reference = CellReferenceRC {
|
||||
@@ -49,13 +48,13 @@ fn issue_155_parser_only_row() {
|
||||
};
|
||||
// This is tricky, I am not sure what to do in these cases
|
||||
let t = parser.parse("A$2:B1", &cell_reference);
|
||||
assert_eq!(to_string(&t, &cell_reference), "A1:B$2");
|
||||
assert_eq!(to_english_localized_string(&t, &cell_reference), "A1:B$2");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn issue_155_parser_only_column() {
|
||||
let worksheets = vec!["Sheet1".to_string()];
|
||||
let mut parser = Parser::new(worksheets, vec![], HashMap::new());
|
||||
let mut parser = new_parser(worksheets, vec![], HashMap::new());
|
||||
|
||||
// Reference cell is Sheet1!A1
|
||||
let cell_reference = CellReferenceRC {
|
||||
@@ -65,5 +64,5 @@ fn issue_155_parser_only_column() {
|
||||
};
|
||||
// This is tricky, I am not sure what to do in these cases
|
||||
let t = parser.parse("D1:$A3", &cell_reference);
|
||||
assert_eq!(to_string(&t, &cell_reference), "$A1:D3");
|
||||
assert_eq!(to_english_localized_string(&t, &cell_reference), "$A1:D3");
|
||||
}
|
||||
|
||||
@@ -2,14 +2,14 @@
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::expressions::parser::stringify::to_string;
|
||||
use crate::expressions::parser::{Node, Parser};
|
||||
use crate::expressions::parser::tests::utils::{new_parser, to_english_localized_string};
|
||||
use crate::expressions::parser::Node;
|
||||
use crate::expressions::types::CellReferenceRC;
|
||||
|
||||
#[test]
|
||||
fn issue_483_parser() {
|
||||
let worksheets = vec!["Sheet1".to_string()];
|
||||
let mut parser = Parser::new(worksheets, vec![], HashMap::new());
|
||||
let mut parser = new_parser(worksheets, vec![], HashMap::new());
|
||||
|
||||
// Reference cell is Sheet1!A1
|
||||
let cell_reference = CellReferenceRC {
|
||||
@@ -19,9 +19,12 @@ fn issue_483_parser() {
|
||||
};
|
||||
let t = parser.parse("-(A1^1.22)", &cell_reference);
|
||||
assert!(matches!(t, Node::UnaryKind { .. }));
|
||||
assert_eq!(to_string(&t, &cell_reference), "-(A1^1.22)");
|
||||
assert_eq!(
|
||||
to_english_localized_string(&t, &cell_reference),
|
||||
"-(A1^1.22)"
|
||||
);
|
||||
|
||||
let t = parser.parse("-A1^1.22", &cell_reference);
|
||||
assert!(matches!(t, Node::OpPowerKind { .. }));
|
||||
assert_eq!(to_string(&t, &cell_reference), "-A1^1.22");
|
||||
assert_eq!(to_english_localized_string(&t, &cell_reference), "-A1^1.22");
|
||||
}
|
||||
|
||||
56
base/src/expressions/parser/tests/test_languages.rs
Normal file
56
base/src/expressions/parser/tests/test_languages.rs
Normal file
@@ -0,0 +1,56 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::expressions::parser::{DefinedNameS, Node, Parser};
|
||||
use crate::expressions::types::CellReferenceRC;
|
||||
|
||||
use crate::expressions::parser::stringify::to_localized_string;
|
||||
use crate::functions::Function;
|
||||
use crate::language::get_language;
|
||||
use crate::locale::get_locale;
|
||||
use crate::types::Table;
|
||||
|
||||
pub fn to_string(t: &Node, cell_reference: &CellReferenceRC) -> String {
|
||||
let locale = get_locale("en").unwrap();
|
||||
let language = get_language("es").unwrap();
|
||||
to_localized_string(t, cell_reference, locale, language)
|
||||
}
|
||||
|
||||
pub fn new_parser(
|
||||
worksheets: Vec<String>,
|
||||
defined_names: Vec<DefinedNameS>,
|
||||
tables: HashMap<String, Table>,
|
||||
) -> Parser {
|
||||
let locale = get_locale("en").unwrap();
|
||||
let language = get_language("es").unwrap();
|
||||
Parser::new(worksheets, defined_names, tables, locale, language)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn simple_language() {
|
||||
let worksheets = vec!["Sheet1".to_string(), "Second Sheet".to_string()];
|
||||
let mut parser = new_parser(worksheets, vec![], HashMap::new());
|
||||
// Reference cell is Sheet1!A1
|
||||
let cell_reference = CellReferenceRC {
|
||||
sheet: "Sheet1".to_string(),
|
||||
row: 1,
|
||||
column: 1,
|
||||
};
|
||||
let t = parser.parse("FALSO", &cell_reference);
|
||||
assert!(matches!(t, Node::BooleanKind(false)));
|
||||
|
||||
let t = parser.parse("VERDADERO", &cell_reference);
|
||||
assert!(matches!(t, Node::BooleanKind(true)));
|
||||
|
||||
let t = parser.parse("TRUE()", &cell_reference);
|
||||
assert!(matches!(t, Node::InvalidFunctionKind { ref name, args: _} if name == "TRUE"));
|
||||
|
||||
let t = parser.parse("VERDADERO()", &cell_reference);
|
||||
assert!(matches!(
|
||||
t,
|
||||
Node::FunctionKind {
|
||||
kind: Function::True,
|
||||
args: _
|
||||
}
|
||||
));
|
||||
assert_eq!(to_string(&t, &cell_reference), "VERDADERO()".to_string());
|
||||
}
|
||||
1
base/src/expressions/parser/tests/test_locales.rs
Normal file
1
base/src/expressions/parser/tests/test_locales.rs
Normal file
@@ -0,0 +1 @@
|
||||
|
||||
@@ -1,8 +1,17 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::expressions::parser::move_formula::{move_formula, MoveContext};
|
||||
use crate::expressions::parser::Parser;
|
||||
use crate::expressions::parser::move_formula::{move_formula as mf, MoveContext};
|
||||
use crate::expressions::parser::tests::utils::new_parser;
|
||||
use crate::expressions::parser::Node;
|
||||
use crate::expressions::types::{Area, CellReferenceRC};
|
||||
use crate::language::Language;
|
||||
use crate::locale::Locale;
|
||||
|
||||
fn move_formula(node: &Node, context: &MoveContext) -> String {
|
||||
let locale = Locale::default();
|
||||
let language = Language::default();
|
||||
mf(node, context, &locale, &language)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_move_formula() {
|
||||
@@ -15,7 +24,7 @@ fn test_move_formula() {
|
||||
column,
|
||||
};
|
||||
let worksheets = vec!["Sheet1".to_string()];
|
||||
let mut parser = Parser::new(worksheets, vec![], HashMap::new());
|
||||
let mut parser = new_parser(worksheets, vec![], HashMap::new());
|
||||
|
||||
// Area is C2:F6
|
||||
let area = &Area {
|
||||
@@ -102,7 +111,7 @@ fn test_move_formula_context_offset() {
|
||||
column,
|
||||
};
|
||||
let worksheets = vec!["Sheet1".to_string()];
|
||||
let mut parser = Parser::new(worksheets, vec![], HashMap::new());
|
||||
let mut parser = new_parser(worksheets, vec![], HashMap::new());
|
||||
|
||||
// Area is C2:F6
|
||||
let area = &Area {
|
||||
@@ -140,7 +149,7 @@ fn test_move_formula_area_limits() {
|
||||
column,
|
||||
};
|
||||
let worksheets = vec!["Sheet1".to_string()];
|
||||
let mut parser = Parser::new(worksheets, vec![], HashMap::new());
|
||||
let mut parser = new_parser(worksheets, vec![], HashMap::new());
|
||||
|
||||
// Area is C2:F6
|
||||
let area = &Area {
|
||||
@@ -195,7 +204,7 @@ fn test_move_formula_ranges() {
|
||||
column,
|
||||
};
|
||||
let worksheets = vec!["Sheet1".to_string()];
|
||||
let mut parser = Parser::new(worksheets, vec![], HashMap::new());
|
||||
let mut parser = new_parser(worksheets, vec![], HashMap::new());
|
||||
|
||||
let area = &Area {
|
||||
sheet: 0,
|
||||
@@ -318,7 +327,7 @@ fn test_move_formula_wrong_reference() {
|
||||
height: 5,
|
||||
};
|
||||
let worksheets = vec!["Sheet1".to_string()];
|
||||
let mut parser = Parser::new(worksheets, vec![], HashMap::new());
|
||||
let mut parser = new_parser(worksheets, vec![], HashMap::new());
|
||||
|
||||
// Wrong formulas will NOT be displaced
|
||||
let node = parser.parse("Sheet3!AB31", context);
|
||||
@@ -377,7 +386,7 @@ fn test_move_formula_misc() {
|
||||
column,
|
||||
};
|
||||
let worksheets = vec!["Sheet1".to_string()];
|
||||
let mut parser = Parser::new(worksheets, vec![], HashMap::new());
|
||||
let mut parser = new_parser(worksheets, vec![], HashMap::new());
|
||||
|
||||
// Area is C2:F6
|
||||
let area = &Area {
|
||||
@@ -445,7 +454,7 @@ fn test_move_formula_another_sheet() {
|
||||
};
|
||||
// we add two sheets and we cut/paste from Sheet1 to Sheet2
|
||||
let worksheets = vec!["Sheet1".to_string(), "Sheet2".to_string()];
|
||||
let mut parser = Parser::new(worksheets, vec![], HashMap::new());
|
||||
let mut parser = new_parser(worksheets, vec![], HashMap::new());
|
||||
|
||||
// Area is C2:F6
|
||||
let area = &Area {
|
||||
@@ -487,7 +496,7 @@ fn move_formula_implicit_intersetion() {
|
||||
column,
|
||||
};
|
||||
let worksheets = vec!["Sheet1".to_string()];
|
||||
let mut parser = Parser::new(worksheets, vec![], HashMap::new());
|
||||
let mut parser = new_parser(worksheets, vec![], HashMap::new());
|
||||
|
||||
// Area is C2:F6
|
||||
let area = &Area {
|
||||
@@ -524,7 +533,7 @@ fn move_formula_implicit_intersetion_with_ranges() {
|
||||
column,
|
||||
};
|
||||
let worksheets = vec!["Sheet1".to_string()];
|
||||
let mut parser = Parser::new(worksheets, vec![], HashMap::new());
|
||||
let mut parser = new_parser(worksheets, vec![], HashMap::new());
|
||||
|
||||
// Area is C2:F6
|
||||
let area = &Area {
|
||||
|
||||
@@ -2,8 +2,8 @@ use std::collections::HashMap;
|
||||
|
||||
use crate::expressions::lexer::LexerMode;
|
||||
|
||||
use crate::expressions::parser::stringify::{to_rc_format, to_string};
|
||||
use crate::expressions::parser::Parser;
|
||||
use crate::expressions::parser::stringify::to_rc_format;
|
||||
use crate::expressions::parser::tests::utils::{new_parser, to_english_localized_string};
|
||||
use crate::expressions::types::CellReferenceRC;
|
||||
|
||||
struct Formula<'a> {
|
||||
@@ -14,7 +14,7 @@ struct Formula<'a> {
|
||||
#[test]
|
||||
fn test_parser_formulas_with_full_ranges() {
|
||||
let worksheets = vec!["Sheet1".to_string(), "Second Sheet".to_string()];
|
||||
let mut parser = Parser::new(worksheets, vec![], HashMap::new());
|
||||
let mut parser = new_parser(worksheets, vec![], HashMap::new());
|
||||
|
||||
let formulas = vec![
|
||||
Formula {
|
||||
@@ -59,7 +59,10 @@ fn test_parser_formulas_with_full_ranges() {
|
||||
},
|
||||
);
|
||||
assert_eq!(to_rc_format(&t), formula.formula_r1c1);
|
||||
assert_eq!(to_string(&t, &cell_reference), formula.formula_a1);
|
||||
assert_eq!(
|
||||
to_english_localized_string(&t, &cell_reference),
|
||||
formula.formula_a1
|
||||
);
|
||||
}
|
||||
|
||||
// Now the inverse
|
||||
@@ -74,14 +77,17 @@ fn test_parser_formulas_with_full_ranges() {
|
||||
},
|
||||
);
|
||||
assert_eq!(to_rc_format(&t), formula.formula_r1c1);
|
||||
assert_eq!(to_string(&t, &cell_reference), formula.formula_a1);
|
||||
assert_eq!(
|
||||
to_english_localized_string(&t, &cell_reference),
|
||||
formula.formula_a1
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_range_inverse_order() {
|
||||
let worksheets = vec!["Sheet1".to_string(), "Sheet2".to_string()];
|
||||
let mut parser = Parser::new(worksheets, vec![], HashMap::new());
|
||||
let mut parser = new_parser(worksheets, vec![], HashMap::new());
|
||||
|
||||
// Reference cell is Sheet1!A1
|
||||
let cell_reference = CellReferenceRC {
|
||||
@@ -96,7 +102,7 @@ fn test_range_inverse_order() {
|
||||
&cell_reference,
|
||||
);
|
||||
assert_eq!(
|
||||
to_string(&t, &cell_reference),
|
||||
to_english_localized_string(&t, &cell_reference),
|
||||
"SUM(C2:D4)*SUM(Sheet2!C4:D20)*SUM($C4:D$20)".to_string()
|
||||
);
|
||||
}
|
||||
|
||||
@@ -2,14 +2,13 @@
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::expressions::parser::stringify::to_string;
|
||||
use crate::expressions::parser::Parser;
|
||||
use crate::expressions::parser::tests::utils::{new_parser, to_english_localized_string};
|
||||
use crate::expressions::types::CellReferenceRC;
|
||||
|
||||
#[test]
|
||||
fn exp_order() {
|
||||
let worksheets = vec!["Sheet1".to_string()];
|
||||
let mut parser = Parser::new(worksheets, vec![], HashMap::new());
|
||||
let mut parser = new_parser(worksheets, vec![], HashMap::new());
|
||||
|
||||
// Reference cell is Sheet1!A1
|
||||
let cell_reference = CellReferenceRC {
|
||||
@@ -18,25 +17,34 @@ fn exp_order() {
|
||||
column: 1,
|
||||
};
|
||||
let t = parser.parse("(1 + 2)^3 + 4", &cell_reference);
|
||||
assert_eq!(to_string(&t, &cell_reference), "(1+2)^3+4");
|
||||
assert_eq!(
|
||||
to_english_localized_string(&t, &cell_reference),
|
||||
"(1+2)^3+4"
|
||||
);
|
||||
|
||||
let t = parser.parse("(C5 + 3)^R4", &cell_reference);
|
||||
assert_eq!(to_string(&t, &cell_reference), "(C5+3)^R4");
|
||||
assert_eq!(
|
||||
to_english_localized_string(&t, &cell_reference),
|
||||
"(C5+3)^R4"
|
||||
);
|
||||
|
||||
let t = parser.parse("(C5 + 3)^(R4*6)", &cell_reference);
|
||||
assert_eq!(to_string(&t, &cell_reference), "(C5+3)^(R4*6)");
|
||||
assert_eq!(
|
||||
to_english_localized_string(&t, &cell_reference),
|
||||
"(C5+3)^(R4*6)"
|
||||
);
|
||||
|
||||
let t = parser.parse("(C5)^(R4)", &cell_reference);
|
||||
assert_eq!(to_string(&t, &cell_reference), "C5^R4");
|
||||
assert_eq!(to_english_localized_string(&t, &cell_reference), "C5^R4");
|
||||
|
||||
let t = parser.parse("(5)^(4)", &cell_reference);
|
||||
assert_eq!(to_string(&t, &cell_reference), "5^4");
|
||||
assert_eq!(to_english_localized_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 mut parser = new_parser(worksheets, vec![], HashMap::new());
|
||||
|
||||
let cell_reference = CellReferenceRC {
|
||||
sheet: "Sheet1".to_string(),
|
||||
@@ -45,26 +53,29 @@ fn correct_parenthesis() {
|
||||
};
|
||||
|
||||
let t = parser.parse("-(1 + 1)", &cell_reference);
|
||||
assert_eq!(to_string(&t, &cell_reference), "-(1+1)");
|
||||
assert_eq!(to_english_localized_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)");
|
||||
assert_eq!(to_english_localized_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),
|
||||
to_english_localized_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");
|
||||
assert_eq!(to_english_localized_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)");
|
||||
assert_eq!(to_english_localized_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)");
|
||||
assert_eq!(
|
||||
to_english_localized_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)");
|
||||
assert_eq!(to_english_localized_string(&t, &cell_reference), "1+(3<5)");
|
||||
}
|
||||
|
||||
@@ -2,8 +2,7 @@
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::expressions::parser::stringify::to_string;
|
||||
use crate::expressions::parser::Parser;
|
||||
use crate::expressions::parser::tests::utils::{new_parser, to_english_localized_string};
|
||||
use crate::expressions::types::CellReferenceRC;
|
||||
use crate::expressions::utils::{number_to_column, parse_reference_a1};
|
||||
use crate::types::{Table, TableColumn, TableStyleInfo};
|
||||
@@ -62,7 +61,7 @@ fn simple_table() {
|
||||
let row_count = 3;
|
||||
let tables = create_test_table("tblIncome", &column_names, "A1", row_count);
|
||||
|
||||
let mut parser = Parser::new(worksheets, vec![], tables);
|
||||
let mut parser = new_parser(worksheets, vec![], tables);
|
||||
// Reference cell is 'Sheet One'!F2
|
||||
let cell_reference = CellReferenceRC {
|
||||
sheet: "Sheet One".to_string(),
|
||||
@@ -72,7 +71,10 @@ fn simple_table() {
|
||||
|
||||
let formula = "SUM(tblIncome[[#This Row],[Jan]:[Dec]])";
|
||||
let t = parser.parse(formula, &cell_reference);
|
||||
assert_eq!(to_string(&t, &cell_reference), "SUM($A$2:$E$2)");
|
||||
assert_eq!(
|
||||
to_english_localized_string(&t, &cell_reference),
|
||||
"SUM($A$2:$E$2)"
|
||||
);
|
||||
|
||||
// Cell A3
|
||||
let cell_reference = CellReferenceRC {
|
||||
@@ -82,7 +84,10 @@ fn simple_table() {
|
||||
};
|
||||
let formula = "SUBTOTAL(109, tblIncome[Jan])";
|
||||
let t = parser.parse(formula, &cell_reference);
|
||||
assert_eq!(to_string(&t, &cell_reference), "SUBTOTAL(109,$A$2:$A$3)");
|
||||
assert_eq!(
|
||||
to_english_localized_string(&t, &cell_reference),
|
||||
"SUBTOTAL(109,$A$2:$A$3)"
|
||||
);
|
||||
|
||||
// Cell A3 in 'Second Sheet'
|
||||
let cell_reference = CellReferenceRC {
|
||||
@@ -93,7 +98,7 @@ fn simple_table() {
|
||||
let formula = "SUBTOTAL(109, tblIncome[Jan])";
|
||||
let t = parser.parse(formula, &cell_reference);
|
||||
assert_eq!(
|
||||
to_string(&t, &cell_reference),
|
||||
to_english_localized_string(&t, &cell_reference),
|
||||
"SUBTOTAL(109,'Sheet One'!$A$2:$A$3)"
|
||||
);
|
||||
}
|
||||
|
||||
29
base/src/expressions/parser/tests/utils.rs
Normal file
29
base/src/expressions/parser/tests/utils.rs
Normal file
@@ -0,0 +1,29 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::{
|
||||
expressions::{
|
||||
parser::{DefinedNameS, Node, Parser},
|
||||
types::CellReferenceRC,
|
||||
},
|
||||
language::Language,
|
||||
locale::Locale,
|
||||
types::Table,
|
||||
};
|
||||
|
||||
use crate::expressions::parser::stringify::to_localized_string;
|
||||
|
||||
pub fn to_english_localized_string(t: &Node, cell_reference: &CellReferenceRC) -> String {
|
||||
let locale = Locale::default();
|
||||
let language = Language::default();
|
||||
to_localized_string(t, cell_reference, &locale, &language)
|
||||
}
|
||||
|
||||
pub fn new_parser(
|
||||
worksheets: Vec<String>,
|
||||
defined_names: Vec<DefinedNameS>,
|
||||
tables: HashMap<String, Table>,
|
||||
) -> Parser {
|
||||
let locale = Locale::default();
|
||||
let language = Language::default();
|
||||
Parser::new(worksheets, defined_names, tables, &locale, &language)
|
||||
}
|
||||
@@ -241,6 +241,7 @@ pub enum TokenType {
|
||||
Percent, // %
|
||||
And, // &
|
||||
At, // @
|
||||
Backslash, // \
|
||||
Reference {
|
||||
sheet: Option<String>,
|
||||
row: i32,
|
||||
|
||||
Reference in New Issue
Block a user