Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add escape for @ and @@ accessors #410

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions moonscript/compile/value.lua
Original file line number Diff line number Diff line change
Expand Up @@ -296,15 +296,32 @@ return {
return self:line("not ", self:value(node[2]))
end,
self = function(self, node)
return "self." .. self:name(node[2])
if data.lua_keywords[self:name(node[2])] then
return "self[\"" .. tostring(self:name(node[2])) .. "\"]"
else
return "self." .. self:name(node[2])
end
end,
self_class = function(self, node)
return "self.__class." .. self:name(node[2])
if data.lua_keywords[self:name(node[2])] then
return "self.__class[\"" .. tostring(self:name(node[2])) .. "\"]"
else
return "self.__class." .. self:name(node[2])
end
end,
self_colon = function(self, node)
return "self:" .. self:name(node[2])
if data.lua_keywords[self:name(node[2])] then
return "(function(...) return self[\"" .. tostring(self:name(node[2])) .. "\"](self, ...) end)"
else
return "self:" .. self:name(node[2])
end
end,
self_class_colon = function(self, node)
if data.lua_keywords[self:name(node[2])] then
return "(function(...) return self.__class[\"" .. tostring(self:name(node[2])) .. "\"](self.__class, ...) end)"
else
return "self.__class:" .. self:name(node[2])
end
return "self.__class:" .. self:name(node[2])
end,
ref = function(self, value)
Expand Down
19 changes: 16 additions & 3 deletions moonscript/compile/value.moon
Original file line number Diff line number Diff line change
Expand Up @@ -183,15 +183,28 @@ string_chars = {
@line "not ", @value node[2]

self: (node) =>
"self."..@name node[2]
return if data.lua_keywords[@name node[2]]
"self[\"#{@name node[2]}\"]"
else
"self."..@name node[2]

self_class: (node) =>
"self.__class."..@name node[2]
return if data.lua_keywords[@name node[2]]
"self.__class[\"#{@name node[2]}\"]"
else
"self.__class."..@name node[2]

self_colon: (node) =>
"self:"..@name node[2]
return if data.lua_keywords[@name node[2]]
"(function(...) return self[\"#{@name node[2]}\"](self, ...) end)"
else
"self:"..@name node[2]

self_class_colon: (node) =>
return if data.lua_keywords[@name node[2]]
"(function(...) return self.__class[\"#{@name node[2]}\"](self.__class, ...) end)"
else
"self.__class:"..@name node[2]
"self.__class:"..@name node[2]

-- a variable reference
Expand Down
25 changes: 25 additions & 0 deletions spec/keyword_support_spec.moon
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

import with_dev from require "spec.helpers"

describe "keyword support", ->

local moonscript

with_dev ->
moonscript = require "moonscript.base"

it "should accept keywords with @", ->
moon_src="class Test\n\tnew: => @if=true\n\tdo: => 1\n\ttest: => @if and @do!\ntest=Test!\ntest\\test!"
lua_src=moonscript.to_lua moon_src
assert.is_not_nil lua_src
fn=(loadstring or load) lua_src
assert.is_not_nil fn
assert.same fn!, 1

it "should accept keywords with @@", ->
moon_src="class Test\n\tnew: => @@if=true\n\t@do: => 1\n\ttest: => @@if and @@do!\ntest=Test!\ntest\\test!"
lua_src=moonscript.to_lua moon_src
assert.is_not_nil lua_src
fn=(loadstring or load) lua_src
assert.is_not_nil fn
assert.same fn!, 1