618f0181ac
* Use pointers instead of copying around ast.Node Node is a 56B struct that is constantly in the hot path. Passing nodes around by copy had a cost that started to add up. This change replaces them by pointers. Using unsafe pointer arithmetic and converting sibling/child indexes to relative offsets, it removes the need to carry around a pointer to the root of the tree. This saves 8B per Node. This space will be used to store an extra []byte slice to provide contextual error handling on all nodes, including the ones whose data is different than the raw input (for example: strings with escaped characters), while staying under the size of a cache line. * Remove conditional * Add Raw to track range in data for parsed values * Simplify reference tracking
153 lines
3.7 KiB
Go
153 lines
3.7 KiB
Go
package ast
|
|
|
|
import (
|
|
"fmt"
|
|
"unsafe"
|
|
|
|
"github.com/pelletier/go-toml/v2/internal/danger"
|
|
)
|
|
|
|
// Iterator starts uninitialized, you need to call Next() first.
|
|
//
|
|
// For example:
|
|
//
|
|
// it := n.Children()
|
|
// for it.Next() {
|
|
// it.Node()
|
|
// }
|
|
type Iterator struct {
|
|
started bool
|
|
node *Node
|
|
}
|
|
|
|
// Next moves the iterator forward and returns true if points to a node, false
|
|
// otherwise.
|
|
func (c *Iterator) Next() bool {
|
|
if !c.started {
|
|
c.started = true
|
|
} else if c.node.Valid() {
|
|
c.node = c.node.Next()
|
|
}
|
|
return c.node.Valid()
|
|
}
|
|
|
|
// IsLast returns true if the current node of the iterator is the last one.
|
|
// Subsequent call to Next() will return false.
|
|
func (c *Iterator) IsLast() bool {
|
|
return c.node.next == 0
|
|
}
|
|
|
|
// Node returns a copy of the node pointed at by the iterator.
|
|
func (c *Iterator) Node() *Node {
|
|
return c.node
|
|
}
|
|
|
|
// Root contains a full AST.
|
|
//
|
|
// It is immutable once constructed with Builder.
|
|
type Root struct {
|
|
nodes []Node
|
|
}
|
|
|
|
// Iterator over the top level nodes.
|
|
func (r *Root) Iterator() Iterator {
|
|
it := Iterator{}
|
|
if len(r.nodes) > 0 {
|
|
it.node = &r.nodes[0]
|
|
}
|
|
return it
|
|
}
|
|
|
|
func (r *Root) at(idx Reference) *Node {
|
|
return &r.nodes[idx]
|
|
}
|
|
|
|
// Arrays have one child per element in the array.
|
|
// InlineTables have one child per key-value pair in the table.
|
|
// KeyValues have at least two children. The first one is the value. The
|
|
// rest make a potentially dotted key.
|
|
// Table and Array table have one child per element of the key they
|
|
// represent (same as KeyValue, but without the last node being the value).
|
|
// children []Node
|
|
type Node struct {
|
|
Kind Kind
|
|
Raw Range // Raw bytes from the input.
|
|
Data []byte // Node value (could be either allocated or referencing the input).
|
|
|
|
// References to other nodes, as offsets in the backing array from this
|
|
// node. References can go backward, so those can be negative.
|
|
next int // 0 if last element
|
|
child int // 0 if no child
|
|
}
|
|
|
|
type Range struct {
|
|
Offset uint32
|
|
Length uint32
|
|
}
|
|
|
|
// Next returns a copy of the next node, or an invalid Node if there is no
|
|
// next node.
|
|
func (n *Node) Next() *Node {
|
|
if n.next == 0 {
|
|
return nil
|
|
}
|
|
ptr := unsafe.Pointer(n)
|
|
size := unsafe.Sizeof(Node{})
|
|
return (*Node)(danger.Stride(ptr, size, n.next))
|
|
}
|
|
|
|
// Child returns a copy of the first child node of this node. Other children
|
|
// can be accessed calling Next on the first child.
|
|
// Returns an invalid Node if there is none.
|
|
func (n *Node) Child() *Node {
|
|
if n.child == 0 {
|
|
return nil
|
|
}
|
|
ptr := unsafe.Pointer(n)
|
|
size := unsafe.Sizeof(Node{})
|
|
return (*Node)(danger.Stride(ptr, size, n.child))
|
|
}
|
|
|
|
// Valid returns true if the node's kind is set (not to Invalid).
|
|
func (n *Node) Valid() bool {
|
|
return n != nil
|
|
}
|
|
|
|
// Key returns the child nodes making the Key on a supported node. Panics
|
|
// otherwise.
|
|
// They are guaranteed to be all be of the Kind Key. A simple key would return
|
|
// just one element.
|
|
func (n *Node) Key() Iterator {
|
|
switch n.Kind {
|
|
case KeyValue:
|
|
value := n.Child()
|
|
if !value.Valid() {
|
|
panic(fmt.Errorf("KeyValue should have at least two children"))
|
|
}
|
|
return Iterator{node: value.Next()}
|
|
case Table, ArrayTable:
|
|
return Iterator{node: n.Child()}
|
|
default:
|
|
panic(fmt.Errorf("Key() is not supported on a %s", n.Kind))
|
|
}
|
|
}
|
|
|
|
// Value returns a pointer to the value node of a KeyValue.
|
|
// Guaranteed to be non-nil.
|
|
// Panics if not called on a KeyValue node, or if the Children are malformed.
|
|
func (n *Node) Value() *Node {
|
|
assertKind(KeyValue, *n)
|
|
return n.Child()
|
|
}
|
|
|
|
// Children returns an iterator over a node's children.
|
|
func (n *Node) Children() Iterator {
|
|
return Iterator{node: n.Child()}
|
|
}
|
|
|
|
func assertKind(k Kind, n Node) {
|
|
if n.Kind != k {
|
|
panic(fmt.Errorf("method was expecting a %s, not a %s", k, n.Kind))
|
|
}
|
|
}
|