diff --git a/crates/fidc-core/src/platform_expr_strategy.rs b/crates/fidc-core/src/platform_expr_strategy.rs index 3d2eab0..9e73179 100644 --- a/crates/fidc-core/src/platform_expr_strategy.rs +++ b/crates/fidc-core/src/platform_expr_strategy.rs @@ -2993,26 +2993,33 @@ impl PlatformExprStrategy { } fn rewrite_ternary(expr: &str) -> String { - let Some((question_idx, colon_idx)) = Self::find_top_level_ternary(expr) else { - return expr.trim().to_string(); + let trimmed = expr.trim(); + let Some((condition_start, question_idx, colon_idx, false_end)) = + Self::find_ternary_span(trimmed) + else { + return trimmed.to_string(); }; - let condition = Self::rewrite_ternary(expr[..question_idx].trim()); - let when_true = Self::rewrite_ternary(expr[question_idx + 1..colon_idx].trim()); - let when_false = Self::rewrite_ternary(expr[colon_idx + 1..].trim()); - format!( - "if {} {{ {} }} else {{ {} }}", + let condition = Self::rewrite_ternary(trimmed[condition_start..question_idx].trim()); + let when_true = Self::rewrite_ternary(trimmed[question_idx + 1..colon_idx].trim()); + let when_false = Self::rewrite_ternary(trimmed[colon_idx + 1..false_end].trim()); + let mut rewritten = String::new(); + rewritten.push_str(&trimmed[..condition_start]); + rewritten.push_str(&format!( + "(if {} {{ {} }} else {{ {} }})", condition, when_true, when_false - ) + )); + rewritten.push_str(&trimmed[false_end..]); + Self::rewrite_ternary(&rewritten) } - fn find_top_level_ternary(expr: &str) -> Option<(usize, usize)> { + fn find_ternary_span(expr: &str) -> Option<(usize, usize, usize, usize)> { let mut paren_depth = 0i32; let mut brace_depth = 0i32; let mut bracket_depth = 0i32; let mut in_single_quote = false; let mut in_double_quote = false; let mut escaped = false; - let mut question_stack = Vec::new(); + let mut question_stack: Vec<(usize, i32, i32, i32)> = Vec::new(); for (idx, ch) in expr.char_indices() { if escaped { @@ -3036,13 +3043,25 @@ impl PlatformExprStrategy { '}' => brace_depth -= 1, '[' => bracket_depth += 1, ']' => bracket_depth -= 1, - '?' if paren_depth == 0 && brace_depth == 0 && bracket_depth == 0 => { - question_stack.push(idx); + '?' => { + question_stack.push((idx, paren_depth, brace_depth, bracket_depth)); } - ':' if paren_depth == 0 && brace_depth == 0 && bracket_depth == 0 => { - if let Some(question_idx) = question_stack.pop() { - if question_stack.is_empty() { - return Some((question_idx, idx)); + ':' => { + if let Some(&(question_idx, question_paren, question_brace, question_bracket)) = + question_stack.last() + { + if question_paren == paren_depth + && question_brace == brace_depth + && question_bracket == bracket_depth + { + question_stack.pop(); + let condition_start = Self::find_ternary_condition_start( + expr, + question_idx, + question_paren, + ); + let false_end = Self::find_ternary_false_end(expr, idx, paren_depth); + return Some((condition_start, question_idx, idx, false_end)); } } } @@ -3052,6 +3071,97 @@ impl PlatformExprStrategy { None } + fn find_ternary_condition_start( + expr: &str, + question_idx: usize, + ternary_paren_depth: i32, + ) -> usize { + let mut paren_depth = 0i32; + let mut brace_depth = 0i32; + let mut bracket_depth = 0i32; + let mut in_single_quote = false; + let mut in_double_quote = false; + let mut escaped = false; + let mut start = 0usize; + + for (idx, ch) in expr.char_indices() { + if idx >= question_idx { + break; + } + if escaped { + escaped = false; + continue; + } + match ch { + '\\' if in_single_quote || in_double_quote => escaped = true, + '\'' if !in_double_quote => in_single_quote = !in_single_quote, + '"' if !in_single_quote => in_double_quote = !in_double_quote, + _ if in_single_quote || in_double_quote => {} + '(' => { + let next_depth = paren_depth + 1; + paren_depth += 1; + if next_depth == ternary_paren_depth && brace_depth == 0 && bracket_depth == 0 + { + start = idx + ch.len_utf8(); + } + } + ')' => paren_depth -= 1, + '{' => brace_depth += 1, + '}' => brace_depth -= 1, + '[' => bracket_depth += 1, + ']' => bracket_depth -= 1, + ',' if paren_depth == 0 && brace_depth == 0 && bracket_depth == 0 => { + start = idx + ch.len_utf8(); + } + _ => {} + } + } + start + } + + fn find_ternary_false_end(expr: &str, colon_idx: usize, ternary_paren_depth: i32) -> usize { + let mut paren_depth = ternary_paren_depth; + let mut brace_depth = 0i32; + let mut bracket_depth = 0i32; + let mut in_single_quote = false; + let mut in_double_quote = false; + let mut escaped = false; + + for (idx, ch) in expr[colon_idx + 1..].char_indices() { + let absolute_idx = colon_idx + 1 + idx; + if escaped { + escaped = false; + continue; + } + match ch { + '\\' if in_single_quote || in_double_quote => escaped = true, + '\'' if !in_double_quote => in_single_quote = !in_single_quote, + '"' if !in_single_quote => in_double_quote = !in_double_quote, + _ if in_single_quote || in_double_quote => {} + '(' => paren_depth += 1, + ')' => { + if paren_depth == ternary_paren_depth && brace_depth == 0 && bracket_depth == 0 + { + return absolute_idx; + } + paren_depth -= 1; + } + '{' => brace_depth += 1, + '}' => brace_depth -= 1, + '[' => bracket_depth += 1, + ']' => bracket_depth -= 1, + ',' if paren_depth == ternary_paren_depth + && brace_depth == 0 + && bracket_depth == 0 => + { + return absolute_idx; + } + _ => {} + } + } + expr.len() + } + fn eval_float( &self, ctx: &StrategyContext<'_>, @@ -4720,6 +4830,14 @@ mod tests { NaiveDate::from_ymd_opt(year, month, day).expect("valid date") } + #[test] + fn platform_expr_rewrites_nested_ternary() { + let expr = "pct_change(\"close\", 10) + (((close / rolling_mean(\"close\", 20)) - 1) > 0 ? 0.04 : -0.04)"; + let rewritten = PlatformExprStrategy::rewrite_ternary(expr); + assert!(rewritten.contains("if ((close / rolling_mean(\"close\", 20)) - 1) > 0")); + assert!(!rewritten.contains('?')); + } + fn sample_calendar() -> TradingCalendar { TradingCalendar::new(vec![ d(2025, 1, 30),