table keys can be reserved words

This commit is contained in:
leaf corcoran 2011-06-19 13:00:27 -07:00
parent 27836f1d80
commit 2fb3b4f9d2
8 changed files with 47 additions and 16 deletions

View File

@ -86,7 +86,7 @@ line_compile = {
end,
["return"] = function(self, node) return self:add_line("return", self:value(node[2])) end,
["break"] = function(self, node) return self:add_line("break") end,
["import"] = function(self, node)
import = function(self, node)
local _, names, source = unpack(node)
local to_bind = { }
local get_name
@ -197,7 +197,7 @@ line_compile = {
self:add_line(inner:render())
return self:add_line("end")
end,
["export"] = function(self, node)
export = function(self, node)
local _, names = unpack(node)
local _item_0 = names
for _index_0=1,#_item_0 do
@ -208,7 +208,7 @@ line_compile = {
end
return nil
end,
["class"] = function(self, node)
class = function(self, node)
local _, name, parent_val, tbl = unpack(node)
local constructor = nil
local final_properties = { }

View File

@ -58,13 +58,13 @@ line_compile =
error"unknown op: "..op if not op_final
@stm {"assign", {name}, {{"exp", name, op_final, exp}}}
["return"]: (node) =>
return: (node) =>
@add_line "return", @value node[2]
["break"]: (node) =>
break: (node) =>
@add_line "break"
["import"]: (node) =>
import: (node) =>
_, names, source = unpack node
to_bind = {}
@ -105,7 +105,7 @@ line_compile =
@add_line "end"
["if"]: (node, ret) =>
if: (node, ret) =>
cond, block = node[2], node[3]
add_clause = (clause) ->
@ -131,7 +131,7 @@ line_compile =
@add_line "end"
["while"]: (node) =>
while: (node) =>
_, cond, block = unpack node
inner = @block()
@ -146,7 +146,7 @@ line_compile =
@add_line inner:render()
@add_line "end"
["for"]: (node) =>
for: (node) =>
_, name, bounds, block = unpack node
bounds = @value {"explist", unpack bounds}
@add_line "for", @name(name), "=", bounds, "do"
@ -155,12 +155,12 @@ line_compile =
@add_line inner:render()
@add_line "end"
["export"]: (node) =>
export: (node) =>
_, names = unpack node
@put_name name for name in *names when type(name) == "string"
nil
["class"]: (node) =>
class: (node) =>
_, name, parent_val, tbl = unpack node
constructor = nil

View File

@ -129,6 +129,9 @@ local value_compile = {
local out
if #tuple == 2 then
local key, value = unpack(tuple)
if type(key) == "string" and data.lua_keywords[key] then
key = { "string", '"', key }
end
local key_val = self:value(key)
if type(key) ~= "string" then
key = ("[%s]"):format(key_val)

View File

@ -38,7 +38,7 @@ value_compile =
_, delim, inner, delim_end = unpack node
delim..inner..(delim_end or delim)
["if"]: (node) =>
if: (node) =>
func = @block()
func:stm node, returner
@format "(function()", func:render(), "end)()"
@ -113,8 +113,11 @@ value_compile =
_comp = (i, tuple) ->
out = if #tuple == 2
key, value = unpack tuple
key_val = @value key
if type(key) == "string" and data.lua_keywords[key]
key = {"string", '"', key}
key_val = @value key
key = if type(key) != "string"
("[%s]"):format key_val
else
@ -143,7 +146,7 @@ value_compile =
length: (node) =>
"#"..@value node[2]
["not"]: (node) =>
not: (node) =>
"not "..@value node[2]
self: (node) =>

View File

@ -43,3 +43,10 @@ function ntype(node)
return node[1]
end
lua_keywords = Set{
'and', 'break', 'do', 'else', 'elseif',
'end', 'false', 'for', 'function', 'if',
'in', 'local', 'nil', 'not', 'or',
'repeat', 'return', 'then', 'true',
'until', 'while'
}

View File

@ -177,6 +177,8 @@ local build_grammar = wrap(function()
return false
end
local SimpleName = Name -- for table key
-- make sure name is not a keyword
local Name = Cmt(Name, function(str, pos, name)
if keywords[name] then return false end
@ -342,7 +344,7 @@ local build_grammar = wrap(function()
ClassDecl = key"class" * Name * (key"extends" * Exp + C"")^-1 * TableBlock / mark"class",
Export = key"export" * Ct(NameList) / mark"export",
KeyValue = Ct((Name + sym"[" * Exp * sym"]") * symx":" * (Exp + TableBlock)),
KeyValue = Ct((SimpleName + sym"[" * Exp * sym"]") * symx":" * (Exp + TableBlock)),
KeyValueList = KeyValue * (sym"," * KeyValue)^0,
KeyValueLine = Cmt(Indent, check_indent) * KeyValueList * sym","^-1,

View File

@ -150,3 +150,12 @@ m %= 2
x = 0
(if ntype(v) == "fndef" then x += 1) for v in *values
hello =
something: world
if: "hello"
else: 3434
function: "okay"
good: 230203

View File

@ -111,4 +111,11 @@ for _index_0=1,#_item_0 do
return x
end
end)())
end
end
local hello = {
something = world,
["if"] = "hello",
["else"] = 3434,
["function"] = "okay",
good = 230203
}