ast.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package unstable
  2. import (
  3. "fmt"
  4. "unsafe"
  5. "github.com/pelletier/go-toml/v2/internal/danger"
  6. )
  7. // Iterator over a sequence of nodes.
  8. //
  9. // Starts uninitialized, you need to call Next() first.
  10. //
  11. // For example:
  12. //
  13. // it := n.Children()
  14. // for it.Next() {
  15. // n := it.Node()
  16. // // do something with n
  17. // }
  18. type Iterator struct {
  19. started bool
  20. node *Node
  21. }
  22. // Next moves the iterator forward and returns true if points to a
  23. // node, false otherwise.
  24. func (c *Iterator) Next() bool {
  25. if !c.started {
  26. c.started = true
  27. } else if c.node.Valid() {
  28. c.node = c.node.Next()
  29. }
  30. return c.node.Valid()
  31. }
  32. // IsLast returns true if the current node of the iterator is the last
  33. // one. Subsequent calls to Next() will return false.
  34. func (c *Iterator) IsLast() bool {
  35. return c.node.next == 0
  36. }
  37. // Node returns a pointer to the node pointed at by the iterator.
  38. func (c *Iterator) Node() *Node {
  39. return c.node
  40. }
  41. // Node in a TOML expression AST.
  42. //
  43. // Depending on Kind, its sequence of children should be interpreted
  44. // differently.
  45. //
  46. // - Array have one child per element in the array.
  47. // - InlineTable have one child per key-value in the table (each of kind
  48. // InlineTable).
  49. // - KeyValue have at least two children. The first one is the value. The rest
  50. // make a potentially dotted key.
  51. // - Table and ArrayTable's children represent a dotted key (same as
  52. // KeyValue, but without the first node being the value).
  53. //
  54. // When relevant, Raw describes the range of bytes this node is referring to in
  55. // the input document. Use Parser.Raw() to retrieve the actual bytes.
  56. type Node struct {
  57. Kind Kind
  58. Raw Range // Raw bytes from the input.
  59. Data []byte // Node value (either allocated or referencing the input).
  60. // References to other nodes, as offsets in the backing array
  61. // from this node. References can go backward, so those can be
  62. // negative.
  63. next int // 0 if last element
  64. child int // 0 if no child
  65. }
  66. // Range of bytes in the document.
  67. type Range struct {
  68. Offset uint32
  69. Length uint32
  70. }
  71. // Next returns a pointer to the next node, or nil if there is no next node.
  72. func (n *Node) Next() *Node {
  73. if n.next == 0 {
  74. return nil
  75. }
  76. ptr := unsafe.Pointer(n)
  77. size := unsafe.Sizeof(Node{})
  78. return (*Node)(danger.Stride(ptr, size, n.next))
  79. }
  80. // Child returns a pointer to the first child node of this node. Other children
  81. // can be accessed calling Next on the first child. Returns an nil if this Node
  82. // has no child.
  83. func (n *Node) Child() *Node {
  84. if n.child == 0 {
  85. return nil
  86. }
  87. ptr := unsafe.Pointer(n)
  88. size := unsafe.Sizeof(Node{})
  89. return (*Node)(danger.Stride(ptr, size, n.child))
  90. }
  91. // Valid returns true if the node's kind is set (not to Invalid).
  92. func (n *Node) Valid() bool {
  93. return n != nil
  94. }
  95. // Key returns the children nodes making the Key on a supported node. Panics
  96. // otherwise. They are guaranteed to be all be of the Kind Key. A simple key
  97. // would return just one element.
  98. func (n *Node) Key() Iterator {
  99. switch n.Kind {
  100. case KeyValue:
  101. value := n.Child()
  102. if !value.Valid() {
  103. panic(fmt.Errorf("KeyValue should have at least two children"))
  104. }
  105. return Iterator{node: value.Next()}
  106. case Table, ArrayTable:
  107. return Iterator{node: n.Child()}
  108. default:
  109. panic(fmt.Errorf("Key() is not supported on a %s", n.Kind))
  110. }
  111. }
  112. // Value returns a pointer to the value node of a KeyValue.
  113. // Guaranteed to be non-nil. Panics if not called on a KeyValue node,
  114. // or if the Children are malformed.
  115. func (n *Node) Value() *Node {
  116. return n.Child()
  117. }
  118. // Children returns an iterator over a node's children.
  119. func (n *Node) Children() Iterator {
  120. return Iterator{node: n.Child()}
  121. }