Skip to content

Commit 32fc5be

Browse files
committed
Add backslash line continuation support in parser preprocessor
1 parent 24f6a5f commit 32fc5be

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1693,6 +1693,13 @@ pub fn insert_statement_separators(input: &str) -> String {
16931693
continue;
16941694
}
16951695

1696+
// Handle line continuation: backslash followed by newline
1697+
if ch == '\\' && i + 1 < len && chars[i + 1] == '\n' {
1698+
// Skip both the backslash and the newline — the next line continues this one
1699+
i += 2;
1700+
continue;
1701+
}
1702+
16961703
// Track nesting depth (including <| |> for associations)
16971704
if ch == '<' && i + 1 < len && chars[i + 1] == '|' {
16981705
depth += 1;

tests/interpreter_tests/syntax.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3701,3 +3701,35 @@ mod tilde_infix {
37013701
);
37023702
}
37033703
}
3704+
3705+
mod line_continuation {
3706+
use super::*;
3707+
3708+
#[test]
3709+
fn backslash_newline_in_definition() {
3710+
// Backslash at end of line continues the expression on the next line
3711+
assert_eq!(interpret("f[x_] :=\\\n x^2\nf[5]").unwrap(), "25");
3712+
}
3713+
3714+
#[test]
3715+
fn backslash_newline_in_expression() {
3716+
assert_eq!(interpret("1 +\\\n2 +\\\n3").unwrap(), "6");
3717+
}
3718+
3719+
#[test]
3720+
fn backslash_newline_preserves_function_def() {
3721+
assert_eq!(
3722+
interpret(
3723+
"ImaginaryQ[u_] :=\\\n Head[u]===Complex && Re[u]===0\nImaginaryQ[3 I]"
3724+
)
3725+
.unwrap(),
3726+
"True"
3727+
);
3728+
}
3729+
3730+
#[test]
3731+
fn backslash_newline_not_in_strings() {
3732+
// Backslash inside strings should NOT be treated as line continuation
3733+
assert_eq!(interpret(r#""hello\nworld""#).unwrap(), r#"hello\nworld"#);
3734+
}
3735+
}

0 commit comments

Comments
 (0)