Skip to content

Commit

Permalink
Remove shape-directed import pattern parsing (nushell#7570)
Browse files Browse the repository at this point in the history
  • Loading branch information
kubouch authored Dec 22, 2022
1 parent 74656bf commit 23a5c5d
Show file tree
Hide file tree
Showing 12 changed files with 121 additions and 81 deletions.
1 change: 1 addition & 0 deletions crates/nu-cli/src/completions/custom_completions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ impl Completer for CustomCompletion {
],
redirect_stdout: true,
redirect_stderr: true,
parser_info: vec![],
},
PipelineData::empty(),
);
Expand Down
7 changes: 6 additions & 1 deletion crates/nu-command/src/core_commands/export_use.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ impl Command for ExportUse {
fn signature(&self) -> nu_protocol::Signature {
Signature::build("export use")
.input_output_types(vec![(Type::Nothing, Type::Nothing)])
.required("pattern", SyntaxShape::ImportPattern, "import pattern")
.required("module", SyntaxShape::String, "Module or module file")
.optional(
"members",
SyntaxShape::Any,
"Which members of the module to import",
)
.category(Category::Core)
}

Expand Down
9 changes: 7 additions & 2 deletions crates/nu-command/src/core_commands/hide.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ impl Command for Hide {
fn signature(&self) -> nu_protocol::Signature {
Signature::build("hide")
.input_output_types(vec![(Type::Nothing, Type::Nothing)])
.required("pattern", SyntaxShape::ImportPattern, "import pattern")
.required("module", SyntaxShape::String, "Module or module file")
.optional(
"members",
SyntaxShape::Any,
"Which members of the module to import",
)
.category(Category::Core)
}

Expand Down Expand Up @@ -44,7 +49,7 @@ This command is a parser keyword. For details, check:
let env_var_name = if let Some(Expression {
expr: Expr::ImportPattern(pat),
..
}) = call.positional_nth(0)
}) = call.parser_info_nth(0)
{
Spanned {
item: String::from_utf8_lossy(&pat.head.name).to_string(),
Expand Down
2 changes: 1 addition & 1 deletion crates/nu-command/src/core_commands/overlay/use_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl Command for OverlayUse {
let mut name_arg: Spanned<String> = call.req(engine_state, caller_stack, 0)?;
name_arg.item = trim_quotes_str(&name_arg.item).to_string();

let maybe_origin_module_id = if let Some(overlay_expr) = call.positional_nth(0) {
let maybe_origin_module_id = if let Some(overlay_expr) = call.parser_info_nth(0) {
if let Expr::Overlay(module_id) = overlay_expr.expr {
module_id
} else {
Expand Down
9 changes: 7 additions & 2 deletions crates/nu-command/src/core_commands/use_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ impl Command for Use {
fn signature(&self) -> nu_protocol::Signature {
Signature::build("use")
.input_output_types(vec![(Type::Nothing, Type::Nothing)])
.required("pattern", SyntaxShape::ImportPattern, "import pattern")
.required("module", SyntaxShape::String, "Module or module file")
.optional(
"members",
SyntaxShape::Any,
"Which members of the module to import",
)
.category(Category::Core)
}

Expand All @@ -43,7 +48,7 @@ impl Command for Use {
let import_pattern = if let Some(Expression {
expr: Expr::ImportPattern(pat),
..
}) = call.positional_nth(0)
}) = call.parser_info_nth(0)
{
pat
} else {
Expand Down
2 changes: 1 addition & 1 deletion crates/nu-command/src/deprecated/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl Command for Source {
) -> Result<PipelineData, ShellError> {
// Note: this hidden positional is the block_id that corresponded to the 0th position
// it is put here by the parser
let block_id: i64 = call.req(engine_state, stack, 1)?;
let block_id: i64 = call.req_parser_info(engine_state, stack, 0)?;

let block = engine_state.get_block(block_id as usize).clone();
eval_block(
Expand Down
2 changes: 1 addition & 1 deletion crates/nu-command/src/env/source_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl Command for SourceEnv {

// Note: this hidden positional is the block_id that corresponded to the 0th position
// it is put here by the parser
let block_id: i64 = call.req(engine_state, caller_stack, 1)?;
let block_id: i64 = call.req_parser_info(engine_state, caller_stack, 0)?;

// Set the currently evaluated directory (file-relative PWD)
let mut parent = if let Some(path) =
Expand Down
26 changes: 26 additions & 0 deletions crates/nu-engine/src/call_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ pub trait CallExt {
stack: &mut Stack,
pos: usize,
) -> Result<T, ShellError>;

fn req_parser_info<T: FromValue>(
&self,
engine_state: &EngineState,
stack: &mut Stack,
pos: usize,
) -> Result<T, ShellError>;
}

impl CallExt for Call {
Expand Down Expand Up @@ -99,4 +106,23 @@ impl CallExt for Call {
))
}
}

fn req_parser_info<T: FromValue>(
&self,
engine_state: &EngineState,
stack: &mut Stack,
pos: usize,
) -> Result<T, ShellError> {
if let Some(expr) = self.parser_info_nth(pos) {
let result = eval_expression(engine_state, stack, expr)?;
FromValue::from_value(&result)
} else if self.parser_info.is_empty() {
Err(ShellError::AccessEmptyContent(self.head))
} else {
Err(ShellError::AccessBeyondEnd(
self.parser_info.len() - 1,
self.head,
))
}
}
}
1 change: 1 addition & 0 deletions crates/nu-engine/src/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,7 @@ pub fn eval_element_with_input(
],
redirect_stdout: false,
redirect_stderr: false,
parser_info: vec![],
},
input,
)
Expand Down
Loading

0 comments on commit 23a5c5d

Please sign in to comment.