mirror of
https://github.com/nushell/nushell.git
synced 2024-12-16 04:01:43 +01:00
Merge pull request #48 from elferherrera/parse-error
Error check on def and alias
This commit is contained in:
commit
7b54e5c4ab
@ -261,6 +261,14 @@ pub fn report_parsing_error(
|
|||||||
.with_labels(vec![Label::primary(diag_file_id, diag_range)
|
.with_labels(vec![Label::primary(diag_file_id, diag_range)
|
||||||
.with_message("needs a parameter name")])
|
.with_message("needs a parameter name")])
|
||||||
}
|
}
|
||||||
|
ParseError::AssignmentMismatch(msg, label, span) => {
|
||||||
|
let (diag_file_id, diag_range) = convert_span_to_diag(working_set, span)?;
|
||||||
|
Diagnostic::error()
|
||||||
|
.with_message(msg)
|
||||||
|
.with_labels(vec![
|
||||||
|
Label::primary(diag_file_id, diag_range).with_message(label)
|
||||||
|
])
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// println!("DIAG");
|
// println!("DIAG");
|
||||||
|
@ -30,4 +30,5 @@ pub enum ParseError {
|
|||||||
RestNeedsName(Span),
|
RestNeedsName(Span),
|
||||||
ExtraColumns(usize, Span),
|
ExtraColumns(usize, Span),
|
||||||
MissingColumns(usize, Span),
|
MissingColumns(usize, Span),
|
||||||
|
AssignmentMismatch(String, String, Span),
|
||||||
}
|
}
|
||||||
|
@ -63,16 +63,35 @@ fn check_call(command: Span, sig: &Signature, call: &Call) -> Option<ParseError>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn check_name(working_set: &mut StateWorkingSet, spans: &[Span]) -> Option<ParseError> {
|
fn check_name<'a>(
|
||||||
if spans[1..].len() < 2 {
|
working_set: &mut StateWorkingSet,
|
||||||
Some(ParseError::UnknownState(
|
spans: &'a [Span],
|
||||||
"missing definition name".into(),
|
) -> Option<(&'a Span, ParseError)> {
|
||||||
span(spans),
|
if spans.len() == 1 {
|
||||||
))
|
None
|
||||||
|
} else if spans.len() < 4 {
|
||||||
|
if working_set.get_span_contents(spans[1]) == b"=" {
|
||||||
|
let name = String::from_utf8_lossy(working_set.get_span_contents(spans[0]));
|
||||||
|
Some((
|
||||||
|
&spans[1],
|
||||||
|
ParseError::AssignmentMismatch(
|
||||||
|
format!("{} missing name", name),
|
||||||
|
"missing name".into(),
|
||||||
|
spans[1],
|
||||||
|
),
|
||||||
|
))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
} else if working_set.get_span_contents(spans[2]) != b"=" {
|
} else if working_set.get_span_contents(spans[2]) != b"=" {
|
||||||
Some(ParseError::UnknownState(
|
let name = String::from_utf8_lossy(working_set.get_span_contents(spans[0]));
|
||||||
"missing equal sign in definition".into(),
|
Some((
|
||||||
span(spans),
|
&spans[2],
|
||||||
|
ParseError::AssignmentMismatch(
|
||||||
|
format!("{} missing sign", name),
|
||||||
|
"missing equal sign".into(),
|
||||||
|
spans[2],
|
||||||
|
),
|
||||||
))
|
))
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
@ -2490,7 +2509,7 @@ pub fn parse_def(
|
|||||||
(
|
(
|
||||||
garbage_statement(spans),
|
garbage_statement(spans),
|
||||||
Some(ParseError::UnknownState(
|
Some(ParseError::UnknownState(
|
||||||
"definition unparseable. Expected structure: def <name> [] {}".into(),
|
"Expected structure: def <name> [] {}".into(),
|
||||||
span(spans),
|
span(spans),
|
||||||
)),
|
)),
|
||||||
)
|
)
|
||||||
@ -2504,9 +2523,9 @@ pub fn parse_alias(
|
|||||||
let name = working_set.get_span_contents(spans[0]);
|
let name = working_set.get_span_contents(spans[0]);
|
||||||
|
|
||||||
if name == b"alias" {
|
if name == b"alias" {
|
||||||
if let Some(err) = check_name(working_set, spans) {
|
if let Some((span, err)) = check_name(working_set, spans) {
|
||||||
return (
|
return (
|
||||||
Statement::Pipeline(Pipeline::from_vec(vec![garbage(span(spans))])),
|
Statement::Pipeline(Pipeline::from_vec(vec![garbage(*span)])),
|
||||||
Some(err),
|
Some(err),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -2562,9 +2581,9 @@ pub fn parse_let(
|
|||||||
let name = working_set.get_span_contents(spans[0]);
|
let name = working_set.get_span_contents(spans[0]);
|
||||||
|
|
||||||
if name == b"let" {
|
if name == b"let" {
|
||||||
if let Some(err) = check_name(working_set, spans) {
|
if let Some((span, err)) = check_name(working_set, spans) {
|
||||||
return (
|
return (
|
||||||
Statement::Pipeline(Pipeline::from_vec(vec![garbage(span(spans))])),
|
Statement::Pipeline(Pipeline::from_vec(vec![garbage(*span)])),
|
||||||
Some(err),
|
Some(err),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -2606,16 +2625,16 @@ pub fn parse_statement(
|
|||||||
working_set: &mut StateWorkingSet,
|
working_set: &mut StateWorkingSet,
|
||||||
spans: &[Span],
|
spans: &[Span],
|
||||||
) -> (Statement, Option<ParseError>) {
|
) -> (Statement, Option<ParseError>) {
|
||||||
// FIXME: improve errors by checking keyword first
|
let name = working_set.get_span_contents(spans[0]);
|
||||||
if let (decl, None) = parse_def(working_set, spans) {
|
|
||||||
(decl, None)
|
match name {
|
||||||
} else if let (stmt, None) = parse_let(working_set, spans) {
|
b"def" => parse_def(working_set, spans),
|
||||||
(stmt, None)
|
b"let" => parse_let(working_set, spans),
|
||||||
} else if let (stmt, None) = parse_alias(working_set, spans) {
|
b"alias" => parse_alias(working_set, spans),
|
||||||
(stmt, None)
|
_ => {
|
||||||
} else {
|
let (expr, err) = parse_expression(working_set, spans);
|
||||||
let (expr, err) = parse_expression(working_set, spans);
|
(Statement::Pipeline(Pipeline::from_vec(vec![expr])), err)
|
||||||
(Statement::Pipeline(Pipeline::from_vec(vec![expr])), err)
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user