Skip to content

Commit e61857c

Browse files
committed
Fix parsing of numerics in function arguments
1 parent be5adfd commit e61857c

File tree

1 file changed

+57
-2
lines changed

1 file changed

+57
-2
lines changed

src/parsing/parser.rs

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1476,8 +1476,8 @@ impl<'i> Parser<'i> {
14761476
.source
14771477
.char_indices()
14781478
{
1479-
if ch.is_whitespace() {
1480-
// Stop at whitespace
1479+
if ch.is_whitespace() || ch == ',' || ch == ')' {
1480+
// Stop at whitespace, comma, or closing parameter boundary
14811481
break;
14821482
} else if ch.is_ascii_alphabetic() || ch == '°' || ch == '/' || ch == 'μ' {
14831483
// Valid character
@@ -1875,6 +1875,9 @@ impl<'i> Parser<'i> {
18751875
inner.parse_string_pieces(inner.source)
18761876
})?;
18771877
params.push(Expression::String(parts));
1878+
} else if is_numeric(content) {
1879+
let numeric = outer.read_numeric()?;
1880+
params.push(Expression::Number(numeric));
18781881
} else {
18791882
let name = outer.read_identifier()?;
18801883
params.push(Expression::Variable(name));
@@ -3502,6 +3505,58 @@ echo "Done"```) }"#,
35023505
)]
35033506
}))
35043507
);
3508+
3509+
// Test function with quantity parameter (like timer with duration)
3510+
input.initialize("{ timer(3 hr) }");
3511+
let result = input.read_code_block();
3512+
assert_eq!(
3513+
result,
3514+
Ok(Expression::Execution(Function {
3515+
target: Identifier("timer"),
3516+
parameters: vec![Expression::Number(Numeric::Scientific(Quantity {
3517+
mantissa: Decimal {
3518+
number: 3,
3519+
precision: 0
3520+
},
3521+
uncertainty: None,
3522+
magnitude: None,
3523+
symbol: "hr"
3524+
}))]
3525+
}))
3526+
);
3527+
3528+
// Test function with integer quantity parameter
3529+
input.initialize("{ measure(100) }");
3530+
let result = input.read_code_block();
3531+
assert_eq!(
3532+
result,
3533+
Ok(Expression::Execution(Function {
3534+
target: Identifier("measure"),
3535+
parameters: vec![Expression::Number(Numeric::Integral(100))]
3536+
}))
3537+
);
3538+
3539+
// Test function with decimal quantity parameter
3540+
input.initialize("{ wait(2.5 s, \"yes\") }");
3541+
let result = input.read_code_block();
3542+
assert_eq!(
3543+
result,
3544+
Ok(Expression::Execution(Function {
3545+
target: Identifier("wait"),
3546+
parameters: vec![
3547+
Expression::Number(Numeric::Scientific(Quantity {
3548+
mantissa: Decimal {
3549+
number: 25,
3550+
precision: 1
3551+
},
3552+
uncertainty: None,
3553+
magnitude: None,
3554+
symbol: "s"
3555+
})),
3556+
Expression::String(vec![Piece::Text("yes")])
3557+
]
3558+
}))
3559+
);
35053560
}
35063561

35073562
#[test]

0 commit comments

Comments
 (0)