From e6f33c0844a6085a80751034df142cd4562337bf Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 2 Oct 2011 00:08:13 -0700 Subject: [PATCH] added export all and export proper --- docs/reference.md | 7 +++++++ moonscript/compile.lua | 16 +++++++++++++--- moonscript/compile.moon | 13 ++++++++++--- moonscript/compile/line.lua | 10 +++++++++- moonscript/compile/line.moon | 8 +++++++- moonscript/parse.lua | 2 +- 6 files changed, 47 insertions(+), 9 deletions(-) diff --git a/docs/reference.md b/docs/reference.md index d32133b..0394a6e 100644 --- a/docs/reference.md +++ b/docs/reference.md @@ -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 diff --git a/moonscript/compile.lua b/moonscript/compile.lua index 02a3f2c..475361e 100644 --- a/moonscript/compile.lua +++ b/moonscript/compile.lua @@ -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) diff --git a/moonscript/compile.moon b/moonscript/compile.moon index 47b4bc1..69abe88 100644 --- a/moonscript/compile.moon +++ b/moonscript/compile.moon @@ -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 diff --git a/moonscript/compile/line.lua b/moonscript/compile/line.lua index 2d71558..3c22902 100644 --- a/moonscript/compile/line.lua +++ b/moonscript/compile/line.lua @@ -409,7 +409,15 @@ line_compile = { end, export = function(self, 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 end, comprehension = function(self, node, action) diff --git a/moonscript/compile/line.moon b/moonscript/compile/line.moon index b04e71b..0d21e57 100644 --- a/moonscript/compile/line.moon +++ b/moonscript/compile/line.moon @@ -194,7 +194,13 @@ line_compile = export: (node) => _, names = unpack node - @declare names + if type(names) == "string" + if names == "*" + @export_all = true + elseif names == "^" + @export_proper = true + else + @declare names nil comprehension: (node, action) => diff --git a/moonscript/parse.lua b/moonscript/parse.lua index 137b271..32f9fa7 100644 --- a/moonscript/parse.lua +++ b/moonscript/parse.lua @@ -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,