GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [java] Remove ParenthesizedExpr by oowekyala · Pull Request #1872 · pmd/pmd

Conversation

@oowekyala

@oowekyala oowekyala commented Jun 17, 2019

Copy link
Copy Markdown
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`

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
oowekyala added 2 commits June 17, 2019 20:23
@oowekyala
Represent parentheses with attribute, not node
62ea1d1
@oowekyala
Add some tests
946e64a
@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
oowekyala added 2 commits June 18, 2019 15:08
@oowekyala
Remove default method
68ed455
@oowekyala
Merge branch 'java-grammar' into grammar-parentheses-flat
dd074c9
adangel
adangel approved these changes Jun 19, 2019
View reviewed changes

@adangel adangel left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this 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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTExpression.java
* 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

@adangel adangel Jun 19, 2019

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

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...

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTLambdaExpression.java
}

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

@adangel adangel Jun 19, 2019

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@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
@oowekyala oowekyala mentioned this pull request Sep 28, 2019
[java] Grammar type expr #2039
Merged
@adangel adangel mentioned this pull request Jan 23, 2023
PMD 7 Tracking Issue #3898
Closed
55 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@adangel adangel adangel approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

in:ast About the AST structure or API, the parsing step

Projects

None yet

Milestone

7.0.0

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@oowekyala @adangel
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.