Skip to content

Commit e2da89e

Browse files
committed
Reduce repeated code in ControllingCExpressionVisitor
1 parent 392feed commit e2da89e

1 file changed

Lines changed: 21 additions & 89 deletions

File tree

src/main/java/org/variantsync/diffdetective/feature/cpp/ControllingCExpressionVisitor.java

Lines changed: 21 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -102,95 +102,47 @@ public StringBuilder visitUnaryOperator(CExpressionParser.UnaryOperatorContext c
102102
// ;
103103
@Override
104104
public StringBuilder visitNamespaceExpression(CExpressionParser.NamespaceExpressionContext ctx) {
105-
if (ctx.primaryExpression().size() > 1) {
106-
// primaryExpression (('*'|'/'|'%') primaryExpression)+
107-
// We have to abstract the arithmetic expression if there is more than one operand
108-
return ctx.accept(abstractingVisitor);
109-
} else {
110-
// primaryExpression
111-
// There is exactly one child expression
112-
return ctx.primaryExpression(0).accept(this);
113-
}
105+
return recurseOnSingleChild(ctx);
114106
}
115107

116108
// multiplicativeExpression
117109
// : primaryExpression (('*'|'/'|'%') primaryExpression)*
118110
// ;
119111
@Override
120112
public StringBuilder visitMultiplicativeExpression(CExpressionParser.MultiplicativeExpressionContext ctx) {
121-
if (ctx.namespaceExpression().size() > 1) {
122-
// primaryExpression (('*'|'/'|'%') primaryExpression)+
123-
// We have to abstract the arithmetic expression if there is more than one operand
124-
return ctx.accept(abstractingVisitor);
125-
} else {
126-
// primaryExpression
127-
// There is exactly one child expression
128-
return ctx.namespaceExpression(0).accept(this);
129-
}
113+
return recurseOnSingleChild(ctx);
130114
}
131115

132116
// additiveExpression
133117
// : multiplicativeExpression (('+'|'-') multiplicativeExpression)*
134118
// ;
135119
@Override
136120
public StringBuilder visitAdditiveExpression(CExpressionParser.AdditiveExpressionContext ctx) {
137-
if (ctx.multiplicativeExpression().size() > 1) {
138-
// multiplicativeExpression (('+'|'-') multiplicativeExpression)+
139-
// We have to abstract the arithmetic expression if there is more than one operand
140-
return ctx.accept(abstractingVisitor);
141-
} else {
142-
// multiplicativeExpression
143-
// There is exactly one child expression
144-
return ctx.multiplicativeExpression(0).accept(this);
145-
}
121+
return recurseOnSingleChild(ctx);
146122
}
147123

148124
// shiftExpression
149125
// : additiveExpression (('<<'|'>>') additiveExpression)*
150126
// ;
151127
@Override
152128
public StringBuilder visitShiftExpression(CExpressionParser.ShiftExpressionContext ctx) {
153-
if (ctx.additiveExpression().size() > 1) {
154-
// additiveExpression (('<<'|'>>') additiveExpression)+
155-
// We have to abstract the shift expression if there is more than one operand
156-
return ctx.accept(abstractingVisitor);
157-
} else {
158-
// additiveExpression
159-
// There is exactly one child expression
160-
return ctx.additiveExpression(0).accept(this);
161-
}
129+
return recurseOnSingleChild(ctx);
162130
}
163131

164132
// relationalExpression
165133
// : shiftExpression (('<'|'>'|'<='|'>=') shiftExpression)*
166134
// ;
167135
@Override
168136
public StringBuilder visitRelationalExpression(CExpressionParser.RelationalExpressionContext ctx) {
169-
if (ctx.shiftExpression().size() > 1) {
170-
// shiftExpression (('<'|'>'|'<='|'>=') shiftExpression)+
171-
// We have to abstract the relational expression if there is more than one operand
172-
return ctx.accept(abstractingVisitor);
173-
} else {
174-
// shiftExpression
175-
// There is exactly one child expression
176-
return ctx.shiftExpression(0).accept(this);
177-
}
137+
return recurseOnSingleChild(ctx);
178138
}
179139

180140
// equalityExpression
181141
// : relationalExpression (('=='| '!=') relationalExpression)*
182142
// ;
183143
@Override
184144
public StringBuilder visitEqualityExpression(CExpressionParser.EqualityExpressionContext ctx) {
185-
if (ctx.relationalExpression().size() > 1) {
186-
// relationalExpression (('=='| '!=') relationalExpression)+
187-
// We have to abstract the equality expression if there is more than one operand
188-
return ctx.accept(abstractingVisitor);
189-
} else {
190-
// relationalExpression
191-
// There is exactly one child expression
192-
return ctx.relationalExpression(0).accept(this);
193-
}
145+
return recurseOnSingleChild(ctx);
194146
}
195147

196148
// specialOperator
@@ -268,61 +220,31 @@ public StringBuilder visitAssignmentOperator(CExpressionParser.AssignmentOperato
268220
// ;
269221
@Override
270222
public StringBuilder visitExpression(CExpressionParser.ExpressionContext ctx) {
271-
if (ctx.assignmentExpression().size() > 1) {
272-
// assignmentExpression (',' assignmentExpression)+
273-
return ctx.accept(abstractingVisitor);
274-
} else {
275-
// assignmentExpression
276-
return ctx.assignmentExpression(0).accept(this);
277-
}
223+
return recurseOnSingleChild(ctx);
278224
}
279225

280226
// andExpression
281227
// : equalityExpression ( '&' equalityExpression)*
282228
// ;
283229
@Override
284230
public StringBuilder visitAndExpression(CExpressionParser.AndExpressionContext ctx) {
285-
if (ctx.equalityExpression().size() > 1) {
286-
// equalityExpression ( '&' equalityExpression)+
287-
// We have to abstract the 'and' expression if there is more than one operand
288-
return ctx.accept(abstractingVisitor);
289-
} else {
290-
// equalityExpression
291-
// There is exactly one child expression
292-
return ctx.equalityExpression(0).accept(this);
293-
}
231+
return recurseOnSingleChild(ctx);
294232
}
295233

296234
// exclusiveOrExpression
297235
// : andExpression ('^' andExpression)*
298236
// ;
299237
@Override
300238
public StringBuilder visitExclusiveOrExpression(CExpressionParser.ExclusiveOrExpressionContext ctx) {
301-
if (ctx.andExpression().size() > 1) {
302-
// andExpression ('^' andExpression)+
303-
// We have to abstract the xor expression if there is more than one operand
304-
return ctx.accept(abstractingVisitor);
305-
} else {
306-
// andExpression
307-
// There is exactly one child expression
308-
return ctx.andExpression(0).accept(this);
309-
}
239+
return recurseOnSingleChild(ctx);
310240
}
311241

312242
// inclusiveOrExpression
313243
// : exclusiveOrExpression ('|' exclusiveOrExpression)*
314244
// ;
315245
@Override
316246
public StringBuilder visitInclusiveOrExpression(CExpressionParser.InclusiveOrExpressionContext ctx) {
317-
if (ctx.exclusiveOrExpression().size() > 1) {
318-
// exclusiveOrExpression ('|' exclusiveOrExpression)+
319-
// We have to abstract the 'or' expression if there is more than one operand
320-
return ctx.accept(abstractingVisitor);
321-
} else {
322-
// exclusiveOrExpression
323-
// There is exactly one child expression
324-
return ctx.exclusiveOrExpression(0).accept(this);
325-
}
247+
return recurseOnSingleChild(ctx);
326248
}
327249

328250
// logicalAndExpression
@@ -365,4 +287,14 @@ private StringBuilder visitLogicalExpression(ParserRuleContext expressionContext
365287
}
366288
return sb;
367289
}
368-
}
290+
291+
private StringBuilder recurseOnSingleChild(ParserRuleContext ctx) {
292+
if (ctx.getChildCount() > 1) {
293+
// We have to abstract the expression if there is more than one operand
294+
return ctx.accept(abstractingVisitor);
295+
} else {
296+
// There is exactly one child expression so we recurse
297+
return ctx.getChild(0).accept(this);
298+
}
299+
}
300+
}

0 commit comments

Comments
 (0)