mirror of
https://github.com/nushell/nushell.git
synced 2025-04-15 16:58:19 +02:00
better it detection and block params in shapes
This commit is contained in:
parent
eb67eab122
commit
32c1f0c8d4
@ -17,7 +17,11 @@ impl Command for Benchmark {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn signature(&self) -> nu_protocol::Signature {
|
fn signature(&self) -> nu_protocol::Signature {
|
||||||
Signature::build("benchmark").required("block", SyntaxShape::Block, "the block to run")
|
Signature::build("benchmark").required(
|
||||||
|
"block",
|
||||||
|
SyntaxShape::Block(Some(vec![])),
|
||||||
|
"the block to run",
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run(
|
fn run(
|
||||||
|
@ -17,7 +17,11 @@ impl Command for Def {
|
|||||||
Signature::build("def")
|
Signature::build("def")
|
||||||
.required("def_name", SyntaxShape::String, "definition name")
|
.required("def_name", SyntaxShape::String, "definition name")
|
||||||
.required("params", SyntaxShape::Signature, "parameters")
|
.required("params", SyntaxShape::Signature, "parameters")
|
||||||
.required("block", SyntaxShape::Block, "body of the definition")
|
.required(
|
||||||
|
"block",
|
||||||
|
SyntaxShape::Block(Some(vec![])),
|
||||||
|
"body of the definition",
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run(
|
fn run(
|
||||||
|
@ -15,7 +15,11 @@ impl Command for Do {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn signature(&self) -> nu_protocol::Signature {
|
fn signature(&self) -> nu_protocol::Signature {
|
||||||
Signature::build("do").required("block", SyntaxShape::Block, "the block to run")
|
Signature::build("do").required(
|
||||||
|
"block",
|
||||||
|
SyntaxShape::Block(Some(vec![])),
|
||||||
|
"the block to run",
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run(
|
fn run(
|
||||||
|
@ -16,7 +16,11 @@ impl Command for Each {
|
|||||||
|
|
||||||
fn signature(&self) -> nu_protocol::Signature {
|
fn signature(&self) -> nu_protocol::Signature {
|
||||||
Signature::build("each")
|
Signature::build("each")
|
||||||
.required("block", SyntaxShape::Block, "the block to run")
|
.required(
|
||||||
|
"block",
|
||||||
|
SyntaxShape::Block(Some(vec![SyntaxShape::Any])),
|
||||||
|
"the block to run",
|
||||||
|
)
|
||||||
.switch("numbered", "iterate with an index", Some('n'))
|
.switch("numbered", "iterate with an index", Some('n'))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -29,7 +29,11 @@ impl Command for For {
|
|||||||
),
|
),
|
||||||
"range of the loop",
|
"range of the loop",
|
||||||
)
|
)
|
||||||
.required("block", SyntaxShape::Block, "the block to run")
|
.required(
|
||||||
|
"block",
|
||||||
|
SyntaxShape::Block(Some(vec![])),
|
||||||
|
"the block to run",
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run(
|
fn run(
|
||||||
|
@ -17,7 +17,7 @@ impl Command for If {
|
|||||||
fn signature(&self) -> nu_protocol::Signature {
|
fn signature(&self) -> nu_protocol::Signature {
|
||||||
Signature::build("if")
|
Signature::build("if")
|
||||||
.required("cond", SyntaxShape::Expression, "condition")
|
.required("cond", SyntaxShape::Expression, "condition")
|
||||||
.required("then_block", SyntaxShape::Block, "then block")
|
.required("then_block", SyntaxShape::Block(Some(vec![])), "then block")
|
||||||
.optional(
|
.optional(
|
||||||
"else",
|
"else",
|
||||||
SyntaxShape::Keyword(b"else".to_vec(), Box::new(SyntaxShape::Expression)),
|
SyntaxShape::Keyword(b"else".to_vec(), Box::new(SyntaxShape::Expression)),
|
||||||
|
@ -1030,20 +1030,7 @@ pub fn parse_variable_expr(
|
|||||||
None,
|
None,
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
// let name = working_set.get_span_contents(span).to_vec();
|
(garbage(span), Some(ParseError::VariableNotFound(span)))
|
||||||
// this seems okay to set it to unknown here, but we should double-check
|
|
||||||
//let id = working_set.add_variable(name, Type::Unknown);
|
|
||||||
// (
|
|
||||||
// Expression {
|
|
||||||
// expr: Expr::Var(id),
|
|
||||||
// span,
|
|
||||||
// ty: Type::Unknown,
|
|
||||||
// },
|
|
||||||
// None,
|
|
||||||
// )
|
|
||||||
// } else {
|
|
||||||
(garbage(span), err)
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
(garbage(span), err)
|
(garbage(span), err)
|
||||||
@ -1243,7 +1230,7 @@ pub fn parse_shape_name(
|
|||||||
b"int" => SyntaxShape::Int,
|
b"int" => SyntaxShape::Int,
|
||||||
b"path" => SyntaxShape::FilePath,
|
b"path" => SyntaxShape::FilePath,
|
||||||
b"glob" => SyntaxShape::GlobPattern,
|
b"glob" => SyntaxShape::GlobPattern,
|
||||||
b"block" => SyntaxShape::Block,
|
b"block" => SyntaxShape::Block(None), //FIXME
|
||||||
b"cond" => SyntaxShape::RowCondition,
|
b"cond" => SyntaxShape::RowCondition,
|
||||||
b"operator" => SyntaxShape::Operator,
|
b"operator" => SyntaxShape::Operator,
|
||||||
b"math" => SyntaxShape::MathExpression,
|
b"math" => SyntaxShape::MathExpression,
|
||||||
@ -1905,6 +1892,7 @@ pub fn parse_table_expression(
|
|||||||
|
|
||||||
pub fn parse_block_expression(
|
pub fn parse_block_expression(
|
||||||
working_set: &mut StateWorkingSet,
|
working_set: &mut StateWorkingSet,
|
||||||
|
shape: &SyntaxShape,
|
||||||
span: Span,
|
span: Span,
|
||||||
) -> (Expression, Option<ParseError>) {
|
) -> (Expression, Option<ParseError>) {
|
||||||
let bytes = working_set.get_span_contents(span);
|
let bytes = working_set.get_span_contents(span);
|
||||||
@ -1945,7 +1933,7 @@ pub fn parse_block_expression(
|
|||||||
working_set.enter_scope();
|
working_set.enter_scope();
|
||||||
|
|
||||||
// Check to see if we have parameters
|
// Check to see if we have parameters
|
||||||
let (signature, amt_to_skip): (Option<Box<Signature>>, usize) = match output.first() {
|
let (mut signature, amt_to_skip): (Option<Box<Signature>>, usize) = match output.first() {
|
||||||
Some(Token {
|
Some(Token {
|
||||||
contents: TokenContents::Pipe,
|
contents: TokenContents::Pipe,
|
||||||
span,
|
span,
|
||||||
@ -1991,6 +1979,23 @@ pub fn parse_block_expression(
|
|||||||
let (output, err) = lite_parse(&output[amt_to_skip..]);
|
let (output, err) = lite_parse(&output[amt_to_skip..]);
|
||||||
error = error.or(err);
|
error = error.or(err);
|
||||||
|
|
||||||
|
if let SyntaxShape::Block(Some(v)) = shape {
|
||||||
|
if signature.is_none() && v.len() == 1 {
|
||||||
|
// We'll assume there's an `$it` present
|
||||||
|
let var_id = working_set.add_variable(b"$it".to_vec(), Type::Unknown);
|
||||||
|
|
||||||
|
let mut new_sigature = Signature::new("");
|
||||||
|
new_sigature.required_positional.push(PositionalArg {
|
||||||
|
var_id: Some(var_id),
|
||||||
|
name: "$it".into(),
|
||||||
|
desc: String::new(),
|
||||||
|
shape: SyntaxShape::Any,
|
||||||
|
});
|
||||||
|
|
||||||
|
signature = Some(Box::new(new_sigature));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let (mut output, err) = parse_block(working_set, &output, false);
|
let (mut output, err) = parse_block(working_set, &output, false);
|
||||||
error = error.or(err);
|
error = error.or(err);
|
||||||
|
|
||||||
@ -2049,8 +2054,8 @@ pub fn parse_value(
|
|||||||
return parse_full_column_path(working_set, None, span);
|
return parse_full_column_path(working_set, None, span);
|
||||||
}
|
}
|
||||||
} else if bytes.starts_with(b"{") {
|
} else if bytes.starts_with(b"{") {
|
||||||
if matches!(shape, SyntaxShape::Block) || matches!(shape, SyntaxShape::Any) {
|
if matches!(shape, SyntaxShape::Block(_)) || matches!(shape, SyntaxShape::Any) {
|
||||||
return parse_block_expression(working_set, span);
|
return parse_block_expression(working_set, shape, span);
|
||||||
} else {
|
} else {
|
||||||
return (
|
return (
|
||||||
Expression::garbage(span),
|
Expression::garbage(span),
|
||||||
@ -2079,9 +2084,9 @@ pub fn parse_value(
|
|||||||
SyntaxShape::String | SyntaxShape::GlobPattern | SyntaxShape::FilePath => {
|
SyntaxShape::String | SyntaxShape::GlobPattern | SyntaxShape::FilePath => {
|
||||||
parse_string(working_set, span)
|
parse_string(working_set, span)
|
||||||
}
|
}
|
||||||
SyntaxShape::Block => {
|
SyntaxShape::Block(_) => {
|
||||||
if bytes.starts_with(b"{") {
|
if bytes.starts_with(b"{") {
|
||||||
parse_block_expression(working_set, span)
|
parse_block_expression(working_set, shape, span)
|
||||||
} else {
|
} else {
|
||||||
(
|
(
|
||||||
Expression::garbage(span),
|
Expression::garbage(span),
|
||||||
@ -2129,7 +2134,7 @@ pub fn parse_value(
|
|||||||
SyntaxShape::Range,
|
SyntaxShape::Range,
|
||||||
SyntaxShape::Filesize,
|
SyntaxShape::Filesize,
|
||||||
SyntaxShape::Duration,
|
SyntaxShape::Duration,
|
||||||
SyntaxShape::Block,
|
SyntaxShape::Block(None),
|
||||||
SyntaxShape::String,
|
SyntaxShape::String,
|
||||||
];
|
];
|
||||||
for shape in shapes.iter() {
|
for shape in shapes.iter() {
|
||||||
@ -2381,7 +2386,8 @@ pub fn parse_def(
|
|||||||
let (sig, err) = parse_signature(working_set, spans[2]);
|
let (sig, err) = parse_signature(working_set, spans[2]);
|
||||||
error = error.or(err);
|
error = error.or(err);
|
||||||
|
|
||||||
let (block, err) = parse_block_expression(working_set, spans[3]);
|
let (block, err) =
|
||||||
|
parse_block_expression(working_set, &SyntaxShape::Block(Some(vec![])), spans[3]);
|
||||||
error = error.or(err);
|
error = error.or(err);
|
||||||
working_set.exit_scope();
|
working_set.exit_scope();
|
||||||
|
|
||||||
|
@ -34,7 +34,7 @@ pub enum SyntaxShape {
|
|||||||
GlobPattern,
|
GlobPattern,
|
||||||
|
|
||||||
/// A block is allowed, eg `{start this thing}`
|
/// A block is allowed, eg `{start this thing}`
|
||||||
Block(Vec<(Vec<u8>, SyntaxShape)>),
|
Block(Option<Vec<SyntaxShape>>),
|
||||||
|
|
||||||
/// A table is allowed, eg `[[first, second]; [1, 2]]`
|
/// A table is allowed, eg `[[first, second]; [1, 2]]`
|
||||||
Table,
|
Table,
|
||||||
@ -75,7 +75,7 @@ impl SyntaxShape {
|
|||||||
pub fn to_type(&self) -> Type {
|
pub fn to_type(&self) -> Type {
|
||||||
match self {
|
match self {
|
||||||
SyntaxShape::Any => Type::Unknown,
|
SyntaxShape::Any => Type::Unknown,
|
||||||
SyntaxShape::Block => Type::Block,
|
SyntaxShape::Block(_) => Type::Block,
|
||||||
SyntaxShape::CellPath => Type::Unknown,
|
SyntaxShape::CellPath => Type::Unknown,
|
||||||
SyntaxShape::Duration => Type::Duration,
|
SyntaxShape::Duration => Type::Duration,
|
||||||
SyntaxShape::Expression => Type::Unknown,
|
SyntaxShape::Expression => Type::Unknown,
|
||||||
|
@ -308,3 +308,11 @@ fn row_condition2() -> TestResult {
|
|||||||
"1",
|
"1",
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn better_block_types() -> TestResult {
|
||||||
|
run_test(
|
||||||
|
r#"([1, 2, 3] | each -n { $"($it.index) is ($it.item)" }).1"#,
|
||||||
|
"1 is 2",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user