KCL: End-exclusive ranges like [0..<10] (#7179)

Closes https://github.com/KittyCAD/modeling-app/issues/6843

To clarify:
`[1..10]` is 1, 2, ..., 8, 9, 10
`[1..<10]` is 1, 2, ... 8, 9
This commit is contained in:
Adam Chalmers
2025-05-22 22:13:27 -05:00
committed by GitHub
parent fa4b3cfd1b
commit d0958220fe
5 changed files with 155 additions and 5 deletions

View File

@ -886,7 +886,7 @@ fn array_end_start(i: &mut TokenSlice) -> PResult<Node<ArrayRangeExpression>> {
ignore_whitespace(i);
let start_element = expression.parse_next(i)?;
ignore_whitespace(i);
double_period.parse_next(i)?;
let end_inclusive = alt((end_inclusive_range.map(|_| true), end_exclusive_range.map(|_| false))).parse_next(i)?;
ignore_whitespace(i);
let end_element = expression.parse_next(i)?;
ignore_whitespace(i);
@ -895,7 +895,7 @@ fn array_end_start(i: &mut TokenSlice) -> PResult<Node<ArrayRangeExpression>> {
ArrayRangeExpression {
start_element,
end_element,
end_inclusive: true,
end_inclusive,
digest: None,
},
start,
@ -2705,7 +2705,7 @@ fn period(i: &mut TokenSlice) -> PResult<()> {
Ok(())
}
fn double_period(i: &mut TokenSlice) -> PResult<Token> {
fn end_inclusive_range(i: &mut TokenSlice) -> PResult<Token> {
any.try_map(|token: Token| {
if matches!(token.token_type, TokenType::DoublePeriod) {
Ok(token)
@ -2724,6 +2724,21 @@ fn double_period(i: &mut TokenSlice) -> PResult<Token> {
.parse_next(i)
}
fn end_exclusive_range(i: &mut TokenSlice) -> PResult<Token> {
any.try_map(|token: Token| {
if matches!(token.token_type, TokenType::DoublePeriodLessThan) {
Ok(token)
} else {
Err(CompilationError::fatal(
token.as_source_range(),
format!("expected a '..<' but found {}", token.value.as_str()),
))
}
})
.context(expected("the ..< operator, used for array ranges like [0..<10]"))
.parse_next(i)
}
fn colon(i: &mut TokenSlice) -> PResult<Token> {
TokenType::Colon.parse_from(i)
}
@ -5344,7 +5359,6 @@ mod snapshot_tests {
);
snapshot_test!(aa, r#"sg = -scale"#);
snapshot_test!(ab, "line(endAbsolute = [0, -1])");
snapshot_test!(ac, "myArray = [0..10]");
snapshot_test!(
ad,
r#"
@ -5485,6 +5499,11 @@ my14 = 4 ^ 2 - 3 ^ 2 * 2
)"#
);
snapshot_test!(kw_function_in_binary_op, r#"val = f(x = 1) + 1"#);
snapshot_test!(
array_ranges,
r#"incl = [1..10]
excl = [0..<10]"#
);
}
#[allow(unused)]