renderer.nim 56 KB

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