mxmlextrema_as3parser/operator/
operator_precedence.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
use num_derive::FromPrimitive;
use num_traits::FromPrimitive;

#[derive(FromPrimitive)]
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[repr(u32)]
pub enum OperatorPrecedence {
    Postfix = 16,
    Unary = 15,
    Exponentiation = 14,
    Multiplicative = 13,
    Additive = 12,
    Shift = 11,
    Relational = 10,
    Equality = 9,
    BitwiseAnd = 8,
    BitwiseXor = 7,
    BitwiseOr = 6,
    LogicalAnd = 5,
    LogicalXor = 4,
    /// Includes logical OR and nullish coalescing (`??`).
    LogicalOrAndOther = 3,
    /// Includes assignment operators, conditional operator, function expression and `yield` operator.
    AssignmentAndOther = 2,
    List = 1,
}

impl OperatorPrecedence {
    pub fn add(&self, value: u32) -> Option<Self> {
        FromPrimitive::from_u32(*self as u32 + value)
    }

    pub fn value_of(&self) -> u32 {
        *self as u32
    }

    pub fn includes(&self, other: &Self) -> bool {
        *self <= *other
    }
}

impl TryFrom<u32> for OperatorPrecedence {
    type Error = ();
    fn try_from(value: u32) -> Result<Self, Self::Error> {
        if let Some(v) = FromPrimitive::from_u32(value as u32) { Ok(v) } else { Err(()) }
    }
}

#[derive(FromPrimitive)]
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[repr(u32)]
pub enum CssOperatorPrecedence {
    Unary = 3,
    MultiValue = 2,
    Array = 1,
}

impl CssOperatorPrecedence {
    pub fn add(&self, value: u32) -> Option<Self> {
        FromPrimitive::from_u32(*self as u32 + value)
    }

    pub fn value_of(&self) -> u32 {
        *self as u32
    }

    pub fn includes(&self, other: &Self) -> bool {
        *self <= *other
    }
}

impl TryFrom<u32> for CssOperatorPrecedence {
    type Error = ();
    fn try_from(value: u32) -> Result<Self, Self::Error> {
        if let Some(v) = FromPrimitive::from_u32(value as u32) { Ok(v) } else { Err(()) }
    }
}