diff --git a/crates/nu-parser/src/errors.rs b/crates/nu-parser/src/errors.rs index a2425e9395..e71c948627 100644 --- a/crates/nu-parser/src/errors.rs +++ b/crates/nu-parser/src/errors.rs @@ -122,35 +122,15 @@ pub enum ParseError { )] BuiltinCommandInPipeline(String, #[label("not allowed in pipeline")] Span), - #[error("Let statement used in pipeline.")] + #[error("{0} statement used in pipeline.")] #[diagnostic( code(nu::parser::unexpected_keyword), url(docsrs), help( - "Assigning '{0}' to '{1}' does not produce a value to be piped. If the pipeline result is meant to be assigned to '{1}', use 'let {1} = ({0} | ...)'." + "Assigning '{1}' to '{2}' does not produce a value to be piped. If the pipeline result is meant to be assigned to '{2}', use '{0} {2} = ({1} | ...)'." ) )] - LetInPipeline(String, String, #[label("let in pipeline")] Span), - - #[error("Const statement used in pipeline.")] - #[diagnostic( - code(nu::parser::unexpected_keyword), - url(docsrs), - help( - "Assigning '{0}' to '{1}' does not produce a value to be piped. If the pipeline result is meant to be assigned to '{1}', use 'const {1} = ({0} | ...)'." - ) - )] - ConstInPipeline(String, String, #[label("const in pipeline")] Span), - - #[error("Mut statement used in pipeline.")] - #[diagnostic( - code(nu::parser::unexpected_keyword), - url(docsrs), - help( - "Assigning '{0}' to '{1}' does not produce a value to be piped. If the pipeline result is meant to be assigned to '{1}', use 'mut {1} = ({0} | ...)'." - ) - )] - MutInPipeline(String, String, #[label("mut in pipeline")] Span), + AssignInPipeline(String, String, String, #[label("'{0}' in pipeline")] Span), #[error("Let used with builtin variable name.")] #[diagnostic( @@ -465,9 +445,7 @@ impl ParseError { ParseError::ExpectedKeyword(_, s) => *s, ParseError::UnexpectedKeyword(_, s) => *s, ParseError::BuiltinCommandInPipeline(_, s) => *s, - ParseError::LetInPipeline(_, _, s) => *s, - ParseError::MutInPipeline(_, _, s) => *s, - ParseError::ConstInPipeline(_, _, s) => *s, + ParseError::AssignInPipeline(_, _, _, s) => *s, ParseError::LetBuiltinVar(_, s) => *s, ParseError::MutBuiltinVar(_, s) => *s, ParseError::ConstBuiltinVar(_, s) => *s, diff --git a/crates/nu-parser/src/parser.rs b/crates/nu-parser/src/parser.rs index 302d11bc4b..43c32661cd 100644 --- a/crates/nu-parser/src/parser.rs +++ b/crates/nu-parser/src/parser.rs @@ -5015,22 +5015,12 @@ pub fn parse_expression( { parse_math_expression(working_set, &spans[pos..], None, expand_aliases_denylist) } else { - let bytes = working_set.get_span_contents(spans[pos]); + let bytes = working_set.get_span_contents(spans[pos]).to_vec(); // For now, check for special parses of certain keywords - match bytes { - b"def" => ( - parse_call( - working_set, - &spans[pos..], - spans[0], - expand_aliases_denylist, - is_subexpression, - ) - .0, - Some(ParseError::BuiltinCommandInPipeline("def".into(), spans[0])), - ), - b"extern" => ( + match bytes.as_slice() { + b"def" | b"extern" | b"for" | b"module" | b"use" | b"source" | b"alias" | b"export" + | b"hide" => ( parse_call( working_set, &spans[pos..], @@ -5040,11 +5030,12 @@ pub fn parse_expression( ) .0, Some(ParseError::BuiltinCommandInPipeline( - "extern".into(), + String::from_utf8(bytes) + .expect("builtin commands bytes should be able to convert to string"), spans[0], )), ), - b"for" => ( + b"let" | b"const" | b"mut" => ( parse_call( working_set, &spans[pos..], @@ -5053,18 +5044,9 @@ pub fn parse_expression( is_subexpression, ) .0, - Some(ParseError::BuiltinCommandInPipeline("for".into(), spans[0])), - ), - b"let" => ( - parse_call( - working_set, - &spans[pos..], - spans[0], - expand_aliases_denylist, - is_subexpression, - ) - .0, - Some(ParseError::LetInPipeline( + Some(ParseError::AssignInPipeline( + String::from_utf8(bytes) + .expect("builtin commands bytes should be able to convert to string"), String::from_utf8_lossy(match spans.len() { 1 | 2 | 3 => b"value", _ => working_set.get_span_contents(spans[3]), @@ -5078,91 +5060,6 @@ pub fn parse_expression( spans[0], )), ), - b"const" => ( - parse_call( - working_set, - &spans[pos..], - spans[0], - expand_aliases_denylist, - is_subexpression, - ) - .0, - Some(ParseError::ConstInPipeline( - String::from_utf8_lossy(match spans.len() { - 1 | 2 | 3 => b"value", - _ => working_set.get_span_contents(spans[3]), - }) - .to_string(), - String::from_utf8_lossy(match spans.len() { - 1 => b"variable", - _ => working_set.get_span_contents(spans[1]), - }) - .to_string(), - spans[0], - )), - ), - b"mut" => ( - parse_call( - working_set, - &spans[pos..], - spans[0], - expand_aliases_denylist, - is_subexpression, - ) - .0, - Some(ParseError::MutInPipeline( - String::from_utf8_lossy(match spans.len() { - 1 | 2 | 3 => b"value", - _ => working_set.get_span_contents(spans[3]), - }) - .to_string(), - String::from_utf8_lossy(match spans.len() { - 1 => b"variable", - _ => working_set.get_span_contents(spans[1]), - }) - .to_string(), - spans[0], - )), - ), - b"alias" => ( - parse_call( - working_set, - &spans[pos..], - spans[0], - expand_aliases_denylist, - is_subexpression, - ) - .0, - Some(ParseError::BuiltinCommandInPipeline( - "alias".into(), - spans[0], - )), - ), - b"module" => ( - parse_call( - working_set, - &spans[pos..], - spans[0], - expand_aliases_denylist, - is_subexpression, - ) - .0, - Some(ParseError::BuiltinCommandInPipeline( - "module".into(), - spans[0], - )), - ), - b"use" => ( - parse_call( - working_set, - &spans[pos..], - spans[0], - expand_aliases_denylist, - is_subexpression, - ) - .0, - Some(ParseError::BuiltinCommandInPipeline("use".into(), spans[0])), - ), b"overlay" => { if spans.len() > 1 && working_set.get_span_contents(spans[1]) == b"list" { // whitelist 'overlay list' @@ -5190,45 +5087,6 @@ pub fn parse_expression( ) } } - b"source" => ( - parse_call( - working_set, - &spans[pos..], - spans[0], - expand_aliases_denylist, - is_subexpression, - ) - .0, - Some(ParseError::BuiltinCommandInPipeline( - "source".into(), - spans[0], - )), - ), - b"export" => ( - parse_call( - working_set, - &spans[pos..], - spans[0], - expand_aliases_denylist, - is_subexpression, - ) - .0, - Some(ParseError::UnexpectedKeyword("export".into(), spans[0])), - ), - b"hide" => ( - parse_call( - working_set, - &spans[pos..], - spans[0], - expand_aliases_denylist, - is_subexpression, - ) - .0, - Some(ParseError::BuiltinCommandInPipeline( - "hide".into(), - spans[0], - )), - ), b"where" => parse_where_expr(working_set, &spans[pos..], expand_aliases_denylist), #[cfg(feature = "plugin")] b"register" => (