mirror of
https://github.com/nim-lang/Nim.git
synced 2025-12-28 17:04:41 +00:00
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:
@@ -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
|
||||
|
||||
@@ -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"
|
||||
|
||||
Reference in New Issue
Block a user