Source Edit

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

See also:

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:

  1. type
  2. NimNodeKind = enum ## kind of a node; only explanatory
  3. nnkNone, ## invalid node kind
  4. nnkEmpty, ## empty node
  5. nnkIdent, ## node contains an identifier
  6. nnkIntLit, ## node contains an int literal (example: 10)
  7. nnkStrLit, ## node contains a string literal (example: "abc")
  8. nnkNilLit, ## node contains a nil literal (example: nil)
  9. nnkCaseStmt, ## node represents a case statement
  10. ... ## many more
  11. NimNode = ref NimNodeObj
  12. NimNodeObj = object
  13. case kind: NimNodeKind ## the node's kind
  14. of nnkNone, nnkEmpty, nnkNilLit:
  15. discard ## node contains no additional fields
  16. of nnkCharLit..nnkUInt64Lit:
  17. intVal: BiggestInt ## the int literal
  18. of nnkFloatLit..nnkFloat64Lit:
  19. floatVal: BiggestFloat ## the float literal
  20. of nnkStrLit..nnkTripleStrLit, nnkCommentStmt, nnkIdent, nnkSym:
  21. strVal: string ## the string literal
  22. else:
  23. 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 expressionCorresponding AST
42nnkIntLit(intVal = 42)
42’i8nnkInt8Lit(intVal = 42)
42’i16nnkInt16Lit(intVal = 42)
42’i32nnkInt32Lit(intVal = 42)
42’i64nnkInt64Lit(intVal = 42)
42’u8nnkUInt8Lit(intVal = 42)
42’u16nnkUInt16Lit(intVal = 42)
42’u32nnkUInt32Lit(intVal = 42)
42’u64nnkUInt64Lit(intVal = 42)
42.0nnkFloat64Lit(floatVal = 42.0)
42.0’f32nnkFloat32Lit(floatVal = 42.0)
42.0’f64nnkFloat64Lit(floatVal = 42.0)
“abc”nnkStrLit(strVal = “abc”)
r”abc”nnkRStrLit(strVal = “abc”)
“””abc”””nnkTripleStrLit(strVal = “abc”)
‘ ‘nnkCharLit(intVal = 32)
nilnnkNilLit()
myIdentifiernnkIdent(strVal = “myIdentifier”)
myIdentifierafter 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:

  1. echo "abc", "xyz"

AST:

  1. nnkCommand(
  2. nnkIdent("echo"),
  3. nnkStrLit("abc"),
  4. nnkStrLit("xyz")
  5. )

Call with ()

Concrete syntax:

  1. echo("abc", "xyz")

AST:

  1. nnkCall(
  2. nnkIdent("echo"),
  3. nnkStrLit("abc"),
  4. nnkStrLit("xyz")
  5. )

Infix operator call

Concrete syntax:

  1. "abc" & "xyz"

AST:

  1. nnkInfix(
  2. nnkIdent("&"),
  3. nnkStrLit("abc"),
  4. nnkStrLit("xyz")
  5. )

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

Concrete syntax:

  1. 5 + 3 * 4

AST:

  1. nnkInfix(
  2. nnkIdent("+"),
  3. nnkIntLit(5),
  4. nnkInfix(
  5. nnkIdent("*"),
  6. nnkIntLit(3),
  7. nnkIntLit(4)
  8. )
  9. )

As a side note, if you choose to use infix operators in a prefix form, the AST behaves as a parenthetical function call with nnkAccQuoted, as follows:

Concrete syntax:

  1. `+`(3, 4)

AST:

  1. nnkCall(
  2. nnkAccQuoted(
  3. nnkIdent("+")
  4. ),
  5. nnkIntLit(3),
  6. nnkIntLit(4)
  7. )

Prefix operator call

Concrete syntax:

  1. ? "xyz"

AST:

  1. nnkPrefix(
  2. nnkIdent("?"),
  3. nnkStrLit("abc")
  4. )

Postfix operator call

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

Concrete syntax:

  1. identifier*

AST:

  1. nnkPostfix(
  2. nnkIdent("*"),
  3. nnkIdent("identifier")
  4. )

Call with named arguments

Concrete syntax:

  1. writeLine(file=stdout, "hallo")

AST:

  1. nnkCall(
  2. nnkIdent("writeLine"),
  3. nnkExprEqExpr(
  4. nnkIdent("file"),
  5. nnkIdent("stdout")
  6. ),
  7. nnkStrLit("hallo")
  8. )

Call with raw string literal

This is used, for example, in the bindSym examples here and with re”some regexp” in the regular expression module.

Concrete syntax:

  1. echo"abc"

AST:

  1. nnkCallStrLit(
  2. nnkIdent("echo"),
  3. nnkRStrLit("hello")
  4. )

Dereference operator []

Concrete syntax:

  1. x[]

AST:

  1. nnkDerefExpr(nnkIdent("x"))

Addr operator

Concrete syntax:

  1. addr(x)

AST:

  1. nnkAddr(nnkIdent("x"))

Cast operator

Concrete syntax:

  1. cast[T](x)

AST:

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

Object access operator .

Concrete syntax:

  1. x.y

AST:

  1. 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:

  1. x[y]

AST:

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

Parentheses

Parentheses for affecting operator precedence use the nnkPar node.

Concrete syntax:

  1. (a + b) * c

AST:

  1. nnkInfix(nnkIdent("*"),
  2. nnkPar(
  3. nnkInfix(nnkIdent("+"), nnkIdent("a"), nnkIdent("b"))),
  4. nnkIdent("c"))

Tuple Constructors

Nodes for tuple construction are built with the nnkTupleConstr node.

Concrete syntax:

  1. (1, 2, 3)
  2. (a: 1, b: 2, c: 3)
  3. ()

AST:

  1. nnkTupleConstr(nnkIntLit(1), nnkIntLit(2), nnkIntLit(3))
  2. nnkTupleConstr(
  3. nnkExprColonExpr(nnkIdent("a"), nnkIntLit(1)),
  4. nnkExprColonExpr(nnkIdent("b"), nnkIntLit(2)),
  5. nnkExprColonExpr(nnkIdent("c"), nnkIntLit(3)))
  6. nnkTupleConstr()

Since the one tuple would be syntactically identical to parentheses with an expression in them, the parser expects a trailing comma for them. For tuple constructors with field names, this is not necessary.

  1. (1,)
  2. (a: 1)

AST:

  1. nnkTupleConstr(nnkIntLit(1))
  2. nnkTupleConstr(
  3. nnkExprColonExpr(nnkIdent("a"), nnkIntLit(1)))

Curly braces

Curly braces are used as the set constructor.

Concrete syntax:

  1. {1, 2, 3}

AST:

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

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

Concrete syntax:

  1. {a: 3, b: 5}

AST:

  1. nnkTableConstr(
  2. nnkExprColonExpr(nnkIdent("a"), nnkIntLit(3)),
  3. nnkExprColonExpr(nnkIdent("b"), nnkIntLit(5))
  4. )

Brackets

Brackets are used as the array constructor.

Concrete syntax:

  1. [1, 2, 3]

AST:

  1. 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. 1..3

AST:

  1. nnkInfix(
  2. nnkIdent(".."),
  3. nnkIntLit(1),
  4. nnkIntLit(3)
  5. )

Example code:

  1. macro genRepeatEcho() =
  2. result = newNimNode(nnkStmtList)
  3. var forStmt = newNimNode(nnkForStmt) # generate a for statement
  4. forStmt.add(ident("i")) # use the variable `i` for iteration
  5. var rangeDef = newNimNode(nnkInfix).add(
  6. ident("..")).add(
  7. newIntLitNode(3),newIntLitNode(5)) # iterate over the range 3..5
  8. forStmt.add(rangeDef)
  9. forStmt.add(newCall(ident("echo"), newIntLitNode(3))) # meat of the loop
  10. result.add(forStmt)
  11. genRepeatEcho() # gives:
  12. # 3
  13. # 3
  14. # 3

If expression

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

Concrete syntax:

  1. if cond1: expr1 elif cond2: expr2 else: expr3

AST:

  1. nnkIfExpr(
  2. nnkElifExpr(cond1, expr1),
  3. nnkElifExpr(cond2, expr2),
  4. nnkElseExpr(expr3)
  5. )

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:

  1. ## This is a comment
  2. ## This is part of the first comment
  3. stmt1
  4. ## Yet another

AST:

  1. nnkCommentStmt() # only appears once for the first two lines!
  2. stmt1
  3. nnkCommentStmt() # another nnkCommentStmt because there is another comment
  4. # (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:

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

AST:

  1. nnkPragma(
  2. nnkExprColonExpr(
  3. nnkIdent("emit"),
  4. nnkStrLit("#include <stdio.h>") # the "argument"
  5. )
  6. )

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

Concrete syntax:

  1. {.pragma: cdeclRename, cdecl.}

AST:

  1. nnkPragma(
  2. nnkExprColonExpr(
  3. nnkIdent("pragma"), # this is always first when declaring a new pragma
  4. nnkIdent("cdeclRename") # the name of the pragma
  5. ),
  6. nnkIdent("cdecl")
  7. )

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:

  1. if cond1:
  2. stmt1
  3. elif cond2:
  4. stmt2
  5. elif cond3:
  6. stmt3
  7. else:
  8. stmt4

AST:

  1. nnkIfStmt(
  2. nnkElifBranch(cond1, stmt1),
  3. nnkElifBranch(cond2, stmt2),
  4. nnkElifBranch(cond3, stmt3),
  5. nnkElse(stmt4)
  6. )

When statement

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

Assignment

Concrete syntax:

  1. x = 42

AST:

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

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

Statement list

Concrete syntax:

  1. stmt1
  2. stmt2
  3. stmt3

AST:

  1. nnkStmtList(stmt1, stmt2, stmt3)

Case statement

Concrete syntax:

  1. case expr1
  2. of expr2, expr3..expr4:
  3. stmt1
  4. of expr5:
  5. stmt2
  6. elif cond1:
  7. stmt3
  8. else:
  9. stmt4

AST:

  1. nnkCaseStmt(
  2. expr1,
  3. nnkOfBranch(expr2, nnkRange(expr3, expr4), stmt1),
  4. nnkOfBranch(expr5, stmt2),
  5. nnkElifBranch(cond1, stmt3),
  6. nnkElse(stmt4)
  7. )

The nnkElifBranch and nnkElse parts may be missing.

While statement

Concrete syntax:

  1. while expr1:
  2. stmt1

AST:

  1. nnkWhileStmt(expr1, stmt1)

For statement

Concrete syntax:

  1. for ident1, ident2 in expr1:
  2. stmt1

AST:

  1. nnkForStmt(ident1, ident2, expr1, stmt1)

Try statement

Concrete syntax:

  1. try:
  2. stmt1
  3. except e1, e2:
  4. stmt2
  5. except e3:
  6. stmt3
  7. except:
  8. stmt4
  9. finally:
  10. stmt5

AST:

  1. nnkTryStmt(
  2. stmt1,
  3. nnkExceptBranch(e1, e2, stmt2),
  4. nnkExceptBranch(e3, stmt3),
  5. nnkExceptBranch(stmt4),
  6. nnkFinally(stmt5)
  7. )

Return statement

Concrete syntax:

  1. return expr1

AST:

  1. nnkReturnStmt(expr1)

Yield statement

Like return, but with nnkYieldStmt kind.

  1. nnkYieldStmt(expr1)

Discard statement

Like return, but with nnkDiscardStmt kind.

  1. nnkDiscardStmt(expr1)

Continue statement

Concrete syntax:

  1. continue

AST:

  1. nnkContinueStmt()

Break statement

Concrete syntax:

  1. break otherLocation

AST:

  1. nnkBreakStmt(nnkIdent("otherLocation"))

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

Block statement

Concrete syntax:

  1. block name:

AST:

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

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

Asm statement

Concrete syntax:

  1. asm """
  2. some asm
  3. """

AST:

  1. nnkAsmStmt(
  2. nnkEmpty(), # for pragmas
  3. nnkTripleStrLit("some asm"),
  4. )

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:

  1. import math

AST:

  1. nnkImportStmt(nnkIdent("math"))

With except, we get nnkImportExceptStmt.

Concrete syntax:

  1. import math except pow

AST:

  1. 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:

  1. import strutils as su

AST:

  1. nnkImportStmt(
  2. nnkInfix(
  3. nnkIdent("as"),
  4. nnkIdent("strutils"),
  5. nnkIdent("su")
  6. )
  7. )

From statement

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

Concrete syntax:

  1. from math import pow

AST:

  1. 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:

  1. export unsigned

AST:

  1. nnkExportStmt(nnkIdent("unsigned"))

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

Concrete syntax:

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

AST:

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

Include statement

Like a plain import statement but with nnkIncludeStmt.

Concrete syntax:

  1. include blocks

AST:

  1. nnkIncludeStmt(nnkIdent("blocks"))

Var section

Concrete syntax:

  1. var a = 3

AST:

  1. nnkVarSection(
  2. nnkIdentDefs(
  3. nnkIdent("a"),
  4. nnkEmpty(), # or nnkIdent(...) if the variable declares the type
  5. nnkIntLit(3),
  6. )
  7. )

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 for details.

Let section

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

Concrete syntax:

  1. let a = 3

AST:

  1. nnkLetSection(
  2. nnkIdentDefs(
  3. nnkIdent("a"),
  4. nnkEmpty(), # or nnkIdent(...) for the type
  5. nnkIntLit(3),
  6. )
  7. )

Const section

Concrete syntax:

  1. const a = 3

AST:

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

Type section

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

Concrete syntax:

  1. type A = int

AST:

  1. nnkTypeSection(
  2. nnkTypeDef(
  3. nnkIdent("A"),
  4. nnkEmpty(),
  5. nnkIdent("int")
  6. )
  7. )

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

Concrete syntax:

  1. type MyInt = distinct int

AST:

  1. # ...
  2. nnkTypeDef(
  3. nnkIdent("MyInt"),
  4. nnkEmpty(),
  5. nnkDistinctTy(
  6. nnkIdent("int")
  7. )
  8. )

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

Concrete syntax:

  1. type A[T] = expr1

AST:

  1. nnkTypeSection(
  2. nnkTypeDef(
  3. nnkIdent("A"),
  4. nnkGenericParams(
  5. nnkIdentDefs(
  6. nnkIdent("T"),
  7. nnkEmpty(), # if the type is declared with options, like
  8. # ``[T: SomeInteger]``, they are given here
  9. nnkEmpty(),
  10. )
  11. )
  12. expr1,
  13. )
  14. )

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

Concrete syntax:

  1. type IO = object of RootObj

AST:

  1. # ...
  2. nnkTypeDef(
  3. nnkIdent("IO"),
  4. nnkEmpty(),
  5. nnkObjectTy(
  6. nnkEmpty(), # no pragmas here
  7. nnkOfInherit(
  8. nnkIdent("RootObj") # inherits from RootObj
  9. ),
  10. nnkEmpty()
  11. )
  12. )

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:

  1. type Obj[T] {.inheritable.} = object
  2. name: string
  3. case isFat: bool
  4. of true:
  5. m: array[100_000, T]
  6. of false:
  7. m: array[10, T]

AST:

  1. # ...
  2. nnkPragmaExpr(
  3. nnkIdent("Obj"),
  4. nnkPragma(nnkIdent("inheritable"))
  5. ),
  6. nnkGenericParams(
  7. nnkIdentDefs(
  8. nnkIdent("T"),
  9. nnkEmpty(),
  10. nnkEmpty())
  11. ),
  12. nnkObjectTy(
  13. nnkEmpty(),
  14. nnkEmpty(),
  15. nnkRecList( # list of object parameters
  16. nnkIdentDefs(
  17. nnkIdent("name"),
  18. nnkIdent("string"),
  19. nnkEmpty()
  20. ),
  21. nnkRecCase( # case statement within object (not nnkCaseStmt)
  22. nnkIdentDefs(
  23. nnkIdent("isFat"),
  24. nnkIdent("bool"),
  25. nnkEmpty()
  26. ),
  27. nnkOfBranch(
  28. nnkIdent("true"),
  29. nnkRecList( # again, a list of object parameters
  30. nnkIdentDefs(
  31. nnkIdent("m"),
  32. nnkBracketExpr(
  33. nnkIdent("array"),
  34. nnkIntLit(100000),
  35. nnkIdent("T")
  36. ),
  37. nnkEmpty()
  38. )
  39. ),
  40. nnkOfBranch(
  41. nnkIdent("false"),
  42. nnkRecList(
  43. nnkIdentDefs(
  44. nnkIdent("m"),
  45. nnkBracketExpr(
  46. nnkIdent("array"),
  47. nnkIntLit(10),
  48. nnkIdent("T")
  49. ),
  50. nnkEmpty()
  51. )
  52. )
  53. )
  54. )
  55. )
  56. )

Using an enum is similar to using an object.

Concrete syntax:

  1. type X = enum
  2. First

AST:

  1. # ...
  2. nnkEnumTy(
  3. nnkEmpty(),
  4. nnkIdent("First") # you need at least one nnkIdent or the compiler complains
  5. )

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

Concrete syntax:

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

AST:

  1. # ...
  2. nnkTypeClassTy( # note this isn't nnkConceptTy!
  3. nnkArgList(
  4. # ... idents for x, y, z
  5. )
  6. # ...
  7. )

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

Concrete syntax:

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

AST:

  1. # ... within nnkGenericParams
  2. nnkIdentDefs(
  3. nnkIdent("T"),
  4. nnkStaticTy(
  5. nnkIdent("int")
  6. ),
  7. nnkEmpty()
  8. )
  9. # ...

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

Nim typeCorresponding AST
staticnnkStaticTy
tuplennkTupleTy
varnnkVarTy
ptrnnkPtrTy
refnnkRefTy
distinctnnkDistinctTy
enumnnkEnumTy
conceptnnkTypeClassTy
arraynnkBracketExpr(nnkIdent(“array”),…
procnnkProcTy
iteratornnkIteratorTy
objectnnkObjectTy

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:

  1. type MyProc[T] = proc(x: T) {.nimcall.}

AST:

  1. # ...
  2. nnkTypeDef(
  3. nnkIdent("MyProc"),
  4. nnkGenericParams( # here, not with the proc
  5. # ...
  6. )
  7. nnkProcTy( # behaves like a procedure declaration from here on
  8. nnkFormalParams(
  9. # ...
  10. ),
  11. nnkPragma(nnkIdent("nimcall"))
  12. )
  13. )

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

Type class versions of these nodes generally share the same node kind but without any child nodes. The tuple type class is represented by nnkTupleClassTy, while a proc or iterator type class with pragmas has an nnkEmpty node in place of the nnkFormalParams node of a concrete proc or iterator type node.

  1. type TypeClass = proc {.nimcall.} | ref | tuple

AST:

  1. nnkTypeDef(
  2. nnkIdent("TypeClass"),
  3. nnkEmpty(),
  4. nnkInfix(
  5. nnkIdent("|"),
  6. nnkProcTy(
  7. nnkEmpty(),
  8. nnkPragma(nnkIdent("nimcall"))
  9. ),
  10. nnkInfix(
  11. nnkIdent("|"),
  12. nnkRefTy(),
  13. nnkTupleClassTy()
  14. )
  15. )
  16. )

Mixin statement

Concrete syntax:

  1. mixin x

AST:

  1. nnkMixinStmt(nnkIdent("x"))

Bind statement

Concrete syntax:

  1. bind x

AST:

  1. 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:

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

AST:

  1. nnkProcDef(
  2. nnkPostfix(nnkIdent("*"), nnkIdent("hello")), # the exported proc name
  3. nnkEmpty(), # patterns for term rewriting in templates and macros (not procs)
  4. nnkGenericParams( # generic type parameters, like with type declaration
  5. nnkIdentDefs(
  6. nnkIdent("T"),
  7. nnkIdent("SomeInteger"),
  8. nnkEmpty()
  9. )
  10. ),
  11. nnkFormalParams(
  12. nnkIdent("int"), # the first FormalParam is the return type. nnkEmpty() if there is none
  13. nnkIdentDefs(
  14. nnkIdent("x"),
  15. nnkIdent("int"), # type type (required for procs, not for templates)
  16. nnkIntLit(3) # a default value
  17. ),
  18. nnkIdentDefs(
  19. nnkIdent("y"),
  20. nnkIdent("float32"),
  21. nnkEmpty()
  22. )
  23. ),
  24. nnkPragma(nnkIdent("inline")),
  25. nnkEmpty(), # reserved slot for future use
  26. nnkStmtList(nnkDiscardStmt(nnkEmpty())) # the meat of the proc
  27. )

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:

  1. proc(a, b: int)

AST:

  1. # ...AST as above...
  2. nnkFormalParams(
  3. nnkEmpty(), # no return here
  4. nnkIdentDefs(
  5. nnkIdent("a"), # the first parameter
  6. nnkIdent("b"), # directly to the second parameter
  7. nnkIdent("int"), # their shared type identifier
  8. nnkEmpty(), # default value would go here
  9. )
  10. ),
  11. # ...

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

Concrete syntax:

  1. proc hello(): var int

AST:

  1. # ...
  2. nnkFormalParams(
  3. nnkVarTy(
  4. nnkIdent("int")
  5. )
  6. )

Iterator declaration

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

Concrete syntax:

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

AST:

  1. nnkIteratorDef(
  2. nnkIdent("nonsense"),
  3. nnkEmpty(),
  4. ...
  5. )

Converter declaration

A converter is similar to a proc.

Concrete syntax:

  1. converter toBool(x: float): bool

AST:

  1. nnkConverterDef(
  2. nnkIdent("toBool"),
  3. # ...
  4. )

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. Notice the nnkEmpty() as the second argument to nnkProcDef and nnkIteratorDef above? That’s where the term-rewriting macros go.

Concrete syntax:

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

AST:

  1. nnkTemplateDef(
  2. nnkIdent("optOpt"),
  3. nnkStmtList( # instead of nnkEmpty()
  4. expr1
  5. ),
  6. # follows like a proc or iterator
  7. )

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.

Hidden Standard Conversion

  1. var f: float = 1

The type of “f” is float but the type of “1” is actually int. Inserting int into a float is a type error. Nim inserts the nnkHiddenStdConv node around the nnkIntLit node so that the new node has the correct type of float. This works for any auto converted nodes and makes the conversion explicit.

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.

Imports

since

Types

  1. BindSymRule = enum
  2. brClosed, ## only the symbols in current scope are bound
  3. brOpen, ## open for overloaded symbols, but may be a single
  4. ## symbol if not ambiguous (the rules match that of
  5. ## binding in generics)
  6. brForceOpen ## same as brOpen, but it will always be open even
  7. ## if not ambiguous (this cannot be achieved with
  8. ## any other means in the language currently)

Specifies how bindSym behaves. The difference between open and closed symbols can be found in manual.html#symbol-lookup-in-generics-open-and-closed-symbols Source Edit

  1. LineInfo = object
  2. filename*: string
  3. line*, column*: int

Source Edit

  1. NimIdent {....deprecated.} = object of RootObj

Deprecated

Represents a Nim identifier in the AST. Note: This is only rarely useful, for identifier construction from a string use ident”abc”. Source Edit

  1. NimNodeKind = enum
  2. nnkNone, nnkEmpty, nnkIdent, nnkSym, nnkType, nnkCharLit, nnkIntLit,
  3. nnkInt8Lit, nnkInt16Lit, nnkInt32Lit, nnkInt64Lit, nnkUIntLit, nnkUInt8Lit,
  4. nnkUInt16Lit, nnkUInt32Lit, nnkUInt64Lit, nnkFloatLit, nnkFloat32Lit,
  5. nnkFloat64Lit, nnkFloat128Lit, nnkStrLit, nnkRStrLit, nnkTripleStrLit,
  6. nnkNilLit, nnkComesFrom, nnkDotCall, nnkCommand, nnkCall, nnkCallStrLit,
  7. nnkInfix, nnkPrefix, nnkPostfix, nnkHiddenCallConv, nnkExprEqExpr,
  8. nnkExprColonExpr, nnkIdentDefs, nnkVarTuple, nnkPar, nnkObjConstr, nnkCurly,
  9. nnkCurlyExpr, nnkBracket, nnkBracketExpr, nnkPragmaExpr, nnkRange, nnkDotExpr,
  10. nnkCheckedFieldExpr, nnkDerefExpr, nnkIfExpr, nnkElifExpr, nnkElseExpr,
  11. nnkLambda, nnkDo, nnkAccQuoted, nnkTableConstr, nnkBind, nnkClosedSymChoice,
  12. nnkOpenSymChoice, nnkHiddenStdConv, nnkHiddenSubConv, nnkConv, nnkCast,
  13. nnkStaticExpr, nnkAddr, nnkHiddenAddr, nnkHiddenDeref, nnkObjDownConv,
  14. nnkObjUpConv, nnkChckRangeF, nnkChckRange64, nnkChckRange, nnkStringToCString,
  15. nnkCStringToString, nnkAsgn, nnkFastAsgn, nnkGenericParams, nnkFormalParams,
  16. nnkOfInherit, nnkImportAs, nnkProcDef, nnkMethodDef, nnkConverterDef,
  17. nnkMacroDef, nnkTemplateDef, nnkIteratorDef, nnkOfBranch, nnkElifBranch,
  18. nnkExceptBranch, nnkElse, nnkAsmStmt, nnkPragma, nnkPragmaBlock, nnkIfStmt,
  19. nnkWhenStmt, nnkForStmt, nnkParForStmt, nnkWhileStmt, nnkCaseStmt,
  20. nnkTypeSection, nnkVarSection, nnkLetSection, nnkConstSection, nnkConstDef,
  21. nnkTypeDef, nnkYieldStmt, nnkDefer, nnkTryStmt, nnkFinally, nnkRaiseStmt,
  22. nnkReturnStmt, nnkBreakStmt, nnkContinueStmt, nnkBlockStmt, nnkStaticStmt,
  23. nnkDiscardStmt, nnkStmtList, nnkImportStmt, nnkImportExceptStmt,
  24. nnkExportStmt, nnkExportExceptStmt, nnkFromStmt, nnkIncludeStmt, nnkBindStmt,
  25. nnkMixinStmt, nnkUsingStmt, nnkCommentStmt, nnkStmtListExpr, nnkBlockExpr,
  26. nnkStmtListType, nnkBlockType, nnkWith, nnkWithout, nnkTypeOfExpr,
  27. nnkObjectTy, nnkTupleTy, nnkTupleClassTy, nnkTypeClassTy, nnkStaticTy,
  28. nnkRecList, nnkRecCase, nnkRecWhen, nnkRefTy, nnkPtrTy, nnkVarTy, nnkConstTy,
  29. nnkOutTy, nnkDistinctTy, nnkProcTy, nnkIteratorTy, nnkSinkAsgn, nnkEnumTy,
  30. nnkEnumFieldDef, nnkArgList, nnkPattern, nnkHiddenTryStmt, nnkClosure,
  31. nnkGotoState, nnkState, nnkBreakState, nnkFuncDef, nnkTupleConstr, nnkError ## erroneous AST node

Source Edit

  1. NimNodeKinds = set[NimNodeKind]

Source Edit

  1. NimSym {....deprecated.} = ref NimSymObj

Deprecated

Represents a Nim symbol in the compiler; a symbol is a looked-up ident. Source Edit

  1. NimSymKind = enum
  2. nskUnknown, nskConditional, nskDynLib, nskParam, nskGenericParam, nskTemp,
  3. nskModule, nskType, nskVar, nskLet, nskConst, nskResult, nskProc, nskFunc,
  4. nskMethod, nskIterator, nskConverter, nskMacro, nskTemplate, nskField,
  5. nskEnumField, nskForVar, nskLabel, nskStub

Source Edit

  1. NimTypeKind = enum
  2. ntyNone, ntyBool, ntyChar, ntyEmpty, ntyAlias, ntyNil, ntyExpr, ntyStmt,
  3. ntyTypeDesc, ntyGenericInvocation, ntyGenericBody, ntyGenericInst,
  4. ntyGenericParam, ntyDistinct, ntyEnum, ntyOrdinal, ntyArray, ntyObject,
  5. ntyTuple, ntySet, ntyRange, ntyPtr, ntyRef, ntyVar, ntySequence, ntyProc,
  6. ntyPointer, ntyOpenArray, ntyString, ntyCString, ntyForward, ntyInt, ntyInt8,
  7. ntyInt16, ntyInt32, ntyInt64, ntyFloat, ntyFloat32, ntyFloat64, ntyFloat128,
  8. ntyUInt, ntyUInt8, ntyUInt16, ntyUInt32, ntyUInt64, ntyUnused0, ntyUnused1,
  9. ntyUnused2, ntyVarargs, ntyUncheckedArray, ntyError, ntyBuiltinTypeClass,
  10. ntyUserTypeClass, ntyUserTypeClassInst, ntyCompositeTypeClass, ntyInferred,
  11. ntyAnd, ntyOr, ntyNot, ntyAnything, ntyStatic, ntyFromExpr, ntyOptDeprecated,
  12. ntyVoid

Source Edit

  1. TNimSymKinds {....deprecated.} = set[NimSymKind]

Deprecated

Source Edit

  1. TNimTypeKinds {....deprecated.} = set[NimTypeKind]

Deprecated

Source Edit

Consts

  1. AtomicNodes = {nnkNone..nnkNilLit}

Source Edit

  1. CallNodes = {nnkCall, nnkInfix, nnkPrefix, nnkPostfix, nnkCommand,
  2. nnkCallStrLit, nnkHiddenCallConv}

Source Edit

  1. nnkCallKinds = {nnkCall, nnkInfix, nnkPrefix, nnkPostfix, nnkCommand,
  2. nnkCallStrLit, nnkHiddenCallConv}

Source Edit

  1. nnkLiterals = {nnkCharLit..nnkNilLit}

Source Edit

  1. nnkMutableTy {....deprecated.} = nnkOutTy

Source Edit

  1. nnkSharedTy {....deprecated.} = nnkSinkAsgn

Source Edit

  1. RoutineNodes = {nnkProcDef, nnkFuncDef, nnkMethodDef, nnkDo, nnkLambda,
  2. nnkIteratorDef, nnkTemplateDef, nnkConverterDef, nnkMacroDef}

Source Edit

Procs

  1. proc `$`(arg: LineInfo): string {....raises: [], tags: [], forbids: [].}

Return a string representation in the form filepath(line, column). Source Edit

  1. proc `$`(i: NimIdent): string {.magic: "NStrVal", noSideEffect, ...deprecated: "Deprecated since version 0.18.1; Use \'strVal\' instead.",
  2. raises: [], tags: [], forbids: [].}

Deprecated: Deprecated since version 0.18.1; Use ‘strVal’ instead.

Converts a Nim identifier to a string. Source Edit

  1. proc `$`(node: NimNode): string {....raises: [], tags: [], forbids: [].}

Get the string of an identifier node. Source Edit

  1. proc `$`(s: NimSym): string {.magic: "NStrVal", noSideEffect, ...deprecated: "Deprecated since version 0.18.1; Use \'strVal\' instead.",
  2. raises: [], tags: [], forbids: [].}

Deprecated: Deprecated since version 0.18.1; Use ‘strVal’ instead.

Converts a Nim symbol to a string. Source Edit

  1. proc `==`(a, b: NimIdent): bool {.magic: "EqIdent", noSideEffect, ...deprecated: "Deprecated since version 0.18.1; Use \'==\' on \'NimNode\' instead.",
  2. raises: [], tags: [], forbids: [].}

Deprecated: Deprecated since version 0.18.1; Use ‘==’ on ‘NimNode’ instead.

Compares two Nim identifiers. Source Edit

  1. proc `==`(a, b: NimNode): bool {.magic: "EqNimrodNode", noSideEffect,
  2. ...raises: [], tags: [], forbids: [].}

Compare two Nim nodes. Return true if nodes are structurally equivalent. This means two independently created nodes can be equal. Source Edit

  1. proc `==`(a, b: NimSym): bool {.magic: "EqNimrodNode", noSideEffect, ...deprecated: "Deprecated since version 0.18.1; Use \'==(NimNode, NimNode)\' instead.",
  2. raises: [], tags: [], forbids: [].}

Deprecated: Deprecated since version 0.18.1; Use ‘==(NimNode, NimNode)’ instead.

Compares two Nim symbols. Source Edit

  1. proc `[]`(n: NimNode; i: BackwardsIndex): NimNode {....raises: [], tags: [],
  2. forbids: [].}

Get n’s i’th child. Source Edit

  1. proc `[]`(n: NimNode; i: int): NimNode {.magic: "NChild", noSideEffect,
  2. ...raises: [], tags: [], forbids: [].}

Get n’s i’th child. Source Edit

  1. proc `[]`[T, U: Ordinal](n: NimNode; x: HSlice[T, U]): seq[NimNode]

Slice operation for NimNode. Returns a seq of child of n who inclusive range [n[x.a], n[x.b]]. Source Edit

  1. proc `[]=`(n: NimNode; i: BackwardsIndex; child: NimNode) {....raises: [],
  2. tags: [], forbids: [].}

Set n’s i’th child to child. Source Edit

  1. proc `[]=`(n: NimNode; i: int; child: NimNode) {.magic: "NSetChild",
  2. noSideEffect, ...raises: [], tags: [], forbids: [].}

Set n’s i’th child to child. Source Edit

  1. proc add(father, child: NimNode): NimNode {.magic: "NAdd", discardable,
  2. noSideEffect, ...raises: [], tags: [], forbids: [].}

Adds the child to the father node. Returns the father node so that calls can be nested. Source Edit

  1. proc add(father: NimNode; children: varargs[NimNode]): NimNode {.
  2. magic: "NAddMultiple", discardable, noSideEffect, ...raises: [], tags: [],
  3. forbids: [].}

Adds each child of children to the father node. Returns the father node so that calls can be nested. Source Edit

  1. proc addIdentIfAbsent(dest: NimNode; ident: string) {....raises: [], tags: [],
  2. forbids: [].}

Add ident to dest if it is not present. This is intended for use with pragmas. Source Edit

  1. proc addPragma(someProc, pragma: NimNode) {....raises: [], tags: [], forbids: [].}

Adds pragma to routine definition. Source Edit

  1. proc astGenRepr(n: NimNode): string {....gcsafe, raises: [], tags: [], forbids: [].}

Convert the AST n to the code required to generate that AST.

See also system: repr, treeRepr, and lispRepr.

Source Edit

  1. proc basename(a: NimNode): NimNode {....raises: [], tags: [], forbids: [].}

Pull an identifier from prefix/postfix expressions. Source Edit

  1. proc basename=(a: NimNode; val: string) {....raises: [], tags: [], forbids: [].}

Source Edit

  1. proc bindSym(ident: string | NimNode; rule: BindSymRule = brClosed): NimNode {.
  2. magic: "NBindSym", noSideEffect, ...raises: [], tags: [], forbids: [].}

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 nnkIdent kind. If rule == brClosed either an nnkClosedSymChoice tree is returned or nnkSym if the symbol is not ambiguous. If rule == brOpen either an nnkOpenSymChoice tree is returned or nnkSym if the symbol is not ambiguous. If rule == brForceOpen always an nnkOpenSymChoice tree is returned even if the symbol is not ambiguous.

See the manual for more details.

Source Edit

  1. proc body(someProc: NimNode): NimNode {....raises: [], tags: [], forbids: [].}

Source Edit

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

Source Edit

  1. proc boolVal(n: NimNode): bool {.noSideEffect, ...raises: [], tags: [], forbids: [].}

Source Edit

  1. proc callsite(): NimNode {.magic: "NCallSite", ...gcsafe, deprecated: "Deprecated since v0.18.1; use `varargs[untyped]` in the macro prototype instead",
  2. raises: [], tags: [], forbids: [].}

Deprecated: Deprecated since v0.18.1; use `varargs[untyped]` in the macro prototype instead

Returns the AST of the invocation expression that invoked this macro. Source Edit

  1. proc copy(node: NimNode): NimNode {....raises: [], tags: [], forbids: [].}

An alias for copyNimTree. Source Edit

  1. proc copyChildrenTo(src, dest: NimNode) {....raises: [], tags: [], forbids: [].}

Copy all children from src to dest. Source Edit

  1. proc copyLineInfo(arg: NimNode; info: NimNode) {.magic: "NLineInfo",
  2. noSideEffect, ...raises: [], tags: [], forbids: [].}

Copy lineinfo from info. Source Edit

  1. proc copyNimNode(n: NimNode): NimNode {.magic: "NCopyNimNode", noSideEffect,
  2. ...raises: [], tags: [], forbids: [].}

Creates a new AST node by copying the node n. Note that unlike copyNimTree, child nodes of n are not copied.

Example:

  1. macro foo(x: typed) =
  2. var s = copyNimNode(x)
  3. doAssert s.len == 0
  4. doAssert s.kind == nnkStmtList
  5. foo:
  6. let x = 12
  7. echo x

Source Edit

  1. proc copyNimTree(n: NimNode): NimNode {.magic: "NCopyNimTree", noSideEffect,
  2. ...raises: [], tags: [], forbids: [].}

Creates a new AST node by recursively copying the node n. Note that unlike copyNimNode, this copies n, the children of n, etc.

Example:

  1. macro foo(x: typed) =
  2. var s = copyNimTree(x)
  3. doAssert s.len == 2
  4. doAssert s.kind == nnkStmtList
  5. foo:
  6. let x = 12
  7. echo x

Source Edit

  1. proc del(father: NimNode; idx = 0; n = 1) {.magic: "NDel", noSideEffect,
  2. ...raises: [], tags: [], forbids: [].}

Deletes n children of father starting at index idx. Source Edit

  1. proc eqIdent(a: NimNode; b: NimNode): bool {.magic: "EqIdent", noSideEffect,
  2. ...raises: [], tags: [], forbids: [].}

Style insensitive comparison. a and b can be an identifier or a symbol. Both may be wrapped in an export marker (nnkPostfix) or quoted with backticks (nnkAccQuoted), these nodes will be unwrapped. Source Edit

  1. proc eqIdent(a: NimNode; b: string): bool {.magic: "EqIdent", noSideEffect,
  2. ...raises: [], tags: [], forbids: [].}

Style insensitive comparison. a can be an identifier or a symbol. a may be wrapped in an export marker (nnkPostfix) or quoted with backticks (nnkAccQuoted), these nodes will be unwrapped. Source Edit

  1. proc eqIdent(a: string; b: NimNode): bool {.magic: "EqIdent", noSideEffect,
  2. ...raises: [], tags: [], forbids: [].}

Style insensitive comparison. b can be an identifier or a symbol. b may be wrapped in an export marker (nnkPostfix) or quoted with backticks (nnkAccQuoted), these nodes will be unwrapped. Source Edit

  1. proc eqIdent(a: string; b: string): bool {.magic: "EqIdent", noSideEffect,
  2. ...raises: [], tags: [], forbids: [].}

Style insensitive comparison. Source Edit

  1. proc error(msg: string; n: NimNode = nil) {.magic: "NError", ...gcsafe, noreturn,
  2. ...raises: [], tags: [], forbids: [].}

Writes an error message at compile time. The optional n: NimNode parameter is used as the source for file and line number information in the compilation error message. Source Edit

  1. proc expectIdent(n: NimNode; name: string) {....raises: [], tags: [], forbids: [].}

Check that eqIdent(n,name) holds true. 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. Source Edit

  1. proc expectKind(n: NimNode; k: NimNodeKind) {....raises: [], tags: [], forbids: [].}

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. Source Edit

  1. proc expectKind(n: NimNode; k: set[NimNodeKind]) {....raises: [], tags: [],
  2. forbids: [].}

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. Source Edit

  1. proc expectLen(n: NimNode; len: int) {....raises: [], tags: [], forbids: [].}

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. Source Edit

  1. proc expectLen(n: NimNode; min, max: int) {....raises: [], tags: [], forbids: [].}

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. Source Edit

  1. proc expectMinLen(n: NimNode; min: int) {....raises: [], tags: [], forbids: [].}

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. Source Edit

  1. proc extractDocCommentsAndRunnables(n: NimNode): NimNode {....raises: [], tags: [],
  2. forbids: [].}

returns a nnkStmtList containing the top-level doc comments and runnableExamples in a, stopping at the first child that is neither. Example:

  1. import std/macros
  2. macro transf(a): untyped =
  3. result = quote do:
  4. proc fun2*() = discard
  5. let header = extractDocCommentsAndRunnables(a.body)
  6. # correct usage: rest is appended
  7. result.body = header
  8. result.body.add quote do: discard # just an example
  9. # incorrect usage: nesting inside a nnkStmtList:
  10. # result.body = quote do: (`header`; discard)
  11. proc fun*() {.transf.} =
  12. ## first comment
  13. runnableExamples: discard
  14. runnableExamples: discard
  15. ## last comment
  16. discard # first statement after doc comments + runnableExamples
  17. ## not docgen'd

Source Edit

  1. proc floatVal(n: NimNode): BiggestFloat {.magic: "NFloatVal", noSideEffect,
  2. ...raises: [], tags: [], forbids: [].}

Returns a float from any floating point literal. Source Edit

  1. proc floatVal=(n: NimNode; val: BiggestFloat) {.magic: "NSetFloatVal",
  2. noSideEffect, ...raises: [], tags: [], forbids: [].}

Source Edit

  1. proc genSym(kind: NimSymKind = nskLet; ident = ""): NimNode {.magic: "NGenSym",
  2. noSideEffect, ...raises: [], tags: [], forbids: [].}

Generates a fresh symbol that is guaranteed to be unique. The symbol needs to occur in a declaration context. Source Edit

  1. proc getAlign(arg: NimNode): int {.magic: "NSizeOf", noSideEffect, ...raises: [],
  2. tags: [], forbids: [].}

Returns the same result as system.alignof if the alignment is known by the Nim compiler. It works on NimNode for use in macro context. Returns a negative value if the Nim compiler does not know the alignment. Source Edit

  1. proc getAst(macroOrTemplate: untyped): NimNode {.magic: "ExpandToAst",
  2. noSideEffect, ...raises: [], tags: [], forbids: [].}

Obtains the AST nodes returned from a macro or template invocation. See also genasts.genAst. Example:

  1. macro FooMacro() =
  2. var ast = getAst(BarTemplate())

Source Edit

  1. proc getImpl(s: NimSym): NimNode {.magic: "GetImpl", noSideEffect, ...deprecated: "use `getImpl: NimNode -> NimNode` instead",
  2. raises: [], tags: [], forbids: [].}

Deprecated: use `getImpl: NimNode -> NimNode` instead

Source Edit

  1. proc getImpl(symbol: NimNode): NimNode {.magic: "GetImpl", noSideEffect,
  2. ...raises: [], tags: [], forbids: [].}

Returns a copy of the declaration of a symbol or nil. Source Edit

  1. proc getImplTransformed(symbol: NimNode): NimNode {.magic: "GetImplTransf",
  2. noSideEffect, ...raises: [], tags: [], forbids: [].}

For a typed proc returns the AST after transformation pass; this is useful for debugging how the compiler transforms code (e.g.: defer, for) but note that code transformations are implementation dependent and subject to change. See an example in tests/macros/tmacros_various.nim. Source Edit

  1. proc getOffset(arg: NimNode): int {.magic: "NSizeOf", noSideEffect, ...raises: [],
  2. tags: [], forbids: [].}

Returns the same result as system.offsetof if the offset is known by the Nim compiler. It expects a resolved symbol node from a field of a type. Therefore it only requires one argument instead of two. Returns a negative value if the Nim compiler does not know the offset. Source Edit

  1. proc getProjectPath(): string {....raises: [], tags: [], forbids: [].}

Returns the path to the currently compiling project.

This is not to be confused with system.currentSourcePath which returns the path of the source file containing that template call.

For example, assume a dir1/foo.nim that imports a dir2/bar.nim, have the bar.nim print out both getProjectPath and currentSourcePath outputs.

Now when foo.nim is compiled, the getProjectPath from bar.nim will return the dir1/ path, while the currentSourcePath will return the path to the bar.nim source file.

Now when bar.nim is compiled directly, the getProjectPath will now return the dir2/ path, and the currentSourcePath will still return the same path, the path to the bar.nim source file.

The path returned by this proc is set at compile time.

See also:

Source Edit

  1. proc getSize(arg: NimNode): int {.magic: "NSizeOf", noSideEffect, ...raises: [],
  2. tags: [], forbids: [].}

Returns the same result as system.sizeof if the size is known by the Nim compiler. Returns a negative value if the Nim compiler does not know the size. Source Edit

  1. proc getType(n: NimNode): NimNode {.magic: "NGetType", noSideEffect, ...raises: [],
  2. tags: [], forbids: [].}

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. Source Edit

  1. proc getType(n: typedesc): NimNode {.magic: "NGetType", noSideEffect,
  2. ...raises: [], tags: [], forbids: [].}

Version of getType which takes a typedesc. Source Edit

  1. proc getTypeImpl(n: NimNode): NimNode {.magic: "NGetType", noSideEffect,
  2. ...raises: [], tags: [], forbids: [].}

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.

Example:

  1. type
  2. Vec[N: static[int], T] = object
  3. arr: array[N, T]
  4. Vec4[T] = Vec[4, T]
  5. Vec4f = Vec4[float32]
  6. var a: Vec4f
  7. var b: Vec4[float32]
  8. var c: Vec[4, float32]
  9. macro dumpTypeImpl(x: typed): untyped =
  10. newLit(x.getTypeImpl.repr)
  11. let t = """
  12. object
  13. arr: array[0 .. 3, float32]"""
  14. doAssert(dumpTypeImpl(a) == t)
  15. doAssert(dumpTypeImpl(b) == t)
  16. doAssert(dumpTypeImpl(c) == t)

Source Edit

  1. proc getTypeImpl(n: typedesc): NimNode {.magic: "NGetType", noSideEffect,
  2. ...raises: [], tags: [], forbids: [].}

Version of getTypeImpl which takes a typedesc. Source Edit

  1. proc getTypeInst(n: NimNode): NimNode {.magic: "NGetType", noSideEffect,
  2. ...raises: [], tags: [], forbids: [].}

Returns the type of a node in a form matching the way the type instance was declared in the code.

Example:

  1. type
  2. Vec[N: static[int], T] = object
  3. arr: array[N, T]
  4. Vec4[T] = Vec[4, T]
  5. Vec4f = Vec4[float32]
  6. var a: Vec4f
  7. var b: Vec4[float32]
  8. var c: Vec[4, float32]
  9. macro dumpTypeInst(x: typed): untyped =
  10. newLit(x.getTypeInst.repr)
  11. doAssert(dumpTypeInst(a) == "Vec4f")
  12. doAssert(dumpTypeInst(b) == "Vec4[float32]")
  13. doAssert(dumpTypeInst(c) == "Vec[4, float32]")

Source Edit

  1. proc getTypeInst(n: typedesc): NimNode {.magic: "NGetType", noSideEffect,
  2. ...raises: [], tags: [], forbids: [].}

Version of getTypeInst which takes a typedesc. Source Edit

  1. proc hasArgOfName(params: NimNode; name: string): bool {....raises: [], tags: [],
  2. forbids: [].}

Search nnkFormalParams for an argument. Source Edit

  1. proc hint(msg: string; n: NimNode = nil) {.magic: "NHint", ...gcsafe, raises: [],
  2. tags: [], forbids: [].}

Writes a hint message at compile time. Source Edit

  1. proc ident(n: NimNode): NimIdent {.magic: "NIdent", noSideEffect, ...deprecated: "Deprecated since version 0.18.1; All functionality is defined on \'NimNode\'.",
  2. raises: [], tags: [], forbids: [].}

Deprecated: Deprecated since version 0.18.1; All functionality is defined on ‘NimNode’.

Source Edit

  1. proc ident(name: string): NimNode {.magic: "StrToIdent", noSideEffect,
  2. ...raises: [], tags: [], forbids: [].}

Create a new ident node from a string. Source Edit

  1. proc ident=(n: NimNode; val: NimIdent) {.magic: "NSetIdent", noSideEffect, ...deprecated: "Deprecated since version 0.18.1; Generate a new \'NimNode\' with \'ident(string)\' instead.",
  2. raises: [], tags: [], forbids: [].}

Deprecated: Deprecated since version 0.18.1; Generate a new ‘NimNode’ with ‘ident(string)’ instead.

Source Edit

  1. proc infix(a: NimNode; op: string; b: NimNode): NimNode {....raises: [], tags: [],
  2. forbids: [].}

Source Edit

  1. proc insert(a: NimNode; pos: int; b: NimNode) {....raises: [], tags: [],
  2. forbids: [].}

Insert node b into node a at pos. Source Edit

  1. proc internalErrorFlag(): string {.magic: "NError", noSideEffect, ...raises: [],
  2. tags: [], forbids: [].}

Some builtins set an error flag. This is then turned into a proper exception. Note: Ordinary application code should not call this. Source Edit

  1. proc intVal(n: NimNode): BiggestInt {.magic: "NIntVal", noSideEffect,
  2. ...raises: [], tags: [], forbids: [].}

Returns an integer value from any integer literal or enum field symbol. Source Edit

  1. proc intVal=(n: NimNode; val: BiggestInt) {.magic: "NSetIntVal", noSideEffect,
  2. ...raises: [], tags: [], forbids: [].}

Source Edit

  1. proc isExported(n: NimNode): bool {.noSideEffect, ...raises: [], tags: [],
  2. forbids: [].}

Returns whether the symbol is exported or not. Source Edit

  1. proc isInstantiationOf(instanceProcSym, genProcSym: NimNode): bool {.
  2. magic: "SymIsInstantiationOf", noSideEffect, ...raises: [], tags: [],
  3. forbids: [].}

Checks if a proc symbol is an instance of the generic proc symbol. Useful to check proc symbols against generic symbols returned by bindSym. Source Edit

  1. proc kind(n: NimNode): NimNodeKind {.magic: "NKind", noSideEffect, ...raises: [],
  2. tags: [], forbids: [].}

Returns the kind of the node n. Source Edit

  1. proc last(node: NimNode): NimNode {....raises: [], tags: [], forbids: [].}

Return the last item in nodes children. Same as node[^1]. Source Edit

  1. proc len(n: NimNode): int {.magic: "NLen", noSideEffect, ...raises: [], tags: [],
  2. forbids: [].}

Returns the number of children of n. Source Edit

  1. proc lineInfo(arg: NimNode): string {....raises: [], tags: [], forbids: [].}

Return line info in the form filepath(line, column). Source Edit

  1. proc lineInfoObj(n: NimNode): LineInfo {....raises: [], tags: [], forbids: [].}

Returns LineInfo of n, using absolute path for filename. Source Edit

  1. proc lispRepr(n: NimNode; indented = false): string {....gcsafe, raises: [],
  2. tags: [], forbids: [].}

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

See also repr, treeRepr, and astGenRepr.

Source Edit

  1. proc name(someProc: NimNode): NimNode {....raises: [], tags: [], forbids: [].}

Source Edit

  1. proc name=(someProc: NimNode; val: NimNode) {....raises: [], tags: [], forbids: [].}

Source Edit

  1. proc nestList(op: NimNode; pack: NimNode): NimNode {....raises: [], tags: [],
  2. forbids: [].}

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. Source Edit

  1. proc nestList(op: NimNode; pack: NimNode; init: NimNode): NimNode {....raises: [],
  2. tags: [], forbids: [].}

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. Source Edit

  1. proc newAssignment(lhs, rhs: NimNode): NimNode {....raises: [], tags: [],
  2. forbids: [].}

Source Edit

  1. proc newBlockStmt(body: NimNode): NimNode {....raises: [], tags: [], forbids: [].}

Create a new block: stmt. Source Edit

  1. proc newBlockStmt(label, body: NimNode): NimNode {....raises: [], tags: [],
  2. forbids: [].}

Create a new block statement with label. Source Edit

  1. proc newCall(theProc: NimIdent; args: varargs[NimNode]): NimNode {....deprecated: "Deprecated since v0.18.1; use \'newCall(string, ...)\' or \'newCall(NimNode, ...)\' instead",
  2. raises: [], tags: [], forbids: [].}

Deprecated: Deprecated since v0.18.1; use ‘newCall(string, …)’ or ‘newCall(NimNode, …)’ instead

Produces a new call node. theProc is the proc that is called with the arguments args[0..]. Source Edit

  1. proc newCall(theProc: NimNode; args: varargs[NimNode]): NimNode {....raises: [],
  2. tags: [], forbids: [].}

Produces a new call node. theProc is the proc that is called with the arguments args[0..]. Source Edit

  1. proc newCall(theProc: string; args: varargs[NimNode]): NimNode {....raises: [],
  2. tags: [], forbids: [].}

Produces a new call node. theProc is the proc that is called with the arguments args[0..]. Source Edit

  1. proc newColonExpr(a, b: NimNode): NimNode {....raises: [], tags: [], forbids: [].}

Create new colon expression. newColonExpr(a, b) -> a: b Source Edit

  1. proc newCommentStmtNode(s: string): NimNode {.noSideEffect, ...raises: [],
  2. tags: [], forbids: [].}

Creates a comment statement node. Source Edit

  1. proc newConstStmt(name, value: NimNode): NimNode {....raises: [], tags: [],
  2. forbids: [].}

Create a new const stmt. Source Edit

  1. proc newDotExpr(a, b: NimNode): NimNode {....raises: [], tags: [], forbids: [].}

Create new dot expression. a.dot(b) -> a.b Source Edit

  1. proc newEmptyNode(): NimNode {.noSideEffect, ...raises: [], tags: [], forbids: [].}

Create a new empty node. Source Edit

  1. proc newEnum(name: NimNode; fields: openArray[NimNode]; public, pure: bool): NimNode {.
  2. ...raises: [], tags: [], forbids: [].}

Creates a new enum. name must be an ident. Fields are allowed to be either idents or EnumFieldDef:

  1. newEnum(
  2. name = ident("Colors"),
  3. fields = [ident("Blue"), ident("Red")],
  4. public = true, pure = false)
  5. # type Colors* = Blue Red

Source Edit

  1. proc newFloatLitNode(f: BiggestFloat): NimNode {....raises: [], tags: [],
  2. forbids: [].}

Creates a float literal node from f. Source Edit

  1. proc newIdentDefs(name, kind: NimNode; default = newEmptyNode()): NimNode {.
  2. ...raises: [], tags: [], forbids: [].}

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:

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

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

  1. result = newNimNode(nnkIdentDefs).add(
  2. ident("a"), ident("b"), ident("c"), ident("string"),
  3. newStrLitNode("Hello"))

Source Edit

  1. proc newIdentNode(i: NimIdent): NimNode {....deprecated: "use ident(string)",
  2. raises: [], tags: [], forbids: [].}

Deprecated: use ident(string)

Creates an identifier node from i. Source Edit

  1. proc newIdentNode(i: string): NimNode {.magic: "StrToIdent", noSideEffect,
  2. ...raises: [], tags: [], forbids: [].}

Creates an identifier node from i. It is simply an alias for ident(string). Use that, it’s shorter. Source Edit

  1. proc newIfStmt(branches: varargs[tuple[cond, body: NimNode]]): NimNode {.
  2. ...raises: [], tags: [], forbids: [].}

Constructor for if statements.

  1. newIfStmt(
  2. (Ident, StmtList),
  3. ...
  4. )

Source Edit

  1. proc newIntLitNode(i: BiggestInt): NimNode {....raises: [], tags: [], forbids: [].}

Creates an int literal node from i. Source Edit

  1. proc newLetStmt(name, value: NimNode): NimNode {....raises: [], tags: [],
  2. forbids: [].}

Create a new let stmt. Source Edit

  1. proc newLit(arg: enum): NimNode

Source Edit

  1. proc newLit(arg: object): NimNode

Source Edit

  1. proc newLit(arg: ref object): NimNode

produces a new ref type literal node. Source Edit

  1. proc newLit(b: bool): NimNode {....raises: [], tags: [], forbids: [].}

Produces a new boolean literal node. Source Edit

  1. proc newLit(c: char): NimNode {....raises: [], tags: [], forbids: [].}

Produces a new character literal node. Source Edit

  1. proc newLit(f: float32): NimNode {....raises: [], tags: [], forbids: [].}

Produces a new float literal node. Source Edit

  1. proc newLit(f: float64): NimNode {....raises: [], tags: [], forbids: [].}

Produces a new float literal node. Source Edit

  1. proc newLit(i: int): NimNode {....raises: [], tags: [], forbids: [].}

Produces a new integer literal node. Source Edit

  1. proc newLit(i: int8): NimNode {....raises: [], tags: [], forbids: [].}

Produces a new integer literal node. Source Edit

  1. proc newLit(i: int16): NimNode {....raises: [], tags: [], forbids: [].}

Produces a new integer literal node. Source Edit

  1. proc newLit(i: int32): NimNode {....raises: [], tags: [], forbids: [].}

Produces a new integer literal node. Source Edit

  1. proc newLit(i: int64): NimNode {....raises: [], tags: [], forbids: [].}

Produces a new integer literal node. Source Edit

  1. proc newLit(i: uint): NimNode {....raises: [], tags: [], forbids: [].}

Produces a new unsigned integer literal node. Source Edit

  1. proc newLit(i: uint8): NimNode {....raises: [], tags: [], forbids: [].}

Produces a new unsigned integer literal node. Source Edit

  1. proc newLit(i: uint16): NimNode {....raises: [], tags: [], forbids: [].}

Produces a new unsigned integer literal node. Source Edit

  1. proc newLit(i: uint32): NimNode {....raises: [], tags: [], forbids: [].}

Produces a new unsigned integer literal node. Source Edit

  1. proc newLit(i: uint64): NimNode {....raises: [], tags: [], forbids: [].}

Produces a new unsigned integer literal node. Source Edit

  1. proc newLit(s: string): NimNode {....raises: [], tags: [], forbids: [].}

Produces a new string literal node. Source Edit

  1. proc newLit[N, T](arg: array[N, T]): NimNode

Source Edit

  1. proc newLit[T: tuple](arg: T): NimNode

Source Edit

  1. proc newLit[T](arg: seq[T]): NimNode

Source Edit

  1. proc newLit[T](s: set[T]): NimNode

Source Edit

  1. proc newNilLit(): NimNode {....raises: [], tags: [], forbids: [].}

New nil literal shortcut. Source Edit

  1. proc newNimNode(kind: NimNodeKind; lineInfoFrom: NimNode = nil): NimNode {.
  2. magic: "NNewNimNode", noSideEffect, ...raises: [], tags: [], forbids: [].}

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.

Source Edit

  1. proc newPar(exprs: NimNode): NimNode {....raises: [], tags: [], forbids: [].}

Create a new parentheses-enclosed expression. Source Edit

  1. proc newPar(exprs: varargs[NimNode]): NimNode {....deprecated: "don\'t use newPar/nnkPar to construct tuple expressions; use nnkTupleConstr instead",
  2. raises: [], tags: [], forbids: [].}

Deprecated: don’t use newPar/nnkPar to construct tuple expressions; use nnkTupleConstr instead

Create a new parentheses-enclosed expression. Source Edit

  1. proc newProc(name = newEmptyNode();
  2. params: openArray[NimNode] = [newEmptyNode()];
  3. body: NimNode = newStmtList(); procType = nnkProcDef;
  4. pragmas: NimNode = newEmptyNode()): NimNode {....raises: [], tags: [],
  5. forbids: [].}

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.

Source Edit

  1. proc newStmtList(stmts: varargs[NimNode]): NimNode {....raises: [], tags: [],
  2. forbids: [].}

Create a new statement list. Source Edit

  1. proc newStrLitNode(s: string): NimNode {.noSideEffect, ...raises: [], tags: [],
  2. forbids: [].}

Creates a string literal node from s. Source Edit

  1. proc newTree(kind: NimNodeKind; children: varargs[NimNode]): NimNode {.
  2. ...raises: [], tags: [], forbids: [].}

Produces a new node with children. Source Edit

  1. proc newVarStmt(name, value: NimNode): NimNode {....raises: [], tags: [],
  2. forbids: [].}

Create a new var stmt. Source Edit

  1. proc nodeID(n: NimNode): int {.magic: "NodeId", ...raises: [], tags: [],
  2. forbids: [].}

Returns the id of n, when the compiler has been compiled with the flag -d:useNodeids, otherwise returns -1. This proc is for the purpose to debug the compiler only. Source Edit

  1. proc owner(sym: NimNode): NimNode {.magic: "SymOwner", noSideEffect, ...raises: [],
  2. tags: [], forbids: [].}

Accepts a node of kind nnkSym and returns its owner’s symbol. The meaning of ‘owner’ depends on sym’s NimSymKind and declaration context. For top level declarations this is an nskModule symbol, for proc local variables an nskProc symbol, for enum/object fields an nskType symbol, etc. For symbols without an owner, nil is returned.

See also:

Source Edit

  1. proc params(someProc: NimNode): NimNode {....raises: [], tags: [], forbids: [].}

Source Edit

  1. proc params=(someProc: NimNode; params: NimNode) {....raises: [], tags: [],
  2. forbids: [].}

Source Edit

  1. proc parseExpr(s: string; filename: string = ""): NimNode {.noSideEffect,
  2. ...raises: [ValueError], tags: [], forbids: [].}

Compiles the passed string to its AST representation. Expects a single expression. Raises ValueError for parsing errors. A filename can be given for more informative errors. Source Edit

  1. proc parseStmt(s: string; filename: string = ""): NimNode {.noSideEffect,
  2. ...raises: [ValueError], tags: [], forbids: [].}

Compiles the passed string to its AST representation. Expects one or more statements. Raises ValueError for parsing errors. A filename can be given for more informative errors. Source Edit

  1. proc postfix(node: NimNode; op: string): NimNode {....raises: [], tags: [],
  2. forbids: [].}

Source Edit

  1. proc pragma(someProc: NimNode): NimNode {....raises: [], tags: [], forbids: [].}

Get the pragma of a proc type. These will be expanded. Source Edit

  1. proc pragma=(someProc: NimNode; val: NimNode) {....raises: [], tags: [],
  2. forbids: [].}

Set the pragma of a proc type. Source Edit

  1. proc prefix(node: NimNode; op: string): NimNode {....raises: [], tags: [],
  2. forbids: [].}

Source Edit

  1. proc quote(bl: typed; op = "``"): NimNode {.magic: "QuoteAst", noSideEffect,
  2. ...raises: [], tags: [], forbids: [].}

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) when used as a unary operator: e.g. @ is escaped as @@, &% is escaped as &%&% and so on; see examples.

A custom operator interpolation needs accent quoted (``) whenever it resolves to a symbol.

See also genasts which avoids some issues with quote.

Example:

  1. macro check(ex: untyped) =
  2. # this is a simplified version of the check macro from the
  3. # unittest module.
  4. # If there is a failed check, we want to make it easy for
  5. # the user to jump to the faulty line in the code, so we
  6. # get the line info here:
  7. var info = ex.lineinfo
  8. # We will also display the code string of the failed check:
  9. var expString = ex.toStrLit
  10. # Finally we compose the code to implement the check:
  11. result = quote do:
  12. if not `ex`:
  13. echo `info` & ": Check failed: " & `expString`
  14. check 1 + 1 == 2

Example:

  1. # example showing how to define a symbol that requires backtick without
  2. # quoting it.
  3. var destroyCalled = false
  4. macro bar() =
  5. let s = newTree(nnkAccQuoted, ident"=destroy")
  6. # let s = ident"`=destroy`" # this would not work
  7. result = quote do:
  8. type Foo = object
  9. # proc `=destroy`(a: var Foo) = destroyCalled = true # this would not work
  10. proc `s`(a: var Foo) = destroyCalled = true
  11. block:
  12. let a = Foo()
  13. bar()
  14. doAssert destroyCalled

Example:

  1. # custom `op`
  2. var destroyCalled = false
  3. macro bar(ident) =
  4. var x = 1.5
  5. result = quote("@") do:
  6. type Foo = object
  7. let `@ident` = 0 # custom op interpolated symbols need quoted (``)
  8. proc `=destroy`(a: var Foo) =
  9. doAssert @x == 1.5
  10. doAssert compiles(@x == 1.5)
  11. let b1 = @[1,2]
  12. let b2 = @@[1,2]
  13. doAssert $b1 == "[1, 2]"
  14. doAssert $b2 == "@[1, 2]"
  15. destroyCalled = true
  16. block:
  17. let a = Foo()
  18. bar(someident)
  19. doAssert destroyCalled
  20. proc `&%`(x: int): int = 1
  21. proc `&%`(x, y: int): int = 2
  22. macro bar2() =
  23. var x = 3
  24. result = quote("&%") do:
  25. var y = &%x # quoting operator
  26. doAssert &%&%y == 1 # unary operator => need to escape
  27. doAssert y &% y == 2 # binary operator => no need to escape
  28. doAssert y == 3
  29. bar2()

Source Edit

  1. proc sameType(a, b: NimNode): bool {.magic: "SameNodeType", noSideEffect,
  2. ...raises: [], tags: [], forbids: [].}

Compares two Nim nodes’ types. Return true if the types are the same, e.g. true when comparing alias with original type. Source Edit

  1. proc setLineInfo(arg: NimNode; file: string; line: int; column: int) {.
  2. ...raises: [], tags: [], forbids: [].}

Sets the line info on the NimNode. The file needs to exists, but can be a relative path. If you want to attach line info to a block using quote you’ll need to add the line information after the quote block. Source Edit

  1. proc setLineInfo(arg: NimNode; lineInfo: LineInfo) {....raises: [], tags: [],
  2. forbids: [].}

See setLineInfo proc Source Edit

  1. proc signatureHash(n: NimNode): string {.magic: "NSigHash", noSideEffect,
  2. ...raises: [], tags: [], forbids: [].}

Returns a stable identifier derived from the signature of a symbol. The signature combines many factors such as the type of the symbol, the owning module of the symbol and others. The same identifier is used in the back-end to produce the mangled symbol name. Source Edit

  1. proc strVal(n: NimNode): string {.magic: "NStrVal", noSideEffect, ...raises: [],
  2. tags: [], forbids: [].}

Returns the string value of an identifier, symbol, comment, or string literal.

See also:

Source Edit

  1. proc strVal=(n: NimNode; val: string) {.magic: "NSetStrVal", noSideEffect,
  2. ...raises: [], tags: [], forbids: [].}

Sets the string value of a string literal or comment. Setting strVal is disallowed for nnkIdent and nnkSym nodes; a new node must be created using ident or bindSym instead.

See also:

Source Edit

  1. proc symBodyHash(s: NimNode): string {.noSideEffect, ...raises: [], tags: [],
  2. forbids: [].}

Returns a stable digest for symbols derived not only from type signature and owning module, but also implementation body. All procs/variables used in the implementation of this symbol are hashed recursively as well, including magics from system module. Source Edit

  1. proc symbol(n: NimNode): NimSym {.magic: "NSymbol", noSideEffect, ...deprecated: "Deprecated since version 0.18.1; All functionality is defined on \'NimNode\'.",
  2. raises: [], tags: [], forbids: [].}

Deprecated: Deprecated since version 0.18.1; All functionality is defined on ‘NimNode’.

Source Edit

  1. proc symbol=(n: NimNode; val: NimSym) {.magic: "NSetSymbol", noSideEffect, ...deprecated: "Deprecated since version 0.18.1; Generate a new \'NimNode\' with \'genSym\' instead.",
  2. raises: [], tags: [], forbids: [].}

Deprecated: Deprecated since version 0.18.1; Generate a new ‘NimNode’ with ‘genSym’ instead.

Source Edit

  1. proc symKind(symbol: NimNode): NimSymKind {.magic: "NSymKind", noSideEffect,
  2. ...raises: [], tags: [], forbids: [].}

Source Edit

  1. proc toNimIdent(s: string): NimIdent {.magic: "StrToIdent", noSideEffect, ...deprecated: "Deprecated since version 0.18.0: Use \'ident\' or \'newIdentNode\' instead.",
  2. raises: [], tags: [], forbids: [].}

Deprecated: Deprecated since version 0.18.0: Use ‘ident’ or ‘newIdentNode’ instead.

Constructs an identifier from the string s. Source Edit

  1. proc toStrLit(n: NimNode): NimNode {....raises: [], tags: [], forbids: [].}

Converts the AST n to the concrete Nim code and wraps that in a string literal node. Source Edit

  1. proc treeRepr(n: NimNode): string {....gcsafe, raises: [], tags: [], forbids: [].}

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

See also repr, lispRepr, and astGenRepr.

Source Edit

  1. proc typeKind(n: NimNode): NimTypeKind {.magic: "NGetType", noSideEffect,
  2. ...raises: [], tags: [], forbids: [].}

Returns the type kind of the node ‘n’ that should represent a type, that means the node should have been obtained via getType. Source Edit

  1. proc unpackInfix(node: NimNode): tuple[left: NimNode, op: string, right: NimNode] {.
  2. ...raises: [], tags: [], forbids: [].}

Source Edit

  1. proc unpackPostfix(node: NimNode): tuple[node: NimNode, op: string] {.
  2. ...raises: [], tags: [], forbids: [].}

Source Edit

  1. proc unpackPrefix(node: NimNode): tuple[node: NimNode, op: string] {....raises: [],
  2. tags: [], forbids: [].}

Source Edit

  1. proc warning(msg: string; n: NimNode = nil) {.magic: "NWarning", ...gcsafe,
  2. raises: [], tags: [], forbids: [].}

Writes a warning message at compile time. Source Edit

Iterators

  1. iterator children(n: NimNode): NimNode {.inline, ...raises: [], tags: [],
  2. forbids: [].}

Iterates over the children of the NimNode n. Source Edit

  1. iterator items(n: NimNode): NimNode {.inline, ...raises: [], tags: [], forbids: [].}

Iterates over the children of the NimNode n. Source Edit

  1. iterator pairs(n: NimNode): (int, NimNode) {.inline, ...raises: [], tags: [],
  2. forbids: [].}

Iterates over the children of the NimNode n and its indices. Source Edit

Macros

  1. macro dumpAstGen(s: untyped): untyped

Accepts a block of nim code and prints the parsed abstract syntax tree using the astGenRepr proc. 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.

For example:

  1. dumpAstGen:
  2. echo "Hello, World!"

Outputs:

  1. nnkStmtList.newTree(
  2. nnkCommand.newTree(
  3. newIdentNode("echo"),
  4. newLit("Hello, World!")
  5. )
  6. )

Also see dumpTree and dumpLisp.

Source Edit

  1. macro dumpLisp(s: untyped): untyped

Accepts a block of nim code and prints the parsed abstract syntax tree using the lispRepr proc. 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.

For example:

  1. dumpLisp:
  2. echo "Hello, World!"

Outputs:

  1. (StmtList
  2. (Command
  3. (Ident "echo")
  4. (StrLit "Hello, World!")))

Also see dumpAstGen and dumpTree.

Source Edit

  1. macro dumpTree(s: untyped): untyped

Accepts a block of nim code and prints the parsed abstract syntax tree using the treeRepr proc. 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.

For example:

  1. dumpTree:
  2. echo "Hello, World!"

Outputs:

  1. StmtList
  2. Command
  3. Ident "echo"
  4. StrLit "Hello, World!"

Also see dumpAstGen and dumpLisp.

Source Edit

  1. 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,

  1. import std/[sugar, macros]
  2. let
  3. x = 10
  4. y = 20
  5. expandMacros:
  6. 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].

Source Edit

  1. 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.

  1. template serializationKey(key: string) {.pragma.}
  2. type
  3. MyObj {.serializationKey: "mo".} = object
  4. myField {.serializationKey: "mf".}: int
  5. var o: MyObj
  6. assert(o.myField.getCustomPragmaVal(serializationKey) == "mf")
  7. assert(o.getCustomPragmaVal(serializationKey) == "mo")
  8. assert(MyObj.getCustomPragmaVal(serializationKey) == "mo")

Source Edit

  1. 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.

  1. template myAttr() {.pragma.}
  2. type
  3. MyObj = object
  4. myField {.myAttr.}: int
  5. proc myProc() {.myAttr.} = discard
  6. var o: MyObj
  7. assert(o.myField.hasCustomPragma(myAttr))
  8. assert(myProc.hasCustomPragma(myAttr))

Source Edit

  1. macro unpackVarargs(callee: untyped; args: varargs[untyped]): untyped

Calls callee with args unpacked as individual arguments. This is useful in 2 cases:

  • when forwarding varargs[T] for some typed T
  • when forwarding varargs[untyped] when args can potentially be empty, due to a compiler limitation

Example:

  1. template call1(fun: typed; args: varargs[untyped]): untyped =
  2. unpackVarargs(fun, args)
  3. # when varargsLen(args) > 0: fun(args) else: fun() # this would also work
  4. template call2(fun: typed; args: varargs[typed]): untyped =
  5. unpackVarargs(fun, args)
  6. proc fn1(a = 0, b = 1) = discard (a, b)
  7. call1(fn1, 10, 11)
  8. call1(fn1) # `args` is empty in this case
  9. if false: call2(echo, 10, 11) # would print 1011

Source Edit

Templates

  1. template findChild(n: NimNode; cond: untyped): NimNode {.dirty.}

Find the first child node matching condition (or nil).

  1. var res = findChild(n, it.kind == nnkPostfix and
  2. it.basename.ident == ident"foo")

Source Edit

  1. template `or`(x, y: NimNode): NimNode

Evaluate x and when it is not an empty node, return it. Otherwise evaluate to y. Can be used to chain several expressions to get the first expression that is not empty.

  1. let node = mightBeEmpty() or mightAlsoBeEmpty() or fallbackNode

Source Edit