ast.go 3.5 KB

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