package jx

import (
	
)

// Elem skips to the start of next array element, returning true boolean
// if element exists.
//
// Can be called before or in Array.
func ( *Decoder) () ( bool,  error) {
	,  := .next()
	if  != nil {
		return false, 
	}
	switch  {
	case '[':
		,  := .more()
		if  != nil {
			return false, 
		}
		if  != ']' {
			.unread()
			return true, nil
		}
		return false, nil
	case ']':
		return false, nil
	case ',':
		return true, nil
	default:
		return false, errors.Wrap(badToken(, .offset()), `"[", "," or "]" expected`)
	}
}

// Arr decodes array and invokes callback on each array element.
func ( *Decoder) ( func( *Decoder) error) error {
	if  := .consume('[');  != nil {
		return errors.Wrap(, `"[" expected`)
	}
	if  == nil {
		return .skipArr()
	}
	if  := .incDepth();  != nil {
		return 
	}
	,  := .more()
	if  != nil {
		return errors.Wrap(, `value or "]" expected`)
	}
	if  == ']' {
		return .decDepth()
	}
	.unread()
	if  := ();  != nil {
		return errors.Wrap(, "callback")
	}

	,  = .more()
	if  != nil {
		return errors.Wrap(, `"," or "]" expected`)
	}
	for  == ',' {
		// Skip whitespace before reading element.
		if ,  := .next();  != nil {
			return 
		}
		.unread()
		if  := ();  != nil {
			return errors.Wrap(, "callback")
		}
		if ,  = .next();  != nil {
			return 
		}
	}
	if  != ']' {
		 := badToken(, .offset()-1)
		return errors.Wrap(, `"]" expected`)
	}
	return .decDepth()
}