Move precedence
to be a method on Operator
This commit is contained in:
parent
de2b752771
commit
89c2c5e690
|
@ -4950,7 +4950,7 @@ pub fn parse_math_expression(
|
|||
let mut expr_stack: Vec<Expression> = vec![];
|
||||
|
||||
let mut idx = 0;
|
||||
let mut last_prec = 1000000;
|
||||
let mut last_prec = u8::MAX;
|
||||
|
||||
let first_span = working_set.get_span_contents(spans[0]);
|
||||
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in New Issue
Block a user