typesrenderer.nim 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 == 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. else: result = ""
  78. proc renderParamTypes(found: var seq[string], n: PNode) =
  79. ## Recursive helper, adds to `found` any types, or keeps diving the AST.
  80. ##
  81. ## The normal `doc` generator doesn't include .typ information, so the
  82. ## function won't render types for parameters with default values. The `doc2`
  83. ## generator does include the information.
  84. case n.kind
  85. of nkFormalParams:
  86. for i in 1..<n.len: renderParamTypes(found, n[i])
  87. of nkIdentDefs:
  88. # These are parameter names + type + default value node.
  89. let typePos = n.len - 2
  90. assert typePos > 0
  91. var typeStr = renderType(n[typePos])
  92. if typeStr.len < 1 and n[typePos+1].kind != nkEmpty:
  93. # Try with the last node, maybe its a default value.
  94. let typ = n[typePos+1].typ
  95. if not typ.isNil: typeStr = typeToString(typ, preferExported)
  96. if typeStr.len < 1: return
  97. for i in 0..<typePos:
  98. found.add(typeStr)
  99. else:
  100. found.add($n)
  101. #internalError(n.info, "renderParamTypes(found,n) with " & $n.kind)
  102. proc renderParamTypes*(n: PNode, sep = defaultParamSeparator): string =
  103. ## Returns the types contained in `n` joined by `sep`.
  104. ##
  105. ## This proc expects to be passed as `n` the parameters of any callable. The
  106. ## string output is meant for the HTML renderer. If there are no parameters,
  107. ## the empty string is returned. The parameters will be joined by `sep` but
  108. ## other characters may appear too, like ``[]`` or ``|``.
  109. result = ""
  110. var found: seq[string] = @[]
  111. renderParamTypes(found, n)
  112. if found.len > 0:
  113. result = found.join(sep)