mirror of
https://github.com/nushell/nushell.git
synced 2024-11-23 00:43:33 +01:00
Fix table parsing
This commit is contained in:
parent
24cd1b591c
commit
46d2efca13
@ -263,7 +263,26 @@ pub fn eval_expression(state: &State, expr: &Expression) -> Result<Value, ShellE
|
||||
span: expr.span,
|
||||
})
|
||||
}
|
||||
Expr::Table(_, _) => Err(ShellError::Unsupported(expr.span)),
|
||||
Expr::Table(headers, vals) => {
|
||||
let mut output_headers = vec![];
|
||||
for expr in headers {
|
||||
output_headers.push(eval_expression(state, expr)?.as_string()?);
|
||||
}
|
||||
|
||||
let mut output_rows = vec![];
|
||||
for val in vals {
|
||||
let mut row = vec![];
|
||||
for expr in val {
|
||||
row.push(eval_expression(state, expr)?);
|
||||
}
|
||||
output_rows.push(row);
|
||||
}
|
||||
Ok(Value::Table {
|
||||
headers: output_headers,
|
||||
val: output_rows,
|
||||
span: expr.span,
|
||||
})
|
||||
}
|
||||
Expr::Keyword(_, _, expr) => eval_expression(state, expr),
|
||||
Expr::String(s) => Ok(Value::String {
|
||||
val: s.clone(),
|
||||
|
@ -6,13 +6,38 @@ use crate::ShellError;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Value {
|
||||
Bool { val: bool, span: Span },
|
||||
Int { val: i64, span: Span },
|
||||
Float { val: f64, span: Span },
|
||||
String { val: String, span: Span },
|
||||
List { val: Vec<Value>, span: Span },
|
||||
Block { val: BlockId, span: Span },
|
||||
Nothing { span: Span },
|
||||
Bool {
|
||||
val: bool,
|
||||
span: Span,
|
||||
},
|
||||
Int {
|
||||
val: i64,
|
||||
span: Span,
|
||||
},
|
||||
Float {
|
||||
val: f64,
|
||||
span: Span,
|
||||
},
|
||||
String {
|
||||
val: String,
|
||||
span: Span,
|
||||
},
|
||||
List {
|
||||
val: Vec<Value>,
|
||||
span: Span,
|
||||
},
|
||||
Table {
|
||||
headers: Vec<String>,
|
||||
val: Vec<Vec<Value>>,
|
||||
span: Span,
|
||||
},
|
||||
Block {
|
||||
val: BlockId,
|
||||
span: Span,
|
||||
},
|
||||
Nothing {
|
||||
span: Span,
|
||||
},
|
||||
}
|
||||
|
||||
impl Value {
|
||||
@ -30,6 +55,7 @@ impl Value {
|
||||
Value::Float { span, .. } => *span,
|
||||
Value::String { span, .. } => *span,
|
||||
Value::List { span, .. } => *span,
|
||||
Value::Table { span, .. } => *span,
|
||||
Value::Block { span, .. } => *span,
|
||||
Value::Nothing { span, .. } => *span,
|
||||
}
|
||||
@ -42,6 +68,7 @@ impl Value {
|
||||
Value::Float { span, .. } => *span = new_span,
|
||||
Value::String { span, .. } => *span = new_span,
|
||||
Value::List { span, .. } => *span = new_span,
|
||||
Value::Table { span, .. } => *span = new_span,
|
||||
Value::Block { span, .. } => *span = new_span,
|
||||
Value::Nothing { span, .. } => *span = new_span,
|
||||
}
|
||||
@ -56,6 +83,7 @@ impl Value {
|
||||
Value::Float { .. } => Type::Float,
|
||||
Value::String { .. } => Type::String,
|
||||
Value::List { .. } => Type::List(Box::new(Type::Unknown)), // FIXME
|
||||
Value::Table { .. } => Type::Table, // FIXME
|
||||
Value::Nothing { .. } => Type::Nothing,
|
||||
Value::Block { .. } => Type::Block,
|
||||
}
|
||||
@ -99,6 +127,26 @@ impl Display for Value {
|
||||
.join(", ".into())
|
||||
)
|
||||
}
|
||||
Value::Table { headers, val, .. } => {
|
||||
write!(
|
||||
f,
|
||||
"[{}]\n[{}]",
|
||||
headers
|
||||
.iter()
|
||||
.map(|x| x.to_string())
|
||||
.collect::<Vec<String>>()
|
||||
.join(", ".into()),
|
||||
val.iter()
|
||||
.map(|x| {
|
||||
x.iter()
|
||||
.map(|x| x.to_string())
|
||||
.collect::<Vec<String>>()
|
||||
.join(", ".into())
|
||||
})
|
||||
.collect::<Vec<String>>()
|
||||
.join("\n")
|
||||
)
|
||||
}
|
||||
Value::Block { .. } => write!(f, "<block>"),
|
||||
Value::Nothing { .. } => write!(f, ""),
|
||||
}
|
||||
|
@ -1882,8 +1882,10 @@ impl<'a> ParserWorkingSet<'a> {
|
||||
_ => {
|
||||
let mut table_headers = vec![];
|
||||
|
||||
let (headers, err) =
|
||||
self.parse_value(output.block[0].commands[0].parts[0], &SyntaxShape::Table);
|
||||
let (headers, err) = self.parse_value(
|
||||
output.block[0].commands[0].parts[0],
|
||||
&SyntaxShape::List(Box::new(SyntaxShape::Any)),
|
||||
);
|
||||
error = error.or(err);
|
||||
|
||||
if let Expression {
|
||||
@ -1896,7 +1898,8 @@ impl<'a> ParserWorkingSet<'a> {
|
||||
|
||||
let mut rows = vec![];
|
||||
for part in &output.block[1].commands[0].parts {
|
||||
let (values, err) = self.parse_value(*part, &SyntaxShape::Table);
|
||||
let (values, err) =
|
||||
self.parse_value(*part, &SyntaxShape::List(Box::new(SyntaxShape::Any)));
|
||||
error = error.or(err);
|
||||
if let Expression {
|
||||
expr: Expr::List(values),
|
||||
|
Loading…
Reference in New Issue
Block a user