GeezerVison is showing github.com — [java] Make binary operators left-recursive by oowekyala · Pull Request #1979 · pmd/pmd
Conversation
oowekyala
commented
Aug 15, 2019
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
So this should fix #1661 definitively
The structure of infix expressions is now always the same: a left-hand side, an operator, a right hand side. Since having different node types for each of those adds no information that the operator constant doesn't already provide, I think it's useful to merge all those nodes (there's 10) into a single one. This decreases the size of the API, and is for example what Eclipse JDT does
I also experimented with generating nice tree images for our javadocs. For example:

Unfortunately rendering the images is not really portable for now, as the build machine needs a LaTeX distribution. So I prerendered them and committed them here.
Copy of the discussion on left-recursiveness
What to do about #1661 (operator nodes) ? Sensible solution would be to make AdditiveExpression be parsed completely left-recursively, but then, it looks weird that all other binary expressions are not left-recursive…
The problem also arises for operators &, |, ^, which may be boolean non-shortcut expressions or numeric bitwise expressions
Arguments for left recursiveness:
- Preserve all intermediary type resolution results, because of numeric promotion -> the whole point of [java] RFC: new layer of abstraction atop PrimaryExpressions #497
- AST is JLS-like, easier to document (we don’t have to explain “sometimes it’s left-recursive and sometimes not”)
((1 + 4) + returnString()) = “14” “5”
((1 + 4) + 4) == (1 + 4 + 4)
- Easier mapping of type resolution algorithms from the JLS
- Stronger contract for binary expressions: all can present the same interface:
getLhs() : ASTExpression
getRhs() : ASTExpression
getOperator() : BinaryOp
Arguments against:
- Not super intuitive
- Breaks away from previous AST structure, needs to be relearned
- Needs more tooling for some use cases:
Eg how to catch a string concat statement like a = … + a + …; (use case taken from #1932's AvoidConcatInLoop)
- In Java we can imagine the following method for binary expressions which yields all operands that are added with the same operator:
operandStream(): NodeStream<ASTExpression>
Eg for <a + b + c * d>, operandStream returns Stream.of(<a>, <b>, <c * d>). So you can do eg:
additiveExpr.operandStream()
.filter(ASTVariableAccess.class)
.any(ref -> ref.getName().equals(“a”))
- However in XPath it’s less trivial.. We’d have to provide access to the node stream with a function:
- Old way, with flat additive expr:
AdditiveExpression[@Operator=”+”]/VariableReference[@Image = “a”]
AdditiveExpression[@Operator=”+”]
[pmd-java:operandsOf(.)/self::VariableReference[@Image = “a”]]
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
1 Message
|
| 📖 |
No java rules are changed! |
Generated by 🚫 Danger
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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
Development
Successfully merging this pull request may close these issues.
Uh oh!
There was an error while loading. Please reload this page.
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.
|