diff options
author | David Tolnay <dtolnay@gmail.com> | 2024-05-11 21:25:48 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-05-11 21:25:48 -0700 |
commit | a317be13694818535d09453369942ee4f32d80ac (patch) | |
tree | 0314cba74e7591d2cceea2625468108f06de1eb7 | |
parent | 45565276d0dcef1250cdb828758ae948839a105e (diff) | |
parent | 4c412bfd13013c8cf79390f8727e7186228e3c10 (diff) | |
download | syn-upstream-master.tar.gz |
Merge pull request #1650 from dtolnay/precedenceupstream-master
Extract Precedence to its own module
-rw-r--r-- | src/expr.rs | 77 | ||||
-rw-r--r-- | src/lib.rs | 3 | ||||
-rw-r--r-- | src/precedence.rs | 71 |
3 files changed, 75 insertions, 76 deletions
diff --git a/src/expr.rs b/src/expr.rs index 107f670c..b1c8021d 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -1100,81 +1100,6 @@ ast_enum! { } #[cfg(feature = "parsing")] -mod precedence { - use super::BinOp; - use std::cmp::Ordering; - - pub(super) enum Precedence { - Any, - Assign, - Range, - Or, - And, - Compare, - BitOr, - BitXor, - BitAnd, - Shift, - Arithmetic, - Term, - Cast, - } - - impl Precedence { - pub(super) fn of(op: &BinOp) -> Self { - match op { - BinOp::Add(_) | BinOp::Sub(_) => Precedence::Arithmetic, - BinOp::Mul(_) | BinOp::Div(_) | BinOp::Rem(_) => Precedence::Term, - BinOp::And(_) => Precedence::And, - BinOp::Or(_) => Precedence::Or, - BinOp::BitXor(_) => Precedence::BitXor, - BinOp::BitAnd(_) => Precedence::BitAnd, - BinOp::BitOr(_) => Precedence::BitOr, - BinOp::Shl(_) | BinOp::Shr(_) => Precedence::Shift, - BinOp::Eq(_) - | BinOp::Lt(_) - | BinOp::Le(_) - | BinOp::Ne(_) - | BinOp::Ge(_) - | BinOp::Gt(_) => Precedence::Compare, - BinOp::AddAssign(_) - | BinOp::SubAssign(_) - | BinOp::MulAssign(_) - | BinOp::DivAssign(_) - | BinOp::RemAssign(_) - | BinOp::BitXorAssign(_) - | BinOp::BitAndAssign(_) - | BinOp::BitOrAssign(_) - | BinOp::ShlAssign(_) - | BinOp::ShrAssign(_) => Precedence::Assign, - } - } - } - - impl Copy for Precedence {} - - impl Clone for Precedence { - fn clone(&self) -> Self { - *self - } - } - - impl PartialEq for Precedence { - fn eq(&self, other: &Self) -> bool { - *self as u8 == *other as u8 - } - } - - impl PartialOrd for Precedence { - fn partial_cmp(&self, other: &Self) -> Option<Ordering> { - let this = *self as u8; - let other = *other as u8; - Some(this.cmp(&other)) - } - } -} - -#[cfg(feature = "parsing")] pub(crate) mod parsing { #[cfg(feature = "full")] use crate::attr; @@ -1182,7 +1107,6 @@ pub(crate) mod parsing { #[cfg(feature = "full")] use crate::classify; use crate::error::{Error, Result}; - use crate::expr::precedence::Precedence; #[cfg(feature = "full")] use crate::expr::{ Arm, ExprArray, ExprAssign, ExprAsync, ExprAwait, ExprBlock, ExprBreak, ExprClosure, @@ -1212,6 +1136,7 @@ pub(crate) mod parsing { #[cfg(feature = "full")] use crate::pat::{Pat, PatType}; use crate::path::{self, AngleBracketedGenericArguments, Path, QSelf}; + use crate::precedence::Precedence; use crate::punctuated::Punctuated; #[cfg(feature = "full")] use crate::stmt::Block; @@ -477,6 +477,9 @@ pub use crate::path::{ ParenthesizedGenericArguments, Path, PathArguments, PathSegment, QSelf, }; +#[cfg(all(any(feature = "full", feature = "derive"), feature = "parsing"))] +mod precedence; + #[cfg(all(any(feature = "full", feature = "derive"), feature = "printing"))] mod print; diff --git a/src/precedence.rs b/src/precedence.rs new file mode 100644 index 00000000..31c5a4ad --- /dev/null +++ b/src/precedence.rs @@ -0,0 +1,71 @@ +use crate::op::BinOp; +use std::cmp::Ordering; + +pub(crate) enum Precedence { + Any, + Assign, + Range, + Or, + And, + Compare, + BitOr, + BitXor, + BitAnd, + Shift, + Arithmetic, + Term, + Cast, +} + +impl Precedence { + pub(crate) fn of(op: &BinOp) -> Self { + match op { + BinOp::Add(_) | BinOp::Sub(_) => Precedence::Arithmetic, + BinOp::Mul(_) | BinOp::Div(_) | BinOp::Rem(_) => Precedence::Term, + BinOp::And(_) => Precedence::And, + BinOp::Or(_) => Precedence::Or, + BinOp::BitXor(_) => Precedence::BitXor, + BinOp::BitAnd(_) => Precedence::BitAnd, + BinOp::BitOr(_) => Precedence::BitOr, + BinOp::Shl(_) | BinOp::Shr(_) => Precedence::Shift, + BinOp::Eq(_) + | BinOp::Lt(_) + | BinOp::Le(_) + | BinOp::Ne(_) + | BinOp::Ge(_) + | BinOp::Gt(_) => Precedence::Compare, + BinOp::AddAssign(_) + | BinOp::SubAssign(_) + | BinOp::MulAssign(_) + | BinOp::DivAssign(_) + | BinOp::RemAssign(_) + | BinOp::BitXorAssign(_) + | BinOp::BitAndAssign(_) + | BinOp::BitOrAssign(_) + | BinOp::ShlAssign(_) + | BinOp::ShrAssign(_) => Precedence::Assign, + } + } +} + +impl Copy for Precedence {} + +impl Clone for Precedence { + fn clone(&self) -> Self { + *self + } +} + +impl PartialEq for Precedence { + fn eq(&self, other: &Self) -> bool { + *self as u8 == *other as u8 + } +} + +impl PartialOrd for Precedence { + fn partial_cmp(&self, other: &Self) -> Option<Ordering> { + let this = *self as u8; + let other = *other as u8; + Some(this.cmp(&other)) + } +} |