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] Make new java expression grammar stricter by oowekyala · Pull Request #1855 · pmd/pmd

Conversation

@oowekyala

@oowekyala oowekyala commented Jun 1, 2019

Copy link
Copy Markdown
Member

This makes the grammar of expressions much stricter than what it is currently on this branch. Currently eg foo()() "parses", but the jjtClose of ASTMethodCall fails, so we get a very undescriptive error message. Other absurdities like a::b[0]::c are parsed and end up in the final tree... Now we get regular syntax errors.

It also optimises the lookaheads in primary expression, although from what I measured:

  • the lookahead that we already removed in 96ea140 was by far the most costly method of the parser anyway.
  • time spent in IO in a typical run makes these optimisations pretty insignificant anyway. That's good though, that means that the parser is about as fast as could be :) This parser is also just as fast as the 6.0.x parser.

I tested the parser on the spring framework and the JDK 12 sources, and added some test cases when I found a few bugs (actually #1843 comes from JDK 12). It can parse those with no problem now.

I didn't add any negative tests, maybe we should do that


Details:

The change doesn't touch many things apart from the parser. I made it so though, that we rely less on jjtClose hooks and more on the parser directly. Some more nodes are LeftRecursiveNodes, but that just means that they get injected with some additional children to their left (the interface name has become not really descriptive).

We can also most of the time avoid using jjtree.extendLeft() and only use regular jjtree nodes with a definite number of children (but the the node needs to implement LeftRecursiveNode to fix the tokens).

The change to the parser boils down to making PrimarySuffix much dumber. It can actually only be a field access, method call, array access or method reference now. When the first method reference is parsed, we stop the iteration, to make eg. expr::foo.bar impossible.

PrimaryPrefix is split into two steps for readability. The production takes care of any start token that is not an ambiguous name. If the expression starts with an ambiguous name though (and only then), we follow to the next step, as still more options are possible than in primary suffix. Then we start iterating PrimarySuffix.

Sorry, something went wrong.

Uh oh!

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

All reactions
oowekyala added 22 commits May 26, 2019 16:39
@oowekyala
Optimise primary prefix lookaheads
0ef4ec7
@oowekyala
Cleanup
68fa619
@oowekyala
Fix constructor invoc ambiguity
8ab8009
@oowekyala
Cleanup tests
cbb153e
@oowekyala
Checkstyle
06d9e5a
@oowekyala
Fix ambiguity between type args & LT comp
cfc31e4
@oowekyala
Move lambda out of primary prefix
13f052d
@oowekyala
Fix explicit constructor invoc args
17abff7
@oowekyala
Fix lambda allowed in ternaries
c42723f
@oowekyala
Test method reference
5af835e
@oowekyala
Make super keyword stricter
066ec0f
@oowekyala
Simplify qualified this & super
c18b380
@oowekyala
Remove jjtclose hook of constructor call
691537e
@oowekyala
Remove jjtclose hook of array access
2379c09
@oowekyala
Forbid method references to be stacked
501bfa8
@oowekyala
Fix some corner cases found in jdk12 sources
0939d4e
@oowekyala
Fix type arg/shift ambig corner case
4d0042f
@oowekyala
Improve statement expression placement
45f040f
@oowekyala
REVERT ME Extract lambda changes
6a8cb2c
@oowekyala
Remove unused prod
0e16462
@oowekyala
Document
53d696a
@oowekyala
Merge branch 'java-grammar' into grammar-opt-expressions
7f6506c
@oowekyala oowekyala added an:enhancement An improvement on existing features / rules in:ast About the AST structure or API, the parsing step labels Jun 1, 2019
@oowekyala oowekyala added this to the 7.0.0 milestone Jun 1, 2019
@oowekyala oowekyala changed the title Grammar opt expressions [java] Make new java expression grammar stricter Jun 2, 2019
@oowekyala
Merge branch 'java-grammar' into grammar-opt-expressions
4159675
adangel
adangel approved these changes Jun 16, 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.

Nice job! 👍

I've tried to update https://github.com/pmd/pmd/wiki/PMD-7.0.0-Java

I tested the parser on the spring framework and the JDK 12 sources

Sounds like we should add openjd12 sources some time to pmd-regression-tester...

Sorry, something went wrong.

Uh oh!

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

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

Uh oh!

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

@adangel
adangel merged commit 4159675 into pmd:java-grammar Jun 16, 2019
@oowekyala
oowekyala deleted the grammar-opt-expressions branch June 17, 2019 00:48
@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

an:enhancement An improvement on existing features / rules 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.