repr_v2.nim 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. proc isNamedTuple(T: typedesc): bool {.magic: "TypeTrait".}
  2. ## imported from typetraits
  3. proc distinctBase(T: typedesc): typedesc {.magic: "TypeTrait".}
  4. ## imported from typetraits
  5. proc repr*(x: NimNode): string {.magic: "Repr", noSideEffect.}
  6. proc repr*(x: int): string {.magic: "IntToStr", noSideEffect.}
  7. ## repr for an integer argument. Returns `x`
  8. ## converted to a decimal string.
  9. proc repr*(x: int64): string {.magic: "Int64ToStr", noSideEffect.}
  10. ## repr for an integer argument. Returns `x`
  11. ## converted to a decimal string.
  12. proc repr*(x: uint64): string {.noSideEffect.} =
  13. ## repr for an unsigned integer argument. Returns `x`
  14. ## converted to a decimal string.
  15. $x #Calls `$` from system/strmantle.nim
  16. proc repr*(x: float): string {.magic: "FloatToStr", noSideEffect.}
  17. ## repr for a float argument. Returns `x`
  18. ## converted to a decimal string.
  19. proc repr*(x: bool): string {.magic: "BoolToStr", noSideEffect.}
  20. ## repr for a boolean argument. Returns `x`
  21. ## converted to the string "false" or "true".
  22. proc repr*(x: char): string {.noSideEffect.} =
  23. ## repr for a character argument. Returns `x`
  24. ## converted to an escaped string.
  25. ##
  26. ## .. code-block:: Nim
  27. ## assert repr('c') == "'c'"
  28. result.add '\''
  29. # Elides string creations if not needed
  30. if x in {'\\', '\0'..'\31', '\127'..'\255'}:
  31. result.add '\\'
  32. if x in {'\0'..'\31', '\127'..'\255'}:
  33. result.add $x.uint8
  34. else:
  35. result.add x
  36. result.add '\''
  37. proc repr*(x: string | cstring): string {.noSideEffect.} =
  38. ## repr for a string argument. Returns `x`
  39. ## converted to a quoted and escaped string.
  40. result.add '\"'
  41. for i in 0..<x.len:
  42. if x[i] in {'"', '\\', '\0'..'\31', '\127'..'\255'}:
  43. result.add '\\'
  44. case x[i]:
  45. of '\n':
  46. result.add "n\n"
  47. of '\0'..'\9', '\11'..'\31', '\127'..'\255':
  48. result.add $x[i].uint8
  49. else:
  50. result.add x[i]
  51. result.add '\"'
  52. proc repr*[Enum: enum](x: Enum): string {.magic: "EnumToStr", noSideEffect.}
  53. ## repr for an enumeration argument. This works for
  54. ## any enumeration type thanks to compiler magic.
  55. ##
  56. ## If a `repr` operator for a concrete enumeration is provided, this is
  57. ## used instead. (In other words: *Overwriting* is possible.)
  58. proc repr*(p: pointer): string =
  59. ## repr of pointer as its hexadecimal value
  60. if p == nil:
  61. result = "nil"
  62. else:
  63. when nimvm:
  64. result = "ptr"
  65. else:
  66. const HexChars = "0123456789ABCDEF"
  67. const len = sizeof(pointer) * 2
  68. var n = cast[uint](p)
  69. result = newString(len)
  70. for j in countdown(len-1, 0):
  71. result[j] = HexChars[n and 0xF]
  72. n = n shr 4
  73. proc repr*(p: proc): string =
  74. ## repr of a proc as its address
  75. repr(cast[pointer](p))
  76. template repr*(x: distinct): string =
  77. repr(distinctBase(typeof(x))(x))
  78. template repr*(t: typedesc): string = $t
  79. proc reprObject[T: tuple|object](res: var string, x: T) =
  80. res.add '('
  81. var firstElement = true
  82. const isNamed = T is object or isNamedTuple(T)
  83. when not isNamed:
  84. var count = 0
  85. for name, value in fieldPairs(x):
  86. if not firstElement: res.add(", ")
  87. when isNamed:
  88. res.add(name)
  89. res.add(": ")
  90. else:
  91. count.inc
  92. res.add repr(value)
  93. firstElement = false
  94. when not isNamed:
  95. if count == 1:
  96. res.add(',') # $(1,) should print as the semantically legal (1,)
  97. res.add(')')
  98. proc repr*[T: tuple|object](x: T): string =
  99. ## Generic `repr` operator for tuples that is lifted from the components
  100. ## of `x`. Example:
  101. ##
  102. ## .. code-block:: Nim
  103. ## $(23, 45) == "(23, 45)"
  104. ## $(a: 23, b: 45) == "(a: 23, b: 45)"
  105. ## $() == "()"
  106. when T is object:
  107. result = $typeof(x)
  108. reprObject(result, x)
  109. proc repr*[T](x: ref T | ptr T): string =
  110. if isNil(x): return "nil"
  111. when T is object:
  112. result = $typeof(x)
  113. reprObject(result, x[])
  114. else:
  115. result = when typeof(x) is ref: "ref " else: "ptr "
  116. result.add repr(x[])
  117. proc collectionToRepr[T](x: T, prefix, separator, suffix: string): string =
  118. result = prefix
  119. var firstElement = true
  120. for value in items(x):
  121. if firstElement:
  122. firstElement = false
  123. else:
  124. result.add(separator)
  125. result.add repr(value)
  126. result.add(suffix)
  127. proc repr*[T](x: set[T]): string =
  128. ## Generic `repr` operator for sets that is lifted from the components
  129. ## of `x`. Example:
  130. ##
  131. ## .. code-block:: Nim
  132. ## ${23, 45} == "{23, 45}"
  133. collectionToRepr(x, "{", ", ", "}")
  134. proc repr*[T](x: seq[T]): string =
  135. ## Generic `repr` operator for seqs that is lifted from the components
  136. ## of `x`. Example:
  137. ##
  138. ## .. code-block:: Nim
  139. ## $(@[23, 45]) == "@[23, 45]"
  140. collectionToRepr(x, "@[", ", ", "]")
  141. proc repr*[T, U](x: HSlice[T, U]): string =
  142. ## Generic `repr` operator for slices that is lifted from the components
  143. ## of `x`. Example:
  144. ##
  145. ## .. code-block:: Nim
  146. ## $(1 .. 5) == "1 .. 5"
  147. result = repr(x.a)
  148. result.add(" .. ")
  149. result.add(repr(x.b))
  150. proc repr*[T, IDX](x: array[IDX, T]): string =
  151. ## Generic `repr` operator for arrays that is lifted from the components.
  152. collectionToRepr(x, "[", ", ", "]")
  153. proc repr*[T](x: openArray[T]): string =
  154. ## Generic `repr` operator for openarrays that is lifted from the components
  155. ## of `x`. Example:
  156. ##
  157. ## .. code-block:: Nim
  158. ## $(@[23, 45].toOpenArray(0, 1)) == "[23, 45]"
  159. collectionToRepr(x, "[", ", ", "]")