Accept type ascription within binary expressions (#6849)

Signed-off-by: Nick Cameron <nrc@ncameron.org>
This commit is contained in:
Nick Cameron
2025-05-12 14:07:57 +12:00
committed by GitHub
parent dfc4b7d0c5
commit 21ccf129d6
8 changed files with 70 additions and 31 deletions

View File

@ -631,8 +631,7 @@ fn operand(i: &mut TokenSlice) -> PResult<BinaryPart> {
| Expr::ArrayExpression(_)
| Expr::ArrayRangeExpression(_)
| Expr::ObjectExpression(_)
| Expr::LabelledExpression(..)
| Expr::AscribedExpression(..) => return Err(CompilationError::fatal(source_range, TODO_783)),
| Expr::LabelledExpression(..) => return Err(CompilationError::fatal(source_range, TODO_783)),
Expr::None(_) => {
return Err(CompilationError::fatal(
source_range,
@ -658,6 +657,7 @@ fn operand(i: &mut TokenSlice) -> PResult<BinaryPart> {
Expr::CallExpressionKw(x) => BinaryPart::CallExpressionKw(x),
Expr::MemberExpression(x) => BinaryPart::MemberExpression(x),
Expr::IfExpression(x) => BinaryPart::IfExpression(x),
Expr::AscribedExpression(x) => BinaryPart::AscribedExpression(x),
};
Ok(expr)
})
@ -2068,7 +2068,7 @@ fn expr_allowed_in_pipe_expr(i: &mut TokenSlice) -> PResult<Expr> {
}
fn possible_operands(i: &mut TokenSlice) -> PResult<Expr> {
alt((
let mut expr = alt((
unary_expression.map(Box::new).map(Expr::UnaryExpression),
bool_value.map(Expr::Literal),
member_expression.map(Box::new).map(Expr::MemberExpression),
@ -2081,7 +2081,14 @@ fn possible_operands(i: &mut TokenSlice) -> PResult<Expr> {
.context(expected(
"a KCL value which can be used as an argument/operand to an operator",
))
.parse_next(i)
.parse_next(i)?;
let ty = opt((colon, opt(whitespace), argument_type)).parse_next(i)?;
if let Some((_, _, ty)) = ty {
expr = Expr::AscribedExpression(Box::new(AscribedExpression::new(expr, ty)))
}
Ok(expr)
}
/// Parse an item visibility specifier, e.g. export.