repr_v2.nim 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. include system/inclrtl
  2. proc isNamedTuple(T: typedesc): bool {.magic: "TypeTrait".}
  3. ## imported from typetraits
  4. proc distinctBase(T: typedesc, recursive: static bool = true): typedesc {.magic: "TypeTrait".}
  5. ## imported from typetraits
  6. proc repr*(x: NimNode): string {.magic: "Repr", noSideEffect.}
  7. proc repr*(x: int): string =
  8. ## Same as $x
  9. $x
  10. proc repr*(x: int64): string =
  11. ## Same as $x
  12. $x
  13. proc repr*(x: uint64): string {.noSideEffect.} =
  14. ## Same as $x
  15. $x
  16. proc repr*(x: float): string =
  17. ## Same as $x
  18. $x
  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 reprDiscriminant*(e: int): string {.compilerproc.} =
  59. # repr and reprjs can use `PNimType` to symbolize `e`; making this work here
  60. # would require a way to pass the set of enum stringified values to cgen.
  61. $e
  62. proc repr*(p: pointer): string =
  63. ## repr of pointer as its hexadecimal value
  64. if p == nil:
  65. result = "nil"
  66. else:
  67. when nimvm:
  68. result = "ptr"
  69. else:
  70. const HexChars = "0123456789ABCDEF"
  71. const len = sizeof(pointer) * 2
  72. var n = cast[uint](p)
  73. result = newString(len)
  74. for j in countdown(len-1, 0):
  75. result[j] = HexChars[n and 0xF]
  76. n = n shr 4
  77. proc repr*(p: proc): string =
  78. ## repr of a proc as its address
  79. repr(cast[ptr pointer](unsafeAddr p)[])
  80. template repr*(x: distinct): string =
  81. repr(distinctBase(typeof(x))(x))
  82. template repr*(t: typedesc): string = $t
  83. proc reprObject[T: tuple|object](res: var string, x: T) =
  84. res.add '('
  85. var firstElement = true
  86. const isNamed = T is object or isNamedTuple(T)
  87. when not isNamed:
  88. var count = 0
  89. for name, value in fieldPairs(x):
  90. if not firstElement: res.add(", ")
  91. when isNamed:
  92. res.add(name)
  93. res.add(": ")
  94. else:
  95. count.inc
  96. res.add repr(value)
  97. firstElement = false
  98. when not isNamed:
  99. if count == 1:
  100. res.add(',') # $(1,) should print as the semantically legal (1,)
  101. res.add(')')
  102. proc repr*[T: tuple|object](x: T): string =
  103. ## Generic `repr` operator for tuples that is lifted from the components
  104. ## of `x`. Example:
  105. ##
  106. ## .. code-block:: Nim
  107. ## $(23, 45) == "(23, 45)"
  108. ## $(a: 23, b: 45) == "(a: 23, b: 45)"
  109. ## $() == "()"
  110. when T is object:
  111. result = $typeof(x)
  112. reprObject(result, x)
  113. proc repr*[T](x: ref T | ptr T): string =
  114. if isNil(x): return "nil"
  115. when T is object:
  116. result = $typeof(x)
  117. reprObject(result, x[])
  118. else:
  119. result = when typeof(x) is ref: "ref " else: "ptr "
  120. result.add repr(x[])
  121. proc collectionToRepr[T](x: T, prefix, separator, suffix: string): string =
  122. result = prefix
  123. var firstElement = true
  124. for value in items(x):
  125. if firstElement:
  126. firstElement = false
  127. else:
  128. result.add(separator)
  129. result.add repr(value)
  130. result.add(suffix)
  131. proc repr*[T](x: set[T]): string =
  132. ## Generic `repr` operator for sets that is lifted from the components
  133. ## of `x`. Example:
  134. ##
  135. ## .. code-block:: Nim
  136. ## ${23, 45} == "{23, 45}"
  137. collectionToRepr(x, "{", ", ", "}")
  138. proc repr*[T](x: seq[T]): string =
  139. ## Generic `repr` operator for seqs that is lifted from the components
  140. ## of `x`. Example:
  141. ##
  142. ## .. code-block:: Nim
  143. ## $(@[23, 45]) == "@[23, 45]"
  144. collectionToRepr(x, "@[", ", ", "]")
  145. proc repr*[T, U](x: HSlice[T, U]): string =
  146. ## Generic `repr` operator for slices that is lifted from the components
  147. ## of `x`. Example:
  148. ##
  149. ## .. code-block:: Nim
  150. ## $(1 .. 5) == "1 .. 5"
  151. result = repr(x.a)
  152. result.add(" .. ")
  153. result.add(repr(x.b))
  154. proc repr*[T, IDX](x: array[IDX, T]): string =
  155. ## Generic `repr` operator for arrays that is lifted from the components.
  156. collectionToRepr(x, "[", ", ", "]")
  157. proc repr*[T](x: openArray[T]): string =
  158. ## Generic `repr` operator for openarrays that is lifted from the components
  159. ## of `x`. Example:
  160. ##
  161. ## .. code-block:: Nim
  162. ## $(@[23, 45].toOpenArray(0, 1)) == "[23, 45]"
  163. collectionToRepr(x, "[", ", ", "]")