Debian Codegraph @

third_party/rust/syn/src/precedence.rs

raw
1 #[cfg(feature = "printing")]
2 use crate::expr::Expr;
3 #[cfg(all(feature = "printing", feature = "full"))]
4 use crate::expr::{ExprBreak, ExprReturn, ExprYield};
5 use crate::op::BinOp;
6 #[cfg(all(feature = "printing", feature = "full"))]
7 use crate::ty::ReturnType;
8 use std::cmp::Ordering;
9
10 // Reference: https://doc.rust-lang.org/reference/expressions.html#expression-precedence
11 pub(crate) enum Precedence {
12 // return, break, closures
13 Jump,
14 // = += -= *= /= %= &= |= ^= <<= >>=
15 Assign,
16 // .. ..=
17 Range,
18 // ||
19 Or,
20 // &&
21 And,
22 // let
23 #[cfg(feature = "printing")]
24 Let,
25 // == != < > <= >=
26 Compare,
27 // |
28 BitOr,
29 // ^
30 BitXor,
31 // &
32 BitAnd,
33 // << >>
34 Shift,
35 // + -
36 Sum,
37 // * / %
38 Product,
39 // as
40 Cast,
41 // unary - * ! & &mut
42 #[cfg(feature = "printing")]
43 Prefix,
44 // paths, loops, function calls, array indexing, field expressions, method calls
45 #[cfg(feature = "printing")]
46 Unambiguous,
47 }
48
49 impl Precedence {
50 pub(crate) const MIN: Self = Precedence::Jump;
51
52 pub(crate) fn of_binop(op: &BinOp) -> Self {
53 match op {
54 BinOp::Add(_) | BinOp::Sub(_) => Precedence::Sum,
55 BinOp::Mul(_) | BinOp::Div(_) | BinOp::Rem(_) => Precedence::Product,
56 BinOp::And(_) => Precedence::And,
57 BinOp::Or(_) => Precedence::Or,
58 BinOp::BitXor(_) => Precedence::BitXor,
59 BinOp::BitAnd(_) => Precedence::BitAnd,
60 BinOp::BitOr(_) => Precedence::BitOr,
61 BinOp::Shl(_) | BinOp::Shr(_) => Precedence::Shift,
62
63 BinOp::Eq(_)
64 | BinOp::Lt(_)
65 | BinOp::Le(_)
66 | BinOp::Ne(_)
67 | BinOp::Ge(_)
68 | BinOp::Gt(_) => Precedence::Compare,
69
70 BinOp::AddAssign(_)
71 | BinOp::SubAssign(_)
72 | BinOp::MulAssign(_)
73 | BinOp::DivAssign(_)
74 | BinOp::RemAssign(_)
75 | BinOp::BitXorAssign(_)
76 | BinOp::BitAndAssign(_)
77 | BinOp::BitOrAssign(_)
78 | BinOp::ShlAssign(_)
79 | BinOp::ShrAssign(_) => Precedence::Assign,
80 }
81 }
82
83 #[cfg(feature = "printing")]
84 pub(crate) fn of(e: &Expr) -> Self {
85 match e {
86 #[cfg(feature = "full")]
87 Expr::Closure(e) => match e.output {
88 ReturnType::Default => Precedence::Jump,
89 ReturnType::Type(..) => Precedence::Unambiguous,
90 },
91
92 #[cfg(feature = "full")]
93 Expr::Break(ExprBreak { expr, .. })
94 | Expr::Return(ExprReturn { expr, .. })
95 | Expr::Yield(ExprYield { expr, .. }) => match expr {
96 Some(_) => Precedence::Jump,
97 None => Precedence::Unambiguous,
98 },
99
100 Expr::Assign(_) => Precedence::Assign,
101 Expr::Range(_) => Precedence::Range,
102 Expr::Binary(e) => Precedence::of_binop(&e.op),
103 Expr::Let(_) => Precedence::Let,
104 Expr::Cast(_) => Precedence::Cast,
105 Expr::RawAddr(_) | Expr::Reference(_) | Expr::Unary(_) => Precedence::Prefix,
106
107 Expr::Array(_)
108 | Expr::Async(_)
109 | Expr::Await(_)
110 | Expr::Block(_)
111 | Expr::Call(_)
112 | Expr::Const(_)
113 | Expr::Continue(_)
114 | Expr::Field(_)
115 | Expr::ForLoop(_)
116 | Expr::Group(_)
117 | Expr::If(_)
118 | Expr::Index(_)
119 | Expr::Infer(_)
120 | Expr::Lit(_)
121 | Expr::Loop(_)
122 | Expr::Macro(_)
123 | Expr::Match(_)
124 | Expr::MethodCall(_)
125 | Expr::Paren(_)
126 | Expr::Path(_)
127 | Expr::Repeat(_)
128 | Expr::Struct(_)
129 | Expr::Try(_)
130 | Expr::TryBlock(_)
131 | Expr::Tuple(_)
132 | Expr::Unsafe(_)
133 | Expr::Verbatim(_)
134 | Expr::While(_) => Precedence::Unambiguous,
135
136 #[cfg(not(feature = "full"))]
137 Expr::Break(_) | Expr::Closure(_) | Expr::Return(_) | Expr::Yield(_) => unreachable!(),
138 }
139 }
140 }
141
142 impl Copy for Precedence {}
143
144 impl Clone for Precedence {
145 fn clone(&self) -> Self {
146 *self
147 }
148 }
149
150 impl PartialEq for Precedence {
151 fn eq(&self, other: &Self) -> bool {
152 *self as u8 == *other as u8
153 }
154 }
155
156 impl PartialOrd for Precedence {
157 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
158 let this = *self as u8;
159 let other = *other as u8;
160 Some(this.cmp(&other))
161 }
162 }