mirror of
https://github.com/leafo/moonscript.git
synced 2025-01-09 00:04:22 +00:00
added export all and export proper
This commit is contained in:
parent
570cf1f8cb
commit
e6f33c0844
@ -581,6 +581,13 @@ module:
|
||||
|
||||
print my_module.length 6, 7 -- errors, `length` not visible
|
||||
|
||||
### Export All & Export Proper
|
||||
|
||||
The `export` statement can also take special symbols `*` and `^`.
|
||||
|
||||
`export *` is used to export any name declared after the statement in the
|
||||
current scope. `export ^` will export all proper names, names that begin with a
|
||||
capital letter.
|
||||
|
||||
## Import Statement
|
||||
|
||||
|
@ -101,6 +101,8 @@ Block_ = (function()
|
||||
local _base_0 = {
|
||||
header = "do",
|
||||
footer = "end",
|
||||
export_all = false,
|
||||
export_proper = false,
|
||||
line_table = function(self)
|
||||
return self._posmap
|
||||
end,
|
||||
@ -155,11 +157,19 @@ Block_ = (function()
|
||||
end
|
||||
self._names[name] = true
|
||||
end,
|
||||
has_name = function(self, name)
|
||||
has_name = function(self, name, skip_exports)
|
||||
if not skip_exports then
|
||||
if self.export_all then
|
||||
return true
|
||||
end
|
||||
if self.export_proper and name:match("^[A-Z]") then
|
||||
return true
|
||||
end
|
||||
end
|
||||
local yes = self._names[name]
|
||||
if yes == nil and self.parent then
|
||||
if not self._name_whitelist or self._name_whitelist[name] then
|
||||
return self.parent:has_name(name)
|
||||
return self.parent:has_name(name, true)
|
||||
end
|
||||
else
|
||||
return yes
|
||||
@ -179,7 +189,7 @@ Block_ = (function()
|
||||
i
|
||||
}, "_")
|
||||
i = i + 1
|
||||
searching = self:has_name(name)
|
||||
searching = self:has_name(name, true)
|
||||
end
|
||||
if not dont_put then
|
||||
self:put_name(name)
|
||||
|
@ -53,6 +53,9 @@ class Block_
|
||||
header: "do"
|
||||
footer: "end"
|
||||
|
||||
export_all: false
|
||||
export_proper: false
|
||||
|
||||
new: (@parent, @header, @footer) =>
|
||||
@current_line = 1
|
||||
|
||||
@ -95,11 +98,15 @@ class Block_
|
||||
name = name\get_name self if util.moon.type(name) == NameProxy
|
||||
@_names[name] = true
|
||||
|
||||
has_name: (name) =>
|
||||
has_name: (name, skip_exports) =>
|
||||
if not skip_exports
|
||||
return true if @export_all
|
||||
return true if @export_proper and name\match"^[A-Z]"
|
||||
|
||||
yes = @_names[name]
|
||||
if yes == nil and @parent
|
||||
if not @_name_whitelist or @_name_whitelist[name]
|
||||
@parent\has_name name
|
||||
@parent\has_name name, true
|
||||
else
|
||||
yes
|
||||
|
||||
@ -113,7 +120,7 @@ class Block_
|
||||
while searching
|
||||
name = concat {"", prefix, i}, "_"
|
||||
i = i + 1
|
||||
searching = @has_name name
|
||||
searching = @has_name name, true
|
||||
|
||||
@put_name name if not dont_put
|
||||
name
|
||||
|
@ -409,7 +409,15 @@ line_compile = {
|
||||
end,
|
||||
export = function(self, node)
|
||||
local _, names = unpack(node)
|
||||
if type(names) == "string" then
|
||||
if names == "*" then
|
||||
self.export_all = true
|
||||
elseif names == "^" then
|
||||
self.export_proper = true
|
||||
end
|
||||
else
|
||||
self:declare(names)
|
||||
end
|
||||
return nil
|
||||
end,
|
||||
comprehension = function(self, node, action)
|
||||
|
@ -194,6 +194,12 @@ line_compile =
|
||||
|
||||
export: (node) =>
|
||||
_, names = unpack node
|
||||
if type(names) == "string"
|
||||
if names == "*"
|
||||
@export_all = true
|
||||
elseif names == "^"
|
||||
@export_proper = true
|
||||
else
|
||||
@declare names
|
||||
nil
|
||||
|
||||
|
@ -408,7 +408,7 @@ local build_grammar = wrap(function()
|
||||
TableBlock = SpaceBreak^1 * Advance * TableBlockInner * PopIndent / mark"table",
|
||||
|
||||
ClassDecl = key"class" * Name * (key"extends" * Exp + C"")^-1 * TableBlock / mark"class",
|
||||
Export = key"export" * Ct(NameList) / mark"export",
|
||||
Export = key"export" * (Ct(NameList) + op"*" + op"^") / mark"export",
|
||||
|
||||
KeyValue = (sym":" * Name) / self_assign + Ct((SimpleName + sym"[" * Exp * sym"]") * symx":" * (Exp + TableBlock)),
|
||||
KeyValueList = KeyValue * (sym"," * KeyValue)^0,
|
||||
|
Loading…
Reference in New Issue
Block a user