Canonical expr block (#1584)

* Add the canonical form for an expression block

* Remove commented out code
This commit is contained in:
Jonathan Turner
2020-04-14 06:59:33 +12:00
committed by GitHub
parent 08a09e2273
commit e3da037b80
5 changed files with 102 additions and 21 deletions

View File

@ -459,7 +459,7 @@ fn parse_arg(
),
}
}
SyntaxShape::Block => {
SyntaxShape::Block | SyntaxShape::Condition => {
// Blocks have one of two forms: the literal block and the implied block
// To parse a literal block, we need to detect that what we have is itself a block
let mut chars = lite_arg.item.chars();
@ -496,14 +496,6 @@ fn parse_arg(
}
}
}
SyntaxShape::Condition => {
// We have an implied condition, but we can't parse this here
// it needed to have been parsed up higher where we have control over more than one arg
(
garbage(lite_arg.span),
Some(ParseError::mismatch("condition", lite_arg.clone())),
)
}
}
}
@ -604,19 +596,19 @@ fn classify_positional_arg(
commands.push(ClassifiedCommand::Expr(Box::new(binary)));
SpannedExpression::new(Expression::Block(commands), span)
} else if idx < lite_cmd.args.len() {
let (arg, err) =
parse_arg(SyntaxShape::FullColumnPath, registry, &lite_cmd.args[idx]);
if error.is_none() {
error = err;
}
arg
} else {
// TODO - this needs to get fixed and return an actual error
let (arg, err) = parse_arg(SyntaxShape::Condition, registry, &lite_cmd.args[idx]);
if error.is_none() {
error = err;
}
arg
} else {
if error.is_none() {
error = Some(ParseError::argument_error(
lite_cmd.name.clone(),
ArgumentError::MissingMandatoryPositional("condition".into()),
))
}
garbage(lite_cmd.span())
}
}
PositionalType::Mandatory(_, shape) => {
@ -806,6 +798,42 @@ pub fn classify_pipeline(
span: Span::new(0, 0),
},
}))
} else if lite_cmd.name.item == "=" {
let idx = 0;
let expr = if (idx + 2) < lite_cmd.args.len() {
let (lhs, err) = parse_arg(SyntaxShape::Any, registry, &lite_cmd.args[idx]);
if error.is_none() {
error = err;
}
let (op, err) = parse_arg(SyntaxShape::Operator, registry, &lite_cmd.args[idx + 1]);
if error.is_none() {
error = err;
}
let (rhs, err) = parse_arg(SyntaxShape::Any, registry, &lite_cmd.args[idx + 2]);
if error.is_none() {
error = err;
}
let span = Span::new(lhs.span.start(), rhs.span.end());
SpannedExpression::new(
Expression::Binary(Box::new(Binary::new(lhs, op, rhs))),
span,
)
} else if idx < lite_cmd.args.len() {
let (arg, err) = parse_arg(SyntaxShape::Any, registry, &lite_cmd.args[idx]);
if error.is_none() {
error = err;
}
arg
} else {
if error.is_none() {
error = Some(ParseError::argument_error(
lite_cmd.name.clone(),
ArgumentError::MissingMandatoryPositional("an expression".into()),
))
}
garbage(lite_cmd.span())
};
commands.push(ClassifiedCommand::Expr(Box::new(expr)))
} else if let Some(signature) = registry.get(&lite_cmd.name.item) {
let (internal_command, err) =
classify_internal_command(&lite_cmd, registry, &signature);