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:
parent
53c1c764ba
commit
f574e6a775
5 changed files with 25 additions and 6 deletions
|
|
@ -52,7 +52,7 @@ local common = {
|
|||
operator_priority = {
|
||||
[";_"] = 1,
|
||||
["$_"] = 1,
|
||||
["@_"] = 2,
|
||||
["@_"] = 1,
|
||||
["_,_"] = 2,
|
||||
["_=_"] = 3,
|
||||
["_!_"] = 12,
|
||||
|
|
|
|||
|
|
@ -42,7 +42,6 @@ local function_parameter_maybe_parenthesis = function_parameter_no_default {
|
|||
end
|
||||
}
|
||||
|
||||
|
||||
-- signature type 1: unary prefix
|
||||
-- :$-parameter exp
|
||||
-- returns symbol, parameter_tuple, rem if success
|
||||
|
|
@ -195,7 +194,7 @@ return primary {
|
|||
-- parse expression
|
||||
local right
|
||||
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
|
||||
local fn = Function:new(parameters, right):set_source(source_start)
|
||||
|
|
|
|||
|
|
@ -21,7 +21,6 @@ local primaries = {
|
|||
-- 1
|
||||
r("prefix.semicolon"),
|
||||
r("prefix.function"),
|
||||
-- 2
|
||||
r("prefix.return"),
|
||||
-- 3.5
|
||||
r("prefix.else"),
|
||||
|
|
|
|||
21
parser/expression/primary/prefix/prefix_maybe_nil_right.lua
Normal file
21
parser/expression/primary/prefix/prefix_maybe_nil_right.lua
Normal 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,
|
||||
}
|
||||
|
|
@ -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 Return = ast.Return
|
||||
|
||||
local operator_priority = require("common").operator_priority
|
||||
|
||||
return prefix {
|
||||
return prefix_maybe_nil_right {
|
||||
operator = "@",
|
||||
priority = operator_priority["@_"],
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue