|
30 | 30 | import org.antlr.v4.runtime.ConsoleErrorListener; |
31 | 31 | import org.antlr.v4.runtime.RecognitionException; |
32 | 32 | import org.antlr.v4.runtime.Recognizer; |
33 | | -import org.antlr.v4.runtime.tree.ParseTreeWalker; |
34 | 33 |
|
35 | 34 | import java.io.IOException; |
36 | 35 | import java.nio.file.FileSystems; |
@@ -125,7 +124,7 @@ public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int |
125 | 124 | }); |
126 | 125 |
|
127 | 126 | UVLListener uvlListener = new UVLListener(); |
128 | | - ParseTreeWalker walker = new ParseTreeWalker(); |
| 127 | + IterativeParseTreeWalker walker = new IterativeParseTreeWalker(); |
129 | 128 | walker.walk(uvlListener, UVLJavaParser.constraintLine()); |
130 | 129 |
|
131 | 130 | return uvlListener.getConstraint(); |
@@ -302,7 +301,7 @@ public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int |
302 | 301 |
|
303 | 302 |
|
304 | 303 | UVLListener uvlListener = new UVLListener(); |
305 | | - ParseTreeWalker walker = new ParseTreeWalker(); |
| 304 | + IterativeParseTreeWalker walker = new IterativeParseTreeWalker(); |
306 | 305 | walker.walk(uvlListener, UVLJavaParser.featureModel()); |
307 | 306 | FeatureModel featureModel = null; |
308 | 307 |
|
@@ -508,28 +507,38 @@ private void validateTypeLevelConstraints(final FeatureModel featureModel) { |
508 | 507 | } |
509 | 508 |
|
510 | 509 | private boolean validateTypeLevelConstraint(final Constraint constraint) { |
511 | | - boolean result = true; |
512 | | - if (constraint instanceof ExpressionConstraint) { |
513 | | - String leftReturnType = ((ExpressionConstraint) constraint).getLeft().getReturnType(); |
514 | | - String rightReturnType = ((ExpressionConstraint) constraint).getRight().getReturnType(); |
| 510 | + final Deque<Constraint> stack = new ArrayDeque<>(); |
| 511 | + stack.push(constraint); |
515 | 512 |
|
516 | | - if (!(leftReturnType.equalsIgnoreCase(Constants.TRUE) || rightReturnType.equalsIgnoreCase(Constants.TRUE))) { |
517 | | - // if not attribute constraint |
518 | | - result = result && ((ExpressionConstraint) constraint).getLeft().getReturnType().equalsIgnoreCase(((ExpressionConstraint) constraint).getRight().getReturnType()); |
519 | | - } |
520 | | - if (!result) { |
521 | | - return false; |
522 | | - } |
523 | | - for (final Expression expr: ((ExpressionConstraint) constraint).getExpressionSubParts()) { |
524 | | - result = result && validateTypeLevelExpression(expr); |
| 513 | + while (!stack.isEmpty()) { |
| 514 | + final Constraint current = stack.pop(); |
| 515 | + |
| 516 | + if (current instanceof ExpressionConstraint) { |
| 517 | + final ExpressionConstraint expressionConstraint = (ExpressionConstraint) current; |
| 518 | + |
| 519 | + final String leftReturnType = expressionConstraint.getLeft().getReturnType(); |
| 520 | + final String rightReturnType = expressionConstraint.getRight().getReturnType(); |
| 521 | + |
| 522 | + if (!(leftReturnType.equalsIgnoreCase(Constants.TRUE) || rightReturnType.equalsIgnoreCase(Constants.TRUE))) { |
| 523 | + if (!leftReturnType.equalsIgnoreCase(rightReturnType)) { |
| 524 | + return false; |
| 525 | + } |
| 526 | + } |
| 527 | + |
| 528 | + for (final Expression expr : expressionConstraint.getExpressionSubParts()) { |
| 529 | + if (!validateTypeLevelExpression(expr)) { |
| 530 | + return false; |
| 531 | + } |
| 532 | + } |
525 | 533 | } |
526 | | - } |
527 | 534 |
|
528 | | - for (final Constraint subCons: constraint.getConstraintSubParts()) { |
529 | | - result = result && validateTypeLevelConstraint(subCons); |
| 535 | + final List<Constraint> subConstraints = current.getConstraintSubParts(); |
| 536 | + for (int i = subConstraints.size() - 1; i >= 0; i--) { |
| 537 | + stack.push(subConstraints.get(i)); |
| 538 | + } |
530 | 539 | } |
531 | 540 |
|
532 | | - return result; |
| 541 | + return true; |
533 | 542 | } |
534 | 543 |
|
535 | 544 | private boolean validateTypeLevelExpression(final Expression expression) { |
|
0 commit comments