4040 * @author Paul Bittner
4141 */
4242public class EliminateEmptyAlternatives implements Transformer <VariationTree <DiffLinesLabel >> {
43- private List <VariationTreeNode > nodesToDrop ;
4443 /**
4544 * Creates a copy of the given label but where the formula is set to the given formula.
4645 * This method also updates the text in the DiffLinesLabel accordingly so that the text is
@@ -61,11 +60,7 @@ private static DiffLinesLabel updatedLabel(DiffLinesLabel l, Node formula) {
6160 Assert .assertTrue (head .content ().contains ("if" ));
6261 final String indent = StringUtils .getLeadingWhitespace (head .content ());
6362
64- final NodeWriter nw = new NodeWriter (formula );
65- nw .setSymbols (NodeWriter .javaSymbols );
66- nw .setEnquoteWhitespace (true );
67- nw .setEnforceBrackets (true );
68- final String newText = indent + "#if " + nw .nodeToString ();
63+ final String newText = indent + "#if " + formula .toString (NodeWriter .javaSymbols );
6964
7065 // We might have replaced multiple lines by a single line here.
7166 // In this case, some line numbers got lost and any variation tree using this updated label somewhere might not
@@ -77,7 +72,7 @@ private static DiffLinesLabel updatedLabel(DiffLinesLabel l, Node formula) {
7772 );
7873 }
7974
80- private void elim (VariationTreeNode <DiffLinesLabel > subtree ) {
75+ private static void elim (VariationTreeNode <DiffLinesLabel > subtree ) {
8176 // We simplify only annotations.
8277 if (!subtree .isAnnotation ()) return ;
8378
@@ -90,7 +85,7 @@ private void elim(VariationTreeNode<DiffLinesLabel> subtree) {
9085 }
9186 // When there is exactly one child and that child is an 'else' or 'elif' we can simplify that nesting.
9287 else if (children .size () == 1 ) {
93- final VariationTreeNode <DiffLinesLabel > child = children .get ( 0 );
88+ final VariationTreeNode <DiffLinesLabel > child = children .getFirst ( );
9489
9590 if ((subtree .isIf () || subtree .isElif ()) && (child .isElif () || child .isElse ())) {
9691 // determine new feaure mapping
@@ -111,10 +106,6 @@ else if (children.size() == 1) {
111106
112107 @ Override
113108 public void transform (VariationTree <DiffLinesLabel > tree ) {
114- nodesToDrop = new ArrayList <>();
115- tree .forAllPostorder (subtree -> elim (subtree ));
116- for (VariationTreeNode <DiffLinesLabel > node : nodesToDrop ) {
117- node .drop ();
118- }
109+ tree .forAllPostorder (EliminateEmptyAlternatives ::elim );
119110 }
120111}
0 commit comments