Skip to content

Commit 742a692

Browse files
authored
Merge pull request #210 from prometheus-lua/bugfix/fix-function-call-parsing
Fix function call parsing and add test
2 parents deebb56 + 47a77d7 commit 742a692

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

src/prometheus/parser.lua

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ local ASSIGNMENT_NO_WARN_LOOKUP = lookupify{
3838
AstKind.VarargExpression
3939
};
4040

41+
local CALLABLE_PREFIX_EXPRESSION_LOOKUP = lookupify{
42+
AstKind.VariableExpression,
43+
AstKind.IndexExpression,
44+
AstKind.FunctionCallExpression,
45+
AstKind.PassSelfFunctionCallExpression
46+
};
47+
4148
local function generateError(self, message)
4249
local token;
4350
if(self.index > self.length) then
@@ -782,6 +789,10 @@ end
782789

783790
function Parser:expressionFunctionCall(scope, base)
784791
base = base or self:expressionIndex(scope);
792+
793+
if(not (base and (CALLABLE_PREFIX_EXPRESSION_LOOKUP[base.kind] or base.isParenthesizedExpression))) then
794+
return base;
795+
end
785796

786797
-- Normal Function Call
787798
local args = {};
@@ -887,6 +898,9 @@ function Parser:expressionLiteral(scope)
887898
if(consume(self, TokenKind.Symbol, "(")) then
888899
local expr = self:expression(scope);
889900
expect(self, TokenKind.Symbol, ")");
901+
if expr then
902+
expr.isParenthesizedExpression = true;
903+
end
890904
return expr;
891905
end
892906

tests/ambiguous-call.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
-- Reproduction for issue #203 where the parser misreads the following
2+
-- as arguments to the previous literal assignment.
3+
local counter = 1
4+
5+
(function()
6+
counter = counter + 1
7+
print("counter:", counter)
8+
end)();

0 commit comments

Comments
 (0)