mxmlextrema_as3parser/operator/
operator_precedence.rs

1use num_derive::FromPrimitive;
2use num_traits::FromPrimitive;
3
4#[derive(FromPrimitive)]
5#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
6#[repr(u32)]
7pub enum OperatorPrecedence {
8    Postfix = 16,
9    Unary = 15,
10    Exponentiation = 14,
11    Multiplicative = 13,
12    Additive = 12,
13    Shift = 11,
14    Relational = 10,
15    Equality = 9,
16    BitwiseAnd = 8,
17    BitwiseXor = 7,
18    BitwiseOr = 6,
19    LogicalAnd = 5,
20    LogicalXor = 4,
21    /// Includes logical OR and nullish coalescing (`??`).
22    LogicalOrAndOther = 3,
23    /// Includes assignment operators, conditional operator, function expression and `yield` operator.
24    AssignmentAndOther = 2,
25    List = 1,
26}
27
28impl OperatorPrecedence {
29    pub fn add(&self, value: u32) -> Option<Self> {
30        FromPrimitive::from_u32(*self as u32 + value)
31    }
32
33    pub fn value_of(&self) -> u32 {
34        *self as u32
35    }
36
37    pub fn includes(&self, other: &Self) -> bool {
38        *self <= *other
39    }
40}
41
42impl TryFrom<u32> for OperatorPrecedence {
43    type Error = ();
44    fn try_from(value: u32) -> Result<Self, Self::Error> {
45        if let Some(v) = FromPrimitive::from_u32(value as u32) { Ok(v) } else { Err(()) }
46    }
47}
48
49#[derive(FromPrimitive)]
50#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
51#[repr(u32)]
52pub enum CssOperatorPrecedence {
53    Unary = 3,
54    MultiValue = 2,
55    Array = 1,
56}
57
58impl CssOperatorPrecedence {
59    pub fn add(&self, value: u32) -> Option<Self> {
60        FromPrimitive::from_u32(*self as u32 + value)
61    }
62
63    pub fn value_of(&self) -> u32 {
64        *self as u32
65    }
66
67    pub fn includes(&self, other: &Self) -> bool {
68        *self <= *other
69    }
70}
71
72impl TryFrom<u32> for CssOperatorPrecedence {
73    type Error = ();
74    fn try_from(value: u32) -> Result<Self, Self::Error> {
75        if let Some(v) = FromPrimitive::from_u32(value as u32) { Ok(v) } else { Err(()) }
76    }
77}