@@ -40,27 +40,24 @@ private String formatExpression(Expression expression) {
4040 return expression .accept (this );
4141 }
4242
43- private String formatParentheses (Expression child , boolean shouldWrap ) {
44- Expression expression = unwrapGroup (child );
43+ private String formatExpression (Expression expression , boolean shouldWrap ) {
44+ expression = unwrapGroup (expression );
4545 if (shouldWrap )
4646 return "(" + formatExpression (expression ) + ")" ;
4747 return formatExpression (expression );
4848 }
4949
50- private String formatLeftOperand (Expression parent , Expression child ) {
51- return formatParentheses (child , needsLeftParentheses (parent , child ));
52- }
53-
54- private String formatRightOperand (BinaryExpression parent , Expression child ) {
55- return formatParentheses (child , needsRightParentheses (parent , child ));
50+ private String formatOperand (Expression parent , Expression child , boolean rightOperand ) {
51+ child = unwrapGroup (child );
52+ return formatExpression (child , needsParentheses (parent , child , rightOperand ));
5653 }
5754
5855 private String formatCondition (Expression child ) {
59- return formatParentheses (child , unwrapGroup (child ) instanceof Ite );
56+ return formatExpression (child , unwrapGroup (child ) instanceof Ite );
6057 }
6158
6259 private String formatArguments (List <Expression > args ) {
63- return args .stream ().map (expression -> formatParentheses (expression , false )).collect (Collectors .joining (", " ));
60+ return args .stream ().map (expression -> formatExpression (expression , false )).collect (Collectors .joining (", " ));
6461 }
6562
6663 private Expression unwrapGroup (Expression expression ) {
@@ -69,35 +66,28 @@ private Expression unwrapGroup(Expression expression) {
6966 return expression ;
7067 }
7168
72- private boolean needsParentheses (Expression parent , Expression child ) {
73- return ExpressionPrecedence .of (child ).isLowerThan (ExpressionPrecedence .of (parent ));
74- }
75-
76- private boolean needsLeftParentheses (Expression parent , Expression child ) {
77- if (needsParentheses (parent , child ))
69+ private boolean needsParentheses (Expression parent , Expression child , boolean rightOperand ) {
70+ ExpressionPrecedence parentPrecedence = ExpressionPrecedence .of (parent );
71+ ExpressionPrecedence childPrecedence = ExpressionPrecedence .of (child );
72+ if (childPrecedence .isLowerThan (parentPrecedence ))
7873 return true ;
79-
80- Expression unwrappedChild = unwrapGroup (child );
81- if (ExpressionPrecedence .of (unwrappedChild ) != ExpressionPrecedence .of (parent ))
74+ if (childPrecedence != parentPrecedence )
8275 return false ;
8376
84- return parent instanceof BinaryExpression binary && isRightAssociative (binary .getOperator ())
85- && unwrappedChild instanceof BinaryExpression ;
86- }
77+ if (parent instanceof BinaryExpression parentBinary && child instanceof BinaryExpression childBinary )
78+ return needsBinaryParentheses (parentBinary , childBinary , rightOperand );
8779
88- private boolean needsRightParentheses (BinaryExpression parent , Expression child ) {
89- if (needsParentheses (parent , child ))
90- return true ;
80+ if (parent instanceof UnaryExpression parentUnary && child instanceof UnaryExpression childUnary )
81+ return parentUnary .getOp ().equals ("-" ) && childUnary .getOp ().equals ("-" );
9182
92- Expression unwrappedChild = unwrapGroup (child );
93- if (ExpressionPrecedence .of (unwrappedChild ) != ExpressionPrecedence .of (parent ))
94- return false ;
83+ return false ;
84+ }
9585
96- if (unwrappedChild instanceof BinaryExpression right )
86+ private boolean needsBinaryParentheses (BinaryExpression parent , BinaryExpression child , boolean rightOperand ) {
87+ if (rightOperand )
9788 return !isRightAssociative (parent .getOperator ())
98- && (!isAssociative (parent .getOperator ()) || !parent .getOperator ().equals (right .getOperator ()));
99-
100- return false ;
89+ && (!isAssociative (parent .getOperator ()) || !parent .getOperator ().equals (child .getOperator ()));
90+ return isRightAssociative (parent .getOperator ());
10191 }
10292
10393 private boolean isAssociative (String operator ) {
@@ -115,8 +105,8 @@ public String visitAliasInvocation(AliasInvocation alias) {
115105
116106 @ Override
117107 public String visitBinaryExpression (BinaryExpression exp ) {
118- return formatLeftOperand (exp , exp .getFirstOperand ()) + " " + exp .getOperator () + " "
119- + formatRightOperand (exp , exp .getSecondOperand ());
108+ return formatOperand (exp , exp .getFirstOperand (), false ) + " " + exp .getOperator () + " "
109+ + formatOperand (exp , exp .getSecondOperand (), true );
120110 }
121111
122112 @ Override
@@ -132,7 +122,7 @@ public String visitGroupExpression(GroupExpression exp) {
132122 @ Override
133123 public String visitIte (Ite ite ) {
134124 return formatCondition (ite .getCondition ()) + " ? " + formatCondition (ite .getThen ()) + " : "
135- + formatLeftOperand (ite , ite .getElse ());
125+ + formatOperand (ite , ite .getElse (), true );
136126 }
137127
138128 @ Override
@@ -167,10 +157,7 @@ public String visitLiteralString(LiteralString lit) {
167157
168158 @ Override
169159 public String visitUnaryExpression (UnaryExpression exp ) {
170- Expression child = unwrapGroup (exp .getExpression ());
171- boolean nestedMinus = child instanceof UnaryExpression unary && exp .getOp ().equals ("-" )
172- && unary .getOp ().equals ("-" );
173- return exp .getOp () + formatParentheses (child , needsParentheses (exp , child ) || nestedMinus );
160+ return exp .getOp () + formatOperand (exp , exp .getExpression (), true );
174161 }
175162
176163 @ Override
0 commit comments