Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[java] Remove ParenthesizedExpr #1872

Merged
merged 4 commits into from
Jun 19, 2019

Conversation

oowekyala
Copy link
Member

Removes ParenthesizedExpr from the current version of java-grammar. It's replaced by an attribute of ASTExpression, that stores the number of parentheses levels around the expression. Most rules, and all analysis passes, would never care about parentheses. So having an extra node causes the following problems:

  • if the parens are unnecessary, then semantically equivalent expressions are parsed differently, which introduces corner cases the rules should potentially deal with...
  • if the parens are necessary, then they can be inferred by the context (precedence of the operators outside and inside the parens), and in that sense are redundant

So eg as today, 1+2*3 is parsed

+ AdditiveExpr
   + `1`
   + MultiplicativeExpr
      + `2`
      + `3` 

but now, this is the same as 1+(2*3) (except in the latter the multiplicative expression has @Parenthesized set to true).

OTOH (1+2)*3 is parsed as

+ MultiplicativeExpression
   + AdditiveExpr [ @Parenthesized = true() ]
      + `1`
      + `2`
   + `3` 

Notice, that this really is a simplification, since because of operator precedence rules, the only way you can nest an addition into a multiplication is with parentheses anyway. That means, that the ParenthesizedExpr in this context was entirely redundant.

Finally, 1 + 2 + 3 is parsed as

+ AdditiveExpr
  + `1`
  + `2`
  + `3` 

whereas (1+2)+3 is parsed as

+ AdditiveExpr
  + AdditiveExpr [ @Parenthesized = true() ]
     + `1`
     + `2`
  + `3` 

meaning, we have to preserve the parens on a node that would otherwise have been flattened - even though they're unnecessary... But that's in fact consistent with 1+(2+3), which already is not flattened either:

+ AdditiveExpr
  + `1` 
  + AdditiveExpr [ @Parenthesized = true() ]
     + `2`
     + `3`

@oowekyala oowekyala added the in:ast About the AST structure or API, the parsing step label Jun 17, 2019
@oowekyala oowekyala added this to the 7.0.0 milestone Jun 17, 2019
Copy link
Member

@adangel adangel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoiding the extra node level in the AST makes sense. The information about the parentheses is still retained, so rules like UselessParentheses are still possible.

* of the JLS. One difference though, is that this production
* also matches lambda expressions, contrary to the JLS.
* This corresponds to the <a href="https://docs.oracle.com/javase/specs/jls/se9/html/jls-15.html#jls-Expression">Expression</a>
* of the JLS.
*
* <p>From 7.0.0 on, this is an interface which all expression nodes
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like we should add this comment on the master branch for PMD 6. Here it is already a interface...

@@ -47,8 +47,16 @@ public boolean isExpressionBody() {
return !isBlockBody();
}

// TODO these are copied from AbstractJavaTypeNode because it's easier to extend abstractMethodLikeNode
// TODO MethodLikeNode should be removed, and this class extend AbstractJavaExpr
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like MethodLikeNode is mostly used in metrics. I see, this is already a point on https://github.com/pmd/pmd/wiki/PMD-7.0.0-Java

@adangel adangel merged commit dd074c9 into pmd:java-grammar Jun 19, 2019
@oowekyala oowekyala deleted the grammar-parentheses-flat branch June 19, 2019 21:54
@adangel adangel mentioned this pull request Jan 23, 2023
55 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in:ast About the AST structure or API, the parsing step
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants