Skip to content

fix(grammar): precise error positions for malformed variable declarations - #72

Open
wadoon wants to merge 1 commit into
mainfrom
fix/issue-71-error-reporting
Open

wadoon wants to merge 1 commit into
mainfrom
fix/issue-71-error-reporting

Conversation

@wadoon

@wadoon wadoon commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

Fixes #71

Summary

Replaces the statement-level lookahead in javaparser-core/src/main/javacc/java.jj:

LOOKAHEAD( VariableDeclarationExpression() ";" )

with a purely syntactic lookahead:

LOOKAHEAD( Modifiers() Type() Identifier() ( ArrayBracketPair() )* ( "," | "=" | ";" ) )

Performing a full parse of the declarator inside the lookahead swallows
the parse error for malformed input such as int i = ;: error recovery
reports the start of the statement instead of the offending token. The
syntactic lookahead keeps the same disambiguation between local variable
declarations and other (incl. JML) statements, while letting the actual
VariableDeclarationExpression() parse report the exact error position.

Motivation

This restores the precise error positions documented in the issue, so
KeYProject/key#b7b5bb1
(and the accompanying error-message test changes) can be reverted.

Verification

  • Before: int i = ; reported (line 1,col 20) Parse error. Found "int" (statement start)

  • After: (line 1,col 28) Parse error. Found ";" (the offending =/;)

  • Parser regenerated with javacc, 0 errors.

  • Added regression test VariableDeclarationErrorPositionTest.

  • Full :jmlparser-core-testing:test, :jmlparser-jml-tests:test,
    :javaparser-key-testing:test and :jmlparser-symbol-solver-testing:test
    pass.

@github-code-quality

github-code-quality Bot commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

Code Coverage Overview

Languages: Java

Java / code-coverage/jacoco-ubuntu-latest-21

The overall line coverage in commit 8fabf60 in the fix/issue-71-error-r... branch remains at 57%, unchanged from commit 471cb6c in the main branch.


Updated October 02, 2026 23:08 UTC

…ions (#71)

Replace the LOOKAHEAD( VariableDeclarationExpression() ";" ) used to
disambiguate local variable declarations from other statements with a
purely syntactic lookahead:

    LOOKAHEAD( Modifiers() Type() Identifier() ( ArrayBracketPair() )* ( "," | "=" | ";" ) )

Performing a full parse of the declarator inside the lookahead swallows
the parse error of malformed input such as `int i = ;`: error recovery
then reports the start of the statement instead of the offending token.
The syntactic lookahead keeps the same disambiguation while letting the
actual VariableDeclarationExpression() parse report the precise error.

This restores the error positions required by KeY (see KeYProject/key
commit b7b5bb1), which can now be reverted together with the corresponding
error-message test modifications.

Adds a regression test asserting the error is reported at the offending
'=' token and not at the statement start.
@wadoon
wadoon force-pushed the fix/issue-71-error-reporting branch from f3c4f8d to 8fabf60 Compare October 2, 2026 23:02

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error Reporting

1 participant