Skip to content

Give AND higher precedence than OR in the parser - #1999

Open
anubhavlal07 wants to merge 1 commit into
rokucommunity:masterfrom
anubhavlal07:fix/and-or-precedence
Open

anubhavlal07 wants to merge 1 commit into
rokucommunity:masterfrom
anubhavlal07:fix/and-or-precedence

Conversation

@anubhavlal07

Copy link
Copy Markdown

Fixes #1994

Problem

Parser.boolean() handled and and or in one matchAny(TokenKind.And, TokenKind.Or) loop, so both operators had the same precedence and grouped left to right. a or b and c was parsed as (a or b) and c. The Roku device gives and higher precedence than or and evaluates a or (b and c).

Plain transpile output looked fine because the tokens are written back in their original order. Plugins that rebuild code from the AST, such as coverage instrumentation, got the wrong grouping.

Fix

boolean() is now split into two levels:

  • boolean() handles or, with booleanAnd() for each operand
  • booleanAnd() handles and, with relational() for each operand

Both levels are still left-associative. The caller (anonymousFunction) is unchanged, so ternary and ?? parsing are not affected. Transpiled output is the same.

Tests

Added precedence tests to src/parser/tests/expression/Boolean.spec.ts. They parse source text and check the shape of the resulting tree:

  • a or b and c -> (a or (b and c))
  • a and b or c -> ((a and b) or c)
  • a or b and c or d and e -> ((a or (b and c)) or (d and e))
  • a and b and c and a or b or c stay left-associative

Without the parser change, the first and third tests fail. The full suite passes with the change (3250 passing) and so does npm run lint.

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.

Parser gives AND and OR the same precedence, but the device evaluates AND first

1 participant