From 8ac9d781fd52b9bbe4aafb9ac06855d8b5ba35c7 Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Fri, 10 Apr 2020 19:56:48 +1200 Subject: [PATCH] Remove source text where not needed (#1567) --- crates/nu-cli/src/cli.rs | 3 +- crates/nu-cli/src/commands/autoview.rs | 4 -- .../src/commands/classified/internal.rs | 4 -- .../src/commands/classified/pipeline.rs | 6 +-- crates/nu-cli/src/commands/command.rs | 12 +---- crates/nu-cli/src/commands/enter.rs | 1 - crates/nu-cli/src/commands/save.rs | 1 - crates/nu-cli/src/commands/sum.rs | 1 - crates/nu-cli/src/context.rs | 21 +++------ crates/nu-cli/src/data/base.rs | 5 +-- crates/nu-cli/src/evaluate/evaluate_args.rs | 10 ++--- crates/nu-cli/src/evaluate/evaluator.rs | 44 +++++++------------ crates/nu-parser/src/hir.rs | 8 ++-- crates/nu-parser/src/shapes.rs | 2 +- 14 files changed, 34 insertions(+), 88 deletions(-) diff --git a/crates/nu-cli/src/cli.rs b/crates/nu-cli/src/cli.rs index c7ec8a5f2c..31acc8b2d1 100644 --- a/crates/nu-cli/src/cli.rs +++ b/crates/nu-cli/src/cli.rs @@ -711,7 +711,7 @@ async fn process_line( None }; - match run_pipeline(pipeline, ctx, input_stream, line).await { + match run_pipeline(pipeline, ctx, input_stream).await { Ok(Some(input)) => { // Running a pipeline gives us back a stream that we can then // work through. At the top level, we just want to pull on the @@ -725,7 +725,6 @@ async fn process_line( ctrl_c: ctx.ctrl_c.clone(), commands: ctx.registry.clone(), name: Tag::unknown(), - source: Text::from(String::new()), }; if let Ok(mut output_stream) = crate::commands::autoview::autoview(context) { diff --git a/crates/nu-cli/src/commands/autoview.rs b/crates/nu-cli/src/commands/autoview.rs index d245643cd2..6017edd779 100644 --- a/crates/nu-cli/src/commands/autoview.rs +++ b/crates/nu-cli/src/commands/autoview.rs @@ -32,7 +32,6 @@ impl WholeStreamCommand for Autoview { commands: registry.clone(), shell_manager: args.shell_manager, host: args.host, - source: args.call_info.source, ctrl_c: args.ctrl_c, name: args.call_info.name_tag, }) @@ -42,7 +41,6 @@ impl WholeStreamCommand for Autoview { pub struct RunnableContextWithoutInput { pub shell_manager: ShellManager, pub host: Arc>>, - pub source: Text, pub ctrl_c: Arc, pub commands: CommandRegistry, pub name: Tag, @@ -53,7 +51,6 @@ impl RunnableContextWithoutInput { let new_context = RunnableContextWithoutInput { shell_manager: context.shell_manager, host: context.host, - source: context.source, ctrl_c: context.ctrl_c, commands: context.commands, name: context.name, @@ -289,7 +286,6 @@ fn create_default_command_args(context: &RunnableContextWithoutInput) -> RawComm named: None, span, }, - source: context.source.clone(), name_tag: context.name.clone(), }, } diff --git a/crates/nu-cli/src/commands/classified/internal.rs b/crates/nu-cli/src/commands/classified/internal.rs index 61872b7559..0e3bcb63c4 100644 --- a/crates/nu-cli/src/commands/classified/internal.rs +++ b/crates/nu-cli/src/commands/classified/internal.rs @@ -9,12 +9,10 @@ pub(crate) fn run_internal_command( command: InternalCommand, context: &mut Context, input: Option, - source: Text, ) -> Result, ShellError> { if log_enabled!(log::Level::Trace) { trace!(target: "nu::run::internal", "->"); trace!(target: "nu::run::internal", "{}", command.name); - trace!(target: "nu::run::internal", "{}", command.args.debug(&source)); } let objects: InputStream = if let Some(input) = input { @@ -30,7 +28,6 @@ pub(crate) fn run_internal_command( internal_command?, Tag::unknown_anchor(command.name_span), command.args.clone(), - &source, objects, ) }; @@ -70,7 +67,6 @@ pub(crate) fn run_internal_command( named: None, span: Span::unknown() }, - source: source.clone(), name_tag: Tag::unknown_anchor(command.name_span), } }; diff --git a/crates/nu-cli/src/commands/classified/pipeline.rs b/crates/nu-cli/src/commands/classified/pipeline.rs index a2f6b4a1f5..a61725b4e0 100644 --- a/crates/nu-cli/src/commands/classified/pipeline.rs +++ b/crates/nu-cli/src/commands/classified/pipeline.rs @@ -4,13 +4,11 @@ use crate::context::Context; use crate::stream::InputStream; use nu_errors::ShellError; use nu_parser::{ClassifiedCommand, ClassifiedPipeline}; -use nu_source::Text; pub(crate) async fn run_pipeline( pipeline: ClassifiedPipeline, ctx: &mut Context, mut input: Option, - line: &str, ) -> Result, ShellError> { let mut iter = pipeline.commands.list.into_iter().peekable(); @@ -29,9 +27,7 @@ pub(crate) async fn run_pipeline( (Some(ClassifiedCommand::Error(err)), _) => return Err(err.into()), (_, Some(ClassifiedCommand::Error(err))) => return Err(err.clone().into()), - (Some(ClassifiedCommand::Internal(left)), _) => { - run_internal_command(left, ctx, input, Text::from(line))? - } + (Some(ClassifiedCommand::Internal(left)), _) => run_internal_command(left, ctx, input)?, (Some(ClassifiedCommand::External(left)), None) => { run_external_command(left, ctx, input, true).await? diff --git a/crates/nu-cli/src/commands/command.rs b/crates/nu-cli/src/commands/command.rs index 17deb04ce2..d0d141fc83 100644 --- a/crates/nu-cli/src/commands/command.rs +++ b/crates/nu-cli/src/commands/command.rs @@ -15,7 +15,6 @@ use std::sync::atomic::AtomicBool; #[derive(Deserialize, Serialize, Debug, Clone)] pub struct UnevaluatedCallInfo { pub args: hir::Call, - pub source: Text, pub name_tag: Tag, } @@ -25,7 +24,7 @@ impl UnevaluatedCallInfo { registry: &CommandRegistry, scope: &Scope, ) -> Result { - let args = evaluate_args(&self.args, registry, scope, &self.source)?; + let args = evaluate_args(&self.args, registry, scope)?; Ok(CallInfo { args, @@ -145,10 +144,6 @@ impl CommandArgs { )) } - pub fn source(&self) -> Text { - self.call_info.source.clone() - } - pub fn process<'de, T: Deserialize<'de>, O: ToOutputStream>( self, registry: &CommandRegistry, @@ -156,7 +151,6 @@ impl CommandArgs { ) -> Result, ShellError> { let shell_manager = self.shell_manager.clone(); let host = self.host.clone(); - let source = self.source(); let ctrl_c = self.ctrl_c.clone(); let args = self.evaluate_once(registry)?; let call_info = args.call_info.clone(); @@ -169,7 +163,6 @@ impl CommandArgs { context: RunnableContext { input, commands: registry.clone(), - source, shell_manager, name: name_tag, host, @@ -193,7 +186,6 @@ impl CommandArgs { let shell_manager = self.shell_manager.clone(); let host = self.host.clone(); - let source = self.source(); let ctrl_c = self.ctrl_c.clone(); let args = self.evaluate_once(registry)?; let call_info = args.call_info.clone(); @@ -207,7 +199,6 @@ impl CommandArgs { context: RunnableContext { input, commands: registry.clone(), - source, shell_manager, name: name_tag, host, @@ -229,7 +220,6 @@ pub struct RunnableContext { pub input: InputStream, pub shell_manager: ShellManager, pub host: Arc>>, - pub source: Text, pub ctrl_c: Arc, pub commands: CommandRegistry, pub name: Tag, diff --git a/crates/nu-cli/src/commands/enter.rs b/crates/nu-cli/src/commands/enter.rs index b8fa2dd933..aebad74322 100644 --- a/crates/nu-cli/src/commands/enter.rs +++ b/crates/nu-cli/src/commands/enter.rs @@ -103,7 +103,6 @@ impl PerItemCommand for Enter { named: None, span: Span::unknown() }, - source: raw_args.call_info.source, name_tag: raw_args.call_info.name_tag, }, }; diff --git a/crates/nu-cli/src/commands/save.rs b/crates/nu-cli/src/commands/save.rs index 0465b40671..660be78ca6 100644 --- a/crates/nu-cli/src/commands/save.rs +++ b/crates/nu-cli/src/commands/save.rs @@ -234,7 +234,6 @@ fn save( named: None, span: Span::unknown() }, - source: raw_args.call_info.source, name_tag: raw_args.call_info.name_tag, } }; diff --git a/crates/nu-cli/src/commands/sum.rs b/crates/nu-cli/src/commands/sum.rs index 11ddb6e4b6..c204fabb1a 100644 --- a/crates/nu-cli/src/commands/sum.rs +++ b/crates/nu-cli/src/commands/sum.rs @@ -30,7 +30,6 @@ impl WholeStreamCommand for Sum { commands: registry.clone(), shell_manager: args.shell_manager, host: args.host, - source: args.call_info.source, ctrl_c: args.ctrl_c, name: args.call_info.name_tag, }) diff --git a/crates/nu-cli/src/context.rs b/crates/nu-cli/src/context.rs index fdaee347c1..739ae2ea65 100644 --- a/crates/nu-cli/src/context.rs +++ b/crates/nu-cli/src/context.rs @@ -176,33 +176,22 @@ impl Context { command: Arc, name_tag: Tag, args: hir::Call, - source: &Text, input: InputStream, ) -> OutputStream { - let command_args = self.command_args(args, input, source, name_tag); + let command_args = self.command_args(args, input, name_tag); command.run(command_args, self.registry()) } - fn call_info(&self, args: hir::Call, source: &Text, name_tag: Tag) -> UnevaluatedCallInfo { - UnevaluatedCallInfo { - args, - source: source.clone(), - name_tag, - } + fn call_info(&self, args: hir::Call, name_tag: Tag) -> UnevaluatedCallInfo { + UnevaluatedCallInfo { args, name_tag } } - fn command_args( - &self, - args: hir::Call, - input: InputStream, - source: &Text, - name_tag: Tag, - ) -> CommandArgs { + fn command_args(&self, args: hir::Call, input: InputStream, name_tag: Tag) -> CommandArgs { CommandArgs { host: self.host.clone(), ctrl_c: self.ctrl_c.clone(), shell_manager: self.shell_manager.clone(), - call_info: self.call_info(args, source, name_tag), + call_info: self.call_info(args, name_tag), input, } } diff --git a/crates/nu-cli/src/data/base.rs b/crates/nu-cli/src/data/base.rs index b00218ccb6..24d6522ab5 100644 --- a/crates/nu-cli/src/data/base.rs +++ b/crates/nu-cli/src/data/base.rs @@ -12,7 +12,7 @@ use nu_protocol::{ Evaluate, EvaluateTrait, Primitive, Scope, ShellTypeName, SpannedTypeName, TaggedDictBuilder, UntaggedValue, Value, }; -use nu_source::{Tag, Text}; +use nu_source::Tag; use nu_value_ext::ValueExt; use num_bigint::BigInt; use num_traits::Zero; @@ -30,7 +30,6 @@ pub struct Operation { #[derive(Debug, Ord, PartialOrd, Eq, PartialEq, Clone, Hash, Serialize, Deserialize, new)] pub struct Block { pub(crate) expressions: Vec, - pub(crate) source: Text, pub(crate) tag: Tag, } @@ -54,7 +53,7 @@ impl EvaluateTrait for Block { ); for expr in self.expressions.iter() { - last = evaluate_baseline_expr(&expr, &CommandRegistry::empty(), &scope, &self.source) + last = evaluate_baseline_expr(&expr, &CommandRegistry::empty(), &scope) } last diff --git a/crates/nu-cli/src/evaluate/evaluate_args.rs b/crates/nu-cli/src/evaluate/evaluate_args.rs index 4f4613770f..632033e97e 100644 --- a/crates/nu-cli/src/evaluate/evaluate_args.rs +++ b/crates/nu-cli/src/evaluate/evaluate_args.rs @@ -5,20 +5,18 @@ use indexmap::IndexMap; use nu_errors::ShellError; use nu_parser::hir; use nu_protocol::{EvaluatedArgs, Scope, UntaggedValue, Value}; -use nu_source::Text; pub(crate) fn evaluate_args( call: &hir::Call, registry: &CommandRegistry, scope: &Scope, - source: &Text, ) -> Result { let positional: Result>, _> = call .positional .as_ref() .map(|p| { p.iter() - .map(|e| evaluate_baseline_expr(e, registry, scope, source)) + .map(|e| evaluate_baseline_expr(e, registry, scope)) .collect() }) .transpose(); @@ -37,10 +35,8 @@ pub(crate) fn evaluate_args( results.insert(name.clone(), UntaggedValue::boolean(true).into_value(tag)); } hir::NamedValue::Value(_, expr) => { - results.insert( - name.clone(), - evaluate_baseline_expr(expr, registry, scope, source)?, - ); + results + .insert(name.clone(), evaluate_baseline_expr(expr, registry, scope)?); } _ => {} diff --git a/crates/nu-cli/src/evaluate/evaluator.rs b/crates/nu-cli/src/evaluate/evaluator.rs index 9ca7b95b80..e451b3c1a2 100644 --- a/crates/nu-cli/src/evaluate/evaluator.rs +++ b/crates/nu-cli/src/evaluate/evaluator.rs @@ -9,20 +9,18 @@ use nu_protocol::{ ColumnPath, Evaluate, Primitive, RangeInclusion, Scope, UnspannedPathMember, UntaggedValue, Value, }; -use nu_source::Text; pub(crate) fn evaluate_baseline_expr( expr: &SpannedExpression, registry: &CommandRegistry, scope: &Scope, - source: &Text, ) -> Result { let tag = Tag { span: expr.span, anchor: None, }; match &expr.expr { - Expression::Literal(literal) => Ok(evaluate_literal(literal, expr.span, source)), + Expression::Literal(literal) => Ok(evaluate_literal(literal, expr.span)), Expression::ExternalWord => Err(ShellError::argument_error( "Invalid external word".spanned(tag.span), ArgumentError::InvalidExternalWord, @@ -31,12 +29,12 @@ pub(crate) fn evaluate_baseline_expr( Expression::Synthetic(hir::Synthetic::String(s)) => { Ok(UntaggedValue::string(s).into_untagged_value()) } - Expression::Variable(var) => evaluate_reference(var, scope, source, tag), - Expression::Command(_) => evaluate_command(tag, scope, source), - Expression::ExternalCommand(external) => evaluate_external(external, scope, source), + Expression::Variable(var) => evaluate_reference(var, scope, tag), + Expression::Command(_) => evaluate_command(tag, scope), + Expression::ExternalCommand(external) => evaluate_external(external, scope), Expression::Binary(binary) => { - let left = evaluate_baseline_expr(&binary.left, registry, scope, source)?; - let right = evaluate_baseline_expr(&binary.right, registry, scope, source)?; + let left = evaluate_baseline_expr(&binary.left, registry, scope)?; + let right = evaluate_baseline_expr(&binary.right, registry, scope)?; trace!("left={:?} right={:?}", left.value, right.value); @@ -57,8 +55,8 @@ pub(crate) fn evaluate_baseline_expr( let left = &range.left; let right = &range.right; - let left = evaluate_baseline_expr(left, registry, scope, source)?; - let right = evaluate_baseline_expr(right, registry, scope, source)?; + let left = evaluate_baseline_expr(left, registry, scope)?; + let right = evaluate_baseline_expr(right, registry, scope)?; let left_span = left.tag.span; let right_span = right.tag.span; @@ -77,7 +75,7 @@ pub(crate) fn evaluate_baseline_expr( let mut exprs = vec![]; for expr in list { - let expr = evaluate_baseline_expr(expr, registry, scope, source)?; + let expr = evaluate_baseline_expr(expr, registry, scope)?; exprs.push(expr); } @@ -85,12 +83,11 @@ pub(crate) fn evaluate_baseline_expr( } Expression::Block(block) => Ok(UntaggedValue::Block(Evaluate::new(Block::new( block.clone(), - source.clone(), tag.clone(), ))) .into_value(&tag)), Expression::Path(path) => { - let value = evaluate_baseline_expr(&path.head, registry, scope, source)?; + let value = evaluate_baseline_expr(&path.head, registry, scope)?; let mut item = value; for member in &path.tail { @@ -132,7 +129,7 @@ pub(crate) fn evaluate_baseline_expr( } } -fn evaluate_literal(literal: &hir::Literal, span: Span, source: &Text) -> Value { +fn evaluate_literal(literal: &hir::Literal, span: Span) -> Value { match &literal { hir::Literal::ColumnPath(path) => { let members = path.iter().map(|member| member.to_path_member()).collect(); @@ -149,21 +146,16 @@ fn evaluate_literal(literal: &hir::Literal, span: Span, source: &Text) -> Value hir::Literal::Size(int, unit) => unit.compute(&int).into_value(span), hir::Literal::String(string) => UntaggedValue::string(string).into_value(span), hir::Literal::GlobPattern(pattern) => UntaggedValue::pattern(pattern).into_value(span), - hir::Literal::Bare => UntaggedValue::string(span.slice(source)).into_value(span), + hir::Literal::Bare(bare) => UntaggedValue::string(bare.clone()).into_value(span), hir::Literal::Operator(_) => unimplemented!("Not sure what to do with operator yet"), } } -fn evaluate_reference( - name: &hir::Variable, - scope: &Scope, - source: &Text, - tag: Tag, -) -> Result { +fn evaluate_reference(name: &hir::Variable, scope: &Scope, tag: Tag) -> Result { trace!("Evaluating {:?} with Scope {:?}", name, scope); match name { hir::Variable::It(_) => Ok(scope.it.value.clone().into_value(tag)), - hir::Variable::Other(_, span) => match span.slice(source) { + hir::Variable::Other(name, _) => match name { x if x == "$nu" => crate::evaluate::variables::nu(tag), x => Ok(scope .vars @@ -174,17 +166,13 @@ fn evaluate_reference( } } -fn evaluate_external( - external: &hir::ExternalCommand, - _scope: &Scope, - _source: &Text, -) -> Result { +fn evaluate_external(external: &hir::ExternalCommand, _scope: &Scope) -> Result { Err(ShellError::syntax_error( "Unexpected external command".spanned(external.name.span), )) } -fn evaluate_command(tag: Tag, _scope: &Scope, _source: &Text) -> Result { +fn evaluate_command(tag: Tag, _scope: &Scope) -> Result { Err(ShellError::syntax_error( "Unexpected command".spanned(tag.span), )) diff --git a/crates/nu-parser/src/hir.rs b/crates/nu-parser/src/hir.rs index 4ca17f7cdc..a4947b530d 100644 --- a/crates/nu-parser/src/hir.rs +++ b/crates/nu-parser/src/hir.rs @@ -462,7 +462,7 @@ pub enum Literal { String(String), GlobPattern(String), ColumnPath(Vec), - Bare, + Bare(String), } impl Literal { @@ -488,7 +488,7 @@ impl ShellTypeName for Literal { Literal::Size(..) => "size", Literal::String(..) => "string", Literal::ColumnPath(..) => "column path", - Literal::Bare => "string", + Literal::Bare(_) => "string", Literal::GlobPattern(_) => "pattern", Literal::Operator(_) => "operator", } @@ -507,7 +507,7 @@ impl PrettyDebugWithSource for SpannedLiteral { Literal::ColumnPath(path) => { b::intersperse_with_source(path.iter(), b::space(), source) } - Literal::Bare => b::delimit("b\"", b::primitive(self.span.slice(source)), "\""), + Literal::Bare(bare) => b::delimit("b\"", b::primitive(bare), "\""), Literal::Operator(operator) => b::primitive(format!("{:?}", operator)), }, } @@ -528,7 +528,7 @@ impl PrettyDebugWithSource for SpannedLiteral { "column path", b::intersperse_with_source(path.iter(), b::space(), source), ), - Literal::Bare => b::typed("bare", b::primitive(self.span.slice(source))), + Literal::Bare(bare) => b::typed("bare", b::primitive(bare)), Literal::Operator(operator) => { b::typed("operator", b::primitive(format!("{:?}", operator))) } diff --git a/crates/nu-parser/src/shapes.rs b/crates/nu-parser/src/shapes.rs index c2ff44ded1..1ad3b59205 100644 --- a/crates/nu-parser/src/shapes.rs +++ b/crates/nu-parser/src/shapes.rs @@ -33,7 +33,7 @@ pub fn expression_to_flat_shape(e: &SpannedExpression) -> Vec output } Expression::Command(command) => vec![FlatShape::InternalCommand.spanned(*command)], - Expression::Literal(Literal::Bare) => vec![FlatShape::BareMember.spanned(e.span)], + Expression::Literal(Literal::Bare(_)) => vec![FlatShape::BareMember.spanned(e.span)], Expression::Literal(Literal::ColumnPath(_)) => vec![FlatShape::Path.spanned(e.span)], Expression::Literal(Literal::GlobPattern(_)) => { vec![FlatShape::GlobPattern.spanned(e.span)]