typesrenderer.nim 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #
  2. #
  3. # The Nim Compiler
  4. # (c) Copyright 2015 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. import renderer, strutils, ast, types
  10. const defaultParamSeparator* = ","
  11. proc renderPlainSymbolName*(n: PNode): string =
  12. ## Returns the first non '*' nkIdent node from the tree.
  13. ##
  14. ## Use this on documentation name nodes to extract the *raw* symbol name,
  15. ## without decorations, parameters, or anything. That can be used as the base
  16. ## for the HTML hyperlinks.
  17. case n.kind
  18. of nkPostfix, nkAccQuoted:
  19. result = renderPlainSymbolName(n[^1])
  20. of nkIdent:
  21. result = n.ident.s
  22. of nkSym:
  23. result = n.sym.renderDefinitionName(noQuotes = true)
  24. of nkPragmaExpr:
  25. result = renderPlainSymbolName(n[0])
  26. else:
  27. result = ""
  28. #internalError(n.info, "renderPlainSymbolName() with " & $n.kind)
  29. proc renderType(n: PNode): string =
  30. ## Returns a string with the node type or the empty string.
  31. case n.kind:
  32. of nkIdent: result = n.ident.s
  33. of nkSym: result = typeToString(n.sym.typ)
  34. of nkVarTy:
  35. if n.len == 1:
  36. result = renderType(n[0])
  37. else:
  38. result = "var"
  39. of nkRefTy:
  40. if n.len == 1:
  41. result = "ref." & renderType(n[0])
  42. else:
  43. result = "ref"
  44. of nkPtrTy:
  45. if n.len == 1:
  46. result = "ptr." & renderType(n[0])
  47. else:
  48. result = "ptr"
  49. of nkProcTy:
  50. assert n.len != 1
  51. if n.len > 1:
  52. let params = n[0]
  53. assert params.kind == nkFormalParams
  54. assert params.len > 0
  55. result = "proc("
  56. for i in 1..<params.len: result.add(renderType(params[i]) & ',')
  57. result[^1] = ')'
  58. else:
  59. result = "proc"
  60. of nkIdentDefs:
  61. assert n.len >= 3
  62. let typePos = n.len - 2
  63. let typeStr = renderType(n[typePos])
  64. result = typeStr
  65. for i in 1..<typePos:
  66. assert n[i].kind in {nkSym, nkIdent}
  67. result.add(',' & typeStr)
  68. of nkTupleTy:
  69. result = "tuple["
  70. for i in 0..<n.len: result.add(renderType(n[i]) & ',')
  71. result[^1] = ']'
  72. of nkBracketExpr:
  73. assert n.len >= 2
  74. result = renderType(n[0]) & '['
  75. for i in 1..<n.len: result.add(renderType(n[i]) & ',')
  76. result[^1] = ']'
  77. of nkCommand:
  78. result = renderType(n[0])
  79. for i in 1..<n.len:
  80. if i > 1: result.add ", "
  81. result.add(renderType(n[i]))
  82. else: result = ""
  83. proc renderParamTypes(found: var seq[string], n: PNode) =
  84. ## Recursive helper, adds to `found` any types, or keeps diving the AST.
  85. ##
  86. ## The normal `doc` generator doesn't include .typ information, so the
  87. ## function won't render types for parameters with default values. The `doc2`
  88. ## generator does include the information.
  89. case n.kind
  90. of nkFormalParams:
  91. for i in 1..<n.len: renderParamTypes(found, n[i])
  92. of nkIdentDefs:
  93. # These are parameter names + type + default value node.
  94. let typePos = n.len - 2
  95. assert typePos > 0
  96. var typeStr = renderType(n[typePos])
  97. if typeStr.len < 1 and n[typePos+1].kind != nkEmpty:
  98. # Try with the last node, maybe its a default value.
  99. let typ = n[typePos+1].typ
  100. if not typ.isNil: typeStr = typeToString(typ, preferExported)
  101. if typeStr.len < 1: return
  102. for i in 0..<typePos:
  103. found.add(typeStr)
  104. else:
  105. found.add($n)
  106. #internalError(n.info, "renderParamTypes(found,n) with " & $n.kind)
  107. proc renderParamTypes*(n: PNode, sep = defaultParamSeparator): string =
  108. ## Returns the types contained in `n` joined by `sep`.
  109. ##
  110. ## This proc expects to be passed as `n` the parameters of any callable. The
  111. ## string output is meant for the HTML renderer. If there are no parameters,
  112. ## the empty string is returned. The parameters will be joined by `sep` but
  113. ## other characters may appear too, like ``[]`` or ``|``.
  114. result = ""
  115. var found: seq[string] = @[]
  116. renderParamTypes(found, n)
  117. if found.len > 0:
  118. result = found.join(sep)