summary refs log tree commit diff stats
path: root/tests/stdlib/tparseipv6.nim
blob: 3e1c23e584dc81e2c670954ea374418ce2f6e0b4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
discard """
  output: "all ok"
"""

import net

const
  positives = [
    "::f:8:a8f:218.17.235.229",
    "::b:228.19.241.2",
    "::8:c:a:f:8.35.8.096",
    "::3:e:a:bc:04.19.2.9",
    "::2:212.242.248.19",
    "::df:a5f:3.250.208.9",
    "::8:c:5:e63:250.208.249.0",
    "::b:f:181.12.9.98",
    "::a:f8:77.08.243.232",
    "::a:b:85:e4d9:252.9.229.056",
    "941:c:8a:c:e::917",
    "e8:7a:e:ad:88a:8:203.235.225.46",
    "139c:9e::f8:254.08.21.249",
    "b38:f0:e::f9:89.6.12.18",
    "ef::8",
    "5::ab",
    "a::8:255.247.96.253",
    "b:c0::c:254.248.95.254",
    "::8c:2:99.251.024.3",
    "98::c:247.240.249.57",
    "9::9",
    "628::f1ed:f",
    "c::cca8",
    "2::3:c",
    "fde::8fcc:92:e",
    "f::3",
    "e85::7",
    "8::b:f6",
    "0::6:8ca",
    "c8::6e:be8",
    "87::e",
    "6:9::a7:9",
    "c::5",
    "49::1:62",
    "df:c0::f:9",
    "a09a:8::21:887a",
    "2:f::c",
    "8bf5:5::2a6e:f8f",
    "a:9e::bc:a",
    "f:60::c:fd",
    "59::52f:0:fa7",
    "8268:6cf::f:9",
    "c:abb::f",
    "a:ff8d::9:7",
    "05:c87::9c:9a",
    "e:f::c:9a:1",
    "ff6:8::962:e",
    "9::bd",
    "68:ec::6",
    "3b8:f::94:3e9:9952",
    "49b4:ae::899:b4",
    "cb9:8e8:af::f4",
    "8::10:9ae6:f9",
    "b9::2:57",
    "ff:fba9::d",
    "4::a:8",
    "caa:c:85a::2:3",
    "5::a5:9",
    "c:ad::a",
    "9a:f:f65::b",
    "f:df::9:0",
    "c:b9::8de",
    "d:f::a",
    "ab88:d4:0::fc:8d",
    "8f:ee2::3",
    "f:f8::bf2:8c8",
    "8::efc",
    "e:5a::b",
    "c:48::94",
    "a:b:5::8",
    "f:88f::f0a6",
    "9:f:e::3",
    "b::fedd",
    "7b:f::c",
    "edf4:7d::88",
    "89::d",
    "c0:a:62::ac",
    "7:f::b",
    "8::a2",
    "0f::1",
    "::",
    "b:8::",
    "44:a::",
    "ef8f::",
    "b:4:d::",
    "a::",
    "5a:8::",
    "ddaf:ecbf::",
    "f:bb:a1::",
    "f8:f::",
    "::e:38:ab:f8",
    "::cd:c",
    "::aa3:eb",
    "::bf:9f9",
    "::7ef:bf8a",
    "::9",
    "::a:9af",
    "::315",
    "::a:a",
    "::aed3:a",
    "f0eb:0:e8:b:c:a:254.098.233.17",
    "bfa:7fc:c66d:15:e9a:ded:254.119.9.9",
    "d:ffa8:9:a:879:3:202.39.08.245",
    "8e:2:8:fa8a:f1d1:1aa8:252.254.245.81",
    "5:d4:a:e9:8:8:06.38.98.253",
    "9c5:4:a5c:f:a6:8c9d:05.250.8.2",
    "d19a:2:f808:be:f:c:98.86.197.249",
    "8:26ac:8:8:cb:f:242.00.254.85",
    "38:e:1:0b88:f:0:8.89.248.92",
    "e7:ff96:a:f:f:b:253.91.052.195",
    "d:8:2:5:894:5:254.0.240.199",
    "2:98:9:8aa:9c8f:fa:252.98.248.17",
    "e9:d4f:890:ccbe:5:8:088.200.228.216",
    "3:3:9:5:6a:df5:255.251.8.12",
    "0280:3:8:8:4:9:255.000.251.249",
    "8:af7:db:aa:0:9:238.248.250.255",
    "ff:ee:9a:9252:a:289:059.083.18.255",
    "9f6:5:fc9:b:a89:a:142.1.250.254",
    "e:981a:da:bf94:9:f8:254.242.18.95",
    "3c:1:4:f2:89:f:8.91.255.14",
    "e::9a2:c:9.050.80.8",
    "9::4a:07:fb:211.241.254.228",
    "9be::2:e:215.189.48.188",
    "f::f:d:069.148.99.168",
    "f::a:97.18.240.47",
    "c::a98e:1:251.253.252.254",
    "668::82:214.87.208.9",
    "9c0::cf0:ecb:253.208.238.255",
    "a::0:f1:210.240.238.049",
    "8::a:1:251.238.34.09",
    "81:dfe::b8:8.255.249.248",
    "d3::7:b:9:83.189.08.244",
    "8::9:8:8:00.7.11.252",
    "2:8::c:a8:250.221.9.249",
    "2::f:99.8.249.247",
    "c:22f5::5:2c:243.15.79.89",
    "e:8e::da:251.243.255.2",
    "f15f:9::a:255.70.247.218",
    "f:b::9f38:31.220.94.022",
    "9::9a48:03.98.249.119",
    "d:d:9b87::2d:a:249.253.38.8",
    "d86d:99b::a9b:5:242.236.8.244",
    "eb:3::f:9cf:1.253.1.228",
    "b::ba2:255.247.114.64",
    "2f:ec:bcb::9:219.254.250.094",
    "da8a:f6::a:e0:19.251.241.251",
    "5e:c1::a:021.250.8.254",
    "c:9::8c9b:248.219.212.252",
    "2:a::8d4a:216.255.198.223",
    "1f::66:255.30.08.150",
    "bc2b:8f::2ff9:6.245.99.230",
    "a:8::a8:9.251.246.255",
    "f:7:7::98:06.14.1.208",
    "e:2::9:218.249.255.254",
    "79:f::6:250.255.98.246",
    "47:9:fb9f::9:038.136.17.251",
    "ed::a:247.9.23.239",
    "6f::f1:88.254.119.9",
    "a::d:218.199.236.0",
    "fc88::9:203.196.04.95",
    "::8.048.255.85",
    "::253.07.255.36",
    "9:d::253.7.178.229",
    "::250.84.158.253",
    "::8.55.204.248",
    "2d:c::253.18.18.252",
    "df9:88ca::248.255.108.17",
    "8e9b::250.206.0.82",
    "::209.8.254.209",
    "::247.088.8.8",
    "::cb:f:ba41:250.208.19.249",
    "::fe:0e8:243.240.229.5",
    "::c:223.251.5.226",
    "::8:08.03.8.250",
    "::f:8.88.11.255",
    "::fda:48:aa:05.189.07.2",
    "::8:c3f:f:240.06.212.255",
    "::f:0aa:244.123.99.16",
    "::c9b5:c:034.8.090.196",
    "::98:c9:254.14.241.81"
  ]
  negatives = ["foo.bar",
    "::::::::::::",
    "yet another failure",
    "de:6:c:ab5:6a::9:252.06.06.249",
    "f9:5f7:fa38:9:b::b6:09.255.248.252",
    "97:c:5b:81:8a::f5dd:144.252.250.9",
    "9:8:cd:8:a9::f:247.255.09.255",
    "18:1:8c:2:3::9:8.254.252.139",
    "e:c298:3:e:a::bb12:254.246.05.250",
    "e:e:c:8e:fd::8:253.8.49.231",
    "9:97f:f:e929:8a::c9:0.8.252.10",
    "0df:b24:7:89:c::2b:16.249.240.092",
    "b:8f5f:485:c:9a::84c:178.7.249.34",
  ]

proc ok(pos: openarray[string]) =
  for p in pos:
    if not isIpAddress(p):
      echo "failure ", p

proc notok(neg: openarray[string]) =
  for n in neg:
    if isIpAddress(n):
      echo "failure ", n

ok(positives)
notok(negatives)
echo "all ok"
href='#n1064'>1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223
#
#
#           The Nimrod Compiler
#        (c) Copyright 2012 Andreas Rumpf
#
#    See the file "copying.txt", included in this
#    distribution, for details about the copyright.
#

# This module implements the renderer of the standard Nimrod representation.

import 
  lexer, options, idents, strutils, ast, msgs, lists

type 
  TRenderFlag* = enum 
    renderNone, renderNoBody, renderNoComments, renderDocComments, 
    renderNoPragmas, renderIds
  TRenderFlags* = set[TRenderFlag]
  TRenderTok*{.final.} = object 
    kind*: TTokType
    length*: int16

  TRenderTokSeq* = seq[TRenderTok]
  TSrcGen*{.final.} = object 
    indent*: int
    lineLen*: int
    pos*: int              # current position for iteration over the buffer
    idx*: int              # current token index for iteration over the buffer
    tokens*: TRenderTokSeq
    buf*: string
    pendingNL*: int        # negative if not active; else contains the
                           # indentation value
    comStack*: seq[PNode]  # comment stack
    flags*: TRenderFlags


proc renderModule*(n: PNode, filename: string, renderFlags: TRenderFlags = {})
proc renderTree*(n: PNode, renderFlags: TRenderFlags = {}): string
proc initTokRender*(r: var TSrcGen, n: PNode, renderFlags: TRenderFlags = {})
proc getNextTok*(r: var TSrcGen, kind: var TTokType, literal: var string)
# implementation
# We render the source code in a two phases: The first
# determines how long the subtree will likely be, the second
# phase appends to a buffer that will be the output.

proc isKeyword*(s: string): bool =
  var i = getIdent(s)
  if (i.id >= ord(tokKeywordLow) - ord(tkSymbol)) and
      (i.id <= ord(tokKeywordHigh) - ord(tkSymbol)): 
    result = true

proc renderDefinitionName*(s: PSym): string =
  let x = s.name.s
  if x[0] in SymStartChars and not renderer.isKeyword(x): result = x
  else: result = '`' & x & '`'

const 
  IndentWidth = 2
  longIndentWid = 4
  MaxLineLen = 80
  LineCommentColumn = 30

proc InitSrcGen(g: var TSrcGen, renderFlags: TRenderFlags) = 
  g.comStack = @[]
  g.tokens = @[]
  g.indent = 0
  g.lineLen = 0
  g.pos = 0
  g.idx = 0
  g.buf = ""
  g.flags = renderFlags
  g.pendingNL = - 1

proc addTok(g: var TSrcGen, kind: TTokType, s: string) = 
  var length = len(g.tokens)
  setlen(g.tokens, length + 1)
  g.tokens[length].kind = kind
  g.tokens[length].length = int16(len(s))
  add(g.buf, s)

proc addPendingNL(g: var TSrcGen) = 
  if g.pendingNL >= 0: 
    addTok(g, tkInd, "\n" & repeatChar(g.pendingNL))
    g.lineLen = g.pendingNL
    g.pendingNL = - 1

proc putNL(g: var TSrcGen, indent: int) = 
  if g.pendingNL >= 0: addPendingNL(g)
  else: addTok(g, tkInd, "\n")
  g.pendingNL = indent
  g.lineLen = indent

proc putNL(g: var TSrcGen) = 
  putNL(g, g.indent)

proc optNL(g: var TSrcGen, indent: int) = 
  g.pendingNL = indent
  g.lineLen = indent          # BUGFIX
  
proc optNL(g: var TSrcGen) = 
  optNL(g, g.indent)

proc indentNL(g: var TSrcGen) = 
  inc(g.indent, indentWidth)
  g.pendingNL = g.indent
  g.lineLen = g.indent

proc Dedent(g: var TSrcGen) = 
  dec(g.indent, indentWidth)
  assert(g.indent >= 0)
  if g.pendingNL > indentWidth: 
    Dec(g.pendingNL, indentWidth)
    Dec(g.lineLen, indentWidth)

proc put(g: var TSrcGen, kind: TTokType, s: string) = 
  addPendingNL(g)
  if len(s) > 0: 
    addTok(g, kind, s)
    inc(g.lineLen, len(s))

proc putLong(g: var TSrcGen, kind: TTokType, s: string, lineLen: int) = 
  # use this for tokens over multiple lines.
  addPendingNL(g)
  addTok(g, kind, s)
  g.lineLen = lineLen

proc toNimChar(c: Char): string = 
  case c
  of '\0': result = "\\0"
  of '\x01'..'\x1F', '\x80'..'\xFF': result = "\\x" & strutils.toHex(ord(c), 2)
  of '\'', '\"', '\\': result = '\\' & c
  else: result = c & ""
  
proc makeNimString(s: string): string = 
  result = "\""
  for i in countup(0, len(s)-1): add(result, toNimChar(s[i]))
  add(result, '\"')

proc putComment(g: var TSrcGen, s: string) = 
  var i = 0
  var comIndent = 1
  var isCode = (len(s) >= 2) and (s[1] != ' ')
  var ind = g.lineLen
  var com = ""
  while true: 
    case s[i]
    of '\0': 
      break 
    of '\x0D': 
      put(g, tkComment, com)
      com = ""
      inc(i)
      if s[i] == '\x0A': inc(i)
      optNL(g, ind)
    of '\x0A': 
      put(g, tkComment, com)
      com = ""
      inc(i)
      optNL(g, ind)
    of '#': 
      add(com, s[i])
      inc(i)
      comIndent = 0
      while s[i] == ' ': 
        add(com, s[i])
        inc(i)
        inc(comIndent)
    of ' ', '\x09': 
      add(com, s[i])
      inc(i)
    else: 
      # we may break the comment into a multi-line comment if the line
      # gets too long:
      # compute length of the following word:
      var j = i
      while s[j] > ' ': inc(j)
      if not isCode and (g.lineLen + (j - i) > MaxLineLen): 
        put(g, tkComment, com)
        optNL(g, ind)
        com = '#' & repeatChar(comIndent)
      while s[i] > ' ': 
        add(com, s[i])
        inc(i)
  put(g, tkComment, com)
  optNL(g)

proc maxLineLength(s: string): int = 
  result = 0
  var i = 0
  var lineLen = 0
  while true: 
    case s[i]
    of '\0': 
      break 
    of '\x0D': 
      inc(i)
      if s[i] == '\x0A': inc(i)
      result = max(result, lineLen)
      lineLen = 0
    of '\x0A': 
      inc(i)
      result = max(result, lineLen)
      lineLen = 0
    else: 
      inc(lineLen)
      inc(i)

proc putRawStr(g: var TSrcGen, kind: TTokType, s: string) = 
  var i = 0
  var hi = len(s) - 1
  var str = ""
  while i <= hi: 
    case s[i]
    of '\x0D': 
      put(g, kind, str)
      str = ""
      inc(i)
      if (i <= hi) and (s[i] == '\x0A'): inc(i)
      optNL(g, 0)
    of '\x0A': 
      put(g, kind, str)
      str = ""
      inc(i)
      optNL(g, 0)
    else: 
      add(str, s[i])
      inc(i)
  put(g, kind, str)

proc containsNL(s: string): bool = 
  for i in countup(0, len(s) - 1): 
    case s[i]
    of '\x0D', '\x0A': 
      return true
    else: 
      nil
  result = false

proc pushCom(g: var TSrcGen, n: PNode) = 
  var length = len(g.comStack)
  setlen(g.comStack, length + 1)
  g.comStack[length] = n

proc popAllComs(g: var TSrcGen) = 
  setlen(g.comStack, 0)

proc popCom(g: var TSrcGen) = 
  setlen(g.comStack, len(g.comStack) - 1)

const 
  Space = " "

proc shouldRenderComment(g: var TSrcGen, n: PNode): bool = 
  result = false
  if n.comment != nil: 
    result = (renderNoComments notin g.flags) or
        (renderDocComments in g.flags) and startsWith(n.comment, "##")
  
proc gcom(g: var TSrcGen, n: PNode) = 
  assert(n != nil)
  if shouldRenderComment(g, n): 
    if (g.pendingNL < 0) and (len(g.buf) > 0) and (g.buf[len(g.buf)-1] != ' '):
      put(g, tkSpaces, Space) 
      # Before long comments we cannot make sure that a newline is generated,
      # because this might be wrong. But it is no problem in practice.
    if (g.pendingNL < 0) and (len(g.buf) > 0) and
        (g.lineLen < LineCommentColumn): 
      var ml = maxLineLength(n.comment)
      if ml + LineCommentColumn <= maxLineLen: 
        put(g, tkSpaces, repeatChar(LineCommentColumn - g.lineLen))
    putComment(g, n.comment)  #assert(g.comStack[high(g.comStack)] = n);
  
proc gcoms(g: var TSrcGen) = 
  for i in countup(0, high(g.comStack)): gcom(g, g.comStack[i])
  popAllComs(g)

proc lsub(n: PNode): int
proc litAux(n: PNode, x: biggestInt, size: int): string =
  proc skip(t: PType): PType = 
    result = t
    while result.kind in {tyGenericInst, tyRange, tyVar, tyDistinct, tyOrdinal,
                          tyConst, tyMutable}:
      result = lastSon(result)
  if n.typ != nil and n.typ.skip.kind in {tyBool, tyEnum}:
    let enumfields = n.typ.skip.n
    # we need a slow linear search because of enums with holes:
    for e in items(enumfields):
      if e.sym.position == x: return e.sym.name.s
    
  if nfBase2 in n.flags: result = "0b" & toBin(x, size * 8)
  elif nfBase8 in n.flags: result = "0o" & toOct(x, size * 3)
  elif nfBase16 in n.flags: result = "0x" & toHex(x, size * 2)
  else: result = $x

proc ulitAux(n: PNode, x: biggestInt, size: int): string = 
  if nfBase2 in n.flags: result = "0b" & toBin(x, size * 8)
  elif nfBase8 in n.flags: result = "0o" & toOct(x, size * 3)
  elif nfBase16 in n.flags: result = "0x" & toHex(x, size * 2)
  else: result = $x
  # XXX proper unsigned output!
  
proc atom(n: PNode): string = 
  var f: float32
  case n.kind
  of nkEmpty: result = ""
  of nkIdent: result = n.ident.s
  of nkSym: result = n.sym.name.s
  of nkStrLit: result = makeNimString(n.strVal)
  of nkRStrLit: result = "r\"" & replace(n.strVal, "\"", "\"\"")  & '\"'
  of nkTripleStrLit: result = "\"\"\"" & n.strVal & "\"\"\""
  of nkCharLit: result = '\'' & toNimChar(chr(int(n.intVal))) & '\''
  of nkIntLit: result = litAux(n, n.intVal, 4)
  of nkInt8Lit: result = litAux(n, n.intVal, 1) & "\'i8"
  of nkInt16Lit: result = litAux(n, n.intVal, 2) & "\'i16"
  of nkInt32Lit: result = litAux(n, n.intVal, 4) & "\'i32"
  of nkInt64Lit: result = litAux(n, n.intVal, 8) & "\'i64"
  of nkUIntLit: result = ulitAux(n, n.intVal, 4) & "\'u"
  of nkUInt8Lit: result = ulitAux(n, n.intVal, 1) & "\'u8"
  of nkUInt16Lit: result = ulitAux(n, n.intVal, 2) & "\'u16"
  of nkUInt32Lit: result = ulitAux(n, n.intVal, 4) & "\'u32"
  of nkUInt64Lit: result = ulitAux(n, n.intVal, 8) & "\'u64"
  of nkFloatLit:
    if n.flags * {nfBase2, nfBase8, nfBase16} == {}: result = $(n.floatVal)
    else: result = litAux(n, (cast[PInt64](addr(n.floatVal)))[] , 8)
  of nkFloat32Lit: 
    if n.flags * {nfBase2, nfBase8, nfBase16} == {}: 
      result = $n.floatVal & "\'f32"
    else: 
      f = n.floatVal
      result = litAux(n, (cast[PInt32](addr(f)))[], 4) & "\'f32"
  of nkFloat64Lit: 
    if n.flags * {nfBase2, nfBase8, nfBase16} == {}: 
      result = $n.floatVal & "\'f64"
    else: 
      result = litAux(n, (cast[PInt64](addr(n.floatVal)))[], 8) & "\'f64"
  of nkNilLit: result = "nil"
  of nkType: 
    if (n.typ != nil) and (n.typ.sym != nil): result = n.typ.sym.name.s
    else: result = "[type node]"
  else: InternalError("rnimsyn.atom " & $n.kind)
  
proc lcomma(n: PNode, start: int = 0, theEnd: int = - 1): int = 
  assert(theEnd < 0)
  result = 0
  for i in countup(start, sonsLen(n) + theEnd): 
    inc(result, lsub(n.sons[i]))
    inc(result, 2)            # for ``, ``
  if result > 0: 
    dec(result, 2)            # last does not get a comma!
  
proc lsons(n: PNode, start: int = 0, theEnd: int = - 1): int = 
  assert(theEnd < 0)
  result = 0
  for i in countup(start, sonsLen(n) + theEnd): inc(result, lsub(n.sons[i]))
  
proc lsub(n: PNode): int = 
  # computes the length of a tree
  if isNil(n): return 0
  if n.comment != nil: return maxLineLen + 1
  case n.kind
  of nkEmpty: result = 0
  of nkTripleStrLit: 
    if containsNL(n.strVal): result = maxLineLen + 1
    else: result = len(atom(n))
  of succ(nkEmpty)..pred(nkTripleStrLit), succ(nkTripleStrLit)..nkNilLit: 
    result = len(atom(n))
  of nkCall, nkBracketExpr, nkCurlyExpr, nkConv:
    result = lsub(n.sons[0]) + lcomma(n, 1) + 2
  of nkHiddenStdConv, nkHiddenSubConv, nkHiddenCallConv: result = lsub(n[1])
  of nkCast: result = lsub(n.sons[0]) + lsub(n.sons[1]) + len("cast[]()")
  of nkAddr: result = lsub(n.sons[0]) + len("addr()")
  of nkStaticExpr: result = lsub(n.sons[0]) + len("static_")
  of nkHiddenAddr, nkHiddenDeref: result = lsub(n.sons[0])
  of nkCommand: result = lsub(n.sons[0]) + lcomma(n, 1) + 1
  of nkExprEqExpr, nkAsgn, nkFastAsgn: result = lsons(n) + 3
  of nkPar, nkCurly, nkBracket, nkClosure: result = lcomma(n) + 2
  of nkTableConstr:
    result = if n.len > 0: lcomma(n) + 2 else: len("{:}")
  of nkSymChoice: result = lsons(n) + len("()") + sonsLen(n) - 1
  of nkTupleTy: result = lcomma(n) + len("tuple[]")
  of nkDotExpr: result = lsons(n) + 1
  of nkBind: result = lsons(n) + len("bind_")
  of nkBindStmt: result = lcomma(n) + len("bind_")
  of nkCheckedFieldExpr: result = lsub(n.sons[0])
  of nkLambda: result = lsons(n) + len("proc__=_")
  of nkDo: result = lsons(n) + len("do__:_")
  of nkConstDef, nkIdentDefs: 
    result = lcomma(n, 0, - 3)
    var L = sonsLen(n)
    if n.sons[L - 2].kind != nkEmpty: result = result + lsub(n.sons[L - 2]) + 2
    if n.sons[L - 1].kind != nkEmpty: result = result + lsub(n.sons[L - 1]) + 3
  of nkVarTuple: result = lcomma(n, 0, - 3) + len("() = ") + lsub(lastSon(n))
  of nkChckRangeF: result = len("chckRangeF") + 2 + lcomma(n)
  of nkChckRange64: result = len("chckRange64") + 2 + lcomma(n)
  of nkChckRange: result = len("chckRange") + 2 + lcomma(n)
  of nkObjDownConv, nkObjUpConv, nkStringToCString, nkCStringToString: 
    result = 2
    if sonsLen(n) >= 1: result = result + lsub(n.sons[0])
    result = result + lcomma(n, 1)
  of nkExprColonExpr: result = lsons(n) + 2
  of nkInfix: result = lsons(n) + 2
  of nkPrefix: result = lsons(n) + 1
  of nkPostfix: result = lsons(n)
  of nkCallStrLit: result = lsons(n)
  of nkPragmaExpr: result = lsub(n.sons[0]) + lcomma(n, 1)
  of nkRange: result = lsons(n) + 2
  of nkDerefExpr: result = lsub(n.sons[0]) + 2
  of nkAccQuoted: result = lsons(n) + 2
  of nkIfExpr: 
    result = lsub(n.sons[0].sons[0]) + lsub(n.sons[0].sons[1]) + lsons(n, 1) +
        len("if_:_")
  of nkElifExpr: result = lsons(n) + len("_elif_:_")
  of nkElseExpr: result = lsub(n.sons[0]) + len("_else:_") # type descriptions
  of nkTypeOfExpr: result = lsub(n.sons[0]) + len("type_")
  of nkRefTy: result = lsub(n.sons[0]) + len("ref_")
  of nkPtrTy: result = lsub(n.sons[0]) + len("ptr_")
  of nkVarTy: result = lsub(n.sons[0]) + len("var_")
  of nkDistinctTy: result = lsub(n.sons[0]) + len("Distinct_")
  of nkTypeDef: result = lsons(n) + 3
  of nkOfInherit: result = lsub(n.sons[0]) + len("of_")
  of nkProcTy: result = lsons(n) + len("proc_")
  of nkEnumTy: 
    if sonsLen(n) > 0:
      result = lsub(n.sons[0]) + lcomma(n, 1) + len("enum_")
    else:
      result = len("enum")
  of nkEnumFieldDef: result = lsons(n) + 3
  of nkVarSection, nkLetSection: 
    if sonsLen(n) > 1: result = maxLineLen + 1
    else: result = lsons(n) + len("var_")
  of nkReturnStmt: result = lsub(n.sons[0]) + len("return_")
  of nkRaiseStmt: result = lsub(n.sons[0]) + len("raise_")
  of nkYieldStmt: result = lsub(n.sons[0]) + len("yield_")
  of nkDiscardStmt: result = lsub(n.sons[0]) + len("discard_")
  of nkBreakStmt: result = lsub(n.sons[0]) + len("break_")
  of nkContinueStmt: result = lsub(n.sons[0]) + len("continue_")
  of nkPragma: result = lcomma(n) + 4
  of nkCommentStmt: result = len(n.comment)
  of nkOfBranch: result = lcomma(n, 0, - 2) + lsub(lastSon(n)) + len("of_:_")
  of nkElifBranch: result = lsons(n) + len("elif_:_")
  of nkElse: result = lsub(n.sons[0]) + len("else:_")
  of nkFinally: result = lsub(n.sons[0]) + len("finally:_")
  of nkGenericParams: result = lcomma(n) + 2
  of nkFormalParams: 
    result = lcomma(n, 1) + 2
    if n.sons[0].kind != nkEmpty: result = result + lsub(n.sons[0]) + 2
  of nkExceptBranch: 
    result = lcomma(n, 0, -2) + lsub(lastSon(n)) + len("except_:_")
  else: result = maxLineLen + 1
  
proc fits(g: TSrcGen, x: int): bool = 
  result = x + g.lineLen <= maxLineLen

type 
  TSubFlag = enum 
    rfLongMode, rfNoIndent, rfInConstExpr
  TSubFlags = set[TSubFlag]
  TContext = tuple[spacing: int, flags: TSubFlags]

const 
  emptyContext: TContext = (spacing: 0, flags: {})

proc initContext(c: var TContext) = 
  c.spacing = 0
  c.flags = {}

proc gsub(g: var TSrcGen, n: PNode, c: TContext)
proc gsub(g: var TSrcGen, n: PNode) = 
  var c: TContext
  initContext(c)
  gsub(g, n, c)

proc hasCom(n: PNode): bool = 
  result = false
  if n.comment != nil: return true
  case n.kind
  of nkEmpty..nkNilLit: nil
  else: 
    for i in countup(0, sonsLen(n) - 1): 
      if hasCom(n.sons[i]): return true
  
proc putWithSpace(g: var TSrcGen, kind: TTokType, s: string) = 
  put(g, kind, s)
  put(g, tkSpaces, Space)

proc gcommaAux(g: var TSrcGen, n: PNode, ind: int, start: int = 0, 
               theEnd: int = - 1, separator = tkComma) = 
  for i in countup(start, sonsLen(n) + theEnd): 
    var c = i < sonsLen(n) + theEnd
    var sublen = lsub(n.sons[i]) + ord(c)
    if not fits(g, sublen) and (ind + sublen < maxLineLen): optNL(g, ind)
    gsub(g, n.sons[i])
    if c: 
      putWithSpace(g, separator, TokTypeToStr[separator])
      if hasCom(n.sons[i]): 
        gcoms(g)
        optNL(g, ind)

proc gcomma(g: var TSrcGen, n: PNode, c: TContext, start: int = 0, 
            theEnd: int = - 1) = 
  var ind: int
  if rfInConstExpr in c.flags: 
    ind = g.indent + indentWidth
  else: 
    ind = g.lineLen
    if ind > maxLineLen div 2: ind = g.indent + longIndentWid
  gcommaAux(g, n, ind, start, theEnd)

proc gcomma(g: var TSrcGen, n: PNode, start: int = 0, theEnd: int = - 1) = 
  var ind = g.lineLen
  if ind > maxLineLen div 2: ind = g.indent + longIndentWid
  gcommaAux(g, n, ind, start, theEnd)

proc gsemicolon(g: var TSrcGen, n: PNode, start: int = 0, theEnd: int = - 1) = 
  var ind = g.lineLen
  if ind > maxLineLen div 2: ind = g.indent + longIndentWid
  gcommaAux(g, n, ind, start, theEnd, tkSemicolon)

proc gsons(g: var TSrcGen, n: PNode, c: TContext, start: int = 0, 
           theEnd: int = - 1) = 
  for i in countup(start, sonsLen(n) + theEnd): gsub(g, n.sons[i], c)

proc gsection(g: var TSrcGen, n: PNode, c: TContext, kind: TTokType, 
              k: string) = 
  if sonsLen(n) == 0: return # empty var sections are possible
  putWithSpace(g, kind, k)
  gcoms(g)
  indentNL(g)
  for i in countup(0, sonsLen(n) - 1): 
    optNL(g)
    gsub(g, n.sons[i], c)
    gcoms(g)
  dedent(g)

proc longMode(n: PNode, start: int = 0, theEnd: int = - 1): bool = 
  result = n.comment != nil
  if not result: 
    # check further
    for i in countup(start, sonsLen(n) + theEnd): 
      if (lsub(n.sons[i]) > maxLineLen): 
        result = true
        break 

proc gstmts(g: var TSrcGen, n: PNode, c: TContext) = 
  if n.kind == nkEmpty: return 
  if (n.kind == nkStmtList) or (n.kind == nkStmtListExpr): 
    indentNL(g)
    for i in countup(0, sonsLen(n) - 1): 
      optNL(g)
      gsub(g, n.sons[i])
      gcoms(g)
    dedent(g)
  else: 
    if rfLongMode in c.flags: indentNL(g)
    gsub(g, n)
    gcoms(g)
    optNL(g)
    if rfLongMode in c.flags: dedent(g)
  
proc gif(g: var TSrcGen, n: PNode) = 
  var c: TContext
  gsub(g, n.sons[0].sons[0])
  initContext(c)
  putWithSpace(g, tkColon, ":")
  if longMode(n) or (lsub(n.sons[0].sons[1]) + g.lineLen > maxLineLen): 
    incl(c.flags, rfLongMode)
  gcoms(g)                    # a good place for comments
  gstmts(g, n.sons[0].sons[1], c)
  var length = sonsLen(n)
  for i in countup(1, length - 1): 
    optNL(g)
    gsub(g, n.sons[i], c)

proc gwhile(g: var TSrcGen, n: PNode) = 
  var c: TContext
  putWithSpace(g, tkWhile, "while")
  gsub(g, n.sons[0])
  putWithSpace(g, tkColon, ":")
  initContext(c)
  if longMode(n) or (lsub(n.sons[1]) + g.lineLen > maxLineLen): 
    incl(c.flags, rfLongMode)
  gcoms(g)                    # a good place for comments
  gstmts(g, n.sons[1], c)

proc gpragmaBlock(g: var TSrcGen, n: PNode) = 
  var c: TContext
  gsub(g, n.sons[0])
  putWithSpace(g, tkColon, ":")
  initContext(c)
  if longMode(n) or (lsub(n.sons[1]) + g.lineLen > maxLineLen):
    incl(c.flags, rfLongMode)
  gcoms(g)                    # a good place for comments
  gstmts(g, n.sons[1], c)

proc gtry(g: var TSrcGen, n: PNode) = 
  var c: TContext
  put(g, tkTry, "try")
  putWithSpace(g, tkColon, ":")
  initContext(c)
  if longMode(n) or (lsub(n.sons[0]) + g.lineLen > maxLineLen): 
    incl(c.flags, rfLongMode)
  gcoms(g)                    # a good place for comments
  gstmts(g, n.sons[0], c)
  gsons(g, n, c, 1)

proc gfor(g: var TSrcGen, n: PNode) = 
  var c: TContext
  var length = sonsLen(n)
  putWithSpace(g, tkFor, "for")
  initContext(c)
  if longMode(n) or
      (lsub(n.sons[length - 1]) + lsub(n.sons[length - 2]) + 6 + g.lineLen >
      maxLineLen): 
    incl(c.flags, rfLongMode)
  gcomma(g, n, c, 0, - 3)
  put(g, tkSpaces, Space)
  putWithSpace(g, tkIn, "in")
  gsub(g, n.sons[length - 2], c)
  putWithSpace(g, tkColon, ":")
  gcoms(g)
  gstmts(g, n.sons[length - 1], c)

proc gmacro(g: var TSrcGen, n: PNode) = 
  var c: TContext
  initContext(c)
  gsub(g, n.sons[0])
  putWithSpace(g, tkColon, ":")
  if longMode(n) or (lsub(n.sons[1]) + g.lineLen > maxLineLen): 
    incl(c.flags, rfLongMode)
  gcoms(g)
  gsons(g, n, c, 1)

proc gcase(g: var TSrcGen, n: PNode) = 
  var c: TContext
  initContext(c)
  var length = sonsLen(n)
  var last = if n.sons[length-1].kind == nkElse: -2 else: -1
  if longMode(n, 0, last): incl(c.flags, rfLongMode)
  putWithSpace(g, tkCase, "case")
  gsub(g, n.sons[0])
  gcoms(g)
  optNL(g)
  gsons(g, n, c, 1, last)
  if last == - 2: 
    initContext(c)
    if longMode(n.sons[length - 1]): incl(c.flags, rfLongMode)
    gsub(g, n.sons[length - 1], c)

proc gproc(g: var TSrcGen, n: PNode) = 
  var c: TContext
  if n.sons[namePos].kind == nkSym:
    put(g, tkSymbol, renderDefinitionName(n.sons[namePos].sym))
  else:
    gsub(g, n.sons[namePos])
  gsub(g, n.sons[1])
  gsub(g, n.sons[2])
  gsub(g, n.sons[3])
  if renderNoBody notin g.flags:
    if n.sons[4].kind != nkEmpty: 
      put(g, tkSpaces, Space)
      putWithSpace(g, tkEquals, "=")
      indentNL(g)
      gcoms(g)
      dedent(g)
      initContext(c)
      gstmts(g, n.sons[4], c)
      putNL(g)
    else: 
      indentNL(g)
      gcoms(g)
      dedent(g)

proc gblock(g: var TSrcGen, n: PNode) = 
  var c: TContext
  initContext(c)
  if n.sons[0].kind != nkEmpty:
    putWithSpace(g, tkBlock, "block")
    gsub(g, n.sons[0])
  else:
    put(g, tkBlock, "block")
  putWithSpace(g, tkColon, ":")
  if longMode(n) or (lsub(n.sons[1]) + g.lineLen > maxLineLen): 
    incl(c.flags, rfLongMode)
  gcoms(g)
  # XXX I don't get why this is needed here! gstmts should already handle this!
  indentNL(g)
  gstmts(g, n.sons[1], c)
  dedent(g)

proc gstaticStmt(g: var TSrcGen, n: PNode) = 
  var c: TContext
  putWithSpace(g, tkStatic, "static")
  putWithSpace(g, tkColon, ":")
  initContext(c)
  if longMode(n) or (lsub(n.sons[0]) + g.lineLen > maxLineLen): 
    incl(c.flags, rfLongMode)
  gcoms(g)                    # a good place for comments
  gstmts(g, n.sons[0], c)

proc gasm(g: var TSrcGen, n: PNode) = 
  putWithSpace(g, tkAsm, "asm")
  gsub(g, n.sons[0])
  gcoms(g)
  gsub(g, n.sons[1])

proc gident(g: var TSrcGen, n: PNode) = 
  var t: TTokType
  var s = atom(n)
  if (s[0] in lexer.SymChars): 
    if (n.kind == nkIdent): 
      if (n.ident.id < ord(tokKeywordLow) - ord(tkSymbol)) or
          (n.ident.id > ord(tokKeywordHigh) - ord(tkSymbol)): 
        t = tkSymbol
      else: 
        t = TTokType(n.ident.id + ord(tkSymbol))
    else: 
      t = tkSymbol
  else: 
    t = tkOpr
  put(g, t, s)
  if n.kind == nkSym and renderIds in g.flags: put(g, tkIntLit, $n.sym.id)

proc doParamsAux(g: var TSrcGen, params: PNode) =
  if params.len > 1:
    put(g, tkParLe, "(")
    gsemicolon(g, params, 1)
    put(g, tkParRi, ")")
  
  if params.sons[0].kind != nkEmpty: 
    putWithSpace(g, tkOpr, "->")
    gsub(g, params.sons[0])

proc gsub(g: var TSrcGen, n: PNode, c: TContext) = 
  if isNil(n): return
  var 
    L: int
    a: TContext
  if n.comment != nil: pushCom(g, n)
  case n.kind                 # atoms:
  of nkTripleStrLit: putRawStr(g, tkTripleStrLit, n.strVal)
  of nkEmpty: nil
  of nkType: put(g, tkInvalid, atom(n))
  of nkSym, nkIdent: gident(g, n)
  of nkIntLit: put(g, tkIntLit, atom(n))
  of nkInt8Lit: put(g, tkInt8Lit, atom(n))
  of nkInt16Lit: put(g, tkInt16Lit, atom(n))
  of nkInt32Lit: put(g, tkInt32Lit, atom(n))
  of nkInt64Lit: put(g, tkInt64Lit, atom(n))
  of nkUIntLit: put(g, tkUIntLit, atom(n))
  of nkUInt8Lit: put(g, tkUInt8Lit, atom(n))
  of nkUInt16Lit: put(g, tkUInt16Lit, atom(n))
  of nkUInt32Lit: put(g, tkUInt32Lit, atom(n))
  of nkUInt64Lit: put(g, tkUInt64Lit, atom(n))
  of nkFloatLit: put(g, tkFloatLit, atom(n))
  of nkFloat32Lit: put(g, tkFloat32Lit, atom(n))
  of nkFloat64Lit: put(g, tkFloat64Lit, atom(n))
  of nkFloat128Lit: put(g, tkFloat128Lit, atom(n))
  of nkStrLit: put(g, tkStrLit, atom(n))
  of nkRStrLit: put(g, tkRStrLit, atom(n))
  of nkCharLit: put(g, tkCharLit, atom(n))
  of nkNilLit: put(g, tkNil, atom(n))    # complex expressions
  of nkCall, nkConv, nkDotCall: 
    if sonsLen(n) >= 1: gsub(g, n.sons[0])
    put(g, tkParLe, "(")
    gcomma(g, n, 1)
    put(g, tkParRi, ")")
  of nkCallStrLit: 
    gsub(g, n.sons[0])
    if n.sons[1].kind == nkRStrLit: 
      put(g, tkRStrLit, '\"' & replace(n[1].strVal, "\"", "\"\"") & '\"')
    else: 
      gsub(g, n.sons[1])
  of nkHiddenStdConv, nkHiddenSubConv, nkHiddenCallConv: gsub(g, n.sons[0])
  of nkCast: 
    put(g, tkCast, "cast")
    put(g, tkBracketLe, "[")
    gsub(g, n.sons[0])
    put(g, tkBracketRi, "]")
    put(g, tkParLe, "(")
    gsub(g, n.sons[1])
    put(g, tkParRi, ")")
  of nkAddr: 
    put(g, tkAddr, "addr")
    put(g, tkParLe, "(")
    gsub(g, n.sons[0])
    put(g, tkParRi, ")")
  of nkStaticExpr:
    put(g, tkStatic, "static")
    put(g, tkSpaces, space)
    gsub(g, n.sons[0])
  of nkBracketExpr: 
    gsub(g, n.sons[0])
    put(g, tkBracketLe, "[")
    gcomma(g, n, 1)
    put(g, tkBracketRi, "]")
  of nkCurlyExpr:
    gsub(g, n.sons[0])
    put(g, tkCurlyLe, "{")
    gcomma(g, n, 1)
    put(g, tkCurlyRi, "}")
  of nkPragmaExpr: 
    gsub(g, n.sons[0])
    gcomma(g, n, 1)
  of nkCommand: 
    gsub(g, n.sons[0])
    put(g, tkSpaces, space)
    gcomma(g, n, 1)
  of nkExprEqExpr, nkAsgn, nkFastAsgn: 
    gsub(g, n.sons[0])
    put(g, tkSpaces, Space)
    putWithSpace(g, tkEquals, "=")
    gsub(g, n.sons[1])
  of nkChckRangeF: 
    put(g, tkSymbol, "chckRangeF")
    put(g, tkParLe, "(")
    gcomma(g, n)
    put(g, tkParRi, ")")
  of nkChckRange64: 
    put(g, tkSymbol, "chckRange64")
    put(g, tkParLe, "(")
    gcomma(g, n)
    put(g, tkParRi, ")")
  of nkChckRange: 
    put(g, tkSymbol, "chckRange")
    put(g, tkParLe, "(")
    gcomma(g, n)
    put(g, tkParRi, ")")
  of nkObjDownConv, nkObjUpConv, nkStringToCString, nkCStringToString: 
    if sonsLen(n) >= 1: gsub(g, n.sons[0])
    put(g, tkParLe, "(")
    gcomma(g, n, 1)
    put(g, tkParRi, ")")
  of nkSymChoice: 
    put(g, tkParLe, "(")
    for i in countup(0, sonsLen(n) - 1): 
      if i > 0: put(g, tkOpr, "|")
      gsub(g, n.sons[i], c)
    put(g, tkParRi, ")")
  of nkPar, nkClosure: 
    put(g, tkParLe, "(")
    gcomma(g, n, c)
    put(g, tkParRi, ")")
  of nkCurly: 
    put(g, tkCurlyLe, "{")
    gcomma(g, n, c)
    put(g, tkCurlyRi, "}")
  of nkTableConstr:
    put(g, tkCurlyLe, "{")
    if n.len > 0: gcomma(g, n, c)
    else: put(g, tkColon, ":")
    put(g, tkCurlyRi, "}")
  of nkBracket:
    put(g, tkBracketLe, "[")
    gcomma(g, n, c)
    put(g, tkBracketRi, "]")
  of nkDotExpr: 
    gsub(g, n.sons[0])
    put(g, tkDot, ".")
    gsub(g, n.sons[1])
  of nkBind: 
    putWithSpace(g, tkBind, "bind")
    gsub(g, n.sons[0])
  of nkCheckedFieldExpr, nkHiddenAddr, nkHiddenDeref: 
    gsub(g, n.sons[0])
  of nkLambda:
    putWithSpace(g, tkLambda, "proc")
    gsub(g, n.sons[paramsPos])
    gsub(g, n.sons[pragmasPos])
    put(g, tkSpaces, Space)
    putWithSpace(g, tkEquals, "=")
    gsub(g, n.sons[bodyPos])
  of nkDo:
    putWithSpace(g, tkDo, "do")
    doParamsAux(g, n.sons[paramsPos])
    gsub(g, n.sons[pragmasPos])
    put(g, tkColon, ":")
    gsub(g, n.sons[bodyPos])
  of nkConstDef, nkIdentDefs: 
    gcomma(g, n, 0, - 3)
    var L = sonsLen(n)
    if n.sons[L - 2].kind != nkEmpty: 
      putWithSpace(g, tkColon, ":")
      gsub(g, n.sons[L - 2])
    if n.sons[L - 1].kind != nkEmpty: 
      put(g, tkSpaces, Space)
      putWithSpace(g, tkEquals, "=")
      gsub(g, n.sons[L - 1], c)
  of nkVarTuple: 
    put(g, tkParLe, "(")
    gcomma(g, n, 0, - 3)
    put(g, tkParRi, ")")
    put(g, tkSpaces, Space)
    putWithSpace(g, tkEquals, "=")
    gsub(g, lastSon(n), c)
  of nkExprColonExpr: 
    gsub(g, n.sons[0])
    putWithSpace(g, tkColon, ":")
    gsub(g, n.sons[1])
  of nkInfix: 
    gsub(g, n.sons[1])
    put(g, tkSpaces, Space)
    gsub(g, n.sons[0])        # binary operator
    if not fits(g, lsub(n.sons[2]) + lsub(n.sons[0]) + 1): 
      optNL(g, g.indent + longIndentWid)
    else: 
      put(g, tkSpaces, Space)
    gsub(g, n.sons[2])
  of nkPrefix: 
    gsub(g, n.sons[0])
    put(g, tkSpaces, space)
    gsub(g, n.sons[1])
  of nkPostfix: 
    gsub(g, n.sons[1])
    gsub(g, n.sons[0])
  of nkRange: 
    gsub(g, n.sons[0])
    put(g, tkDotDot, "..")
    gsub(g, n.sons[1])
  of nkDerefExpr: 
    gsub(g, n.sons[0])
    putWithSpace(g, tkOpr, "^") 
    # unfortunately this requires a space, because ^. would be only one opr
  of nkAccQuoted:
    put(g, tkAccent, "`")
    if n.len > 0: gsub(g, n.sons[0])
    for i in 1 .. <n.len:
      put(g, tkSpaces, Space)
      gsub(g, n.sons[i])
    put(g, tkAccent, "`")
  of nkIfExpr: 
    putWithSpace(g, tkIf, "if")
    gsub(g, n.sons[0].sons[0])
    putWithSpace(g, tkColon, ":")
    gsub(g, n.sons[0].sons[1])
    gsons(g, n, emptyContext, 1)
  of nkElifExpr: 
    putWithSpace(g, tkElif, " elif")
    gsub(g, n.sons[0])
    putWithSpace(g, tkColon, ":")
    gsub(g, n.sons[1])
  of nkElseExpr: 
    put(g, tkElse, " else")
    putWithSpace(g, tkColon, ":")
    gsub(g, n.sons[0])
  of nkTypeOfExpr: 
    putWithSpace(g, tkType, "type")
    gsub(g, n.sons[0])
  of nkRefTy: 
    if sonsLen(n) > 0:
      putWithSpace(g, tkRef, "ref")
      gsub(g, n.sons[0])
    else:
      put(g, tkRef, "ref")
  of nkPtrTy: 
    if sonsLen(n) > 0:
      putWithSpace(g, tkPtr, "ptr")
      gsub(g, n.sons[0])
    else:
      put(g, tkPtr, "ptr")
  of nkVarTy: 
    if sonsLen(n) > 0:
      putWithSpace(g, tkVar, "var")
      gsub(g, n.sons[0])
    else:
      put(g, tkVar, "var")
  of nkDistinctTy: 
    if sonsLen(n) > 0:
      putWithSpace(g, tkDistinct, "distinct")
      gsub(g, n.sons[0])
    else:
      put(g, tkDistinct, "distinct")
  of nkTypeDef: 
    gsub(g, n.sons[0])
    gsub(g, n.sons[1])
    put(g, tkSpaces, Space)
    if n.sons[2].kind != nkEmpty: 
      putWithSpace(g, tkEquals, "=")
      gsub(g, n.sons[2])
  of nkObjectTy: 
    if sonsLen(n) > 0:
      putWithSpace(g, tkObject, "object")
      gsub(g, n.sons[0])
      gsub(g, n.sons[1])
      gcoms(g)
      gsub(g, n.sons[2])
    else:
      put(g, tkObject, "object")
  of nkRecList: 
    indentNL(g)
    for i in countup(0, sonsLen(n) - 1): 
      optNL(g)
      gsub(g, n.sons[i], c)
      gcoms(g)
    dedent(g)
    putNL(g)
  of nkOfInherit: 
    putWithSpace(g, tkOf, "of")
    gsub(g, n.sons[0])
  of nkProcTy: 
    if sonsLen(n) > 0:
      putWithSpace(g, tkProc, "proc")
      gsub(g, n.sons[0])
      gsub(g, n.sons[1])
    else:
      put(g, tkProc, "proc")
  of nkEnumTy: 
    if sonsLen(n) > 0:
      putWithSpace(g, tkEnum, "enum")
      gsub(g, n.sons[0])
      gcoms(g)
      indentNL(g)
      gcommaAux(g, n, g.indent, 1)
      gcoms(g)                  # BUGFIX: comment for the last enum field
      dedent(g)
    else:
      put(g, tkEnum, "enum")
  of nkEnumFieldDef: 
    gsub(g, n.sons[0])
    put(g, tkSpaces, Space)
    putWithSpace(g, tkEquals, "=")
    gsub(g, n.sons[1])
  of nkStmtList, nkStmtListExpr: gstmts(g, n, emptyContext)
  of nkIfStmt: 
    putWithSpace(g, tkIf, "if")
    gif(g, n)
  of nkWhen, nkRecWhen: 
    putWithSpace(g, tkWhen, "when")
    gif(g, n)
  of nkWhileStmt: gwhile(g, n)
  of nkPragmaBlock: gpragmaBlock(g, n)
  of nkCaseStmt, nkRecCase: gcase(g, n)
  of nkMacroStmt: gmacro(g, n)
  of nkTryStmt: gtry(g, n)
  of nkForStmt, nkParForStmt: gfor(g, n)
  of nkBlockStmt, nkBlockExpr: gblock(g, n)
  of nkStaticStmt: gstaticStmt(g, n)
  of nkAsmStmt: gasm(g, n)
  of nkProcDef: 
    putWithSpace(g, tkProc, "proc")
    gproc(g, n)
  of nkConverterDef:
    putWithSpace(g, tkConverter, "converter")
    gproc(g, n)
  of nkMethodDef: 
    putWithSpace(g, tkMethod, "method")
    gproc(g, n)
  of nkIteratorDef: 
    putWithSpace(g, tkIterator, "iterator")
    gproc(g, n)
  of nkMacroDef: 
    putWithSpace(g, tkMacro, "macro")
    gproc(g, n)
  of nkTemplateDef: 
    putWithSpace(g, tkTemplate, "template")
    gproc(g, n)
  of nkTypeSection: 
    gsection(g, n, emptyContext, tkType, "type")
  of nkConstSection: 
    initContext(a)
    incl(a.flags, rfInConstExpr)
    gsection(g, n, a, tkConst, "const")
  of nkVarSection, nkLetSection:
    L = sonsLen(n)
    if L == 0: return
    if n.kind == nkVarSection: putWithSpace(g, tkVar, "var")
    else: putWithSpace(g, tkLet, "let")
    if L > 1: 
      gcoms(g)
      indentNL(g)
      for i in countup(0, L - 1): 
        optNL(g)
        gsub(g, n.sons[i])
        gcoms(g)
      dedent(g)
    else: 
      gsub(g, n.sons[0])
  of nkReturnStmt: 
    putWithSpace(g, tkReturn, "return")
    gsub(g, n.sons[0])
  of nkRaiseStmt: 
    putWithSpace(g, tkRaise, "raise")
    gsub(g, n.sons[0])
  of nkYieldStmt: 
    putWithSpace(g, tkYield, "yield")
    gsub(g, n.sons[0])
  of nkDiscardStmt: 
    putWithSpace(g, tkDiscard, "discard")
    gsub(g, n.sons[0])
  of nkBreakStmt: 
    putWithSpace(g, tkBreak, "break")
    gsub(g, n.sons[0])
  of nkContinueStmt: 
    putWithSpace(g, tkContinue, "continue")
    gsub(g, n.sons[0])
  of nkPragma: 
    if not (renderNoPragmas in g.flags): 
      put(g, tkCurlyDotLe, "{.")
      gcomma(g, n, emptyContext)
      put(g, tkCurlyDotRi, ".}")
  of nkImportStmt: 
    putWithSpace(g, tkImport, "import")
    gcoms(g)
    indentNL(g)
    gcommaAux(g, n, g.indent)
    dedent(g)
    putNL(g)
  of nkFromStmt: 
    putWithSpace(g, tkFrom, "from")
    gsub(g, n.sons[0])
    put(g, tkSpaces, Space)
    putWithSpace(g, tkImport, "import")
    gcomma(g, n, emptyContext, 1)
    putNL(g)
  of nkIncludeStmt: 
    putWithSpace(g, tkInclude, "include")
    gcoms(g)
    indentNL(g)
    gcommaAux(g, n, g.indent)
    dedent(g)
    putNL(g)
  of nkCommentStmt: 
    gcoms(g)
    optNL(g)
  of nkOfBranch: 
    optNL(g)
    putWithSpace(g, tkOf, "of")
    gcomma(g, n, c, 0, - 2)
    putWithSpace(g, tkColon, ":")
    gcoms(g)
    gstmts(g, lastSon(n), c)
  of nkBindStmt: 
    putWithSpace(g, tkBind, "bind")
    gcomma(g, n, c)
  of nkElifBranch: 
    optNL(g)
    putWithSpace(g, tkElif, "elif")
    gsub(g, n.sons[0])
    putWithSpace(g, tkColon, ":")
    gcoms(g)
    gstmts(g, n.sons[1], c)
  of nkElse: 
    optNL(g)
    put(g, tkElse, "else")
    putWithSpace(g, tkColon, ":")
    gcoms(g)
    gstmts(g, n.sons[0], c)
  of nkFinally: 
    optNL(g)
    put(g, tkFinally, "finally")
    putWithSpace(g, tkColon, ":")
    gcoms(g)
    gstmts(g, n.sons[0], c)
  of nkExceptBranch: 
    optNL(g)
    putWithSpace(g, tkExcept, "except")
    gcomma(g, n, 0, - 2)
    putWithSpace(g, tkColon, ":")
    gcoms(g)
    gstmts(g, lastSon(n), c)
  of nkGenericParams: 
    put(g, tkBracketLe, "[")
    gcomma(g, n)
    put(g, tkBracketRi, "]")
  of nkFormalParams: 
    put(g, tkParLe, "(")
    gsemicolon(g, n, 1)
    put(g, tkParRi, ")")
    if n.sons[0].kind != nkEmpty: 
      putWithSpace(g, tkColon, ":")
      gsub(g, n.sons[0])
  of nkTupleTy: 
    put(g, tkTuple, "tuple")
    if sonsLen(n) > 0:
      put(g, tkBracketLe, "[")
      gcomma(g, n)
      put(g, tkBracketRi, "]")
  else: 
    #nkNone, nkMetaNode, nkExplicitTypeListCall: 
    InternalError(n.info, "rnimsyn.gsub(" & $n.kind & ')')

proc renderTree(n: PNode, renderFlags: TRenderFlags = {}): string = 
  var g: TSrcGen
  initSrcGen(g, renderFlags)
  gsub(g, n)
  result = g.buf

proc renderModule(n: PNode, filename: string, 
                  renderFlags: TRenderFlags = {}) =
  var
    f: tfile
    g: TSrcGen
  initSrcGen(g, renderFlags)
  for i in countup(0, sonsLen(n) - 1):
    gsub(g, n.sons[i])
    optNL(g)
    case n.sons[i].kind
    of nkTypeSection, nkConstSection, nkVarSection, nkLetSection,
       nkCommentStmt: putNL(g)
    else: nil
  gcoms(g)
  if optStdout in gGlobalOptions:
    write(stdout, g.buf)
  elif open(f, filename, fmWrite):
    write(f, g.buf)
    close(f)
  else:
    rawMessage(errCannotOpenFile, filename)    

proc initTokRender(r: var TSrcGen, n: PNode, renderFlags: TRenderFlags = {}) = 
  initSrcGen(r, renderFlags)
  gsub(r, n)

proc getNextTok(r: var TSrcGen, kind: var TTokType, literal: var string) = 
  if r.idx < len(r.tokens): 
    kind = r.tokens[r.idx].kind
    var length = r.tokens[r.idx].length.int
    literal = substr(r.buf, r.pos, r.pos + length - 1)
    inc(r.pos, length)
    inc(r.idx)
  else: 
    kind = tkEof