func NewParserError(highlight []byte, format string, args ...interface{}) error
NewParserError is a convenience function to create a ParserError
Warning: Highlight needs to be a subslice of Parser.data, so only slices returned by Parser.Raw are valid candidates.
Iterator over a sequence of nodes.
Starts uninitialized, you need to call Next() first.
For example:
it := n.Children() for it.Next() { n := it.Node() // do something with n }
type Iterator struct {
// contains filtered or unexported fields
}
func (c *Iterator) IsLast() bool
IsLast returns true if the current node of the iterator is the last one. Subsequent calls to Next() will return false.
func (c *Iterator) Next() bool
Next moves the iterator forward and returns true if points to a node, false otherwise.
func (c *Iterator) Node() *Node
Node returns a pointer to the node pointed at by the iterator.
Kind represents the type of TOML structure contained in a given Node.
type Kind int
const ( // Meta Invalid Kind = iota Comment Key // Top level structures Table ArrayTable KeyValue // Containers values Array InlineTable // Values String Bool Float Integer LocalDate LocalTime LocalDateTime DateTime )
func (k Kind) String() string
String implementation of fmt.Stringer.
Node in a TOML expression AST.
Depending on Kind, its sequence of children should be interpreted differently.
When relevant, Raw describes the range of bytes this node is referring to in the input document. Use Parser.Raw() to retrieve the actual bytes.
type Node struct { Kind Kind Raw Range // Raw bytes from the input. Data []byte // Node value (either allocated or referencing the input). // contains filtered or unexported fields }
func (n *Node) Child() *Node
Child returns a pointer to the first child node of this node. Other children can be accessed calling Next on the first child. Returns an nil if this Node has no child.
func (n *Node) Children() Iterator
Children returns an iterator over a node's children.
func (n *Node) Key() Iterator
Key returns the children 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) Next() *Node
Next returns a pointer to the next node, or nil if there is no next node.
func (n *Node) Valid() bool
Valid returns true if the node's kind is set (not to Invalid).
func (n *Node) Value() *Node
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.
Parser scans over a TOML-encoded document and generates an iterative AST.
To prime the Parser, first reset it with the contents of a TOML document. Then, process all top-level expressions sequentially. See Example.
Don't forget to check Error() after you're done parsing.
Each top-level expression needs to be fully processed before calling NextExpression() again. Otherwise, calls to various Node methods may panic if the parser has moved on the next expression.
For performance reasons, go-toml doesn't make a copy of the input bytes to the parser. Make sure to copy all the bytes you need to outlive the slice given to the parser.
type Parser struct { KeepComments bool // contains filtered or unexported fields }
▹ Example
▹ Example (Comments)
func (p *Parser) Data() []byte
Data returns the slice provided to the last call to Reset.
func (p *Parser) Error() error
Error returns any error that has occurred during parsing.
func (p *Parser) Expression() *Node
Expression returns a pointer to the node representing the last successfully parsed expression.
func (p *Parser) NextExpression() bool
NextExpression parses the next top-level expression. If an expression was successfully parsed, it returns true. If the parser is at the end of the document or an error occurred, it returns false.
Retrieve the parsed expression with Expression().
func (p *Parser) Range(b []byte) Range
Range returns a range description that corresponds to a given slice of the input. If the argument is not a subslice of the parser input, this function panics.
func (p *Parser) Raw(raw Range) []byte
Raw returns the slice corresponding to the bytes in the given range.
func (p *Parser) Reset(b []byte)
Reset brings the parser to its initial state for a given input. It wipes an reuses internal storage to reduce allocation.
func (p *Parser) Shape(r Range) Shape
Shape returns the shape of the given range in the input. Will panic if the range is not a subslice of the input.
ParserError describes an error relative to the content of the document.
It cannot outlive the instance of Parser it refers to, and may cause panics if the parser is reset.
type ParserError struct { Highlight []byte Message string Key []string // optional }
func (e *ParserError) Error() string
Error is the implementation of the error interface.
Position describes a position in the input.
type Position struct { // Number of bytes from the beginning of the input. Offset int // Line number, starting at 1. Line int // Column number, starting at 1. Column int }
Range of bytes in the document.
type Range struct { Offset uint32 Length uint32 }
Shape describes the position of a range in the input.
type Shape struct { Start Position End Position }