go-toml/parser/user_api.go

40 lines
1.0 KiB
Go

package parser
// Next retrieves the next parsed item.
// When a valid item was found, then the boolean return parameter will be true.
// On error or when successfully reaching the end of the input, false is returned.
// When an error occurred, it will be set in the error return value, nil otherwise.
func (l *Parser) Next() (Item, *Error, bool) {
for {
select {
case i := <-l.items:
switch {
case i.Type == ItemEOF:
return i, nil, false
case i.Type == ItemError:
l.err = &Error{i.Value, l.cursorRow, l.cursorColumn}
return i, l.err, false
default:
l.item = i
return i, nil, true
}
default:
l.state = l.state(l)
}
}
}
// ToArray returns Parser items as an array (mainly intended for testing purposes)
// When an error occurs during scanning, a partial result will be
// returned, accompanied by the error that occurred.
func (l *Parser) ToArray() ([]Item, *Error) {
var items []Item
for {
item, err, more := l.Next()
if !more {
return items, err
}
items = append(items, item)
}
}