mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 19:27:44 +02:00
Revert numberlike parsing restriction (#8845)
# Description This effectively reverts #8635. We shipped this change with 0.78 and received many comments/issues related to this restriction feeling like a step backward. fixes: #8844 (and probably other issues) # User-Facing Changes Returns numbers and number-like values to being allowed to be bare words. Examples: `3*`, `1fb43`, `4,5`, and related. # Tests + Formatting Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass - `cargo run -- crates/nu-std/tests/run.nu` to run the tests for the standard library > **Note** > from `nushell` you can also use the `toolkit` as follows > ```bash > use toolkit.nu # or use an `env_change` hook to activate it automatically > toolkit check pr > ``` # After Submitting If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date.
This commit is contained in:
@ -1553,83 +1553,6 @@ pub fn parse_paren_expr(
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse_numberlike_expr(
|
||||
working_set: &mut StateWorkingSet,
|
||||
span: Span,
|
||||
shape: &SyntaxShape,
|
||||
) -> Expression {
|
||||
match shape {
|
||||
SyntaxShape::Binary => parse_binary(working_set, span),
|
||||
SyntaxShape::Number => parse_number(working_set, span),
|
||||
SyntaxShape::Decimal => parse_float(working_set, span),
|
||||
SyntaxShape::Int => parse_int(working_set, span),
|
||||
SyntaxShape::Duration => parse_duration(working_set, span),
|
||||
SyntaxShape::DateTime => parse_datetime(working_set, span),
|
||||
SyntaxShape::Filesize => parse_filesize(working_set, span),
|
||||
SyntaxShape::Range => parse_range(working_set, span),
|
||||
SyntaxShape::CellPath => parse_simple_cell_path(working_set, span),
|
||||
SyntaxShape::String => {
|
||||
working_set.error(ParseError::Mismatch(
|
||||
"string".into(),
|
||||
"number-like value (hint: use quotes or backticks)".into(),
|
||||
span,
|
||||
));
|
||||
garbage(span)
|
||||
}
|
||||
SyntaxShape::Any => {
|
||||
let bytes = working_set.get_span_contents(span);
|
||||
|
||||
if bytes == b"0b" {
|
||||
// FIXME: having to work around this filesize that also looks like a binary value
|
||||
parse_filesize(working_set, span)
|
||||
} else if bytes.starts_with(b"0x[")
|
||||
|| bytes.starts_with(b"0b[")
|
||||
|| bytes.starts_with(b"0o[")
|
||||
{
|
||||
parse_binary(working_set, span)
|
||||
} else if bytes.starts_with(b"0x")
|
||||
|| bytes.starts_with(b"0b")
|
||||
|| bytes.starts_with(b"0o")
|
||||
{
|
||||
parse_int(working_set, span)
|
||||
} else {
|
||||
for shape in &[
|
||||
SyntaxShape::Range,
|
||||
SyntaxShape::Int,
|
||||
SyntaxShape::Binary,
|
||||
SyntaxShape::Filesize,
|
||||
SyntaxShape::Duration,
|
||||
SyntaxShape::DateTime, //FIXME requires 3 failed conversion attempts before failing
|
||||
SyntaxShape::Number,
|
||||
] {
|
||||
let starting_error_count = working_set.parse_errors.len();
|
||||
|
||||
let result = parse_value(working_set, span, shape);
|
||||
|
||||
if starting_error_count == working_set.parse_errors.len() {
|
||||
return result;
|
||||
} else {
|
||||
working_set.parse_errors.truncate(starting_error_count);
|
||||
}
|
||||
}
|
||||
|
||||
working_set.error(ParseError::Expected(
|
||||
"number-like value (int, float, date, etc)".into(),
|
||||
span,
|
||||
));
|
||||
garbage(span)
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
working_set.error(ParseError::Expected(
|
||||
"number-like value (int, float, date, etc)".into(),
|
||||
span,
|
||||
));
|
||||
garbage(span)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse_brace_expr(
|
||||
working_set: &mut StateWorkingSet,
|
||||
span: Span,
|
||||
@ -4477,7 +4400,7 @@ pub fn parse_value(
|
||||
};
|
||||
}
|
||||
b"-inf" | b"inf" | b"NaN" => {
|
||||
return parse_numberlike_expr(working_set, span, shape);
|
||||
return parse_float(working_set, span);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
@ -4500,20 +4423,6 @@ pub fn parse_value(
|
||||
return Expression::garbage(span);
|
||||
}
|
||||
},
|
||||
x if x.is_ascii_digit() => {
|
||||
// Anything that starts with a number now has to be a number-like value
|
||||
// These include values like ints, floats, dates, durations, etc
|
||||
// To create a string, wrap in quotes, to create a bare word, wrap in backticks
|
||||
return parse_numberlike_expr(working_set, span, shape);
|
||||
}
|
||||
b'-' | b'+' => {
|
||||
if bytes.len() > 1 && bytes[1].is_ascii_digit() {
|
||||
// Anything that starts with a negative number now has to be a number-like value
|
||||
// These include values like ints, floats, dates, durations, etc
|
||||
// To create a string, wrap in quotes, to create a bare word, wrap in backticks
|
||||
return parse_numberlike_expr(working_set, span, shape);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
@ -4523,6 +4432,12 @@ pub fn parse_value(
|
||||
expression.custom_completion = Some(*custom_completion);
|
||||
expression
|
||||
}
|
||||
SyntaxShape::Number => parse_number(working_set, span),
|
||||
SyntaxShape::Decimal => parse_float(working_set, span),
|
||||
SyntaxShape::Int => parse_int(working_set, span),
|
||||
SyntaxShape::Duration => parse_duration(working_set, span),
|
||||
SyntaxShape::DateTime => parse_datetime(working_set, span),
|
||||
SyntaxShape::Filesize => parse_filesize(working_set, span),
|
||||
SyntaxShape::Range => parse_range(working_set, span),
|
||||
SyntaxShape::Filepath => parse_filepath(working_set, span),
|
||||
SyntaxShape::Directory => parse_directory(working_set, span),
|
||||
@ -4591,10 +4506,16 @@ pub fn parse_value(
|
||||
parse_full_cell_path(working_set, None, span)
|
||||
} else {
|
||||
let shapes = [
|
||||
SyntaxShape::Binary,
|
||||
SyntaxShape::Filesize,
|
||||
SyntaxShape::Duration,
|
||||
SyntaxShape::Range,
|
||||
SyntaxShape::DateTime, //FIXME requires 3 failed conversion attempts before failing
|
||||
SyntaxShape::Record,
|
||||
SyntaxShape::Closure(None),
|
||||
SyntaxShape::Block,
|
||||
SyntaxShape::Int,
|
||||
SyntaxShape::Number,
|
||||
SyntaxShape::String,
|
||||
];
|
||||
for shape in shapes.iter() {
|
||||
|
Reference in New Issue
Block a user