ast: add getPIdent (#17684)

* ast: add getPIdent

* fixup
This commit is contained in:
Timothee Cour 2021-04-09 01:00:13 -05:00 • committed by GitHub
commit cce1b24b1c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 16 deletions

View file

@ -1054,6 +1054,14 @@ const
defaultAlignment = -1 defaultAlignment = -1
defaultOffset = -1 defaultOffset = -1
proc getPIdent*(a: PNode): PIdent {.inline.} =
## Returns underlying `PIdent` for `{nkSym, nkIdent}`, or `nil`.
# xxx consider whether also returning the 1st ident for {nkOpenSymChoice, nkClosedSymChoice}
# which may simplify code.
case a.kind
of nkSym: a.sym.name
of nkIdent: a.ident
else: nil
proc getnimblePkg*(a: PSym): PSym = proc getnimblePkg*(a: PSym): PSym =
result = a result = a

View file

@ -964,14 +964,12 @@ proc skipHiddenNodes(n: PNode): PNode =
else: break else: break
proc accentedName(g: var TSrcGen, n: PNode) = proc accentedName(g: var TSrcGen, n: PNode) =
# This is for cases where ident should've really been a `nkAccQuoted`, e.g. `:tmp`
# or if user writes a macro with `ident":foo"`. It's unclear whether these should be legal.
const backticksNeeded = OpChars + {'[', '{', '\''} const backticksNeeded = OpChars + {'[', '{', '\''}
if n == nil: return if n == nil: return
let isOperator = let ident = n.getPIdent
if n.kind == nkIdent and n.ident.s.len > 0 and n.ident.s[0] in backticksNeeded: true if ident != nil and ident.s[0] in backticksNeeded:
elif n.kind == nkSym and n.sym.name.s.len > 0 and n.sym.name.s[0] in backticksNeeded: true
else: false
if isOperator:
put(g, tkAccent, "`") put(g, tkAccent, "`")
gident(g, n) gident(g, n)
put(g, tkAccent, "`") put(g, tkAccent, "`")
@ -999,9 +997,9 @@ proc infixArgument(g: var TSrcGen, n: PNode, i: int) =
put(g, tkParRi, ")") put(g, tkParRi, ")")
proc isCustomLit(n: PNode): bool = proc isCustomLit(n: PNode): bool =
n.len == 2 and n[0].kind == nkRStrLit and if n.len == 2 and n[0].kind == nkRStrLit:
(n[1].kind == nkIdent and n[1].ident.s.startsWith('\'')) or let ident = n[1].getPIdent
(n[1].kind == nkSym and n[1].sym.name.s.startsWith('\'')) result = ident != nil and ident.s.startsWith('\'')
proc gsub(g: var TSrcGen, n: PNode, c: TContext, fromStmtList = false) = proc gsub(g: var TSrcGen, n: PNode, c: TContext, fromStmtList = false) =
if isNil(n): return if isNil(n): return
@ -1234,7 +1232,7 @@ proc gsub(g: var TSrcGen, n: PNode, c: TContext, fromStmtList = false) =
else: else:
gsub(g, n, 0) gsub(g, n, 0)
put(g, tkDot, ".") put(g, tkDot, ".")
if n.len > 1: assert n.len == 2, $n.len
accentedName(g, n[1]) accentedName(g, n[1])
of nkBind: of nkBind:
putWithSpace(g, tkBind, "bind") putWithSpace(g, tkBind, "bind")

View file

@ -36,11 +36,8 @@ proc newDepN(id: int, pnode: PNode): DepN =
proc accQuoted(cache: IdentCache; n: PNode): PIdent = proc accQuoted(cache: IdentCache; n: PNode): PIdent =
var id = "" var id = ""
for i in 0..<n.len: for i in 0..<n.len:
let x = n[i] let ident = n[i].getPIdent
case x.kind if ident != nil: id.add(ident.s)
of nkIdent: id.add(x.ident.s)
of nkSym: id.add(x.sym.name.s)
else: discard
result = getIdent(cache, id) result = getIdent(cache, id)
proc addDecl(cache: IdentCache; n: PNode; declares: var IntSet) = proc addDecl(cache: IdentCache; n: PNode; declares: var IntSet) =