@@ -132,12 +132,13 @@ void RCParser::consume() {
132132//
133133// The following grammar is used to parse the expressions Exp1:
134134// Exp1 ::= Exp2 || Exp1 + Exp2 || Exp1 - Exp2 || Exp1 | Exp2 || Exp1 & Exp2
135- // Exp2 ::= -Exp2 || ~Exp2 || not Expr2 || Int || (Exp1).
136- // (More conveniently, Exp1 is a non-empty sequence of Exp2 expressions,
137- // separated by binary operators.)
135+ // Exp2 ::= Exp3 || Exp3 * Exp3 || Exp3 / Exp3
136+ // Exp3 ::= -Exp3 || ~Exp3 || not Expr3 || Int || (Exp1)
137+ // (More conveniently, Exp1 and Exp2 are non-empty sequences of Exp3
138+ // expressions, separated by binary operators.)
138139//
139- // Expressions of type Exp1 are read by parseIntExpr1(Inner) method, while Exp2
140- // is read by parseIntExpr2().
140+ // Expressions of type Exp1 are read by parseIntExpr1(Inner) method, Exp2
141+ // is read by parseIntExpr2() and Exp3 is read by parseIntExpr3() .
141142//
142143// The original Microsoft tool handles multiple unary operators incorrectly.
143144// For example, in 16-bit little-endian integers:
@@ -158,7 +159,7 @@ Expected<IntWithNotMask> RCParser::parseIntExpr1() {
158159 ASSIGN_OR_RETURN (FirstResult, parseIntExpr2 ());
159160 IntWithNotMask Result = *FirstResult;
160161
161- while (!isEof () && look ().isBinaryOp ()) {
162+ while (!isEof () && look ().isLowPrecedenceBinaryOp ()) {
162163 auto OpToken = read ();
163164 ASSIGN_OR_RETURN (NextResult, parseIntExpr2 ());
164165
@@ -180,15 +181,41 @@ Expected<IntWithNotMask> RCParser::parseIntExpr1() {
180181 break ;
181182
182183 default :
183- llvm_unreachable (" Already processed all binary ops." );
184+ llvm_unreachable (" Already processed all low precedence binary ops." );
184185 }
185186 }
186187
187188 return Result;
188189}
189190
190191Expected<IntWithNotMask> RCParser::parseIntExpr2 () {
191- // Exp2 ::= -Exp2 || ~Exp2 || not Expr2 || Int || (Exp1).
192+ // Exp2 ::= Exp3 || Exp3 * Exp3 || Exp3 / Exp3.
193+ ASSIGN_OR_RETURN (FirstResult, parseIntExpr3 ());
194+ IntWithNotMask Result = *FirstResult;
195+
196+ while (!isEof () && look ().isHighPrecedenceBinaryOp ()) {
197+ auto OpToken = read ();
198+ ASSIGN_OR_RETURN (NextResult, parseIntExpr3 ());
199+
200+ switch (OpToken.kind ()) {
201+ case Kind::Asterisk:
202+ Result *= *NextResult;
203+ break ;
204+
205+ case Kind::Slash:
206+ Result /= *NextResult;
207+ break ;
208+
209+ default :
210+ llvm_unreachable (" Already processed all high precedence binary ops." );
211+ }
212+ }
213+
214+ return Result;
215+ }
216+
217+ Expected<IntWithNotMask> RCParser::parseIntExpr3 () {
218+ // Exp3 ::= -Exp3 || ~Exp3 || not Expr3 || Int || (Exp1).
192219 static const char ErrorMsg[] = " '-', '~', integer or '('" ;
193220
194221 if (isEof ())
@@ -197,13 +224,13 @@ Expected<IntWithNotMask> RCParser::parseIntExpr2() {
197224 switch (look ().kind ()) {
198225 case Kind::Minus: {
199226 consume ();
200- ASSIGN_OR_RETURN (Result, parseIntExpr2 ());
227+ ASSIGN_OR_RETURN (Result, parseIntExpr3 ());
201228 return -(*Result);
202229 }
203230
204231 case Kind::Tilde: {
205232 consume ();
206- ASSIGN_OR_RETURN (Result, parseIntExpr2 ());
233+ ASSIGN_OR_RETURN (Result, parseIntExpr3 ());
207234 return ~(*Result);
208235 }
209236
@@ -220,7 +247,7 @@ Expected<IntWithNotMask> RCParser::parseIntExpr2() {
220247 case Kind::Identifier: {
221248 if (!read ().value ().equals_insensitive (" not" ))
222249 return getExpectedError (ErrorMsg, true );
223- ASSIGN_OR_RETURN (Result, parseIntExpr2 ());
250+ ASSIGN_OR_RETURN (Result, parseIntExpr3 ());
224251 return IntWithNotMask (0 , (*Result).getValue ());
225252 }
226253
0 commit comments