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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
use parse::token::{Token, BinOpToken, keywords};
use ast;
#[derive(Debug, PartialEq, Eq)]
pub enum AssocOp {
Add,
Subtract,
Multiply,
Divide,
Modulus,
LAnd,
LOr,
BitXor,
BitAnd,
BitOr,
ShiftLeft,
ShiftRight,
Equal,
Less,
LessEqual,
NotEqual,
Greater,
GreaterEqual,
Assign,
Inplace,
AssignOp(BinOpToken),
As,
DotDot
}
#[derive(Debug, PartialEq, Eq)]
pub enum Fixity {
Left,
Right,
None
}
impl AssocOp {
pub fn from_token(t: &Token) -> Option<AssocOp> {
use self::AssocOp::*;
match *t {
Token::BinOpEq(k) => Some(AssignOp(k)),
Token::LArrow => Some(Inplace),
Token::Eq => Some(Assign),
Token::BinOp(BinOpToken::Star) => Some(Multiply),
Token::BinOp(BinOpToken::Slash) => Some(Divide),
Token::BinOp(BinOpToken::Percent) => Some(Modulus),
Token::BinOp(BinOpToken::Plus) => Some(Add),
Token::BinOp(BinOpToken::Minus) => Some(Subtract),
Token::BinOp(BinOpToken::Shl) => Some(ShiftLeft),
Token::BinOp(BinOpToken::Shr) => Some(ShiftRight),
Token::BinOp(BinOpToken::And) => Some(BitAnd),
Token::BinOp(BinOpToken::Caret) => Some(BitXor),
Token::BinOp(BinOpToken::Or) => Some(BitOr),
Token::Lt => Some(Less),
Token::Le => Some(LessEqual),
Token::Ge => Some(GreaterEqual),
Token::Gt => Some(Greater),
Token::EqEq => Some(Equal),
Token::Ne => Some(NotEqual),
Token::AndAnd => Some(LAnd),
Token::OrOr => Some(LOr),
Token::DotDot => Some(DotDot),
_ if t.is_keyword(keywords::As) => Some(As),
_ => None
}
}
pub fn from_ast_binop(op: ast::BinOp_) -> Self {
use self::AssocOp::*;
match op {
ast::BiLt => Less,
ast::BiGt => Greater,
ast::BiLe => LessEqual,
ast::BiGe => GreaterEqual,
ast::BiEq => Equal,
ast::BiNe => NotEqual,
ast::BiMul => Multiply,
ast::BiDiv => Divide,
ast::BiRem => Modulus,
ast::BiAdd => Add,
ast::BiSub => Subtract,
ast::BiShl => ShiftLeft,
ast::BiShr => ShiftRight,
ast::BiBitAnd => BitAnd,
ast::BiBitXor => BitXor,
ast::BiBitOr => BitOr,
ast::BiAnd => LAnd,
ast::BiOr => LOr
}
}
pub fn precedence(&self) -> usize {
use self::AssocOp::*;
match *self {
As => 14,
Multiply | Divide | Modulus => 13,
Add | Subtract => 12,
ShiftLeft | ShiftRight => 11,
BitAnd => 10,
BitXor => 9,
BitOr => 8,
Less | Greater | LessEqual | GreaterEqual | Equal | NotEqual => 7,
LAnd => 6,
LOr => 5,
DotDot => 4,
Inplace => 3,
Assign | AssignOp(_) => 2,
}
}
pub fn fixity(&self) -> Fixity {
use self::AssocOp::*;
match *self {
Inplace | Assign | AssignOp(_) => Fixity::Right,
As | Multiply | Divide | Modulus | Add | Subtract | ShiftLeft | ShiftRight | BitAnd |
BitXor | BitOr | Less | Greater | LessEqual | GreaterEqual | Equal | NotEqual |
LAnd | LOr => Fixity::Left,
DotDot => Fixity::None
}
}
pub fn is_comparison(&self) -> bool {
use self::AssocOp::*;
match *self {
Less | Greater | LessEqual | GreaterEqual | Equal | NotEqual => true,
Inplace | Assign | AssignOp(_) | As | Multiply | Divide | Modulus | Add | Subtract |
ShiftLeft | ShiftRight | BitAnd | BitXor | BitOr | LAnd | LOr | DotDot => false
}
}
pub fn is_assign_like(&self) -> bool {
use self::AssocOp::*;
match *self {
Assign | AssignOp(_) | Inplace => true,
Less | Greater | LessEqual | GreaterEqual | Equal | NotEqual | As | Multiply | Divide |
Modulus | Add | Subtract | ShiftLeft | ShiftRight | BitAnd | BitXor | BitOr | LAnd |
LOr | DotDot => false
}
}
pub fn to_ast_binop(&self) -> Option<ast::BinOp_> {
use self::AssocOp::*;
match *self {
Less => Some(ast::BiLt),
Greater => Some(ast::BiGt),
LessEqual => Some(ast::BiLe),
GreaterEqual => Some(ast::BiGe),
Equal => Some(ast::BiEq),
NotEqual => Some(ast::BiNe),
Multiply => Some(ast::BiMul),
Divide => Some(ast::BiDiv),
Modulus => Some(ast::BiRem),
Add => Some(ast::BiAdd),
Subtract => Some(ast::BiSub),
ShiftLeft => Some(ast::BiShl),
ShiftRight => Some(ast::BiShr),
BitAnd => Some(ast::BiBitAnd),
BitXor => Some(ast::BiBitXor),
BitOr => Some(ast::BiBitOr),
LAnd => Some(ast::BiAnd),
LOr => Some(ast::BiOr),
Inplace | Assign | AssignOp(_) | As | DotDot => None
}
}
}