W3cubDocs

/Nim

Module macros

This module contains the interface to the compiler's abstract syntax tree (AST). Macros operate on this tree.

The AST in Nim

This section describes how the AST is modelled with Nim's type system. The AST consists of nodes (NimNode) with a variable number of children. Each node has a field named kind which describes what the node contains:

type
  NimNodeKind = enum     ## kind of a node; only explanatory
    nnkNone,             ## invalid node kind
    nnkEmpty,            ## empty node
    nnkIdent,            ## node contains an identifier
    nnkIntLit,           ## node contains an int literal (example: 10)
    nnkStrLit,           ## node contains a string literal (example: "abc")
    nnkNilLit,           ## node contains a nil literal (example: nil)
    nnkCaseStmt,         ## node represents a case statement
    ...                  ## many more
  
  NimNode = ref NimNodeObj
  NimNodeObj = object
    case kind: NimNodeKind           ## the node's kind
    of nnkNone, nnkEmpty, nnkNilLit:
      discard                        ## node contains no additional fields
    of nnkCharLit..nnkUInt64Lit:
      intVal: BiggestInt             ## the int literal
    of nnkFloatLit..nnkFloat64Lit:
      floatVal: BiggestFloat         ## the float literal
    of nnkStrLit..nnkTripleStrLit, nnkCommentStmt, nnkIdent, nnkSym:
      strVal: string                 ## the string literal
    else:
      sons: seq[NimNode]             ## the node's sons (or children)

For the NimNode type, the [] operator has been overloaded: n[i] is n's i-th child.

To specify the AST for the different Nim constructs, the notation nodekind(son1, son2, ...) or nodekind(value) or nodekind(field=value) is used.

Some child may be missing. A missing child is a node of kind nnkEmpty; a child can never be nil.

Leaf nodes/Atoms

A leaf of the AST often corresponds to a terminal symbol in the concrete syntax. Note that the default float in Nim maps to float64 such that the default AST for a float is nnkFloat64Lit as below.

Nim expression Corresponding AST
42 nnkIntLit(intVal = 42)
42'i8 nnkInt8Lit(intVal = 42)
42'i16 nnkInt16Lit(intVal = 42)
42'i32 nnkInt32Lit(intVal = 42)
42'i64 nnkInt64Lit(intVal = 42)
42'u8 nnkUInt8Lit(intVal = 42)
42'u16 nnkUInt16Lit(intVal = 42)
42'u32 nnkUInt32Lit(intVal = 42)
42'u64 nnkUInt64Lit(intVal = 42)
42.0 nnkFloat64Lit(floatVal = 42.0)
42.0'f32 nnkFloat32Lit(floatVal = 42.0)
42.0'f64 nnkFloat64Lit(floatVal = 42.0)
"abc" nnkStrLit(strVal = "abc")
r"abc" nnkRStrLit(strVal = "abc")
"""abc""" nnkTripleStrLit(strVal = "abc")
' ' nnkCharLit(intVal = 32)
nil nnkNilLit()
myIdentifier nnkIdent(strVal = "myIdentifier")
myIdentifier after lookup pass: nnkSym(strVal = "myIdentifier", ...)

Identifiers are nnkIdent nodes. After the name lookup pass these nodes get transferred into nnkSym nodes.

Calls/expressions

Command call

Concrete syntax:

echo "abc", "xyz"

AST:

nnkCommand(
  nnkIdent("echo"),
  nnkStrLit("abc"),
  nnkStrLit("xyz")
)

Call with ()

Concrete syntax:

echo("abc", "xyz")

AST:

nnkCall(
  nnkIdent("echo"),
  nnkStrLit("abc"),
  nnkStrLit("xyz")
)

Infix operator call

Concrete syntax:

"abc" & "xyz"

AST:

nnkInfix(
  nnkIdent("&"),
  nnkStrLit("abc"),
  nnkStrLit("xyz")
)

Note that with multiple infix operators, the command is parsed by operator precedence.

Concrete syntax:

5 + 3 * 4

AST:

nnkInfix(
  nnkIdent("+"),
  nnkIntLit(5),
  nnkInfix(
    nnkIdent("*"),
    nnkIntLit(3),
    nnkIntLit(4)
  )
)

As a side note, if you choose to use infix operators in a prefix form, the AST behaves as a [parenthetical function call](./macros.html#calls-expressions-call-with) with nnkAccQuoted, as follows:

Concrete syntax:

`+`(3, 4)

AST:

nnkCall(
  nnkAccQuoted(
    nnkIdent("+")
  ),
  nnkIntLit(3),
  nnkIntLit(4)
)

Prefix operator call

Concrete syntax:

? "xyz"

AST:

nnkPrefix(
  nnkIdent("?"),
  nnkStrLit("abc")
)

Postfix operator call

Note: There are no postfix operators in Nim. However, the nnkPostfix node is used for the asterisk export marker *:

Concrete syntax:

identifier*

AST:

nnkPostfix(
  nnkIdent("*"),
  nnkIdent("identifier")
)

Call with named arguments

Concrete syntax:

writeLine(file=stdout, "hallo")

AST:

nnkCall(
  nnkIdent("writeLine"),
  nnkExprEqExpr(
    nnkIdent("file"),
    nnkIdent("stdout")
  ),
  nnkStrLit("hallo")
)

Call with raw string literal

This is used, for example, in the bindSym examples [here](http://nim-lang.org/docs/manual.html#macros-bindsym) and with re"some regexp" in the regular expression module.

Concrete syntax:

echo"abc"

AST:

nnkCallStrLit(
  nnkIdent("echo"),
  nnkRStrLit("hello")
)

Dereference operator []

Concrete syntax:

x[]

AST:

nnkDerefExpr(nnkIdent("x"))

Addr operator

Concrete syntax:

addr(x)

AST:

nnkAddr(nnkIdent("x"))

Cast operator

Concrete syntax:

cast[T](x)

AST:

nnkCast(nnkIdent("T"), nnkIdent("x"))

Object access operator .

Concrete syntax:

x.y

AST:

nnkDotExpr(nnkIdent("x"), nnkIdent("y"))

If you use Nim's flexible calling syntax (as in x.len()), the result is the same as above but wrapped in an nnkCall.

Array access operator []

Concrete syntax:

x[y]

AST:

nnkBracketExpr(nnkIdent("x"), nnkIdent("y"))

Parentheses

Parentheses for affecting operator precedence or tuple construction are built with the nnkPar node.

Concrete syntax:

(1, 2, (3))

AST:

nnkPar(nnkIntLit(1), nnkIntLit(2), nnkPar(nnkIntLit(3)))

Curly braces

Curly braces are used as the set constructor.

Concrete syntax:

{1, 2, 3}

AST:

nnkCurly(nnkIntLit(1), nnkIntLit(2), nnkIntLit(3))

When used as a table constructor, the syntax is different.

Concrete syntax:

{a: 3, b: 5}

AST:

nnkTableConstr(
  nnkExprColonExpr(nnkIdent("a"), nnkIntLit(3)),
  nnkExprColonExpr(nnkIdent("b"), nnkIntLit(5))
)

Brackets

Brackets are used as the array constructor.

Concrete syntax:

[1, 2, 3]

AST:

nnkBracket(nnkIntLit(1), nnkIntLit(2), nnkIntLit(3))

Ranges

Ranges occur in set constructors, case statement branches, or array slices. Internally, the node kind nnkRange is used, but when constructing the AST, construction with .. as an infix operator should be used instead.

Concrete syntax:

1..3

AST:

nnkInfix(
  nnkIdent(".."),
  nnkIntLit(1),
  nnkIntLit(3)
)

Example code:

macro genRepeatEcho(): stmt =
  result = newNimNode(nnkStmtList)
  
  var forStmt = newNimNode(nnkForStmt) # generate a for statement
  forStmt.add(ident("i")) # use the variable `i` for iteration
  
  var rangeDef = newNimNode(nnkInfix).add(
    ident("..")).add(
    newIntLitNode(3),newIntLitNode(5)) # iterate over the range 3..5
  
  forStmt.add(rangeDef)
  forStmt.add(newCall(ident("echo"), newIntLitNode(3))) # meat of the loop
  result.add(forStmt)

genRepeatEcho() # gives:
                # 3
                # 3
                # 3

If expression

The representation of the if expression is subtle, but easy to traverse.

Concrete syntax:

if cond1: expr1 elif cond2: expr2 else: expr3

AST:

nnkIfExpr(
  nnkElifExpr(cond1, expr1),
  nnkElifExpr(cond2, expr2),
  nnkElseExpr(expr3)
)

Documentation Comments

Double-hash (##) comments in the code actually have their own format, using strVal to get and set the comment text. Single-hash (#) comments are ignored.

Concrete syntax:

## This is a comment
## This is part of the first comment
stmt1
## Yet another

AST:

nnkCommentStmt() # only appears once for the first two lines!
stmt1
nnkCommentStmt() # another nnkCommentStmt because there is another comment
                 # (separate from the first)

Pragmas

One of Nim's cool features is pragmas, which allow fine-tuning of various aspects of the language. They come in all types, such as adorning procs and objects, but the standalone emit pragma shows the basics with the AST.

Concrete syntax:

{.emit: "#include <stdio.h>".}

AST:

nnkPragma(
  nnkExprColonExpr(
    nnkIdent("emit"),
    nnkStrLit("#include <stdio.h>") # the "argument"
  )
)

As many nnkIdent appear as there are pragmas between {..}. Note that the declaration of new pragmas is essentially the same:

Concrete syntax:

{.pragma: cdeclRename, cdecl.}

AST:

nnkPragma(
  nnkExprColonExpr(
    nnkIdent("pragma"), # this is always first when declaring a new pragma
    nnkIdent("cdeclRename") # the name of the pragma
  ),
  nnkIdent("cdecl")
)

Statements

If statement

The representation of the if statement is subtle, but easy to traverse. If there is no else branch, no nnkElse child exists.

Concrete syntax:

if cond1:
  stmt1
elif cond2:
  stmt2
elif cond3:
  stmt3
else:
  stmt4

AST:

nnkIfStmt(
  nnkElifBranch(cond1, stmt1),
  nnkElifBranch(cond2, stmt2),
  nnkElifBranch(cond3, stmt3),
  nnkElse(stmt4)
)

When statement

Like the if statement, but the root has the kind nnkWhenStmt.

Assignment

Concrete syntax:

x = 42

AST:

nnkAsgn(nnkIdent("x"), nnkIntLit(42))

This is not the syntax for assignment when combined with var, let, or const.

Statement list

Concrete syntax:

stmt1
stmt2
stmt3

AST:

nnkStmtList(stmt1, stmt2, stmt3)

Case statement

Concrete syntax:

case expr1
of expr2, expr3..expr4:
  stmt1
of expr5:
  stmt2
elif cond1:
  stmt3
else:
  stmt4

AST:

nnkCaseStmt(
  expr1,
  nnkOfBranch(expr2, nnkRange(expr3, expr4), stmt1),
  nnkOfBranch(expr5, stmt2),
  nnkElifBranch(cond1, stmt3),
  nnkElse(stmt4)
)

The nnkElifBranch and nnkElse parts may be missing.

While statement

Concrete syntax:

while expr1:
  stmt1

AST:

nnkWhileStmt(expr1, stmt1)

For statement

Concrete syntax:

for ident1, ident2 in expr1:
  stmt1

AST:

nnkForStmt(ident1, ident2, expr1, stmt1)

Try statement

Concrete syntax:

try:
  stmt1
except e1, e2:
  stmt2
except e3:
  stmt3
except:
  stmt4
finally:
  stmt5

AST:

nnkTryStmt(
  stmt1,
  nnkExceptBranch(e1, e2, stmt2),
  nnkExceptBranch(e3, stmt3),
  nnkExceptBranch(stmt4),
  nnkFinally(stmt5)
)

Return statement

Concrete syntax:

return expr1

AST:

nnkReturnStmt(expr1)

Yield statement

Like return, but with nnkYieldStmt kind.

nnkYieldStmt(expr1)

Discard statement

Like return, but with nnkDiscardStmt kind.

nnkDiscardStmt(expr1)

Continue statement

Concrete syntax:

continue

AST:

nnkContinueStmt()

Break statement

Concrete syntax:

break otherLocation

AST:

nnkBreakStmt(nnkIdent("otherLocation"))

If break is used without a jump-to location, nnkEmpty replaces nnkIdent.

Block statement

Concrete syntax:

block name:

AST:

nnkBlockStmt(nnkIdent("name"), nnkStmtList(...))

A block doesn't need an name, in which case nnkEmpty is used.

Asm statement

Concrete syntax:

asm """
  some asm
"""

AST:

nnkAsmStmt(
  nnkEmpty(), # for pragmas
  nnkTripleStrLit("some asm"),
)

Import section

Nim's import statement actually takes different variations depending on what keywords are present. Let's start with the simplest form.

Concrete syntax:

import math

AST:

nnkImportStmt(nnkIdent("math"))

With except, we get nnkImportExceptStmt.

Concrete syntax:

import math except pow

AST:

nnkImportExceptStmt(nnkIdent("math"),nnkIdent("pow"))

Note that import math as m does not use a different node; rather, we use nnkImportStmt with as as an infix operator.

Concrete syntax:

import strutils as su

AST:

nnkImportStmt(
  nnkInfix(
    nnkIdent("as"),
    nnkIdent("strutils"),
    nnkIdent("su")
  )
)

From statement

If we use from ... import, the result is different, too.

Concrete syntax:

from math import pow

AST:

nnkFromStmt(nnkIdent("math"), nnkIdent("pow"))

Using from math as m import pow works identically to the as modifier with the import statement, but wrapped in nnkFromStmt.

Export statement

When you are making an imported module accessible by modules that import yours, the export syntax is pretty straightforward.

Concrete syntax:

export unsigned

AST:

nnkExportStmt(nnkIdent("unsigned"))

Similar to the import statement, the AST is different for export ... except.

Concrete syntax:

export math except pow # we're going to implement our own exponentiation

AST:

nnkExportExceptStmt(nnkIdent("math"),nnkIdent("pow"))

Include statement

Like a plain import statement but with nnkIncludeStmt.

Concrete syntax:

include blocks

AST:

nnkIncludeStmt(nnkIdent("blocks"))

Var section

Concrete syntax:

var a = 3

AST:

nnkVarSection(
  nnkIdentDefs(
    nnkIdent("a"),
    nnkEmpty(), # or nnkIdent(...) if the variable declares the type
    nnkIntLit(3),
  )
)

Note that either the second or third (or both) parameters above must exist, as the compiler needs to know the type somehow (which it can infer from the given assignment).

This is not the same AST for all uses of var. See [Procedure declaration](http://nim-lang.org/docs/macros.html#statements-procedure-declaration) for details.

Let section

This is equivalent to var, but with nnkLetSection rather than nnkVarSection.

Concrete syntax:

let a = 3

AST:

nnkLetSection(
  nnkIdentDefs(
    nnkIdent("a"),
    nnkEmpty(), # or nnkIdent(...) for the type
    nnkIntLit(3),
  )
)

Const section

Concrete syntax:

const a = 3

AST:

nnkConstSection(
  nnkConstDef( # not nnkConstDefs!
    nnkIdent("a"),
    nnkEmpty(), # or nnkIdent(...) if the variable declares the type
    nnkIntLit(3), # required in a const declaration!
  )
)

Type section

Starting with the simplest case, a type section appears much like var and const.

Concrete syntax:

type A = int

AST:

nnkTypeSection(
  nnkTypeDef(
    nnkIdent("A"),
    nnkEmpty(),
    nnkIdent("int")
  )
)

Declaring distinct types is similar, with the last nnkIdent wrapped in nnkDistinctTy.

Concrete syntax:

type MyInt = distinct int

AST:

# ...
nnkTypeDef(
  nnkIdent("MyInt"),
  nnkEmpty(),
  nnkDistinctTy(
    nnkIdent("int")
  )
)

If a type section uses generic parameters, they are treated here:

Concrete syntax:

type A[T] = expr1

AST:

nnkTypeSection(
  nnkTypeDef(
    nnkIdent("A"),
    nnkGenericParams(
      nnkIdentDefs(
        nnkIdent("T"),
        nnkEmpty(), # if the type is declared with options, like
                    # ``[T: SomeInteger]``, they are given here
        nnkEmpty(),
      )
    )
    expr1,
  )
)

Note that not all nnkTypeDef utilize nnkIdent as their their parameter. One of the most common uses of type declarations is to work with objects.

Concrete syntax:

type IO = object of RootObj

AST:

# ...
nnkTypeDef(
  nnkIdent("IO"),
  nnkEmpty(),
  nnkObjectTy(
    nnkEmpty(), # no pragmas here
    nnkOfInherit(
      nnkIdent("RootObj") # inherits from RootObj
    )
    nnkEmpty()
  )
)

Nim's object syntax is rich. Let's take a look at an involved example in its entirety to see some of the complexities.

Concrete syntax:

type Obj[T] = object {.inheritable.}
  name: string
  case isFat: bool
  of true:
    m: array[100_000, T]
  of false:
    m: array[10, T]

AST:

# ...
nnkObjectTy(
  nnkPragma(
    nnkIdent("inheritable")
  ),
  nnkEmpty(),
  nnkRecList( # list of object parameters
    nnkIdentDefs(
      nnkIdent("name"),
      nnkIdent("string"),
      nnkEmpty()
    ),
    nnkRecCase( # case statement within object (not nnkCaseStmt)
      nnkIdentDefs(
        nnkIdent("isFat"),
        nnkIdent("bool"),
        nnkEmpty()
      ),
      nnkOfBranch(
        nnkIdent("true"),
        nnkRecList( # again, a list of object parameters
          nnkIdentDefs(
            nnkIdent("m"),
            nnkBracketExpr(
              nnkIdent("array"),
              nnkIntLit(100000),
              nnkIdent("T")
            ),
            nnkEmpty()
        )
      ),
      nnkOfBranch(
        nnkIdent("false"),
        nnkRecList(
          nnkIdentDefs(
            nnkIdent("m"),
            nnkBracketExpr(
              nnkIdent("array"),
              nnkIntLit(10),
              nnkIdent("T")
            ),
            nnkEmpty()
          )
        )
      )
    )
  )
)

Using an enum is similar to using an object.

Concrete syntax:

type X = enum
  First

AST:

# ...
nnkEnumTy(
  nnkEmpty(),
  nnkIdent("First") # you need at least one nnkIdent or the compiler complains
)

The usage of concept (experimental) is similar to objects.

Concrete syntax:

type Con = concept x,y,z
  (x & y & z) is string

AST:

# ...
nnkTypeClassTy( # note this isn't nnkConceptTy!
  nnkArglist(
    # ... idents for x, y, z
  )
  # ...
)

Static types, like static[int], use nnkIdent wrapped in nnkStaticTy.

Concrete syntax:

type A[T: static[int]] = object

AST:

# ... within nnkGenericParams
nnkIdentDefs(
  nnkIdent("T"),
  nnkStaticTy(
    nnkIdent("int")
  ),
  nnkEmpty()
)
# ...

In general, declaring types mirrors this syntax (i.e., nnkStaticTy for static, etc.). Examples follow (exceptions marked by *):

Nim type Corresponding AST
static nnkStaticTy
tuple nnkTupleTy
var nnkVarTy
ptr nnkPtrTy
ref nnkRefTy
distinct nnkDistinctTy
enum nnkEnumTy
concept nnkTypeClassTy*
array nnkBracketExpr(nnkIdent("array"),...*
proc nnkProcTy
iterator nnkIteratorTy
object nnkObjectTy

Take special care when declaring types as proc. The behavior is similar to Procedure declaration, below, but does not treat nnkGenericParams. Generic parameters are treated in the type, not the proc itself.

Concrete syntax:

type MyProc[T] = proc(x: T)

AST:

# ...
nnkTypeDef(
  nnkIdent("MyProc"),
  nnkGenericParams( # here, not with the proc
    # ...
  )
  nnkProcTy( # behaves like a procedure declaration from here on
    nnkFormalParams(
      # ...
    )
  )
)

The same syntax applies to iterator (with nnkIteratorTy), but does not apply to converter or template.

Mixin statement

Concrete syntax:

mixin x

AST:

nnkMixinStmt(nnkIdent("x"))

Bind statement

Concrete syntax:

bind x

AST:

nnkBindStmt(nnkIdent("x"))

Procedure declaration

Let's take a look at a procedure with a lot of interesting aspects to get a feel for how procedure calls are broken down.

Concrete syntax:

proc hello*[T: SomeInteger](x: int = 3, y: float32): int {.inline.} = discard

AST:

nnkProcDef(
  nnkPostfix(nnkIdent("*"), nnkIdent("hello")), # the exported proc name
  nnkEmpty(), # patterns for term rewriting in templates and macros (not procs)
  nnkGenericParams( # generic type parameters, like with type declaration
    nnkIdentDefs(
      nnkIdent("T"), nnkIdent("SomeInteger")
    )
  ),
  nnkFormalParams(
    nnkIdent("int"), # the first FormalParam is the return type. nnkEmpty() if there is none
    nnkIdentDefs(
      nnkIdent("x"),
      nnkIdent("int"), # type type (required for procs, not for templates)
      nnkIntLit(3) # a default value
    ),
    nnkIdentDefs(
      nnkIdent("y"),
      nnkIdent("float32"),
      nnkEmpty()
    )
  ),
  nnkPragma(nnkIdent("inline")),
  nnkEmpty(), # reserved slot for future use
  nnkStmtList(nnkDiscardStmt(nnkEmpty())) # the meat of the proc
)

There is another consideration. Nim has flexible type identification for its procs. Even though proc(a: int, b: int) and proc(a, b: int) are equivalent in the code, the AST is a little different for the latter.

Concrete syntax:

proc(a, b: int)

AST:

# ...AST as above...
nnkFormalParams(
  nnkEmpty(), # no return here
  nnkIdentDefs(
    nnkIdent("a"), # the first parameter
    nnkIdent("b"), # directly to the second parameter
    nnkIdent("int"), # their shared type identifier
    nnkEmpty(), # default value would go here
  )
),
# ...

When a procedure uses the special var type return variable, the result is different from that of a var section.

Concrete syntax:

proc hello(): var int

AST:

# ...
nnkFormalParams(
  nnkVarTy(
    nnkIdent("int")
  )
)

Iterator declaration

The syntax for iterators is similar to procs, but with nnkIteratorDef replacing nnkProcDef.

Concrete syntax:

iterator nonsense[T](x: seq[T]): float {.closure.} = ...

AST:

nnkIteratorDef(
  nnkIdent("nonsense"),
  nnkEmpty(),
  ...
)

Converter declaration

A converter is similar to a proc.

Concrete syntax:

converter toBool(x: float): bool

AST:

nnkConverterDef(
  nnkIdent("toBool"),
  # ...
)

Template declaration

Templates (as well as macros, as we'll see) have a slightly expanded AST when compared to procs and iterators. The reason for this is [term-rewriting macros](http://nim-lang.org/docs/manual.html#term-rewriting-macros). Notice the nnkEmpty() as the second argument to nnkProcDef and nnkIteratorDef above? That's where the term-rewriting macros go.

Concrete syntax:

template optOpt{expr1}(a: int): int

AST:

nnkTemplateDef(
  nnkIdent("optOpt"),
  nnkStmtList( # instead of nnkEmpty()
    expr1
  ),
  # follows like a proc or iterator
)

If the template does not have types for its parameters, the type identifiers inside nnkFormalParams just becomes nnkEmpty.

Macro declaration

Macros behave like templates, but nnkTemplateDef is replaced with nnkMacroDef.

Special node kinds

There are several node kinds that are used for semantic checking or code generation. These are accessible from this module, but should not be used. Other node kinds are especially designed to make AST manipulations easier. These are explained here.

To be written.

Types

NimNodeKind = enum
  nnkNone, nnkEmpty, nnkIdent, nnkSym, nnkType, nnkCharLit, nnkIntLit, nnkInt8Lit,
  nnkInt16Lit, nnkInt32Lit, nnkInt64Lit, nnkUIntLit, nnkUInt8Lit, nnkUInt16Lit,
  nnkUInt32Lit, nnkUInt64Lit, nnkFloatLit, nnkFloat32Lit, nnkFloat64Lit,
  nnkFloat128Lit, nnkStrLit, nnkRStrLit, nnkTripleStrLit, nnkNilLit, nnkComesFrom,
  nnkDotCall, nnkCommand, nnkCall, nnkCallStrLit, nnkInfix, nnkPrefix, nnkPostfix,
  nnkHiddenCallConv, nnkExprEqExpr, nnkExprColonExpr, nnkIdentDefs, nnkVarTuple,
  nnkPar, nnkObjConstr, nnkCurly, nnkCurlyExpr, nnkBracket, nnkBracketExpr,
  nnkPragmaExpr, nnkRange, nnkDotExpr, nnkCheckedFieldExpr, nnkDerefExpr, nnkIfExpr,
  nnkElifExpr, nnkElseExpr, nnkLambda, nnkDo, nnkAccQuoted, nnkTableConstr, nnkBind,
  nnkClosedSymChoice, nnkOpenSymChoice, nnkHiddenStdConv, nnkHiddenSubConv, nnkConv,
  nnkCast, nnkStaticExpr, nnkAddr, nnkHiddenAddr, nnkHiddenDeref, nnkObjDownConv,
  nnkObjUpConv, nnkChckRangeF, nnkChckRange64, nnkChckRange, nnkStringToCString,
  nnkCStringToString, nnkAsgn, nnkFastAsgn, nnkGenericParams, nnkFormalParams,
  nnkOfInherit, nnkImportAs, nnkProcDef, nnkMethodDef, nnkConverterDef, nnkMacroDef,
  nnkTemplateDef, nnkIteratorDef, nnkOfBranch, nnkElifBranch, nnkExceptBranch,
  nnkElse, nnkAsmStmt, nnkPragma, nnkPragmaBlock, nnkIfStmt, nnkWhenStmt, nnkForStmt,
  nnkParForStmt, nnkWhileStmt, nnkCaseStmt, nnkTypeSection, nnkVarSection,
  nnkLetSection, nnkConstSection, nnkConstDef, nnkTypeDef, nnkYieldStmt, nnkDefer,
  nnkTryStmt, nnkFinally, nnkRaiseStmt, nnkReturnStmt, nnkBreakStmt, nnkContinueStmt,
  nnkBlockStmt, nnkStaticStmt, nnkDiscardStmt, nnkStmtList, nnkImportStmt,
  nnkImportExceptStmt, nnkExportStmt, nnkExportExceptStmt, nnkFromStmt,
  nnkIncludeStmt, nnkBindStmt, nnkMixinStmt, nnkUsingStmt, nnkCommentStmt,
  nnkStmtListExpr, nnkBlockExpr, nnkStmtListType, nnkBlockType, nnkWith, nnkWithout,
  nnkTypeOfExpr, nnkObjectTy, nnkTupleTy, nnkTupleClassTy, nnkTypeClassTy,
  nnkStaticTy, nnkRecList, nnkRecCase, nnkRecWhen, nnkRefTy, nnkPtrTy, nnkVarTy,
  nnkConstTy, nnkMutableTy, nnkDistinctTy, nnkProcTy, nnkIteratorTy, nnkSharedTy,
  nnkEnumTy, nnkEnumFieldDef, nnkArglist, nnkPattern, nnkReturnToken, nnkClosure,
  nnkGotoState, nnkState, nnkBreakState, nnkFuncDef, nnkTupleConstr
NimNodeKinds = set[NimNodeKind]
NimTypeKind = enum
  ntyNone, ntyBool, ntyChar, ntyEmpty, ntyAlias, ntyNil, ntyExpr, ntyStmt, ntyTypeDesc,
  ntyGenericInvocation, ntyGenericBody, ntyGenericInst, ntyGenericParam,
  ntyDistinct, ntyEnum, ntyOrdinal, ntyArray, ntyObject, ntyTuple, ntySet, ntyRange,
  ntyPtr, ntyRef, ntyVar, ntySequence, ntyProc, ntyPointer, ntyOpenArray, ntyString,
  ntyCString, ntyForward, ntyInt, ntyInt8, ntyInt16, ntyInt32, ntyInt64, ntyFloat,
  ntyFloat32, ntyFloat64, ntyFloat128, ntyUInt, ntyUInt8, ntyUInt16, ntyUInt32,
  ntyUInt64, ntyUnused0, ntyUnused1, ntyUnused2, ntyVarargs, ntyUnused, ntyError,
  ntyBuiltinTypeClass, ntyUserTypeClass, ntyUserTypeClassInst,
  ntyCompositeTypeClass, ntyInferred, ntyAnd, ntyOr, ntyNot, ntyAnything, ntyStatic,
  ntyFromExpr, ntyOpt, ntyVoid
TNimTypeKinds {...}{.deprecated.} = set[NimTypeKind]
NimSymKind = enum
  nskUnknown, nskConditional, nskDynLib, nskParam, nskGenericParam, nskTemp,
  nskModule, nskType, nskVar, nskLet, nskConst, nskResult, nskProc, nskFunc, nskMethod,
  nskIterator, nskConverter, nskMacro, nskTemplate, nskField, nskEnumField, nskForVar,
  nskLabel, nskStub
TNimSymKinds {...}{.deprecated.} = set[NimSymKind]
NimIdent = object of RootObj
represents a Nim identifier in the AST. Note: This is only rarely useful, for identifier construction from a string use ident"abc".
NimSym {...}{.deprecated.} = ref NimSymObj
represents a Nim symbol in the compiler; a symbol is a looked-up ident.
BindSymRule = enum
  brClosed,                   ## only the symbols in current scope are bound
  brOpen,                     ## open wrt overloaded symbols, but may be a single
         ## symbol if not ambiguous (the rules match that of
         ## binding in generics)
  brForceOpen                 ## same as brOpen, but it will always be open even
             ## if not ambiguous (this cannot be achieved with
             ## any other means in the language currently)
specifies how bindSym behaves
LineInfo = object
  filename*: string
  line*, column*: int

Consts

nnkLiterals = {nnkCharLit..nnkNilLit}
nnkCallKinds = {nnkCall, nnkInfix, nnkPrefix, nnkPostfix, nnkCommand, nnkCallStrLit}
RoutineNodes = {nnkProcDef, nnkFuncDef, nnkMethodDef, nnkDo, nnkLambda, nnkIteratorDef,
              nnkTemplateDef, nnkConverterDef}
AtomicNodes = {nnkNone..nnkNilLit}
CallNodes = {nnkCall, nnkInfix, nnkPrefix, nnkPostfix, nnkCommand, nnkCallStrLit,
           nnkHiddenCallConv}

Procs

proc `!`(s: string): NimIdent {...}{.magic: "StrToIdent", noSideEffect, deprecated.}
constructs an identifier from the string s Deprecated since version 0.18.0: Use ident or newIdentNode instead.
proc toNimIdent(s: string): NimIdent {...}{.magic: "StrToIdent", noSideEffect, deprecated.}
constructs an identifier from the string s Deprecated since version 0.18.1; Use ident or newIdentNode instead.
proc `==`(a, b: NimIdent): bool {...}{.magic: "EqIdent", noSideEffect, deprecated.}
compares two Nim identifiers Deprecated since version 0.18.1; Use == on NimNode instead.
proc `==`(a, b: NimNode): bool {...}{.magic: "EqNimrodNode", noSideEffect.}
compares two Nim nodes
proc `==`(a, b: NimSym): bool {...}{.magic: "EqNimrodNode", noSideEffect, deprecated.}
compares two Nim symbols Deprecated since version 0.18.1; Use ```==`(NimNode,NimNode)`` instead.
proc sameType(a, b: NimNode): bool {...}{.magic: "SameNodeType", noSideEffect, raises: [],
                                tags: [].}
compares two Nim nodes' types. Return true if the types are the same, eg. true when comparing alias with original type.
proc len(n: NimNode): int {...}{.magic: "NLen", noSideEffect.}
returns the number of children of n.
proc `[]`(n: NimNode; i: int): NimNode {...}{.magic: "NChild", noSideEffect.}
get n's i'th child.
proc `[]`(n: NimNode; i: BackwardsIndex): NimNode {...}{.raises: [], tags: [].}
get n's i'th child.
proc `[]=`(n: NimNode; i: int; child: NimNode) {...}{.magic: "NSetChild", noSideEffect.}
set n's i'th child to child.
proc `[]=`(n: NimNode; i: BackwardsIndex; child: NimNode) {...}{.raises: [], tags: [].}
set n's i'th child to child.
proc add(father, child: NimNode): NimNode {...}{.magic: "NAdd", discardable, noSideEffect,
                                       locks: 0.}
Adds the child to the father node. Returns the father node so that calls can be nested.
proc add(father: NimNode; children: varargs[NimNode]): NimNode {...}{.
    magic: "NAddMultiple", discardable, noSideEffect, locks: 0.}
Adds each child of children to the father node. Returns the father node so that calls can be nested.
proc del(father: NimNode; idx = 0; n = 1) {...}{.magic: "NDel", noSideEffect.}
deletes n children of father starting at index idx.
proc kind(n: NimNode): NimNodeKind {...}{.magic: "NKind", noSideEffect.}
returns the kind of the node n.
proc intVal(n: NimNode): BiggestInt {...}{.magic: "NIntVal", noSideEffect.}
proc floatVal(n: NimNode): BiggestFloat {...}{.magic: "NFloatVal", noSideEffect.}
proc ident(n: NimNode): NimIdent {...}{.magic: "NIdent", noSideEffect, deprecated,
                               raises: [], tags: [].}
Deprecated since version 0.18.1; All functionality is defined on NimNode.
proc symbol(n: NimNode): NimSym {...}{.magic: "NSymbol", noSideEffect, deprecated.}
Deprecated since version 0.18.1; All functionality is defined on NimNode.
proc getImpl(s: NimSym): NimNode {...}{.magic: "GetImpl", noSideEffect, deprecated: "use `getImpl: NimNode -> NimNode` instead".}
proc symKind(symbol: NimNode): NimSymKind {...}{.magic: "NSymKind", noSideEffect.}
proc getImpl(symbol: NimNode): NimNode {...}{.magic: "GetImpl", noSideEffect.}
proc strVal(n: NimNode): string {...}{.magic: "NStrVal", noSideEffect.}
retrieve the implementation of symbol. symbol can be a routine or a const.
proc `$`(i: NimIdent): string {...}{.magic: "NStrVal", noSideEffect, deprecated.}
converts a Nim identifier to a string Deprecated since version 0.18.1; Use strVal instead.
proc `$`(s: NimSym): string {...}{.magic: "NStrVal", noSideEffect, deprecated.}
converts a Nim symbol to a string Deprecated since version 0.18.1; Use strVal instead.
proc owner(sym: NimNode): NimNode {...}{.magic: "SymOwner", noSideEffect.}
accepts node of kind nnkSym and returns its owner's symbol. result is also mnde of kind nnkSym if owner exists otherwise nnkNilLit is returned
proc getType(n: NimNode): NimNode {...}{.magic: "NGetType", noSideEffect.}
with 'getType' you can access the node's type. A Nim type is mapped to a Nim AST too, so it's slightly confusing but it means the same API can be used to traverse types. Recursive types are flattened for you so there is no danger of infinite recursions during traversal. To resolve recursive types, you have to call 'getType' again. To see what kind of type it is, call typeKind on getType's result.
proc getType(n: typedesc): NimNode {...}{.magic: "NGetType", noSideEffect.}
Returns the Nim type node for given type. This can be used to turn macro typedesc parameter into proper NimNode representing type, since typedesc are an exception in macro calls - they are not mapped implicitly to NimNode like any other arguments.
proc typeKind(n: NimNode): NimTypeKind {...}{.magic: "NGetType", noSideEffect.}
Returns the type kind of the node 'n' that should represent a type, that means the node should have been obtained via getType.
proc getTypeInst(n: NimNode): NimNode {...}{.magic: "NGetType", noSideEffect, raises: [],
                                    tags: [].}
Returns the type of a node in a form matching the way the type instance was declared in the code.

Examples:

type
  Vec[N: static[int]; T] = object
    arr: array[N, T]

  Vec4[T] = Vec[4, T]
  Vec4f = Vec4[float32]
var a: Vec4f
var b: Vec4[float32]
var c: Vec[4, float32]
macro dumpTypeInst(x: typed): untyped =
  newLit(x.getTypeInst.repr)

doAssert(dumpTypeInst(a) == "Vec4f")
doAssert(dumpTypeInst(b) == "Vec4[float32]")
doAssert(dumpTypeInst(c) == "Vec[4, float32]")
proc getTypeInst(n: typedesc): NimNode {...}{.magic: "NGetType", noSideEffect.}
Version of getTypeInst which takes a typedesc.
proc getTypeImpl(n: NimNode): NimNode {...}{.magic: "NGetType", noSideEffect, raises: [],
                                    tags: [].}
Returns the type of a node in a form matching the implementation of the type. Any intermediate aliases are expanded to arrive at the final type implementation. You can instead use getImpl on a symbol if you want to find the intermediate aliases.

Examples:

type
  Vec[N: static[int]; T] = object
    arr: array[N, T]

  Vec4[T] = Vec[4, T]
  Vec4f = Vec4[float32]
var a: Vec4f
var b: Vec4[float32]
var c: Vec[4, float32]
macro dumpTypeImpl(x: typed): untyped =
  newLit(x.getTypeImpl.repr)

let t = """object
  arr: array[0 .. 3, float32]
"""
doAssert(dumpTypeImpl(a) == t)
doAssert(dumpTypeImpl(b) == t)
doAssert(dumpTypeImpl(c) == t)
proc getTypeImpl(n: typedesc): NimNode {...}{.magic: "NGetType", noSideEffect.}
Version of getTypeImpl which takes a typedesc.
proc intVal=(n: NimNode; val: BiggestInt) {...}{.magic: "NSetIntVal", noSideEffect.}
proc floatVal=(n: NimNode; val: BiggestFloat) {...}{.magic: "NSetFloatVal", noSideEffect.}
proc symbol=(n: NimNode; val: NimSym) {...}{.magic: "NSetSymbol", noSideEffect, deprecated.}
Deprecated since version 0.18.1; Generate a new NimNode with genSym instead.
proc ident=(n: NimNode; val: NimIdent) {...}{.magic: "NSetIdent", noSideEffect, deprecated.}
Deprecated since version 0.18.1; Generate a new NimNode with ident(string) instead.
proc strVal=(n: NimNode; val: string) {...}{.magic: "NSetStrVal", noSideEffect.}
proc newNimNode(kind: NimNodeKind; lineInfoFrom: NimNode = nil): NimNode {...}{.
    magic: "NNewNimNode", noSideEffect.}

Creates a new AST node of the specified kind.

The lineInfoFrom parameter is used for line information when the produced code crashes. You should ensure that it is set to a node that you are transforming.

proc copyNimNode(n: NimNode): NimNode {...}{.magic: "NCopyNimNode", noSideEffect.}
proc copyNimTree(n: NimNode): NimNode {...}{.magic: "NCopyNimTree", noSideEffect.}
proc error(msg: string; n: NimNode = nil) {...}{.magic: "NError", gcsafe, locks: 0.}
writes an error message at compile time
proc warning(msg: string; n: NimNode = nil) {...}{.magic: "NWarning", gcsafe, locks: 0.}
writes a warning message at compile time
proc hint(msg: string; n: NimNode = nil) {...}{.magic: "NHint", gcsafe, locks: 0.}
writes a hint message at compile time
proc newStrLitNode(s: string): NimNode {...}{.compileTime, noSideEffect, raises: [], tags: [].}
creates a string literal node from s
proc newCommentStmtNode(s: string): NimNode {...}{.compileTime, noSideEffect, raises: [],
    tags: [].}
creates a comment statement node
proc newIntLitNode(i: BiggestInt): NimNode {...}{.compileTime, raises: [], tags: [].}
creates a int literal node from i
proc newFloatLitNode(f: BiggestFloat): NimNode {...}{.compileTime, raises: [], tags: [].}
creates a float literal node from f
proc newIdentNode(i: NimIdent): NimNode {...}{.compileTime, raises: [], tags: [].}
creates an identifier node from i
proc newIdentNode(i: string): NimNode {...}{.magic: "StrToIdent", noSideEffect.}
creates an identifier node from i. It is simply an alias for ident(string). Use that, it's shorter.
proc bindSym(ident: string | NimNode; rule: BindSymRule = brClosed): NimNode {...}{.
    magic: "NBindSym", noSideEffect.}

creates a node that binds ident to a symbol node. The bound symbol may be an overloaded symbol. if ident is a NimNode, it must have nkIdent kind. If rule == brClosed either an nkClosedSymChoice tree is returned or nkSym if the symbol is not ambiguous. If rule == brOpen either an nkOpenSymChoice tree is returned or nkSym if the symbol is not ambiguous. If rule == brForceOpen always an nkOpenSymChoice tree is returned even if the symbol is not ambiguous.

experimental feature: use {.experimental: "dynamicBindSym".} to activate it if called from template / regular code, ident and rule must be constant expression / literal value. if called from macros / compile time procs / static blocks, ident and rule can be VM computed value.

proc genSym(kind: NimSymKind = nskLet; ident = ""): NimNode {...}{.magic: "NGenSym",
    noSideEffect.}
generates a fresh symbol that is guaranteed to be unique. The symbol needs to occur in a declaration context.
proc callsite(): NimNode {...}{.magic: "NCallSite", gcsafe, locks: 0, deprecated: "use varargs[untyped] in the macro prototype instead".}
returns the AST of the invocation expression that invoked this macro. Deprecated since version 0.18.1.
proc toStrLit(n: NimNode): NimNode {...}{.compileTime, raises: [], tags: [].}
converts the AST n to the concrete Nim code and wraps that in a string literal node
proc `$`(arg: LineInfo): string {...}{.raises: [], tags: [].}
returns the position the node appears in the original source file in the form filename(line, col)
proc copyLineInfo(arg: NimNode; info: NimNode) {...}{.magic: "NLineInfo", noSideEffect.}
copy lineinfo from info node
proc lineInfoObj(n: NimNode): LineInfo {...}{.compileTime, raises: [], tags: [].}
returns LineInfo of n, using absolute path for filename
proc lineInfo(arg: NimNode): string {...}{.compileTime, raises: [], tags: [].}
proc internalErrorFlag(): string {...}{.magic: "NError", noSideEffect.}
Some builtins set an error flag. This is then turned into a proper exception. Note: Ordinary application code should not call this.
proc parseExpr(s: string): NimNode {...}{.noSideEffect, compileTime, raises: [ValueError],
                                 tags: [].}
Compiles the passed string to its AST representation. Expects a single expression. Raises ValueError for parsing errors.
proc parseStmt(s: string): NimNode {...}{.noSideEffect, compileTime, raises: [ValueError],
                                 tags: [].}
Compiles the passed string to its AST representation. Expects one or more statements. Raises ValueError for parsing errors.
proc getAst(macroOrTemplate: untyped): NimNode {...}{.magic: "ExpandToAst", noSideEffect.}
Obtains the AST nodes returned from a macro or template invocation. Example:
macro FooMacro() =
  var ast = getAst(BarTemplate())
proc quote(bl: typed; op = "``"): NimNode {...}{.magic: "QuoteAst", noSideEffect.}

Quasi-quoting operator. Accepts an expression or a block and returns the AST that represents it. Within the quoted AST, you are able to interpolate NimNode expressions from the surrounding scope. If no operator is given, quoting is done using backticks. Otherwise, the given operator must be used as a prefix operator for any interpolated expression. The original meaning of the interpolation operator may be obtained by escaping it (by prefixing it with itself): e.g. @ is escaped as @@, @@ is escaped as @@@ and so on.

Example:

macro check(ex: untyped): typed =
  # this is a simplified version of the check macro from the
  # unittest module.
  
  # If there is a failed check, we want to make it easy for
  # the user to jump to the faulty line in the code, so we
  # get the line info here:
  var info = ex.lineinfo
  
  # We will also display the code string of the failed check:
  var expString = ex.toStrLit
  
  # Finally we compose the code to implement the check:
  result = quote do:
    if not `ex`:
      echo `info` & ": Check failed: " & `expString`
proc expectKind(n: NimNode; k: NimNodeKind) {...}{.compileTime, raises: [], tags: [].}
checks that n is of kind k. If this is not the case, compilation aborts with an error message. This is useful for writing macros that check the AST that is passed to them.
proc expectMinLen(n: NimNode; min: int) {...}{.compileTime, raises: [], tags: [].}
checks that n has at least min children. If this is not the case, compilation aborts with an error message. This is useful for writing macros that check its number of arguments.
proc expectLen(n: NimNode; len: int) {...}{.compileTime, raises: [], tags: [].}
checks that n has exactly len children. If this is not the case, compilation aborts with an error message. This is useful for writing macros that check its number of arguments.
proc expectLen(n: NimNode; min, max: int) {...}{.compileTime, raises: [], tags: [].}
checks that n has a number of children in the range min..max. If this is not the case, compilation aborts with an error message. This is useful for writing macros that check its number of arguments.
proc newTree(kind: NimNodeKind; children: varargs[NimNode]): NimNode {...}{.compileTime,
    raises: [], tags: [].}
produces a new node with children.
proc newCall(theProc: NimNode; args: varargs[NimNode]): NimNode {...}{.compileTime,
    raises: [], tags: [].}
produces a new call node. theProc is the proc that is called with the arguments args[0..].
proc newCall(theProc: NimIdent; args: varargs[NimNode]): NimNode {...}{.compileTime,
    deprecated, raises: [], tags: [].}
produces a new call node. theProc is the proc that is called with the arguments args[0..]. Deprecated since version 0.18.1; Use newCall(string, ...), or newCall(NimNode, ...) instead.
proc newCall(theProc: string; args: varargs[NimNode]): NimNode {...}{.compileTime,
    raises: [], tags: [].}
produces a new call node. theProc is the proc that is called with the arguments args[0..].
proc newLit(c: char): NimNode {...}{.compileTime, raises: [], tags: [].}
produces a new character literal node.
proc newLit(i: int): NimNode {...}{.compileTime, raises: [], tags: [].}
produces a new integer literal node.
proc newLit(i: int8): NimNode {...}{.compileTime, raises: [], tags: [].}
produces a new integer literal node.
proc newLit(i: int16): NimNode {...}{.compileTime, raises: [], tags: [].}
produces a new integer literal node.
proc newLit(i: int32): NimNode {...}{.compileTime, raises: [], tags: [].}
produces a new integer literal node.
proc newLit(i: int64): NimNode {...}{.compileTime, raises: [], tags: [].}
produces a new integer literal node.
proc newLit(i: uint): NimNode {...}{.compileTime, raises: [], tags: [].}
produces a new unsigned integer literal node.
proc newLit(i: uint8): NimNode {...}{.compileTime, raises: [], tags: [].}
produces a new unsigned integer literal node.
proc newLit(i: uint16): NimNode {...}{.compileTime, raises: [], tags: [].}
produces a new unsigned integer literal node.
proc newLit(i: uint32): NimNode {...}{.compileTime, raises: [], tags: [].}
produces a new unsigned integer literal node.
proc newLit(i: uint64): NimNode {...}{.compileTime, raises: [], tags: [].}
produces a new unsigned integer literal node.
proc newLit(b: bool): NimNode {...}{.compileTime, raises: [], tags: [].}
produces a new boolean literal node.
proc newLit(f: float32): NimNode {...}{.compileTime, raises: [], tags: [].}
produces a new float literal node.
proc newLit(f: float64): NimNode {...}{.compileTime, raises: [], tags: [].}
produces a new float literal node.
proc newLit(arg: object): NimNode {...}{.compileTime.}
proc newLit[N, T](arg: array[N, T]): NimNode {...}{.compileTime.}
proc newLit[T](arg: seq[T]): NimNode {...}{.compileTime.}
proc newLit(arg: tuple): NimNode {...}{.compileTime.}
proc newLit(s: string): NimNode {...}{.compileTime, raises: [], tags: [].}
produces a new string literal node.
proc nestList(op: NimNode; pack: NimNode): NimNode {...}{.compileTime, raises: [], tags: [].}
nests the list pack into a tree of call expressions: [a, b, c] is transformed into op(a, op(c, d)). This is also known as fold expression.
proc nestList(op: NimNode; pack: NimNode; init: NimNode): NimNode {...}{.compileTime,
    raises: [], tags: [].}
nests the list pack into a tree of call expressions: [a, b, c] is transformed into op(a, op(c, d)). This is also known as fold expression.
proc nestList(theProc: NimIdent; x: NimNode): NimNode {...}{.compileTime, deprecated,
    raises: [], tags: [].}
Deprecated since version 0.18.1; Use one of nestList(NimNode, ...) instead.
proc treeRepr(n: NimNode): string {...}{.compileTime, gcsafe, locks: 0, raises: [], tags: [].}

Convert the AST n to a human-readable tree-like string.

See also repr, lispRepr, and astGenRepr.

proc lispRepr(n: NimNode): string {...}{.compileTime, gcsafe, locks: 0, raises: [], tags: [].}

Convert the AST n to a human-readable lisp-like string,

See also repr, treeRepr, and astGenRepr.

proc astGenRepr(n: NimNode): string {...}{.compileTime, gcsafe, locks: 0, raises: [], tags: [].}
Convert the AST n to the code required to generate that AST. So for example
astGenRepr:
  echo "Hello world"

Would output:

nnkStmtList.newTree(
  nnkCommand.newTree(
    newIdentNode("echo"),
    newLit("Hello world")
  )
)

See also repr, treeRepr, and lispRepr.

proc newEmptyNode(): NimNode {...}{.compileTime, noSideEffect, raises: [], tags: [].}
Create a new empty node
proc newStmtList(stmts: varargs[NimNode]): NimNode {...}{.compileTime, raises: [], tags: [].}
Create a new statement list
proc newPar(exprs: varargs[NimNode]): NimNode {...}{.compileTime, raises: [], tags: [].}
Create a new parentheses-enclosed expression
proc newBlockStmt(label, body: NimNode): NimNode {...}{.compileTime, raises: [], tags: [].}
Create a new block statement with label
proc newBlockStmt(body: NimNode): NimNode {...}{.compiletime, raises: [], tags: [].}
Create a new block: stmt
proc newVarStmt(name, value: NimNode): NimNode {...}{.compiletime, raises: [], tags: [].}
Create a new var stmt
proc newLetStmt(name, value: NimNode): NimNode {...}{.compiletime, raises: [], tags: [].}
Create a new let stmt
proc newConstStmt(name, value: NimNode): NimNode {...}{.compileTime, raises: [], tags: [].}
Create a new const stmt
proc newAssignment(lhs, rhs: NimNode): NimNode {...}{.compileTime, raises: [], tags: [].}
proc newDotExpr(a, b: NimNode): NimNode {...}{.compileTime, raises: [], tags: [].}
Create new dot expression a.dot(b) -> a.b
proc newColonExpr(a, b: NimNode): NimNode {...}{.compileTime, raises: [], tags: [].}
Create new colon expression newColonExpr(a, b) -> a: b
proc newIdentDefs(name, kind: NimNode; default = newEmptyNode()): NimNode {...}{.compileTime,
    raises: [], tags: [].}

Creates a new nnkIdentDefs node of a specific kind and value.

nnkIdentDefs need to have at least three children, but they can have more: first comes a list of identifiers followed by a type and value nodes. This helper proc creates a three node subtree, the first subnode being a single identifier name. Both the kind node and default (value) nodes may be empty depending on where the nnkIdentDefs appears: tuple or object definitions will have an empty default node, let or var blocks may have an empty kind node if the identifier is being assigned a value. Example:

var varSection = newNimNode(nnkVarSection).add(
  newIdentDefs(ident("a"), ident("string")),
  newIdentDefs(ident("b"), newEmptyNode(), newLit(3)))
# --> var
#       a: string
#       b = 3

If you need to create multiple identifiers you need to use the lower level newNimNode:

result = newNimNode(nnkIdentDefs).add(
  ident("a"), ident("b"), ident("c"), ident("string"),
    newStrLitNode("Hello"))
proc newNilLit(): NimNode {...}{.compileTime, raises: [], tags: [].}
New nil literal shortcut
proc last(node: NimNode): NimNode {...}{.compileTime, raises: [], tags: [].}
Return the last item in nodes children. Same as node[^1]
proc expectKind(n: NimNode; k: set[NimNodeKind]) {...}{.compileTime, raises: [], tags: [].}

Raises AssertionError with msg if cond is false. Note that AssertionError is hidden from the effect system, so it doesn't produce {.raises: [AssertionError].}. This exception is only supposed to be caught by unit testing frameworks.

The compiler may not generate any code at all for assert if it is advised to do so through the -d:release or --assertions:off command line switches.

proc newProc(name = newEmptyNode(); params: openArray[NimNode] = [newEmptyNode()];
            body: NimNode = newStmtList(); procType = nnkProcDef): NimNode {...}{.
    compileTime, raises: [], tags: [].}

shortcut for creating a new proc

The params array must start with the return type of the proc, followed by a list of IdentDefs which specify the params.

proc newIfStmt(branches: varargs[tuple[cond, body: NimNode]]): NimNode {...}{.compiletime,
    raises: [], tags: [].}
Constructor for if statements.
newIfStmt(
  (Ident, StmtList),
  ...
)
proc newEnum(name: NimNode; fields: openArray[NimNode]; public, pure: bool): NimNode {...}{.
    compileTime, raises: [], tags: [].}
Creates a new enum. name must be an ident. Fields are allowed to be either idents or EnumFieldDef
newEnum(
  name    = ident("Colors"),
  fields  = [ident("Blue"), ident("Red")],
  public  = true, pure = false)

# type Colors* = Blue Red
proc copyChildrenTo(src, dest: NimNode) {...}{.compileTime, raises: [], tags: [].}
Copy all children from src to dest
proc name(someProc: NimNode): NimNode {...}{.compileTime, raises: [], tags: [].}
proc name=(someProc: NimNode; val: NimNode) {...}{.compileTime, raises: [], tags: [].}
proc params(someProc: NimNode): NimNode {...}{.compileTime, raises: [], tags: [].}
proc params=(someProc: NimNode; params: NimNode) {...}{.compileTime, raises: [], tags: [].}

Raises AssertionError with msg if cond is false. Note that AssertionError is hidden from the effect system, so it doesn't produce {.raises: [AssertionError].}. This exception is only supposed to be caught by unit testing frameworks.

The compiler may not generate any code at all for assert if it is advised to do so through the -d:release or --assertions:off command line switches.

proc pragma(someProc: NimNode): NimNode {...}{.compileTime, raises: [], tags: [].}
Get the pragma of a proc type These will be expanded
proc pragma=(someProc: NimNode; val: NimNode) {...}{.compileTime, raises: [], tags: [].}
Set the pragma of a proc type
proc addPragma(someProc, pragma: NimNode) {...}{.compileTime, raises: [], tags: [].}
Adds pragma to routine definition
proc body(someProc: NimNode): NimNode {...}{.compileTime, raises: [], tags: [].}

Raises AssertionError with msg if cond is false. Note that AssertionError is hidden from the effect system, so it doesn't produce {.raises: [AssertionError].}. This exception is only supposed to be caught by unit testing frameworks.

The compiler may not generate any code at all for assert if it is advised to do so through the -d:release or --assertions:off command line switches.

proc body=(someProc: NimNode; val: NimNode) {...}{.compileTime, raises: [], tags: [].}

Raises AssertionError with msg if cond is false. Note that AssertionError is hidden from the effect system, so it doesn't produce {.raises: [AssertionError].}. This exception is only supposed to be caught by unit testing frameworks.

The compiler may not generate any code at all for assert if it is advised to do so through the -d:release or --assertions:off command line switches.

proc `$`(node: NimNode): string {...}{.compileTime, raises: [Exception], tags: [RootEffect].}
Get the string of an identifier node
proc ident(name: string): NimNode {...}{.magic: "StrToIdent", noSideEffect.}
Create a new ident node from a string
proc insert(a: NimNode; pos: int; b: NimNode) {...}{.compileTime, raises: [], tags: [].}
Insert node B into A at pos
proc basename(a: NimNode): NimNode {...}{.compiletime, gcsafe, locks: 0, raises: [], tags: [].}
Pull an identifier from prefix/postfix expressions
proc basename=(a: NimNode; val: string) {...}{.compileTime, raises: [], tags: [].}
proc postfix(node: NimNode; op: string): NimNode {...}{.compileTime, raises: [], tags: [].}
proc prefix(node: NimNode; op: string): NimNode {...}{.compileTime, raises: [], tags: [].}
proc infix(a: NimNode; op: string; b: NimNode): NimNode {...}{.compileTime, raises: [], tags: [].}
proc unpackPostfix(node: NimNode): tuple[node: NimNode, op: string] {...}{.compileTime,
    raises: [Exception], tags: [RootEffect].}
proc unpackPrefix(node: NimNode): tuple[node: NimNode, op: string] {...}{.compileTime,
    raises: [Exception], tags: [RootEffect].}
proc unpackInfix(node: NimNode): tuple[left: NimNode, op: string, right: NimNode] {...}{.
    compileTime, raises: [Exception], tags: [RootEffect].}

Raises AssertionError with msg if cond is false. Note that AssertionError is hidden from the effect system, so it doesn't produce {.raises: [AssertionError].}. This exception is only supposed to be caught by unit testing frameworks.

The compiler may not generate any code at all for assert if it is advised to do so through the -d:release or --assertions:off command line switches.

proc copy(node: NimNode): NimNode {...}{.compileTime, raises: [], tags: [].}
An alias for copyNimTree().
proc eqIdent(a: string; b: string): bool {...}{.magic: "EqIdent", noSideEffect.}
Style insensitive comparison.
proc eqIdent(a: NimNode; b: string): bool {...}{.magic: "EqIdent", noSideEffect.}
Style insensitive comparison. a can be an identifier or a symbol.
proc eqIdent(a: string; b: NimNode): bool {...}{.magic: "EqIdent", noSideEffect.}
Style insensitive comparison. b can be an identifier or a symbol.
proc eqIdent(a: NimNode; b: NimNode): bool {...}{.magic: "EqIdent", noSideEffect.}
Style insensitive comparison. a and b can be an identifier or a symbol.
proc hasArgOfName(params: NimNode; name: string): bool {...}{.compiletime,
    raises: [Exception], tags: [RootEffect].}
Search nnkFormalParams for an argument.
proc addIdentIfAbsent(dest: NimNode; ident: string) {...}{.compiletime,
    raises: [Exception], tags: [RootEffect].}
Add ident to dest if it is not present. This is intended for use with pragmas.
proc boolVal(n: NimNode): bool {...}{.compileTime, noSideEffect, raises: [], tags: [].}
unequals operator. This is a shorthand for not (x == y).
proc getProjectPath(): string {...}{.raises: [], tags: [].}
Returns the path to the currently compiling project

Iterators

iterator items(n: NimNode): NimNode {...}{.inline, raises: [], tags: [].}
Iterates over the children of the NimNode n.
iterator pairs(n: NimNode): (int, NimNode) {...}{.inline, raises: [], tags: [].}
Iterates over the children of the NimNode n and its indices.
iterator children(n: NimNode): NimNode {...}{.inline, raises: [], tags: [].}
Iterates over the children of the NimNode n.

Macros

macro dumpTree(s: untyped): untyped

Accepts a block of nim code and prints the parsed abstract syntax tree using the treeRepr function. Printing is done at compile time.

You can use this as a tool to explore the Nim's abstract syntax tree and to discover what kind of nodes must be created to represent a certain expression/statement.

macro dumpLisp(s: untyped): untyped

Accepts a block of nim code and prints the parsed abstract syntax tree using the lispRepr function. Printing is done at compile time.

See dumpTree.

macro dumpAstGen(s: untyped): untyped

Accepts a block of nim code and prints the parsed abstract syntax tree using the astGenRepr function. Printing is done at compile time.

You can use this as a tool to write macros quicker by writing example outputs and then copying the snippets into the macro for modification.

See dumpTree.

macro dumpTreeImm(s: untyped): untyped {...}{.deprecated.}
Deprecated. Use dumpTree instead.
macro dumpLispImm(s: untyped): untyped {...}{.deprecated.}
Deprecated. Use dumpLisp instead.
macro expandMacros(body: typed): untyped

Expands one level of macro - useful for debugging. Can be used to inspect what happens when a macro call is expanded, without altering its result.

For instance,

import future, macros

let
  x = 10
  y = 20
expandMacros:
  dump(x + y)

will actually dump x + y, but at the same time will print at compile time the expansion of the dump macro, which in this case is debugEcho ["x + y", " = ", x + y].

macro hasCustomPragma(n: typed; cp: typed{nkSym}): untyped

Expands to true if expression n which is expected to be nnkDotExpr (if checking a field), a proc or a type has custom pragma cp.

See also getCustomPragmaVal.

template myAttr() {.pragma.}
type
  MyObj = object
    myField {.myAttr.}: int

proc myProc() {.myAttr.} = discard

var o: MyObj
assert(o.myField.hasCustomPragma(myAttr))
assert(myProc.hasCustomPragma(myAttr))
macro getCustomPragmaVal(n: typed; cp: typed{nkSym}): untyped

Expands to value of custom pragma cp of expression n which is expected to be nnkDotExpr, a proc or a type.

See also hasCustomPragma

template serializationKey(key: string) {.pragma.}
type
  MyObj {.serializationKey: "mo".} = object
    myField {.serializationKey: "mf".}: int
var o: MyObj
assert(o.myField.getCustomPragmaVal(serializationKey) == "mf")
assert(o.getCustomPragmaVal(serializationKey) == "mo")
assert(MyObj.getCustomPragmaVal(serializationKey) == "mo")
macro unpackVarargs(callee: untyped; args: varargs[untyped]): untyped

Templates

template findChild(n: NimNode; cond: untyped): NimNode {...}{.dirty.}
Find the first child node matching condition (or nil).
var res = findChild(n, it.kind == nnkPostfix and
                       it.basename.ident == toNimIdent"foo")
template emit(e: static[string]): untyped {...}{.deprecated.}
accepts a single string argument and treats it as nim code that should be inserted verbatim in the program Example:
emit("echo " & '"' & "hello world".toUpper & '"')

Deprecated since version 0.15 since it's so rarely useful.

© 2006–2018 Andreas Rumpf
Licensed under the MIT License.
https://nim-lang.org/docs/macros.html