package parse

import (
	"git.makaay.nl/mauricem/go-parsekit/parse"
)

var (
	// Keys may be either bare or quoted.
	detectKey = c.Any(bareKeyRune, a.SingleQuote, a.DoubleQuote)

	// Both [tables] and [[arrays of tables]] start with a square open bracket.
	detectTable = a.SquareOpen
)

func (t *parser) startDocument(p *parse.API) {
	for {
		switch {
		case p.Accept(whitespaceInclNewlines.Or(comment)):
			// NOOP
		case p.Peek(detectTable):
			p.Handle(t.startTable)
		case p.Peek(detectKey):
			p.Handle(t.startKeyValuePair)
		case p.Accept(a.EndOfFile):
			p.Stop()
		default:
			p.Expected("key/value pair, table or array of tables")
			return
		}
		if p.IsStoppedOrInError() {
			return
		}
	}
}
