Documentation ¶
Overview ¶
Package unstable provides APIs that do not meet the backward compatibility guarantees yet.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewParserError ¶
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.
Types ¶
type Iterator ¶
type Iterator struct {
// contains filtered or unexported fields
}
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 }
func (*Iterator) IsLast ¶
IsLast returns true if the current node of the iterator is the last one. Subsequent calls to Next() will return false.
type Node ¶
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 }
Node in a TOML expression AST.
Depending on Kind, its sequence of children should be interpreted differently.
- Array have one child per element in the array.
- InlineTable have one child per key-value in the table (each of kind InlineTable).
- KeyValue have at least two children. The first one is the value. The rest make a potentially dotted key.
- Table and ArrayTable's children represent a dotted key (same as KeyValue, but without the first node being the value).
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.
func (*Node) Child ¶
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 (*Node) Key ¶
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.
type Parser ¶
type Parser struct {
// contains filtered or unexported fields
}
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.
The parser doesn't provide nodes for comments yet, nor for whitespace.
Example ¶
doc := ` hello = "world" value = 42 ` p := Parser{} p.Reset([]byte(doc)) for p.NextExpression() { e := p.Expression() fmt.Printf("Expression: %s\n", e.Kind) value := e.Value() it := e.Key() k := it.Node() // shortcut: we know there is no dotted key in the example fmt.Printf("%s -> (%s) %s\n", k.Data, value.Kind, value.Data) }
Output: Expression: KeyValue hello -> (String) world Expression: KeyValue value -> (Integer) 42
func (*Parser) Expression ¶
Expression returns a pointer to the node representing the last successfully parsed expression.
func (*Parser) NextExpression ¶
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 (*Parser) 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.
type ParserError ¶
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.
func (*ParserError) Error ¶
func (e *ParserError) Error() string
Error is the implementation of the error interface.