better error when assigning invalid left hand side, fixes #36

This commit is contained in:
leaf corcoran 2012-10-31 00:43:26 -07:00
parent 021d4878a0
commit 5b9e1a4389

View File

@ -166,10 +166,15 @@ end
-- makes sure the last item in a chain is an index -- makes sure the last item in a chain is an index
local _assignable = { index = true, dot = true, slice = true } local _assignable = { index = true, dot = true, slice = true }
local function is_assignable(node)
local t = ntype(node)
return t == "self" or t == "value" or
t == "chain" and _assignable[ntype(node[#node])]
end
local function check_assignable(str, pos, value) local function check_assignable(str, pos, value)
if ntype(value) == "chain" and _assignable[ntype(value[#value])] if is_assignable(value) then
or type(value) == "string"
then
return true, value return true, value
end end
return false return false
@ -181,6 +186,13 @@ local function format_assign(lhs_exps, assign)
return flatten_explist(lhs_exps) return flatten_explist(lhs_exps)
end end
for _, assign_exp in ipairs(lhs_exps) do
if not is_assignable(assign_exp) then
print(util.dump(assign_exp))
error {assign_exp, "left hand expression is not assignable"}
end
end
local t = ntype(assign) local t = ntype(assign)
if t == "assign" then if t == "assign" then
return {"assign", lhs_exps, unpack(assign, 2)} return {"assign", lhs_exps, unpack(assign, 2)}
@ -276,7 +288,7 @@ local function self_assign(name)
return {{"key_literal", name}, name} return {{"key_literal", name}, name}
end end
local err_msg = "Failed to parse:\n [%d] >> %s (%d)" local err_msg = "Failed to parse:%s\n [%d] >> %s"
local build_grammar = wrap_env(function() local build_grammar = wrap_env(function()
local _indent = Stack(0) -- current indent local _indent = Stack(0) -- current indent
@ -559,16 +571,30 @@ local build_grammar = wrap_env(function()
end end
local tree local tree
local args = {...} local pass, err = pcall(function(...)
local pass, err = assert(pcall(function() tree = self._g:match(str, ...)
tree = self._g:match(str, unpack(args)) end, ...)
end))
-- regular error, let it bubble up
if type(err) == "string" then
error(err)
end
if not tree then if not tree then
local line_no = pos_to_line(last_pos) local pos = last_pos
local msg
if err then
local node
node, msg = unpack(err)
msg = msg and " " .. msg
pos = node[-1]
end
local line_no = pos_to_line(pos)
local line_str = get_line(line_no) or "" local line_str = get_line(line_no) or ""
return nil, err_msg:format(line_no, trim(line_str), _indent:top()) return nil, err_msg:format(msg or "", line_no, trim(line_str))
end end
return tree return tree
end end