added export all and export proper

This commit is contained in:
leaf corcoran 2011-10-02 00:08:13 -07:00
parent 570cf1f8cb
commit e6f33c0844
6 changed files with 47 additions and 9 deletions

View File

@ -581,6 +581,13 @@ module:
print my_module.length 6, 7 -- errors, `length` not visible 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 ## Import Statement

View File

@ -101,6 +101,8 @@ Block_ = (function()
local _base_0 = { local _base_0 = {
header = "do", header = "do",
footer = "end", footer = "end",
export_all = false,
export_proper = false,
line_table = function(self) line_table = function(self)
return self._posmap return self._posmap
end, end,
@ -155,11 +157,19 @@ Block_ = (function()
end end
self._names[name] = true self._names[name] = true
end, 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] local yes = self._names[name]
if yes == nil and self.parent then if yes == nil and self.parent then
if not self._name_whitelist or self._name_whitelist[name] 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 end
else else
return yes return yes
@ -179,7 +189,7 @@ Block_ = (function()
i i
}, "_") }, "_")
i = i + 1 i = i + 1
searching = self:has_name(name) searching = self:has_name(name, true)
end end
if not dont_put then if not dont_put then
self:put_name(name) self:put_name(name)

View File

@ -53,6 +53,9 @@ class Block_
header: "do" header: "do"
footer: "end" footer: "end"
export_all: false
export_proper: false
new: (@parent, @header, @footer) => new: (@parent, @header, @footer) =>
@current_line = 1 @current_line = 1
@ -95,11 +98,15 @@ class Block_
name = name\get_name self if util.moon.type(name) == NameProxy name = name\get_name self if util.moon.type(name) == NameProxy
@_names[name] = true @_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] yes = @_names[name]
if yes == nil and @parent if yes == nil and @parent
if not @_name_whitelist or @_name_whitelist[name] if not @_name_whitelist or @_name_whitelist[name]
@parent\has_name name @parent\has_name name, true
else else
yes yes
@ -113,7 +120,7 @@ class Block_
while searching while searching
name = concat {"", prefix, i}, "_" name = concat {"", prefix, i}, "_"
i = i + 1 i = i + 1
searching = @has_name name searching = @has_name name, true
@put_name name if not dont_put @put_name name if not dont_put
name name

View File

@ -409,7 +409,15 @@ line_compile = {
end, end,
export = function(self, node) export = function(self, node)
local _, names = unpack(node) local _, names = unpack(node)
self:declare(names) 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 return nil
end, end,
comprehension = function(self, node, action) comprehension = function(self, node, action)

View File

@ -194,7 +194,13 @@ line_compile =
export: (node) => export: (node) =>
_, names = unpack node _, names = unpack node
@declare names if type(names) == "string"
if names == "*"
@export_all = true
elseif names == "^"
@export_proper = true
else
@declare names
nil nil
comprehension: (node, action) => comprehension: (node, action) =>

View File

@ -408,7 +408,7 @@ local build_grammar = wrap(function()
TableBlock = SpaceBreak^1 * Advance * TableBlockInner * PopIndent / mark"table", TableBlock = SpaceBreak^1 * Advance * TableBlockInner * PopIndent / mark"table",
ClassDecl = key"class" * Name * (key"extends" * Exp + C"")^-1 * TableBlock / mark"class", 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)), KeyValue = (sym":" * Name) / self_assign + Ct((SimpleName + sym"[" * Exp * sym"]") * symx":" * (Exp + TableBlock)),
KeyValueList = KeyValue * (sym"," * KeyValue)^0, KeyValueList = KeyValue * (sym"," * KeyValue)^0,