mirror of
https://github.com/leafo/moonscript.git
synced 2024-10-05 05:34:19 +00:00
differentiate between table key literals and variable references #42
This commit is contained in:
parent
b170172465
commit
290b73f92b
@ -257,18 +257,18 @@ value_compile = {
|
||||
format_line = function(tuple)
|
||||
if #tuple == 2 then
|
||||
local key, value = unpack(tuple)
|
||||
if type(key) == "string" and data.lua_keywords[key] then
|
||||
if ntype(key) == "key_literal" and data.lua_keywords[key[2]] then
|
||||
key = {
|
||||
"string",
|
||||
'"',
|
||||
key
|
||||
key[2]
|
||||
}
|
||||
end
|
||||
local assign
|
||||
if type(key) ~= "string" then
|
||||
assign = self:line("[", _with_0:value(key), "]")
|
||||
if ntype(key) == "key_literal" then
|
||||
assign = key[2]
|
||||
else
|
||||
assign = key
|
||||
assign = self:line("[", _with_0:value(key), "]")
|
||||
end
|
||||
_with_0:set("current_block", key)
|
||||
local out = self:line(assign, " = ", _with_0:value(value))
|
||||
|
@ -141,13 +141,14 @@ value_compile =
|
||||
if #tuple == 2
|
||||
key, value = unpack tuple
|
||||
|
||||
if type(key) == "string" and data.lua_keywords[key]
|
||||
key = {"string", '"', key}
|
||||
-- escape keys that are lua keywords
|
||||
if ntype(key) == "key_literal" and data.lua_keywords[key[2]]
|
||||
key = {"string", '"', key[2]}
|
||||
|
||||
assign = if type(key) != "string"
|
||||
@line "[", \value(key), "]"
|
||||
assign = if ntype(key) == "key_literal"
|
||||
key[2]
|
||||
else
|
||||
key
|
||||
@line "[", \value(key), "]"
|
||||
|
||||
\set "current_block", key
|
||||
out = @line assign, " = ", \value(value)
|
||||
|
@ -219,7 +219,7 @@ end
|
||||
|
||||
-- :name in table literal
|
||||
local function self_assign(name)
|
||||
return {name, name}
|
||||
return {{"key_literal", name}, name}
|
||||
end
|
||||
|
||||
local err_msg = "Failed to parse:\n [%d] >> %s (%d)"
|
||||
@ -276,7 +276,7 @@ local build_grammar = wrap_env(function()
|
||||
"@" * (_Name / mark"self_class" + Cc"self.__class") +
|
||||
_Name / mark"self" + Cc"self")
|
||||
|
||||
local KeyName = SelfName + Space * _Name
|
||||
local KeyName = SelfName + Space * _Name / mark"key_literal"
|
||||
|
||||
local Name = SelfName + Name + Space * "..." / trim
|
||||
|
||||
|
@ -640,8 +640,9 @@ Statement = Transformer({
|
||||
local _list_1 = properties
|
||||
for _index_0 = 1, #_list_1 do
|
||||
local tuple = _list_1[_index_0]
|
||||
local key = tuple[1]
|
||||
local _value_0
|
||||
if tuple[1] == constructor_name then
|
||||
if key[1] == "key_literal" and key[2] == constructor_name then
|
||||
constructor = tuple[2]
|
||||
_value_0 = nil
|
||||
else
|
||||
@ -838,16 +839,17 @@ Statement = Transformer({
|
||||
unpack(head[2])
|
||||
}
|
||||
}
|
||||
local act
|
||||
if ntype(calling_name) ~= "value" then
|
||||
act = "index"
|
||||
if ntype(calling_name) == "key_literal" then
|
||||
insert(new_chain, {
|
||||
"dot",
|
||||
calling_name[2]
|
||||
})
|
||||
else
|
||||
act = "dot"
|
||||
insert(new_chain, {
|
||||
"index",
|
||||
calling_name
|
||||
})
|
||||
end
|
||||
insert(new_chain, {
|
||||
act,
|
||||
calling_name
|
||||
})
|
||||
elseif "colon" == _exp_0 then
|
||||
local call = head[3]
|
||||
insert(new_chain, {
|
||||
|
@ -329,7 +329,8 @@ Statement = Transformer {
|
||||
-- find constructor
|
||||
constructor = nil
|
||||
properties = for tuple in *properties
|
||||
if tuple[1] == constructor_name
|
||||
key = tuple[1]
|
||||
if key[1] == "key_literal" and key[2] == constructor_name
|
||||
constructor = tuple[2]
|
||||
nil
|
||||
else
|
||||
@ -423,8 +424,11 @@ Statement = Transformer {
|
||||
when "call"
|
||||
calling_name = block\get"current_block"
|
||||
slice[1] = {"call", {"self", unpack head[2]}}
|
||||
act = if ntype(calling_name) != "value" then "index" else "dot"
|
||||
insert new_chain, {act, calling_name}
|
||||
|
||||
if ntype(calling_name) == "key_literal"
|
||||
insert new_chain, {"dot", calling_name[2]}
|
||||
else
|
||||
insert new_chain, {"index", calling_name}
|
||||
|
||||
-- colon call on super, replace class with self as first arg
|
||||
when "colon"
|
||||
|
@ -187,6 +187,16 @@ build = setmetatable({
|
||||
if tbl == nil then
|
||||
tbl = { }
|
||||
end
|
||||
local _list_0 = tbl
|
||||
for _index_0 = 1, #_list_0 do
|
||||
local tuple = _list_0[_index_0]
|
||||
if type(tuple[1]) == "string" then
|
||||
tuple[1] = {
|
||||
"key_literal",
|
||||
tuple[1]
|
||||
}
|
||||
end
|
||||
end
|
||||
return {
|
||||
"table",
|
||||
tbl
|
||||
|
@ -105,7 +105,13 @@ build = setmetatable {
|
||||
names: {name}
|
||||
values: {value}
|
||||
}
|
||||
|
||||
table: (tbl={}) ->
|
||||
-- convert strings to key literals
|
||||
for tuple in *tbl
|
||||
if type(tuple[1]) == "string"
|
||||
tuple[1] = {"key_literal", tuple[1]}
|
||||
|
||||
{"table", tbl}
|
||||
block_exp: (body) ->
|
||||
{"block_exp", body}
|
||||
|
@ -90,3 +90,9 @@ y = {
|
||||
|
||||
call_me "hello", :x, :y, :z
|
||||
|
||||
t = {
|
||||
a: 'a'
|
||||
[b]: 'b'
|
||||
}
|
||||
|
||||
|
||||
|
@ -116,4 +116,8 @@ call_me("hello", {
|
||||
x = x,
|
||||
y = y,
|
||||
z = z
|
||||
})
|
||||
})
|
||||
local t = {
|
||||
a = 'a',
|
||||
[b] = 'b'
|
||||
}
|
Loading…
Reference in New Issue
Block a user