differentiate between table key literals and variable references #42

This commit is contained in:
leaf corcoran 2012-09-07 18:51:19 -07:00
parent b170172465
commit 290b73f92b
9 changed files with 58 additions and 25 deletions

View File

@ -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))

View File

@ -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)

View File

@ -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

View File

@ -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"
else
act = "dot"
end
if ntype(calling_name) == "key_literal" then
insert(new_chain, {
act,
"dot",
calling_name[2]
})
else
insert(new_chain, {
"index",
calling_name
})
end
elseif "colon" == _exp_0 then
local call = head[3]
insert(new_chain, {

View File

@ -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"

View File

@ -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

View File

@ -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}

View File

@ -90,3 +90,9 @@ y = {
call_me "hello", :x, :y, :z
t = {
a: 'a'
[b]: 'b'
}

View File

@ -117,3 +117,7 @@ call_me("hello", {
y = y,
z = z
})
local t = {
a = 'a',
[b] = 'b'
}