package goabnf

import (
	
	
	
	
)

var (
	// ErrNoSolutionFound is an error returned when parsing an ABNF
	// grammar and no solution has been found.
	//
	// On the ABNF parsing path it is wrapped by a [*ParseError] carrying the
	// position at which parsing got stuck; errors.Is(err, ErrNoSolutionFound)
	// keeps matching.
	ErrNoSolutionFound = errors.New("no solution found, input ABNF grammar may be invalid")

	// ErrHandlingProseVal is an error returned when an operation tried
	// to produce something on a prose-val, but it can't be handled properly.
	ErrHandlingProseVal = errors.New("can't handle prose-val descriptions")

	// ErrProseValInTransitionGraph is returned when building a transition graph
	// from a grammar that contains a prose-val, which has no defined expansion.
	ErrProseValInTransitionGraph = errors.New("prose value is not supported in transition graphs")

	// ErrRepetitionThreshold is returned when expanding a repetition whose bound
	// exceeds the threshold set with WithRepetitionThreshold.
	ErrRepetitionThreshold = errors.New("repetition threshold reached")

	// ErrRepetitionMinNotOne is returned when a mandatory (max == 1) repetition
	// has a minimum other than 1.
	ErrRepetitionMinNotOne = errors.New("repetition minimum must be 1 when maximum is 1")

	// ErrRepetitionMinGtMax is returned when a repetition has a minimum greater
	// than its maximum.
	ErrRepetitionMinGtMax = errors.New("repetition minimum is greater than maximum")
)

// ParseError reports where parsing input against a grammar got stuck. It is
// returned (wrapped around [ErrNoSolutionFound]) when an input does not parse,
// to help locate the offending construct -- in particular when writing or
// debugging an ABNF grammar.
type ParseError struct {
	// Offset is the byte offset into the input of the furthest position the
	// parser was able to reach before failing.
	Offset int
	// Line and Col are the 1-based line and column of Offset.
	Line, Col int
	// Expected lists the terminals the parser could have consumed at Offset, if
	// that information was available. It may be empty.
	Expected []string
	// Found describes the input byte at Offset (e.g. `%x0A (LF)`), or "EOF" when
	// Offset is at the end of input. It may be empty.
	Found string
	// Hint, when set, gives a plain-language diagnosis of a common mistake (e.g.
	// bare LF line endings where ABNF requires CR LF).
	Hint string
}

var _ error = (*ParseError)(nil)

func ( *ParseError) () string {
	var  strings.Builder
	fmt.Fprintf(&, "parse error at line %d, column %d (offset %d)", .Line, .Col, .Offset)
	switch {
	case len(.Expected) > 0:
		fmt.Fprintf(&, ": expected %s", strings.Join(.Expected, ", "))
		if .Found != "" {
			fmt.Fprintf(&, ", found %s", .Found)
		}
	case .Found != "":
		fmt.Fprintf(&, ": unexpected %s", .Found)
	default:
		fmt.Fprintf(&, ": %s", ErrNoSolutionFound)
	}
	if .Hint != "" {
		fmt.Fprintf(&, " (hint: %s)", .Hint)
	}
	return .String()
}

// Unwrap reports ErrNoSolutionFound so that callers matching the historical
// sentinel with errors.Is keep working.
func ( *ParseError) () error { return ErrNoSolutionFound }

// describeByte renders an input byte the way ABNF terminals are written, naming
// the control characters that matter most when debugging a grammar.
func describeByte( byte) string {
	switch  {
	case '\r':
		return "%x0D (CR)"
	case '\n':
		return "%x0A (LF)"
	case '\t':
		return "%x09 (HTAB)"
	case ' ':
		return "%x20 (SP)"
	}
	if  >= 0x20 &&  < 0x7f {
		return fmt.Sprintf("%q", string())
	}
	return fmt.Sprintf("%%x%02X", )
}

// newParseError builds a ParseError from a byte offset into input, computing the
// 1-based line and column, the byte found there, and a hint for the bare-LF
// line-ending mistake (the most common ABNF authoring error).
func newParseError( []byte,  int,  []string) *ParseError {
	if  < 0 {
		 = 0
	}
	if  > len() {
		 = len()
	}
	,  := 1, 1
	for  := 0;  < ; ++ {
		if [] == '\n' {
			++
			 = 1
		} else {
			++
		}
	}
	 := append([]string(nil), ...)
	sort.Strings()
	 := &ParseError{Offset: , Line: , Col: , Expected: }
	if  < len() {
		.Found = describeByte([])
		// Bare LF where the previous byte is not CR: ABNF lines end with CR LF,
		// so a lone LF is almost always the cause.
		if [] == '\n' && ( == 0 || [-1] != '\r') {
			.Hint = `input uses bare LF line endings, but ABNF requires CR LF ("\r\n")`
		}
	} else {
		.Found = "EOF"
	}
	return 
}

// ErrMultipleSolutionsFound is an error returned when a parser found
// multiple solutions when none or one were expected.
type ErrMultipleSolutionsFound struct{}

var _ error = (*ErrMultipleSolutionsFound)(nil)

func ( ErrMultipleSolutionsFound) () string {
	return "multiple solutions found, this should not happen. Please open an issue. This could eventually need an Erratum from IETF tracking"
}

// ErrRuleNotFound is an error returned when the rule was not found
// as part of the grammar.
type ErrRuleNotFound struct {
	Rulename string
}

var _ error = (*ErrRuleNotFound)(nil)

func ( ErrRuleNotFound) () string {
	return fmt.Sprintf("rule %s was not found in grammar", .Rulename)
}

// ErrCoreRuleModify is an error returned when an incremental alternative
// for a core rule.
type ErrCoreRuleModify struct {
	CoreRulename string
}

var _ error = (*ErrCoreRuleModify)(nil)

func ( ErrCoreRuleModify) () string {
	return fmt.Sprintf("core rule %s can't be modified", .CoreRulename)
}

// ErrDependencyNotFound is an error returned during ABNF grammar
// semantic vaildation, if a rule depends on an unexisting rule.
type ErrDependencyNotFound struct {
	Rulename string
}

var _ error = (*ErrDependencyNotFound)(nil)

func ( ErrDependencyNotFound) () string {
	return fmt.Sprintf("unsatisfied dependency (rule) %s", .Rulename)
}

// ErrSemanticRepetition is an error returned during ABNF grammar
// semantic validation, if a repetition has min < max.
type ErrSemanticRepetition struct {
	Repetition Repetition
}

var _ error = (*ErrSemanticRepetition)(nil)

func ( ErrSemanticRepetition) () string {
	return fmt.Sprintf("invalid semantic of input ABNF grammar for repetition %s", .Repetition)
}

// ErrTooLargeNumeral is an error returned when the numeral value
// provided to parse cannot be handled as a 7-bit US-ASCII valid value.
type ErrTooLargeNumeral struct {
	Base, Value string
}

var _ error = (*ErrTooLargeNumeral)(nil)

func ( ErrTooLargeNumeral) () string {
	return fmt.Sprintf("too large numeral value %s for base %s", .Value, .Base)
}

// ErrDuplicatedRule is an error returned when the rule already
// exist as part of the grammar.
type ErrDuplicatedRule struct {
	Rulename string
}

var _ error = (*ErrDuplicatedRule)(nil)

func ( ErrDuplicatedRule) () string {
	return fmt.Sprintf("rule %s was already defined in grammar", .Rulename)
}

// ErrCyclicRule is an error returned when can't work due to an
// unavoidable cyclic rule.
type ErrCyclicRule struct {
	Rulename string
}

var _ error = (*ErrCyclicRule)(nil)

func ( ErrCyclicRule) () string {
	return fmt.Sprintf("can't generate a content as the rule %s involves an unavoidable cycle", .Rulename)
}

// ErrMaxNodesExceeded is returned by TransitionGraph when construction would
// allocate more nodes than the budget set with WithMaxNodes.
type ErrMaxNodesExceeded struct {
	Max int
}

var _ error = (*ErrMaxNodesExceeded)(nil)

func ( ErrMaxNodesExceeded) () string {
	return fmt.Sprintf("transition graph node budget of %d exceeded", .Max)
}