Add CommentStmt to astGenRepr (#7313)

* Added codeRepr and dumpCode to the macros module.

This allows those writing macros to write examples, get the code to generate the AST for that example, and then modify that code to be dynamic with the macro function.
This commit is contained in:
PMunch
2018-03-16 15:57:40 +01:00
committed by Andreas Rumpf
parent d56ca42b1a
commit a9f21cffdf
2 changed files with 26 additions and 6 deletions

View File

@@ -678,7 +678,7 @@ proc astGenRepr*(n: NimNode): string {.compileTime, benign.} =
## See also `repr`, `treeRepr`, and `lispRepr`.
const
NodeKinds = {nnkEmpty, nnkNilLit, nnkIdent, nnkSym, nnkNone}
NodeKinds = {nnkEmpty, nnkNilLit, nnkIdent, nnkSym, nnkNone, nnkCommentStmt}
LitKinds = {nnkCharLit..nnkInt64Lit, nnkFloatLit..nnkFloat64Lit, nnkStrLit..nnkTripleStrLit}
proc escape(s: string, prefix = "\"", suffix = "\""): string {.noSideEffect.} =
@@ -723,7 +723,7 @@ proc astGenRepr*(n: NimNode): string {.compileTime, benign.} =
of nnkCharLit: res.add("'" & $chr(n.intVal) & "'")
of nnkIntLit..nnkInt64Lit: res.add($n.intVal)
of nnkFloatLit..nnkFloat64Lit: res.add($n.floatVal)
of nnkStrLit..nnkTripleStrLit: res.add($n.strVal.escape())
of nnkStrLit..nnkTripleStrLit, nnkCommentStmt: res.add($n.strVal.escape())
of nnkIdent: res.add(($n.ident).escape())
of nnkSym: res.add(($n.symbol).escape())
of nnkNone: assert false

View File

@@ -2,16 +2,33 @@ discard """
msg: '''nnkStmtList.newTree(
nnkVarSection.newTree(
nnkIdentDefs.newTree(
newIdentNode(!"x"),
newIdentNode("x"),
newEmptyNode(),
nnkCall.newTree(
nnkDotExpr.newTree(
newIdentNode(!"foo"),
newIdentNode(!"create")
newIdentNode("baz"),
newIdentNode("create")
),
newLit(56)
)
)
),
nnkProcDef.newTree(
newIdentNode("foo"),
newEmptyNode(),
newEmptyNode(),
nnkFormalParams.newTree(
newEmptyNode()
),
newEmptyNode(),
newEmptyNode(),
nnkStmtList.newTree(
newCommentStmtNode("This is a docstring"),
nnkCommand.newTree(
newIdentNode("echo"),
newLit("bar")
)
)
)
)'''
"""
@@ -21,5 +38,8 @@ msg: '''nnkStmtList.newTree(
import macros
dumpAstGen:
var x = foo.create(56)
var x = baz.create(56)
proc foo() =
## This is a docstring
echo "bar"