renderer.nim 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763
  1. #
  2. #
  3. # The Nim Compiler
  4. # (c) Copyright 2013 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. # This module implements the renderer of the standard Nim representation.
  10. when defined(nimHasUsed):
  11. # 'import renderer' is so useful for debugging
  12. # that Nim shouldn't produce a warning for that:
  13. {.used.}
  14. import
  15. lexer, options, idents, strutils, ast, msgs, lineinfos
  16. type
  17. TRenderFlag* = enum
  18. renderNone, renderNoBody, renderNoComments, renderDocComments,
  19. renderNoPragmas, renderIds, renderNoProcDefs, renderSyms, renderRunnableExamples,
  20. renderIr
  21. TRenderFlags* = set[TRenderFlag]
  22. TRenderTok* = object
  23. kind*: TokType
  24. length*: int16
  25. sym*: PSym
  26. TRenderTokSeq* = seq[TRenderTok]
  27. TSrcGen* = object
  28. indent*: int
  29. lineLen*: int
  30. col: int
  31. pos*: int # current position for iteration over the buffer
  32. idx*: int # current token index for iteration over the buffer
  33. tokens*: TRenderTokSeq
  34. buf*: string
  35. pendingNL*: int # negative if not active; else contains the
  36. # indentation value
  37. pendingWhitespace: int
  38. comStack*: seq[PNode] # comment stack
  39. flags*: TRenderFlags
  40. inGenericParams: bool
  41. checkAnon: bool # we're in a context that can contain sfAnon
  42. inPragma: int
  43. when defined(nimpretty):
  44. pendingNewlineCount: int
  45. fid*: FileIndex
  46. config*: ConfigRef
  47. mangler: seq[PSym]
  48. proc renderTree*(n: PNode, renderFlags: TRenderFlags = {}): string
  49. # We render the source code in a two phases: The first
  50. # determines how long the subtree will likely be, the second
  51. # phase appends to a buffer that will be the output.
  52. proc disamb(g: var TSrcGen; s: PSym): int =
  53. # we group by 's.name.s' to compute the stable name ID.
  54. result = 0
  55. for i in 0 ..< g.mangler.len:
  56. if s == g.mangler[i]: return result
  57. if s.name.s == g.mangler[i].name.s: inc result
  58. g.mangler.add s
  59. proc isKeyword*(i: PIdent): bool =
  60. if (i.id >= ord(tokKeywordLow) - ord(tkSymbol)) and
  61. (i.id <= ord(tokKeywordHigh) - ord(tkSymbol)):
  62. result = true
  63. proc renderDefinitionName*(s: PSym, noQuotes = false): string =
  64. ## Returns the definition name of the symbol.
  65. ##
  66. ## If noQuotes is false the symbol may be returned in backticks. This will
  67. ## happen if the name happens to be a keyword or the first character is not
  68. ## part of the SymStartChars set.
  69. let x = s.name.s
  70. if noQuotes or (x[0] in SymStartChars and not renderer.isKeyword(s.name)):
  71. result = x
  72. else:
  73. result = '`' & x & '`'
  74. const
  75. IndentWidth = 2
  76. longIndentWid = IndentWidth * 2
  77. when defined(nimpretty):
  78. proc minmaxLine(n: PNode): (int, int) =
  79. case n.kind
  80. of nkTripleStrLit:
  81. result = (n.info.line.int, n.info.line.int + countLines(n.strVal))
  82. of nkCommentStmt:
  83. result = (n.info.line.int, n.info.line.int + countLines(n.comment))
  84. else:
  85. result = (n.info.line.int, n.info.line.int)
  86. for i in 0..<n.safeLen:
  87. let (currMin, currMax) = minmaxLine(n[i])
  88. if currMin < result[0]: result[0] = currMin
  89. if currMax > result[1]: result[1] = currMax
  90. proc lineDiff(a, b: PNode): int =
  91. result = minmaxLine(b)[0] - minmaxLine(a)[1]
  92. const
  93. MaxLineLen = 80
  94. LineCommentColumn = 30
  95. proc initSrcGen(g: var TSrcGen, renderFlags: TRenderFlags; config: ConfigRef) =
  96. g.comStack = @[]
  97. g.tokens = @[]
  98. g.indent = 0
  99. g.lineLen = 0
  100. g.pos = 0
  101. g.idx = 0
  102. g.buf = ""
  103. g.flags = renderFlags
  104. g.pendingNL = -1
  105. g.pendingWhitespace = -1
  106. g.inGenericParams = false
  107. g.config = config
  108. proc addTok(g: var TSrcGen, kind: TokType, s: string; sym: PSym = nil) =
  109. g.tokens.add TRenderTok(kind: kind, length: int16(s.len), sym: sym)
  110. g.buf.add(s)
  111. if kind != tkSpaces:
  112. inc g.col, s.len
  113. proc addPendingNL(g: var TSrcGen) =
  114. if g.pendingNL >= 0:
  115. when defined(nimpretty):
  116. let newlines = repeat("\n", clamp(g.pendingNewlineCount, 1, 3))
  117. else:
  118. const newlines = "\n"
  119. addTok(g, tkSpaces, newlines & spaces(g.pendingNL))
  120. g.lineLen = g.pendingNL
  121. g.col = g.pendingNL
  122. g.pendingNL = - 1
  123. g.pendingWhitespace = -1
  124. elif g.pendingWhitespace >= 0:
  125. addTok(g, tkSpaces, spaces(g.pendingWhitespace))
  126. g.pendingWhitespace = -1
  127. proc putNL(g: var TSrcGen, indent: int) =
  128. if g.pendingNL >= 0: addPendingNL(g)
  129. else:
  130. addTok(g, tkSpaces, "\n")
  131. g.col = 0
  132. g.pendingNL = indent
  133. g.lineLen = indent
  134. g.pendingWhitespace = -1
  135. proc previousNL(g: TSrcGen): bool =
  136. result = g.pendingNL >= 0 or (g.tokens.len > 0 and
  137. g.tokens[^1].kind == tkSpaces)
  138. proc putNL(g: var TSrcGen) =
  139. putNL(g, g.indent)
  140. proc optNL(g: var TSrcGen, indent: int) =
  141. g.pendingNL = indent
  142. g.lineLen = indent
  143. g.col = g.indent
  144. when defined(nimpretty): g.pendingNewlineCount = 0
  145. proc optNL(g: var TSrcGen) =
  146. optNL(g, g.indent)
  147. proc optNL(g: var TSrcGen; a, b: PNode) =
  148. g.pendingNL = g.indent
  149. g.lineLen = g.indent
  150. g.col = g.indent
  151. when defined(nimpretty): g.pendingNewlineCount = lineDiff(a, b)
  152. proc indentNL(g: var TSrcGen) =
  153. inc(g.indent, IndentWidth)
  154. g.pendingNL = g.indent
  155. g.lineLen = g.indent
  156. proc dedent(g: var TSrcGen) =
  157. dec(g.indent, IndentWidth)
  158. assert(g.indent >= 0)
  159. if g.pendingNL > IndentWidth:
  160. dec(g.pendingNL, IndentWidth)
  161. dec(g.lineLen, IndentWidth)
  162. proc put(g: var TSrcGen, kind: TokType, s: string; sym: PSym = nil) =
  163. if kind != tkSpaces:
  164. addPendingNL(g)
  165. if s.len > 0 or kind in {tkHideableStart, tkHideableEnd}:
  166. addTok(g, kind, s, sym)
  167. else:
  168. g.pendingWhitespace = s.len
  169. inc g.col, s.len
  170. inc(g.lineLen, s.len)
  171. proc putComment(g: var TSrcGen, s: string) =
  172. if s.len == 0: return
  173. var i = 0
  174. let hi = s.len - 1
  175. let isCode = (s.len >= 2) and (s[1] != ' ')
  176. let ind = g.col
  177. var com = "## "
  178. while i <= hi:
  179. case s[i]
  180. of '\0':
  181. break
  182. of '\r':
  183. put(g, tkComment, com)
  184. com = "## "
  185. inc(i)
  186. if i <= hi and s[i] == '\n': inc(i)
  187. optNL(g, ind)
  188. of '\n':
  189. put(g, tkComment, com)
  190. com = "## "
  191. inc(i)
  192. optNL(g, ind)
  193. of ' ', '\t':
  194. com.add(s[i])
  195. inc(i)
  196. else:
  197. # we may break the comment into a multi-line comment if the line
  198. # gets too long:
  199. # compute length of the following word:
  200. var j = i
  201. while j <= hi and s[j] > ' ': inc(j)
  202. if not isCode and (g.col + (j - i) > MaxLineLen):
  203. put(g, tkComment, com)
  204. optNL(g, ind)
  205. com = "## "
  206. while i <= hi and s[i] > ' ':
  207. com.add(s[i])
  208. inc(i)
  209. put(g, tkComment, com)
  210. optNL(g)
  211. proc maxLineLength(s: string): int =
  212. if s.len == 0: return 0
  213. var i = 0
  214. let hi = s.len - 1
  215. var lineLen = 0
  216. while i <= hi:
  217. case s[i]
  218. of '\0':
  219. break
  220. of '\r':
  221. inc(i)
  222. if i <= hi and s[i] == '\n': inc(i)
  223. result = max(result, lineLen)
  224. lineLen = 0
  225. of '\n':
  226. inc(i)
  227. result = max(result, lineLen)
  228. lineLen = 0
  229. else:
  230. inc(lineLen)
  231. inc(i)
  232. proc putRawStr(g: var TSrcGen, kind: TokType, s: string) =
  233. var i = 0
  234. let hi = s.len - 1
  235. var str = ""
  236. while i <= hi:
  237. case s[i]
  238. of '\r':
  239. put(g, kind, str)
  240. str = ""
  241. inc(i)
  242. if i <= hi and s[i] == '\n': inc(i)
  243. optNL(g, 0)
  244. of '\n':
  245. put(g, kind, str)
  246. str = ""
  247. inc(i)
  248. optNL(g, 0)
  249. else:
  250. str.add(s[i])
  251. inc(i)
  252. put(g, kind, str)
  253. proc containsNL(s: string): bool =
  254. for i in 0..<s.len:
  255. case s[i]
  256. of '\r', '\n':
  257. return true
  258. else:
  259. discard
  260. result = false
  261. proc pushCom(g: var TSrcGen, n: PNode) =
  262. setLen(g.comStack, g.comStack.len + 1)
  263. g.comStack[^1] = n
  264. proc popAllComs(g: var TSrcGen) =
  265. setLen(g.comStack, 0)
  266. const
  267. Space = " "
  268. proc shouldRenderComment(g: TSrcGen): bool {.inline.} =
  269. (renderNoComments notin g.flags or renderDocComments in g.flags)
  270. proc shouldRenderComment(g: TSrcGen, n: PNode): bool {.inline.} =
  271. shouldRenderComment(g) and n.comment.len > 0
  272. proc gcom(g: var TSrcGen, n: PNode) =
  273. assert(n != nil)
  274. if shouldRenderComment(g, n):
  275. var oneSpaceAdded = 0
  276. if (g.pendingNL < 0) and (g.buf.len > 0) and (g.buf[^1] != ' '):
  277. put(g, tkSpaces, Space)
  278. oneSpaceAdded = 1
  279. # Before long comments we cannot make sure that a newline is generated,
  280. # because this might be wrong. But it is no problem in practice.
  281. if (g.pendingNL < 0) and (g.buf.len > 0) and
  282. (g.col < LineCommentColumn):
  283. var ml = maxLineLength(n.comment)
  284. if ml + LineCommentColumn <= MaxLineLen:
  285. put(g, tkSpaces, spaces(LineCommentColumn - g.col))
  286. dec g.col, oneSpaceAdded
  287. putComment(g, n.comment) #assert(g.comStack[high(g.comStack)] = n);
  288. proc gcoms(g: var TSrcGen) =
  289. for i in 0..high(g.comStack): gcom(g, g.comStack[i])
  290. popAllComs(g)
  291. proc lsub(g: TSrcGen; n: PNode): int
  292. proc litAux(g: TSrcGen; n: PNode, x: BiggestInt, size: int): string =
  293. proc skip(t: PType): PType =
  294. result = t
  295. while result != nil and result.kind in {tyGenericInst, tyRange, tyVar,
  296. tyLent, tyDistinct, tyOrdinal, tyAlias, tySink}:
  297. result = lastSon(result)
  298. let typ = n.typ.skip
  299. if typ != nil and typ.kind in {tyBool, tyEnum}:
  300. if sfPure in typ.sym.flags:
  301. result = typ.sym.name.s & '.'
  302. let enumfields = typ.n
  303. # we need a slow linear search because of enums with holes:
  304. for e in items(enumfields):
  305. if e.sym.position == x:
  306. result &= e.sym.name.s
  307. return
  308. if nfBase2 in n.flags: result = "0b" & toBin(x, size * 8)
  309. elif nfBase8 in n.flags:
  310. var y = if size < sizeof(BiggestInt): x and ((1.BiggestInt shl (size*8)) - 1)
  311. else: x
  312. result = "0o" & toOct(y, size * 3)
  313. elif nfBase16 in n.flags: result = "0x" & toHex(x, size * 2)
  314. else: result = $x
  315. proc ulitAux(g: TSrcGen; n: PNode, x: BiggestInt, size: int): string =
  316. if nfBase2 in n.flags: result = "0b" & toBin(x, size * 8)
  317. elif nfBase8 in n.flags: result = "0o" & toOct(x, size * 3)
  318. elif nfBase16 in n.flags: result = "0x" & toHex(x, size * 2)
  319. else: result = $cast[BiggestUInt](x)
  320. proc atom(g: TSrcGen; n: PNode): string =
  321. when defined(nimpretty):
  322. doAssert g.config != nil, "g.config not initialized!"
  323. let comment = if n.info.commentOffsetA < n.info.commentOffsetB:
  324. " " & fileSection(g.config, g.fid, n.info.commentOffsetA, n.info.commentOffsetB)
  325. else:
  326. ""
  327. if n.info.offsetA <= n.info.offsetB:
  328. # for some constructed tokens this can not be the case and we're better
  329. # off to not mess with the offset then.
  330. return fileSection(g.config, g.fid, n.info.offsetA, n.info.offsetB) & comment
  331. var f: float32
  332. case n.kind
  333. of nkEmpty: result = ""
  334. of nkIdent: result = n.ident.s
  335. of nkSym: result = n.sym.name.s
  336. of nkStrLit: result = ""; result.addQuoted(n.strVal)
  337. of nkRStrLit: result = "r\"" & replace(n.strVal, "\"", "\"\"") & '\"'
  338. of nkTripleStrLit: result = "\"\"\"" & n.strVal & "\"\"\""
  339. of nkCharLit:
  340. result = "\'"
  341. result.addEscapedChar(chr(int(n.intVal)));
  342. result.add '\''
  343. of nkIntLit: result = litAux(g, n, n.intVal, 4)
  344. of nkInt8Lit: result = litAux(g, n, n.intVal, 1) & "\'i8"
  345. of nkInt16Lit: result = litAux(g, n, n.intVal, 2) & "\'i16"
  346. of nkInt32Lit: result = litAux(g, n, n.intVal, 4) & "\'i32"
  347. of nkInt64Lit: result = litAux(g, n, n.intVal, 8) & "\'i64"
  348. of nkUIntLit: result = ulitAux(g, n, n.intVal, 4) & "\'u"
  349. of nkUInt8Lit: result = ulitAux(g, n, n.intVal, 1) & "\'u8"
  350. of nkUInt16Lit: result = ulitAux(g, n, n.intVal, 2) & "\'u16"
  351. of nkUInt32Lit: result = ulitAux(g, n, n.intVal, 4) & "\'u32"
  352. of nkUInt64Lit: result = ulitAux(g, n, n.intVal, 8) & "\'u64"
  353. of nkFloatLit:
  354. if n.flags * {nfBase2, nfBase8, nfBase16} == {}: result = $(n.floatVal)
  355. else: result = litAux(g, n, (cast[PInt64](addr(n.floatVal)))[] , 8)
  356. of nkFloat32Lit:
  357. if n.flags * {nfBase2, nfBase8, nfBase16} == {}:
  358. result = $n.floatVal & "\'f32"
  359. else:
  360. f = n.floatVal.float32
  361. result = litAux(g, n, (cast[PInt32](addr(f)))[], 4) & "\'f32"
  362. of nkFloat64Lit:
  363. if n.flags * {nfBase2, nfBase8, nfBase16} == {}:
  364. result = $n.floatVal & "\'f64"
  365. else:
  366. result = litAux(g, n, (cast[PInt64](addr(n.floatVal)))[], 8) & "\'f64"
  367. of nkNilLit: result = "nil"
  368. of nkType:
  369. if (n.typ != nil) and (n.typ.sym != nil): result = n.typ.sym.name.s
  370. else: result = "[type node]"
  371. else:
  372. internalError(g.config, "renderer.atom " & $n.kind)
  373. result = ""
  374. proc lcomma(g: TSrcGen; n: PNode, start: int = 0, theEnd: int = - 1): int =
  375. assert(theEnd < 0)
  376. result = 0
  377. for i in start..n.len + theEnd:
  378. let param = n[i]
  379. if nfDefaultParam notin param.flags:
  380. inc(result, lsub(g, param))
  381. inc(result, 2) # for ``, ``
  382. if result > 0:
  383. dec(result, 2) # last does not get a comma!
  384. proc lsons(g: TSrcGen; n: PNode, start: int = 0, theEnd: int = - 1): int =
  385. assert(theEnd < 0)
  386. result = 0
  387. for i in start..n.len + theEnd: inc(result, lsub(g, n[i]))
  388. proc lsub(g: TSrcGen; n: PNode): int =
  389. # computes the length of a tree
  390. if isNil(n): return 0
  391. if shouldRenderComment(g, n): return MaxLineLen + 1
  392. case n.kind
  393. of nkEmpty: result = 0
  394. of nkTripleStrLit:
  395. if containsNL(n.strVal): result = MaxLineLen + 1
  396. else: result = atom(g, n).len
  397. of succ(nkEmpty)..pred(nkTripleStrLit), succ(nkTripleStrLit)..nkNilLit:
  398. result = atom(g, n).len
  399. of nkCall, nkBracketExpr, nkCurlyExpr, nkConv, nkPattern, nkObjConstr:
  400. result = lsub(g, n[0]) + lcomma(g, n, 1) + 2
  401. of nkHiddenStdConv, nkHiddenSubConv, nkHiddenCallConv: result = lsub(g, n[1])
  402. of nkCast: result = lsub(g, n[0]) + lsub(g, n[1]) + len("cast[]()")
  403. of nkAddr: result = (if n.len>0: lsub(g, n[0]) + len("addr()") else: 4)
  404. of nkStaticExpr: result = lsub(g, n[0]) + len("static_")
  405. of nkHiddenAddr, nkHiddenDeref, nkStringToCString, nkCStringToString: result = lsub(g, n[0])
  406. of nkCommand: result = lsub(g, n[0]) + lcomma(g, n, 1) + 1
  407. of nkExprEqExpr, nkAsgn, nkFastAsgn: result = lsons(g, n) + 3
  408. of nkPar, nkCurly, nkBracket, nkClosure: result = lcomma(g, n) + 2
  409. of nkTupleConstr:
  410. # assume the trailing comma:
  411. result = lcomma(g, n) + 3
  412. of nkArgList: result = lcomma(g, n)
  413. of nkTableConstr:
  414. result = if n.len > 0: lcomma(g, n) + 2 else: len("{:}")
  415. of nkClosedSymChoice, nkOpenSymChoice:
  416. if n.len > 0: result += lsub(g, n[0])
  417. of nkTupleTy: result = lcomma(g, n) + len("tuple[]")
  418. of nkTupleClassTy: result = len("tuple")
  419. of nkDotExpr: result = lsons(g, n) + 1
  420. of nkBind: result = lsons(g, n) + len("bind_")
  421. of nkBindStmt: result = lcomma(g, n) + len("bind_")
  422. of nkMixinStmt: result = lcomma(g, n) + len("mixin_")
  423. of nkCheckedFieldExpr: result = lsub(g, n[0])
  424. of nkLambda: result = lsons(g, n) + len("proc__=_")
  425. of nkDo: result = lsons(g, n) + len("do__:_")
  426. of nkConstDef, nkIdentDefs:
  427. result = lcomma(g, n, 0, - 3)
  428. if n[^2].kind != nkEmpty: result += lsub(g, n[^2]) + 2
  429. if n[^1].kind != nkEmpty: result += lsub(g, n[^1]) + 3
  430. of nkVarTuple:
  431. if n[^1].kind == nkEmpty:
  432. result = lcomma(g, n, 0, - 2) + len("()")
  433. else:
  434. result = lcomma(g, n, 0, - 3) + len("() = ") + lsub(g, lastSon(n))
  435. of nkChckRangeF: result = len("chckRangeF") + 2 + lcomma(g, n)
  436. of nkChckRange64: result = len("chckRange64") + 2 + lcomma(g, n)
  437. of nkChckRange: result = len("chckRange") + 2 + lcomma(g, n)
  438. of nkObjDownConv, nkObjUpConv:
  439. result = 2
  440. if n.len >= 1: result += lsub(g, n[0])
  441. result += lcomma(g, n, 1)
  442. of nkExprColonExpr: result = lsons(g, n) + 2
  443. of nkInfix: result = lsons(g, n) + 2
  444. of nkPrefix:
  445. result = lsons(g, n)+1+(if n.len > 0 and n[1].kind == nkInfix: 2 else: 0)
  446. of nkPostfix: result = lsons(g, n)
  447. of nkCallStrLit: result = lsons(g, n)
  448. of nkPragmaExpr: result = lsub(g, n[0]) + lcomma(g, n, 1)
  449. of nkRange: result = lsons(g, n) + 2
  450. of nkDerefExpr: result = lsub(g, n[0]) + 2
  451. of nkAccQuoted: result = lsons(g, n) + 2
  452. of nkIfExpr:
  453. result = lsub(g, n[0][0]) + lsub(g, n[0][1]) + lsons(g, n, 1) +
  454. len("if_:_")
  455. of nkElifExpr: result = lsons(g, n) + len("_elif_:_")
  456. of nkElseExpr: result = lsub(g, n[0]) + len("_else:_") # type descriptions
  457. of nkTypeOfExpr: result = (if n.len > 0: lsub(g, n[0]) else: 0)+len("typeof()")
  458. of nkRefTy: result = (if n.len > 0: lsub(g, n[0])+1 else: 0) + len("ref")
  459. of nkPtrTy: result = (if n.len > 0: lsub(g, n[0])+1 else: 0) + len("ptr")
  460. of nkVarTy: result = (if n.len > 0: lsub(g, n[0])+1 else: 0) + len("var")
  461. of nkDistinctTy:
  462. result = len("distinct") + (if n.len > 0: lsub(g, n[0])+1 else: 0)
  463. if n.len > 1:
  464. result += (if n[1].kind == nkWith: len("_with_") else: len("_without_"))
  465. result += lcomma(g, n[1])
  466. of nkStaticTy: result = (if n.len > 0: lsub(g, n[0]) else: 0) +
  467. len("static[]")
  468. of nkTypeDef: result = lsons(g, n) + 3
  469. of nkOfInherit: result = lsub(g, n[0]) + len("of_")
  470. of nkProcTy: result = lsons(g, n) + len("proc_")
  471. of nkIteratorTy: result = lsons(g, n) + len("iterator_")
  472. of nkSharedTy: result = lsons(g, n) + len("shared_")
  473. of nkEnumTy:
  474. if n.len > 0:
  475. result = lsub(g, n[0]) + lcomma(g, n, 1) + len("enum_")
  476. else:
  477. result = len("enum")
  478. of nkEnumFieldDef: result = lsons(g, n) + 3
  479. of nkVarSection, nkLetSection:
  480. if n.len > 1: result = MaxLineLen + 1
  481. else: result = lsons(g, n) + len("var_")
  482. of nkUsingStmt:
  483. if n.len > 1: result = MaxLineLen + 1
  484. else: result = lsons(g, n) + len("using_")
  485. of nkReturnStmt:
  486. if n.len > 0 and n[0].kind == nkAsgn:
  487. result = len("return_") + lsub(g, n[0][1])
  488. else:
  489. result = len("return_") + lsub(g, n[0])
  490. of nkRaiseStmt: result = lsub(g, n[0]) + len("raise_")
  491. of nkYieldStmt: result = lsub(g, n[0]) + len("yield_")
  492. of nkDiscardStmt: result = lsub(g, n[0]) + len("discard_")
  493. of nkBreakStmt: result = lsub(g, n[0]) + len("break_")
  494. of nkContinueStmt: result = lsub(g, n[0]) + len("continue_")
  495. of nkPragma: result = lcomma(g, n) + 4
  496. of nkCommentStmt: result = n.comment.len
  497. of nkOfBranch: result = lcomma(g, n, 0, - 2) + lsub(g, lastSon(n)) + len("of_:_")
  498. of nkImportAs: result = lsub(g, n[0]) + len("_as_") + lsub(g, n[1])
  499. of nkElifBranch: result = lsons(g, n) + len("elif_:_")
  500. of nkElse: result = lsub(g, n[0]) + len("else:_")
  501. of nkFinally: result = lsub(g, n[0]) + len("finally:_")
  502. of nkGenericParams: result = lcomma(g, n) + 2
  503. of nkFormalParams:
  504. result = lcomma(g, n, 1) + 2
  505. if n[0].kind != nkEmpty: result += lsub(g, n[0]) + 2
  506. of nkExceptBranch:
  507. result = lcomma(g, n, 0, -2) + lsub(g, lastSon(n)) + len("except_:_")
  508. of nkObjectTy:
  509. result = len("object_")
  510. else: result = MaxLineLen + 1
  511. proc fits(g: TSrcGen, x: int): bool =
  512. result = x <= MaxLineLen
  513. type
  514. TSubFlag = enum
  515. rfLongMode, rfInConstExpr
  516. TSubFlags = set[TSubFlag]
  517. TContext = tuple[spacing: int, flags: TSubFlags]
  518. const
  519. emptyContext: TContext = (spacing: 0, flags: {})
  520. proc initContext(c: var TContext) =
  521. c.spacing = 0
  522. c.flags = {}
  523. proc gsub(g: var TSrcGen, n: PNode, c: TContext, fromStmtList = false)
  524. proc gsub(g: var TSrcGen, n: PNode, fromStmtList = false) =
  525. var c: TContext
  526. initContext(c)
  527. gsub(g, n, c, fromStmtList = fromStmtList)
  528. proc hasCom(n: PNode): bool =
  529. result = false
  530. if n.isNil: return false
  531. if n.comment.len > 0: return true
  532. case n.kind
  533. of nkEmpty..nkNilLit: discard
  534. else:
  535. for i in 0..<n.len:
  536. if hasCom(n[i]): return true
  537. proc putWithSpace(g: var TSrcGen, kind: TokType, s: string) =
  538. put(g, kind, s)
  539. put(g, tkSpaces, Space)
  540. proc isHideable(config: ConfigRef, n: PNode): bool =
  541. # xxx compare `ident` directly with `getIdent(cache, wRaises)`, but
  542. # this requires a `cache`.
  543. case n.kind
  544. of nkExprColonExpr: result = n[0].kind == nkIdent and n[0].ident.s in ["raises", "tags", "extern", "deprecated"]
  545. of nkIdent: result = n.ident.s in ["gcsafe", "deprecated"]
  546. else: result = false
  547. proc gcommaAux(g: var TSrcGen, n: PNode, ind: int, start: int = 0,
  548. theEnd: int = - 1, separator = tkComma) =
  549. let inPragma = g.inPragma == 1 # just the top-level
  550. var inHideable = false
  551. for i in start..n.len + theEnd:
  552. var c = i < n.len + theEnd
  553. var sublen = lsub(g, n[i]) + ord(c)
  554. if not fits(g, g.lineLen + sublen) and (ind + sublen < MaxLineLen): optNL(g, ind)
  555. let oldLen = g.tokens.len
  556. if inPragma:
  557. if not inHideable and isHideable(g.config, n[i]):
  558. inHideable = true
  559. put(g, tkHideableStart, "")
  560. elif inHideable and not isHideable(g.config, n[i]):
  561. inHideable = false
  562. put(g, tkHideableEnd, "")
  563. gsub(g, n[i])
  564. if c:
  565. if g.tokens.len > oldLen:
  566. putWithSpace(g, separator, $separator)
  567. if shouldRenderComment(g) and hasCom(n[i]):
  568. gcoms(g)
  569. optNL(g, ind)
  570. if inHideable:
  571. put(g, tkHideableEnd, "")
  572. inHideable = false
  573. proc gcomma(g: var TSrcGen, n: PNode, c: TContext, start: int = 0,
  574. theEnd: int = - 1) =
  575. var ind: int
  576. if rfInConstExpr in c.flags:
  577. ind = g.indent + IndentWidth
  578. else:
  579. ind = g.lineLen
  580. if ind > MaxLineLen div 2: ind = g.indent + longIndentWid
  581. gcommaAux(g, n, ind, start, theEnd)
  582. proc gcomma(g: var TSrcGen, n: PNode, start: int = 0, theEnd: int = - 1) =
  583. var ind = g.lineLen
  584. if ind > MaxLineLen div 2: ind = g.indent + longIndentWid
  585. gcommaAux(g, n, ind, start, theEnd)
  586. proc gsemicolon(g: var TSrcGen, n: PNode, start: int = 0, theEnd: int = - 1) =
  587. var ind = g.lineLen
  588. if ind > MaxLineLen div 2: ind = g.indent + longIndentWid
  589. gcommaAux(g, n, ind, start, theEnd, tkSemiColon)
  590. proc gsons(g: var TSrcGen, n: PNode, c: TContext, start: int = 0,
  591. theEnd: int = - 1) =
  592. for i in start..n.len + theEnd: gsub(g, n[i], c)
  593. proc gsection(g: var TSrcGen, n: PNode, c: TContext, kind: TokType,
  594. k: string) =
  595. if n.len == 0: return # empty var sections are possible
  596. putWithSpace(g, kind, k)
  597. gcoms(g)
  598. indentNL(g)
  599. for i in 0..<n.len:
  600. optNL(g)
  601. gsub(g, n[i], c)
  602. gcoms(g)
  603. dedent(g)
  604. proc longMode(g: TSrcGen; n: PNode, start: int = 0, theEnd: int = - 1): bool =
  605. result = shouldRenderComment(g, n)
  606. if not result:
  607. # check further
  608. for i in start..n.len + theEnd:
  609. if (lsub(g, n[i]) > MaxLineLen):
  610. result = true
  611. break
  612. proc gstmts(g: var TSrcGen, n: PNode, c: TContext, doIndent=true) =
  613. if n.kind == nkEmpty: return
  614. if n.kind in {nkStmtList, nkStmtListExpr, nkStmtListType}:
  615. if doIndent: indentNL(g)
  616. for i in 0..<n.len:
  617. if i > 0:
  618. optNL(g, n[i-1], n[i])
  619. else:
  620. optNL(g)
  621. if n[i].kind in {nkStmtList, nkStmtListExpr, nkStmtListType}:
  622. gstmts(g, n[i], c, doIndent=false)
  623. else:
  624. gsub(g, n[i], fromStmtList = true)
  625. gcoms(g)
  626. if doIndent: dedent(g)
  627. else:
  628. indentNL(g)
  629. gsub(g, n)
  630. gcoms(g)
  631. dedent(g)
  632. optNL(g)
  633. proc gcond(g: var TSrcGen, n: PNode) =
  634. if n.kind == nkStmtListExpr:
  635. put(g, tkParLe, "(")
  636. gsub(g, n)
  637. if n.kind == nkStmtListExpr:
  638. put(g, tkParRi, ")")
  639. proc gif(g: var TSrcGen, n: PNode) =
  640. var c: TContext
  641. gcond(g, n[0][0])
  642. initContext(c)
  643. putWithSpace(g, tkColon, ":")
  644. if longMode(g, n) or (lsub(g, n[0][1]) + g.lineLen > MaxLineLen):
  645. incl(c.flags, rfLongMode)
  646. gcoms(g) # a good place for comments
  647. gstmts(g, n[0][1], c)
  648. for i in 1..<n.len:
  649. optNL(g)
  650. gsub(g, n[i], c)
  651. proc gwhile(g: var TSrcGen, n: PNode) =
  652. var c: TContext
  653. putWithSpace(g, tkWhile, "while")
  654. gcond(g, n[0])
  655. putWithSpace(g, tkColon, ":")
  656. initContext(c)
  657. if longMode(g, n) or (lsub(g, n[1]) + g.lineLen > MaxLineLen):
  658. incl(c.flags, rfLongMode)
  659. gcoms(g) # a good place for comments
  660. gstmts(g, n[1], c)
  661. proc gpattern(g: var TSrcGen, n: PNode) =
  662. var c: TContext
  663. put(g, tkCurlyLe, "{")
  664. initContext(c)
  665. if longMode(g, n) or (lsub(g, n[0]) + g.lineLen > MaxLineLen):
  666. incl(c.flags, rfLongMode)
  667. gcoms(g) # a good place for comments
  668. gstmts(g, n, c)
  669. put(g, tkCurlyRi, "}")
  670. proc gpragmaBlock(g: var TSrcGen, n: PNode) =
  671. var c: TContext
  672. gsub(g, n[0])
  673. putWithSpace(g, tkColon, ":")
  674. initContext(c)
  675. if longMode(g, n) or (lsub(g, n[1]) + g.lineLen > MaxLineLen):
  676. incl(c.flags, rfLongMode)
  677. gcoms(g) # a good place for comments
  678. gstmts(g, n[1], c)
  679. proc gtry(g: var TSrcGen, n: PNode) =
  680. var c: TContext
  681. put(g, tkTry, "try")
  682. putWithSpace(g, tkColon, ":")
  683. initContext(c)
  684. if longMode(g, n) or (lsub(g, n[0]) + g.lineLen > MaxLineLen):
  685. incl(c.flags, rfLongMode)
  686. gcoms(g) # a good place for comments
  687. gstmts(g, n[0], c)
  688. gsons(g, n, c, 1)
  689. proc gfor(g: var TSrcGen, n: PNode) =
  690. var c: TContext
  691. putWithSpace(g, tkFor, "for")
  692. initContext(c)
  693. if longMode(g, n) or
  694. (lsub(g, n[^1]) + lsub(g, n[^2]) + 6 + g.lineLen > MaxLineLen):
  695. incl(c.flags, rfLongMode)
  696. gcomma(g, n, c, 0, - 3)
  697. put(g, tkSpaces, Space)
  698. putWithSpace(g, tkIn, "in")
  699. gsub(g, n[^2], c)
  700. putWithSpace(g, tkColon, ":")
  701. gcoms(g)
  702. gstmts(g, n[^1], c)
  703. proc gcase(g: var TSrcGen, n: PNode) =
  704. var c: TContext
  705. initContext(c)
  706. if n.len == 0: return
  707. var last = if n[^1].kind == nkElse: -2 else: -1
  708. if longMode(g, n, 0, last): incl(c.flags, rfLongMode)
  709. putWithSpace(g, tkCase, "case")
  710. gcond(g, n[0])
  711. gcoms(g)
  712. optNL(g)
  713. gsons(g, n, c, 1, last)
  714. if last == - 2:
  715. initContext(c)
  716. if longMode(g, n[^1]): incl(c.flags, rfLongMode)
  717. gsub(g, n[^1], c)
  718. proc genSymSuffix(result: var string, s: PSym) {.inline.} =
  719. if sfGenSym in s.flags:
  720. result.add '_'
  721. result.addInt s.id
  722. proc gproc(g: var TSrcGen, n: PNode) =
  723. var c: TContext
  724. if n[namePos].kind == nkSym:
  725. let s = n[namePos].sym
  726. var ret = renderDefinitionName(s)
  727. ret.genSymSuffix(s)
  728. put(g, tkSymbol, ret)
  729. else:
  730. gsub(g, n[namePos])
  731. if n[patternPos].kind != nkEmpty:
  732. gpattern(g, n[patternPos])
  733. let oldInGenericParams = g.inGenericParams
  734. g.inGenericParams = true
  735. if renderNoBody in g.flags and n[miscPos].kind != nkEmpty and
  736. n[miscPos][1].kind != nkEmpty:
  737. gsub(g, n[miscPos][1])
  738. else:
  739. gsub(g, n[genericParamsPos])
  740. g.inGenericParams = oldInGenericParams
  741. gsub(g, n[paramsPos])
  742. if renderNoPragmas notin g.flags:
  743. gsub(g, n[pragmasPos])
  744. if renderNoBody notin g.flags:
  745. if n[bodyPos].kind != nkEmpty:
  746. put(g, tkSpaces, Space)
  747. putWithSpace(g, tkEquals, "=")
  748. indentNL(g)
  749. gcoms(g)
  750. dedent(g)
  751. initContext(c)
  752. gstmts(g, n[bodyPos], c)
  753. putNL(g)
  754. else:
  755. indentNL(g)
  756. gcoms(g)
  757. dedent(g)
  758. proc gTypeClassTy(g: var TSrcGen, n: PNode) =
  759. var c: TContext
  760. initContext(c)
  761. putWithSpace(g, tkConcept, "concept")
  762. gsons(g, n[0], c) # arglist
  763. gsub(g, n[1]) # pragmas
  764. gsub(g, n[2]) # of
  765. gcoms(g)
  766. indentNL(g)
  767. gcoms(g)
  768. gstmts(g, n[3], c)
  769. dedent(g)
  770. proc gblock(g: var TSrcGen, n: PNode) =
  771. # you shouldn't simplify it to `n.len < 2`
  772. # because the following codes should be executed
  773. # even when block stmt has only one child for getting
  774. # better error messages.
  775. if n.len == 0:
  776. return
  777. var c: TContext
  778. initContext(c)
  779. if n[0].kind != nkEmpty:
  780. putWithSpace(g, tkBlock, "block")
  781. gsub(g, n[0])
  782. else:
  783. put(g, tkBlock, "block")
  784. # block stmt should have two children
  785. if n.len == 1:
  786. return
  787. putWithSpace(g, tkColon, ":")
  788. if longMode(g, n) or (lsub(g, n[1]) + g.lineLen > MaxLineLen):
  789. incl(c.flags, rfLongMode)
  790. gcoms(g)
  791. gstmts(g, n[1], c)
  792. proc gstaticStmt(g: var TSrcGen, n: PNode) =
  793. var c: TContext
  794. putWithSpace(g, tkStatic, "static")
  795. putWithSpace(g, tkColon, ":")
  796. initContext(c)
  797. if longMode(g, n) or (lsub(g, n[0]) + g.lineLen > MaxLineLen):
  798. incl(c.flags, rfLongMode)
  799. gcoms(g) # a good place for comments
  800. gstmts(g, n[0], c)
  801. proc gasm(g: var TSrcGen, n: PNode) =
  802. putWithSpace(g, tkAsm, "asm")
  803. gsub(g, n[0])
  804. gcoms(g)
  805. if n.len > 1:
  806. gsub(g, n[1])
  807. proc gident(g: var TSrcGen, n: PNode) =
  808. if g.inGenericParams and n.kind == nkSym:
  809. if sfAnon in n.sym.flags or
  810. (n.typ != nil and tfImplicitTypeParam in n.typ.flags): return
  811. var t: TokType
  812. var s = atom(g, n)
  813. if s.len > 0 and s[0] in lexer.SymChars:
  814. if n.kind == nkIdent:
  815. if (n.ident.id < ord(tokKeywordLow) - ord(tkSymbol)) or
  816. (n.ident.id > ord(tokKeywordHigh) - ord(tkSymbol)):
  817. t = tkSymbol
  818. else:
  819. t = TokType(n.ident.id + ord(tkSymbol))
  820. else:
  821. t = tkSymbol
  822. else:
  823. t = tkOpr
  824. if renderIr in g.flags and n.kind == nkSym:
  825. let localId = disamb(g, n.sym)
  826. if localId != 0 and n.sym.magic == mNone:
  827. s.add '_'
  828. s.addInt localId
  829. if sfCursor in n.sym.flags:
  830. s.add "_cursor"
  831. elif n.kind == nkSym and (renderIds in g.flags or sfGenSym in n.sym.flags or n.sym.kind == skTemp):
  832. s.add '_'
  833. s.addInt n.sym.id
  834. when defined(debugMagics):
  835. s.add '_'
  836. s.add $n.sym.magic
  837. put(g, t, s, if n.kind == nkSym and renderSyms in g.flags: n.sym else: nil)
  838. proc doParamsAux(g: var TSrcGen, params: PNode) =
  839. if params.len > 1:
  840. put(g, tkParLe, "(")
  841. gsemicolon(g, params, 1)
  842. put(g, tkParRi, ")")
  843. if params.len > 0 and params[0].kind != nkEmpty:
  844. putWithSpace(g, tkOpr, "->")
  845. gsub(g, params[0])
  846. proc gsub(g: var TSrcGen; n: PNode; i: int) =
  847. if i < n.len:
  848. gsub(g, n[i])
  849. else:
  850. put(g, tkOpr, "<<" & $i & "th child missing for " & $n.kind & " >>")
  851. type
  852. BracketKind = enum
  853. bkNone, bkBracket, bkBracketAsgn, bkCurly, bkCurlyAsgn
  854. proc bracketKind*(g: TSrcGen, n: PNode): BracketKind =
  855. if renderIds notin g.flags:
  856. case n.kind
  857. of nkClosedSymChoice, nkOpenSymChoice:
  858. if n.len > 0: result = bracketKind(g, n[0])
  859. of nkSym:
  860. result = case n.sym.name.s
  861. of "[]": bkBracket
  862. of "[]=": bkBracketAsgn
  863. of "{}": bkCurly
  864. of "{}=": bkCurlyAsgn
  865. else: bkNone
  866. else: result = bkNone
  867. proc skipHiddenNodes(n: PNode): PNode =
  868. result = n
  869. while result != nil:
  870. if result.kind in {nkHiddenStdConv, nkHiddenSubConv, nkHiddenCallConv} and result.len > 1:
  871. result = result[1]
  872. elif result.kind in {nkCheckedFieldExpr, nkHiddenAddr, nkHiddenDeref, nkStringToCString, nkCStringToString} and
  873. result.len > 0:
  874. result = result[0]
  875. else: break
  876. proc accentedName(g: var TSrcGen, n: PNode) =
  877. # This is for cases where ident should've really been a `nkAccQuoted`, e.g. `:tmp`
  878. # or if user writes a macro with `ident":foo"`. It's unclear whether these should be legal.
  879. const backticksNeeded = OpChars + {'[', '{', '\''}
  880. if n == nil: return
  881. let ident = n.getPIdent
  882. if ident != nil and ident.s[0] in backticksNeeded:
  883. put(g, tkAccent, "`")
  884. gident(g, n)
  885. put(g, tkAccent, "`")
  886. else:
  887. gsub(g, n)
  888. proc infixArgument(g: var TSrcGen, n: PNode, i: int) =
  889. if i < 1 and i > 2: return
  890. var needsParenthesis = false
  891. let nNext = n[i].skipHiddenNodes
  892. if nNext.kind == nkInfix:
  893. if nNext[0].kind in {nkSym, nkIdent} and n[0].kind in {nkSym, nkIdent}:
  894. let nextId = if nNext[0].kind == nkSym: nNext[0].sym.name else: nNext[0].ident
  895. let nnId = if n[0].kind == nkSym: n[0].sym.name else: n[0].ident
  896. if i == 1:
  897. if getPrecedence(nextId) < getPrecedence(nnId):
  898. needsParenthesis = true
  899. elif i == 2:
  900. if getPrecedence(nextId) <= getPrecedence(nnId):
  901. needsParenthesis = true
  902. if needsParenthesis:
  903. put(g, tkParLe, "(")
  904. gsub(g, n, i)
  905. if needsParenthesis:
  906. put(g, tkParRi, ")")
  907. proc isCustomLit(n: PNode): bool =
  908. if n.len == 2 and n[0].kind == nkRStrLit:
  909. let ident = n[1].getPIdent
  910. result = ident != nil and ident.s.startsWith('\'')
  911. proc gsub(g: var TSrcGen, n: PNode, c: TContext, fromStmtList = false) =
  912. if isNil(n): return
  913. var
  914. a: TContext
  915. if shouldRenderComment(g, n): pushCom(g, n)
  916. case n.kind # atoms:
  917. of nkTripleStrLit: put(g, tkTripleStrLit, atom(g, n))
  918. of nkEmpty: discard
  919. of nkType: put(g, tkInvalid, atom(g, n))
  920. of nkSym, nkIdent: gident(g, n)
  921. of nkIntLit: put(g, tkIntLit, atom(g, n))
  922. of nkInt8Lit: put(g, tkInt8Lit, atom(g, n))
  923. of nkInt16Lit: put(g, tkInt16Lit, atom(g, n))
  924. of nkInt32Lit: put(g, tkInt32Lit, atom(g, n))
  925. of nkInt64Lit: put(g, tkInt64Lit, atom(g, n))
  926. of nkUIntLit: put(g, tkUIntLit, atom(g, n))
  927. of nkUInt8Lit: put(g, tkUInt8Lit, atom(g, n))
  928. of nkUInt16Lit: put(g, tkUInt16Lit, atom(g, n))
  929. of nkUInt32Lit: put(g, tkUInt32Lit, atom(g, n))
  930. of nkUInt64Lit: put(g, tkUInt64Lit, atom(g, n))
  931. of nkFloatLit: put(g, tkFloatLit, atom(g, n))
  932. of nkFloat32Lit: put(g, tkFloat32Lit, atom(g, n))
  933. of nkFloat64Lit: put(g, tkFloat64Lit, atom(g, n))
  934. of nkFloat128Lit: put(g, tkFloat128Lit, atom(g, n))
  935. of nkStrLit: put(g, tkStrLit, atom(g, n))
  936. of nkRStrLit: put(g, tkRStrLit, atom(g, n))
  937. of nkCharLit: put(g, tkCharLit, atom(g, n))
  938. of nkNilLit: put(g, tkNil, atom(g, n)) # complex expressions
  939. of nkCall, nkConv, nkDotCall, nkPattern, nkObjConstr:
  940. if n.len > 1 and n.lastSon.kind in {nkStmtList, nkStmtListExpr}:
  941. accentedName(g, n[0])
  942. var i = 1
  943. while i < n.len and n[i].kind notin {nkStmtList, nkStmtListExpr}: i.inc
  944. if i > 1:
  945. put(g, tkParLe, "(")
  946. gcomma(g, n, 1, i - 1 - n.len)
  947. put(g, tkParRi, ")")
  948. if fromStmtList:
  949. put(g, tkColon, ":")
  950. else:
  951. put(g, tkSpaces, Space)
  952. put(g, tkDo, "do")
  953. put(g, tkColon, ":")
  954. gsub(g, n, i)
  955. i.inc
  956. for j in i ..< n.len:
  957. optNL(g)
  958. put(g, tkDo, "do")
  959. put(g, tkColon, ":")
  960. gsub(g, n, j)
  961. elif n.len >= 1:
  962. case bracketKind(g, n[0])
  963. of bkBracket:
  964. gsub(g, n, 1)
  965. put(g, tkBracketLe, "[")
  966. gcomma(g, n, 2)
  967. put(g, tkBracketRi, "]")
  968. of bkBracketAsgn:
  969. gsub(g, n, 1)
  970. put(g, tkBracketLe, "[")
  971. gcomma(g, n, 2, -2)
  972. put(g, tkBracketRi, "]")
  973. put(g, tkSpaces, Space)
  974. putWithSpace(g, tkEquals, "=")
  975. gsub(g, n, n.len - 1)
  976. of bkCurly:
  977. gsub(g, n, 1)
  978. put(g, tkCurlyLe, "{")
  979. gcomma(g, n, 2)
  980. put(g, tkCurlyRi, "}")
  981. of bkCurlyAsgn:
  982. gsub(g, n, 1)
  983. put(g, tkCurlyLe, "{")
  984. gcomma(g, n, 2, -2)
  985. put(g, tkCurlyRi, "}")
  986. put(g, tkSpaces, Space)
  987. putWithSpace(g, tkEquals, "=")
  988. gsub(g, n, n.len - 1)
  989. of bkNone:
  990. accentedName(g, n[0])
  991. put(g, tkParLe, "(")
  992. gcomma(g, n, 1)
  993. put(g, tkParRi, ")")
  994. else:
  995. put(g, tkParLe, "(")
  996. put(g, tkParRi, ")")
  997. of nkCallStrLit:
  998. if n.len > 0: accentedName(g, n[0])
  999. if n.len > 1 and n[1].kind == nkRStrLit:
  1000. put(g, tkRStrLit, '\"' & replace(n[1].strVal, "\"", "\"\"") & '\"')
  1001. else:
  1002. gsub(g, n, 1)
  1003. of nkHiddenStdConv, nkHiddenSubConv:
  1004. if n.len >= 2:
  1005. when false:
  1006. # if {renderIds, renderIr} * g.flags != {}:
  1007. put(g, tkSymbol, "(conv)")
  1008. put(g, tkParLe, "(")
  1009. gsub(g, n[1])
  1010. put(g, tkParRi, ")")
  1011. else:
  1012. gsub(g, n[1])
  1013. else:
  1014. put(g, tkSymbol, "(wrong conv)")
  1015. of nkHiddenCallConv:
  1016. if {renderIds, renderIr} * g.flags != {}:
  1017. accentedName(g, n[0])
  1018. put(g, tkParLe, "(")
  1019. gcomma(g, n, 1)
  1020. put(g, tkParRi, ")")
  1021. elif n.len >= 2:
  1022. gsub(g, n[1])
  1023. else:
  1024. put(g, tkSymbol, "(wrong conv)")
  1025. of nkCast:
  1026. put(g, tkCast, "cast")
  1027. if n.len > 0 and n[0].kind != nkEmpty:
  1028. put(g, tkBracketLe, "[")
  1029. gsub(g, n, 0)
  1030. put(g, tkBracketRi, "]")
  1031. put(g, tkParLe, "(")
  1032. gsub(g, n, 1)
  1033. put(g, tkParRi, ")")
  1034. of nkAddr:
  1035. put(g, tkAddr, "addr")
  1036. if n.len > 0:
  1037. put(g, tkParLe, "(")
  1038. gsub(g, n[0])
  1039. put(g, tkParRi, ")")
  1040. of nkStaticExpr:
  1041. put(g, tkStatic, "static")
  1042. put(g, tkSpaces, Space)
  1043. gsub(g, n, 0)
  1044. of nkBracketExpr:
  1045. gsub(g, n, 0)
  1046. put(g, tkBracketLe, "[")
  1047. gcomma(g, n, 1)
  1048. put(g, tkBracketRi, "]")
  1049. of nkCurlyExpr:
  1050. gsub(g, n, 0)
  1051. put(g, tkCurlyLe, "{")
  1052. gcomma(g, n, 1)
  1053. put(g, tkCurlyRi, "}")
  1054. of nkPragmaExpr:
  1055. gsub(g, n, 0)
  1056. gcomma(g, n, 1)
  1057. of nkCommand:
  1058. accentedName(g, n[0])
  1059. put(g, tkSpaces, Space)
  1060. if n[^1].kind == nkStmtList:
  1061. for i, child in n:
  1062. if i > 1 and i < n.len - 1:
  1063. put(g, tkComma, ",")
  1064. elif i == n.len - 1:
  1065. put(g, tkColon, ":")
  1066. if i > 0:
  1067. gsub(g, child)
  1068. else:
  1069. gcomma(g, n, 1)
  1070. of nkExprEqExpr, nkAsgn, nkFastAsgn:
  1071. gsub(g, n, 0)
  1072. put(g, tkSpaces, Space)
  1073. putWithSpace(g, tkEquals, "=")
  1074. gsub(g, n, 1)
  1075. of nkChckRangeF:
  1076. put(g, tkSymbol, "chckRangeF")
  1077. put(g, tkParLe, "(")
  1078. gcomma(g, n)
  1079. put(g, tkParRi, ")")
  1080. of nkChckRange64:
  1081. put(g, tkSymbol, "chckRange64")
  1082. put(g, tkParLe, "(")
  1083. gcomma(g, n)
  1084. put(g, tkParRi, ")")
  1085. of nkChckRange:
  1086. put(g, tkSymbol, "chckRange")
  1087. put(g, tkParLe, "(")
  1088. gcomma(g, n)
  1089. put(g, tkParRi, ")")
  1090. of nkObjDownConv, nkObjUpConv:
  1091. let typ = if (n.typ != nil) and (n.typ.sym != nil): n.typ.sym.name.s else: ""
  1092. put(g, tkParLe, typ & "(")
  1093. if n.len >= 1: gsub(g, n[0])
  1094. put(g, tkParRi, ")")
  1095. of nkClosedSymChoice, nkOpenSymChoice:
  1096. if renderIds in g.flags:
  1097. put(g, tkParLe, "(")
  1098. for i in 0..<n.len:
  1099. if i > 0: put(g, tkOpr, "|")
  1100. if n[i].kind == nkSym:
  1101. let s = n[i].sym
  1102. if s.owner != nil:
  1103. put g, tkSymbol, n[i].sym.owner.name.s
  1104. put g, tkOpr, "."
  1105. put g, tkSymbol, n[i].sym.name.s
  1106. else:
  1107. gsub(g, n[i], c)
  1108. put(g, tkParRi, if n.kind == nkOpenSymChoice: "|...)" else: ")")
  1109. else:
  1110. gsub(g, n, 0)
  1111. of nkPar, nkClosure:
  1112. put(g, tkParLe, "(")
  1113. gcomma(g, n, c)
  1114. put(g, tkParRi, ")")
  1115. of nkTupleConstr:
  1116. put(g, tkParLe, "(")
  1117. gcomma(g, n, c)
  1118. if n.len == 1 and n[0].kind != nkExprColonExpr: put(g, tkComma, ",")
  1119. put(g, tkParRi, ")")
  1120. of nkCurly:
  1121. put(g, tkCurlyLe, "{")
  1122. gcomma(g, n, c)
  1123. put(g, tkCurlyRi, "}")
  1124. of nkArgList:
  1125. gcomma(g, n, c)
  1126. of nkTableConstr:
  1127. put(g, tkCurlyLe, "{")
  1128. if n.len > 0: gcomma(g, n, c)
  1129. else: put(g, tkColon, ":")
  1130. put(g, tkCurlyRi, "}")
  1131. of nkBracket:
  1132. put(g, tkBracketLe, "[")
  1133. gcomma(g, n, c)
  1134. put(g, tkBracketRi, "]")
  1135. of nkDotExpr:
  1136. if isCustomLit(n):
  1137. put(g, tkCustomLit, n[0].strVal)
  1138. gsub(g, n, 1)
  1139. else:
  1140. gsub(g, n, 0)
  1141. put(g, tkDot, ".")
  1142. assert n.len == 2, $n.len
  1143. accentedName(g, n[1])
  1144. of nkBind:
  1145. putWithSpace(g, tkBind, "bind")
  1146. gsub(g, n, 0)
  1147. of nkCheckedFieldExpr, nkHiddenAddr, nkHiddenDeref, nkStringToCString, nkCStringToString:
  1148. if renderIds in g.flags:
  1149. putWithSpace(g, tkAddr, $n.kind)
  1150. gsub(g, n, 0)
  1151. of nkLambda:
  1152. putWithSpace(g, tkProc, "proc")
  1153. gsub(g, n, paramsPos)
  1154. gsub(g, n, pragmasPos)
  1155. put(g, tkSpaces, Space)
  1156. putWithSpace(g, tkEquals, "=")
  1157. gsub(g, n, bodyPos)
  1158. of nkDo:
  1159. putWithSpace(g, tkDo, "do")
  1160. if paramsPos < n.len:
  1161. doParamsAux(g, n[paramsPos])
  1162. gsub(g, n, pragmasPos)
  1163. put(g, tkColon, ":")
  1164. gsub(g, n, bodyPos)
  1165. of nkConstDef, nkIdentDefs:
  1166. gcomma(g, n, 0, -3)
  1167. if n.len >= 2 and n[^2].kind != nkEmpty:
  1168. putWithSpace(g, tkColon, ":")
  1169. gsub(g, n, n.len - 2)
  1170. if n.len >= 1 and n[^1].kind != nkEmpty:
  1171. put(g, tkSpaces, Space)
  1172. putWithSpace(g, tkEquals, "=")
  1173. gsub(g, n[^1], c)
  1174. of nkVarTuple:
  1175. if n[^1].kind == nkEmpty:
  1176. put(g, tkParLe, "(")
  1177. gcomma(g, n, 0, -2)
  1178. put(g, tkParRi, ")")
  1179. else:
  1180. put(g, tkParLe, "(")
  1181. gcomma(g, n, 0, -3)
  1182. put(g, tkParRi, ")")
  1183. put(g, tkSpaces, Space)
  1184. putWithSpace(g, tkEquals, "=")
  1185. gsub(g, lastSon(n), c)
  1186. of nkExprColonExpr:
  1187. gsub(g, n, 0)
  1188. putWithSpace(g, tkColon, ":")
  1189. gsub(g, n, 1)
  1190. of nkInfix:
  1191. let oldLineLen = g.lineLen # we cache this because lineLen gets updated below
  1192. infixArgument(g, n, 1)
  1193. put(g, tkSpaces, Space)
  1194. gsub(g, n, 0) # binary operator
  1195. # e.g.: `n1 == n2` decompses as following sum:
  1196. if n.len == 3 and not fits(g, oldLineLen + lsub(g, n[1]) + lsub(g, n[2]) + lsub(g, n[0]) + len(" ")):
  1197. optNL(g, g.indent + longIndentWid)
  1198. else:
  1199. put(g, tkSpaces, Space)
  1200. infixArgument(g, n, 2)
  1201. of nkPrefix:
  1202. gsub(g, n, 0)
  1203. if n.len > 1:
  1204. let opr = if n[0].kind == nkIdent: n[0].ident
  1205. elif n[0].kind == nkSym: n[0].sym.name
  1206. elif n[0].kind in {nkOpenSymChoice, nkClosedSymChoice}: n[0][0].sym.name
  1207. else: nil
  1208. let nNext = skipHiddenNodes(n[1])
  1209. if nNext.kind == nkPrefix or (opr != nil and renderer.isKeyword(opr)):
  1210. put(g, tkSpaces, Space)
  1211. if nNext.kind == nkInfix:
  1212. put(g, tkParLe, "(")
  1213. gsub(g, n[1])
  1214. put(g, tkParRi, ")")
  1215. else:
  1216. gsub(g, n[1])
  1217. of nkPostfix:
  1218. gsub(g, n, 1)
  1219. gsub(g, n, 0)
  1220. of nkRange:
  1221. gsub(g, n, 0)
  1222. put(g, tkDotDot, "..")
  1223. gsub(g, n, 1)
  1224. of nkDerefExpr:
  1225. gsub(g, n, 0)
  1226. put(g, tkOpr, "[]")
  1227. of nkAccQuoted:
  1228. put(g, tkAccent, "`")
  1229. for i in 0..<n.len:
  1230. proc getStrVal(n: PNode): string =
  1231. # pending https://github.com/nim-lang/Nim/pull/17540, use `getStrVal`
  1232. case n.kind
  1233. of nkIdent: n.ident.s
  1234. of nkSym: n.sym.name.s
  1235. else: ""
  1236. proc isAlpha(n: PNode): bool =
  1237. if n.kind in {nkIdent, nkSym}:
  1238. let tmp = n.getStrVal
  1239. result = tmp.len > 0 and tmp[0] in {'a'..'z', 'A'..'Z'}
  1240. var useSpace = false
  1241. if i == 1 and n[0].kind == nkIdent and n[0].ident.s in ["=", "'"]:
  1242. if not n[1].isAlpha: # handle `=destroy`, `'big'
  1243. useSpace = true
  1244. elif i == 1 and n[1].kind == nkIdent and n[1].ident.s == "=":
  1245. if not n[0].isAlpha: # handle setters, e.g. `foo=`
  1246. useSpace = true
  1247. elif i > 0: useSpace = true
  1248. if useSpace: put(g, tkSpaces, Space)
  1249. gsub(g, n[i])
  1250. put(g, tkAccent, "`")
  1251. of nkIfExpr:
  1252. putWithSpace(g, tkIf, "if")
  1253. if n.len > 0: gcond(g, n[0][0])
  1254. putWithSpace(g, tkColon, ":")
  1255. if n.len > 0: gsub(g, n[0], 1)
  1256. gsons(g, n, emptyContext, 1)
  1257. of nkElifExpr:
  1258. putWithSpace(g, tkElif, " elif")
  1259. gcond(g, n[0])
  1260. putWithSpace(g, tkColon, ":")
  1261. gsub(g, n, 1)
  1262. of nkElseExpr:
  1263. put(g, tkElse, " else")
  1264. putWithSpace(g, tkColon, ":")
  1265. gsub(g, n, 0)
  1266. of nkTypeOfExpr:
  1267. put(g, tkType, "typeof")
  1268. put(g, tkParLe, "(")
  1269. if n.len > 0: gsub(g, n[0])
  1270. put(g, tkParRi, ")")
  1271. of nkRefTy:
  1272. if n.len > 0:
  1273. putWithSpace(g, tkRef, "ref")
  1274. gsub(g, n[0])
  1275. else:
  1276. put(g, tkRef, "ref")
  1277. of nkPtrTy:
  1278. if n.len > 0:
  1279. putWithSpace(g, tkPtr, "ptr")
  1280. gsub(g, n[0])
  1281. else:
  1282. put(g, tkPtr, "ptr")
  1283. of nkVarTy:
  1284. if n.len > 0:
  1285. putWithSpace(g, tkVar, "var")
  1286. gsub(g, n[0])
  1287. else:
  1288. put(g, tkVar, "var")
  1289. of nkDistinctTy:
  1290. if n.len > 0:
  1291. putWithSpace(g, tkDistinct, "distinct")
  1292. gsub(g, n[0])
  1293. if n.len > 1:
  1294. if n[1].kind == nkWith:
  1295. putWithSpace(g, tkSymbol, " with")
  1296. else:
  1297. putWithSpace(g, tkSymbol, " without")
  1298. gcomma(g, n[1])
  1299. else:
  1300. put(g, tkDistinct, "distinct")
  1301. of nkTypeDef:
  1302. if n[0].kind == nkPragmaExpr:
  1303. # generate pragma after generic
  1304. gsub(g, n[0], 0)
  1305. gsub(g, n, 1)
  1306. gsub(g, n[0], 1)
  1307. else:
  1308. gsub(g, n, 0)
  1309. gsub(g, n, 1)
  1310. put(g, tkSpaces, Space)
  1311. if n.len > 2 and n[2].kind != nkEmpty:
  1312. putWithSpace(g, tkEquals, "=")
  1313. gsub(g, n[2])
  1314. of nkObjectTy:
  1315. if n.len > 0:
  1316. putWithSpace(g, tkObject, "object")
  1317. gsub(g, n[0])
  1318. gsub(g, n[1])
  1319. gcoms(g)
  1320. gsub(g, n[2])
  1321. else:
  1322. put(g, tkObject, "object")
  1323. of nkRecList:
  1324. indentNL(g)
  1325. for i in 0..<n.len:
  1326. optNL(g)
  1327. gsub(g, n[i], c)
  1328. gcoms(g)
  1329. dedent(g)
  1330. putNL(g)
  1331. of nkOfInherit:
  1332. putWithSpace(g, tkOf, "of")
  1333. gsub(g, n, 0)
  1334. of nkProcTy:
  1335. if n.len > 0:
  1336. putWithSpace(g, tkProc, "proc")
  1337. gsub(g, n, 0)
  1338. gsub(g, n, 1)
  1339. else:
  1340. put(g, tkProc, "proc")
  1341. of nkIteratorTy:
  1342. if n.len > 0:
  1343. putWithSpace(g, tkIterator, "iterator")
  1344. gsub(g, n, 0)
  1345. gsub(g, n, 1)
  1346. else:
  1347. put(g, tkIterator, "iterator")
  1348. of nkStaticTy:
  1349. put(g, tkStatic, "static")
  1350. put(g, tkBracketLe, "[")
  1351. if n.len > 0:
  1352. gsub(g, n[0])
  1353. put(g, tkBracketRi, "]")
  1354. of nkEnumTy:
  1355. if n.len > 0:
  1356. putWithSpace(g, tkEnum, "enum")
  1357. gsub(g, n[0])
  1358. gcoms(g)
  1359. indentNL(g)
  1360. gcommaAux(g, n, g.indent, 1)
  1361. gcoms(g) # BUGFIX: comment for the last enum field
  1362. dedent(g)
  1363. else:
  1364. put(g, tkEnum, "enum")
  1365. of nkEnumFieldDef:
  1366. gsub(g, n, 0)
  1367. put(g, tkSpaces, Space)
  1368. putWithSpace(g, tkEquals, "=")
  1369. gsub(g, n, 1)
  1370. of nkStmtList, nkStmtListExpr, nkStmtListType:
  1371. if n.len == 1 and n[0].kind == nkDiscardStmt:
  1372. put(g, tkParLe, "(")
  1373. gsub(g, n[0])
  1374. put(g, tkParRi, ")")
  1375. else:
  1376. gstmts(g, n, emptyContext)
  1377. of nkIfStmt:
  1378. putWithSpace(g, tkIf, "if")
  1379. gif(g, n)
  1380. of nkWhen, nkRecWhen:
  1381. putWithSpace(g, tkWhen, "when")
  1382. gif(g, n)
  1383. of nkWhileStmt: gwhile(g, n)
  1384. of nkPragmaBlock: gpragmaBlock(g, n)
  1385. of nkCaseStmt, nkRecCase: gcase(g, n)
  1386. of nkTryStmt, nkHiddenTryStmt: gtry(g, n)
  1387. of nkForStmt, nkParForStmt: gfor(g, n)
  1388. of nkBlockStmt, nkBlockExpr: gblock(g, n)
  1389. of nkStaticStmt: gstaticStmt(g, n)
  1390. of nkAsmStmt: gasm(g, n)
  1391. of nkProcDef:
  1392. if renderNoProcDefs notin g.flags: putWithSpace(g, tkProc, "proc")
  1393. gproc(g, n)
  1394. of nkFuncDef:
  1395. if renderNoProcDefs notin g.flags: putWithSpace(g, tkFunc, "func")
  1396. gproc(g, n)
  1397. of nkConverterDef:
  1398. if renderNoProcDefs notin g.flags: putWithSpace(g, tkConverter, "converter")
  1399. gproc(g, n)
  1400. of nkMethodDef:
  1401. if renderNoProcDefs notin g.flags: putWithSpace(g, tkMethod, "method")
  1402. gproc(g, n)
  1403. of nkIteratorDef:
  1404. if renderNoProcDefs notin g.flags: putWithSpace(g, tkIterator, "iterator")
  1405. gproc(g, n)
  1406. of nkMacroDef:
  1407. if renderNoProcDefs notin g.flags: putWithSpace(g, tkMacro, "macro")
  1408. gproc(g, n)
  1409. of nkTemplateDef:
  1410. if renderNoProcDefs notin g.flags: putWithSpace(g, tkTemplate, "template")
  1411. gproc(g, n)
  1412. of nkTypeSection:
  1413. gsection(g, n, emptyContext, tkType, "type")
  1414. of nkConstSection:
  1415. initContext(a)
  1416. incl(a.flags, rfInConstExpr)
  1417. gsection(g, n, a, tkConst, "const")
  1418. of nkVarSection, nkLetSection, nkUsingStmt:
  1419. if n.len == 0: return
  1420. if n.kind == nkVarSection: putWithSpace(g, tkVar, "var")
  1421. elif n.kind == nkLetSection: putWithSpace(g, tkLet, "let")
  1422. else: putWithSpace(g, tkUsing, "using")
  1423. if n.len > 1:
  1424. gcoms(g)
  1425. indentNL(g)
  1426. for i in 0..<n.len:
  1427. optNL(g)
  1428. gsub(g, n[i])
  1429. gcoms(g)
  1430. dedent(g)
  1431. else:
  1432. gsub(g, n[0])
  1433. of nkReturnStmt:
  1434. putWithSpace(g, tkReturn, "return")
  1435. if n.len > 0 and n[0].kind == nkAsgn:
  1436. gsub(g, n[0], 1)
  1437. else:
  1438. gsub(g, n, 0)
  1439. of nkRaiseStmt:
  1440. putWithSpace(g, tkRaise, "raise")
  1441. gsub(g, n, 0)
  1442. of nkYieldStmt:
  1443. putWithSpace(g, tkYield, "yield")
  1444. gsub(g, n, 0)
  1445. of nkDiscardStmt:
  1446. putWithSpace(g, tkDiscard, "discard")
  1447. gsub(g, n, 0)
  1448. of nkBreakStmt:
  1449. putWithSpace(g, tkBreak, "break")
  1450. gsub(g, n, 0)
  1451. of nkContinueStmt:
  1452. putWithSpace(g, tkContinue, "continue")
  1453. gsub(g, n, 0)
  1454. of nkPragma:
  1455. if g.inPragma <= 0:
  1456. inc g.inPragma
  1457. #if not previousNL(g):
  1458. put(g, tkSpaces, Space)
  1459. put(g, tkCurlyDotLe, "{.")
  1460. gcomma(g, n, emptyContext)
  1461. put(g, tkCurlyDotRi, ".}")
  1462. dec g.inPragma
  1463. else:
  1464. gcomma(g, n, emptyContext)
  1465. of nkImportStmt, nkExportStmt:
  1466. if n.kind == nkImportStmt:
  1467. putWithSpace(g, tkImport, "import")
  1468. else:
  1469. putWithSpace(g, tkExport, "export")
  1470. gcoms(g)
  1471. indentNL(g)
  1472. gcommaAux(g, n, g.indent)
  1473. dedent(g)
  1474. putNL(g)
  1475. of nkImportExceptStmt, nkExportExceptStmt:
  1476. if n.kind == nkImportExceptStmt:
  1477. putWithSpace(g, tkImport, "import")
  1478. else:
  1479. putWithSpace(g, tkExport, "export")
  1480. gsub(g, n, 0)
  1481. put(g, tkSpaces, Space)
  1482. putWithSpace(g, tkExcept, "except")
  1483. gcommaAux(g, n, g.indent, 1)
  1484. gcoms(g)
  1485. putNL(g)
  1486. of nkFromStmt:
  1487. putWithSpace(g, tkFrom, "from")
  1488. gsub(g, n, 0)
  1489. put(g, tkSpaces, Space)
  1490. putWithSpace(g, tkImport, "import")
  1491. gcomma(g, n, emptyContext, 1)
  1492. putNL(g)
  1493. of nkIncludeStmt:
  1494. putWithSpace(g, tkInclude, "include")
  1495. gcoms(g)
  1496. indentNL(g)
  1497. gcommaAux(g, n, g.indent)
  1498. dedent(g)
  1499. putNL(g)
  1500. of nkCommentStmt:
  1501. gcoms(g)
  1502. optNL(g)
  1503. of nkOfBranch:
  1504. optNL(g)
  1505. putWithSpace(g, tkOf, "of")
  1506. gcomma(g, n, c, 0, - 2)
  1507. putWithSpace(g, tkColon, ":")
  1508. gcoms(g)
  1509. gstmts(g, lastSon(n), c)
  1510. of nkImportAs:
  1511. gsub(g, n, 0)
  1512. put(g, tkSpaces, Space)
  1513. putWithSpace(g, tkAs, "as")
  1514. gsub(g, n, 1)
  1515. of nkBindStmt:
  1516. putWithSpace(g, tkBind, "bind")
  1517. gcomma(g, n, c)
  1518. of nkMixinStmt:
  1519. putWithSpace(g, tkMixin, "mixin")
  1520. gcomma(g, n, c)
  1521. of nkElifBranch:
  1522. optNL(g)
  1523. putWithSpace(g, tkElif, "elif")
  1524. gsub(g, n, 0)
  1525. putWithSpace(g, tkColon, ":")
  1526. gcoms(g)
  1527. gstmts(g, n[1], c)
  1528. of nkElse:
  1529. optNL(g)
  1530. put(g, tkElse, "else")
  1531. putWithSpace(g, tkColon, ":")
  1532. gcoms(g)
  1533. gstmts(g, n[0], c)
  1534. of nkFinally, nkDefer:
  1535. optNL(g)
  1536. if n.kind == nkFinally:
  1537. put(g, tkFinally, "finally")
  1538. else:
  1539. put(g, tkDefer, "defer")
  1540. putWithSpace(g, tkColon, ":")
  1541. gcoms(g)
  1542. gstmts(g, n[0], c)
  1543. of nkExceptBranch:
  1544. optNL(g)
  1545. if n.len != 1:
  1546. putWithSpace(g, tkExcept, "except")
  1547. else:
  1548. put(g, tkExcept, "except")
  1549. gcomma(g, n, 0, -2)
  1550. putWithSpace(g, tkColon, ":")
  1551. gcoms(g)
  1552. gstmts(g, lastSon(n), c)
  1553. of nkGenericParams:
  1554. proc hasExplicitParams(gp: PNode): bool =
  1555. for p in gp:
  1556. if p.typ == nil or tfImplicitTypeParam notin p.typ.flags:
  1557. return true
  1558. return false
  1559. if n.hasExplicitParams:
  1560. put(g, tkBracketLe, "[")
  1561. gsemicolon(g, n)
  1562. put(g, tkBracketRi, "]")
  1563. of nkFormalParams:
  1564. put(g, tkParLe, "(")
  1565. gsemicolon(g, n, 1)
  1566. put(g, tkParRi, ")")
  1567. if n.len > 0 and n[0].kind != nkEmpty:
  1568. putWithSpace(g, tkColon, ":")
  1569. gsub(g, n[0])
  1570. of nkTupleTy:
  1571. put(g, tkTuple, "tuple")
  1572. put(g, tkBracketLe, "[")
  1573. gcomma(g, n)
  1574. put(g, tkBracketRi, "]")
  1575. of nkTupleClassTy:
  1576. put(g, tkTuple, "tuple")
  1577. of nkComesFrom:
  1578. put(g, tkParLe, "(ComesFrom|")
  1579. gsub(g, n, 0)
  1580. put(g, tkParRi, ")")
  1581. of nkGotoState:
  1582. var c: TContext
  1583. initContext c
  1584. putWithSpace g, tkSymbol, "goto"
  1585. gsons(g, n, c)
  1586. of nkState:
  1587. var c: TContext
  1588. initContext c
  1589. putWithSpace g, tkSymbol, "state"
  1590. gsub(g, n[0], c)
  1591. putWithSpace(g, tkColon, ":")
  1592. indentNL(g)
  1593. gsons(g, n, c, 1)
  1594. dedent(g)
  1595. of nkBreakState:
  1596. put(g, tkTuple, "breakstate")
  1597. if renderIds in g.flags:
  1598. gsons(g, n, c, 0)
  1599. of nkTypeClassTy:
  1600. gTypeClassTy(g, n)
  1601. of nkError:
  1602. putWithSpace(g, tkSymbol, "error")
  1603. #gcomma(g, n, c)
  1604. gsub(g, n[0], c)
  1605. else:
  1606. #nkNone, nkExplicitTypeListCall:
  1607. internalError(g.config, n.info, "renderer.gsub(" & $n.kind & ')')
  1608. proc renderTree*(n: PNode, renderFlags: TRenderFlags = {}): string =
  1609. if n == nil: return "<nil tree>"
  1610. var g: TSrcGen
  1611. initSrcGen(g, renderFlags, newPartialConfigRef())
  1612. # do not indent the initial statement list so that
  1613. # writeFile("file.nim", repr n)
  1614. # produces working Nim code:
  1615. if n.kind in {nkStmtList, nkStmtListExpr, nkStmtListType}:
  1616. gstmts(g, n, emptyContext, doIndent = false)
  1617. else:
  1618. gsub(g, n)
  1619. result = g.buf
  1620. proc `$`*(n: PNode): string = n.renderTree
  1621. proc renderModule*(n: PNode, outfile: string,
  1622. renderFlags: TRenderFlags = {};
  1623. fid = FileIndex(-1);
  1624. conf: ConfigRef = nil) =
  1625. var
  1626. f: File
  1627. g: TSrcGen
  1628. initSrcGen(g, renderFlags, conf)
  1629. g.fid = fid
  1630. for i in 0..<n.len:
  1631. gsub(g, n[i])
  1632. optNL(g)
  1633. case n[i].kind
  1634. of nkTypeSection, nkConstSection, nkVarSection, nkLetSection,
  1635. nkCommentStmt: putNL(g)
  1636. else: discard
  1637. gcoms(g)
  1638. if open(f, outfile, fmWrite):
  1639. write(f, g.buf)
  1640. close(f)
  1641. else:
  1642. rawMessage(g.config, errGenerated, "cannot open file: " & outfile)
  1643. proc initTokRender*(r: var TSrcGen, n: PNode, renderFlags: TRenderFlags = {}) =
  1644. initSrcGen(r, renderFlags, newPartialConfigRef())
  1645. gsub(r, n)
  1646. proc getNextTok*(r: var TSrcGen, kind: var TokType, literal: var string) =
  1647. if r.idx < r.tokens.len:
  1648. kind = r.tokens[r.idx].kind
  1649. let length = r.tokens[r.idx].length.int
  1650. literal = substr(r.buf, r.pos, r.pos + length - 1)
  1651. inc(r.pos, length)
  1652. inc(r.idx)
  1653. else:
  1654. kind = tkEof
  1655. proc getTokSym*(r: TSrcGen): PSym =
  1656. if r.idx > 0 and r.idx <= r.tokens.len:
  1657. result = r.tokens[r.idx-1].sym
  1658. else:
  1659. result = nil