renderer.nim 56 KB

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