Skip to content

Fix Operator Precedence#209

Open
rcosta358 wants to merge 2 commits intomainfrom
operator-precedence
Open

Fix Operator Precedence#209
rcosta358 wants to merge 2 commits intomainfrom
operator-precedence

Conversation

@rcosta358
Copy link
Copy Markdown
Collaborator

This PR fixes operator precedence in the LiquidJava refinements, where e.g. the operators + and - had the same precedence as *, /, and %. This could lead to unexpected bugs and forced users to explicitly group expressions in parentheses.

Also, the grammar rejected some valid nested boolean expressions, such as "_ == (true || false && false)", because && and || were not allowed in the exp rule.

Finally, the parser could accept only a valid prefix of the expression. For example, the refinement true false would be accepted as true instead of throwing a syntax error, silently ignoring false.

Changes

The grammar now defines expression parsing in a single left-recursive pred rule, making the precedence order explicit, with the last two being right associative:

!* / %+ -== != >= > <= <&&||-->? :

To make sure the whole refinement string is consumed, prog: start | ; was changed to to prog: start EOF; and parser.start() to parser.prog().

Examples

@Refinement("_ == 1 + 2 * 0")
int x = 1;
// Before: Refinement Error: #x⁰ == 1 is not a subtype of #x⁰ == 0
// After: No error
@Refinement("_ == (true || false && false)")
// Before: Syntax Error
// After: No error
@Refinement("true false")
// Before: No error
// After: Syntax Error

@rcosta358 rcosta358 self-assigned this May 5, 2026
@rcosta358 rcosta358 requested a review from CatarinaGamboa May 5, 2026 12:46
@rcosta358 rcosta358 added bug Something isn't working enhancement New feature or request labels May 5, 2026
Copy link
Copy Markdown
Collaborator

@CatarinaGamboa CatarinaGamboa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok this looks good

| pred BOOLOP pred #expBool
| pred '&&' pred #predLogic
| pred '||' pred #predLogic
| <assoc=right> pred '-->' pred #predLogic
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok didnt know you could use <assoc=right>

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

Labels

bug Something isn't working enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants