|
33 | 33 | import org.antlr.v4.runtime.ConsoleErrorListener; |
34 | 34 | import org.antlr.v4.runtime.RecognitionException; |
35 | 35 | import org.antlr.v4.runtime.Recognizer; |
36 | | -import org.antlr.v4.runtime.tree.ParseTreeWalker; |
37 | 36 |
|
38 | 37 | import java.io.IOException; |
39 | 38 | import java.nio.file.FileSystems; |
@@ -134,7 +133,8 @@ public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int |
134 | 133 | }); |
135 | 134 |
|
136 | 135 | UVLListener uvlListener = createUVLListener(); |
137 | | - ParseTreeWalker walker = new ParseTreeWalker(); |
| 136 | + IterativeParseTreeWalker walker = new IterativeParseTreeWalker(); |
| 137 | + |
138 | 138 | walker.walk(uvlListener, UVLJavaParser.constraintLine()); |
139 | 139 |
|
140 | 140 | return uvlListener.getConstraint(); |
@@ -314,8 +314,10 @@ public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int |
314 | 314 | }); |
315 | 315 |
|
316 | 316 |
|
| 317 | + |
317 | 318 | UVLListener uvlListener = createUVLListener(); |
318 | | - ParseTreeWalker walker = new ParseTreeWalker(); |
| 319 | + IterativeParseTreeWalker walker = new IterativeParseTreeWalker(); |
| 320 | + |
319 | 321 | walker.walk(uvlListener, UVLJavaParser.featureModel()); |
320 | 322 | FeatureModel featureModel = null; |
321 | 323 |
|
@@ -521,28 +523,38 @@ private void validateTypeLevelConstraints(final FeatureModel featureModel) { |
521 | 523 | } |
522 | 524 |
|
523 | 525 | private boolean validateTypeLevelConstraint(final Constraint constraint) { |
524 | | - boolean result = true; |
525 | | - if (constraint instanceof ExpressionConstraint) { |
526 | | - String leftReturnType = ((ExpressionConstraint) constraint).getLeft().getReturnType(); |
527 | | - String rightReturnType = ((ExpressionConstraint) constraint).getRight().getReturnType(); |
| 526 | + final Deque<Constraint> stack = new ArrayDeque<>(); |
| 527 | + stack.push(constraint); |
528 | 528 |
|
529 | | - if (!(leftReturnType.equalsIgnoreCase(Constants.TRUE) || rightReturnType.equalsIgnoreCase(Constants.TRUE))) { |
530 | | - // if not attribute constraint |
531 | | - result = result && ((ExpressionConstraint) constraint).getLeft().getReturnType().equalsIgnoreCase(((ExpressionConstraint) constraint).getRight().getReturnType()); |
532 | | - } |
533 | | - if (!result) { |
534 | | - return false; |
535 | | - } |
536 | | - for (final Expression expr: ((ExpressionConstraint) constraint).getExpressionSubParts()) { |
537 | | - result = result && validateTypeLevelExpression(expr); |
| 529 | + while (!stack.isEmpty()) { |
| 530 | + final Constraint current = stack.pop(); |
| 531 | + |
| 532 | + if (current instanceof ExpressionConstraint) { |
| 533 | + final ExpressionConstraint expressionConstraint = (ExpressionConstraint) current; |
| 534 | + |
| 535 | + final String leftReturnType = expressionConstraint.getLeft().getReturnType(); |
| 536 | + final String rightReturnType = expressionConstraint.getRight().getReturnType(); |
| 537 | + |
| 538 | + if (!(leftReturnType.equalsIgnoreCase(Constants.TRUE) || rightReturnType.equalsIgnoreCase(Constants.TRUE))) { |
| 539 | + if (!leftReturnType.equalsIgnoreCase(rightReturnType)) { |
| 540 | + return false; |
| 541 | + } |
| 542 | + } |
| 543 | + |
| 544 | + for (final Expression expr : expressionConstraint.getExpressionSubParts()) { |
| 545 | + if (!validateTypeLevelExpression(expr)) { |
| 546 | + return false; |
| 547 | + } |
| 548 | + } |
538 | 549 | } |
539 | | - } |
540 | 550 |
|
541 | | - for (final Constraint subCons: constraint.getConstraintSubParts()) { |
542 | | - result = result && validateTypeLevelConstraint(subCons); |
| 551 | + final List<Constraint> subConstraints = current.getConstraintSubParts(); |
| 552 | + for (int i = subConstraints.size() - 1; i >= 0; i--) { |
| 553 | + stack.push(subConstraints.get(i)); |
| 554 | + } |
543 | 555 | } |
544 | 556 |
|
545 | | - return result; |
| 557 | + return true; |
546 | 558 | } |
547 | 559 |
|
548 | 560 | private boolean validateTypeLevelExpression(final Expression expression) { |
|
0 commit comments