package parse

import (
	
	

	
)

const parseErrorType = "parse error"

// Error stores multiple underlying parse errors, and can pretty print them.
type Error struct {
	Entries []*diag.Error
}

var _ diag.Shower = &Error{}

// GetError returns an *Error if the given error has dynamic type *Error, i.e.
// is returned by one of the Parse functions. Otherwise it returns nil.
func ( error) *Error {
	if ,  := .(*Error);  {
		return 
	}
	return nil
}

func ( *Error) ( string,  *diag.Context) {
	 := &diag.Error{Type: parseErrorType, Message: , Context: *}
	.Entries = append(.Entries, )
}

// Error returns a string representation of the error.
func ( *Error) () string {
	switch len(.Entries) {
	case 0:
		return "no parse error"
	case 1:
		return .Entries[0].Error()
	default:
		 := new(strings.Builder)
		// Contexts of parse error entries all have the same name
		fmt.Fprintf(, "multiple parse errors in %s: ", .Entries[0].Context.Name)
		for ,  := range .Entries {
			if  > 0 {
				fmt.Fprint(, "; ")
			}
			fmt.Fprintf(, "%d-%d: %s", .Context.From, .Context.To, .Message)
		}
		return .String()
	}
}

// Show shows the error.
func ( *Error) ( string) string {
	switch len(.Entries) {
	case 0:
		return "no parse error"
	case 1:
		return .Entries[0].Show()
	default:
		 := new(strings.Builder)
		fmt.Fprint(, "Multiple parse errors:")
		for ,  := range .Entries {
			.WriteString("\n" +  + "  ")
			fmt.Fprintf(, "\033[31;1m%s\033[m\n", .Message)
			.WriteString( + "    ")
			.WriteString(.Context.Show( + "      "))
		}
		return .String()
	}
}