@@ -41,42 +41,61 @@ private String formatExpression(Expression expression) {
4141 }
4242
4343 private String formatParentheses (Expression child , boolean shouldWrap ) {
44+ Expression expression = unwrapGroup (child );
4445 if (shouldWrap )
45- return "(" + formatExpression (child ) + ")" ;
46- if (child instanceof GroupExpression group )
47- return "(" + formatExpression (group .getExpression ()) + ")" ;
48- return formatExpression (child );
46+ return "(" + formatExpression (expression ) + ")" ;
47+ return formatExpression (expression );
4948 }
5049
51- private String formatOperand (Expression parent , Expression child ) {
52- return formatParentheses (child , needsParentheses (parent , child ));
50+ private String formatLeftOperand (Expression parent , Expression child ) {
51+ return formatParentheses (child , needsLeftParentheses (parent , child ));
5352 }
5453
5554 private String formatRightOperand (BinaryExpression parent , Expression child ) {
5655 return formatParentheses (child , needsRightParentheses (parent , child ));
5756 }
5857
5958 private String formatCondition (Expression child ) {
60- return formatParentheses (child , child instanceof Ite );
59+ return formatParentheses (child , unwrapGroup ( child ) instanceof Ite );
6160 }
6261
6362 private String formatArguments (List <Expression > args ) {
6463 return args .stream ().map (expression -> formatParentheses (expression , false )).collect (Collectors .joining (", " ));
6564 }
6665
66+ private Expression unwrapGroup (Expression expression ) {
67+ while (expression instanceof GroupExpression group )
68+ expression = group .getExpression ();
69+ return expression ;
70+ }
71+
6772 private boolean needsParentheses (Expression parent , Expression child ) {
6873 return ExpressionPrecedence .of (child ).isLowerThan (ExpressionPrecedence .of (parent ));
6974 }
7075
76+ private boolean needsLeftParentheses (Expression parent , Expression child ) {
77+ if (needsParentheses (parent , child ))
78+ return true ;
79+
80+ Expression unwrappedChild = unwrapGroup (child );
81+ if (ExpressionPrecedence .of (unwrappedChild ) != ExpressionPrecedence .of (parent ))
82+ return false ;
83+
84+ return parent instanceof BinaryExpression binary && isRightAssociative (binary .getOperator ())
85+ && unwrappedChild instanceof BinaryExpression ;
86+ }
87+
7188 private boolean needsRightParentheses (BinaryExpression parent , Expression child ) {
7289 if (needsParentheses (parent , child ))
7390 return true ;
7491
75- if (ExpressionPrecedence .of (child ) != ExpressionPrecedence .of (parent ))
92+ Expression unwrappedChild = unwrapGroup (child );
93+ if (ExpressionPrecedence .of (unwrappedChild ) != ExpressionPrecedence .of (parent ))
7694 return false ;
7795
78- if (child instanceof BinaryExpression right )
79- return !isAssociative (parent .getOperator ()) || !parent .getOperator ().equals (right .getOperator ());
96+ if (unwrappedChild instanceof BinaryExpression right )
97+ return !isRightAssociative (parent .getOperator ())
98+ && (!isAssociative (parent .getOperator ()) || !parent .getOperator ().equals (right .getOperator ()));
8099
81100 return false ;
82101 }
@@ -85,14 +104,18 @@ private boolean isAssociative(String operator) {
85104 return operator .equals ("&&" ) || operator .equals ("||" ) || operator .equals ("+" ) || operator .equals ("*" );
86105 }
87106
107+ private boolean isRightAssociative (String operator ) {
108+ return operator .equals ("-->" );
109+ }
110+
88111 @ Override
89112 public String visitAliasInvocation (AliasInvocation alias ) {
90113 return alias .getName () + "(" + formatArguments (alias .getArgs ()) + ")" ;
91114 }
92115
93116 @ Override
94117 public String visitBinaryExpression (BinaryExpression exp ) {
95- return formatOperand (exp , exp .getFirstOperand ()) + " " + exp .getOperator () + " "
118+ return formatLeftOperand (exp , exp .getFirstOperand ()) + " " + exp .getOperator () + " "
96119 + formatRightOperand (exp , exp .getSecondOperand ());
97120 }
98121
@@ -103,13 +126,13 @@ public String visitFunctionInvocation(FunctionInvocation fun) {
103126
104127 @ Override
105128 public String visitGroupExpression (GroupExpression exp ) {
106- return "(" + formatExpression (exp .getExpression ()) + ")" ;
129+ return formatExpression (exp .getExpression ());
107130 }
108131
109132 @ Override
110133 public String visitIte (Ite ite ) {
111134 return formatCondition (ite .getCondition ()) + " ? " + formatCondition (ite .getThen ()) + " : "
112- + formatOperand (ite , ite .getElse ());
135+ + formatLeftOperand (ite , ite .getElse ());
113136 }
114137
115138 @ Override
@@ -144,7 +167,10 @@ public String visitLiteralString(LiteralString lit) {
144167
145168 @ Override
146169 public String visitUnaryExpression (UnaryExpression exp ) {
147- return exp .getOp () + formatOperand (exp , exp .getExpression ());
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 );
148174 }
149175
150176 @ Override
0 commit comments