import brackets support as and prefixes [backport] (#21636)

* import brackets support `as` and prefixes

fixes #21635

* copyTree -> copyNode
This commit is contained in:
metagn
2023-04-18 11:35:35 +03:00
committed by GitHub
parent 9dc1f2dd0f
commit 2f547afb0f
2 changed files with 22 additions and 15 deletions

View File

@@ -323,22 +323,24 @@ proc evalImport*(c: PContext, n: PNode): PNode =
result = newNodeI(nkImportStmt, n.info)
for i in 0..<n.len:
let it = n[i]
if it.kind == nkInfix and it.len == 3 and it[2].kind == nkBracket:
let sep = it[0]
let dir = it[1]
var imp = newNodeI(nkInfix, it.info)
imp.add sep
imp.add dir
imp.add sep # dummy entry, replaced in the loop
for x in it[2]:
if it.kind in {nkInfix, nkPrefix} and it[^1].kind == nkBracket:
let lastPos = it.len - 1
var imp = copyNode(it)
newSons(imp, it.len)
for i in 0 ..< lastPos: imp[i] = it[i]
imp[lastPos] = imp[0] # dummy entry, replaced in the loop
for x in it[lastPos]:
# transform `a/b/[c as d]` to `/a/b/c as d`
if x.kind == nkInfix and x[0].ident.s == "as":
let impAs = copyTree(x)
imp[2] = x[1]
var impAs = copyNode(x)
newSons(impAs, 3)
impAs[0] = x[0]
imp[lastPos] = x[1]
impAs[1] = imp
impMod(c, imp, result)
impAs[2] = x[2]
impMod(c, impAs, result)
else:
imp[2] = x
imp[lastPos] = x
impMod(c, imp, result)
else:
impMod(c, it, result)