topt_cursor2.nim 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. discard """
  2. output: '''emptyemptyempty'''
  3. cmd: '''nim c --gc:arc $file'''
  4. """
  5. # bug #15039
  6. import lists
  7. type
  8. Token = ref object of RootObj
  9. children: DoublyLinkedList[Token]
  10. Paragraph = ref object of Token
  11. method `$`(token: Token): string {.base.} =
  12. result = "empty"
  13. method `$`(token: Paragraph): string =
  14. if token.children.head == nil:
  15. result = ""
  16. else:
  17. for c in token.children:
  18. result.add $c
  19. proc parseLeafBlockInlines(token: Token) =
  20. token.children.append(Token())
  21. token.children.append(Token()) # <-- this one AAA
  22. var emNode = newDoublyLinkedNode(Token())
  23. var i = 0
  24. var it = token.children.head
  25. while it != nil:
  26. var nxt = it.next # this is not a cursor, it takes over ownership.
  27. var childNode = it
  28. if i == 0:
  29. childNode.next = emNode # frees the object allocated in line 29 marked with AAA
  30. elif i == 1:
  31. emNode.next = childNode #
  32. it = nxt # incref on freed data, 'nxt' is freed
  33. inc i
  34. proc parse() =
  35. var token = Token()
  36. token.children.append Paragraph()
  37. parseLeafBlockInlines(token.children.head.value)
  38. for children in token.children:
  39. echo children
  40. parse()