Compare commits
1 Commits
v2026.4.28
...
v2026.4.29
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e5439956eb |
@@ -2993,26 +2993,33 @@ impl PlatformExprStrategy {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn rewrite_ternary(expr: &str) -> String {
|
fn rewrite_ternary(expr: &str) -> String {
|
||||||
let Some((question_idx, colon_idx)) = Self::find_top_level_ternary(expr) else {
|
let trimmed = expr.trim();
|
||||||
return expr.trim().to_string();
|
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 condition = Self::rewrite_ternary(trimmed[condition_start..question_idx].trim());
|
||||||
let when_true = Self::rewrite_ternary(expr[question_idx + 1..colon_idx].trim());
|
let when_true = Self::rewrite_ternary(trimmed[question_idx + 1..colon_idx].trim());
|
||||||
let when_false = Self::rewrite_ternary(expr[colon_idx + 1..].trim());
|
let when_false = Self::rewrite_ternary(trimmed[colon_idx + 1..false_end].trim());
|
||||||
format!(
|
let mut rewritten = String::new();
|
||||||
"if {} {{ {} }} else {{ {} }}",
|
rewritten.push_str(&trimmed[..condition_start]);
|
||||||
|
rewritten.push_str(&format!(
|
||||||
|
"(if {} {{ {} }} else {{ {} }})",
|
||||||
condition, when_true, when_false
|
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 paren_depth = 0i32;
|
||||||
let mut brace_depth = 0i32;
|
let mut brace_depth = 0i32;
|
||||||
let mut bracket_depth = 0i32;
|
let mut bracket_depth = 0i32;
|
||||||
let mut in_single_quote = false;
|
let mut in_single_quote = false;
|
||||||
let mut in_double_quote = false;
|
let mut in_double_quote = false;
|
||||||
let mut escaped = 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() {
|
for (idx, ch) in expr.char_indices() {
|
||||||
if escaped {
|
if escaped {
|
||||||
@@ -3036,13 +3043,25 @@ impl PlatformExprStrategy {
|
|||||||
'}' => brace_depth -= 1,
|
'}' => brace_depth -= 1,
|
||||||
'[' => bracket_depth += 1,
|
'[' => bracket_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 let Some(&(question_idx, question_paren, question_brace, question_bracket)) =
|
||||||
if question_stack.is_empty() {
|
question_stack.last()
|
||||||
return Some((question_idx, idx));
|
{
|
||||||
|
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
|
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(
|
fn eval_float(
|
||||||
&self,
|
&self,
|
||||||
ctx: &StrategyContext<'_>,
|
ctx: &StrategyContext<'_>,
|
||||||
@@ -4720,6 +4830,14 @@ mod tests {
|
|||||||
NaiveDate::from_ymd_opt(year, month, day).expect("valid date")
|
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 {
|
fn sample_calendar() -> TradingCalendar {
|
||||||
TradingCalendar::new(vec![
|
TradingCalendar::new(vec![
|
||||||
d(2025, 1, 30),
|
d(2025, 1, 30),
|
||||||
|
|||||||
Reference in New Issue
Block a user