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] Merge different increment/decrement expressions by oowekyala · Pull Request #1890 · pmd/pmd

Conversation

@oowekyala

@oowekyala oowekyala commented Jun 26, 2019

Copy link
Copy Markdown
Member

The current grammar has PreDecrementExpression, PreIncrementExpression, and PostfixExpression to represent mutation expressions.

  • This is asymmetric, and even more so because UnaryExpr has the same precedence as the former two
  • Most usages of those classes don't care about the different node type and try to get all of them.
    • That means, that most of the time, you'd just want to fetch increment and decrement expressions at the same time, whether they're postfix or prefix (also, whether they're ++ or --).

This PR makes two contributions:

  • It merges those three nodes into a single one, IncrementExpression. This simplifies some existing code by placing syntax (post-/prefix) to second rank, and removes the asymmetry. The operator is represented by the new IncrementOp, which adopts constants from UnaryOp (which didn't really belong there).
  • It introduces a subdivision of PrimaryExpression to represent those expressions that can be assigned ( evaluate to lvalues): ASTAssignableExpr.
    • Subclasses are FieldAccess, ArrayAccess, and VariableAccess.
    • VariableReference is renamed to VariableAccess for consistency
    • AssignableExprs have a new @AccessType attribute to check if they're read or written to.

Sorry, something went wrong.

Uh oh!

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

All reactions
oowekyala added 7 commits June 26, 2019 23:01
@oowekyala
Add assignable expression
4ef3156
@oowekyala
Test feature
ddb0ca9
@oowekyala
Merge Pre[Inc|Dec]rementExpr and PostfixExpr into IncrementExpr
613f6bb
@oowekyala
Rename VariableReference to VariableAccess
e59d10f
@oowekyala
Move AccessType into AssignableExpr
13c1f74
@oowekyala
Cleanup diff
866e839
@oowekyala
Cleanup impl
6a50cc5
@oowekyala oowekyala added the in:ast About the AST structure or API, the parsing step label Jun 26, 2019
@oowekyala oowekyala added this to the 7.0.0 milestone Jun 26, 2019
oowekyala added 4 commits June 27, 2019 08:46
@oowekyala
Remove unused node types
fb850ab
@oowekyala
Merge branch 'java-grammar' into grammar-assignable-expression
5fd8a26
@oowekyala
Merge branch 'java-grammar' into grammar-assignable-expression
556e713
@oowekyala
Fix node name
ac7e2a8
@adangel adangel self-assigned this Jul 31, 2019
adangel
adangel reviewed Aug 5, 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.

@oowekyala I've added a samples section here: https://github.com/pmd/pmd/wiki/Java_clean_changes#merge-different-increment-and-decrement-expressions
Could you please review? I'm unsure about how the PMD 7 AST looks like.

Only two things I'd change for now:

  • ASTIncrementExpression::getBaseExpression() -> getOperand()
  • TestExtension.kt: variableRef -> variableAccess

Let me know, if you agree and whether I should change it.

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/ASTIncrementExpression.java
*
* </pre>
*/
public final class ASTIncrementExpression extends AbstractJavaExpr implements ASTExpression, LeftRecursiveNode {

@adangel adangel Aug 5, 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.

I'm not so sure about the name. "IncrementExpression" doesn't sound right, since the node can also be a decrement expression...
But I also don't know a better name.

I had a look at how eclipse jdt exposes this: They have a PrefixExpression and a PostfixExpression. So they combine the grammar in a different way....
Is this something we should do as well? Maybe to keep it in mind (not for this PR though).

Sorry, something went wrong.

Uh oh!

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

All reactions

@oowekyala oowekyala Aug 5, 2019

Copy link
Copy Markdown
Member Author

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.

IMO PrefixExpr + PostfixExpr are not so great because they share the two operators ++ and --, and I'd rather not duplicate them in separate enums.

AFAIK there's no term for "decrement or increment" expression. Maybe "XcrementExpression"? Just kidding 😆

However there is a term for something that is "either postfix or prefix": "unary". Maybe then, it would make sense to merge IncrementExpression into UnaryExpression and have an attribute for whether it's prefix or postfix instead. That way we have a single node, and also a single enum for unary operators too. Wdyt?

Sorry, something went wrong.

Uh oh!

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

All reactions

@adangel adangel Aug 6, 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.

Not sure if merging IncrementExpr with UnaryExpr is helpful - In the current use cases in the rules, we would then need to check for the operator, right? (At least in AvoidReassigningLoopVariablesRule)

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/ASTIncrementExpression.java Resolved

Uh oh!

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

Comment thread pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/IncrementOp.java Resolved

Uh oh!

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

Comment thread ...java/net/sourceforge/pmd/lang/java/rule/bestpractices/AvoidReassigningLoopVariablesRule.java Resolved

Uh oh!

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

Comment thread pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/ASTArrayAccessTest.kt Resolved

Uh oh!

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

Comment thread pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/TestExtensions.kt Resolved

Uh oh!

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

oowekyala and others added 3 commits August 6, 2019 01:24
@oowekyala
Merge branch 'java-grammar' into grammar-assignable-expression
ca604ea
@oowekyala
Rename getBaseExpression to getOperand
7d99c84
@oowekyala @adangel
Update pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/Incre…
d0b3044
…mentOp.java

Co-Authored-By: Andreas Dangel <[email protected]>
adangel
adangel approved these changes Aug 6, 2019
View reviewed changes
@adangel
adangel merged commit d0b3044 into pmd:java-grammar Aug 6, 2019
@oowekyala
oowekyala deleted the grammar-assignable-expression branch August 6, 2019 15:11
oowekyala added a commit to oowekyala/pmd that referenced this pull request Nov 19, 2019
@oowekyala
Split UnaryExpr into Prefix and Postfix
b769629
Among the different possible categorisations
of unary expressions, this is probably the
most logical and easiest to document.

Here's a comparison of different possible
categorisations. Note: `_++` is the postfix
increment operator, while `++_` is the prefix
one - idem for decrement. The last one is the
one implemented by this commit.

\## 6.0.x

```
  Unary = { -, + }
  UnaryNotPlusMinus { !, ~ }
  PreIncrement = { ++_ }
  PreDecrement { --_ }
  Postfix { _++, _++ }
```

* Very assymmetric, splits operators based on parsing
concerns

\## Before pmd#1890:

```
  Unary = { -, + , !, ~ }
  PreIncrement = { ++_ }
  PreDecrement { --_ }
  Postfix { _++, _++ }
```

* Minor simplification

\## pmd#1890:

```
  Unary = Prefix \ { ++_, --_ })
  Increment ( { ++ , -- } x (postfix, prefix) )
```
* Names are weird (IncrementExpr may be decrement, Unary != Increment
  even though semantically, Increment \subset Unary)
* No possibility to introduce a supertype (what would it be?)
* But easy to match all increment/decrement expressions

\## JLS (also, Eclipse):

```
  Prefix = { !, ~, -, +, ++_, --_ }
  Postfix = { _++, _-- }
```

* Both can have super interface UnaryExpr
  * This allows matching all increment/decrement expressions easily too
* Easiest to document, JLS like, AST like
* Fits well with `InfixExpr`
@oowekyala oowekyala mentioned this pull request Dec 9, 2019
[java] Merge prefix/postfix expressions into one node #2155
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

@adangel adangel

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.