# # # Nim's Runtime Library # (c) Copyright 2015 Andreas Rumpf # # See the file "copying.txt", included in this # distribution, for details about the copyright. # include "system/inclrtl" ## This module contains the interface to the compiler's abstract syntax ## tree (`AST`:idx:). Macros operate on this tree. ## .. include:: ../../doc/astspec.txt type 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, nnkMetaNode, 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, # iterator type nnkSharedTy, # 'shared T' nnkEnumTy, nnkEnumFieldDef, nnkArglist, nnkPattern nnkReturnToken, nnkClosure, nnkGotoState, nnkState, nnkBreakState NimNodeKinds* = set[NimNodeKind] NimTypeKind* = enum # some types are no longer used, see ast.nim 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, ntyFieldAccessor, ntyVoid TNimTypeKinds* {.deprecated.} = set[NimTypeKind] NimSymKind* = enum nskUnknown, nskConditional, nskDynLib, nskParam, nskGenericParam, nskTemp, nskModule, nskType, nskVar, nskLet, nskConst, nskResult, nskProc, nskMethod, nskIterator, nskConverter, nskMacro, nskTemplate, nskField, nskEnumField, nskForVar, nskLabel, nskStub TNimSymKinds* {.deprecated.} = set[NimSymKind] type NimIdent* = object of RootObj ## represents a Nim identifier in the AST NimSymObj = object # hidden NimSym* = ref NimSymObj ## represents a Nim *symbol* in the compiler; a *symbol* is a looked-up ## *ident*. {.deprecated: [TNimrodNodeKind: NimNodeKind, TNimNodeKinds: NimNodeKinds, TNimrodTypeKind: NimTypeKind, TNimrodSymKind: NimSymKind, TNimrodIdent: NimIdent, PNimrodSymbol: NimSym].} const nnkLiterals* = {nnkCharLit..nnkNilLit} nnkCallKinds* = {nnkCall, nnkInfix, nnkPrefix, nnkPostfix, nnkCommand, nnkCallStrLit} proc `[]`*(n: NimNode, i: int): NimNode {.magic: "NChild", noSideEffect.} ## 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 `!`*(s: string): NimIdent {.magic: "StrToIdent", noSideEffect.} ## constructs an identifier from the string `s` proc `$`*(i: NimIdent): string {.magic: "IdentToStr", noSideEffect.} ## converts a Nim identifier to a string proc `$`*(s: NimSym): string {.magic: "IdentToStr", noSideEffect.} ## converts a Nim symbol to a string proc `==`*(a, b: NimIdent): bool {.magic: "EqIdent", noSideEffect.} ## compares two Nim identifiers proc `==`*(a, b: NimNode): bool {.magic: "EqNimrodNode", noSideEffect.} ## compares two Nim nodes proc sameType*(a, b: NimNode): bool {.magic: "SameNodeType", noSideEffect.} = ## compares two Nim nodes' types. Return true if the types are the same, ## eg. true when comparing alias with original type. discard proc len*(n: NimNode): int {.magic: "NLen", noSideEffect.} ## returns the number of children of `n`. 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 symbol*(n: NimNode): NimSym {.magic: "NSymbol", noSideEffect.} proc ident*(n: NimNode): NimIdent {.magic: "NIdent", noSideEffect.} proc getType*(n: NimNode): NimNode {.magic: "NGetType", noSideEffect.} ## with 'getType' you can access the node's `type`:idx:. 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.} ## Like getType except it includes generic parameters for a specific instance proc getTypeInst*(n: typedesc): NimNode {.magic: "NGetType", noSideEffect.} ## Like getType except it includes generic parameters for a specific instance proc getTypeImpl*(n: NimNode): NimNode {.magic: "NGetType", noSideEffect.} ## Like getType except it includes generic parameters for the implementation proc getTypeImpl*(n: typedesc): NimNode {.magic: "NGetType", noSideEffect.} ## Like getType except it includes generic parameters for the implementation proc strVal*(n: NimNode): string {.magic: "NStrVal", noSideEffect.} 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.} proc `ident=`*(n: NimNode, val: NimIdent) {.magic: "NSetIdent", noSideEffect.} #proc `typ=`*(n: NimNode, typ: typedesc) {.magic: "NSetType".} # this is not sound! Unfortunately forbidding 'typ=' is not enough, as you # can easily do: # let bracket = semCheck([1, 2]) # let fake = semCheck(2.0) # bracket[0] = fake # constructs a mixed array with ints and floats! 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 getImpl*(s: NimSym): NimNode {.magic: "GetImpl", noSideEffect.} = ## retrieve the implementation of a symbol `s`. `s` can be a routine or a ## const. discard proc error*(msg: string, n: NimNode = nil) {.magic: "NError", benign.} ## writes an error message at compile time proc warning*(msg: string) {.magic: "NWarning", benign.} ## writes a warning message at compile time proc hint*(msg: string) {.magic: "NHint", benign.} ## writes a hint message at compile time proc newStrLitNode*(s: string): NimNode {.compileTime, noSideEffect.} = ## creates a string literal node from `s` result = newNimNode(nnkStrLit) result.strVal = s proc newCommentStmtNode*(s: string): NimNode {.compileTime, noSideEffect.} = ## creates a comment statement node result = newNimNode(nnkCommentStmt) result.strVal = s proc newIntLitNode*(i: BiggestInt): NimNode {.compileTime.} = ## creates a int literal node from `i` result = newNimNode(nnkIntLit) result.intVal = i proc newFloatLitNode*(f: BiggestFloat): NimNode {.compileTime.} = ## creates a float literal node from `f` result = newNimNode(nnkFloatLit) result.floatVal = f proc newIdentNode*(i: NimIdent): NimNode {.compileTime.} = ## creates an identifier node from `i` result = newNimNode(nnkIdent) result.ident = i proc newIdentNode*(i: string): NimNode {.compileTime.} = ## creates an identifier node from `i` result = newNimNode(nnkIdent) result.ident = !i type BindSymRule* = enum ## specifies how ``bindSym`` behaves 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) {.deprecated: [TBindSymRule: BindSymRule].} proc bindSym*(ident: string, 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 ``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. 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", benign.} ## returns the AST of the invocation expression that invoked this macro. proc toStrLit*(n: NimNode): NimNode {.compileTime.} = ## converts the AST `n` to the concrete Nim code and wraps that ## in a string literal node return newStrLitNode(repr(n)) type LineInfo* = object filename*: string line*,column*: int proc `$`*(arg: Lineinfo): string = result = arg.filename & "(" & $arg.line & ", " & $arg.column & ")" #proc lineinfo*(n: NimNode): LineInfo {.magic: "NLineInfo", noSideEffect.} ## returns the position the node appears in the original source file ## in the form filename(line, col) proc getLine(arg: NimNode): int {.magic: "NLineInfo", noSideEffect.} proc getColumn(arg: NimNode): int {.magic: "NLineInfo", noSideEffect.} proc getFile(arg: NimNode): string {.magic: "NLineInfo", noSideEffect.} proc lineInfoObj*(n: NimNode): LineInfo {.compileTime.} = result.filename = n.getFile result.line = n.getLine result.column = n.getColumn proc lineInfo*(arg: NimNode): string {.compileTime.} = $arg.lineInfoObj proc internalParseExpr(s: string): NimNode {. magic: "ParseExprToAst", noSideEffect.} proc internalParseStmt(s: string): NimNode {. magic: "ParseStmtToAst", noSideEffect.} 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.} = ## Compiles the passed string to its AST representation. ## Expects a single expression. Raises ``ValueError`` for parsing errors. result = internalParseExpr(s) let x = internalErrorFlag() if x.len > 0: raise newException(ValueError, x) proc parseStmt*(s: string): NimNode {.noSideEffect, compileTime.} = ## Compiles the passed string to its AST representation. ## Expects one or more statements. Raises ``ValueError`` for parsing errors. result = internalParseStmt(s) let x = internalErrorFlag() if x.len > 0: raise newException(ValueError, x) proc getAst*(macroOrTemplate: untyped): NimNode {.magic: "ExpandToAst", noSideEffect.} ## Obtains the AST nodes returned from a macro or template invocation. ## Example: ## ## .. code-block:: nim ## ## 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: ## ## .. code-block:: nim ## ## macro check(ex: expr): stmt = ## # 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.} = ## 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. if n.kind != k: error("Expected a node of kind " & $k & ", got " & $n.kind, n) proc expectMinLen*(n: NimNode, min: int) {.compileTime.} = ## 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. if n.len < min: error("macro expects a node with " & $min & " children", n) proc expectLen*(n: NimNode, len: int) {.compileTime.} = ## 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. if n.len != len: error("macro expects a node with " & $len & " children", n) proc newTree*(kind: NimNodeKind, children: varargs[NimNode]): NimNode {.compileTime.} = ## produces a new node with children. result = newNimNode(kind) result.add(children) proc newCall*(theProc: NimNode, args: varargs[NimNode]): NimNode {.compileTime.} = ## produces a new call node. `theProc` is the proc that is called with ## the arguments ``args[0..]``. result = newNimNode(nnkCall) result.add(theProc) result.add(args) proc newCall*(theProc: NimIdent, args: varargs[NimNode]): NimNode {.compileTime.} = ## produces a new call node. `theProc` is the proc that is called with ## the arguments ``args[0..]``. result = newNimNode(nnkCall) result.add(newIdentNode(theProc)) result.add(args) proc newCall*(theProc: string, args: varargs[NimNode]): NimNode {.compileTime.} = ## produces a new call node. `theProc` is the proc that is called with ## the arguments ``args[0..]``. result = newNimNode(nnkCall) result.add(newIdentNode(theProc)) result.add(args) proc newLit*(c: char): NimNode {.compileTime.} = ## produces a new character literal node. result = newNimNode(nnkCharLit) result.intVal = ord(c) proc newLit*(i: int): NimNode {.compileTime.} = ## produces a new integer literal node. result = newNimNode(nnkIntLit) result.intVal = i proc newLit*(i: int8): NimNode {.compileTime.} = ## produces a new integer literal node. result = newNimNode(nnkInt8Lit) result.intVal = i proc newLit*(i: int16): NimNode {.compileTime.} = ## produces a new integer literal node. result = newNimNode(nnkInt16Lit) result.intVal = i proc newLit*(i: int32): NimNode {.compileTime.} = ## produces a new integer literal node. result = newNimNode(nnkInt32Lit) result.intVal = i proc newLit*(i: int64): NimNode {.compileTime.} = ## produces a new integer literal node. result = newNimNode(nnkInt64Lit) result.intVal = i proc newLit*(i: uint): NimNode {.compileTime.} = ## produces a new unsigned integer literal node. result = newNimNode(nnkUIntLit) result.intVal = BiggestInt(i) proc newLit*(i: uint8): NimNode {.compileTime.} = ## produces a new unsigned integer literal node. result = newNimNode(nnkUInt8Lit) result.intVal = BiggestInt(i) proc newLit*(i: uint16): NimNode {.compileTime.} = ## produces a new unsigned integer literal node. result = newNimNode(nnkUInt16Lit) result.intVal = BiggestInt(i) proc newLit*(i: uint32): NimNode {.compileTime.} = ## produces a new unsigned integer literal node. result = newNimNode(nnkUInt32Lit) result.intVal = BiggestInt(i) proc newLit*(i: uint64): NimNode {.compileTime.} = ## produces a new unsigned integer literal node. result = newNimNode(nnkUInt64Lit) result.intVal = BiggestInt(i) proc newLit*(b: bool): NimNode {.compileTime.} = ## produces a new boolean literal node. result = if b: bindSym"true" else: bindSym"false" when false: # the float type is not really a distinct type as described in https://github.com/nim-lang/Nim/issues/5875 proc newLit*(f: float): NimNode {.compileTime.} = ## produces a new float literal node. result = newNimNode(nnkFloatLit) result.floatVal = f proc newLit*(f: float32): NimNode {.compileTime.} = ## produces a new float literal node. result = newNimNode(nnkFloat32Lit) result.floatVal = f proc newLit*(f: float64): NimNode {.compileTime.} = ## produces a new float literal node. result = newNimNode(nnkFloat64Lit) result.floatVal = f when compiles(float128): proc newLit*(f: float128): NimNode {.compileTime.} = ## produces a new float literal node. result = newNimNode(nnkFloat128Lit) result.floatVal = f proc newLit*(arg: object): NimNode {.compileTime.} = result = nnkObjConstr.newTree(arg.type.getTypeInst[1]) for a, b in arg.fieldPairs: result.add nnkExprColonExpr.newTree( newIdentNode(a), newLit(b) ) proc newLit*[N,T](arg: array[N,T]): NimNode {.compileTime.} = result = nnkBracket.newTree for x in arg: result.add newLit(x) proc newLit*[T](arg: seq[T]): NimNode {.compileTime.} = var bracket = nnkBracket.newTree for x in arg: bracket.add newLit(x) result = nnkCall.newTree( nnkBracketExpr.newTree( nnkAccQuoted.newTree( bindSym"@" ), getTypeInst( bindSym"T" ) ), bracket ) proc newLit*(arg: tuple): NimNode {.compileTime.} = result = nnkPar.newTree for a,b in arg.fieldPairs: result.add nnkExprColonExpr.newTree( newIdentNode(a), newLit(b) ) proc newLit*(s: string): NimNode {.compileTime.} = ## produces a new string literal node. result = newNimNode(nnkStrLit) result.strVal = s proc nestList*(theProc: NimIdent, x: NimNode): NimNode {.compileTime.} = ## nests the list `x` into a tree of call expressions: ## ``[a, b, c]`` is transformed into ``theProc(a, theProc(c, d))``. var L = x.len result = newCall(theProc, x[L-2], x[L-1]) for i in countdown(L-3, 0): # XXX the 'copyNimTree' here is necessary due to a bug in the evaluation # engine that would otherwise create an endless loop here. :-( # This could easily user code and so should be fixed in evals.nim somehow. result = newCall(theProc, x[i], copyNimTree(result)) proc treeRepr*(n: NimNode): string {.compileTime, benign.} = ## Convert the AST `n` to a human-readable tree-like string. ## ## See also `repr`, `lispRepr`, and `astGenRepr`. proc traverse(res: var string, level: int, n: NimNode) {.benign.} = for i in 0..level-1: res.add " " res.add(($n.kind).substr(3)) case n.kind of nnkEmpty: discard # same as nil node in this representation of nnkNilLit: res.add(" nil") of nnkCharLit..nnkInt64Lit: res.add(" " & $n.intVal) of nnkFloatLit..nnkFloat64Lit: res.add(" " & $n.floatVal) of nnkStrLit..nnkTripleStrLit: res.add(" " & $n.strVal) of nnkIdent: res.add(" !\"" & $n.ident & '"') of nnkSym: res.add(" \"" & $n.symbol & '"') of nnkNone: assert false else: for j in 0..n.len-1: res.add "\n" traverse(res, level + 1, n[j]) result = "" traverse(result, 0, n) proc lispRepr*(n: NimNode): string {.compileTime, benign.} = ## Convert the AST `n` to a human-readable lisp-like string, ## ## See also `repr`, `treeRepr`, and `astGenRepr`. result = ($n.kind).substr(3) add(result, "(") case n.kind of nnkEmpty: discard # same as nil node in this representation of nnkNilLit: add(result, "nil") of nnkCharLit..nnkInt64Lit: add(result, $n.intVal) of nnkFloatLit..nnkFloat64Lit: add(result, $n.floatVal) of nnkStrLit..nnkTripleStrLit: add(result, $n.strVal) of nnkIdent: add(result, "!\"" & $n.ident & '"') of nnkSym: add(result, $n.symbol) of nnkNone: assert false else: if n.len > 0: add(result, lispRepr(n[0])) for j in 1..n.len-1: add(result, ", ") add(result, lispRepr(n[j])) add(result, ")") proc astGenRepr*(n: NimNode): string {.compileTime, benign.} = ## Convert the AST `n` to the code required to generate that AST. So for example ## ## .. code-block:: nim ## astGenRepr: ## echo "Hello world" ## ## Would output: ## ## .. code-block:: nim ## nnkStmtList.newTree( ## nnkCommand.newTree( ## newIdentNode(!"echo"), ## newLit("Hello world") ## ) ## ) ## ## See also `repr`, `treeRepr`, and `lispRepr`. const NodeKinds = {nnkEmpty, nnkNilLit, nnkIdent, nnkSym, nnkNone} LitKinds = {nnkCharLit..nnkInt64Lit, nnkFloatLit..nnkFloat64Lit, nnkStrLit..nnkTripleStrLit} proc escape(s: string, prefix = "\"", suffix = "\""): string {.noSideEffect.} = ## Functions copied from strutils proc toHex(x: BiggestInt, len: Positive): string {.noSideEffect, rtl.} = const HexChars = "0123456789ABCDEF" var t = x result = newString(len) for j in countdown(len-1, 0): result[j] = HexChars[t and 0xF] t = t shr 4 # handle negative overflow if t == 0 and x < 0: t = -1 result = newStringOfCap(s.len + s.len shr 2) result.add(prefix) for c in items(s): case c of '\0'..'\31', '\128'..'\255': add(result, "\\x") add(result, toHex(ord(c), 2)) of '\\': add(result, "\\\\") of '\'': add(result, "\\'") of '\"': add(result, "\\\"") else: add(result, c) add(result, suffix) proc traverse(res: var string, level: int, n: NimNode) {.benign.} = for i in 0..level-1: res.add " " if n.kind in NodeKinds: res.add("new" & ($n.kind).substr(3) & "Node(") elif n.kind in LitKinds: res.add("newLit(") else: res.add($n.kind) case n.kind of nnkEmpty: discard of nnkNilLit: res.add("nil") of nnkCharLit: res.add("'" & $chr(n.intVal) & "'") of nnkIntLit..nnkInt64Lit: res.add($n.intVal) of nnkFloatLit..nnkFloat64Lit: res.add($n.floatVal) of nnkStrLit..nnkTripleStrLit: res.add($n.strVal.escape()) of nnkIdent: res.add("!" & ($n.ident).escape()) of nnkSym: res.add(($n.symbol).escape()) of nnkNone: assert false else: res.add(".newTree(") for j in 0.. `a.b` return newNimNode(nnkDotExpr).add(a, b) proc newColonExpr*(a, b: NimNode): NimNode {.compileTime.} = ## Create new colon expression ## newColonExpr(a, b) -> `a: b` newNimNode(nnkExprColonExpr).add(a, b) proc newIdentDefs*(name, kind: NimNode; default = newEmptyNode()): NimNode {.compileTime.} = ## 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: ## ## .. code-block:: nim ## ## 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``: ## ## .. code-block:: nim ## ## result = newNimNode(nnkIdentDefs).add( ## ident("a"), ident("b"), ident("c"), ident("string"), ## newStrLitNode("Hello")) newNimNode(nnkIdentDefs).add(name, kind, default) proc newNilLit*(): NimNode {.compileTime.} = ## New nil literal shortcut result = newNimNode(nnkNilLit) proc last*(node: NimNode): NimNode {.compileTime.} = node[