Skip to content

Commit 51d1733

Browse files
committed
Enhance expressionPow to support unary operators on the right-hand side, preserving right-associativity. Added tests to validate the fix for precedence issues.
1 parent 728a837 commit 51d1733

2 files changed

Lines changed: 22 additions & 2 deletions

File tree

src/prometheus/parser.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -716,7 +716,8 @@ function Parser:expressionPow(scope)
716716
local lhs = self:tableOrFunctionLiteral(scope);
717717

718718
if(consume(self, TokenKind.Symbol, "^")) then
719-
local rhs = self:expressionPow(scope);
719+
-- Allow unary operators on the rhs (e.g. 2 ^ #x, 2 ^ -x) while preserving right-associativity. ~ SpinnySpiwal
720+
local rhs = self:expressionUnary(scope);
720721
return Ast.PowExpression(lhs, rhs, true);
721722
end
722723

tests/syntax.lua

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
-- Syntax Test Suite
33
-- Target: Unparser
44
-- Author: SpinnySpiwal
5-
-- Purpose: Validate appropriate unparser functionality, specifically in unseen edge cases.
5+
-- Purpose: Validate appropriate parser & unparser functionality, specifically in unseen edge cases.
6+
-- Update 1: Added test for precedence bug fix in expressionPow.
67
--============================================================
78

89
local char = ("").char
@@ -17,3 +18,21 @@ local ok = pcall(function(...)
1718
print("hello " .. ...)
1819
end)
1920
print(ok and "no" or "yes")
21+
22+
local function getString()
23+
return "this string is 24 chars!"
24+
end
25+
26+
-- Test for precedence bug fix in expressionPow
27+
if 2 ^ #getString() == 16777216 then
28+
print("TEST 1 PASSED")
29+
else
30+
print("TEST 1 FAILED")
31+
end
32+
33+
-- Check if it still works the other way around
34+
if (#getString()) ^ 2 == 576 then
35+
print("TEST 2 PASSED")
36+
else
37+
print("TEST 2 FAILED")
38+
end

0 commit comments

Comments
 (0)