mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 06:55:36 +02:00
Require that values that look like numbers parse as numberlike (#8635)
# Description Require that any value that looks like it might be a number (starts with a digit, or a '-' + digit, or a '+' + digits, or a special form float like `-inf`, `inf`, or `NaN`) must now be treated as a number-like value. Number-like syntax can only parse into number-like values. Number-like values include: durations, ints, floats, ranges, filesizes, binary data, etc. # User-Facing Changes BREAKING CHANGE BREAKING CHANGE BREAKING CHANGE BREAKING CHANGE BREAKING CHANGE BREAKING CHANGE BREAKING CHANGE BREAKING CHANGE Just making sure we see this for release notes 😅 This breaks any and all numberlike values that were treated as strings before. Example, we used to allow `3,` as a bare word. Anything like this would now require quotes or backticks to be treated as a string or bare word, respectively. # 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 > **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:
@ -1744,6 +1744,72 @@ pub fn parse_paren_expr(
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse_numberlike_expr(
|
||||
working_set: &mut StateWorkingSet,
|
||||
span: Span,
|
||||
shape: &SyntaxShape,
|
||||
expand_aliases_denylist: &[usize],
|
||||
) -> (Expression, Option<ParseError>) {
|
||||
let bytes = working_set.get_span_contents(span);
|
||||
match shape {
|
||||
SyntaxShape::Binary => parse_binary(working_set, span),
|
||||
SyntaxShape::Number => parse_number(bytes, span),
|
||||
SyntaxShape::Decimal => parse_float(bytes, span),
|
||||
SyntaxShape::Int => parse_int(bytes, 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, expand_aliases_denylist),
|
||||
SyntaxShape::CellPath => parse_simple_cell_path(working_set, span, expand_aliases_denylist),
|
||||
SyntaxShape::Any => {
|
||||
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(bytes, 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 (result, err) =
|
||||
parse_value(working_set, span, shape, expand_aliases_denylist);
|
||||
if err.is_none() {
|
||||
return (result, err);
|
||||
}
|
||||
}
|
||||
(
|
||||
garbage(span),
|
||||
Some(ParseError::Expected(
|
||||
"number-like value (int, float, date, etc)".into(),
|
||||
span,
|
||||
)),
|
||||
)
|
||||
}
|
||||
}
|
||||
_ => (
|
||||
garbage(span),
|
||||
Some(ParseError::Expected(
|
||||
"number-like value (int, float, date, etc)".into(),
|
||||
span,
|
||||
)),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse_brace_expr(
|
||||
working_set: &mut StateWorkingSet,
|
||||
span: Span,
|
||||
@ -4891,7 +4957,9 @@ pub fn parse_value(
|
||||
None,
|
||||
);
|
||||
}
|
||||
|
||||
b"-inf" | b"inf" | b"NaN" => {
|
||||
return parse_numberlike_expr(working_set, span, shape, expand_aliases_denylist);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
@ -4915,6 +4983,20 @@ pub fn parse_value(
|
||||
);
|
||||
}
|
||||
},
|
||||
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, expand_aliases_denylist);
|
||||
}
|
||||
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, expand_aliases_denylist);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
@ -4925,12 +5007,6 @@ pub fn parse_value(
|
||||
expression.custom_completion = Some(*custom_completion);
|
||||
(expression, err)
|
||||
}
|
||||
SyntaxShape::Number => parse_number(bytes, span),
|
||||
SyntaxShape::Decimal => parse_float(bytes, span),
|
||||
SyntaxShape::Int => parse_int(bytes, 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, expand_aliases_denylist),
|
||||
SyntaxShape::Filepath => parse_filepath(working_set, span),
|
||||
SyntaxShape::Directory => parse_directory(working_set, span),
|
||||
@ -5004,32 +5080,11 @@ pub fn parse_value(
|
||||
//parse_value(working_set, span, &SyntaxShape::Table)
|
||||
parse_full_cell_path(working_set, None, span, expand_aliases_denylist)
|
||||
} else {
|
||||
/* Parser very sensitive to order of shapes tried. Recording the original order for postierity
|
||||
let shapes = [
|
||||
SyntaxShape::Binary,
|
||||
SyntaxShape::Int,
|
||||
SyntaxShape::Number,
|
||||
SyntaxShape::Range,
|
||||
SyntaxShape::DateTime,
|
||||
SyntaxShape::Filesize,
|
||||
SyntaxShape::Duration,
|
||||
SyntaxShape::Record,
|
||||
SyntaxShape::Closure(None),
|
||||
SyntaxShape::Block,
|
||||
SyntaxShape::String,
|
||||
];
|
||||
*/
|
||||
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() {
|
||||
@ -5054,7 +5109,10 @@ pub fn parse_value(
|
||||
)
|
||||
}
|
||||
}
|
||||
_ => (garbage(span), Some(ParseError::IncompleteParser(span))),
|
||||
x => (
|
||||
garbage(span),
|
||||
Some(ParseError::Expected(x.to_type().to_string(), span)),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user