arrow_parser/
operator.rs

1use crate::operator::Arity::*;
2use crate::operator::Associativity::*;
3use crate::operator::ExprOperatorName::*;
4use lazy_static::lazy_static;
5use std::collections::HashMap;
6
7#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
8pub enum AssignOperator {
9  Default,
10  Addition,
11  BitwiseAnd,
12  BitwiseLeftShift,
13  BitwiseOr,
14  BitwiseRightShift,
15  BitwiseUnsignedRightShift,
16  BitwiseXor,
17  Division,
18  Exponentiation,
19  Index,
20  LogicalAnd,
21  LogicalOr,
22  Multiplication,
23  Remainder,
24  Subtraction,
25}
26
27#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
28pub enum ExprOperatorName {
29  Addition,
30  Arrow,
31  As,
32  Bind,
33  BitwiseAnd,
34  BitwiseLeftShift,
35  BitwiseOr,
36  BitwiseRightShift,
37  BitwiseUnsignedRightShift,
38  BitwiseXor,
39  Call,
40  Division,
41  Equality,
42  Exponentiation,
43  GreaterThan,
44  GreaterThanOrEqual,
45  Index,
46  Inequality,
47  LessThan,
48  LessThanOrEqual,
49  LogicalAnd,
50  LogicalOr,
51  MemberAccess,
52  Multiplication,
53  Negation,
54  Not,
55  OptionalCall,
56  OptionalCoalescing,
57  OptionalIndex,
58  OptionalMemberAccess,
59  Remainder,
60  Subtraction,
61}
62
63#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
64pub enum Arity {
65  Unary,
66  Binary,
67}
68
69#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
70pub enum Associativity {
71  Left,
72  Right,
73}
74
75pub struct ExprOperator {
76  pub name: ExprOperatorName,
77  pub arity: Arity,
78  pub associativity: Associativity,
79  pub precedence: u8,
80}
81
82const PRECEDENCE_LEVELS: &'static [&'static [(ExprOperatorName, Arity, Associativity)]] = &[
83  &[
84    (Bind, Binary, Left),
85    (MemberAccess, Binary, Left),
86    (Index, Binary, Left),
87    (Call, Binary, Left),
88    (OptionalMemberAccess, Binary, Left),
89    (OptionalIndex, Binary, Left),
90    (OptionalCall, Binary, Left),
91  ],
92  &[(Not, Unary, Right), (Negation, Unary, Right)],
93  &[(As, Binary, Left)],
94  &[(Exponentiation, Binary, Right)],
95  &[
96    (Multiplication, Binary, Left),
97    (Division, Binary, Left),
98    (Remainder, Binary, Left),
99  ],
100  &[(Addition, Binary, Left), (Subtraction, Binary, Left)],
101  &[
102    (BitwiseLeftShift, Binary, Left),
103    (BitwiseRightShift, Binary, Left),
104    (BitwiseUnsignedRightShift, Binary, Left),
105  ],
106  &[
107    (LessThan, Binary, Left),
108    (LessThanOrEqual, Binary, Left),
109    (GreaterThan, Binary, Left),
110    (GreaterThanOrEqual, Binary, Left),
111  ],
112  &[(Equality, Binary, Left), (Inequality, Binary, Left)],
113  &[(BitwiseAnd, Binary, Left)],
114  &[(BitwiseXor, Binary, Left)],
115  &[(BitwiseOr, Binary, Left)],
116  &[(LogicalAnd, Binary, Left)],
117  &[
118    (LogicalOr, Binary, Left),
119    (OptionalCoalescing, Binary, Left),
120  ],
121  &[(Arrow, Binary, Left)],
122];
123
124lazy_static! {
125  pub static ref EXPR_OPERATORS: HashMap<ExprOperatorName, ExprOperator> = {
126    let mut map = HashMap::<ExprOperatorName, ExprOperator>::new();
127    for (i, ops) in PRECEDENCE_LEVELS.iter().enumerate() {
128      let precedence = (PRECEDENCE_LEVELS.len() - i) as u8;
129      for &(name, arity, associativity) in ops.iter() {
130        map.insert(name, ExprOperator {
131          name,
132          arity,
133          associativity,
134          precedence,
135        });
136      }
137    }
138    map
139  };
140}