mirror of
https://github.com/nushell/nushell.git
synced 2025-07-07 18:07:02 +02:00
Custom command input/output types (#9690)
# Description This adds input/output types to custom commands. These are input/output pairs that related an input type to an output type. For example (a single int-to-int input/output pair): ``` def foo []: int -> int { ... } ``` You can also have multiple input/output pairs: ``` def bar []: [int -> string, string -> list<string>] { ... } ``` These types are checked during definition time in the parser. If the block does not match the type, the user will get a parser error. This `:` to begin the input/output signatures should immediately follow the argument signature as shown above. The PR also improves type parsing by re-using the shape parser. The shape parser is now the canonical way to parse types/shapes in user code. This PR also splits `extern` into `extern`/`extern-wrapped` because of the parser limitation that a multi-span argument (which Signature now is) can't precede an optional argument. `extern-wrapped` now takes the required block that was previously optional. # User-Facing Changes The change to `extern` to split into `extern` and `extern-wrapped` is a breaking change. # Tests + Formatting <!-- Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect -A clippy::result_large_err` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass - `cargo run -- -c "use std testing; testing run-tests --path crates/nu-std"` to run the tests for the standard library > **Note** > from `nushell` you can also use the `toolkit` as follows > ```bash > use toolkit.nu # or use an `env_change` hook to activate it automatically > toolkit check pr > ``` --> # After Submitting <!-- If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date. -->
This commit is contained in:
@ -682,6 +682,14 @@ pub fn parse_multispan_value(
|
||||
|
||||
arg
|
||||
}
|
||||
SyntaxShape::Signature => {
|
||||
trace!("parsing: signature");
|
||||
|
||||
let sig = parse_full_signature(working_set, &spans[*spans_idx..]);
|
||||
*spans_idx = spans.len() - 1;
|
||||
|
||||
sig
|
||||
}
|
||||
SyntaxShape::Keyword(keyword, arg) => {
|
||||
trace!(
|
||||
"parsing: keyword({}) {:?}",
|
||||
@ -2932,36 +2940,7 @@ fn prepare_inner_span(
|
||||
}
|
||||
|
||||
pub fn parse_type(working_set: &mut StateWorkingSet, bytes: &[u8], span: Span) -> Type {
|
||||
match bytes {
|
||||
b"binary" => Type::Binary,
|
||||
b"block" => {
|
||||
working_set.error(ParseError::LabeledErrorWithHelp {
|
||||
error: "Blocks are not support as first-class values".into(),
|
||||
label: "blocks are not supported as values".into(),
|
||||
help: "Use 'closure' instead of 'block'".into(),
|
||||
span,
|
||||
});
|
||||
|
||||
Type::Any
|
||||
}
|
||||
b"bool" => Type::Bool,
|
||||
b"cellpath" => Type::CellPath,
|
||||
b"closure" => Type::Closure,
|
||||
b"date" => Type::Date,
|
||||
b"duration" => Type::Duration,
|
||||
b"error" => Type::Error,
|
||||
b"filesize" => Type::Filesize,
|
||||
b"float" | b"decimal" => Type::Float,
|
||||
b"int" => Type::Int,
|
||||
b"list" => Type::List(Box::new(Type::Any)),
|
||||
b"number" => Type::Number,
|
||||
b"range" => Type::Range,
|
||||
b"record" => Type::Record(vec![]),
|
||||
b"string" => Type::String,
|
||||
b"table" => Type::Table(vec![]), //FIXME
|
||||
|
||||
_ => Type::Any,
|
||||
}
|
||||
parse_shape_name(working_set, bytes, span).to_type()
|
||||
}
|
||||
|
||||
pub fn parse_import_pattern(working_set: &mut StateWorkingSet, spans: &[Span]) -> Expression {
|
||||
@ -3199,6 +3178,95 @@ pub fn expand_to_cell_path(
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse_input_output_types(
|
||||
working_set: &mut StateWorkingSet,
|
||||
spans: &[Span],
|
||||
) -> Vec<(Type, Type)> {
|
||||
let mut full_span = span(spans);
|
||||
|
||||
let mut bytes = working_set.get_span_contents(full_span);
|
||||
|
||||
if bytes.starts_with(b"[") {
|
||||
bytes = &bytes[1..];
|
||||
full_span.start += 1;
|
||||
}
|
||||
|
||||
if bytes.ends_with(b"]") {
|
||||
bytes = &bytes[..(bytes.len() - 1)];
|
||||
full_span.end -= 1;
|
||||
}
|
||||
|
||||
let (tokens, parse_error) = lex(bytes, full_span.start, &[b','], &[], true);
|
||||
|
||||
if let Some(parse_error) = parse_error {
|
||||
working_set.parse_errors.push(parse_error);
|
||||
}
|
||||
|
||||
let mut output = vec![];
|
||||
|
||||
let mut idx = 0;
|
||||
while idx < tokens.len() {
|
||||
let type_bytes = working_set.get_span_contents(tokens[idx].span).to_vec();
|
||||
let input_type = parse_type(working_set, &type_bytes, tokens[idx].span);
|
||||
|
||||
idx += 1;
|
||||
if idx >= tokens.len() {
|
||||
working_set.error(ParseError::Expected(
|
||||
"arrow (->)",
|
||||
Span::new(tokens[idx - 1].span.end, tokens[idx - 1].span.end),
|
||||
));
|
||||
break;
|
||||
}
|
||||
|
||||
let arrow = working_set.get_span_contents(tokens[idx].span);
|
||||
if arrow != b"->" {
|
||||
working_set.error(ParseError::Expected("arrow (->)", tokens[idx].span));
|
||||
}
|
||||
|
||||
idx += 1;
|
||||
if idx >= tokens.len() {
|
||||
working_set.error(ParseError::MissingType(Span::new(
|
||||
tokens[idx - 1].span.end,
|
||||
tokens[idx - 1].span.end,
|
||||
)));
|
||||
break;
|
||||
}
|
||||
|
||||
let type_bytes = working_set.get_span_contents(tokens[idx].span).to_vec();
|
||||
let output_type = parse_type(working_set, &type_bytes, tokens[idx].span);
|
||||
|
||||
output.push((input_type, output_type));
|
||||
|
||||
idx += 1;
|
||||
}
|
||||
|
||||
output
|
||||
}
|
||||
|
||||
pub fn parse_full_signature(working_set: &mut StateWorkingSet, spans: &[Span]) -> Expression {
|
||||
let arg_signature = working_set.get_span_contents(spans[0]);
|
||||
|
||||
if arg_signature.ends_with(b":") {
|
||||
let mut arg_signature =
|
||||
parse_signature(working_set, Span::new(spans[0].start, spans[0].end - 1));
|
||||
|
||||
let input_output_types = parse_input_output_types(working_set, &spans[1..]);
|
||||
|
||||
if let Expression {
|
||||
expr: Expr::Signature(sig),
|
||||
span: expr_span,
|
||||
..
|
||||
} = &mut arg_signature
|
||||
{
|
||||
sig.input_output_types = input_output_types;
|
||||
expr_span.end = span(&spans[1..]).end;
|
||||
}
|
||||
arg_signature
|
||||
} else {
|
||||
parse_signature(working_set, spans[0])
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse_row_condition(working_set: &mut StateWorkingSet, spans: &[Span]) -> Expression {
|
||||
let var_id = working_set.add_variable(b"$it".to_vec(), span(spans), Type::Any, false);
|
||||
let expression = parse_math_expression(working_set, spans, Some(var_id));
|
||||
@ -5026,8 +5094,8 @@ pub fn parse_expression(
|
||||
|
||||
// For now, check for special parses of certain keywords
|
||||
match bytes.as_slice() {
|
||||
b"def" | b"extern" | b"for" | b"module" | b"use" | b"source" | b"alias" | b"export"
|
||||
| b"hide" => {
|
||||
b"def" | b"extern" | b"extern-wrapped" | b"for" | b"module" | b"use" | b"source"
|
||||
| b"alias" | b"export" | b"hide" => {
|
||||
working_set.error(ParseError::BuiltinCommandInPipeline(
|
||||
String::from_utf8(bytes)
|
||||
.expect("builtin commands bytes should be able to convert to string"),
|
||||
@ -5194,7 +5262,7 @@ pub fn parse_builtin_commands(
|
||||
|
||||
match name {
|
||||
b"def" | b"def-env" => parse_def(working_set, lite_command, None),
|
||||
b"extern" => parse_extern(working_set, lite_command, None),
|
||||
b"extern" | b"extern-wrapped" => parse_extern(working_set, lite_command, None),
|
||||
b"let" => parse_let(working_set, &lite_command.parts),
|
||||
b"const" => parse_const(working_set, &lite_command.parts),
|
||||
b"mut" => parse_mut(working_set, &lite_command.parts),
|
||||
@ -5582,7 +5650,10 @@ pub fn parse_block(
|
||||
|
||||
block.span = Some(span);
|
||||
|
||||
type_check::check_block_input_output(working_set, &block);
|
||||
let errors = type_check::check_block_input_output(working_set, &block);
|
||||
if !errors.is_empty() {
|
||||
working_set.parse_errors.extend_from_slice(&errors);
|
||||
}
|
||||
|
||||
block
|
||||
}
|
||||
|
Reference in New Issue
Block a user