mirror of
https://github.com/nushell/nushell.git
synced 2025-08-10 04:48:20 +02:00
help operators
refactor (#13307)
# Description Refactors `help operators` so that its output is always up to date with the parser. # User-Facing Changes - The order of output rows for `help operators` was changed. - `not` is now listed as a boolean operator instead of a comparison operator. - Edited some of the descriptions for the operators.
This commit is contained in:
@ -27,42 +27,9 @@ impl Expression {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn precedence(&self) -> usize {
|
||||
pub fn precedence(&self) -> u8 {
|
||||
match &self.expr {
|
||||
Expr::Operator(operator) => {
|
||||
use super::operator::*;
|
||||
// Higher precedence binds tighter
|
||||
|
||||
match operator {
|
||||
Operator::Math(Math::Pow) => 100,
|
||||
Operator::Math(Math::Multiply)
|
||||
| Operator::Math(Math::Divide)
|
||||
| Operator::Math(Math::Modulo)
|
||||
| Operator::Math(Math::FloorDivision) => 95,
|
||||
Operator::Math(Math::Plus) | Operator::Math(Math::Minus) => 90,
|
||||
Operator::Bits(Bits::ShiftLeft) | Operator::Bits(Bits::ShiftRight) => 85,
|
||||
Operator::Comparison(Comparison::NotRegexMatch)
|
||||
| Operator::Comparison(Comparison::RegexMatch)
|
||||
| Operator::Comparison(Comparison::StartsWith)
|
||||
| Operator::Comparison(Comparison::EndsWith)
|
||||
| Operator::Comparison(Comparison::LessThan)
|
||||
| Operator::Comparison(Comparison::LessThanOrEqual)
|
||||
| Operator::Comparison(Comparison::GreaterThan)
|
||||
| Operator::Comparison(Comparison::GreaterThanOrEqual)
|
||||
| Operator::Comparison(Comparison::Equal)
|
||||
| Operator::Comparison(Comparison::NotEqual)
|
||||
| Operator::Comparison(Comparison::In)
|
||||
| Operator::Comparison(Comparison::NotIn)
|
||||
| Operator::Math(Math::Append) => 80,
|
||||
Operator::Bits(Bits::BitAnd) => 75,
|
||||
Operator::Bits(Bits::BitXor) => 70,
|
||||
Operator::Bits(Bits::BitOr) => 60,
|
||||
Operator::Boolean(Boolean::And) => 50,
|
||||
Operator::Boolean(Boolean::Xor) => 45,
|
||||
Operator::Boolean(Boolean::Or) => 40,
|
||||
Operator::Assignment(_) => 10,
|
||||
}
|
||||
}
|
||||
Expr::Operator(operator) => operator.precedence(),
|
||||
_ => 0,
|
||||
}
|
||||
}
|
||||
|
@ -68,6 +68,40 @@ pub enum Operator {
|
||||
Assignment(Assignment),
|
||||
}
|
||||
|
||||
impl Operator {
|
||||
pub fn precedence(&self) -> u8 {
|
||||
match self {
|
||||
Self::Math(Math::Pow) => 100,
|
||||
Self::Math(Math::Multiply)
|
||||
| Self::Math(Math::Divide)
|
||||
| Self::Math(Math::Modulo)
|
||||
| Self::Math(Math::FloorDivision) => 95,
|
||||
Self::Math(Math::Plus) | Self::Math(Math::Minus) => 90,
|
||||
Self::Bits(Bits::ShiftLeft) | Self::Bits(Bits::ShiftRight) => 85,
|
||||
Self::Comparison(Comparison::NotRegexMatch)
|
||||
| Self::Comparison(Comparison::RegexMatch)
|
||||
| Self::Comparison(Comparison::StartsWith)
|
||||
| Self::Comparison(Comparison::EndsWith)
|
||||
| Self::Comparison(Comparison::LessThan)
|
||||
| Self::Comparison(Comparison::LessThanOrEqual)
|
||||
| Self::Comparison(Comparison::GreaterThan)
|
||||
| Self::Comparison(Comparison::GreaterThanOrEqual)
|
||||
| Self::Comparison(Comparison::Equal)
|
||||
| Self::Comparison(Comparison::NotEqual)
|
||||
| Self::Comparison(Comparison::In)
|
||||
| Self::Comparison(Comparison::NotIn)
|
||||
| Self::Math(Math::Append) => 80,
|
||||
Self::Bits(Bits::BitAnd) => 75,
|
||||
Self::Bits(Bits::BitXor) => 70,
|
||||
Self::Bits(Bits::BitOr) => 60,
|
||||
Self::Boolean(Boolean::And) => 50,
|
||||
Self::Boolean(Boolean::Xor) => 45,
|
||||
Self::Boolean(Boolean::Or) => 40,
|
||||
Self::Assignment(_) => 10,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for Operator {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
@ -95,10 +129,10 @@ impl Display for Operator {
|
||||
Operator::Math(Math::Multiply) => write!(f, "*"),
|
||||
Operator::Math(Math::Divide) => write!(f, "/"),
|
||||
Operator::Math(Math::Modulo) => write!(f, "mod"),
|
||||
Operator::Math(Math::FloorDivision) => write!(f, "fdiv"),
|
||||
Operator::Math(Math::FloorDivision) => write!(f, "//"),
|
||||
Operator::Math(Math::Pow) => write!(f, "**"),
|
||||
Operator::Boolean(Boolean::And) => write!(f, "&&"),
|
||||
Operator::Boolean(Boolean::Or) => write!(f, "||"),
|
||||
Operator::Boolean(Boolean::And) => write!(f, "and"),
|
||||
Operator::Boolean(Boolean::Or) => write!(f, "or"),
|
||||
Operator::Boolean(Boolean::Xor) => write!(f, "xor"),
|
||||
Operator::Bits(Bits::BitOr) => write!(f, "bit-or"),
|
||||
Operator::Bits(Bits::BitXor) => write!(f, "bit-xor"),
|
||||
|
Reference in New Issue
Block a user