From 8562e8f18b8e5c35f113c2e8e93fbb335cacc002 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89tienne=20Reuh=20Fildadut?= Date: Mon, 8 Jan 2024 16:41:44 +0100 Subject: [PATCH] Allow function call with tuple and struct without parentheses --- .../expression/secondary/suffix/call.lua | 28 +++++++++++++------ test/results/function call struct.ans | 7 +++++ test/results/function call tuple.ans | 7 +++++ test/tests/function call struct.ans | 4 +++ test/tests/function call tuple.ans | 4 +++ 5 files changed, 42 insertions(+), 8 deletions(-) create mode 100644 test/results/function call struct.ans create mode 100644 test/results/function call tuple.ans create mode 100644 test/tests/function call struct.ans create mode 100644 test/tests/function call tuple.ans diff --git a/anselme/parser/expression/secondary/suffix/call.lua b/anselme/parser/expression/secondary/suffix/call.lua index 100015c..dc7664a 100644 --- a/anselme/parser/expression/secondary/suffix/call.lua +++ b/anselme/parser/expression/secondary/suffix/call.lua @@ -2,6 +2,8 @@ local secondary = require("anselme.parser.expression.secondary.secondary") local parenthesis = require("anselme.parser.expression.primary.parenthesis") +local tuple = require("anselme.parser.expression.primary.tuple") +local struct = require("anselme.parser.expression.primary.struct") local operator_priority = require("anselme.common").operator_priority @@ -12,22 +14,32 @@ return secondary { priority = operator_priority["_()"], match = function(self, str, current_priority, primary) - return self.priority > current_priority and parenthesis:match(str) + return self.priority > current_priority and (parenthesis:match(str) or tuple:match(str) or struct:match(str)) end, parse = function(self, source, str, limit_pattern, current_priority, primary) local start_source = source:clone() local args = ArgumentTuple:new() - local exp, rem = parenthesis:parse(source, str, limit_pattern) + local exp, rem - if Nil:is(exp) then - if str:match("^%(%s*%(%s*%)%s*%)") then -- special case: single nil argument - exp = Tuple:new(Nil:new()) - else -- no arguments - exp = Tuple:new() + if parenthesis:match(str) then + exp, rem = parenthesis:parse(source, str, limit_pattern) + + if Nil:is(exp) then + if str:match("^%(%s*%(%s*%)%s*%)") then -- special case: single nil argument + exp = Tuple:new(Nil:new()) + else -- no arguments + exp = Tuple:new() + end + elseif not Tuple:is(exp) or exp.explicit then -- single argument + exp = Tuple:new(exp) end - elseif not Tuple:is(exp) or exp.explicit then -- single argument + elseif tuple:match(str) then + exp, rem = tuple:parse(source, str, limit_pattern) + exp = Tuple:new(exp) + else + exp, rem = struct:parse(source, str, limit_pattern) exp = Tuple:new(exp) end diff --git a/test/results/function call struct.ans b/test/results/function call struct.ans new file mode 100644 index 0000000..7074c1d --- /dev/null +++ b/test/results/function call struct.ans @@ -0,0 +1,7 @@ +--# run #-- +--- text --- +| {}"" {}"{\"l\":8, 4:3, true:6}" {}"" | +--- return --- +() +--# saved #-- +{} \ No newline at end of file diff --git a/test/results/function call tuple.ans b/test/results/function call tuple.ans new file mode 100644 index 0000000..8ff4c6a --- /dev/null +++ b/test/results/function call tuple.ans @@ -0,0 +1,7 @@ +--# run #-- +--- text --- +| {}"" {}"[3, 6, 8]" {}"" | +--- return --- +() +--# saved #-- +{} \ No newline at end of file diff --git a/test/tests/function call struct.ans b/test/tests/function call struct.ans new file mode 100644 index 0000000..e3ec8f2 --- /dev/null +++ b/test/tests/function call struct.ans @@ -0,0 +1,4 @@ +:f = $(l) + |{l} + +f{4:3,true:6,"l":8} diff --git a/test/tests/function call tuple.ans b/test/tests/function call tuple.ans new file mode 100644 index 0000000..773bc1e --- /dev/null +++ b/test/tests/function call tuple.ans @@ -0,0 +1,4 @@ +:f = $(l) + |{l} + +f[3,6,8]