renderer.nim 54 KB

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