Add ListItem type for Expr::List (#12529)

# Description
This PR adds a `ListItem` enum to our set of AST types. It encodes the
two possible expressions inside of list expression: a singular item or a
spread. This is similar to the existing `RecordItem` enum. Adding
`ListItem` allows us to remove the existing `Expr::Spread` case which
was previously used for list spreads. As a consequence, this guarantees
(via the type system) that spreads can only ever occur inside lists,
records, or as command args.

This PR also does a little bit of cleanup in relevant parser code.
This commit is contained in:
Ian Manske
2024-04-18 11:21:05 +00:00
committed by GitHub
parent 57b0c722c6
commit 6ccd547d81
8 changed files with 179 additions and 133 deletions

View File

@ -1,6 +1,6 @@
use nu_engine::command_prelude::*;
use nu_protocol::{
ast::{Expr, Expression, RecordItem},
ast::{Expr, Expression, ListItem, RecordItem},
engine::StateWorkingSet,
Range, Unit,
};
@ -253,8 +253,21 @@ fn convert_to_value(
}),
Expr::List(vals) => {
let mut output = vec![];
for val in vals {
output.push(convert_to_value(val, span, original_text)?);
for item in vals {
match item {
ListItem::Item(expr) => {
output.push(convert_to_value(expr, span, original_text)?);
}
ListItem::Spread(_, inner) => {
return Err(ShellError::OutsideSpannedLabeledError {
src: original_text.to_string(),
error: "Error when loading".into(),
msg: "spread operator not supported in nuon".into(),
span: inner.span,
});
}
}
}
Ok(Value::list(output, span))
@ -351,12 +364,6 @@ fn convert_to_value(
msg: "signatures not supported in nuon".into(),
span: expr.span,
}),
Expr::Spread(..) => Err(ShellError::OutsideSpannedLabeledError {
src: original_text.to_string(),
error: "Error when loading".into(),
msg: "spread operator not supported in nuon".into(),
span: expr.span,
}),
Expr::String(s) => Ok(Value::string(s, span)),
Expr::StringInterpolation(..) => Err(ShellError::OutsideSpannedLabeledError {
src: original_text.to_string(),