use crate::core::types::{AstNode, BinaryOp, CalcError, Span, UnaryOp};
use regex::Regex;
const MAX_AST_DEPTH: usize = 256;
pub(crate) const MAX_EXPR_LEN: usize = 4096;
pub fn parse(input: &str) -> Result<AstNode, CalcError> {
if input.len() > MAX_EXPR_LEN {
let char_count = input.chars().count();
return Err(CalcError::parse(format!(
"expression length {} exceeds maximum of {} characters",
char_count, MAX_EXPR_LEN
))
.with_span(Span::new(0, char_count))
.with_i18n(
"msg.core.parse_length_exceeds",
vec![
("len".to_string(), char_count.to_string()),
("max".to_string(), MAX_EXPR_LEN.to_string()),
],
));
}
let trimmed = input.trim();
if trimmed.is_empty() {
return Err(CalcError::parse("expression is empty".to_string())
.with_span(Span::new(0, 0))
.with_i18n("msg.core.parse_empty", vec![]));
}
let (without_strings, placeholders) = preprocess_strings(trimmed)?;
let (without_brackets, mut placeholders) = preprocess_brackets(&without_strings, placeholders)?;
if placeholders.len() == 1 {
let name = placeholders.keys().next().unwrap().clone();
if without_brackets == name {
return Ok(placeholders.remove(&name).unwrap());
}
}
let without_bigint = preprocess_bigint(&without_brackets, &mut placeholders)?;
validate_no_consecutive_operators(&without_bigint)?;
let after_complex = preprocess_complex(&without_bigint)?;
let after_factorial = preprocess_factorial(&after_complex)?;
let after_implicit = insert_implicit_multiplication(&after_factorial);
let expr = mathexpr::parse(&after_implicit).map_err(|e| {
CalcError::parse(format!("{}", e))
.with_span(Span::new(0, trimmed.chars().count()))
.with_i18n(
"msg.core.parse_mathexpr_error",
vec![("error".to_string(), e.to_string())],
)
})?;
let mut ast = convert_with_depth(&expr, 1)?;
replace_placeholders(&mut ast, &placeholders);
Ok(ast)
}
fn preprocess_complex(input: &str) -> Result<String, CalcError> {
use std::sync::OnceLock;
static RE_COMPLEX_FULL: OnceLock<Regex> = OnceLock::new();
static RE_PURE_IMAGINARY: OnceLock<Regex> = OnceLock::new();
let re_full = RE_COMPLEX_FULL
.get_or_init(|| Regex::new(r"(\d+(?:\.\d+)?)\s*([+-])\s*(\d+(?:\.\d+)?)\s*i").unwrap());
let re_pure = RE_PURE_IMAGINARY.get_or_init(|| Regex::new(r"(\d+(?:\.\d+)?)\s*i").unwrap());
let mut result = input.to_string();
result = re_full
.replace_all(&result, |caps: ®ex::Captures| {
let re = caps.get(1).unwrap().as_str();
let sign = caps.get(2).unwrap().as_str();
let im = caps.get(3).unwrap().as_str();
format!("complex({re},{sign}{im})")
})
.to_string();
result = re_pure
.replace_all(&result, |caps: ®ex::Captures| {
let im = caps.get(1).unwrap().as_str();
format!("complex(0,{})", im)
})
.to_string();
Ok(result)
}
fn parse_bracket_literal(input: &str) -> Result<AstNode, CalcError> {
let trimmed = input.trim();
if trimmed.starts_with("[[") {
parse_matrix_literal(trimmed)
} else if trimmed.starts_with('[') {
parse_list_literal(trimmed)
} else {
Err(
CalcError::parse(format!("expected bracket literal, got: {}", trimmed))
.with_span(Span::new(0, trimmed.chars().count()))
.with_i18n(
"msg.core.parse_expected_bracket",
vec![("token".to_string(), trimmed.to_string())],
),
)
}
}
fn preprocess_strings(
input: &str,
) -> Result<(String, std::collections::HashMap<String, AstNode>), CalcError> {
let mut result = String::with_capacity(input.len());
let mut placeholders: std::collections::HashMap<String, AstNode> =
std::collections::HashMap::new();
let chars: Vec<char> = input.chars().collect();
let mut i = 0;
let mut count = 0;
while i < chars.len() {
if chars[i] == '"' {
let start = i; i += 1; let mut content = String::new();
let mut closed = false;
while i < chars.len() {
if chars[i] == '\\' {
if i + 1 < chars.len() && chars[i + 1] == '"' {
content.push('"');
i += 2;
} else if i + 1 < chars.len() && chars[i + 1] == '\\' {
content.push('\\');
i += 2;
} else {
content.push('\\');
i += 1;
}
} else if chars[i] == '"' {
i += 1;
closed = true;
break;
} else {
content.push(chars[i]);
i += 1;
}
}
if !closed {
return Err(CalcError::parse("unclosed string literal".to_string())
.with_span(Span::new(start, i))
.with_i18n("msg.core.parse_unclosed_string", vec![]));
}
let placeholder = format!("__str_{}", count);
count += 1;
placeholders.insert(placeholder.clone(), AstNode::Str(content));
result.push_str(&placeholder);
} else {
result.push(chars[i]);
i += 1;
}
}
Ok((result, placeholders))
}
fn preprocess_brackets(
input: &str,
mut placeholders: std::collections::HashMap<String, AstNode>,
) -> Result<(String, std::collections::HashMap<String, AstNode>), CalcError> {
let mut result = String::with_capacity(input.len());
let chars: Vec<char> = input.chars().collect();
let mut i = 0;
let mut count = 0;
while i < chars.len() {
if chars[i] == '[' {
let start = i;
let mut depth = 1;
i += 1;
while i < chars.len() && depth > 0 {
if chars[i] == '[' {
depth += 1;
} else if chars[i] == ']' {
depth -= 1;
}
i += 1;
}
if depth != 0 {
return Err(CalcError::parse("unmatched '[' in expression".to_string())
.with_span(Span::new(start, i))
.with_i18n("msg.core.parse_unmatched_bracket", vec![]));
}
let literal: String = chars[start..i].iter().collect();
let node = parse_bracket_literal(&literal)?;
let placeholder = format!("__cb_{}", count);
count += 1;
placeholders.insert(placeholder.clone(), node);
result.push_str(&placeholder);
} else {
result.push(chars[i]);
i += 1;
}
}
Ok((result, placeholders))
}
fn preprocess_bigint(
input: &str,
placeholders: &mut std::collections::HashMap<String, AstNode>,
) -> Result<String, CalcError> {
let mut result = String::with_capacity(input.len());
let chars: Vec<char> = input.chars().collect();
let mut i = 0;
let mut count = 0;
while i < chars.len() {
if chars[i].is_ascii_digit() {
let start = i;
while i < chars.len() && chars[i].is_ascii_digit() {
i += 1;
}
let digit_count = i - start;
let prev_char = if start > 0 {
Some(chars[start - 1])
} else {
None
};
let next_char = if i < chars.len() {
Some(chars[i])
} else {
None
};
let is_decimal = prev_char == Some('.') || next_char == Some('.');
if digit_count >= 16 && !is_decimal {
let digits: String = chars[start..i].iter().collect();
let placeholder = format!("__bn_{}", count);
count += 1;
placeholders.insert(placeholder.clone(), AstNode::BigNumber(digits));
result.push_str(&placeholder);
} else {
for c in &chars[start..i] {
result.push(*c);
}
}
} else {
result.push(chars[i]);
i += 1;
}
}
Ok(result)
}
fn replace_placeholders(
ast: &mut AstNode,
placeholders: &std::collections::HashMap<String, AstNode>,
) {
match ast {
AstNode::Variable(name) => {
if let Some(node) = placeholders.get(name) {
*ast = node.clone();
}
}
AstNode::BinaryOp(_, l, r) => {
replace_placeholders(l, placeholders);
replace_placeholders(r, placeholders);
}
AstNode::UnaryOp(_, e) => {
replace_placeholders(e, placeholders);
}
AstNode::FunctionCall(_, args) => {
for arg in args {
replace_placeholders(arg, placeholders);
}
}
AstNode::Matrix(rows) => {
for row in rows {
for elem in row {
replace_placeholders(elem, placeholders);
}
}
}
AstNode::List(elements) => {
for elem in elements {
replace_placeholders(elem, placeholders);
}
}
AstNode::Number(_) | AstNode::Complex(_, _) | AstNode::BigNumber(_) | AstNode::Str(_) => {}
}
}
fn parse_matrix_literal(input: &str) -> Result<AstNode, CalcError> {
let trimmed = input.trim();
if !trimmed.starts_with("[[") || !trimmed.ends_with("]]") {
return Err(
CalcError::parse(format!("invalid matrix literal: {}", trimmed))
.with_span(Span::new(0, trimmed.chars().count()))
.with_i18n(
"msg.core.parse_invalid_matrix_literal",
vec![("detail".to_string(), trimmed.to_string())],
),
);
}
let inner = &trimmed[2..trimmed.len() - 2];
let rows_str = split_by_pattern(inner, "],[");
let mut rows: Vec<Vec<AstNode>> = Vec::with_capacity(rows_str.len());
for row_str in &rows_str {
let row_full = format!("[{}]", row_str.trim());
let row_node = parse_list_literal(&row_full)?;
match row_node {
AstNode::List(elements) => rows.push(elements),
_ => {
let node_debug: String = format!("{:?}", row_node).chars().take(100).collect();
return Err(CalcError::parse(format!(
"expected list row in matrix, got: {}",
node_debug
))
.with_span(Span::new(0, trimmed.chars().count()))
.with_i18n(
"msg.core.parse_expected_list_row",
vec![("node".to_string(), node_debug)],
));
}
}
}
if rows.is_empty() {
return Err(CalcError::parse("empty matrix literal".to_string())
.with_span(Span::new(0, trimmed.chars().count()))
.with_i18n("msg.core.parse_empty_matrix_literal", vec![]));
}
Ok(AstNode::Matrix(rows))
}
fn parse_list_literal(input: &str) -> Result<AstNode, CalcError> {
let trimmed = input.trim();
if !trimmed.starts_with('[') || !trimmed.ends_with(']') {
return Err(
CalcError::parse(format!("invalid list literal: {}", trimmed))
.with_span(Span::new(0, trimmed.chars().count()))
.with_i18n(
"msg.core.parse_invalid_list_literal",
vec![("detail".to_string(), trimmed.to_string())],
),
);
}
let inner = &trimmed[1..trimmed.len() - 1];
let inner_trimmed = inner.trim();
if inner_trimmed.is_empty() {
return Ok(AstNode::List(vec![]));
}
let parts = split_top_level_commas(inner_trimmed);
let mut elements: Vec<AstNode> = Vec::with_capacity(parts.len());
for part in &parts {
elements.push(parse(part)?);
}
Ok(AstNode::List(elements))
}
fn split_by_pattern(input: &str, sep: &str) -> Vec<String> {
let mut result = Vec::new();
let mut current = String::new();
let chars: Vec<char> = input.chars().collect();
let sep_chars: Vec<char> = sep.chars().collect();
let mut i = 0;
let mut bracket_depth = 0i32;
let mut paren_depth = 0i32;
while i < chars.len() {
if bracket_depth == 0 && paren_depth == 0 && i + sep_chars.len() <= chars.len() {
let candidate: String = chars[i..i + sep_chars.len()].iter().collect();
if candidate == sep {
result.push(current.clone());
current.clear();
i += sep_chars.len();
continue;
}
}
let c = chars[i];
if c == '[' {
bracket_depth += 1;
} else if c == ']' {
bracket_depth -= 1;
} else if c == '(' {
paren_depth += 1;
} else if c == ')' {
paren_depth -= 1;
}
current.push(c);
i += 1;
}
result.push(current);
result
}
fn split_top_level_commas(input: &str) -> Vec<String> {
split_by_pattern(input, ",")
}
fn validate_no_consecutive_operators(input: &str) -> Result<(), CalcError> {
const ILLEGAL_CONSECUTIVE_OPS: &[&str] = &["++", "**", "//", "^^"];
let chars: Vec<char> = input.chars().collect();
let mut i = 0;
while i < chars.len() {
for &op in ILLEGAL_CONSECUTIVE_OPS {
let op_chars: Vec<char> = op.chars().collect();
let mut j = i + 1;
while j < chars.len() && chars[j].is_whitespace() {
j += 1;
}
if chars[i] == op_chars[0] && j < chars.len() && chars[j] == op_chars[1] {
return Err(
CalcError::parse(format!("illegal consecutive operators '{}'", op))
.with_span(Span::new(i, j + 1))
.with_i18n(
"msg.core.parse_illegal_consecutive_ops",
vec![("op".to_string(), op.to_string())],
),
);
}
}
i += 1;
}
Ok(())
}
fn preprocess_factorial(input: &str) -> Result<String, CalcError> {
let chars: Vec<char> = input.chars().collect();
let mut result: Vec<char> = Vec::with_capacity(chars.len() + 32);
let mut i = 0;
while i < chars.len() {
if chars[i] == '!' {
let operand_start =
find_operand_start(&result).map_err(|e| e.with_span(Span::point(i)))?;
let operand: String = result[operand_start..].iter().collect();
result.truncate(operand_start);
result.extend("factorial(".chars());
result.extend(operand.chars());
result.push(')');
} else {
result.push(chars[i]);
}
i += 1;
}
Ok(result.into_iter().collect())
}
fn find_operand_start(chars: &[char]) -> Result<usize, CalcError> {
let mut pos = chars.len();
while pos > 0 && chars[pos - 1].is_whitespace() {
pos -= 1;
}
if pos == 0 {
return Err(
CalcError::parse("factorial operator '!' has no operand".to_string())
.with_i18n("msg.core.parse_factorial_no_operand", vec![]),
);
}
if chars[pos - 1] == ')' {
pos = match_paren_backward(chars, pos - 1)?;
pos = scan_identifier_backward(chars, pos);
} else {
while pos > 0 {
let c = chars[pos - 1];
if c.is_alphanumeric() || c == '.' || c == '_' {
pos -= 1;
} else {
break;
}
}
}
Ok(pos)
}
fn match_paren_backward(chars: &[char], close_idx: usize) -> Result<usize, CalcError> {
let mut depth = 1;
let mut pos = close_idx;
while pos > 0 && depth > 0 {
pos -= 1;
match chars[pos] {
')' => depth += 1,
'(' => depth -= 1,
_ => {}
}
}
if depth != 0 {
return Err(
CalcError::parse("unmatched parenthesis in factorial operand".to_string())
.with_i18n("msg.core.parse_unmatched_paren_factorial", vec![]),
);
}
Ok(pos)
}
fn scan_identifier_backward(chars: &[char], mut pos: usize) -> usize {
while pos > 0 && chars[pos - 1].is_alphabetic() {
pos -= 1;
}
pos
}
fn insert_implicit_multiplication(input: &str) -> String {
let chars: Vec<char> = input.chars().collect();
let mut result = String::with_capacity(chars.len() + 16);
let mut prev_was_number_end = false; let mut prev_was_close_paren = false;
let mut in_identifier = false;
for (i, &c) in chars.iter().enumerate() {
if i > 0 {
if prev_was_number_end && (c == 'e' || c == 'E') && is_scientific_notation(&chars, i) {
} else if should_insert_implicit_mult(prev_was_number_end, prev_was_close_paren, c) {
result.push('*');
in_identifier = false;
}
}
result.push(c);
prev_was_close_paren = c == ')';
if c.is_alphabetic() || c == '_' {
in_identifier = true;
prev_was_number_end = false;
} else if c.is_ascii_digit() {
prev_was_number_end = !in_identifier;
} else {
in_identifier = false;
prev_was_number_end = false;
}
}
result
}
fn is_scientific_notation(chars: &[char], pos: usize) -> bool {
if let Some(&next) = chars.get(pos + 1) {
if next.is_ascii_digit() {
return true;
}
if next == '+' || next == '-' {
if let Some(&next2) = chars.get(pos + 2) {
return next2.is_ascii_digit();
}
}
}
false
}
fn should_insert_implicit_mult(
prev_was_number_end: bool,
prev_was_close_paren: bool,
curr: char,
) -> bool {
let curr_is_var_start = curr.is_alphabetic() || curr == '_';
let curr_is_digit = curr.is_ascii_digit();
let curr_is_open_paren = curr == '(';
if prev_was_number_end && (curr_is_var_start || curr_is_open_paren) {
return true;
}
if prev_was_close_paren && (curr_is_var_start || curr_is_digit || curr_is_open_paren) {
return true;
}
false
}
fn convert_with_depth(expr: &mathexpr::Expr, depth: usize) -> Result<AstNode, CalcError> {
if depth > MAX_AST_DEPTH {
return Err(CalcError::depth_exceeded());
}
use mathexpr::{BinOp as MBinOp, Expr};
match expr {
Expr::Number(n) => Ok(AstNode::Number(*n)),
Expr::Variable(name) => Ok(AstNode::Variable(name.clone())),
Expr::CurrentValue => Ok(AstNode::Variable("_".to_string())),
Expr::BinaryOp { op, left, right } => {
let l = convert_with_depth(left, depth + 1)?;
let r = convert_with_depth(right, depth + 1)?;
match op {
MBinOp::Mod => Ok(AstNode::FunctionCall("mod".to_string(), vec![l, r])),
MBinOp::Add => Ok(AstNode::BinaryOp(BinaryOp::Add, Box::new(l), Box::new(r))),
MBinOp::Sub => Ok(AstNode::BinaryOp(BinaryOp::Sub, Box::new(l), Box::new(r))),
MBinOp::Mul => Ok(AstNode::BinaryOp(BinaryOp::Mul, Box::new(l), Box::new(r))),
MBinOp::Div => Ok(AstNode::BinaryOp(BinaryOp::Div, Box::new(l), Box::new(r))),
MBinOp::Pow => Ok(AstNode::BinaryOp(BinaryOp::Pow, Box::new(l), Box::new(r))),
}
}
Expr::UnaryMinus(inner) => {
let e = convert_with_depth(inner, depth + 1)?;
Ok(AstNode::UnaryOp(UnaryOp::Neg, Box::new(e)))
}
Expr::FunctionCall { name, args } => {
if args.is_empty() && (name == "pi" || name == "e") {
return Ok(AstNode::Variable(name.clone()));
}
let mut converted_args = Vec::with_capacity(args.len());
for arg in args {
converted_args.push(convert_with_depth(arg, depth + 1)?);
}
if name == "complex" && converted_args.len() == 2 {
if let Some(complex) = try_complex_literal(&converted_args) {
return Ok(complex);
}
}
Ok(AstNode::FunctionCall(name.clone(), converted_args))
}
}
}
fn try_complex_literal(args: &[AstNode]) -> Option<AstNode> {
let re_val = match &args[0] {
AstNode::Number(n) => Some(*n),
_ => None,
};
let im_val = match &args[1] {
AstNode::Number(n) => Some(*n),
AstNode::UnaryOp(UnaryOp::Neg, inner) => {
if let AstNode::Number(n) = inner.as_ref() {
Some(-*n)
} else {
None
}
}
_ => None,
};
match (re_val, im_val) {
(Some(re), Some(im)) => Some(AstNode::Complex(re, im)),
_ => None,
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::core::types::*;
fn binop(op: BinaryOp, l: AstNode, r: AstNode) -> AstNode {
AstNode::BinaryOp(op, Box::new(l), Box::new(r))
}
fn unary(op: UnaryOp, e: AstNode) -> AstNode {
AstNode::UnaryOp(op, Box::new(e))
}
fn call(name: &str, args: Vec<AstNode>) -> AstNode {
AstNode::FunctionCall(name.to_string(), args)
}
fn num(n: f64) -> AstNode {
AstNode::Number(n)
}
fn var(name: &str) -> AstNode {
AstNode::Variable(name.to_string())
}
#[test]
fn test_simple_addition() {
let ast = parse("2+3").unwrap();
assert_eq!(ast, binop(BinaryOp::Add, num(2.0), num(3.0)));
}
#[test]
fn test_mixed_arithmetic_precedence() {
let ast = parse("(2+9)*7-6").unwrap();
assert_eq!(
ast,
binop(
BinaryOp::Sub,
binop(
BinaryOp::Mul,
binop(BinaryOp::Add, num(2.0), num(9.0)),
num(7.0)
),
num(6.0)
)
);
}
#[test]
fn test_left_associative_subtraction() {
let ast = parse("10-3-2").unwrap();
assert_eq!(
ast,
binop(
BinaryOp::Sub,
binop(BinaryOp::Sub, num(10.0), num(3.0)),
num(2.0)
)
);
}
#[test]
fn test_simple_power() {
let ast = parse("2^10").unwrap();
assert_eq!(ast, binop(BinaryOp::Pow, num(2.0), num(10.0)));
}
#[test]
fn test_power_right_associative() {
let ast = parse("2^3^2").unwrap();
assert_eq!(
ast,
binop(
BinaryOp::Pow,
num(2.0),
binop(BinaryOp::Pow, num(3.0), num(2.0))
)
);
}
#[test]
fn test_power_higher_precedence_than_mul() {
let ast = parse("2*3^2").unwrap();
assert_eq!(
ast,
binop(
BinaryOp::Mul,
num(2.0),
binop(BinaryOp::Pow, num(3.0), num(2.0))
)
);
}
#[test]
fn test_integer_factorial() {
let ast = parse("5!").unwrap();
assert_eq!(ast, call("factorial", vec![num(5.0)]));
}
#[test]
fn test_factorial_on_parenthesized_expr() {
let ast = parse("(2+3)!").unwrap();
assert_eq!(
ast,
call("factorial", vec![binop(BinaryOp::Add, num(2.0), num(3.0))])
);
}
#[test]
fn test_factorial_with_unary_minus() {
let ast = parse("-5!").unwrap();
assert_eq!(ast, unary(UnaryOp::Neg, call("factorial", vec![num(5.0)])));
}
#[test]
fn test_simple_modulo() {
let ast = parse("10%3").unwrap();
assert_eq!(ast, call("mod", vec![num(10.0), num(3.0)]));
}
#[test]
fn test_modulo_with_arithmetic() {
let ast = parse("10%3+1").unwrap();
assert_eq!(
ast,
binop(
BinaryOp::Add,
call("mod", vec![num(10.0), num(3.0)]),
num(1.0)
)
);
}
#[test]
fn test_modulo_with_subexpressions() {
let ast = parse("(2+8)%(4-1)").unwrap();
assert_eq!(
ast,
call(
"mod",
vec![
binop(BinaryOp::Add, num(2.0), num(8.0)),
binop(BinaryOp::Sub, num(4.0), num(1.0))
]
)
);
}
#[test]
fn test_single_arg_function() {
let ast = parse("sin(pi/2)").unwrap();
assert_eq!(
ast,
call("sin", vec![binop(BinaryOp::Div, var("pi"), num(2.0))])
);
}
#[test]
fn test_multi_arg_function() {
let ast = parse("log(100, 10)").unwrap();
assert_eq!(ast, call("log", vec![num(100.0), num(10.0)]));
}
#[test]
fn test_special_function_gamma() {
let ast = parse("gamma(5)").unwrap();
assert_eq!(ast, call("gamma", vec![num(5.0)]));
}
#[test]
fn test_nested_function_calls() {
let ast = parse("sin(cos(0))").unwrap();
assert_eq!(ast, call("sin", vec![call("cos", vec![num(0.0)])]));
}
#[test]
fn test_simple_variable_expr() {
let ast = parse("x+y").unwrap();
assert_eq!(ast, binop(BinaryOp::Add, var("x"), var("y")));
}
#[test]
fn test_variable_as_function_arg() {
let ast = parse("sin(x)").unwrap();
assert_eq!(ast, call("sin", vec![var("x")]));
}
#[test]
fn test_multichar_variable_names() {
let ast = parse("alpha+beta_1").unwrap();
assert_eq!(ast, binop(BinaryOp::Add, var("alpha"), var("beta_1")));
}
#[test]
fn test_double_nested_parens() {
let ast = parse("((1+2)*3)").unwrap();
assert_eq!(
ast,
binop(
BinaryOp::Mul,
binop(BinaryOp::Add, num(1.0), num(2.0)),
num(3.0)
)
);
}
#[test]
fn test_multi_level_parens() {
let ast = parse("(1+(2+3))*4").unwrap();
assert_eq!(
ast,
binop(
BinaryOp::Mul,
binop(
BinaryOp::Add,
num(1.0),
binop(BinaryOp::Add, num(2.0), num(3.0))
),
num(4.0)
)
);
}
#[test]
fn test_optional_parens_equivalent() {
let with_parens = parse("(1+2)").unwrap();
let without_parens = parse("1+2").unwrap();
assert_eq!(with_parens, without_parens);
}
#[test]
fn test_pi_constant() {
let ast = parse("pi").unwrap();
assert_eq!(ast, var("pi"));
}
#[test]
fn test_e_constant_in_expr() {
let ast = parse("e*2").unwrap();
assert_eq!(ast, binop(BinaryOp::Mul, var("e"), num(2.0)));
}
#[test]
fn test_constant_in_function_arg() {
let ast = parse("cos(pi)").unwrap();
assert_eq!(ast, call("cos", vec![var("pi")]));
}
#[test]
fn test_empty_string_rejected() {
let err = parse("").unwrap_err();
assert!(err.kind == ErrorKind::Parse && err.message.contains("empty"));
}
#[test]
fn test_unmatched_parens_rejected() {
let err1 = parse("(2+3").unwrap_err();
assert!(err1.kind == ErrorKind::Parse);
let err2 = parse("2+3)").unwrap_err();
assert!(err2.kind == ErrorKind::Parse);
}
#[test]
fn test_consecutive_operators_rejected() {
let err = parse("2++3").unwrap_err();
assert!(err.kind == ErrorKind::Parse && err.message.contains("consecutive operators"));
}
#[test]
fn test_consecutive_mul_operators_rejected() {
let err = parse("2**3").unwrap_err();
assert_eq!(err.kind, ErrorKind::Parse);
assert!(
err.message.contains("consecutive operators"),
"error message should mention 'consecutive operators', got: {}",
err.message
);
assert!(
err.message.contains("**"),
"error message should mention '**', got: {}",
err.message
);
assert_eq!(
err.i18n_key,
Some("msg.core.parse_illegal_consecutive_ops"),
"should use dedicated i18n key, got: {:?}",
err.i18n_key
);
}
#[test]
fn test_consecutive_div_operators_rejected() {
let err = parse("2//3").unwrap_err();
assert_eq!(err.kind, ErrorKind::Parse);
assert!(
err.message.contains("consecutive operators") && err.message.contains("//"),
"error message should mention 'consecutive operators' and '//', got: {}",
err.message
);
assert_eq!(err.i18n_key, Some("msg.core.parse_illegal_consecutive_ops"));
}
#[test]
fn test_consecutive_pow_operators_rejected() {
let err = parse("2^^3").unwrap_err();
assert_eq!(err.kind, ErrorKind::Parse);
assert!(
err.message.contains("consecutive operators") && err.message.contains("^^"),
"error message should mention 'consecutive operators' and '^^', got: {}",
err.message
);
assert_eq!(err.i18n_key, Some("msg.core.parse_illegal_consecutive_ops"));
}
#[test]
fn test_consecutive_operators_with_whitespace_rejected() {
let err = parse("2* *3").unwrap_err();
assert_eq!(err.kind, ErrorKind::Parse);
assert_eq!(err.i18n_key, Some("msg.core.parse_illegal_consecutive_ops"));
}
#[test]
fn test_double_negation_still_allowed() {
let result = parse("2--3");
assert!(
result.is_ok(),
"'--' should be allowed (double negation), got error: {:?}",
result.err()
);
}
#[test]
fn test_single_operator_still_allowed() {
assert!(parse("2*3").is_ok(), "'*' alone should be allowed");
assert!(parse("6/3").is_ok(), "'/' alone should be allowed");
assert!(parse("2^3").is_ok(), "'^' alone should be allowed");
assert!(parse("2+3").is_ok(), "'+' alone should be allowed");
}
#[test]
fn test_unclosed_function_rejected() {
let err = parse("sin(").unwrap_err();
assert!(err.kind == ErrorKind::Parse);
}
#[test]
fn test_operator_only_rejected() {
assert!(parse("+").is_err());
assert!(parse("*").is_err());
}
#[test]
fn test_depth_at_limit_passes() {
let expr = format!("1{}", "+1".repeat(255));
assert!(parse(&expr).is_ok());
}
#[test]
fn test_depth_exceeds_limit_rejected() {
let expr = format!("1{}", "+1".repeat(256));
let err = parse(&expr).unwrap_err();
assert_eq!(err, CalcError::depth_exceeded());
}
#[test]
fn test_depth_check_at_parse_time() {
let deep_expr = format!("1{}", "+1".repeat(256));
assert!(matches!(parse(&deep_expr), Err(e) if e.kind == ErrorKind::Depth));
}
#[test]
fn test_length_at_limit_passes() {
let var_name = "a".repeat(4094);
let expr = format!("{}+1", var_name);
assert_eq!(expr.len(), MAX_EXPR_LEN);
assert!(parse(&expr).is_ok());
}
#[test]
fn test_length_exceeds_limit_rejected() {
let mut expr = String::from("a");
while expr.len() <= MAX_EXPR_LEN {
expr.push_str("+1");
}
assert!(expr.len() > MAX_EXPR_LEN);
let err = parse(&expr).unwrap_err();
assert!(err.kind == ErrorKind::Parse && err.message.contains("length"));
}
#[test]
fn test_oversized_input_fast_fail() {
let expr = "a".repeat(100_000);
let err = parse(&expr).unwrap_err();
assert!(err.kind == ErrorKind::Parse);
}
#[test]
fn test_whitespace_trimmed() {
let ast = parse(" 2+3 ").unwrap();
assert_eq!(ast, binop(BinaryOp::Add, num(2.0), num(3.0)));
}
#[test]
fn test_decimal_numbers() {
let ast = parse("3.14").unwrap();
assert_eq!(ast, num(3.14));
}
#[test]
fn test_double_factorial() {
let ast = parse("5!!").unwrap();
assert_eq!(
ast,
call("factorial", vec![call("factorial", vec![num(5.0)])])
);
}
#[test]
fn test_factorial_on_variable() {
let ast = parse("x!").unwrap();
assert_eq!(ast, call("factorial", vec![var("x")]));
}
#[test]
fn test_factorial_on_function_call() {
let ast = parse("sin(x)!").unwrap();
assert_eq!(ast, call("factorial", vec![call("sin", vec![var("x")])]));
}
#[test]
fn test_factorial_in_expression() {
let ast = parse("2*3!").unwrap();
assert_eq!(
ast,
binop(BinaryOp::Mul, num(2.0), call("factorial", vec![num(3.0)]))
);
}
#[test]
fn test_complex_standard_literal() {
let ast = parse("3+4i").unwrap();
assert_eq!(ast, AstNode::Complex(3.0, 4.0));
}
#[test]
fn test_complex_pure_imaginary() {
let ast = parse("2i").unwrap();
assert_eq!(ast, AstNode::Complex(0.0, 2.0));
}
#[test]
fn test_complex_negative_imaginary() {
let ast = parse("3-4i").unwrap();
assert_eq!(ast, AstNode::Complex(3.0, -4.0));
}
#[test]
fn test_complex_real_only_not_triggered() {
let ast = parse("5").unwrap();
assert_eq!(ast, num(5.0));
}
#[test]
fn test_matrix_2x2_literal() {
let ast = parse("[[1,2],[3,4]]").unwrap();
assert_eq!(
ast,
AstNode::Matrix(vec![vec![num(1.0), num(2.0)], vec![num(3.0), num(4.0)],])
);
}
#[test]
fn test_matrix_2x3_literal() {
let ast = parse("[[1,2,3],[4,5,6]]").unwrap();
assert_eq!(
ast,
AstNode::Matrix(vec![
vec![num(1.0), num(2.0), num(3.0)],
vec![num(4.0), num(5.0), num(6.0)],
])
);
}
#[test]
fn test_matrix_1x3_literal() {
let ast = parse("[[1,2,3]]").unwrap();
assert_eq!(
ast,
AstNode::Matrix(vec![vec![num(1.0), num(2.0), num(3.0)]])
);
}
#[test]
fn test_list_standard_literal() {
let ast = parse("[1,2,3,4,5]").unwrap();
assert_eq!(
ast,
AstNode::List(vec![num(1.0), num(2.0), num(3.0), num(4.0), num(5.0),])
);
}
#[test]
fn test_list_single_element() {
let ast = parse("[42]").unwrap();
assert_eq!(ast, AstNode::List(vec![num(42.0)]));
}
#[test]
fn test_parse_bracket_literal_non_bracket_input() {
let result = parse_bracket_literal("abc");
assert!(result.is_err());
let err = result.unwrap_err();
assert!(err.kind == ErrorKind::Parse && err.message.contains("expected bracket literal"));
}
#[test]
fn test_unmatched_open_bracket_rejected() {
let result = parse("[[1,2]");
assert!(result.is_err());
let err = result.unwrap_err();
assert!(err.kind == ErrorKind::Parse && err.message.contains("unmatched '['"));
}
#[test]
fn test_replace_placeholders_in_matrix() {
let mut ast = AstNode::Matrix(vec![vec![
AstNode::Variable("__bn_0".to_string()),
AstNode::Number(1.0),
]]);
let mut placeholders = std::collections::HashMap::new();
placeholders.insert(
"__bn_0".to_string(),
AstNode::BigNumber("1234567890123456".to_string()),
);
replace_placeholders(&mut ast, &placeholders);
let AstNode::Matrix(rows) = &ast else {
panic!("expected Matrix after placeholder replacement")
};
assert_eq!(
rows[0][0],
AstNode::BigNumber("1234567890123456".to_string())
);
assert_eq!(rows[0][1], AstNode::Number(1.0));
}
#[test]
fn test_replace_placeholders_in_list() {
let mut ast = AstNode::List(vec![
AstNode::Variable("__bn_0".to_string()),
AstNode::Number(2.0),
]);
let mut placeholders = std::collections::HashMap::new();
placeholders.insert(
"__bn_0".to_string(),
AstNode::BigNumber("9876543210987654".to_string()),
);
replace_placeholders(&mut ast, &placeholders);
let AstNode::List(elements) = &ast else {
panic!("expected List after placeholder replacement")
};
assert_eq!(
elements[0],
AstNode::BigNumber("9876543210987654".to_string())
);
assert_eq!(elements[1], AstNode::Number(2.0));
}
#[test]
fn test_parse_matrix_literal_invalid_input() {
let result = parse_matrix_literal("[1,2]");
assert!(result.is_err());
let err = result.unwrap_err();
assert!(err.kind == ErrorKind::Parse && err.message.contains("invalid matrix literal"));
}
#[test]
fn test_parse_list_literal_invalid_input() {
let result = parse_list_literal("1,2");
assert!(result.is_err());
let err = result.unwrap_err();
assert!(err.kind == ErrorKind::Parse && err.message.contains("invalid list literal"));
}
#[test]
fn test_list_with_parens_in_elements() {
let ast = parse("[sin(x), 2]").unwrap();
assert_eq!(
ast,
AstNode::List(vec![call("sin", vec![var("x")]), num(2.0)])
);
}
#[test]
fn test_matrix_with_parens_in_elements() {
let ast = parse("[[sin(x), 2]]").unwrap();
assert_eq!(
ast,
AstNode::Matrix(vec![vec![call("sin", vec![var("x")]), num(2.0)]])
);
}
#[test]
fn test_factorial_with_space_before_operator() {
let ast = parse("5 !").unwrap();
assert_eq!(ast, call("factorial", vec![num(5.0)]));
}
#[test]
fn test_factorial_no_operand_error() {
let result = parse("!5");
assert!(result.is_err());
let err = result.unwrap_err();
assert!(err.kind == ErrorKind::Parse && err.message.contains("no operand"));
}
#[test]
fn test_factorial_on_nested_parens() {
let ast = parse("((1+2))!").unwrap();
assert_eq!(
ast,
call("factorial", vec![binop(BinaryOp::Add, num(1.0), num(2.0))])
);
}
#[test]
fn test_factorial_unmatched_paren_error() {
let result = parse("1+2)!");
assert!(result.is_err());
let err = result.unwrap_err();
assert!(err.kind == ErrorKind::Parse && err.message.contains("unmatched parenthesis"));
}
#[test]
fn test_parse_current_value_underscore() {
let ast = parse("_").unwrap();
assert_eq!(ast, var("_"));
}
#[test]
fn test_complex_call_with_variable_real() {
let ast = parse("complex(x, 4)").unwrap();
assert_eq!(ast, call("complex", vec![var("x"), num(4.0)]));
}
#[test]
fn test_complex_call_with_variable_imag() {
let ast = parse("complex(3, x)").unwrap();
assert_eq!(ast, call("complex", vec![num(3.0), var("x")]));
}
#[test]
fn test_complex_call_with_neg_variable_imag() {
let ast = parse("complex(3, -x)").unwrap();
assert_eq!(
ast,
call("complex", vec![num(3.0), unary(UnaryOp::Neg, var("x"))])
);
}
#[test]
fn test_implicit_mult_number_before_variable() {
let ast = parse("2x").unwrap();
assert_eq!(ast, binop(BinaryOp::Mul, num(2.0), var("x")));
}
#[test]
fn test_implicit_mult_number_before_paren() {
let ast = parse("3(x+1)").unwrap();
assert_eq!(
ast,
binop(
BinaryOp::Mul,
num(3.0),
binop(BinaryOp::Add, var("x"), num(1.0))
)
);
}
#[test]
fn test_implicit_mult_close_paren_before_open_paren() {
let ast = parse("(x+1)(x-1)").unwrap();
assert_eq!(
ast,
binop(
BinaryOp::Mul,
binop(BinaryOp::Add, var("x"), num(1.0)),
binop(BinaryOp::Sub, var("x"), num(1.0))
)
);
}
#[test]
fn test_implicit_mult_variable_before_paren_not_triggered() {
let ast = parse("sin(x)").unwrap();
assert_eq!(ast, call("sin", vec![var("x")]));
}
#[test]
fn test_implicit_mult_number_before_constant() {
let ast = parse("2pi").unwrap();
assert_eq!(ast, binop(BinaryOp::Mul, num(2.0), var("pi")));
}
#[test]
fn test_implicit_mult_close_paren_before_variable() {
let ast = parse("(x+1)x").unwrap();
assert_eq!(
ast,
binop(
BinaryOp::Mul,
binop(BinaryOp::Add, var("x"), num(1.0)),
var("x")
)
);
}
#[test]
fn test_implicit_mult_close_paren_before_number() {
let ast = parse("(x+1)2").unwrap();
assert_eq!(
ast,
binop(
BinaryOp::Mul,
binop(BinaryOp::Add, var("x"), num(1.0)),
num(2.0)
)
);
}
#[test]
fn test_implicit_mult_identifier_digit_not_triggered() {
let ast = parse("beta_1+1").unwrap();
assert_eq!(ast, binop(BinaryOp::Add, var("beta_1"), num(1.0)));
}
#[test]
fn test_implicit_mult_decimal_number() {
let ast = parse("3.14x").unwrap();
assert_eq!(ast, binop(BinaryOp::Mul, num(3.14), var("x")));
}
#[test]
fn test_parse_diff_function() {
let ast = parse("diff(x^2, x)").unwrap();
assert_eq!(
ast,
call(
"diff",
vec![binop(BinaryOp::Pow, var("x"), num(2.0)), var("x"),]
)
);
}
#[test]
fn test_parse_integrate_function() {
let ast = parse("integrate(sin(x), x)").unwrap();
assert_eq!(
ast,
call("integrate", vec![call("sin", vec![var("x")]), var("x"),])
);
}
#[test]
fn test_parse_simplify_function() {
let ast = parse("simplify(x^2+2*x+1)").unwrap();
let expected_inner = binop(
BinaryOp::Add,
binop(
BinaryOp::Add,
binop(BinaryOp::Pow, var("x"), num(2.0)),
binop(BinaryOp::Mul, num(2.0), var("x")),
),
num(1.0),
);
assert_eq!(ast, call("simplify", vec![expected_inner]));
}
#[test]
fn test_parse_limit_function() {
let ast = parse("limit(x^2, x, 0)").unwrap();
assert_eq!(
ast,
call(
"limit",
vec![binop(BinaryOp::Pow, var("x"), num(2.0)), var("x"), num(0.0),]
)
);
}
#[test]
fn test_parse_taylor_function() {
let ast = parse("taylor(sin(x), x, 5)").unwrap();
assert_eq!(
ast,
call(
"taylor",
vec![call("sin", vec![var("x")]), var("x"), num(5.0),]
)
);
}
#[test]
fn test_symbolic_function_not_implicit_mult() {
let ast = parse("diff(x)").unwrap();
assert_eq!(ast, call("diff", vec![var("x")]));
assert!(!matches!(ast, AstNode::BinaryOp(BinaryOp::Mul, _, _)));
}
#[test]
fn test_is_scientific_notation_e_followed_by_digit() {
let chars: Vec<char> = "1e5".chars().collect();
assert!(is_scientific_notation(&chars, 1));
}
#[test]
fn test_is_scientific_notation_e_followed_by_plus_digit() {
let chars: Vec<char> = "1e+5".chars().collect();
assert!(is_scientific_notation(&chars, 1));
}
#[test]
fn test_is_scientific_notation_e_followed_by_minus_digit() {
let chars: Vec<char> = "1e-5".chars().collect();
assert!(is_scientific_notation(&chars, 1));
}
#[test]
fn test_is_scientific_notation_e_followed_by_plus_non_digit() {
let chars: Vec<char> = "1e+x".chars().collect();
assert!(!is_scientific_notation(&chars, 1));
}
#[test]
fn test_is_scientific_notation_e_at_end() {
let chars: Vec<char> = "1e".chars().collect();
assert!(!is_scientific_notation(&chars, 1));
}
#[test]
fn test_is_scientific_notation_e_plus_at_end() {
let chars: Vec<char> = "1e+".chars().collect();
assert!(!is_scientific_notation(&chars, 1));
}
#[test]
fn test_is_scientific_notation_e_followed_by_non_digit_non_sign() {
let chars: Vec<char> = "1ex".chars().collect();
assert!(!is_scientific_notation(&chars, 1));
}
use proptest::prelude::*;
fn valid_expr_strategy() -> impl Strategy<Value = String> {
let num = (1u32..10000).prop_map(|n| n.to_string());
let op = prop_oneof![Just("+"), Just("-"), Just("*"), Just("/")];
(num.clone(), op, num).prop_map(|(a, op, b)| format!("{}{}{}", a, op, b))
}
proptest! {
#![proptest_config(ProptestConfig { cases: 256, ..ProptestConfig::default() })]
#[test]
fn prop_parse_idempotent(expr in valid_expr_strategy()) {
let first = parse(&expr);
let second = parse(&expr);
prop_assert_eq!(first, second);
}
#[test]
fn prop_valid_expr_parseable(expr in valid_expr_strategy()) {
let result = parse(&expr);
prop_assert!(result.is_ok(), "expected Ok for valid expr {:?}, got {:?}", expr, result);
}
#[test]
fn prop_depth_limit(n in 1usize..300) {
let expr = format!("1{}", "+1".repeat(n - 1));
let result = parse(&expr);
if n <= 256 {
prop_assert!(result.is_ok(), "depth {} should pass, got {:?}", n, result);
} else {
prop_assert!(matches!(&result, Err(e) if e.kind == ErrorKind::Depth),
"depth {} should fail, got {:?}", n, result);
}
}
}
fn assert_span(err: &CalcError, expected: Span) {
assert_eq!(
err.span,
Some(expected),
"span mismatch: got {:?}, expected {:?}, message: {}",
err.span,
expected,
err.message
);
}
#[test]
fn test_span_empty_expression() {
let err = parse("").unwrap_err();
assert_eq!(err.kind, ErrorKind::Parse);
assert_span(&err, Span::new(0, 0));
}
#[test]
fn test_span_whitespace_only_expression() {
let err = parse(" ").unwrap_err();
assert_eq!(err.kind, ErrorKind::Parse);
assert_span(&err, Span::new(0, 0));
}
#[test]
fn test_span_length_exceeded() {
let expr = "a".repeat(MAX_EXPR_LEN + 1);
let err = parse(&expr).unwrap_err();
assert_eq!(err.kind, ErrorKind::Parse);
assert_span(&err, Span::new(0, expr.chars().count()));
}
#[test]
fn test_span_mathexpr_parse_failure() {
let err = parse("@").unwrap_err();
assert_eq!(err.kind, ErrorKind::Parse);
assert_span(&err, Span::new(0, 1));
}
#[test]
fn test_span_mathexpr_failure_points_to_original_input() {
let err = parse("2x@").unwrap_err();
assert_eq!(err.kind, ErrorKind::Parse);
assert_span(&err, Span::new(0, 3));
}
#[test]
fn test_span_unmatched_open_bracket() {
let err = parse("[[1,2]").unwrap_err();
assert_eq!(err.kind, ErrorKind::Parse);
assert_span(&err, Span::new(0, 6));
}
#[test]
fn test_span_invalid_bracket_literal() {
let err = parse_bracket_literal("abc").unwrap_err();
assert_eq!(err.kind, ErrorKind::Parse);
assert_span(&err, Span::new(0, 3));
}
#[test]
fn test_span_invalid_matrix_literal() {
let err = parse_matrix_literal("[1,2]").unwrap_err();
assert_eq!(err.kind, ErrorKind::Parse);
assert_span(&err, Span::new(0, 5));
}
#[test]
fn test_span_invalid_list_literal() {
let err = parse_list_literal("1,2").unwrap_err();
assert_eq!(err.kind, ErrorKind::Parse);
assert_span(&err, Span::new(0, 3));
}
#[test]
fn test_span_consecutive_plus() {
let err = parse("2++3").unwrap_err();
assert_eq!(err.kind, ErrorKind::Parse);
assert_span(&err, Span::new(1, 3));
}
#[test]
fn test_span_consecutive_plus_with_spaces() {
let err = parse("2 + + 3").unwrap_err();
assert_eq!(err.kind, ErrorKind::Parse);
assert_span(&err, Span::new(2, 5));
}
#[test]
fn test_span_factorial_no_operand() {
let err = parse("!5").unwrap_err();
assert_eq!(err.kind, ErrorKind::Parse);
assert_span(&err, Span::point(0));
}
#[test]
fn test_span_factorial_unmatched_paren() {
let err = parse("1+2)!").unwrap_err();
assert_eq!(err.kind, ErrorKind::Parse);
assert_span(&err, Span::point(4));
}
#[test]
fn test_parse_string_literal_in_function_arg() {
let ast = parse(r#"date("2026-07-25")"#).unwrap();
assert_eq!(
ast,
call("date", vec![AstNode::Str("2026-07-25".to_string())])
);
}
#[test]
fn test_parse_string_literal_at_any_position() {
let ast = parse(r#"convert(100,"cm","m")"#).unwrap();
assert_eq!(
ast,
call(
"convert",
vec![
num(100.0),
AstNode::Str("cm".to_string()),
AstNode::Str("m".to_string()),
]
)
);
let ast = parse(r#"format_date("%Y", 0)"#).unwrap();
assert_eq!(
ast,
call(
"format_date",
vec![AstNode::Str("%Y".to_string()), num(0.0)]
)
);
let ast = parse(r#"reformat_date(0, "%d/%m/%Y", "%Y-%m-%d")"#).unwrap();
assert_eq!(
ast,
call(
"reformat_date",
vec![
num(0.0),
AstNode::Str("%d/%m/%Y".to_string()),
AstNode::Str("%Y-%m-%d".to_string()),
]
)
);
}
#[test]
fn test_parse_string_literal_with_escaped_quote() {
let ast = parse(r#"f("a\"b")"#).unwrap();
assert_eq!(ast, call("f", vec![AstNode::Str("a\"b".to_string())]));
}
#[test]
fn test_parse_string_literal_unclosed_rejected() {
let err = parse(r#"f("unclosed)"#).unwrap_err();
assert_eq!(err.kind, ErrorKind::Parse);
assert!(
err.message.contains("unclosed string"),
"error message should mention 'unclosed string', got: {}",
err.message
);
}
#[test]
fn test_parse_string_literal_empty() {
let ast = parse(r#"f("")"#).unwrap();
assert_eq!(ast, call("f", vec![AstNode::Str("".to_string())]));
}
#[test]
fn test_parse_string_literal_preserves_brackets() {
let ast = parse(r#"f("[1,2]")"#).unwrap();
assert_eq!(ast, call("f", vec![AstNode::Str("[1,2]".to_string())]));
}
#[test]
fn test_parse_string_literal_preserves_bigint() {
let ast = parse(r#"f("12345678901234567890")"#).unwrap();
assert_eq!(
ast,
call("f", vec![AstNode::Str("12345678901234567890".to_string())])
);
}
#[test]
fn test_parse_string_literal_with_special_chars() {
let ast = parse(r#"f("2026-07-25 12:30:00")"#).unwrap();
assert_eq!(
ast,
call("f", vec![AstNode::Str("2026-07-25 12:30:00".to_string())])
);
}
#[test]
fn test_parse_string_literal_with_nested_escaped_quotes() {
let ast = parse(r#"f("a\"b\"c")"#).unwrap();
assert_eq!(ast, call("f", vec![AstNode::Str("a\"b\"c".to_string())]));
}
#[test]
fn test_parse_multiple_string_literals() {
let ast = parse(r#"f("a", "b", "c")"#).unwrap();
assert_eq!(
ast,
call(
"f",
vec![
AstNode::Str("a".to_string()),
AstNode::Str("b".to_string()),
AstNode::Str("c".to_string()),
]
)
);
}
}