package chroma

import 

// An Iterator across tokens.
//
// EOF will be returned at the end of the Token stream.
//
// If an error occurs within an Iterator, it may propagate this in a panic. Formatters should recover.
type Iterator func() Token

// Tokens consumes all tokens from the iterator and returns them as a slice.
func ( Iterator) () []Token {
	var  []Token
	for  := ();  != EOF;  = () {
		 = append(, )
	}
	return 
}

// Concaterator concatenates tokens from a series of iterators.
func ( ...Iterator) Iterator {
	return func() Token {
		for len() > 0 {
			 := [0]()
			if  != EOF {
				return 
			}
			 = [1:]
		}
		return EOF
	}
}

// Literator converts a sequence of literal Tokens into an Iterator.
func ( ...Token) Iterator {
	return func() Token {
		if len() == 0 {
			return EOF
		}
		 := [0]
		 = [1:]
		return 
	}
}

// SplitTokensIntoLines splits tokens containing newlines in two.
func ( []Token) ( [][]Token) {
	var  []Token // nolint: prealloc
	for ,  := range  {
		for strings.Contains(.Value, "\n") {
			 := strings.SplitAfterN(.Value, "\n", 2)
			// Token becomes the tail.
			.Value = [1]

			// Append the head to the line and flush the line.
			 := .Clone()
			.Value = [0]
			 = append(, )
			 = append(, )
			 = nil
		}
		 = append(, )
	}
	if len() > 0 {
		 = append(, )
	}
	// Strip empty trailing token line.
	if len() > 0 {
		 := [len()-1]
		if len() == 1 && [0].Value == "" {
			 = [:len()-1]
		}
	}
	return
}