From b64b0afe0cea6a3110a804e163e2f50042a1ef1e Mon Sep 17 00:00:00 2001 From: Anubhav Date: Sat, 21 Nov 2026 19:54:34 +0530 Subject: [PATCH] Give AND higher precedence than OR --- src/parser/Parser.ts | 16 +++++++- src/parser/tests/expression/Boolean.spec.ts | 43 +++++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/src/parser/Parser.ts b/src/parser/Parser.ts index 60254e7b3..ad66ac42d 100644 --- a/src/parser/Parser.ts +++ b/src/parser/Parser.ts @@ -2560,9 +2560,23 @@ export class Parser { } private boolean(): Expression { + let expr = this.booleanAnd(); + + while (this.match(TokenKind.Or)) { + let operator = this.previous(); + this.consumeNewlinesIfAllowed(); + let right = this.booleanAnd(); + this.addExpressionsToReferences(expr, right); + expr = new BinaryExpression(expr, operator, right); + } + + return expr; + } + + private booleanAnd(): Expression { let expr = this.relational(); - while (this.matchAny(TokenKind.And, TokenKind.Or)) { + while (this.match(TokenKind.And)) { let operator = this.previous(); this.consumeNewlinesIfAllowed(); let right = this.relational(); diff --git a/src/parser/tests/expression/Boolean.spec.ts b/src/parser/tests/expression/Boolean.spec.ts index d7d18ac93..19145f32d 100644 --- a/src/parser/tests/expression/Boolean.spec.ts +++ b/src/parser/tests/expression/Boolean.spec.ts @@ -4,6 +4,8 @@ import { Parser } from '../../Parser'; import { TokenKind } from '../../../lexer/TokenKind'; import { EOF, identifier, token } from '../Parser.spec'; import { Range } from 'vscode-languageserver'; +import type { AssignmentStatement } from '../../Statement'; +import type { BinaryExpression, VariableExpression } from '../../Expression'; describe('parser boolean expressions', () => { @@ -87,4 +89,45 @@ describe('parser boolean expressions', () => { Range.create(0, 4, 0, 18) ); }); + + describe('precedence', () => { + function stringify(expr: any): string { + if (expr.operator) { + return `(${stringify(expr.left)} ${expr.operator.text} ${stringify(expr.right)})`; + } + return (expr as VariableExpression).name.text; + } + + function parseValue(text: string) { + const { statements, diagnostics } = Parser.parse(text); + expect(diagnostics).to.be.lengthOf(0); + return (statements[0] as AssignmentStatement).value as BinaryExpression; + } + + it('binds AND tighter than OR when OR comes first', () => { + const value = parseValue('x = a or b and c'); + expect(value.operator.kind).to.equal(TokenKind.Or); + expect(stringify(value)).to.equal('(a or (b and c))'); + }); + + it('binds AND tighter than OR when AND comes first', () => { + const value = parseValue('x = a and b or c'); + expect(value.operator.kind).to.equal(TokenKind.Or); + expect(stringify(value)).to.equal('((a and b) or c)'); + }); + + it('binds AND tighter than OR in longer chains', () => { + expect( + stringify(parseValue('x = a or b and c or d and e')) + ).to.equal('((a or (b and c)) or (d and e))'); + }); + + it('keeps AND left-associative', () => { + expect(stringify(parseValue('x = a and b and c'))).to.equal('((a and b) and c)'); + }); + + it('keeps OR left-associative', () => { + expect(stringify(parseValue('x = a or b or c'))).to.equal('((a or b) or c)'); + }); + }); });