1
0
Fork 0
mirror of https://github.com/Reuh/anselme.git synced 2025-10-27 16:49:31 +00:00

Allow no expression in return

This commit is contained in:
Étienne Fildadut 2023-12-28 13:04:29 +01:00
parent 53c1c764ba
commit f574e6a775
5 changed files with 25 additions and 6 deletions

View file

@ -52,7 +52,7 @@ local common = {
operator_priority = { operator_priority = {
[";_"] = 1, [";_"] = 1,
["$_"] = 1, ["$_"] = 1,
["@_"] = 2, ["@_"] = 1,
["_,_"] = 2, ["_,_"] = 2,
["_=_"] = 3, ["_=_"] = 3,
["_!_"] = 12, ["_!_"] = 12,

View file

@ -42,7 +42,6 @@ local function_parameter_maybe_parenthesis = function_parameter_no_default {
end end
} }
-- signature type 1: unary prefix -- signature type 1: unary prefix
-- :$-parameter exp -- :$-parameter exp
-- returns symbol, parameter_tuple, rem if success -- returns symbol, parameter_tuple, rem if success
@ -195,7 +194,7 @@ return primary {
-- parse expression -- parse expression
local right local right
s, right, rem = pcall(expression_to_ast, source, rem, limit_pattern, operator_priority["$_"]) s, right, rem = pcall(expression_to_ast, source, rem, limit_pattern, operator_priority["$_"])
if not s then error(("invalid expression after unop %q: %s"):format(self.operator, right), 0) end if not s then error(("invalid expression in function definition: %s"):format(right), 0) end
-- return function -- return function
local fn = Function:new(parameters, right):set_source(source_start) local fn = Function:new(parameters, right):set_source(source_start)

View file

@ -21,7 +21,6 @@ local primaries = {
-- 1 -- 1
r("prefix.semicolon"), r("prefix.semicolon"),
r("prefix.function"), r("prefix.function"),
-- 2
r("prefix.return"), r("prefix.return"),
-- 3.5 -- 3.5
r("prefix.else"), r("prefix.else"),

View file

@ -0,0 +1,21 @@
local prefix = require("parser.expression.primary.prefix.prefix")
local escape = require("common").escape
local expression_to_ast = require("parser.expression.to_ast")
local ast = require("ast")
local Nil = ast.Nil
return prefix {
parse = function(self, source, str, limit_pattern)
local source_start = source:clone()
local escaped = escape(self.operator)
local sright = source:consume(str:match("^("..escaped..")(.*)$"))
local s, right, rem = pcall(expression_to_ast, source, sright, limit_pattern, self.priority)
if not s then
return self:build_ast(Nil:new()):set_source(source_start), sright
else
return self:build_ast(right):set_source(source_start), rem
end
end,
}

View file

@ -1,11 +1,11 @@
local prefix = require("parser.expression.primary.prefix.prefix") local prefix_maybe_nil_right = require("parser.expression.primary.prefix.prefix_maybe_nil_right")
local ast = require("ast") local ast = require("ast")
local Return = ast.Return local Return = ast.Return
local operator_priority = require("common").operator_priority local operator_priority = require("common").operator_priority
return prefix { return prefix_maybe_nil_right {
operator = "@", operator = "@",
priority = operator_priority["@_"], priority = operator_priority["@_"],