implement the export/except statement
This commit is contained in:
parent
7297613900
commit
cf13c5fba4
6 changed files with 41 additions and 7 deletions
|
|
@ -108,6 +108,8 @@
|
||||||
- Thread-local variables can now be declared inside procs. This implies all
|
- Thread-local variables can now be declared inside procs. This implies all
|
||||||
the effects of the `global` pragma.
|
the effects of the `global` pragma.
|
||||||
|
|
||||||
|
- Nim now supports `except` clause in the export statement.
|
||||||
|
|
||||||
### Tool changes
|
### Tool changes
|
||||||
|
|
||||||
- ``jsondoc2`` has been renamed ``jsondoc``, similar to how ``doc2`` was renamed
|
- ``jsondoc2`` has been renamed ``jsondoc``, similar to how ``doc2`` was renamed
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,13 @@ import
|
||||||
proc evalImport*(c: PContext, n: PNode): PNode
|
proc evalImport*(c: PContext, n: PNode): PNode
|
||||||
proc evalFrom*(c: PContext, n: PNode): PNode
|
proc evalFrom*(c: PContext, n: PNode): PNode
|
||||||
|
|
||||||
|
proc readExceptSet*(n: PNode): IntSet =
|
||||||
|
assert n.kind in {nkImportExceptStmt, nkExportExceptStmt}
|
||||||
|
result = initIntSet()
|
||||||
|
for i in 1 ..< n.len:
|
||||||
|
let ident = lookups.considerQuotedIdent(n[i])
|
||||||
|
result.incl(ident.id)
|
||||||
|
|
||||||
proc importPureEnumField*(c: PContext; s: PSym) =
|
proc importPureEnumField*(c: PContext; s: PSym) =
|
||||||
var check = strTableGet(c.importTable.symbols, s.name)
|
var check = strTableGet(c.importTable.symbols, s.name)
|
||||||
if check == nil:
|
if check == nil:
|
||||||
|
|
@ -198,9 +205,5 @@ proc evalImportExcept*(c: PContext, n: PNode): PNode =
|
||||||
if m != nil:
|
if m != nil:
|
||||||
n.sons[0] = newSymNode(m)
|
n.sons[0] = newSymNode(m)
|
||||||
addDecl(c, m, n.info) # add symbol to symbol table of module
|
addDecl(c, m, n.info) # add symbol to symbol table of module
|
||||||
var exceptSet = initIntSet()
|
importAllSymbolsExcept(c, m, readExceptSet(n))
|
||||||
for i in countup(1, sonsLen(n) - 1):
|
|
||||||
let ident = lookups.considerQuotedIdent(n.sons[i])
|
|
||||||
exceptSet.incl(ident.id)
|
|
||||||
importAllSymbolsExcept(c, m, exceptSet)
|
|
||||||
#importForwarded(c, m.ast, exceptSet)
|
#importForwarded(c, m.ast, exceptSet)
|
||||||
|
|
|
||||||
|
|
@ -2160,9 +2160,25 @@ proc semBlock(c: PContext, n: PNode): PNode =
|
||||||
closeScope(c)
|
closeScope(c)
|
||||||
dec(c.p.nestedBlockCounter)
|
dec(c.p.nestedBlockCounter)
|
||||||
|
|
||||||
|
proc semExportExcept(c: PContext, n: PNode): PNode =
|
||||||
|
let moduleName = semExpr(c, n[0])
|
||||||
|
if moduleName.kind != nkSym or moduleName.sym.kind != skModule:
|
||||||
|
localError(n.info, "The export/except syntax expects a module name")
|
||||||
|
return
|
||||||
|
let exceptSet = readExceptSet(n)
|
||||||
|
let exported = moduleName.sym
|
||||||
|
strTableAdd(c.module.tab, exported)
|
||||||
|
var i: TTabIter
|
||||||
|
var s = initTabIter(i, exported.tab)
|
||||||
|
while s != nil:
|
||||||
|
if s.kind in ExportableSymKinds+{skModule} and
|
||||||
|
s.name.id notin exceptSet:
|
||||||
|
strTableAdd(c.module.tab, s)
|
||||||
|
s = nextIter(i, exported.tab)
|
||||||
|
result = n
|
||||||
|
|
||||||
proc semExport(c: PContext, n: PNode): PNode =
|
proc semExport(c: PContext, n: PNode): PNode =
|
||||||
var x = newNodeI(n.kind, n.info)
|
var x = newNodeI(n.kind, n.info)
|
||||||
#let L = if n.kind == nkExportExceptStmt: L = 1 else: n.len
|
|
||||||
for i in 0..<n.len:
|
for i in 0..<n.len:
|
||||||
let a = n.sons[i]
|
let a = n.sons[i]
|
||||||
var o: TOverloadIter
|
var o: TOverloadIter
|
||||||
|
|
@ -2448,9 +2464,12 @@ proc semExpr(c: PContext, n: PNode, flags: TExprFlags = {}): PNode =
|
||||||
of nkIncludeStmt:
|
of nkIncludeStmt:
|
||||||
#if not isTopLevel(c): localError(n.info, errXOnlyAtModuleScope, "include")
|
#if not isTopLevel(c): localError(n.info, errXOnlyAtModuleScope, "include")
|
||||||
result = evalInclude(c, n)
|
result = evalInclude(c, n)
|
||||||
of nkExportStmt, nkExportExceptStmt:
|
of nkExportStmt:
|
||||||
if not isTopLevel(c): localError(n.info, errXOnlyAtModuleScope, "export")
|
if not isTopLevel(c): localError(n.info, errXOnlyAtModuleScope, "export")
|
||||||
result = semExport(c, n)
|
result = semExport(c, n)
|
||||||
|
of nkExportExceptStmt:
|
||||||
|
if not isTopLevel(c): localError(n.info, errXOnlyAtModuleScope, "export")
|
||||||
|
result = semExportExcept(c, n)
|
||||||
of nkPragmaBlock:
|
of nkPragmaBlock:
|
||||||
result = semPragmaBlock(c, n)
|
result = semPragmaBlock(c, n)
|
||||||
of nkStaticStmt:
|
of nkStaticStmt:
|
||||||
|
|
|
||||||
|
|
@ -6298,6 +6298,9 @@ modules don't need to import a module's dependencies:
|
||||||
var x: MyObject
|
var x: MyObject
|
||||||
echo $x
|
echo $x
|
||||||
|
|
||||||
|
When the exported symbol is another module, all of its definitions will
|
||||||
|
be forwarded. You can use an ``except`` list to exclude some of the symbols.
|
||||||
|
|
||||||
Note on paths
|
Note on paths
|
||||||
-----------
|
-----------
|
||||||
In module related statements, if any part of the module name /
|
In module related statements, if any part of the module name /
|
||||||
|
|
|
||||||
4
tests/modules/definitions.nim
Normal file
4
tests/modules/definitions.nim
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
var v*: int
|
||||||
|
proc p* = echo "proc p called"
|
||||||
|
template t* = echo "template t expanded"
|
||||||
|
|
||||||
3
tests/modules/proxy_module.nim
Normal file
3
tests/modules/proxy_module.nim
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
import definitions
|
||||||
|
export definitions except p
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue