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
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104


#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum OperatorKind {
    Prefix,
    Infix,
    Postfix,
}


// UNARY_OPERATORS
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum PrefixOperator {
    Await,     // await
    Delete,    // delete
    Void,      // void
    TypeOf,    // typeof
    /// The unary positive operator +.
    Positive,  // +
    /// The unary negation operator -.
    Negative,  // -
    BitNot,    // ~
    Not,       // !

    Increment, // ++
    Decrement, // --
}

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum InfixOperator {
    // BINARY_OPERATORS
    Add,      // +
    Sub,      // -
    Mul,      // *
    Div,      // /
    Rem,      // %
    Pow,      // **
    BitShl,   // <<
    BitShr,   // >>
    BitUShr,  // >>>
    And,      // &&
    Or,       // ||
    BitAnd,   // &
    BitXor,   // ^
    BitOr,    // |

    // COMPARE_OPERATORS
    Gt,        // >
    Lt,        // <
    GtEq,      // >=
    LtEq,      // <=
    Eq,        // ==
    Neq,       // !=
    StrictEq,  // ===
    StrictNeq, // !==

    InstanceOf,  // instanceof
    In,          // in
}

impl InfixOperator {
    pub fn precedence(&self) -> i8 {
        use self::InfixOperator::*;

        match *self {
            Pow => 15,
            Mul | Div | Rem => 14,
            Add | Sub => 13,
            BitShl | BitShr | BitUShr => 12,
            Lt | LtEq | Gt | GtEq | In | InstanceOf => 11,
            Eq | Neq | StrictEq | StrictNeq => 10,
            BitAnd => 9,
            BitXor => 8,
            BitOr => 7,
            And => 6,
            Or => 5,
        }
    }
}

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum PostfixOperator {
    Increment, // ++
    Decrement, // --
}

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum AssignmentOperator {
    Assign,         //    =
    AddAssign,      //   +=
    SubAssign,      //   -=
    MulAssign,      //   *=
    DivAssign,      //   /=
    RemAssign,      //   %=
    PowAssign,      //  **=

    BitAndAssign,   //   &=
    BitOrAssign,    //   |=
    BitXorAssign,   //   ^=
    BitShlAssign,   //  <<=
    BitShrAssign,   //  >>=
    BitUShrAssign,  // >>>=
}