From a67dad3d159aa531dd44add1928f07114a473ea4 Mon Sep 17 00:00:00 2001 From: Stefan Holderbach Date: Wed, 17 Apr 2024 00:33:50 +0200 Subject: [PATCH] Minor housekeeping in the parser (#12540) - **Move lone `check_name` to the alias place** - **Restrict visibility of `check_call` helper** --- crates/nu-parser/src/parse_keywords.rs | 44 ++++++++++++++++++++++-- crates/nu-parser/src/parser.rs | 47 ++++---------------------- 2 files changed, 48 insertions(+), 43 deletions(-) diff --git a/crates/nu-parser/src/parse_keywords.rs b/crates/nu-parser/src/parse_keywords.rs index 5c770015d9..37ded54647 100644 --- a/crates/nu-parser/src/parse_keywords.rs +++ b/crates/nu-parser/src/parse_keywords.rs @@ -34,7 +34,7 @@ use crate::{ lex, lite_parser::{lite_parse, LiteCommand}, parser::{ - check_call, check_name, garbage, garbage_pipeline, parse, parse_call, parse_expression, + check_call, garbage, garbage_pipeline, parse, parse_call, parse_expression, parse_full_signature, parse_import_pattern, parse_internal_call, parse_multispan_value, parse_string, parse_value, parse_var_with_opt_type, trim_quotes, ParsedInternalCall, }, @@ -803,6 +803,46 @@ pub fn parse_extern( }]) } +fn check_alias_name<'a>(working_set: &mut StateWorkingSet, spans: &'a [Span]) -> Option<&'a Span> { + let command_len = if !spans.is_empty() { + if working_set.get_span_contents(spans[0]) == b"export" { + 2 + } else { + 1 + } + } else { + return None; + }; + + if spans.len() == 1 { + None + } else if spans.len() < command_len + 3 { + if working_set.get_span_contents(spans[command_len]) == b"=" { + let name = + String::from_utf8_lossy(working_set.get_span_contents(span(&spans[..command_len]))); + working_set.error(ParseError::AssignmentMismatch( + format!("{name} missing name"), + "missing name".into(), + spans[command_len], + )); + Some(&spans[command_len]) + } else { + None + } + } else if working_set.get_span_contents(spans[command_len + 1]) != b"=" { + let name = + String::from_utf8_lossy(working_set.get_span_contents(span(&spans[..command_len]))); + working_set.error(ParseError::AssignmentMismatch( + format!("{name} missing sign"), + "missing equal sign".into(), + spans[command_len + 1], + )); + Some(&spans[command_len + 1]) + } else { + None + } +} + pub fn parse_alias( working_set: &mut StateWorkingSet, lite_command: &LiteCommand, @@ -831,7 +871,7 @@ pub fn parse_alias( return garbage_pipeline(spans); } - if let Some(span) = check_name(working_set, spans) { + if let Some(span) = check_alias_name(working_set, spans) { return Pipeline::from_vec(vec![garbage(*span)]); } diff --git a/crates/nu-parser/src/parser.rs b/crates/nu-parser/src/parser.rs index 310d8e44ca..8ea77e6051 100644 --- a/crates/nu-parser/src/parser.rs +++ b/crates/nu-parser/src/parser.rs @@ -148,7 +148,12 @@ pub fn trim_quotes_str(s: &str) -> &str { } } -pub fn check_call(working_set: &mut StateWorkingSet, command: Span, sig: &Signature, call: &Call) { +pub(crate) fn check_call( + working_set: &mut StateWorkingSet, + command: Span, + sig: &Signature, + call: &Call, +) { // Allow the call to pass if they pass in the help flag if call.named_iter().any(|(n, _, _)| n.item == "help") { return; @@ -211,46 +216,6 @@ pub fn check_call(working_set: &mut StateWorkingSet, command: Span, sig: &Signat } } -pub fn check_name<'a>(working_set: &mut StateWorkingSet, spans: &'a [Span]) -> Option<&'a Span> { - let command_len = if !spans.is_empty() { - if working_set.get_span_contents(spans[0]) == b"export" { - 2 - } else { - 1 - } - } else { - return None; - }; - - if spans.len() == 1 { - None - } else if spans.len() < command_len + 3 { - if working_set.get_span_contents(spans[command_len]) == b"=" { - let name = - String::from_utf8_lossy(working_set.get_span_contents(span(&spans[..command_len]))); - working_set.error(ParseError::AssignmentMismatch( - format!("{name} missing name"), - "missing name".into(), - spans[command_len], - )); - Some(&spans[command_len]) - } else { - None - } - } else if working_set.get_span_contents(spans[command_len + 1]) != b"=" { - let name = - String::from_utf8_lossy(working_set.get_span_contents(span(&spans[..command_len]))); - working_set.error(ParseError::AssignmentMismatch( - format!("{name} missing sign"), - "missing equal sign".into(), - spans[command_len + 1], - )); - Some(&spans[command_len + 1]) - } else { - None - } -} - fn parse_external_arg(working_set: &mut StateWorkingSet, span: Span) -> ExternalArgument { let contents = working_set.get_span_contents(span);