mirror of
https://github.com/nushell/nushell.git
synced 2024-11-25 18:03:51 +01:00
Minor housekeeping in the parser (#12540)
- **Move lone `check_name` to the alias place** - **Restrict visibility of `check_call` helper**
This commit is contained in:
parent
43814dcb0f
commit
a67dad3d15
@ -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)]);
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user